Swap Ether and ERC20 tokens on the Ethereum network. This is beta software so, please, use with caution.
If you found this helpful then you can buy me a beer at 0xA459552915C85d079b2EC8e28024314a538f3fd3.
- The buyer calls
createto begin the Swap.
TokenSwap.create(token, tokenAmount, price, seller, buyer, [recipient])
tokenis theaddressof an ERC20-compatible token contracttokenAmountis the amount of tokens to be soldpriceis the total price, in Wei, to be paid for the tokensselleris the address of the token holderbuyeris the address of the Ether holder (this address must send the payment to conclude the Swap)recipientis an optional argument specifying an address to receive the tokens; if norecipientis specified thenbuyeris used
- The seller approves the TokenSwap contract to spend
tokenAmounttokens, using the ERC20approvefunction. - The buyer calls
concludewithpriceWei.
If the tokens have not been approved then the call will fail. If the tokens have been, and the value of the call is sufficient, the tokens will be sent to the recipient (or buyer, if none is specified) and the Ether to the seller.
An additional cancel function has also been provided. The buyer, seller and recipient are all able to cancel a Swap at any time before it is concluded. Any tokens that have been approved will be refunded.
- Install
truffleandopenzeppelin-solidityby runningnpm i. - Compile the contracts using
truffle compile. - Run the test by installing and launching ganache, then running
truffle test.
MIT © Luke Mitchell