-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cookbook example for working with relayer
- Loading branch information
1 parent
81802a5
commit 47509fd
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
packages/cookbook/transactions/meta-transaction-relayer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}()); | ||
} |