Peer-to-Peer Payments
Transfer funds from one user to another without Payment Hubs.
Peer to Peer Payments with the Etherspot SDK
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:
1
/**
2
* User Wallets
3
* - Network: Ropsten
4
* - const senderEtherspotUser
5
*
6
* - Network: Ropsten
7
* - const receiverEtherspotUser
8
*/
Copied!

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:
1
const { p2pDepositAddress } = senderEtherspotUser.state;
Copied!
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:
1
/**
2
* We're going to set 1 ETH as amount to be transferred.
3
*/
4
const amountToSend = ethers.utils.parseEther('1');
5
​
6
const output = await senderEtherspotUser.updateP2PPaymentChannel({
7
recipient: receiverEtherspotUser.state.accountAddress,
8
totalAmount: amountToSend,
9
}).catch(console.error);
10
​
11
// This is the hash we will use in the next step.
12
console.log('Hash:', output.hash)
Copied!

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:
1
/*
2
* Remember to clear your batch and keep the house clean!
3
*/
4
await receiverEtherspotUser.clearGatewayBatch();
5
​
6
/**
7
* Next, commit the Payment Channel. The
8
* batchCommitP2PPaymentChannel takes an object with two
9
* properties:
10
* - hash: the previously created Payment channel hash
11
* - deposit:
12
* - - true: the exchange amount is transferred to the p2pDepositAddress.
13
* - - false: the exchange amount is transferred to the accountAddress
14
*/
15
await receiverEtherspotUser.batchCommitP2PPaymentChannel({
16
hash: output.hash,
17
deposit: true, // See notes above
18
}).catch(console.error);
19
​
20
/**
21
* Next, we estimate the cost of the transaction...
22
*/
23
await receiverEtherspotUser
24
.estimateGatewayBatch();
25
​
26
/**
27
* And finally we submit this to the ETherspot Gateway.
28
*/
29
await receiverEtherspotUser
30
.submitGatewayBatch();
Copied!
Make sure that the receiver wallet has enough gas fees to pay on your p2pPaymentDepositAddress.

​
πŸŽ‰
Finished!

Last modified 3mo ago