Skip to content

Commit

Permalink
feat: add dataSuffix arg to all transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
rkalis committed Dec 11, 2024
1 parent f3833a3 commit 682ca41
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
3 changes: 3 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ export const HARPIE_API_KEY = process.env.HARPIE_API_KEY ?? process.env.NEXT_PUB
export const WEBACY_API_KEY = process.env.WEBACY_API_KEY ?? process.env.NEXT_PUBLIC_WEBACY_API_KEY;
export const NEFTURE_API_KEY = process.env.NEFTURE_API_KEY ?? process.env.NEXT_PUBLIC_NEFTURE_API_KEY;
export const RESERVOIR_API_KEY = process.env.RESERVOIR_API_KEY ?? process.env.NEXT_PUBLIC_RESERVOIR_API_KEY;

// https://etherscan.io/tx/0xdf137dccbf456e51ac371eb23e00337329e376e2ca968f7a83470542e6dc609b
export const DOMAIN_SUFFIX = '0x4EE96F33' as const;
12 changes: 9 additions & 3 deletions lib/hooks/ethereum/useMarketplaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import eventsDB from 'lib/databases/events';
import type { Marketplace, MarketplaceConfig, OnCancel } from 'lib/interfaces';
import ky from 'lib/ky';
import { getLogsProvider } from 'lib/providers';
import { addressToTopic, getWalletAddress, isNullish, logSorterChronological } from 'lib/utils';
import {
addressToTopic,
getWalletAddress,
isNullish,
logSorterChronological,
writeContractUnlessExcessiveGas,
} from 'lib/utils';
import { createViemPublicClientForChain } from 'lib/utils/chains';
import type { TimeLog } from 'lib/utils/events';
import { mapAsync } from 'lib/utils/promises';
Expand Down Expand Up @@ -60,7 +66,7 @@ export const useMarketplaces = () => {
],
approvalFilterAddress: '0x1E0049783F008A0085193E00003D00cd54003c71',
cancelSignatures: async (walletClient: WalletClient): Promise<Hash> => {
return walletClient.writeContract({
return writeContractUnlessExcessiveGas(publicClient, walletClient, {
address: '0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC',
abi: OPENSEA_SEAPORT_ABI,
account: await getWalletAddress(walletClient),
Expand All @@ -83,7 +89,7 @@ export const useMarketplaces = () => {
chains: [ChainId.EthereumMainnet],
approvalFilterAddress: '0x2f18F339620a63e43f0839Eeb18D7de1e1Be4DfB',
cancelSignatures: async (walletClient: WalletClient): Promise<Hash> => {
return walletClient.writeContract({
return writeContractUnlessExcessiveGas(publicClient, walletClient, {
address: '0xb2ecfE4E4D61f8790bbb9DE2D1259B9e2410CEA5',
abi: BLUR_ABI,
account: await getWalletAddress(walletClient),
Expand Down
18 changes: 12 additions & 6 deletions lib/utils/allowances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import type { useHandleTransaction } from 'lib/hooks/ethereum/useHandleTransacti
import { type TransactionSubmitted, TransactionType } from 'lib/interfaces';
import type { TransactionStore } from 'lib/stores/transaction-store';
import { type Address, type PublicClient, type WalletClient, type WriteContractParameters, formatUnits } from 'viem';
import { deduplicateArray, isNullish, waitForTransactionConfirmation, writeContractUnlessExcessiveGas } from '.';
import {
deduplicateArray,
estimateContractGas,
isNullish,
waitForTransactionConfirmation,
writeContractUnlessExcessiveGas,
} from '.';
import { track } from './analytics';
import { isNetworkError, isRevertedError, isUserRejectionError, parseErrorMessage, stringifyError } from './errors';
import {
Expand Down Expand Up @@ -409,7 +415,7 @@ export const prepareRevokeErc721Allowance = async (
value: 0n as any as never, // Workaround for Gnosis Safe, TODO: remove when fixed
};

const gas = await allowance.contract.publicClient.estimateContractGas(transactionRequest);
const gas = await estimateContractGas(allowance.contract.publicClient, transactionRequest);
return { ...transactionRequest, gas };
}

Expand All @@ -422,7 +428,7 @@ export const prepareRevokeErc721Allowance = async (
value: 0n as any as never, // Workaround for Gnosis Safe, TODO: remove when fixed
};

const gas = await allowance.contract.publicClient.estimateContractGas(transactionRequest);
const gas = await estimateContractGas(allowance.contract.publicClient, transactionRequest);
return { ...transactionRequest, gas };
};

Expand Down Expand Up @@ -474,7 +480,7 @@ export const prepareUpdateErc20Allowance = async (
args: [allowance.payload.spender, newAmount] as const,
};

const gas = await allowance.contract.publicClient.estimateContractGas(transactionRequest);
const gas = await estimateContractGas(allowance.contract.publicClient, transactionRequest);
return { ...transactionRequest, gas };
} catch (e) {
if (!isRevertedError(parseErrorMessage(e))) throw e;
Expand All @@ -489,7 +495,7 @@ export const prepareUpdateErc20Allowance = async (
args: [allowance.payload.spender, differenceAmount] as const,
};

const gas = await allowance.contract.publicClient.estimateContractGas(transactionRequest);
const gas = await estimateContractGas(allowance.contract.publicClient, transactionRequest);
return { ...transactionRequest, gas };
}

Expand All @@ -501,7 +507,7 @@ export const prepareUpdateErc20Allowance = async (
args: [allowance.payload.spender, -differenceAmount] as const,
};

const gas = await allowance.contract.publicClient.estimateContractGas(transactionRequest);
const gas = await estimateContractGas(allowance.contract.publicClient, transactionRequest);
return { ...transactionRequest, gas };
}
};
Expand Down
12 changes: 10 additions & 2 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChainId } from '@revoke.cash/chains';
import { DOMAIN_SUFFIX } from 'lib/constants';
import type { TransactionSubmitted } from 'lib/interfaces';
import ky from 'lib/ky';
import type { getTranslations } from 'next-intl/server';
Expand Down Expand Up @@ -137,9 +138,16 @@ export const writeContractUnlessExcessiveGas = async (
walletClient: WalletClient,
transactionRequest: WriteContractParameters,
) => {
const estimatedGas = transactionRequest.gas ?? (await publicClient.estimateContractGas(transactionRequest));
const transactionRequestWithDataSuffix = { ...transactionRequest, dataSuffix: DOMAIN_SUFFIX };
const estimatedGas =
transactionRequest.gas ?? (await publicClient.estimateContractGas(transactionRequestWithDataSuffix));
throwIfExcessiveGas(transactionRequest.chain!.id, transactionRequest.address, estimatedGas);
return walletClient.writeContract({ ...transactionRequest, gas: estimatedGas });
return walletClient.writeContract({ ...transactionRequestWithDataSuffix, gas: estimatedGas });
};

export const estimateContractGas = async (publicClient: PublicClient, transactionRequest: WriteContractParameters) => {
const transactionRequestWithDataSuffix = { ...transactionRequest, dataSuffix: DOMAIN_SUFFIX };
return publicClient.estimateContractGas(transactionRequestWithDataSuffix);
};

export const waitForTransactionConfirmation = async (hash: Hash, publicClient: PublicClient) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const permit = async (
// DAI uses a different form of permit
if (DAI_ADDRESSES.includes(verifyingContract)) {
const { v, r, s } = await signDaiPermit(walletClient, domain, address, spender, nonce, deadline, false);
return walletClient.writeContract({
return writeContractUnlessExcessiveGas(contract.publicClient, walletClient, {
...contract,
account: address,
abi: DAI_PERMIT_ABI,
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/permit2.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PERMIT2_ABI } from 'lib/abis';
import blocksDB from 'lib/databases/blocks';
import type { Address, WalletClient } from 'viem';
import { deduplicateArray, getWalletAddress, writeContractUnlessExcessiveGas } from '.';
import { deduplicateArray, estimateContractGas, getWalletAddress, writeContractUnlessExcessiveGas } from '.';
import { AllowanceType, type Permit2Erc20Allowance } from './allowances';
import { type Permit2Event, type TokenEvent, TokenEventType } from './events';
import { SECOND } from './time';
Expand Down Expand Up @@ -101,7 +101,7 @@ export const preparePermit2Approve = async (
value: 0n as any as never, // Workaround for Gnosis Safe, TODO: remove when fixed
};

const gas = await tokenContract.publicClient.estimateContractGas(transactionRequest);
const gas = await estimateContractGas(tokenContract.publicClient, transactionRequest);

return { ...transactionRequest, gas };
};

0 comments on commit 682ca41

Please sign in to comment.