Sending NGNT
Gas Stations Network (GSN)
The Code
const Web3 = require('web3');
const {GSNProvider} = require("@openzeppelin/gsn-provider");
const HDNode = require('ethers').utils.HDNode;
const mnemonic = <seed phrase>;
const masterNode = HDNode.fromMnemonic(mnemonic);
const url = <url to Ethereum client>;
const fromAddress = <sender address>;
const toAddress = <recipient address>;
// the next two lines of code assume you're storing addresses & path in a mongoose model called Account
const account = await Account.findOne({address: fromAddress});
const derivationPath = account.path;
const addressNode = masterNode.derivePath(derivationPath);
let privateKey = addressNode.privateKey;
const gsnProvider = new GSNProvider(url, {signKey: privateKey});
const web3Provider = new Web3(gsnProvider);
const provider = web3Provider.eth;
const NGNT = new provider.Contract(NGNTAbi, NGNTContractAddress);
// Use GSN to call transfer function & pay gas fee in NGNT.
await NGNT.methods.transfer(toAddress, valueInKobo).send({from: fromAddress})
// or you can call the transfer function without GSN.
// this requires that the sender address has some Ethereum to pay for gas.
if (privateKey.startsWith('0x')) {
privateKey = privateKey.substring(2);
}
const privateKeyBuffer = Buffer.from(privateKey, 'hex');
const transactionData = await NGNT.methods.transfer(toAccount, valueInKobo).encodeABI();
const txObject = {
nonce: await this.provider.getTransactionCount(fromAddress),
to: toAddress,
gasLimit: <gas limit>, // these determine how much you're willing to pay as gas for the transaction
gasPrice: <gas price>,
data: transactionData,
};
const transaction = new EthereumTx(txObject);
transaction.sign(privateKeyBuffer);
const serializedTransaction = transaction.serialize();
const rawTransaction = '0x' + serializedTransaction.toString('hex');
// it does not matter that the provider is still a GSN provider.
// The sendSignedTransaction method acts like the HttpProvider and sends the transaction the normal way.
provider.sendSignedTransaction(rawTransaction);Last updated
Was this helpful?