Peer-to-Peer Payments

Transfer funds from one user to another without Payment Hubs.

Before we continue, please ensure that you have had a look at our Supported Ethereum Chains, followed the steps in Install Etherspot SDK and how to Bootstrap Etherspot SDK. We're assuming that you have completed these steps before going forward.

In this example, we're going to show you how to transfer the funds using Peer-to-Peer Deposits

🛑 Before we continue...

We're going to be using two Etherspot SDK instances here:

  • A ropsten Etherspot SDK for our sender user wallet

↗️ We will use this instance to send our ETH from.

  • A ropsten Etherspot SDK for our receiver user wallet

↘️ We will use this instance to receive our ETH on ropsten chain and ETH to pay the gas fees.

⚠️ Make sure you've checked out Supported Ethereum Chains before you continue as we also show you the code to instantiate ropsten version of the SDK. Remember to use different private keys or authentication for both SDK instances to get different Ethereum addresses on both Ropsten Testnet.

Getting started

For this guide, we're going to transfer ETH from one wallet to another wallet on the Ropsten network. To achieve this, we're going to send ETH from our p2pPaymentDeposit address linked to the user wallet to the another wallet on the Ropsten network.

We're going to assume that we're working with the following definitions:

/**
* User Wallets
* - Network: Ropsten
* - const senderEtherspotUser
*
* - Network: Ropsten
* - const receiverEtherspotUser
*/

Ensure Payment Deposit Address is funded

For this process to work, we need to make sure that the Payment Deposit have enough liquidity in them to facilitate the transfer to another wallet. We're going to be working with the p2pDepositAddress from our user wallet Etherspot SDKs. We can get this address as follows:

const { p2pDepositAddress } = senderEtherspotUser.state;

The p2pDepositAddress exists purely to provide liquidity for other operations. Your address is unaffected.

We now need to add Test ETH from the Ropsten Faucet. Please follow the instructions on the Ropsten Faucet website to receive Test ETH to the p2pDepositAddress.

Once you have received the Test ETH to the p2pDepositAddress in the senderEtherspotUser instance, we're ready to move on.

Transfer Funds via Payment Deposit

We need to transfer the desired amount of funds from our p2pDepositAddress in the senderEtherspotUser instance to the p2pDepositAddress in the receiverEtherspotUser instance

For this, we need to use the Etherspot SDK function updateP2PPaymentChannel to transfer from one Etherspot SDK to another with the following code:

/**
* We're going to set 1 ETH as amount to be transferred.
*/
const amountToSend = ethers.utils.parseEther('1');

const output = await senderEtherspotUser.updateP2PPaymentChannel({
    recipient: receiverEtherspotUser.state.accountAddress,
    totalAmount: amountToSend, 
}).catch(console.error);

// This is the hash we will use in the next step.
console.log('Hash:', output.hash)

Withdraw Funds from Payment Channel

To be able to withdraw the above amountToSend from the receiver Etherspot SDK, we need to perform a few final steps.

We're now working primarily with the Ropsten Receiver Etherspot SDK.

From the above output we need to get the hash to commit from the receiver SDK. This will allow us to withdraw the amount transferred. This can be done with the following:

/*
* Remember to clear your batch and keep the house clean!
*/
await receiverEtherspotUser.clearGatewayBatch();

/**
* Next, commit the Payment Channel. The 
* batchCommitP2PPaymentChannel takes an object with two
* properties:
* - hash: the previously created Payment channel hash
* - deposit: 
* - - true: the exchange amount is transferred to the p2pDepositAddress.
* - - false: the exchange amount is transferred to the accountAddress
*/
await receiverEtherspotUser.batchCommitP2PPaymentChannel({
  hash: output.hash,
  deposit: true, // See notes above
}).catch(console.error);

/**
* Next, we estimate the cost of the transaction...
*/
await receiverEtherspotUser
 .estimateGatewayBatch();

/**
* And finally we submit this to the ETherspot Gateway.
*/
await receiverEtherspotUser
  .submitGatewayBatch();

Make sure that the receiver wallet has enough gas fees to pay on your p2pPaymentDepositAddress.

🎉 Finished!

Last updated