Skip to content

Commit

Permalink
chore: addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wainola committed Jun 7, 2024
1 parent 35259b0 commit 88a0922
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 40 deletions.
14 changes: 8 additions & 6 deletions packages/widget/src/controllers/transfers/evm/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import type { UnsignedTransaction, BigNumber } from 'ethers';
import { constants, utils } from 'ethers';
import type { Eip1193Provider } from '../../../interfaces';

type BuildEvmFungibleTransactionsArtifacts = {
pendingEvmApprovalTransactions: UnsignedTransaction[];
pendingTransferTransaction: UnsignedTransaction;
fee: EvmFee;
resourceAmount: BigNumber;
};

/**
* @dev If we did proper validation this shouldn't throw.
* Not sure how to handle if it throws :shrug:
Expand Down Expand Up @@ -42,12 +49,7 @@ export async function buildEvmFungibleTransactions({
pendingTransferTransaction: UnsignedTransaction;
sourceNetwork: Domain | null;
fee: EvmFee;
}): Promise<{
pendingEvmApprovalTransactions: UnsignedTransaction[];
pendingTransferTransaction: UnsignedTransaction;
fee: EvmFee;
resourceAmount: BigNumber;
}> {
}): Promise<BuildEvmFungibleTransactionsArtifacts> {
const evmTransfer = new EVMAssetTransfer();
await evmTransfer.init(new Web3Provider(provider, providerChainId), env);

Expand Down
12 changes: 6 additions & 6 deletions packages/widget/src/controllers/transfers/evm/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
Web3Provider,
type TransactionRequest
} from '@ethersproject/providers';
import type { UnsignedTransaction } from 'ethers';
import type { PopulatedTransaction } from 'ethers';
import type { Eip1193Provider } from '../../../interfaces';
import { estimateEvmGas } from '../../../utils/gas';
import { estimateEvmTransactionsGasCost } from '../../../utils/gas';
import {
FungibleTransferState,
type FungibleTokenTransferController
Expand Down Expand Up @@ -38,11 +38,11 @@ export async function executeNextEvmTransaction(
this.pendingTransferTransaction
);

this.estimatedGas = await estimateEvmGas(
this.estimatedGas = await estimateEvmTransactionsGasCost(
this.sourceNetwork?.chainId as number,
this.walletContext.value?.evmWallet?.provider as Eip1193Provider,
this.walletContext.value?.evmWallet?.address as string,
transactions as UnsignedTransaction[]
transactions as PopulatedTransaction[]
);
} catch (e) {
console.log(e);
Expand All @@ -51,11 +51,11 @@ export async function executeNextEvmTransaction(
this.waitingUserConfirmation = false;
this.waitingTxExecution = false;
this.host.requestUpdate();
await estimateEvmGas(
await estimateEvmTransactionsGasCost(
this.sourceNetwork?.chainId as number,
this.walletContext.value?.evmWallet?.provider as Eip1193Provider,
this.walletContext.value?.evmWallet?.address as string,
transactions as UnsignedTransaction[]
transactions as PopulatedTransaction[]
);
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
} from '@buildwithsygma/sygma-sdk-core';
import { ContextConsumer } from '@lit/context';
import { ethers } from 'ethers';
import type { UnsignedTransaction, BigNumber } from 'ethers';
import type {
UnsignedTransaction,
BigNumber,
PopulatedTransaction
} from 'ethers';
import type { ReactiveController, ReactiveElement } from 'lit';
import type { SubmittableExtrinsic } from '@polkadot/api/types';
import type { ApiPromise, SubmittableResult } from '@polkadot/api';
Expand All @@ -29,7 +33,10 @@ import { validateAddress } from '../../utils';
import type { Eip1193Provider } from '../../interfaces';
import { SdkInitializedEvent } from '../../interfaces';
import { substrateProviderContext } from '../../context/wallet';
import { estimateEvmGas, estimateSubstrateGas } from '../../utils/gas';
import {
estimateEvmTransactionsGasCost,
estimateSubstrateGas
} from '../../utils/gas';
import { buildEvmFungibleTransactions, executeNextEvmTransaction } from './evm';
import {
buildSubstrateFungibleTransactions,
Expand Down Expand Up @@ -77,7 +84,6 @@ export class FungibleTokenTransferController implements ReactiveController {
public estimatedGas: BigNumber | undefined;

//Evm transfer
protected buildEvmTransactions = buildEvmFungibleTransactions;
protected executeNextEvmTransaction = executeNextEvmTransaction;
protected pendingEvmApprovalTransactions: UnsignedTransaction[] = [];
public pendingTransferTransaction?:
Expand Down Expand Up @@ -548,7 +554,7 @@ export class FungibleTokenTransferController implements ReactiveController {
this.isBuildingTransactions = true;

try {
const evmTransferArtifacts = await this.buildEvmTransactions({
const evmTransferArtifacts = await buildEvmFungibleTransactions({
address: address!,
chainId: this.destinationNetwork!.chainId,
destinationAddress: this.destinationAddress!,
Expand Down Expand Up @@ -580,11 +586,11 @@ export class FungibleTokenTransferController implements ReactiveController {
transactions.push(this.pendingTransferTransaction);
}

this.estimatedGas = await estimateEvmGas(
this.estimatedGas = await estimateEvmTransactionsGasCost(
this.sourceNetwork?.chainId as number,
this.walletContext.value?.evmWallet?.provider as Eip1193Provider,
this.walletContext.value?.evmWallet?.address as string,
transactions
transactions as PopulatedTransaction[]
);
} catch (error) {
console.error('Error Building transactions: ', error);
Expand Down
12 changes: 7 additions & 5 deletions packages/widget/src/controllers/transfers/substrate/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import type { ApiPromise } from '@polkadot/api';
import type { BigNumber } from 'ethers';
import type { SubstrateTransaction } from '../fungible-token-transfer';

type BuildSubstrateFungibleTransactionsArtifacts = {
pendingTransferTransaction: SubstrateTransaction;
resourceAmount: BigNumber;
fee: SubstrateFee;
};

export async function buildSubstrateFungibleTransactions({
address,
substrateProvider,
Expand All @@ -25,11 +31,7 @@ export async function buildSubstrateFungibleTransactions({
resourceAmount: BigNumber;
pendingTransferTransaction: SubstrateTransaction;
fee: SubstrateFee;
}): Promise<{
pendingTransferTransaction: SubstrateTransaction;
resourceAmount: BigNumber;
fee: SubstrateFee;
}> {
}): Promise<BuildSubstrateFungibleTransactionsArtifacts> {
const substrateTransfer = new SubstrateAssetTransfer();
await substrateTransfer.init(substrateProvider, env);

Expand Down
16 changes: 0 additions & 16 deletions packages/widget/src/utils/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@ export async function estimateEvmTransactionsGasCost(
return gasPrice.mul(cost);
}

export async function estimateEvmGas(
chainId: number,
provider: Eip1193Provider,
address: string,
transactions: UnsignedTransaction[]
): Promise<BigNumber> {
const estimatedGas = await estimateEvmTransactionsGasCost(
chainId,
provider,
address,
transactions as PopulatedTransaction[]
);

return estimatedGas;
}

export async function estimateSubstrateGas(
signerAddress: string,
pendingTransferTransaction: SubstrateTransaction
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const validateAddress = (

export const debounceAmountChange = <Args extends string>(
cb: (args: Args) => void,
delay?: number
delay: number
): ((value: Args) => void) => {
let timeout: NodeJS.Timeout;
return (args: Args): void => {
Expand Down

0 comments on commit 88a0922

Please sign in to comment.