From 7e2331b84515e7a11b65fd081bc3a5260231c2e2 Mon Sep 17 00:00:00 2001 From: James Heal <790364833@qq.com> Date: Mon, 25 Sep 2023 14:24:51 +0800 Subject: [PATCH] feat: export processeor in utils (#27) --- package.json | 2 +- .../action/processor/common/sign-info.ts | 5 ++-- src/modules/utils/index.ts | 4 +-- src/modules/utils/internal-utils.ts | 29 ++++++++++--------- src/types/utils.ts | 8 +++-- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 07eb627..a2e66f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nftgo/gotrading", - "version": "1.0.9", + "version": "1.0.10", "main": "dist/index.js", "module": "dist/index.esm.js", "types": "dist/index.d.ts", diff --git a/src/modules/utils/action/processor/common/sign-info.ts b/src/modules/utils/action/processor/common/sign-info.ts index 5547586..8a2141f 100644 --- a/src/modules/utils/action/processor/common/sign-info.ts +++ b/src/modules/utils/action/processor/common/sign-info.ts @@ -1,8 +1,9 @@ import { Transaction } from '@/types'; +import { SafeAny } from 'src/types/safe-any'; -type SendTransactionFn = (params: any) => Transaction; +type SendTransactionFn = (params: SafeAny) => Transaction; -export async function signInfo(params: any, sendTransaction: SendTransactionFn): Promise { +export async function signInfo(params: SafeAny, sendTransaction: SendTransactionFn): Promise { return new Promise((resolve, reject) => { sendTransaction(params) .on('error', err => { diff --git a/src/modules/utils/index.ts b/src/modules/utils/index.ts index 6858e54..94b6021 100644 --- a/src/modules/utils/index.ts +++ b/src/modules/utils/index.ts @@ -5,14 +5,12 @@ import { AggregateActionProcessor } from './action/processor'; export function createUtils(config: Config, http: HTTPClient): Utils { const internalUtils = new InternalAggregatorUtils(config, http); - const processor = new AggregateActionProcessor(internalUtils, config, http); internalUtils.createActionExecutor = (actions: AggregatorAction[]) => { return new BrowserActionTaskExecutor(actions, processor); }; - + internalUtils.processor = processor; const utils = internalUtils as Utils; - return utils; } diff --git a/src/modules/utils/internal-utils.ts b/src/modules/utils/internal-utils.ts index 3846039..4839e28 100644 --- a/src/modules/utils/internal-utils.ts +++ b/src/modules/utils/internal-utils.ts @@ -17,20 +17,21 @@ import { Config, AggregatorAction, InternalUtils, - Authenticator, HTTPClient, BlurAuthenticator, X2Y2Authenticator, + ActionProcessor, } from '@/types'; import { UtilsException } from '@/exceptions'; import { BlurMarketAuthenticator } from './blur-auth'; import { X2Y2MarketplaceAuthenticator } from './x2y2-auth'; +import { SafeAny } from 'src/types/safe-any'; export class InternalAggregatorUtils implements InternalUtils { private provider?: provider; private walletConfig?: WalletConfig; public _ethersProvider: ethers.providers.Web3Provider; - public _ethersSigner: any; + public _ethersSigner: SafeAny; public _web3Instance: Web3; public account: string | undefined = this.walletConfig?.address; public blurAccessToken: string | undefined; @@ -41,15 +42,15 @@ export class InternalAggregatorUtils implements InternalUtils { private PUNK_BOUGHT_TOPIC: string; public blurAuthenticator: BlurAuthenticator; public x2y2Authenticator: X2Y2Authenticator; - + public processor?: ActionProcessor | undefined; constructor(config: Config, client: HTTPClient) { this.provider = config.web3Provider; this.walletConfig = config.walletConfig; - this._web3Instance = new Web3(this.provider || (globalThis as any)?.ethereum); + this._web3Instance = new Web3(this.provider || (globalThis as SafeAny)?.ethereum); this.x2y2Authenticator = new X2Y2MarketplaceAuthenticator(this._web3Instance); - this._ethersProvider = new ethers.providers.Web3Provider(this.provider || (globalThis as any)?.ethereum); + this._ethersProvider = new ethers.providers.Web3Provider(this.provider || (globalThis as SafeAny)?.ethereum); if (this.walletConfig) { if (typeof this.walletConfig?.address !== 'string') { throw UtilsException.invalidParamError('walletConfig.address'); @@ -61,7 +62,7 @@ export class InternalAggregatorUtils implements InternalUtils { this._ethersSigner = this.provider ? new ethers.Wallet( this.walletConfig.privateKey, - new ethers.providers.JsonRpcProvider((this.provider as any).host) + new ethers.providers.JsonRpcProvider((this.provider as SafeAny).host) ) : this._ethersProvider.getSigner(this.walletConfig?.address); @@ -196,7 +197,7 @@ export class InternalAggregatorUtils implements InternalUtils { this._web3Instance.eth.getTransactionCount(transactionConfig.from as string).then(nonce => { transactionConfig.value = BigNumber.isBigNumber(transactionConfig.value) ? transactionConfig.value - : (BigNumber.from(transactionConfig.value) as any); + : (BigNumber.from(transactionConfig.value) as SafeAny); transactionConfig.nonce = nonce; transactionConfig.type = 2; const priorityFee = BigNumber.from(transactionConfig?.maxPriorityFeePerGas || 2000000000); @@ -223,7 +224,7 @@ export class InternalAggregatorUtils implements InternalUtils { }); }; // Client - if ((globalThis as any).ethereum) { + if ((globalThis as SafeAny).ethereum) { this._web3Instance.eth .sign(unsignedTransactionHash, transactionConfig.from as string) .then(async signedTransaction => { @@ -246,7 +247,7 @@ export class InternalAggregatorUtils implements InternalUtils { const signedTrx = ethers.utils.serializeTransaction(transactionConfig, signedTransaction.signature); flashBotsSendTx(signedTrx); } catch (error) { - transactionInstance.errorHandler?.(error as any); + transactionInstance.errorHandler?.(error as SafeAny); } finally { transactionInstance.finallyHandler?.(); } @@ -274,9 +275,9 @@ export class InternalAggregatorUtils implements InternalUtils { .then(estimateGas => { // some wallet(eg: coinbase wallet) will inject providers object into window, which provide all providers available in current browser transactionConfig.gas = BigNumber.from(estimateGas).toHexString(); - if (typeof (globalThis as any)?.ethereum === 'object') { - let finalProvider = (globalThis as any)?.ethereum as any; - if (finalProvider?.providers && (this._web3Instance.currentProvider as any)?.isMetaMask) { + if (typeof (globalThis as SafeAny)?.ethereum === 'object') { + let finalProvider = (globalThis as SafeAny)?.ethereum as SafeAny; + if (finalProvider?.providers && (this._web3Instance.currentProvider as SafeAny)?.isMetaMask) { finalProvider = finalProvider?.providers.filter( (provider: { isMetaMask: boolean }) => provider.isMetaMask )[0]; @@ -323,8 +324,8 @@ export class InternalAggregatorUtils implements InternalUtils { }; signMessage = async (message: string): Promise => { - if ((globalThis as any).ethereum) { - const provider = (globalThis as any).ethereum; + if ((globalThis as SafeAny).ethereum) { + const provider = (globalThis as SafeAny).ethereum; const accounts = await provider.request({ method: 'eth_requestAccounts' }); const account = accounts[0]; const signature = await this._web3Instance.eth.personal.sign(message, account, ''); diff --git a/src/types/utils.ts b/src/types/utils.ts index b9a62a1..3a81d53 100644 --- a/src/types/utils.ts +++ b/src/types/utils.ts @@ -1,7 +1,8 @@ -import { ActionKind, ActionTaskExecutor, AggregatorAction } from './action'; +import { ActionKind, ActionProcessor, ActionTaskExecutor, AggregatorAction } from './action'; import { ethers } from 'ethers'; import { Log, TransactionConfig, TransactionReceipt } from 'web3-core'; import { BlurAuthenticator, X2Y2Authenticator } from './authenticator'; +import { SafeAny } from './safe-any'; export interface InternalUtils { blurAuthenticator: BlurAuthenticator; @@ -15,7 +16,7 @@ export interface InternalUtils { * @returns res {@link DecodeLogRes} */ decodeLog(log: Log): DecodeLogRes | null; - + processor?: ActionProcessor; /** * Send transaction with safe mode, using flash bot * - details: {@link } @@ -42,11 +43,12 @@ export interface InternalUtils { signMessage(message: string): Promise; // TODO: signer - getSigner(): any; + getSigner(): SafeAny; } export interface Utils extends InternalUtils { createActionExecutor(actions: AggregatorAction[]): ActionTaskExecutor; + processor: ActionProcessor; } export interface DecodeLogRes {