everVision
Published in

everVision

Arweave Development Tutorial: How to Use everpay-js for Atomic Swaps

Introduction

everPay is a decentralized cross-chain financial protocol built on Arweave that supports Bundle transactions and naturally supports atomic swap between tokens.

What is an atomic swap?

Bundle is a many-to-many transaction format where any subtransaction in a Bundle fails and 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, the user’s payment of 1000 USDT will be rolled back and the Bundle transaction will be executed 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 easily implement 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 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

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 debug parameter in 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.

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

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.

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.

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. 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.

Now, walletA has 0.1 ETH on everPay and walletB has 1000 USDT on 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

🔗 Our links: everFinance | everPay | Twitter | Github | Discord | Telegram

--

--

Arweave All-In-One Web3 Infrastructure Provider

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store