diff --git a/docs/sdk-and-tools/sdk-js/sdk-js-cookbook-v13.md b/docs/sdk-and-tools/sdk-js/sdk-js-cookbook-v13.md index 41e96b1ef..2d58b02e0 100644 --- a/docs/sdk-and-tools/sdk-js/sdk-js-cookbook-v13.md +++ b/docs/sdk-and-tools/sdk-js/sdk-js-cookbook-v13.md @@ -107,6 +107,48 @@ const tx = new Transaction({ tx.nonce = 42n; ``` +### Preparing a relayed transaction + +We are currently on the third iteration of relayed transactions. V1 and V2 are soon to be deactivated so we'll focus on V3. +For V3, two new fields have been added on transactions: `relayer` and `relayerSignature`. +Before the sender signs the transaction, the relayer needs to be set. After the sender has signed the transaction, the relayer can also sign the transaction and broadcast it. +Keep in mind that, for relayed V3 transactions we need an extra 50_000 gas. Let's see how we can create a relayed transaction: + +```js +import { Transaction } from "@multiversx/sdk-core"; + +const grace = await loadTestWallet("grace"); + +# alice will be our relayer, that means she is paying the gas for the transaction +const alice = await loadTestWallet("alice"); +const transactionComputer = new TransactionComputer(); + +# fetch the sender nonce of the network +const nonce = (await apiProvider.getAccount(grace.getAddress())).nonce; +# create the transaction +const transaction = new Transaction({ + receiver: grace.getAddress().bech32(), + sender: grace.getAddress().bech32(), + gasPrice: BigInt(1000000000), + gasLimit: BigInt(150000), + chainID: "D", + nonce: BigInt(nonce), + relayer: alice.getAddress(), + value: BigInt(1), +}); + +# sender signs the transaction +transaction.signature = await grace.signer.sign(transactionComputer.computeBytesForSigning(transaction)); +const buffer = transactionComputer.computeBytesForSigning(transaction); + +# relayer signs the transaction +const signature = await alice.signer.sign(Buffer.from(buffer)); +transaction.relayerSignature = signature; + +# broadcast the transaction +await proxyProvider.sendTransaction(transaction); +``` + ### Signing a transaction :::important