Skip to content

Commit e5e2a22

Browse files
committed
update the example code
1 parent d70d0cf commit e5e2a22

File tree

4 files changed

+16
-37
lines changed

4 files changed

+16
-37
lines changed

cex/go-example/main.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/ethereum/go-ethereum/common/hexutil"
1313
"github.com/ethereum/go-ethereum/core/types"
1414
"github.com/ethereum/go-ethereum/crypto"
15-
"github.com/ethereum/go-ethereum/ethclient"
1615
)
1716

1817
const TokenContractAddress = "0x.."
@@ -21,12 +20,10 @@ const SponsorPolicyId = ".."
2120
const HotwalletPrivateKey = ".."
2221

2322
const sponsorAPIEndpoint = "https://open-platform.nodereal.io/{Your_API_key}/megafuel"
24-
const web3ProviderEndpoint = "https://bsc-dataseed.bnbchain.org"
2523
const paymasterEndpoint = "https://bsc-megafuel.nodereal.io"
2624

2725
// testnet endpoint
2826
// const sponsorAPIEndpoint = "https://open-platform.nodereal.io/{Your_API_key}/megafuel-testnet"
29-
// const web3ProviderEndpoint = "https://bsc-testnet-dataseed.bnbchain.org"
3027
// const paymasterEndpoint = "https://bsc-megafuel-testnet.nodereal.io'"
3128

3229
func main() {
@@ -64,11 +61,6 @@ func sponsorSetUpPolicyRules() {
6461

6562
func cexDoGaslessWithdrawl() {
6663
withdrawAmount := big.NewInt(1e17)
67-
// Connect to an Ethereum node (for transaction assembly)
68-
client, err := ethclient.Dial(web3ProviderEndpoint)
69-
if err != nil {
70-
log.Fatalf("Failed to connect to the Ethereum network: %v", err)
71-
}
7264
// Create a PaymasterClient (for transaction sending)
7365
paymasterClient, err := NewPaymasterClient(paymasterEndpoint)
7466
if err != nil {
@@ -92,8 +84,9 @@ func cexDoGaslessWithdrawl() {
9284
log.Fatalf("Failed to create ERC20 transfer data: %v", err)
9385
}
9486

95-
// Get the latest nonce for the from address
96-
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
87+
// Get the pending nonce for the from address, strongly suggest to fetch nonce from paymaster endpoint when
88+
// submitting multiple transactions in rapid succession, to ensure that the nonce are sequential.
89+
nonce, err := paymasterClient.PendingNonceAt(context.Background(), fromAddress)
9790
if err != nil {
9891
log.Fatalf("Failed to get nonce: %v", err)
9992
}
@@ -103,7 +96,7 @@ func cexDoGaslessWithdrawl() {
10396
tx := types.NewTransaction(nonce, tokenAddress, big.NewInt(0), 300000, gasPrice, data)
10497

10598
// Get the chain ID
106-
chainID, err := client.ChainID(context.Background())
99+
chainID, err := paymasterClient.ChainID(context.Background())
107100
if err != nil {
108101
log.Fatalf("Failed to get chain ID: %v", err)
109102
}

cex/js-example/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ const userWithdrawAddress = 'USER_WITHDRAW_ADDRESS';
88
const erc20TokenAddress = 'TOKEN_CONTRACT_ADDRESS';
99
const policyID = 'SPONSOR_POLICY_ID'
1010

11-
const web3ProviderEndpoint = 'https://bsc-dataseed.bnbchain.org';
1211
const paymasterEndpoint = 'https://bsc-megafuel.nodereal.io';
1312
const sponsorEndpoint = 'https://open-platform.nodereal.io/{SPONSOR_API_KEY}/megafuel';
1413

1514
// testnet endpoint
16-
// const web3ProviderEndpoint = 'https://bsc-testnet-dataseed.bnbchain.org';
1715
// const paymasterEndpoint = 'https://bsc-megafuel-testnet.nodereal.io';
1816
// const sponsorEndpoint = 'https://open-platform.nodereal.io/{SPONSOR_API_KEY}/megafuel-testnet';
1917

@@ -60,13 +58,10 @@ class PaymasterProvider extends ethers.providers.JsonRpcProvider {
6058

6159
async function cexDoGaslessWithdrawTx() {
6260

63-
// Provider for assembling the transaction (e.g., mainnet)
64-
const assemblyProvider = new ethers.providers.JsonRpcProvider(web3ProviderEndpoint);
65-
6661
// Provider for sending the transaction (e.g., could be a different network or provider)
6762
const paymasterProvider = new PaymasterProvider(paymasterEndpoint);
6863

69-
const wallet = new ethers.Wallet(hotwalletPrivateKey, assemblyProvider);
64+
const wallet = new ethers.Wallet(hotwalletPrivateKey);
7065
// ERC20 token ABI (only including the transfer function)
7166
const tokenAbi = [
7267
"function transfer(address to, uint256 amount) returns (bool)"
@@ -79,13 +74,16 @@ async function cexDoGaslessWithdrawTx() {
7974
const tokenAmount = ethers.utils.parseUnits('1.0', 18); // Amount of tokens to send (adjust decimals as needed)
8075

8176
try {
82-
// Get the current nonce for the sender's address
83-
const nonce = await assemblyProvider.getTransactionCount(wallet.address);
77+
// Get the pending nonce, strongly suggest to fetch nonce from paymaster endpoint when
78+
// submitting multiple transactions in rapid succession, to ensure that the nonce are sequential.
79+
const nonce = await paymasterProvider.getTransactionCount(wallet.address, 'pending');
80+
const network = await paymasterProvider.getNetwork();
8481

8582
// Create the transaction object
8683
const transaction = await tokenContract.populateTransaction.transfer(userWithdrawAddress, tokenAmount);
8784

8885
// Add nonce and gas settings
86+
transaction.chainId = network.chainId;
8987
transaction.nonce = nonce;
9088
transaction.gasPrice = 0; // Set gas price to 0
9189
transaction.gasLimit = 100000; // Adjust gas limit as needed for token transfers

payment-gateway/go-example/main.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/ethereum/go-ethereum/common/hexutil"
1313
"github.com/ethereum/go-ethereum/core/types"
1414
"github.com/ethereum/go-ethereum/crypto"
15-
"github.com/ethereum/go-ethereum/ethclient"
1615
)
1716

1817
const PaymentTokenContractAddress = "0x.."
@@ -21,12 +20,10 @@ const PaymentSponsorPolicyId = ".."
2120
const UserPrivateKey = "..."
2221

2322
const sponsorAPIEndpoint = "https://open-platform.nodereal.io/{Your_API_key}/megafuel"
24-
const web3ProviderEndpoint = "https://bsc-dataseed.bnbchain.org"
2523
const paymasterEndpoint = "https://bsc-megafuel.nodereal.io"
2624

2725
// testnet endpoint
2826
// const sponsorAPIEndpoint = "https://open-platform.nodereal.io/{Your_API_key}/megafuel-testnet"
29-
// const web3ProviderEndpoint = "https://bsc-testnet-dataseed.bnbchain.org"
3027
// const paymasterEndpoint = "https://bsc-megafuel-testnet.nodereal.io'"
3128

3229
func main() {
@@ -95,11 +92,6 @@ func paymentGatewaySetUpPolicyRules(receiver common.Address) {
9592
}
9693

9794
func userDoGaslessPayment(receiver common.Address, amount *big.Int) {
98-
// Connect to an Ethereum node (for transaction assembly)
99-
client, err := ethclient.Dial(web3ProviderEndpoint)
100-
if err != nil {
101-
log.Fatalf("Failed to connect to the Ethereum network: %v", err)
102-
}
10395
// Create a PaymasterClient (for transaction sending)
10496
paymasterClient, err := NewPaymasterClient(paymasterEndpoint)
10597
if err != nil {
@@ -129,7 +121,7 @@ func userDoGaslessPayment(receiver common.Address, amount *big.Int) {
129121
}
130122

131123
// Get the latest nonce for the from address
132-
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
124+
nonce, err := paymasterClient.PendingNonceAt(context.Background(), fromAddress)
133125
if err != nil {
134126
log.Fatalf("Failed to get nonce: %v", err)
135127
}
@@ -139,7 +131,7 @@ func userDoGaslessPayment(receiver common.Address, amount *big.Int) {
139131
tx := types.NewTransaction(nonce, tokenAddress, big.NewInt(0), 300000, gasPrice, data)
140132

141133
// Get the chain ID
142-
chainID, err := client.ChainID(context.Background())
134+
chainID, err := paymasterClient.ChainID(context.Background())
143135
if err != nil {
144136
log.Fatalf("Failed to get chain ID: %v", err)
145137
}

payment-gateway/js-example/index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ const paymentReceiverAddress = 'PAYMENT_RECIPIENT_ADDRESS';
88
const erc20TokenAddress = 'TOKEN_CONTRACT_ADDRESS';
99
const policyID = 'SPONSOR_POLICY_ID'
1010

11-
const web3ProviderEndpoint = 'https://bsc-dataseed.bnbchain.org';
1211
const paymasterEndpoint = 'https://bsc-megafuel.nodereal.io';
1312
const sponsorEndpoint = 'https://open-platform.nodereal.io/{SPONSOR_API_KEY}/megafuel';
1413

1514
// testnet endpoint
16-
// const web3ProviderEndpoint = 'https://bsc-testnet-dataseed.bnbchain.org';
1715
// const paymasterEndpoint = 'https://bsc-megafuel-testnet.nodereal.io';
1816
// const sponsorEndpoint = 'https://open-platform.nodereal.io/{SPONSOR_API_KEY}/megafuel-testnet';
1917

@@ -58,14 +56,10 @@ class PaymasterProvider extends ethers.providers.JsonRpcProvider {
5856
}
5957

6058
async function userDoGaslessPayment() {
61-
62-
// Provider for assembling the transaction (e.g., mainnet)
63-
const assemblyProvider = new ethers.providers.JsonRpcProvider(web3ProviderEndpoint);
64-
6559
// Provider for sending the transaction (e.g., could be a different network or provider)
6660
const paymasterProvider = new PaymasterProvider(paymasterEndpoint);
6761

68-
const wallet = new ethers.Wallet(userPrivateKey, assemblyProvider);
62+
const wallet = new ethers.Wallet(userPrivateKey);
6963
// ERC20 token ABI (only including the transfer function)
7064
const tokenAbi = [
7165
"function transfer(address to, uint256 amount) returns (bool)"
@@ -79,12 +73,14 @@ async function userDoGaslessPayment() {
7973

8074
try {
8175
// Get the current nonce for the sender's address
82-
const nonce = await assemblyProvider.getTransactionCount(wallet.address);
76+
const nonce = await paymasterProvider.getTransactionCount(wallet.address);
77+
const network = await paymasterProvider.getNetwork();
8378

8479
// Create the transaction object
8580
const transaction = await tokenContract.populateTransaction.transfer(paymentReceiverAddress, tokenAmount);
8681

8782
// Add nonce and gas settings
83+
transaction.chainId = network.chainId;
8884
transaction.nonce = nonce;
8985
transaction.gasPrice = 0; // Set gas price to 0
9086
transaction.gasLimit = 100000; // Adjust gas limit as needed for token transfers

0 commit comments

Comments
 (0)