Arweave.news

Arweave Development Tutorial: How to Use everPay-js for Atomic Swap

0 156

everPay is a decentralized cross-chain financial protocol built on Arweave that supports Bundle transactions and naturally supports atomic swaps between tokens. Unlike other traditional blockchains where transfers have to wait for packaging, everPay transactions are verified and cached meticulously by everPay servers, and assets arrive in real-time. Therefore, there is no need to wait for transactions to be packaged.

What is an atomic swap?

The bundle is a many-to-many transaction format where if any subtransaction in a Bundle fails, the entire transaction is rolled back. When making a Bundle transaction, the user only needs to consider their balance and expenses and does not need to worry about the counterparty defaulting. If a user signs a Bundle transaction that pays 1000 USDT and receives 0.1 ETH when the transaction is executed.

If the user does not receive 0.1 ETH, his payment of 1000 USDT will be rolled back. The Bundle transaction will be completed successfully only if the user’s payments and receipts meet the order conditions.

everPay-js is a js-sdk open source library for everPay. With everPay-js functions, you can quickly implement an atomic token swap. The following section describes how to perform an atomic swap between two wallets, A and B.

Show me the code👇

https://github.com/everFinance/everpay-example/blob/main/javascript/Bundle.js 

Prerequisites

Wallets

everPay protocol supports both Ethereum and Arweave wallet addresses, and the following example uses Ethereum addresses.

  1. Wallet address A: 0x73f45F01C66c81B4016858C63E695CDfEe347F54, with private key: ba7da8e8893b516f43f73a8d69ca4ed4ffd60e55f6a739fa707350f1add89f2f
  2. Wallet address B: 0x06eACBB9c550F0a014DC1BF0244312a96DF1a411, with private key:  dfe071748c3d7c9110ecc214305645a7a63f4203792e30d8207dd412ec8c0908

Test token claiming

The following examples will be performed at everPay Testnet. If you need some test tokens, you can apply for test tokens of 1000 USDT and 0.1 ETH by reaching out to @Xaber | everFinance#4893 at the dev channel in everPay discord

Discord Dev Channel:https://discord.gg/dCreupzseF 

Test token transfer

At this point, wallet A has 1000 USDT and wallet B has 0.1 ETH on everPay

Atomic Swap

Initialize everPay with Ethereum wallet

import  {  ethers  }  from  ‘ethers’
import  Everpay  from  ‘everpay’

const  genEverpay  =  (wallet)  =>  {
  const  provider  =  new  ethers.providers.InfuraProvider(‘kovan’)
  const  signer  =  new  ethers.Wallet(wallet.privateKey,  provider)
  const  everpay  =  new  Everpay({
    account:  wallet.address,
    ethConnectedSigner:  signer,    chainType: ‘ethereum’,
    debug:  true
})
  return  everpay
}

const  walletA  =  {
  address:  ‘0x73f45F01C66c81B4016858C63E695CDfEe347F54’,
  privateKey:  ‘ba7da8e8893b516f43f73a8d69ca4ed4ffd60e55f6a739fa707350f1add89f2f’
}

const  walletB  =  {
  address:  ‘0x06eACBB9c550F0a014DC1BF0244312a96DF1a411’,
  privateKey:  ‘dfe071748c3d7c9110ecc214305645a7a63f4203792e30d8207dd412ec8c0908’
}

const  everpayA  =  genEverpay(walletA)
const  everpayB  =  genEverpay(walletB)

We initialize two everPay instances of everpayA and everpayB with their private keys. The initialized everPay instances can sign orders and send transactions to the everPay node.

Note: When debugging parameter in the new everPay is true, everPay Testnet will be used in the development. If you need to use mainnet, please set debug to false.

Initialize everPay with Arweave wallet

The jwk parameter is RSA Key, generated on your own. Skip the following code if you are not using Arweave wallet.

const arWallet = {
  address: ‘5NPqYBdIsIpJzPeYixuz7BEH_W7BEk_mb8HxBD3OHXo’,
  jwk: { kty: ‘RSA’, n: ‘odtNk…’, e: ‘AQAB’, d: ‘mTc…’, p: ‘znk…’, q: ‘yK5…’, dp: ‘OrD…’, dq: ‘cPC…’, qi: ‘Qzp…’ }
}


const everpayC = new Everpay({
  account: arWallet.address,
  arJWK: arWallet.jwk,  chainType: ‘arweave’,
  debug: true
})

 

Generate bundle data for atomic swap

  • Transfer 1000 USDT from walletA’s everPay wallet to walletB’s everPay wallet
  • Transfer 0.1 ETH from walletB’s everPay wallet to walletA’s everPay wallet
const  bundleData  =  await  everpayA.getBundleData([
  {
    symbol:  ‘USDT’,
    from:  walletA.address,
    to:  walletB.address,
    amount:  ‘1000’
  },  {
    symbol:  ‘ETH’,
    from:  walletB.address,
    to:  walletA.address,
    amount:  ‘0.1’
  }
])

The above code is a Bundle transaction describing a transfer of 1000 USDT from A to B and a transfer of 0.1 ETH from B to A.

Sign bundle data

For a Bundle transaction to be successfully executed, all the transferring parties need to provide signatures. The “from” addresses of the transferring parties of our Bundle are walletA and walletB respectively, so they need to sign the Bundle, and the signing method is shown below.

const  bundleDataWithSigA  =  await everpayA.signBundleData(bundleData)

const  bundleDataWithSigB  =  await  everpayB.signBundleData(bundleDataWithSigA)

 

The signBundleData function will automatically send the signature of walletA (everpayA) to bundleDataWithSigA. After the two signatures above, bundleDataWithSigB will contain the signatures of both walletA and walletB accounts for the Bundle transaction.

Post signed bundle data

Finally, we post this Bundle to everPay to complete the atomic swap of 1000 USDT and 0.1 ETH for walletA and walletB in the everPay network.

const  bundleResult  =  await  everpayA.bundle({
  symbol:  ‘ETH’,
  to:  walletA.address,
  amount:  ‘0’,
  data:  {
    bundle:  bundleDataWithSigB
  }
})

const  everHash  =  bundleResult.everHash
console.log(everHash)

 

The bundleDataWithSigB containing the two signatures will be sent to the everPay node via the bundle function. Please fill in the symbol, to, and amount fields in the above code to follow the tutorial. A bundle is an internal transaction, see more details in our documentation.

Please keep the output everHash and use everHash to check the status of the bundle transaction in your browser.

Check transaction status at everScan

Open up everPay dev Blockchain Explorer, and search for everHash

View transaction status

Check balance changes

Call the everPay balances() function to see all token balances under an account.

// ETH 0.1
await  everpayA.balances()

// USDT 1000
await  everpayB.balances()

 

Now, walletA has 0.1 ETH in everPay and walletB has 1000 USDT in everPay.

walletA and walletB successfully complete an atomic swap of ETH-USDT via Bundle in the everPay network.

Demo code

https://github.com/everFinance/everpay-example/blob/main/javascript/bundle.js 

 


Join our

Telegram /Discord /Twitter

Leave A Reply

Your email address will not be published.