Skip to content

Instantly share code, notes, and snippets.

@iamchrissmith
Last active March 11, 2019 17:02
Show Gist options
  • Save iamchrissmith/693d09b7e8bd836a03a10fbb9a5c7300 to your computer and use it in GitHub Desktop.
Save iamchrissmith/693d09b7e8bd836a03a10fbb9a5c7300 to your computer and use it in GitHub Desktop.

SETH Examples

Setup

Need .sethrc for the blockchain you want to connect to. For this demo we'll be using dapp testnet and the setup-testnet.sh script from icetan. This script gets and sets some env variables for us:

  • ETH_FROM
  • ETH_PASSWORD
  • ETH_KEYSTORE
  • ETH_RPC_URL
  • ETH_GAS

Start and connect to testnet:

dapp testnet (in separate terminal) In main terminal run: eval "$(setup-testnet.sh 8545)" (where 8545 is the port for the testnet that you just started)

Launch Token Contract

  1. Now let's launch the token, from within a repo with the DSToken.sol file run

    dapp create DSToken "$(seth --to-bytes32 "$(seth --from-ascii "Sai")")" This will output some seth-send info and then the last line is the address of your new token contract.

    Breaking this down:

    • seth --from-ascii "Sai" converts "Sai" from a string to hexdata (returns 0x536169)
    • seth --to-bytes32 0x536169 converts that hexdata into bytes32 (returns 5361690000000000000000000000000000000000000000000000000000000000)
    • This is then passed to as the first and only arg to the DSToken constructor becoming the symbol for our token.
    • dapp create DSToken 5361690000000000000000000000000000000000000000000000000000000000 deploys the contract and passed our arg.
  2. For future use, let's save the address of the contract with sai=<ADDRESS>

  3. Let's check the ETH balance of our address with seth balance $ETH_FROM or with seth ls, both of which should return a huge number in wei.

    NOTE: If you want, you can convert this number to ETH using seth --from-wei $(seth balance $ETH_FROM)

  4. Let's send some ETH with seth send --value $(seth --to-wei 1.5 eth) <ANY OTHER ADDRESS>

  5. Confirm that worked with seth --from-wei $(seth balance $ETH_FROM)

  6. Ok, now back to our contract: Let's check our Sai symbol is set correctly with seth --to-ascii $(seth call $sai "symbol()") (returns Sai)

    Breaking this down:

    • seth call $sai "symbol()" this makes a call to our $sai contract's getter function for the symbol we set in the constructor. The seth call command receives the <name>(<types>) for our function here and performs the ABI encoding for us. (returns 0x5361690000000000000000000000000000000000000000000000000000000000)
    • We take that return and convert it from the hexdata to ASCII (returns Sai)
  7. Let's check our balance: seth call $sai "balanceOf(address)" $ETH_FROM should return 0x0000000000000000000000000000000000000000000000000000000000000000 since DSToken is not created with any tokens.

  8. Now we need some tokens minted: seth send $sai "mint(uint)" $(seth --to-uint256 $(seth --to-wei 1000000 eth))

    Breaking this down:

    • seth --to-wei 1000000 eth converts our 1 million desired tokens into their wei (18 decimal unit) format. (returns 1000000000000000000000000)
    • seth --to-uint256 1000000000000000000000000 converts our wei token number into uint256 hexdata (returns 00000000000000000000000000000000000000000000d3c21bcecceda1000000)
    • We take that return and send it as the parameter to our transaction that we are sending to the token's mint function. This will mint this many tokens to msg.sender, in this case our $ETH_FROM.
  9. Let's check our balance again: seth --from-wei $(seth --to-dec $(seth call $sai "balanceOf(address)" $ETH_FROM)) should now return 1000000.000000000000000000. (the call function returns hexdata (0x00000000000000000000000000000000000000000000d3c21bcecceda1000000 that needs to be converted to decimal and then to ether denominations))

  10. Let's transfer some Sai, first we'll check the another address balance with seth call $sai "balanceOf(address)" <OTHER-ADDRESS> and see that it returns 0 (in hex). Now let's do our transfer with seth send $sai "transfer(address,uint)" <OTHER-ADDRESS> $(seth --to-uint256 $(seth --to-wei 1000 eth))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment