Coder Social home page Coder Social logo

ether-cross-chain-swap's Introduction

README

This smart contract demonstrates an 'Atomic cross-chain swap between different blockchain networks'. The blockchains needs to support 'secret lock' and 'secret proof' functionalities. It's possible with Ethereum, Bitcoin, NEM, NEO and several other products. This smart contract is based on the NEM cross chain swap functionality.

However if it's real 'Atomic' is valid point to discuss. But both parties have a strong incentive to fullfill the rules

How it works

Lets say Alice wants to convert Ether to NEM:XEM and Bob NEM:XEM to Ether.

Hint: The wording in this Ethereum smart contract is a bit different as in the NEM documentation. In my opinion NEM naming is confusing. Especial in NEM secret represents a hash and is not the real secret.

Steps:

  • Both agree on an exchange rate.
  • Both exchange their addresses to receive the funds.
  • Alice creates a secret and the hash of it.
  • Alice writes to the Ethereum smart contract secretLock(uint256 _lockTimeSec, bytes32 _hash, address _recipient) and adds the agreed amount of Ether.
  • Alice submits the hash to Bob.
  • Bob is now able to validate/audit the 'secret lock' in the Ethereum smart contract.
  • Bob will now create a 'secret lock transaction' in NEM SecretLockTransaction.create(Deadline.create(), new Mosaic(new MosaicId('nem:xem'), exchangeValue), lockTime, HashType.SHA256, secret, Alice.address, NetworkType.MIJIN);.
  • Alice gets notified and can validate/audit the NEM secret lock.
  • Alice will then call on the NEM network a 'secret proof transaction'. SecretProofTransaction.create( Deadline.create(), HashType.SHA256, hash, proof, NetworkType.MAIN_NET);.
  • So Alice will get the XEM:NEM and for her the swap was successful.
  • Bob gets notified by the NEMs secret proof transaction. As it is a public blockchain, Bob sees the proof the real secret.
  • Bob then creates an Ethereum transaction to the smart contract. secretProof(bytes32 _hash, bytes _proof). So Bob will receive the Ether.

Notes

Web3 version 1.x is installed with NPM. Also Truffle comes with Web3 0.2x. The API of these Web3 are not always compatible.

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.