@@ -22,78 +22,60 @@ This JavaScript SDK is thin wrapper of MegaFuel clients, offering a streamlined
22
22
``` js
23
23
import ' dotenv/config' ;
24
24
import {ethers } from " ethers" ;
25
- import { PaymasterClient } from ' megafuel-js-sdk' ;
25
+ import {PaymasterClient , SponsorClient } from ' megafuel-js-sdk' ;
26
26
27
- const PAYMASTER_URL = " https://bsc-megafuel-testnet.nodereal.io"
28
- const CHAIN_URL = " https://data-seed-prebsc-2-s1.binance.org:8545/"
29
- const SPONSOR_URL = " https://open-platform.nodereal.io/<api-key>/megafuel-testnet"
30
- const CHAIN_ID = " 97"
27
+ const PAYMASTER_URL = " https://bsc-megafuel.nodereal.io"
28
+ const SPONSOR_URL = " https://open-platform.nodereal.io/{YOUR_API_KEY}/megafuel"
31
29
32
- const POLICY_UUID = " a2381160-xxxx-xxxx-xxxxceca86556834"
33
- const TOKEN_CONTRACT_ADDRESS = " 0xeD2.....12Ee"
34
- const RECIPIENT_ADDRESS = " 0x8e9......3EA2"
35
- const YOUR_PRIVATE_KEY = " 69......929"
30
+ const POLICY_UUID = " a2381160-xxxx-xxxx-xxxxceca86556834"
31
+ const TOKEN_CONTRACT_ADDRESS = " 0xeD2.....12Ee"
32
+ const RECIPIENT_ADDRESS = " 0x8e9......3EA2"
33
+ const YOUR_PRIVATE_KEY = " 69......929"
36
34
37
- const client = new SponsorClient (SPONSOR_URL , null , { staticNetwork: ethers .Network .from (Number (CHAIN_ID )) });
35
+ const paymasterClient = new PaymasterClient (PAYMASTER_URL );
36
+ const network = await paymasterClient .getNetwork ();
37
+ const sponsorClient = new SponsorClient (SPONSOR_URL , null , {staticNetwork: ethers .Network .from (Number (network .chainId ))});
38
38
39
39
try {
40
40
// sponsor the tx that interact with the stable coin ERC20 contract
41
- const res1 = await client .addToWhitelist ({
41
+ const res = await sponsorClient .addToWhitelist ({
42
42
PolicyUUID: POLICY_UUID ,
43
43
WhitelistType: WhitelistType .ToAccountWhitelist ,
44
44
Values: [RECIPIENT_ADDRESS ]
45
45
});
46
- console .log (" Added ERC20 contract address to whitelist " , res1 );
47
- } catch (error){
46
+ console .log (" Added ERC20 contract address to whitelist " , res );
47
+ } catch (error) {
48
48
console .error (" Error:" , error)
49
49
}
50
50
51
- // Provider for assembling the transaction (e.g., mainnet)
52
- const assemblyProvider = new ethers.JsonRpcProvider (CHAIN_URL );
53
-
54
- // Provider for sending the transaction (e.g., could be a different network or provider)
55
- const paymasterProvider = new PaymasterClient (PAYMASTER_URL );
56
-
57
- const wallet = new ethers.Wallet (YOUR_PRIVATE_KEY , assemblyProvider);
58
- // ERC20 token ABI (only including the transfer function)
59
- const tokenAbi = [
60
- " function transfer(address,uint256) returns (bool)"
61
- ];
62
-
63
- // Create contract instance
64
- const tokenContract = new ethers.Contract (TOKEN_CONTRACT_ADDRESS , tokenAbi, wallet);
65
-
66
- // Transaction details
51
+ const wallet = new ethers.Wallet (YOUR_PRIVATE_KEY );
52
+ const tokenAbi = [" function transfer(address,uint256) returns (bool)" ]; // ERC20 token ABI (only including the transfer function)
53
+ const tokenContract = new ethers.Contract (TOKEN_CONTRACT_ADDRESS , tokenAbi, wallet); // Create contract instance
54
+ // Get the current nonce for the sender's address
55
+ const nonce = await paymasterClient .getTransactionCount (wallet .address , ' pending' );
67
56
const tokenAmount = ethers .parseUnits (' 1.0' , 18 ); // Amount of tokens to send (adjust decimals as needed)
57
+ const transaction = await tokenContract .transfer .populateTransaction (RECIPIENT_ADDRESS , tokenAmount);
58
+
59
+ // Add nonce and gas settings
60
+ transaction .from = wallet .address ;
61
+ transaction .nonce = nonce;
62
+ transaction .gasLimit = 100000 ; // Adjust gas limit as needed for token transfers
63
+ transaction .chainId = network .chainId ;
64
+ transaction .gasPrice = 0 ; // Set gas price to 0
68
65
69
66
try {
70
- // Get the current nonce for the sender's address
71
- const nonce = await assemblyProvider .getTransactionCount (wallet .address );
72
-
73
- // Create the transaction object
74
- const transaction = await tokenContract .transfer .populateTransaction (RECIPIENT_ADDRESS , tokenAmount);
75
-
76
- // Add nonce and gas settings
77
- transaction .from = wallet .address ;
78
- transaction .nonce = nonce;
79
- transaction .gasLimit = 100000 ; // Adjust gas limit as needed for token transfers
80
- transaction .chainId = 97 ;
81
- transaction .gasPrice = 0 ; // Set gas price to 0
82
-
83
- try {
84
- const sponsorableInfo = await paymasterProvider .isSponsorable (transaction);
85
- console .log (' Sponsorable Information:' , sponsorableInfo);
86
- } catch (error) {
87
- console .error (' Error checking sponsorable status:' , error);
88
- }
67
+ const sponsorableInfo = await paymasterClient .isSponsorable (transaction);
68
+ console .log (' Sponsorable Information:' , sponsorableInfo);
69
+ } catch (error) {
70
+ console .error (' Error checking sponsorable status:' , error);
71
+ }
89
72
73
+ try {
90
74
// Sign the transaction
91
75
const signedTx = await wallet .signTransaction (transaction);
92
-
93
76
// Send the raw transaction using the sending provider
94
- const tx = await paymasterProvider .sendRawTransaction (signedTx);
77
+ const tx = await paymasterClient .sendRawTransaction (signedTx);
95
78
console .log (' Transaction sent:' , tx);
96
-
97
79
} catch (error) {
98
80
console .error (' Error sending transaction:' , error);
99
81
}
0 commit comments