Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Latest commit

 

History

History
104 lines (77 loc) · 5.76 KB

README.md

File metadata and controls

104 lines (77 loc) · 5.76 KB

etherdelta-go

GitHub release

This is an example of how to interact with an Ethereum contract from Go through RPC.

The example shows three things:

  • how to generate Go language bindings given a contract's ABI definition,
  • how to read data from the blockchain without spending gas, and
  • how to change state on the blockchain by providing the correct private key and spending gas

The contract library

We use abigen to generate Go language bindings given a contract's ABI definition.

To regenerate the code install abigen, put the ABI definition into a file (e.g., for the EtherDelta contract) and run:

$ cd contract
$ abigen --abi etherdelta.abi --pkg contract --type EtherDelta --out etherdelta.go

This will create an etherdelta.go file that you can import into your Go programs.

Example program

The example code contains a working interaction with the Ethereum blockchain and may even be useful for somebody.

When using EtherDelta (a decentralized exchange based on smart contracts) a user deposits funds from her own address into a pool that can be accessed by EtherDelta for executing trades. Later the user withdraws any funds from the pool back to her own address. Often there's a tiny amount left in the pool which is tedious to withdraw manually.

etherdelta-go to the rescue; it allows you to withdraw any deposited Ethereum or ERC20 Tokens known to 0xProject's Token Registry back to you.

Build and install the binary:

$ cd $GOPATH/src/github.com/linki/etherdelta-go
$ dep ensure -vendor-only
$ go install

Make sure $GOPATH/bin is in your $PATH, then run it like that:

$ etherdelta-go --keystore-file "UTC-...98c"
Found tokens: 50
Your address: 0x001...98C
Deposited ETH: 1000000000000000000
...
Deposited AIR: 500000000
...

Note the balances are displayed using the smallest spendable unit. The above maps to 1 Ethereum and 5 AirToken.

If you have anything deposited you can run the command with the --withdraw-all flag which will attempt to withdraw ETH and tokens that have a balance greater than zero. When using --withdraw-all you also have to provide --passphrase to unlock your Keystore file.

$ etherdelta-go --keystore-file "UTC-...98c" --passphrase "my...pass" --withdraw-all
Found tokens: 50
Your address: 0x001...98C
Deposited ETH: 1000000000000000000
Withdrawing ETH: 1000000000000000000
Transaction hash: 0x7e3892...be9249
...
Deposited AIR: 500000000
Withdrawing AIR: 500000000
Transaction hash: 0x1a6328...7ef38a
...

The tool won't wait until the transactions have been mined. So just make sure the transactions got successfully submitted and give them some time to be mined. Then you can run the command again and validate that all your deposited ETH and tokens have been withdrawn.

$ etherdelta-go --keystore-file "UTC-...98c"
Found tokens: 50
Deposited ETH: 0
...
Deposited AIR: 0
...

It takes the following arguments:

  • keystore-file: the location of an Ethereum Keystore file which describes where the funds will be withdrawn to as well as contains the encrypted private key for that address, e.g., ~/Library/Ethereum/keystore/UTC--2017-11-...61d3f9
  • withdraw-all: sends a transactions to withdraw the balance if it's greater than zero. Without this flag it will merely print the deposited balances. Note that passphrase must be provided and correct in order to successfully sign the transaction. Defaults to false (disable withdrawal).
  • passphrase: the passphrase unlocking the keystore-file. It proves that you are the owner of the Keystore file and is only required when using withdraw-all.

There are some optional arguments as well:

  • endpoint: an RPC endpoint to interact with the Ethereum blockchain. You can run your own Ethereum node or connect to one provided by Infura free of charge. Defaults to https://mainnet.infura.io.
  • etherdelta: The address of the EtherDelta contract to use. Since contracts are immutable each change to the contract requires a new contract to be deployed and results in a new contract address. Defaults to the most recent contract as of December 2017: 0x8d12A197cB00D4747a1fe03395095ce2A5CC6819.
  • token-registry: The address of 0xProject's TokenRegistry contract. Defaults to the most recent contract as of December 2017: 0x926a74c5C36adf004C87399e65f75628b0f98D2C.
  • gas-price: The gas price in wei for the withdrawal transaction. Defaults to 1000000000 which is 1 Gwei.
  • gas-limit: The maximum gas to use for the withdrawal transaction. Defaults to 100000. During my testing for AirToken the final gas for successful transactions hovered around 50000.
  • timeout: The timeout to submit a transaction to the Ethereum endpoint. Defaults to 5 seconds.

Keeping the defaults for gas-price and gas-limit will result in a maximum transaction fee of 0.0001 ETH.

Requirements

  • Go 1.10
  • Ethereum 1.8

Disclaimer

No warranty for any funds lost. See LICENSE for details.