Ethereum Smart Contract

My first attempt to write a smart contract (a computer program) on Ethereum (a Blockchain).

The exercise is to create a bank on Blockchain that can perform the following functions:

  • “openAccount” — create an account if using user’s address if it does not exist on the list
  • “deposit” — deposit a specified amount of ether (unit of value in Ethereum) from customer’s account to the bank
  • “withdraw” — withdraw a specific amount of ether from the bank
  • “borrow” — allow a customer to borrow at certain lending rate regardless of current deposit balance
  • “balance” — check the outstanding balance (deposit net of borrowing) of a customer
  • “transfer” — transfer ether from one customer to another, even if the recipient is a customer of another bank
  • “closeBank” — return all funding back to customers (net of borrowing) if there is no outstanding negative balance

Some of the checking / validation mechanisms include:

  • Initial finding of the bank (when the smart contract is deployed)  > 2 ether
  • Validation of sufficient balance of the bank / customer before any transfer is made
  • Authorisation of lending by bank given only when bank’s total remaining balance > 1 ether after the lending
  • Closure of bank can only be done by bank owner
  • Maximum of 10 accounts allowed for each bank

 

In Code We Trust

Below are the codes I have written which seem to work. To be sure, this is very primitive and far from professional level, I am sure the program is full of bugs and vulnerabilities. Anyway, this is what I am able to come up with as an amateur. You can try running my codes on Remix (Ethereum’s integrated development environment) to see how the logic flows.

Trivial though it may seem, the key takeaway is to get a taste of how “programmable money” works.

“In code we trust” — good code only, don’t trust mine.