Skip to content

Commit

Permalink
Update relayed to follow structions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Nov 14, 2024
1 parent 198e69c commit 54a869c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/relayed/relayedController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class RelayedController {
nonce: bigint,
options: RelayedV1TransactionInput,
): Promise<Transaction> {
const transaction = this.factory.createRelayedV1Transaction({ ...options, relayerAddress: sender.address });
const transaction = this.factory.createRelayedV1Transaction(sender.address, options);

transaction.nonce = nonce;
transaction.signature = await sender.sign(this.txComputer.computeBytesForSigning(transaction));
Expand All @@ -38,7 +38,7 @@ export class RelayedController {
nonce: bigint,
options: RelayedV2TransactionInput,
): Promise<Transaction> {
const transaction = this.factory.createRelayedV2Transaction({ ...options, relayerAddress: sender.address });
const transaction = this.factory.createRelayedV2Transaction(sender.address, options);

transaction.nonce = nonce;
transaction.gasLimit = BigInt(0);
Expand Down
28 changes: 10 additions & 18 deletions src/relayed/relayedTransactionsFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ describe("test relayed transactions factory", function () {
});

assert.throws(() => {
factory.createRelayedV1Transaction({ innerTransaction: innerTransaction, relayerAddress: bob.address }),
factory.createRelayedV1Transaction(bob.address, { innerTransaction: innerTransaction }),
"The inner transaction is not signed";
});

innerTransaction.gasLimit = 0n;
innerTransaction.signature = Buffer.from("invalidsignature");

assert.throws(() => {
factory.createRelayedV1Transaction({ innerTransaction: innerTransaction, relayerAddress: bob.address }),
factory.createRelayedV1Transaction(bob.address, { innerTransaction: innerTransaction }),
"The gas limit is not set for the inner transaction";
});
});
Expand All @@ -51,9 +51,8 @@ describe("test relayed transactions factory", function () {
const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction);
innerTransaction.signature = await bob.signer.sign(serializedInnerTransaction);

const relayedTransaction = factory.createRelayedV1Transaction({
const relayedTransaction = factory.createRelayedV1Transaction(alice.getAddress(), {
innerTransaction: innerTransaction,
relayerAddress: alice.address,
});
relayedTransaction.nonce = 2627n;

Expand Down Expand Up @@ -85,9 +84,8 @@ describe("test relayed transactions factory", function () {
const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction);
innerTransaction.signature = await carol.signer.sign(serializedInnerTransaction);

const relayedTransaction = factory.createRelayedV1Transaction({
const relayedTransaction = factory.createRelayedV1Transaction(frank.getAddress(), {
innerTransaction: innerTransaction,
relayerAddress: frank.address,
});
relayedTransaction.nonce = 715n;

Expand Down Expand Up @@ -119,9 +117,8 @@ describe("test relayed transactions factory", function () {
const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction);
innerTransaction.signature = await carol.signer.sign(serializedInnerTransaction);

const relayedTransaction = factory.createRelayedV1Transaction({
const relayedTransaction = factory.createRelayedV1Transaction(frank.getAddress(), {
innerTransaction: innerTransaction,
relayerAddress: frank.address,
});
relayedTransaction.nonce = 715n;

Expand Down Expand Up @@ -155,9 +152,8 @@ describe("test relayed transactions factory", function () {
innerTransaction.signature = await bob.signer.sign(serializedInnerTransaction);
innerTransaction.guardianSignature = await grace.signer.sign(serializedInnerTransaction);

const relayedTransaction = factory.createRelayedV1Transaction({
const relayedTransaction = factory.createRelayedV1Transaction(alice.getAddress(), {
innerTransaction: innerTransaction,
relayerAddress: alice.address,
});
relayedTransaction.nonce = 2627n;

Expand Down Expand Up @@ -191,9 +187,8 @@ describe("test relayed transactions factory", function () {
innerTransaction.signature = await bob.signer.sign(serializedInnerTransaction);
innerTransaction.guardianSignature = await grace.signer.sign(serializedInnerTransaction);

const relayedTransaction = factory.createRelayedV1Transaction({
const relayedTransaction = factory.createRelayedV1Transaction(alice.getAddress(), {
innerTransaction: innerTransaction,
relayerAddress: alice.address,
});
relayedTransaction.nonce = 2627n;
relayedTransaction.options = 2;
Expand Down Expand Up @@ -222,21 +217,19 @@ describe("test relayed transactions factory", function () {
});

assert.throws(() => {
factory.createRelayedV2Transaction({
factory.createRelayedV2Transaction(carol.getAddress(), {
innerTransaction: innerTransaction,
innerTransactionGasLimit: 50000n,
relayerAddress: carol.address,
}),
"The gas limit should not be set for the inner transaction";
});

innerTransaction.gasLimit = 0n;

assert.throws(() => {
factory.createRelayedV2Transaction({
factory.createRelayedV2Transaction(carol.getAddress(), {
innerTransaction: innerTransaction,
innerTransactionGasLimit: 50000n,
relayerAddress: carol.address,
}),
"The inner transaction is not signed";
});
Expand All @@ -257,10 +250,9 @@ describe("test relayed transactions factory", function () {
const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction);
innerTransaction.signature = await bob.signer.sign(serializedInnerTransaction);

const relayedTransaction = factory.createRelayedV2Transaction({
const relayedTransaction = factory.createRelayedV2Transaction(alice.getAddress(), {
innerTransaction: innerTransaction,
innerTransactionGasLimit: 60000000n,
relayerAddress: alice.address,
});
relayedTransaction.nonce = 37n;

Expand Down
20 changes: 11 additions & 9 deletions src/relayed/relayedTransactionsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BigNumber from "bignumber.js";
import { AddressValue, ArgSerializer, BytesValue, U64Value } from "../abi";
import { Address } from "../address";
import { ErrInvalidInnerTransaction } from "../errors";
import { IAddress, ITransaction } from "../interface";
import { ITransaction } from "../interface";
import { Transaction } from "../transaction";

const JSONbig = require("json-bigint");
Expand All @@ -23,7 +23,7 @@ export class RelayedTransactionsFactory {
this.config = options.config;
}

createRelayedV1Transaction(options: { innerTransaction: ITransaction; relayerAddress: IAddress }): Transaction {
createRelayedV1Transaction(relayerAddress: Address, options: { innerTransaction: ITransaction }): Transaction {
if (!options.innerTransaction.gasLimit) {
throw new ErrInvalidInnerTransaction("The gas limit is not set for the inner transaction");
}
Expand All @@ -40,18 +40,20 @@ export class RelayedTransactionsFactory {

return new Transaction({
chainID: this.config.chainID,
sender: options.relayerAddress.bech32(),
sender: relayerAddress.bech32(),
receiver: options.innerTransaction.sender,
gasLimit: gasLimit,
data: Buffer.from(data),
});
}

createRelayedV2Transaction(options: {
innerTransaction: ITransaction;
innerTransactionGasLimit: bigint;
relayerAddress: IAddress;
}): Transaction {
createRelayedV2Transaction(
relayerAddress: Address,
options: {
innerTransaction: ITransaction;
innerTransactionGasLimit: bigint;
},
): Transaction {
if (options.innerTransaction.gasLimit) {
throw new ErrInvalidInnerTransaction("The gas limit should not be set for the inner transaction");
}
Expand All @@ -73,7 +75,7 @@ export class RelayedTransactionsFactory {
const gasLimit = options.innerTransactionGasLimit + this.config.minGasLimit + additionalGasForDataLength;

return new Transaction({
sender: options.relayerAddress.bech32(),
sender: relayerAddress.bech32(),
receiver: options.innerTransaction.sender,
value: 0n,
gasLimit: gasLimit,
Expand Down

0 comments on commit 54a869c

Please sign in to comment.