Skip to content

Commit 7a65945

Browse files
authored
Merge pull request #1051 from multiversx/TOOL-444-add-relayed-v-3-documentation
Add relayed v3 documetation
2 parents 82b9c1a + 9963d73 commit 7a65945

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/sdk-and-tools/sdk-js/sdk-js-cookbook-v13.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,48 @@ const tx = new Transaction({
107107
tx.nonce = 42n;
108108
```
109109

110+
### Preparing a relayed transaction
111+
112+
We are currently on the third iteration of relayed transactions. V1 and V2 are soon to be deactivated so we'll focus on V3.
113+
For V3, two new fields have been added on transactions: `relayer` and `relayerSignature`.
114+
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.
115+
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:
116+
117+
```js
118+
import { Transaction } from "@multiversx/sdk-core";
119+
120+
const grace = await loadTestWallet("grace");
121+
122+
# alice will be our relayer, that means she is paying the gas for the transaction
123+
const alice = await loadTestWallet("alice");
124+
const transactionComputer = new TransactionComputer();
125+
126+
# fetch the sender nonce of the network
127+
const nonce = (await apiProvider.getAccount(grace.getAddress())).nonce;
128+
# create the transaction
129+
const transaction = new Transaction({
130+
receiver: grace.getAddress().bech32(),
131+
sender: grace.getAddress().bech32(),
132+
gasPrice: BigInt(1000000000),
133+
gasLimit: BigInt(150000),
134+
chainID: "D",
135+
nonce: BigInt(nonce),
136+
relayer: alice.getAddress(),
137+
value: BigInt(1),
138+
});
139+
140+
# sender signs the transaction
141+
transaction.signature = await grace.signer.sign(transactionComputer.computeBytesForSigning(transaction));
142+
const buffer = transactionComputer.computeBytesForSigning(transaction);
143+
144+
# relayer signs the transaction
145+
const signature = await alice.signer.sign(Buffer.from(buffer));
146+
transaction.relayerSignature = signature;
147+
148+
# broadcast the transaction
149+
await proxyProvider.sendTransaction(transaction);
150+
```
151+
110152
### Signing a transaction
111153

112154
:::important

0 commit comments

Comments
 (0)