A demo which showcases the transfer of ERC 865 tokens using the metamask browser extension and the truffle webpack demo
The ERC865 token standard adds a delegated version to each of the ERC20 methods (except those that result in state changes) to allow a delegate to execute the method on behalf of the token holder -
- transferPreSigned()
- approvePreSigned()
- increaseApprovalPreSigned()
- decreaseApprovalPreSigned()
- approveAndCallPreSigned()
- revokeSignaturePreSigned()
Each of the above methods accepts a signature of type bytes in addition to the other parameters such as the receiving address, value to be sent, fee to the delegate and the nonce of the sender.
A delegated transfer (say using the transferPreSigned() method) would involve the following steps -
- In the wallet interface, gather user input for the receiving address, value, fee and nonce
- Create a hash of the above parameters using keccak256 in solidity or the getPreSignedHash() method which is usually built into the ERC865 contract
- In the backend code of your wallet, use web3.eth.sign() to generate a signature of the above hash with the user's account.
- This signature can be now passed to the delegate, along with the other parameters of the transaction and the corresponding method can be executed in the smart contract
Note: The ERC865 contract used in this demo is not up to date. Kindly refer to the CoinvestV2 smart contract for a cleaner implementation - https://github.com/RobertMCForster/CoinvestV2Audit/blob/master/contracts/CoinvestTokenV2.sol
What things you need to install the software and how to install them
- Metamask (https://metamask.io/)
- NodeJS (https://nodejs.org/en/)
- Ganache (https://truffleframework.com/ganache)
- Truffle Framework
npm install -g truffle
- Extract the code to a project directory
- Run Ganache and ensure that the RPC port is up and running. For the purpose of this demo, we assume Ganache is running your private Ethereum node on http://127.0.0.1:7545
- Add your custom RPC in Metamask at http://127.0.0.1:7545
- Add your first account on Ganache onto Metamask, by exporting private key
- Open the file truffle.js, and make sure the host and port points to http://127.0.0.1 and 7545 respectively
- Connect truffle to your private Ethereum node run by Ganache
truffle console --network dev
- Run the commands below in sequence
truffle(dev)> compile
truffle(dev)> migrate --reset
- Open another console window and start your server with the below command
npm run dev
- Follow the demo at https://youtu.be/3e_P5OdMMdM for transfering tokens without ether
If you run into problems running the code should try removing the node modules folder and running a fresh install
rm -r node_modules
npm install
Follow the steps below if you want to skip all the hardship and head straight to a demo
- Head over to http://delegation.adilharis.com:8080/
- Connect your metamask to Rinkeby and get some ether from the test faucet
- After getting hold of some test ether, hit the "Mint Token" button to receive 1000 ERC865 Token
- Refer this video and its description if you have queries on how to use this demo: https://youtu.be/dAbD3Vhdy6U
Kindly don't spam transactions, hack or bring my node down. Please.
- Adil Haris - http://www.adilharis.com