Skip to content

Commit 34b6a86

Browse files
committed
chore: refac readme example
1 parent 5ae45fc commit 34b6a86

File tree

1 file changed

+33
-51
lines changed

1 file changed

+33
-51
lines changed

README.md

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,78 +22,60 @@ This JavaScript SDK is thin wrapper of MegaFuel clients, offering a streamlined
2222
```js
2323
import 'dotenv/config';
2424
import {ethers} from "ethers";
25-
import { PaymasterClient } from 'megafuel-js-sdk';
25+
import {PaymasterClient, SponsorClient} from 'megafuel-js-sdk';
2626

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"
3129

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"
3634

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))});
3838

3939
try {
4040
// sponsor the tx that interact with the stable coin ERC20 contract
41-
const res1 = await client.addToWhitelist({
41+
const res = await sponsorClient.addToWhitelist({
4242
PolicyUUID: POLICY_UUID,
4343
WhitelistType: WhitelistType.ToAccountWhitelist,
4444
Values: [RECIPIENT_ADDRESS]
4545
});
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) {
4848
console.error("Error:", error)
4949
}
5050

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');
6756
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
6865

6966
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+
}
8972

73+
try {
9074
// Sign the transaction
9175
const signedTx = await wallet.signTransaction(transaction);
92-
9376
// Send the raw transaction using the sending provider
94-
const tx = await paymasterProvider.sendRawTransaction(signedTx);
77+
const tx = await paymasterClient.sendRawTransaction(signedTx);
9578
console.log('Transaction sent:', tx);
96-
9779
} catch (error) {
9880
console.error('Error sending transaction:', error);
9981
}

0 commit comments

Comments
 (0)