Skip to content

Commit

Permalink
Merge pull request #534 from multiversx/TOOL-355-add-relayed-controllers
Browse files Browse the repository at this point in the history
Add relayed controller
  • Loading branch information
danielailie authored Nov 14, 2024
2 parents b98b5b5 + 54a869c commit 5e562c0
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 50 deletions.
40 changes: 20 additions & 20 deletions src/delegation/delegationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TransactionsFactoryConfig } from "../transactionsFactories";
import { TransactionWatcher } from "../transactionWatcher";
import { DelegationTransactionsFactory } from "./delegationTransactionsFactory";
import { DelegationTransactionsOutcomeParser } from "./delegationTransactionsOutcomeParser";
import * as inputs from "./resources";
import * as resources from "./resources";

export class DelegationController {
private transactionAwaiter: TransactionWatcher;
Expand All @@ -27,7 +27,7 @@ export class DelegationController {
async createTransactionForNewDelegationContract(
sender: IAccount,
nonce: bigint,
options: inputs.NewDelegationContractInput,
options: resources.NewDelegationContractInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForNewDelegationContract(sender.address, options);

Expand All @@ -49,7 +49,7 @@ export class DelegationController {
async createTransactionForAddingNodes(
sender: IAccount,
nonce: bigint,
options: inputs.AddNodesInput,
options: resources.AddNodesInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForAddingNodes(sender.address, options);

Expand All @@ -62,7 +62,7 @@ export class DelegationController {
async createTransactionForRemovingNodes(
sender: IAccount,
nonce: bigint,
options: inputs.ManageNodesInput,
options: resources.ManageNodesInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForRemovingNodes(sender.address, options);

Expand All @@ -75,7 +75,7 @@ export class DelegationController {
async createTransactionForStakingNodes(
sender: IAccount,
nonce: bigint,
options: inputs.ManageNodesInput,
options: resources.ManageNodesInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForStakingNodes(sender.address, options);

Expand All @@ -88,7 +88,7 @@ export class DelegationController {
async createTransactionForUnbondingNodes(
sender: IAccount,
nonce: bigint,
options: inputs.ManageNodesInput,
options: resources.ManageNodesInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForUnbondingNodes(sender.address, options);

Expand All @@ -101,7 +101,7 @@ export class DelegationController {
async createTransactionForUnstakingNodes(
sender: IAccount,
nonce: bigint,
options: inputs.ManageNodesInput,
options: resources.ManageNodesInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForUnstakingNodes(sender.address, options);

Expand All @@ -114,7 +114,7 @@ export class DelegationController {
async createTransactionForUnjailingNodes(
sender: IAccount,
nonce: bigint,
options: inputs.UnjailingNodesInput,
options: resources.UnjailingNodesInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForUnjailingNodes(sender.address, options);

Expand All @@ -127,7 +127,7 @@ export class DelegationController {
async createTransactionForChangingServiceFee(
sender: IAccount,
nonce: bigint,
options: inputs.ChangeServiceFee,
options: resources.ChangeServiceFee,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForChangingServiceFee(sender.address, options);

Expand All @@ -140,7 +140,7 @@ export class DelegationController {
async createTransactionForModifyingDelegationCap(
sender: IAccount,
nonce: bigint,
options: inputs.ModifyDelegationCapInput,
options: resources.ModifyDelegationCapInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForModifyingDelegationCap(sender.address, options);

Expand All @@ -153,7 +153,7 @@ export class DelegationController {
async createTransactionForSettingAutomaticActivation(
sender: IAccount,
nonce: bigint,
options: inputs.ManageDelegationContractInput,
options: resources.ManageDelegationContractInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForSettingAutomaticActivation(sender.address, options);

Expand All @@ -166,7 +166,7 @@ export class DelegationController {
async createTransactionForUnsettingAutomaticActivation(
sender: IAccount,
nonce: bigint,
options: inputs.ManageDelegationContractInput,
options: resources.ManageDelegationContractInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForUnsettingAutomaticActivation(sender.address, options);

Expand All @@ -179,7 +179,7 @@ export class DelegationController {
async createTransactionForSettingCapCheckOnRedelegateRewards(
sender: IAccount,
nonce: bigint,
options: inputs.ManageDelegationContractInput,
options: resources.ManageDelegationContractInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForSettingCapCheckOnRedelegateRewards(
sender.address,
Expand All @@ -195,7 +195,7 @@ export class DelegationController {
async createTransactionForUnsettingCapCheckOnRedelegateRewards(
sender: IAccount,
nonce: bigint,
options: inputs.ManageDelegationContractInput,
options: resources.ManageDelegationContractInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForUnsettingCapCheckOnRedelegateRewards(
sender.address,
Expand All @@ -211,7 +211,7 @@ export class DelegationController {
async createTransactionForSettingMetadata(
sender: IAccount,
nonce: bigint,
options: inputs.SetContractMetadataInput,
options: resources.SetContractMetadataInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForSettingMetadata(sender.address, options);

Expand All @@ -224,7 +224,7 @@ export class DelegationController {
async createTransactionForDelegating(
sender: IAccount,
nonce: bigint,
options: inputs.DelegateActionsInput,
options: resources.DelegateActionsInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForDelegating(sender.address, options);

Expand All @@ -237,7 +237,7 @@ export class DelegationController {
async createTransactionForClaimingRewards(
sender: IAccount,
nonce: bigint,
options: inputs.ManageDelegationContractInput,
options: resources.ManageDelegationContractInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForClaimingRewards(sender.address, options);

Expand All @@ -250,7 +250,7 @@ export class DelegationController {
async createTransactionForRedelegatingRewards(
sender: IAccount,
nonce: bigint,
options: inputs.ManageDelegationContractInput,
options: resources.ManageDelegationContractInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForRedelegatingRewards(sender.address, options);

Expand All @@ -263,7 +263,7 @@ export class DelegationController {
async createTransactionForUndelegating(
sender: IAccount,
nonce: bigint,
options: inputs.DelegateActionsInput,
options: resources.DelegateActionsInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForUndelegating(sender.address, options);

Expand All @@ -276,7 +276,7 @@ export class DelegationController {
async createTransactionForWithdrawing(
sender: IAccount,
nonce: bigint,
options: inputs.ManageDelegationContractInput,
options: resources.ManageDelegationContractInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForWithdrawing(sender.address, options);

Expand Down
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";
49 changes: 49 additions & 0 deletions src/relayed/relayedController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { IAccount } from "../accounts/interfaces";
import { Transaction } from "../transaction";
import { TransactionComputer } from "../transactionComputer";
import { TransactionsFactoryConfig } from "../transactionsFactories";
import { RelayedTransactionsFactory } from "./relayedTransactionsFactory";
import { RelayedV1TransactionInput, RelayedV2TransactionInput } 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,
nonce: bigint,
options: RelayedV1TransactionInput,
): Promise<Transaction> {
const transaction = this.factory.createRelayedV1Transaction(sender.address, options);

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

return transaction;
}

async createRelayedV2Transaction(
sender: IAccount,
nonce: bigint,
options: RelayedV2TransactionInput,
): Promise<Transaction> {
const transaction = this.factory.createRelayedV2Transaction(sender.address, options);

transaction.nonce = 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 All @@ -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
Loading

0 comments on commit 5e562c0

Please sign in to comment.