Trust Fund account on Ethereum

Unlike with legacy finance systems where you need to rely on a well-established third party, you can build your own financial instrument on Ethereum.

The objective of this tutorial is to show how easy it is to build and run your own instance of a simple decentralized finance example, or DeFi.

In this tutorial, you will:

  • Create a basic Trust Fund account smart contract with the following interaction options:

    • fund the account from any Ethereum address;
    • withdraw all funds from the account only from the account owner address;
    • withdraw partial funds from the account only from the account owner address;
    • transfer the account ownership to any Ethereum address;
    • terminate the account and get the balance back to the owner.
  • Compile the smart contract with Remix IDE.

  • Deploy the smart contract to Ethereum Ropsten testnet through a Chainstack node.

  • Interact with the smart contract through Remix IDE and a Chainstack node.

Ethereum Ropsten testnet

For illustration purposes, this tutorial uses Ethereum Ropsten testnet.

For Ethereum mainnet, the steps are exactly the same, except you need to use mainnet ether.

Prerequisites

  • Chainstack account to deploy an Ethereum node.
  • Remix IDE to compile the contract and deploy through MetaMask.
  • MetaMask to deploy the contract through Chainstack node and interact with the contract.

Overview

To get from zero to a deployed Trust Fund account on the Ethereum Ropsten testnet, do the following:

  1. With Chainstack, create a Public chain project.
  2. With Chainstack, join the Ethereum Ropsten testnet.
  3. With Chainstack, access your Ethereum node credentials.
  4. Set up your MetaMask to work through a Chainstack node.
  5. With Remix IDE, create and compile the Trust Fund smart contract.
  6. Set up your Remix IDE to work through a Chainstack node.
  7. With Remix IDE, deploy the contract to the Ethereum Ropsten testnet.
  8. With Remix IDE, interact with the contract on the Ethereum Ropsten testnet.

Step-by-step

Create a Public chain project

See Create a project.

Join the Ethereum Ropsten testnet

See Join a public network.

Get your Ethereum node access and credentials

See View node access and credentials.

Set up MetaMask

See Tools: MetaMask.

Having set up your MetaMask to interact through a Chainstack node, your Remix IDE will also interact with the network through a Chainstack node.

Create at least two accounts in MetaMask. You need two accounts to transfer the contract ownership from one to another.

In your MetaMask, fund each account with Ropsten ether by clicking Deposit > Test Faucet > Get Ether.

Create and compile the Trust Fund smart contract

  1. Open Remix IDE.
  2. On the home page, click Environments > Solidity.
  3. On the left pane, click File explorers > plus sign.
  4. In the modal, give any name to your contract. For example, transferableTrustFund.sol.
  5. Put in the contract code:
pragma solidity ^0.5.0;

contract TransferableTrustFundAccount {
    address owner;

    constructor() public {
        owner = msg.sender;
    }

    function withdrawAll() public {
        require(owner == msg.sender);
        msg.sender.transfer(address(this).balance);
    }

    function withdrawAmount(uint256 amount) public {
        require(owner == msg.sender);
        require(address(this).balance >= amount);
        msg.sender.transfer(amount);
    }

    function() external payable {}

    function transferAccount(address newAccount) public {
    require(owner == msg.sender);
    require(newAccount != address(0));
    owner = newAccount;
    }

    function terminateAccount() public {
    require(owner == msg.sender);
    selfdestruct(msg.sender);
    }
}

This is your Trust Fund account smart contract:

  • The contract belongs to the Ethereum account that deploys the contract through:
contract TransferableTrustFundAccount {
    address owner;
    ...
}
  • Only the contract owner can withdraw all funds from the contract through:
function withdrawAll() public {
        require(owner == msg.sender);
        msg.sender.transfer(address(this).balance);
    }
  • Only the contract owner can withdraw partial funds from the contract through:
function withdrawAmount(uint256 amount) public {
        require(owner == msg.sender);
        require(address(this).balance >= amount);
        msg.sender.transfer(amount);
    }
  • Anyone can deposit funds to the contract through:
function() external payable {}
  • Only the contract owner can transfer the contract ownership to any other Ethereum account through:
function transferAccount(address newAccount) public {
    require(owner == msg.sender);
    require(newAccount != address(0));
    owner = newAccount;
    }
  • Only the contract owner can terminate the contract and return all funds to the current owner through:
 function terminateAccount() public {
    require(owner == msg.sender);
    selfdestruct(msg.sender);
    }
  1. Compile the contract:

On the left pane, click Solidity compiler > Compile:

Select compile

Set up Remix IDE to work through your Chainstack node

On the left pane, click Deploy and switch to Injected Web3:

Inject

This will engage your MetaMask and interact with the network through the Chainstack node provided in MetaMask.

Deploy the Trust Fund smart contract

On the left pane, click Deploy & run transactions > Deploy:

Deploy

This will engage your MetaMask to deploy the contract to the Ethereum Ropsten testnet through your currently selected MetaMask account. Click Confirm in the MetaMask modal.

Interact with the Trust Fund smart contract

Once the contract is deployed, fund the contract:

  1. Copy the contract address under Deployed Contracts:

Contract address

  1. Open your MetaMask and send Ropsten ether to the copied contract address.

Now that your contract is funded, you can interact with it.

Expand the contract under Deployed Contracts:

Interact

  • withdrawAmount — enter any amount that is less than the current contract balance to withdraw partial funds.
  • withdrawAll — click to withdraw all funds from the contract.
  • transferAccount — enter any Ethereum address to transfer the contract ownership. For this example, enter the address of your second account in MetaMask.
  • terminateAccount — click to terminate the account and have the remaining funds returned to the current contract owner.