Skip to content

Commit

Permalink
feat: export processeor in utils (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHeal committed Sep 25, 2023
1 parent 5ff1562 commit 7e2331b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/modules/utils/action/processor/common/sign-info.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
export async function signInfo(params: SafeAny, sendTransaction: SendTransactionFn): Promise<boolean> {
return new Promise((resolve, reject) => {
sendTransaction(params)
.on('error', err => {
Expand Down
4 changes: 1 addition & 3 deletions src/modules/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActionKind>[]) => {
return new BrowserActionTaskExecutor(actions, processor);
};

internalUtils.processor = processor;
const utils = internalUtils as Utils;

return utils;
}
29 changes: 15 additions & 14 deletions src/modules/utils/internal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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 => {
Expand All @@ -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?.();
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -323,8 +324,8 @@ export class InternalAggregatorUtils implements InternalUtils {
};

signMessage = async (message: string): Promise<string> => {
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, '');
Expand Down
8 changes: 5 additions & 3 deletions src/types/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 }
Expand All @@ -42,11 +43,12 @@ export interface InternalUtils {
signMessage(message: string): Promise<string>;

// TODO: signer
getSigner(): any;
getSigner(): SafeAny;
}

export interface Utils extends InternalUtils {
createActionExecutor(actions: AggregatorAction<ActionKind>[]): ActionTaskExecutor;
processor: ActionProcessor;
}

export interface DecodeLogRes {
Expand Down

0 comments on commit 7e2331b

Please sign in to comment.