Skip to content

Commit

Permalink
feat: cookbook example for working with relayer
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-haynes committed Apr 3, 2023
1 parent 81802a5 commit 47509fd
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/cookbook/transactions/meta-transaction-relayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { Account } = require('@near-js/accounts');
const { UnencryptedFileSystemKeyStore } = require('@near-js/keystores-node');
const { JsonRpcProvider } = require('@near-js/providers');
const { InMemorySigner } = require('@near-js/signers');
const { actionCreators } = require('@near-js/transactions');
const BN = require('bn.js');
const os = require('os');
const path = require('path');

const { transfer } = actionCreators;

async function sendNearThroughRelayer({ amount, receiverId, senderAccount }) {
const response = await senderAccount.signAndSendSignedDelegate({
actions: [transfer(amount)],
receiverId,
relayerUrl: 'https://relayer.org/relay',
});

console.log(response);
return response;
}

module.exports = {
sendNearThroughRelayer,
};

if (require.main === module) {
(async function () {
const networkId = 'testnet';
const provider = new JsonRpcProvider({ url: 'https://rpc.testnet.near.org' });

const CREDENTIALS_DIR = '.near-credentials';
const credentialsPath = path.join(os.homedir(), CREDENTIALS_DIR);

const RECEIVER_ACCOUNT_ID = 'receiver.testnet'; // the ultimate recipient of the meta transaction execution
const SENDER_ACCOUNT_ID = 'sender.testnet'; // the account requesting the transaction be executed

const senderAccount = new Account({
networkId,
provider,
signer: new InMemorySigner(new UnencryptedFileSystemKeyStore(credentialsPath))
}, SENDER_ACCOUNT_ID);

await sendNearThroughRelayer({
amount: new BN('1000000000'),
receiverId: RECEIVER_ACCOUNT_ID,
senderAccount,
});
}());
}

0 comments on commit 47509fd

Please sign in to comment.