Skip to content

Commit

Permalink
address @yorhodes comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nbayindirli committed Apr 30, 2024
1 parent 6c3d62a commit 08f6b66
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 130 deletions.
4 changes: 2 additions & 2 deletions typescript/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"cross-fetch": "^3.1.5",
"ethers": "^5.7.2",
"pino": "^8.19.0",
"stack-typescript": "^1.0.4",
"viem": "^1.20.0",
"zod": "^3.21.2"
},
Expand Down Expand Up @@ -67,7 +66,8 @@
],
"license": "Apache-2.0",
"scripts": {
"build": "tsc",
"build": "yarn build:fixSafeLib && tsc",
"build:fixSafeLib": "rm -rf ../../node_modules/@safe-global/protocol-kit/dist/typechain/",
"dev": "tsc --watch",
"check": "tsc --noEmit",
"clean": "rm -rf ./dist ./cache",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,5 @@ export interface TxSubmitterInterface<
* Should execute all transactions and return their receipts.
* @param txs The array of transactions to execute
*/
submitTxs(txs: TX[]): Promise<TR[] | void>;
/**
* Should execute a transaction and return its receipt.
* @param tx The transaction to execute
*/
submitTx?(tx: TX): Promise<TR | void>;
submit(...txs: TX[]): Promise<TR[] | void>;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum TxSubmitterType {
JSON_RPC = 'Json RPC',
JSON_RPC = 'JSON RPC',
IMPERSONATED_ACCOUNT = 'Impersonated Account',
GNOSIS_SAFE = 'Gnosis Safe',
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import assert from 'assert';
import { Logger } from 'pino';
import { Stack } from 'stack-typescript';

import { rootLogger } from '@hyperlane-xyz/utils';

import {
TypedTransaction,
TypedTransactionReceipt,
} from '../../../ProviderType.js';
import { TxTransformerInterface } from '../../transformer/TxTransformer.js';
import { TxSubmitterInterface } from '../TxSubmitter.js';
import { TxTransformerInterface } from '../../transformer/TxTransformerInterface.js';
import { TxSubmitterInterface } from '../TxSubmitterInterface.js';

/**
* Builds a TxSubmitterBuilder for batch transaction submission.
Expand Down Expand Up @@ -38,12 +36,9 @@ export class TxSubmitterBuilder<
module: 'submitter-builder',
});

private currentSubmitter?: TxSubmitterInterface<TX, TR>;

constructor(
private readonly currentTransformers: Stack<
TxTransformerInterface<TX>
> = new Stack<TxTransformerInterface<TX>>(),
private currentSubmitter: TxSubmitterInterface<TX, TR>,
private readonly currentTransformers: TxTransformerInterface<TX>[] = [],
) {}

/**
Expand All @@ -64,11 +59,6 @@ export class TxSubmitterBuilder<
public transform(
txTransformer: TxTransformerInterface<TX>,
): TxSubmitterBuilder<TX, TR> {
assert(
this.currentSubmitter,
'No submitter specified for which to execute the transform.',
);

this.currentTransformers.push(txTransformer);
return this;
}
Expand All @@ -77,33 +67,26 @@ export class TxSubmitterBuilder<
* Submits a set of transactions to the builder.
* @param txs The transactions to submit
*/
public async submit(txs: TX[]): Promise<TR[]> {
assert(
this.currentSubmitter,
'Must specify submitter to submit transactions.',
);

public async submit(...txs: TX[]): Promise<TR[]> {
this.logger.info(
`Submitting ${txs.length} transactions to the ${this.currentSubmitter.txSubmitterType} submitter...`,
);

let transformedTxs = txs;
while (this.currentTransformers.size > 0) {
while (this.currentTransformers.length > 0) {
const currentTransformer: TxTransformerInterface<TX> =
this.currentTransformers.pop();
transformedTxs = await currentTransformer.transformTxs(transformedTxs);
this.currentTransformers.pop()!;
transformedTxs = await currentTransformer.transform(...transformedTxs);
this.logger.info(
`🔄 Transformed ${transformedTxs.length} transactions with the ${currentTransformer.txTransformerType} transformer...`,
);
}

const txReceipts = await this.currentSubmitter.submitTxs(transformedTxs);
const txReceipts = await this.currentSubmitter.submit(...transformedTxs);
this.logger.info(
`✅ Successfully submitted ${transformedTxs.length} transactions to the ${this.currentSubmitter.txSubmitterType} submitter.`,
);

this.currentSubmitter = undefined;

return txReceipts ?? [];
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import SafeApiKit from '@safe-global/api-kit';
import Safe, { EthSafeSignature } from '@safe-global/protocol-kit';
import {
MetaTransactionData,
SafeTransactionData,
} from '@safe-global/safe-core-sdk-types';
import assert from 'assert';
import { Logger } from 'pino';

Expand All @@ -12,31 +16,9 @@ import {
EthersV5Transaction,
EthersV5TransactionReceipt,
} from '../../../ProviderType.js';
import { TxSubmitterInterface } from '../TxSubmitter.js';
import { TxSubmitterInterface } from '../TxSubmitterInterface.js';
import { TxSubmitterType } from '../TxSubmitterTypes.js';

enum OperationType {
Call = 0,
DelegateCall = 1,
}

interface MetaTransactionData {
to: string;
value: string;
data: string;
operation?: OperationType;
}

interface SafeTransactionData extends MetaTransactionData {
operation: OperationType;
safeTxGas: string;
baseGas: string;
gasPrice: string;
gasToken: string;
refundReceiver: string;
nonce: number;
}

interface GnosisSafeTxSubmitterProps {
safeAddress: Address;
signerAddress?: Address;
Expand All @@ -59,7 +41,7 @@ export class GnosisSafeTxSubmitter
public readonly props: GnosisSafeTxSubmitterProps,
) {}

public async submitTxs(txs: EthersV5Transaction[]): Promise<void> {
public async submit(...txs: EthersV5Transaction[]): Promise<void> {
const safe: Safe.default = await getSafe(
this.chain,
this.multiProvider,
Expand All @@ -77,7 +59,7 @@ export class GnosisSafeTxSubmitter
const { to, data, value } = transaction;
assert(
to && data,
'Invalid EthersV5Transaction: Missing required metadata.',
'Invalid EthersV5Transaction: Missing required field to or data.',
);
return { to, data, value: value?.toString() ?? '0' };
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
EthersV5TransactionReceipt,
ProviderType,
} from '../../../ProviderType.js';
import { TxSubmitterInterface } from '../TxSubmitter.js';
import { TxSubmitterInterface } from '../TxSubmitterInterface.js';
import { TxSubmitterType } from '../TxSubmitterTypes.js';

interface ImpersonatedAccountTxSubmitterProps {
Expand All @@ -36,31 +36,24 @@ export class ImpersonatedAccountTxSubmitter
public readonly props: ImpersonatedAccountTxSubmitterProps,
) {}

public async submitTxs(
txs: EthersV5Transaction[],
public async submit(
...txs: EthersV5Transaction[]
): Promise<EthersV5TransactionReceipt[]> {
const receipts: EthersV5TransactionReceipt[] = [];
for (const tx of txs) {
const receipt = await this.submitTx(tx);
receipts.push(receipt);
const signer = await impersonateAccount(this.props.address);
this.multiProvider.setSigner(this.chain, signer);
const receipt: ContractReceipt = await this.multiProvider.sendTransaction(
this.chain,
tx.transaction,
);

this.logger.debug(
`Submitted EthersV5Transaction on ${this.chain}: ${receipt.transactionHash}`,
);

receipts.push({ type: ProviderType.EthersV5, receipt });
}
return receipts;
}

public async submitTx(
tx: EthersV5Transaction,
): Promise<EthersV5TransactionReceipt> {
const signer = await impersonateAccount(this.props.address);
this.multiProvider.setSigner(this.chain, signer);
const receipt: ContractReceipt = await this.multiProvider.sendTransaction(
this.chain,
tx.transaction,
);

this.logger.debug(
`Submitted EthersV5Transaction on ${this.chain}: ${receipt.transactionHash}`,
);

return { type: ProviderType.EthersV5, receipt };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
EthersV5TransactionReceipt,
ProviderType,
} from '../../../ProviderType.js';
import { TxSubmitterInterface } from '../TxSubmitter.js';
import { TxSubmitterInterface } from '../TxSubmitterInterface.js';
import { TxSubmitterType } from '../TxSubmitterTypes.js';

export class JsonRpcTxSubmitter
Expand All @@ -28,29 +28,20 @@ export class JsonRpcTxSubmitter
public readonly chain: ChainName,
) {}

public async submitTxs(
txs: EthersV5Transaction[],
public async submit(
...txs: EthersV5Transaction[]
): Promise<EthersV5TransactionReceipt[]> {
const receipts: EthersV5TransactionReceipt[] = [];
for (const tx of txs) {
const receipt = await this.submitTx(tx);
receipts.push(receipt);
const receipt: ContractReceipt = await this.multiProvider.sendTransaction(
this.chain,
tx.transaction,
);
this.logger.debug(
`Submitted EthersV5Transaction on ${this.chain}: ${receipt.transactionHash}`,
);
receipts.push({ type: ProviderType.EthersV5, receipt });
}
return receipts;
}

public async submitTx(
tx: EthersV5Transaction,
): Promise<EthersV5TransactionReceipt> {
const receipt: ContractReceipt = await this.multiProvider.sendTransaction(
this.chain,
tx.transaction,
);

this.logger.debug(
`Submitted EthersV5Transaction on ${this.chain}: ${receipt.transactionHash}`,
);

return { type: ProviderType.EthersV5, receipt };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,5 @@ export interface TxTransformerInterface<TX extends TypedTransaction> {
* Should transform all transactions of type TX into transactions of type TX.
* @param txs The array of transactions to transform
*/
transformTxs(txs: TX[]): Promise<TX[]>;

/**
* Should transform a transaction of type TX into a transaction of type TX.
* @param tx The transaction to transform
*/
transformTx?(tx: TX): Promise<TX>;
transform(...txs: TX[]): Promise<TX[]>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AccountConfig } from '../../../../middleware/account/types.js';
import { ChainName } from '../../../../types.js';
import { MultiProvider } from '../../../MultiProvider.js';
import { EthersV5Transaction, ProviderType } from '../../../ProviderType.js';
import { TxTransformerInterface } from '../TxTransformer.js';
import { TxTransformerInterface } from '../TxTransformerInterface.js';
import { TxTransformerType } from '../TxTransformerTypes.js';

interface InterchainAccountTxTransformerProps {
Expand All @@ -32,8 +32,8 @@ export class InterchainAccountTxTransformer
public readonly props: InterchainAccountTxTransformerProps,
) {}

public async transformTxs(
txs: EthersV5Transaction[],
public async transform(
...txs: EthersV5Transaction[]
): Promise<EthersV5Transaction[]> {
const destinationChainId = txs[0].transaction.chainId;
assert(
Expand All @@ -46,7 +46,7 @@ export class InterchainAccountTxTransformer
const { to, data, value } = transaction;
assert(
to && data,
'Invalid EthersV5Transaction: Missing required metadata.',
'Invalid EthersV5Transaction: Missing required field to or data.',
);
return { to, data, value };
},
Expand Down
17 changes: 0 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5172,7 +5172,6 @@ __metadata:
pino: "npm:^8.19.0"
prettier: "npm:^2.8.8"
sinon: "npm:^13.0.2"
stack-typescript: "npm:^1.0.4"
ts-node: "npm:^10.8.0"
tsx: "npm:^4.7.1"
typescript: "npm:5.3.3"
Expand Down Expand Up @@ -17313,13 +17312,6 @@ __metadata:
languageName: node
linkType: hard

"linked-list-typescript@npm:^1.0.11":
version: 1.0.15
resolution: "linked-list-typescript@npm:1.0.15"
checksum: 91c87ab00fe4bb9850f169adc62b811ae7e91a743bac1a9fc969a815f20ddd0fb285f84a4d31e98900512234f59297f7beef6b076dd382cf879a319c7a14c68b
languageName: node
linkType: hard

"lint-staged@npm:^12.4.3":
version: 12.5.0
resolution: "lint-staged@npm:12.5.0"
Expand Down Expand Up @@ -21882,15 +21874,6 @@ __metadata:
languageName: node
linkType: hard

"stack-typescript@npm:^1.0.4":
version: 1.0.4
resolution: "stack-typescript@npm:1.0.4"
dependencies:
linked-list-typescript: "npm:^1.0.11"
checksum: a3211a70b01ab4ece788a9c0c4612a132be4019427dc1d9daa7f289b5ab495d4a30cdc947888858eb9225b4c55428530ae60526c56b141f843047db1d878114e
languageName: node
linkType: hard

"stack-utils@npm:^2.0.3":
version: 2.0.6
resolution: "stack-utils@npm:2.0.6"
Expand Down

0 comments on commit 08f6b66

Please sign in to comment.