Giter Club home page Giter Club logo

token-swap's Introduction

Token Swap

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.

Usage

  1. The buyer calls create to begin the Swap.
TokenSwap.create(token, tokenAmount, price, seller, buyer, [recipient])
  • token is the address of an ERC20-compatible token contract
  • tokenAmount is the amount of tokens to be sold
  • price is the total price, in Wei, to be paid for the tokens
  • seller is the address of the token holder
  • buyer is the address of the Ether holder (this address must send the payment to conclude the Swap)
  • recipient is an optional argument specifying an address to receive the tokens; if no recipient is specified then buyer is used
  1. The seller approves the TokenSwap contract to spend tokenAmount tokens, using the ERC20 approve function.
  2. The buyer calls conclude with price Wei.

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.

Development

  1. Install truffle and openzeppelin-solidity by running npm i.
  2. Compile the contracts using truffle compile.
  3. Run the test by installing and launching ganache, then running truffle test.

License

MIT © Luke Mitchell

token-swap's People

Contributors

lukem512 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

token-swap's Issues

Test refunding overpayment to buyer

If the buyer overpays in Ether when calling connect() the contract will return the difference. There is a stub test, however, this does not check that the difference has been refunded in a robust manner.

Test multiple Swaps for a single `buyer`

This has been prevented with #8a7fc554056928b7ce6b97cca529278763b110fc but there is not yet a test in place. The following code requires modification to check for the a failed require, which will eventually map to a REVERT opcode.

it("user cannot create a second Swap whilst one exists", function() {
    return ts.create(token.address, amount, price, seller, buyer, {from: buyer})
    .then(function() {
      return ts.create(token.address, amount, price, seller, buyer, {from: buyer})
    }).then(function() {
      // TODO: check for REVERT
    })
  });

Test `cancel`

The cancel function should fail the require if it it is called by addresses other than the buyer, seller, recipient or contract owner

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.