Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development to main #1052

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/sdk-and-tools/sdk-js/sdk-js-cookbook-v13.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading