|
1 |
| -# megafuel-js-sdk |
| 1 | +# megafuel-js-sdk |
| 2 | + |
| 3 | +This JavaScript SDK is thin wrapper of MegaFuel clients, offering a streamlined interface to interact with the MegaFuel. For more information, please refer to the [API documentation](https://docs.nodereal.io/docs/megafuel-api). |
| 4 | + |
| 5 | +## Network Endpoint |
| 6 | + |
| 7 | +| Network | [Paymaster]( https://docs.nodereal.io/reference/pm-issponsorable) | [Sponsor](https://docs.nodereal.io/reference/pm-addtowhitelist) | |
| 8 | +|:-------------:|:-------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------:| |
| 9 | +| BSC mainnet | https://bsc-megafuel.nodereal.io | https://open-platform.nodereal.io/{YOUR_API_KEY}/megafuel | |
| 10 | +| BSC testnet | https://bsc-megafuel-testnet.nodereal.io | https://open-platform.nodereal.io/{YOUR_API_KEY}/megafuel-testnet | |
| 11 | +| opBNB mainnet | https://opbnb-megafuel.nodereal.io | https://open-platform.nodereal.io/{YOUR_API_KEY}/megafuel | |
| 12 | +| opBNB testnet | https://opbnb-megafuel-testnet.nodereal.io | https://open-platform.nodereal.io/{YOUR_API_KEY}/megafuel-testnet | |
| 13 | + |
| 14 | +## Quick Start |
| 15 | +1. Install dependency |
| 16 | + |
| 17 | +```shell |
| 18 | + $ npm install megafuel-js-sdk |
| 19 | + ``` |
| 20 | + |
| 21 | +2. Example |
| 22 | +```js |
| 23 | +import 'dotenv/config'; |
| 24 | +import {ethers} from "ethers"; |
| 25 | +import { PaymasterClient } from 'megafuel-js-sdk'; |
| 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" |
| 31 | + |
| 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" |
| 36 | + |
| 37 | +const client = new SponsorClient(SPONSOR_URL, null, { staticNetwork: ethers.Network.from(Number(CHAIN_ID)) }); |
| 38 | + |
| 39 | +try { |
| 40 | + // sponsor the tx that interact with the stable coin ERC20 contract |
| 41 | + const res1 = await client.addToWhitelist({ |
| 42 | + PolicyUUID: POLICY_UUID, |
| 43 | + WhitelistType: WhitelistType.ToAccountWhitelist, |
| 44 | + Values: [RECIPIENT_ADDRESS] |
| 45 | + }); |
| 46 | + console.log("Added ERC20 contract address to whitelist ", res1); |
| 47 | +} catch (error){ |
| 48 | + console.error("Error:", error) |
| 49 | +} |
| 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 |
| 67 | +const tokenAmount = ethers.parseUnits('1.0', 18); // Amount of tokens to send (adjust decimals as needed) |
| 68 | + |
| 69 | +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 | + } |
| 89 | + |
| 90 | + // Sign the transaction |
| 91 | + const signedTx = await wallet.signTransaction(transaction); |
| 92 | + |
| 93 | + // Send the raw transaction using the sending provider |
| 94 | + const tx = await paymasterProvider.sendRawTransaction(signedTx); |
| 95 | + console.log('Transaction sent:', tx); |
| 96 | + |
| 97 | +} catch (error) { |
| 98 | + console.error('Error sending transaction:', error); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +More examples can be found in the [examples](https://github.com/node-real/megafuel-client-example). |
| 103 | + |
0 commit comments