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

Add relayed controller #534

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from "./logger";
export * from "./message";
export * from "./networkParams";
export * from "./networkProviders";
export * from "./relayed";
export * from "./relayedTransactionV1Builder";
export * from "./relayedTransactionV2Builder";
export * from "./signableMessage";
Expand Down
2 changes: 2 additions & 0 deletions src/relayed/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./relayedController";
export * from "./relayedTransactionsFactory";
41 changes: 41 additions & 0 deletions src/relayed/relayedController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { IAccount } from "../accounts/interfaces";
import { Transaction } from "../transaction";
import { TransactionComputer } from "../transactionComputer";
import { TransactionsFactoryConfig } from "../transactionsFactories";
import { RelayedTransactionsFactory } from "./relayedTransactionsFactory";
import { CreateV1RelayTransactionInput, CreateV2RelayTransactionInput } from "./resources";

export class RelayedController {
private factory: RelayedTransactionsFactory;
private txComputer: TransactionComputer;

/**
* The transactions are created from the perspective of the relayer.
* The 'sender' represents the relayer.
*/
constructor(options: { chainID: string }) {
this.factory = new RelayedTransactionsFactory({
config: new TransactionsFactoryConfig(options),
});
this.txComputer = new TransactionComputer();
}

async createRelayedV1Transaction(sender: IAccount, options: CreateV1RelayTransactionInput): Promise<Transaction> {
const transaction = this.factory.createRelayedV1Transaction({ ...options, relayerAddress: sender.address });

transaction.nonce = options.nonce;
transaction.signature = await sender.sign(this.txComputer.computeBytesForSigning(transaction));

return transaction;
}

async createRelayedV2Transaction(sender: IAccount, options: CreateV2RelayTransactionInput): Promise<Transaction> {
const transaction = this.factory.createRelayedV2Transaction({ ...options, relayerAddress: sender.address });

transaction.nonce = options.nonce;
transaction.gasLimit = BigInt(0);
transaction.signature = await sender.sign(this.txComputer.computeBytesForSigning(transaction));

return transaction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { assert } from "chai";
import { TestWallet, loadTestWallets } from "../testutils";
import { Transaction } from "../transaction";
import { TransactionComputer } from "../transactionComputer";
import { TransactionsFactoryConfig } from "../transactionsFactories/transactionsFactoryConfig";
import { RelayedTransactionsFactory } from "./relayedTransactionsFactory";
import { TransactionsFactoryConfig } from "./transactionsFactoryConfig";

describe("test relayed transactions factory", function () {
const config = new TransactionsFactoryConfig({ chainID: "T" });
Expand Down
8 changes: 8 additions & 0 deletions src/relayed/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ITransaction } from "../interface";

export type CreateV1RelayTransactionInput = { nonce: bigint; innerTransaction: ITransaction };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or "...Relayed..." 💭

export type CreateV2RelayTransactionInput = {
nonce: bigint;
innerTransaction: ITransaction;
innerTransactionGasLimit: bigint;
};
2 changes: 0 additions & 2 deletions src/transactionsFactories/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from "../accountManagement/accountTransactionsFactory";
export * from "./relayedTransactionsFactory";
export * from "./smartContractTransactionsFactory";
export * from "./tokenManagementTransactionsFactory";
export * from "./transactionsFactoryConfig";
Expand Down
Loading