From c8bfb5cc17c77fdcdce78df3ed868a9b65d316cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Sat, 9 Mar 2024 21:06:54 +0900 Subject: [PATCH] refactor: keyrxng fixes --- ...lti-permits.t.ts => multi-permits.test.ts} | 6 +-- static/scripts/rewards/constants.ts | 2 +- static/scripts/rewards/helpers.ts | 1 - .../render-transaction/insert-table-data.ts | 6 ++- static/scripts/rewards/web3/erc20-permit.ts | 37 ++++++++++++++----- static/scripts/rewards/web3/wallet.ts | 4 +- 6 files changed, 36 insertions(+), 20 deletions(-) rename scripts/typescript/{multi-permits.t.ts => multi-permits.test.ts} (76%) diff --git a/scripts/typescript/multi-permits.t.ts b/scripts/typescript/multi-permits.test.ts similarity index 76% rename from scripts/typescript/multi-permits.t.ts rename to scripts/typescript/multi-permits.test.ts index 71e89574..0bebf2ad 100644 --- a/scripts/typescript/multi-permits.t.ts +++ b/scripts/typescript/multi-permits.test.ts @@ -1,15 +1,13 @@ import { generateERC20Permit } from "./generate-permit2-url"; import { log, verifyEnvironmentVariables } from "./utils"; -export async function generateMultiERC20Permits() { +(async function generateMultiERC20Permits() { for (let i = 0; i < 5; i++) { const url = await generateERC20Permit(); log.ok("Testing URL:"); console.log(url); } -} - -generateMultiERC20Permits().catch((error) => { +})().catch((error) => { console.error(error); verifyEnvironmentVariables(); process.exitCode = 1; diff --git a/static/scripts/rewards/constants.ts b/static/scripts/rewards/constants.ts index 891a6513..0cef1353 100644 --- a/static/scripts/rewards/constants.ts +++ b/static/scripts/rewards/constants.ts @@ -49,7 +49,7 @@ export const networkRpcs: Record = { [NetworkIds.Mainnet]: ["https://rpc-pay.ubq.fi/v1/mainnet", ...(extraRpcs[NetworkIds.Mainnet] || [])], [NetworkIds.Goerli]: ["https://rpc-pay.ubq.fi/v1/goerli", ...(extraRpcs[NetworkIds.Goerli] || [])], [NetworkIds.Gnosis]: ["https://rpc.ankr.com/gnosis", ...(extraRpcs[NetworkIds.Gnosis] || [])], - [NetworkIds.Anvil]: ["http://127.0.0.1:8545", ""], + [NetworkIds.Anvil]: ["http://127.0.0.1:8545"], }; export const permit2Address = "0x000000000022D473030F116dDEE9F6B43aC78BA3"; diff --git a/static/scripts/rewards/helpers.ts b/static/scripts/rewards/helpers.ts index b9c3896e..f2af8196 100644 --- a/static/scripts/rewards/helpers.ts +++ b/static/scripts/rewards/helpers.ts @@ -44,7 +44,6 @@ export async function getOptimalProvider(networkId: number) { return new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545", { name: "http://127.0.0.1:8545", chainId: 31337, - ensAddress: "", }); const promises = networkRpcs[networkId].map(async (baseURL: string) => { diff --git a/static/scripts/rewards/render-transaction/insert-table-data.ts b/static/scripts/rewards/render-transaction/insert-table-data.ts index 0da37199..1363627e 100644 --- a/static/scripts/rewards/render-transaction/insert-table-data.ts +++ b/static/scripts/rewards/render-transaction/insert-table-data.ts @@ -30,7 +30,7 @@ export async function insertErc20PermitTableData( { name: "Allowance", value: "Loading..." }, ]); - renderTokenSymbol({ + const params = { requestedAmountElement, tokenAddress: permit.permit.permitted.token, ownerAddress: permit.owner, @@ -38,7 +38,9 @@ export async function insertErc20PermitTableData( explorerUrl: networkExplorers[permit.networkId], symbol, decimals, - }); + }; + + renderTokenSymbol(params); // Optimistically rendered what we can so consider it loaded table.setAttribute(`data-claim`, "ok"); diff --git a/static/scripts/rewards/web3/erc20-permit.ts b/static/scripts/rewards/web3/erc20-permit.ts index fd26d9f5..1a680d6f 100644 --- a/static/scripts/rewards/web3/erc20-permit.ts +++ b/static/scripts/rewards/web3/erc20-permit.ts @@ -1,25 +1,33 @@ +import { JsonRpcProvider } from "@ethersproject/providers"; import { BigNumber, BigNumberish, ethers } from "ethers"; import { permit2Abi } from "../abis"; import { permit2Address } from "../constants"; import { getErc20Contract, getOptimalProvider } from "../helpers"; -import { Erc20Permit } from "../render-transaction/tx-type"; -import { toaster, resetClaimButton, errorToast, loadingClaimButton, claimButton } from "../toaster"; -import { renderTransaction } from "../render-transaction/render-transaction"; -import { connectWallet } from "./wallet"; import invalidateButton from "../invalidate-component"; -import { JsonRpcProvider } from "@ethersproject/providers"; -import { tokens } from "../render-transaction/render-token-symbol"; import { insertErc20PermitTableData } from "../render-transaction/insert-table-data"; +import { tokens } from "../render-transaction/render-token-symbol"; +import { renderTransaction } from "../render-transaction/render-transaction"; +import { Erc20Permit } from "../render-transaction/tx-type"; +import { claimButton, errorToast, loadingClaimButton, resetClaimButton, toaster } from "../toaster"; +import { connectWallet } from "./wallet"; export async function processERC20(tokenAddress: string, provider: JsonRpcProvider, permit: Erc20Permit, table: Element) { - let symbol = tokenAddress === tokens[0].address ? tokens[0].name : tokenAddress === tokens[1].address ? tokens[1].name : ""; - let decimals = tokenAddress === tokens[0].address ? 18 : tokenAddress === tokens[1].address ? 18 : -1; + let symbol; + let decimals = -1; + + if (tokenAddress === tokens[0].address) { + symbol = tokens[0].name; + decimals = await getDecimalsFromContract(tokenAddress, provider); + } else if (tokenAddress === tokens[1].address) { + symbol = tokens[1].name; + decimals = await getDecimalsFromContract(tokenAddress, provider); + } if (!symbol || decimals === -1) { try { const contract = await getErc20Contract(tokenAddress, provider); - symbol = contract.symbol(); - decimals = contract.decimals(); + symbol = await contract.symbol(); + decimals = await getDecimalsFromContract(tokenAddress, provider); } catch (err) { throw new Error(`Error fetching symbol and decimals for token address: ${tokenAddress}`); } @@ -181,3 +189,12 @@ export function nonceBitmap(nonce: BigNumberish): { wordPos: BigNumber; bitPos: const bitPos = BigNumber.from(nonce).and(255).toNumber(); return { wordPos, bitPos }; } + +export async function getDecimalsFromContract(tokenAddress: string, provider: JsonRpcProvider): Promise { + try { + const contract = await getErc20Contract(tokenAddress, provider); + return await contract.decimals(); + } catch (err) { + throw new Error(`Error fetching decimals for token address: ${tokenAddress}`); + } +} diff --git a/static/scripts/rewards/web3/wallet.ts b/static/scripts/rewards/web3/wallet.ts index 34766b85..32f5d40e 100644 --- a/static/scripts/rewards/web3/wallet.ts +++ b/static/scripts/rewards/web3/wallet.ts @@ -32,8 +32,8 @@ export async function handleNetwork(desiredNetworkId: number) { loadingClaimButton(false); invalidateButton.disabled = true; } - - const currentNetworkId = (await web3provider.getNetwork()).chainId; + const network = await web3provider.getNetwork(); + const currentNetworkId = network.chainId; // watch for network changes window.ethereum.on("chainChanged", (newNetworkId: T | string) => handleIfOnCorrectNetwork(parseInt(newNetworkId as string, 16), desiredNetworkId));