From c031b6064e01e413a413ba2804b14a1be75ae393 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: Fri, 16 Feb 2024 18:42:13 +0900 Subject: [PATCH 01/13] feat: upgrade deployments sync to our ts-template --- .github/workflows/cloudflare-deploy.yml | 31 +++++++++ .github/workflows/continuous-deploy.yml | 83 ------------------------- 2 files changed, 31 insertions(+), 83 deletions(-) create mode 100644 .github/workflows/cloudflare-deploy.yml delete mode 100644 .github/workflows/continuous-deploy.yml diff --git a/.github/workflows/cloudflare-deploy.yml b/.github/workflows/cloudflare-deploy.yml new file mode 100644 index 00000000..9301cefa --- /dev/null +++ b/.github/workflows/cloudflare-deploy.yml @@ -0,0 +1,31 @@ +name: Deploy to Cloudflare Pages + +on: + push: + +jobs: + deploy-to-cloudflare: + name: Deploy to Cloudflare Pages + runs-on: ubuntu-22.04 + permissions: + contents: read + + steps: + - name: Check Cloudflare API Token + run: | + if [[ -z "${{ secrets.CLOUDFLARE_API_TOKEN }}" ]]; then + echo "CLOUDFLARE_API_TOKEN secret must be set" >&2 + exit 1 + fi + - name: Deploy to Cloudflare + uses: ubiquity/cloudflare-deploy-action@main + with: + cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }} + repository: ${{ github.repository }} + production_branch: ${{ github.event.repository.default_branch }} + output_directory: "static" + is_production: ${{ github.event_name == 'push' && github.ref == 'refs/heads/${{ github.event.repository.default_branch }}' }} + # env: + # Add any environment variables you need to pass along here + # SUPABASE_URL: ${{ secrets.SUPABASE_URL }} + # SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }} diff --git a/.github/workflows/continuous-deploy.yml b/.github/workflows/continuous-deploy.yml deleted file mode 100644 index d3b47be7..00000000 --- a/.github/workflows/continuous-deploy.yml +++ /dev/null @@ -1,83 +0,0 @@ -name: Cloudflare Preview Deployment - -on: - workflow_run: - workflows: ["Build"] - types: - - completed - -jobs: - deploy-to-cloudflare: - name: Deploy to Cloudflare Pages - runs-on: ubuntu-22.04 - permissions: - contents: read - pull-requests: write - - if: ${{ github.event.workflow_run.conclusion == 'success' }} - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Setup Node - uses: actions/setup-node@v3 - with: - node-version: "18.14.1" - - - name: Wrangler Install - run: npm install -g wrangler - - - name: Download build artifact - uses: dawidd6/action-download-artifact@v2 - with: - name: pr - workflow: build.yml - path: pr - - - name: Publish to Cloudflare - id: publish-to-cloudflare - env: - CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} - REPOSITORY: ${{ github.repository }} - PRODUCTION_BRANCH: "development" - OUTPUT_DIRECTORY: "pr" - run: | - IFS='/' read -ra fields <<< "$REPOSITORY" - projectName="${fields[1]}" - projectName=$(echo $projectName | sed 's/\./-/g') - echo $projectName - wrangler pages project list > project_list.txt - if grep -q $projectName project_list.txt; then - echo "Project found" - else - echo "Project not found" - wrangler pages project create "$projectName" --production-branch "$PRODUCTION_BRANCH" - fi - echo DEPLOY_URL=$(wrangler pages publish "$OUTPUT_DIRECTORY" --project-name "$projectName" | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*") >> $GITHUB_OUTPUT - - - name: Print deploy URL - run: echo ${{ steps.publish-to-cloudflare.outputs.DEPLOY_URL }} - - - name: Download PR number artifact - uses: dawidd6/action-download-artifact@v2 - with: - name: pr-num - workflow: build.yml - path: pr-num - - - name: Read PR number from file - id: pr-num-reader - uses: juliangruber/read-file-action@v1.1.6 - with: - path: pr-num/pr-num.txt - - - name: Print PR number from file - run: echo ${{ steps.pr-num-reader.outputs.content }} - - - name: Comment PR - # PR number exists only on "pull_request" event so comment only when it exists - if: ${{ steps.pr-num-reader.outputs.content }} - uses: thollander/actions-comment-pull-request@v2 - with: - message: ${{ steps.publish-to-cloudflare.outputs.DEPLOY_URL }} - pr_number: ${{ steps.pr-num-reader.outputs.content }} From 70811a70d5ddb73e7c0e524a2a3a40b26b89c158 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: Tue, 20 Feb 2024 08:29:01 +0900 Subject: [PATCH 02/13] fix: remove deprecated ci --- .github/workflows/cloudflare-deploy.yml | 31 ------------------------- 1 file changed, 31 deletions(-) delete mode 100644 .github/workflows/cloudflare-deploy.yml diff --git a/.github/workflows/cloudflare-deploy.yml b/.github/workflows/cloudflare-deploy.yml deleted file mode 100644 index 9301cefa..00000000 --- a/.github/workflows/cloudflare-deploy.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: Deploy to Cloudflare Pages - -on: - push: - -jobs: - deploy-to-cloudflare: - name: Deploy to Cloudflare Pages - runs-on: ubuntu-22.04 - permissions: - contents: read - - steps: - - name: Check Cloudflare API Token - run: | - if [[ -z "${{ secrets.CLOUDFLARE_API_TOKEN }}" ]]; then - echo "CLOUDFLARE_API_TOKEN secret must be set" >&2 - exit 1 - fi - - name: Deploy to Cloudflare - uses: ubiquity/cloudflare-deploy-action@main - with: - cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }} - repository: ${{ github.repository }} - production_branch: ${{ github.event.repository.default_branch }} - output_directory: "static" - is_production: ${{ github.event_name == 'push' && github.ref == 'refs/heads/${{ github.event.repository.default_branch }}' }} - # env: - # Add any environment variables you need to pass along here - # SUPABASE_URL: ${{ secrets.SUPABASE_URL }} - # SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }} From 86f7a8f76b19450e760ce90cddfea4df5ba1cee6 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: Tue, 20 Feb 2024 18:05:04 +0900 Subject: [PATCH 03/13] chore: update supabase key name ref: https://github.com/ubiquity/pay.ubq.fi/pull/164#discussion_r1495461160 --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 503dac78..638993d1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -51,4 +51,4 @@ jobs: app_installation_id: ${{ secrets.APP_INSTALLATION_ID }} # Add any environment variables you need to pass along here # SUPABASE_URL: ${{ secrets.SUPABASE_URL }} - # SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }} + # SUPABASE_ANON_KEY: ${{ secrets.SUPABASE_ANON_KEY }} From 047368b9bbaad69dc11bd178c2f0bdded76b396a 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: Tue, 20 Feb 2024 18:06:12 +0900 Subject: [PATCH 04/13] chore: update supabase key name ref: https://github.com/ubiquity/pay.ubq.fi/pull/164#discussion_r1495461160 --- lib/ubiquibot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ubiquibot b/lib/ubiquibot index cb084909..9288dd2c 160000 --- a/lib/ubiquibot +++ b/lib/ubiquibot @@ -1 +1 @@ -Subproject commit cb084909ab332f67bba271b5a17b907d21716bd1 +Subproject commit 9288dd2c4da4a8ab37409e7ec38ff90d560295d2 From f5e3a80fbbba11464b09e98a61942cf3fe67b196 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 20 Feb 2024 15:40:35 +0000 Subject: [PATCH 05/13] feat: sub 1s load time --- .../render-transaction/insert-table-data.ts | 47 +++++++++++++--- .../render-transaction/render-token-symbol.ts | 29 +++------- .../render-transaction/render-transaction.ts | 28 ++-------- static/scripts/rewards/web3/erc20-permit.ts | 56 +++++++++---------- static/scripts/rewards/web3/wallet.ts | 3 +- 5 files changed, 83 insertions(+), 80 deletions(-) diff --git a/static/scripts/rewards/render-transaction/insert-table-data.ts b/static/scripts/rewards/render-transaction/insert-table-data.ts index 565bc1ce..0da37199 100644 --- a/static/scripts/rewards/render-transaction/insert-table-data.ts +++ b/static/scripts/rewards/render-transaction/insert-table-data.ts @@ -1,30 +1,61 @@ -import { BigNumber, ethers } from "ethers"; +import { ethers } from "ethers"; import { app } from "."; import { Erc20Permit, Erc721Permit } from "./tx-type"; +import { fetchTreasury } from "../web3/erc20-permit"; +import { renderTokenSymbol } from "./render-token-symbol"; +import { networkExplorers } from "../constants"; export function shortenAddress(address: string): string { return `${address.slice(0, 10)}...${address.slice(-8)}`; } -export function insertErc20PermitTableData( +export async function insertErc20PermitTableData( permit: Erc20Permit, - table: Element, - treasury: { balance: BigNumber; allowance: BigNumber; decimals: number; symbol: string } -): Element { + provider: ethers.providers.JsonRpcProvider, + symbol: string, + decimals: number, + table: Element +) { const requestedAmountElement = document.getElementById("rewardAmount") as Element; renderToFields(permit.transferDetails.to, app.currentExplorerUrl); renderTokenFields(permit.permit.permitted.token, app.currentExplorerUrl); + renderDetailsFields([ { name: "From", value: `${permit.owner}` }, { name: "Expiry", value: permit.permit.deadline.lte(Number.MAX_SAFE_INTEGER.toString()) ? new Date(permit.permit.deadline.toNumber()).toLocaleString() : undefined, }, - { name: "Balance", value: treasury.balance.gte(0) ? `${ethers.utils.formatUnits(treasury.balance, treasury.decimals)} ${treasury.symbol}` : "N/A" }, - { name: "Allowance", value: treasury.allowance.gte(0) ? `${ethers.utils.formatUnits(treasury.allowance, treasury.decimals)} ${treasury.symbol}` : "N/A" }, + { name: "Balance", value: "Loading..." }, + { name: "Allowance", value: "Loading..." }, ]); + + renderTokenSymbol({ + requestedAmountElement, + tokenAddress: permit.permit.permitted.token, + ownerAddress: permit.owner, + amount: permit.transferDetails.requestedAmount, + explorerUrl: networkExplorers[permit.networkId], + symbol, + decimals, + }); + + // Optimistically rendered what we can so consider it loaded + table.setAttribute(`data-claim`, "ok"); + table.setAttribute(`data-contract-loaded`, "true"); table.setAttribute(`data-claim-rendered`, "true"); - return requestedAmountElement; + + const { balance, allowance } = await fetchTreasury(permit.permit.permitted.token, permit.owner, provider); + + renderDetailsFields([ + { name: "From", value: `${permit.owner}` }, + { + name: "Expiry", + value: permit.permit.deadline.lte(Number.MAX_SAFE_INTEGER.toString()) ? new Date(permit.permit.deadline.toNumber()).toLocaleString() : undefined, + }, + { name: "Balance", value: balance.gte(0) ? `${ethers.utils.formatUnits(balance, decimals)} ${symbol}` : "N/A" }, + { name: "Allowance", value: allowance.gte(0) ? `${ethers.utils.formatUnits(allowance, decimals)} ${symbol}` : "N/A" }, + ]); } export function insertErc721PermitTableData(permit: Erc721Permit, table: Element): Element { diff --git a/static/scripts/rewards/render-transaction/render-token-symbol.ts b/static/scripts/rewards/render-transaction/render-token-symbol.ts index d7af472a..429bda7a 100644 --- a/static/scripts/rewards/render-transaction/render-token-symbol.ts +++ b/static/scripts/rewards/render-transaction/render-token-symbol.ts @@ -1,6 +1,5 @@ -import { BigNumberish, Contract, utils } from "ethers"; +import { BigNumberish, utils } from "ethers"; import { getErc20Contract } from "../helpers"; -import { MaxUint256 } from "@uniswap/permit2-sdk"; import { JsonRpcProvider } from "@ethersproject/providers"; export const tokens = [ @@ -14,37 +13,27 @@ export const tokens = [ }, ]; -export async function renderTokenSymbol({ - table, +export function renderTokenSymbol({ requestedAmountElement, tokenAddress, ownerAddress, amount, explorerUrl, - provider, + symbol, + decimals, }: { - table: Element; requestedAmountElement: Element; tokenAddress: string; ownerAddress: string; amount: BigNumberish; explorerUrl: string; - provider: JsonRpcProvider; -}): Promise { - let symbol = tokenAddress === tokens[0].address ? tokens[0].name : tokenAddress === tokens[1].address ? tokens[1].name : false; - let decimals = tokenAddress === tokens[0].address ? 18 : tokenAddress === tokens[1].address ? 18 : MaxUint256; - - if (!symbol || decimals === MaxUint256) { - const contract: Contract = await getErc20Contract(tokenAddress, provider); - symbol = await contract.symbol(); - decimals = await contract.decimals(); - } - - table.setAttribute(`data-contract-loaded`, "true"); - requestedAmountElement.innerHTML = `${utils.formatUnits( + symbol: string; + decimals: number; +}) { + return (requestedAmountElement.innerHTML = `${utils.formatUnits( amount, decimals - )} ${symbol}`; + )} ${symbol}`); } export async function renderNftSymbol({ diff --git a/static/scripts/rewards/render-transaction/render-transaction.ts b/static/scripts/rewards/render-transaction/render-transaction.ts index 7d416e6f..256af563 100644 --- a/static/scripts/rewards/render-transaction/render-transaction.ts +++ b/static/scripts/rewards/render-transaction/render-transaction.ts @@ -4,13 +4,12 @@ import { Value } from "@sinclair/typebox/value"; import { networkExplorers } from "../constants"; import { getOptimalProvider } from "../helpers"; import { claimButton, hideClaimButton, resetClaimButton } from "../toaster"; -import { claimErc20PermitHandler, fetchTreasury, generateInvalidatePermitAdminControl } from "../web3/erc20-permit"; +import { claimErc20PermitHandler, generateInvalidatePermitAdminControl, processERC20 } from "../web3/erc20-permit"; import { claimErc721PermitHandler } from "../web3/erc721-permit"; -import { handleNetwork } from "../web3/wallet"; import { app } from "./index"; -import { insertErc20PermitTableData, insertErc721PermitTableData } from "./insert-table-data"; +import { insertErc721PermitTableData } from "./insert-table-data"; import { renderEnsName } from "./render-ens-name"; -import { renderNftSymbol, renderTokenSymbol } from "./render-token-symbol"; +import { renderNftSymbol } from "./render-token-symbol"; import { setClaimMessage } from "./set-claim-message"; import { claimTxT } from "./tx-type"; import { removeAllEventListeners } from "./utils"; @@ -35,7 +34,7 @@ export async function init() { app.claimTxs = claimTxs; optimalRPC = await getOptimalProvider(app.currentTx?.networkId ?? app.claimTxs[0].networkId); - handleNetwork(app.currentTx?.networkId ?? app.claimTxs[0].networkId).catch(console.error); + // handleNetwork(app.currentTx?.networkId ?? app.claimTxs[0].networkId).catch(console.error); } catch (error) { console.error(error); setClaimMessage({ type: "Error", message: `Invalid claim data passed in URL` }); @@ -86,7 +85,7 @@ export async function init() { } } - renderTransaction(optimalRPC, true).catch(console.error); + renderTransaction(optimalRPC).catch(console.error); } function setPagination(nextTxButton: Element | null, prevTxButton: Element | null) { @@ -123,25 +122,10 @@ export async function renderTransaction(provider: JsonRpcProvider, nextTx?: bool return false; } - handleNetwork(app.currentTx.networkId).catch(console.error); - if (app.currentTx.type === "erc20-permit") { - const treasury = await fetchTreasury(app.currentTx, provider); + await processERC20(app.currentTx.permit.permitted.token, provider, app.currentTx, table); // insert tx data into table - const requestedAmountElement = insertErc20PermitTableData(app.currentTx, table, treasury); - table.setAttribute(`data-claim`, "ok"); - - renderTokenSymbol({ - tokenAddress: app.currentTx.permit.permitted.token, - ownerAddress: app.currentTx.owner, - amount: app.currentTx.transferDetails.requestedAmount, - explorerUrl: networkExplorers[app.currentTx.networkId], - table, - requestedAmountElement, - provider, - }).catch(console.error); - const toElement = document.getElementById(`rewardRecipient`) as Element; renderEnsName({ element: toElement, address: app.currentTx.transferDetails.to }).catch(console.error); diff --git a/static/scripts/rewards/web3/erc20-permit.ts b/static/scripts/rewards/web3/erc20-permit.ts index 014f68fb..5417b7ac 100644 --- a/static/scripts/rewards/web3/erc20-permit.ts +++ b/static/scripts/rewards/web3/erc20-permit.ts @@ -9,35 +9,35 @@ 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"; -export async function fetchTreasury( - permit: Erc20Permit, - provider: JsonRpcProvider -): Promise<{ balance: BigNumber; allowance: BigNumber; decimals: number; symbol: string }> { - try { - const tokenAddress = permit.permit.permitted.token.toLowerCase(); - const tokenContract = await getErc20Contract(tokenAddress, provider); - - if (tokenAddress === tokens[0].address || tokenAddress === tokens[1].address) { - const decimals = tokenAddress === tokens[0].address ? 18 : tokenAddress === tokens[1].address ? 18 : -1; - const symbol = tokenAddress === tokens[0].address ? tokens[0].name : tokenAddress === tokens[1].address ? tokens[1].name : ""; - - const [balance, allowance] = await Promise.all([tokenContract.balanceOf(permit.owner), tokenContract.allowance(permit.owner, permit2Address)]); - - return { balance, allowance, decimals, symbol }; - } else { - console.log(`Hardcode this token in render-token-symbol.ts and save two calls: ${tokenAddress}`); - const [balance, allowance, decimals, symbol] = await Promise.all([ - tokenContract.balanceOf(permit.owner), - tokenContract.allowance(permit.owner, permit2Address), - tokenContract.decimals(), - tokenContract.symbol(), - ]); - - return { balance, allowance, decimals, symbol }; +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; + const contract = await getErc20Contract(tokenAddress, provider); + + if (!symbol || decimals === -1) { + try { + symbol = contract.symbol(); + decimals = contract.decimals(); + } catch (err) { + throw new Error(`Error fetching symbol and decimals for token address: ${tokenAddress}`); } - } catch (error: unknown) { - return { balance: BigNumber.from(-1), allowance: BigNumber.from(-1), decimals: -1, symbol: "" }; + } + + await insertErc20PermitTableData(permit, provider, symbol, decimals, table); +} + +export async function fetchTreasury(contractAddr: string, owner: string, provider: JsonRpcProvider) { + const contract = await getErc20Contract(contractAddr, provider); + + try { + const [balance, allowance] = await Promise.all([contract.balanceOf(owner), contract.allowance(owner, permit2Address)]); + return { balance, allowance } as { balance: BigNumber; allowance: BigNumber }; + } catch (err) { + console.log(err); + console.log(contractAddr); + throw new Error(`Error fetching treasury data for token address: ${contractAddr}`); } } @@ -85,7 +85,7 @@ export async function checkPermitClaimable(permit: Erc20Permit, signer: ethers.p return false; } - const { balance, allowance } = await fetchTreasury(permit, provider); + const { balance, allowance } = await fetchTreasury(permit.permit.permitted.token, permit.owner, provider); const permitted = BigNumber.from(permit.permit.permitted.amount); const isSolvent = balance.gte(permitted); const isAllowed = allowance.gte(permitted); diff --git a/static/scripts/rewards/web3/wallet.ts b/static/scripts/rewards/web3/wallet.ts index 56ce5e73..34766b85 100644 --- a/static/scripts/rewards/web3/wallet.ts +++ b/static/scripts/rewards/web3/wallet.ts @@ -33,8 +33,7 @@ export async function handleNetwork(desiredNetworkId: number) { invalidateButton.disabled = true; } - const chainIdHex = String(web3provider.network.chainId); - const currentNetworkId = parseInt(chainIdHex, 16); + const currentNetworkId = (await web3provider.getNetwork()).chainId; // watch for network changes window.ethereum.on("chainChanged", (newNetworkId: T | string) => handleIfOnCorrectNetwork(parseInt(newNetworkId as string, 16), desiredNetworkId)); From 7fab1b4dcf224ebca7ed50e97d7c4ef713403838 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 20 Feb 2024 16:10:54 +0000 Subject: [PATCH 06/13] chore: fetch inside try --- static/scripts/rewards/web3/erc20-permit.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/static/scripts/rewards/web3/erc20-permit.ts b/static/scripts/rewards/web3/erc20-permit.ts index 5417b7ac..fd26d9f5 100644 --- a/static/scripts/rewards/web3/erc20-permit.ts +++ b/static/scripts/rewards/web3/erc20-permit.ts @@ -14,10 +14,10 @@ import { insertErc20PermitTableData } from "../render-transaction/insert-table-d 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; - const contract = await getErc20Contract(tokenAddress, provider); if (!symbol || decimals === -1) { try { + const contract = await getErc20Contract(tokenAddress, provider); symbol = contract.symbol(); decimals = contract.decimals(); } catch (err) { @@ -29,16 +29,15 @@ export async function processERC20(tokenAddress: string, provider: JsonRpcProvid } export async function fetchTreasury(contractAddr: string, owner: string, provider: JsonRpcProvider) { - const contract = await getErc20Contract(contractAddr, provider); - try { + const contract = await getErc20Contract(contractAddr, provider); const [balance, allowance] = await Promise.all([contract.balanceOf(owner), contract.allowance(owner, permit2Address)]); return { balance, allowance } as { balance: BigNumber; allowance: BigNumber }; } catch (err) { console.log(err); - console.log(contractAddr); - throw new Error(`Error fetching treasury data for token address: ${contractAddr}`); } + + return { balance: BigNumber.from(0), allowance: BigNumber.from(0) }; } export function claimErc20PermitHandler(permit: Erc20Permit, provider: JsonRpcProvider) { From f1ca988221b088f8a091a2cb2ccc4e32d09a252f Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 20 Feb 2024 15:40:35 +0000 Subject: [PATCH 07/13] feat: sub 1s load time --- lib/chainlist | 1 - .../render-transaction/insert-table-data.ts | 47 +++++++++++++--- .../render-transaction/render-token-symbol.ts | 29 +++------- .../render-transaction/render-transaction.ts | 28 ++-------- static/scripts/rewards/web3/erc20-permit.ts | 56 +++++++++---------- static/scripts/rewards/web3/wallet.ts | 3 +- 6 files changed, 83 insertions(+), 81 deletions(-) delete mode 160000 lib/chainlist diff --git a/lib/chainlist b/lib/chainlist deleted file mode 160000 index 80592837..00000000 --- a/lib/chainlist +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 805928372c7a97eec513e8c74c344dd759f07e1c diff --git a/static/scripts/rewards/render-transaction/insert-table-data.ts b/static/scripts/rewards/render-transaction/insert-table-data.ts index 565bc1ce..0da37199 100644 --- a/static/scripts/rewards/render-transaction/insert-table-data.ts +++ b/static/scripts/rewards/render-transaction/insert-table-data.ts @@ -1,30 +1,61 @@ -import { BigNumber, ethers } from "ethers"; +import { ethers } from "ethers"; import { app } from "."; import { Erc20Permit, Erc721Permit } from "./tx-type"; +import { fetchTreasury } from "../web3/erc20-permit"; +import { renderTokenSymbol } from "./render-token-symbol"; +import { networkExplorers } from "../constants"; export function shortenAddress(address: string): string { return `${address.slice(0, 10)}...${address.slice(-8)}`; } -export function insertErc20PermitTableData( +export async function insertErc20PermitTableData( permit: Erc20Permit, - table: Element, - treasury: { balance: BigNumber; allowance: BigNumber; decimals: number; symbol: string } -): Element { + provider: ethers.providers.JsonRpcProvider, + symbol: string, + decimals: number, + table: Element +) { const requestedAmountElement = document.getElementById("rewardAmount") as Element; renderToFields(permit.transferDetails.to, app.currentExplorerUrl); renderTokenFields(permit.permit.permitted.token, app.currentExplorerUrl); + renderDetailsFields([ { name: "From", value: `${permit.owner}` }, { name: "Expiry", value: permit.permit.deadline.lte(Number.MAX_SAFE_INTEGER.toString()) ? new Date(permit.permit.deadline.toNumber()).toLocaleString() : undefined, }, - { name: "Balance", value: treasury.balance.gte(0) ? `${ethers.utils.formatUnits(treasury.balance, treasury.decimals)} ${treasury.symbol}` : "N/A" }, - { name: "Allowance", value: treasury.allowance.gte(0) ? `${ethers.utils.formatUnits(treasury.allowance, treasury.decimals)} ${treasury.symbol}` : "N/A" }, + { name: "Balance", value: "Loading..." }, + { name: "Allowance", value: "Loading..." }, ]); + + renderTokenSymbol({ + requestedAmountElement, + tokenAddress: permit.permit.permitted.token, + ownerAddress: permit.owner, + amount: permit.transferDetails.requestedAmount, + explorerUrl: networkExplorers[permit.networkId], + symbol, + decimals, + }); + + // Optimistically rendered what we can so consider it loaded + table.setAttribute(`data-claim`, "ok"); + table.setAttribute(`data-contract-loaded`, "true"); table.setAttribute(`data-claim-rendered`, "true"); - return requestedAmountElement; + + const { balance, allowance } = await fetchTreasury(permit.permit.permitted.token, permit.owner, provider); + + renderDetailsFields([ + { name: "From", value: `${permit.owner}` }, + { + name: "Expiry", + value: permit.permit.deadline.lte(Number.MAX_SAFE_INTEGER.toString()) ? new Date(permit.permit.deadline.toNumber()).toLocaleString() : undefined, + }, + { name: "Balance", value: balance.gte(0) ? `${ethers.utils.formatUnits(balance, decimals)} ${symbol}` : "N/A" }, + { name: "Allowance", value: allowance.gte(0) ? `${ethers.utils.formatUnits(allowance, decimals)} ${symbol}` : "N/A" }, + ]); } export function insertErc721PermitTableData(permit: Erc721Permit, table: Element): Element { diff --git a/static/scripts/rewards/render-transaction/render-token-symbol.ts b/static/scripts/rewards/render-transaction/render-token-symbol.ts index d7af472a..429bda7a 100644 --- a/static/scripts/rewards/render-transaction/render-token-symbol.ts +++ b/static/scripts/rewards/render-transaction/render-token-symbol.ts @@ -1,6 +1,5 @@ -import { BigNumberish, Contract, utils } from "ethers"; +import { BigNumberish, utils } from "ethers"; import { getErc20Contract } from "../helpers"; -import { MaxUint256 } from "@uniswap/permit2-sdk"; import { JsonRpcProvider } from "@ethersproject/providers"; export const tokens = [ @@ -14,37 +13,27 @@ export const tokens = [ }, ]; -export async function renderTokenSymbol({ - table, +export function renderTokenSymbol({ requestedAmountElement, tokenAddress, ownerAddress, amount, explorerUrl, - provider, + symbol, + decimals, }: { - table: Element; requestedAmountElement: Element; tokenAddress: string; ownerAddress: string; amount: BigNumberish; explorerUrl: string; - provider: JsonRpcProvider; -}): Promise { - let symbol = tokenAddress === tokens[0].address ? tokens[0].name : tokenAddress === tokens[1].address ? tokens[1].name : false; - let decimals = tokenAddress === tokens[0].address ? 18 : tokenAddress === tokens[1].address ? 18 : MaxUint256; - - if (!symbol || decimals === MaxUint256) { - const contract: Contract = await getErc20Contract(tokenAddress, provider); - symbol = await contract.symbol(); - decimals = await contract.decimals(); - } - - table.setAttribute(`data-contract-loaded`, "true"); - requestedAmountElement.innerHTML = `${utils.formatUnits( + symbol: string; + decimals: number; +}) { + return (requestedAmountElement.innerHTML = `${utils.formatUnits( amount, decimals - )} ${symbol}`; + )} ${symbol}`); } export async function renderNftSymbol({ diff --git a/static/scripts/rewards/render-transaction/render-transaction.ts b/static/scripts/rewards/render-transaction/render-transaction.ts index 7d416e6f..256af563 100644 --- a/static/scripts/rewards/render-transaction/render-transaction.ts +++ b/static/scripts/rewards/render-transaction/render-transaction.ts @@ -4,13 +4,12 @@ import { Value } from "@sinclair/typebox/value"; import { networkExplorers } from "../constants"; import { getOptimalProvider } from "../helpers"; import { claimButton, hideClaimButton, resetClaimButton } from "../toaster"; -import { claimErc20PermitHandler, fetchTreasury, generateInvalidatePermitAdminControl } from "../web3/erc20-permit"; +import { claimErc20PermitHandler, generateInvalidatePermitAdminControl, processERC20 } from "../web3/erc20-permit"; import { claimErc721PermitHandler } from "../web3/erc721-permit"; -import { handleNetwork } from "../web3/wallet"; import { app } from "./index"; -import { insertErc20PermitTableData, insertErc721PermitTableData } from "./insert-table-data"; +import { insertErc721PermitTableData } from "./insert-table-data"; import { renderEnsName } from "./render-ens-name"; -import { renderNftSymbol, renderTokenSymbol } from "./render-token-symbol"; +import { renderNftSymbol } from "./render-token-symbol"; import { setClaimMessage } from "./set-claim-message"; import { claimTxT } from "./tx-type"; import { removeAllEventListeners } from "./utils"; @@ -35,7 +34,7 @@ export async function init() { app.claimTxs = claimTxs; optimalRPC = await getOptimalProvider(app.currentTx?.networkId ?? app.claimTxs[0].networkId); - handleNetwork(app.currentTx?.networkId ?? app.claimTxs[0].networkId).catch(console.error); + // handleNetwork(app.currentTx?.networkId ?? app.claimTxs[0].networkId).catch(console.error); } catch (error) { console.error(error); setClaimMessage({ type: "Error", message: `Invalid claim data passed in URL` }); @@ -86,7 +85,7 @@ export async function init() { } } - renderTransaction(optimalRPC, true).catch(console.error); + renderTransaction(optimalRPC).catch(console.error); } function setPagination(nextTxButton: Element | null, prevTxButton: Element | null) { @@ -123,25 +122,10 @@ export async function renderTransaction(provider: JsonRpcProvider, nextTx?: bool return false; } - handleNetwork(app.currentTx.networkId).catch(console.error); - if (app.currentTx.type === "erc20-permit") { - const treasury = await fetchTreasury(app.currentTx, provider); + await processERC20(app.currentTx.permit.permitted.token, provider, app.currentTx, table); // insert tx data into table - const requestedAmountElement = insertErc20PermitTableData(app.currentTx, table, treasury); - table.setAttribute(`data-claim`, "ok"); - - renderTokenSymbol({ - tokenAddress: app.currentTx.permit.permitted.token, - ownerAddress: app.currentTx.owner, - amount: app.currentTx.transferDetails.requestedAmount, - explorerUrl: networkExplorers[app.currentTx.networkId], - table, - requestedAmountElement, - provider, - }).catch(console.error); - const toElement = document.getElementById(`rewardRecipient`) as Element; renderEnsName({ element: toElement, address: app.currentTx.transferDetails.to }).catch(console.error); diff --git a/static/scripts/rewards/web3/erc20-permit.ts b/static/scripts/rewards/web3/erc20-permit.ts index 014f68fb..5417b7ac 100644 --- a/static/scripts/rewards/web3/erc20-permit.ts +++ b/static/scripts/rewards/web3/erc20-permit.ts @@ -9,35 +9,35 @@ 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"; -export async function fetchTreasury( - permit: Erc20Permit, - provider: JsonRpcProvider -): Promise<{ balance: BigNumber; allowance: BigNumber; decimals: number; symbol: string }> { - try { - const tokenAddress = permit.permit.permitted.token.toLowerCase(); - const tokenContract = await getErc20Contract(tokenAddress, provider); - - if (tokenAddress === tokens[0].address || tokenAddress === tokens[1].address) { - const decimals = tokenAddress === tokens[0].address ? 18 : tokenAddress === tokens[1].address ? 18 : -1; - const symbol = tokenAddress === tokens[0].address ? tokens[0].name : tokenAddress === tokens[1].address ? tokens[1].name : ""; - - const [balance, allowance] = await Promise.all([tokenContract.balanceOf(permit.owner), tokenContract.allowance(permit.owner, permit2Address)]); - - return { balance, allowance, decimals, symbol }; - } else { - console.log(`Hardcode this token in render-token-symbol.ts and save two calls: ${tokenAddress}`); - const [balance, allowance, decimals, symbol] = await Promise.all([ - tokenContract.balanceOf(permit.owner), - tokenContract.allowance(permit.owner, permit2Address), - tokenContract.decimals(), - tokenContract.symbol(), - ]); - - return { balance, allowance, decimals, symbol }; +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; + const contract = await getErc20Contract(tokenAddress, provider); + + if (!symbol || decimals === -1) { + try { + symbol = contract.symbol(); + decimals = contract.decimals(); + } catch (err) { + throw new Error(`Error fetching symbol and decimals for token address: ${tokenAddress}`); } - } catch (error: unknown) { - return { balance: BigNumber.from(-1), allowance: BigNumber.from(-1), decimals: -1, symbol: "" }; + } + + await insertErc20PermitTableData(permit, provider, symbol, decimals, table); +} + +export async function fetchTreasury(contractAddr: string, owner: string, provider: JsonRpcProvider) { + const contract = await getErc20Contract(contractAddr, provider); + + try { + const [balance, allowance] = await Promise.all([contract.balanceOf(owner), contract.allowance(owner, permit2Address)]); + return { balance, allowance } as { balance: BigNumber; allowance: BigNumber }; + } catch (err) { + console.log(err); + console.log(contractAddr); + throw new Error(`Error fetching treasury data for token address: ${contractAddr}`); } } @@ -85,7 +85,7 @@ export async function checkPermitClaimable(permit: Erc20Permit, signer: ethers.p return false; } - const { balance, allowance } = await fetchTreasury(permit, provider); + const { balance, allowance } = await fetchTreasury(permit.permit.permitted.token, permit.owner, provider); const permitted = BigNumber.from(permit.permit.permitted.amount); const isSolvent = balance.gte(permitted); const isAllowed = allowance.gte(permitted); diff --git a/static/scripts/rewards/web3/wallet.ts b/static/scripts/rewards/web3/wallet.ts index 39d67a39..34766b85 100644 --- a/static/scripts/rewards/web3/wallet.ts +++ b/static/scripts/rewards/web3/wallet.ts @@ -33,8 +33,7 @@ export async function handleNetwork(desiredNetworkId: number) { invalidateButton.disabled = true; } - const network = await web3provider.getNetwork(); - const currentNetworkId = network.chainId; + const currentNetworkId = (await web3provider.getNetwork()).chainId; // watch for network changes window.ethereum.on("chainChanged", (newNetworkId: T | string) => handleIfOnCorrectNetwork(parseInt(newNetworkId as string, 16), desiredNetworkId)); From 97fb13ce97e2444e092eb41b91ac15f96656c431 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 20 Feb 2024 16:10:54 +0000 Subject: [PATCH 08/13] chore: fetch inside try --- static/scripts/rewards/web3/erc20-permit.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/static/scripts/rewards/web3/erc20-permit.ts b/static/scripts/rewards/web3/erc20-permit.ts index 5417b7ac..fd26d9f5 100644 --- a/static/scripts/rewards/web3/erc20-permit.ts +++ b/static/scripts/rewards/web3/erc20-permit.ts @@ -14,10 +14,10 @@ import { insertErc20PermitTableData } from "../render-transaction/insert-table-d 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; - const contract = await getErc20Contract(tokenAddress, provider); if (!symbol || decimals === -1) { try { + const contract = await getErc20Contract(tokenAddress, provider); symbol = contract.symbol(); decimals = contract.decimals(); } catch (err) { @@ -29,16 +29,15 @@ export async function processERC20(tokenAddress: string, provider: JsonRpcProvid } export async function fetchTreasury(contractAddr: string, owner: string, provider: JsonRpcProvider) { - const contract = await getErc20Contract(contractAddr, provider); - try { + const contract = await getErc20Contract(contractAddr, provider); const [balance, allowance] = await Promise.all([contract.balanceOf(owner), contract.allowance(owner, permit2Address)]); return { balance, allowance } as { balance: BigNumber; allowance: BigNumber }; } catch (err) { console.log(err); - console.log(contractAddr); - throw new Error(`Error fetching treasury data for token address: ${contractAddr}`); } + + return { balance: BigNumber.from(0), allowance: BigNumber.from(0) }; } export function claimErc20PermitHandler(permit: Erc20Permit, provider: JsonRpcProvider) { From 517dd4866df4b251d6efd8097405bff4dee224dc Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Wed, 21 Feb 2024 16:45:35 +0000 Subject: [PATCH 09/13] fix: use allnetworkurls --- build/esbuild-build.ts | 2 +- lib/chainlist | 1 + static/scripts/rewards/constants.ts | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) create mode 160000 lib/chainlist diff --git a/build/esbuild-build.ts b/build/esbuild-build.ts index 30d024c6..02674a73 100644 --- a/build/esbuild-build.ts +++ b/build/esbuild-build.ts @@ -62,6 +62,6 @@ function createEnvDefines(envVarNames: string[], extras: Record defines[key] = JSON.stringify(extras[key]); } } - defines["extraRpcs"] = JSON.stringify(extraRpcs); + defines["extraRpcs"] = JSON.stringify(allNetworkUrls); return defines; } diff --git a/lib/chainlist b/lib/chainlist new file mode 160000 index 00000000..80592837 --- /dev/null +++ b/lib/chainlist @@ -0,0 +1 @@ +Subproject commit 805928372c7a97eec513e8c74c344dd759f07e1c diff --git a/static/scripts/rewards/constants.ts b/static/scripts/rewards/constants.ts index 2794e84b..ec0bf5a7 100644 --- a/static/scripts/rewards/constants.ts +++ b/static/scripts/rewards/constants.ts @@ -9,7 +9,6 @@ export enum NetworkIds { Goerli = 5, Gnosis = 100, } -console.trace({ extraRpcs }); export enum Tokens { DAI = "0x6b175474e89094c44da98b954eedeac495271d0f", From 1fe025d81ca08e06f47b60fc7a6277d95e2f0920 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Fri, 23 Feb 2024 12:29:35 +0000 Subject: [PATCH 10/13] fix: rpc blacklist --- build/esbuild-build.ts | 31 ++++++++++++++++++++-- scripts/typescript/generate-permit2-url.ts | 25 ++++++++++++----- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/build/esbuild-build.ts b/build/esbuild-build.ts index 02674a73..6137d6f0 100644 --- a/build/esbuild-build.ts +++ b/build/esbuild-build.ts @@ -1,3 +1,4 @@ +// @ts-expect-error - Could not find a declaration file for module import extraRpcs from "../lib/chainlist/constants/extraRpcs"; import esbuild from "esbuild"; import * as dotenv from "dotenv"; @@ -13,9 +14,35 @@ export const entries = [...typescriptEntries, ...cssEntries]; const allNetworkUrls: Record = {}; // this flattens all the rpcs into a single object, with key names that match the networkIds. The arrays are just of URLs per network ID. +const blacklist = ["https://xdai-archive.blockscout.com"]; + Object.keys(extraRpcs).forEach((networkId) => { - const officialUrls = extraRpcs[networkId].rpcs.filter((rpc) => typeof rpc === "string"); - const extraUrls: string[] = extraRpcs[networkId].rpcs.filter((rpc) => rpc.url !== undefined).map((rpc) => rpc.url); + const officialUrls = extraRpcs[networkId].rpcs.filter((rpc) => { + if (typeof rpc === "string") { + if (blacklist.includes(rpc)) { + return ""; + } else { + return rpc; + } + } + }); + const extraUrls: string[] = extraRpcs[networkId].rpcs + .filter((rpc) => rpc.url !== undefined) + .map((rpc) => { + if (typeof rpc === "string") { + if (blacklist.includes(rpc)) { + return ""; + } else { + return rpc; + } + } else { + if (blacklist.includes(rpc.url)) { + return ""; + } else { + return rpc.url; + } + } + }); allNetworkUrls[networkId] = [...officialUrls, ...extraUrls]; }); diff --git a/scripts/typescript/generate-permit2-url.ts b/scripts/typescript/generate-permit2-url.ts index 7abf435d..b523bfb4 100644 --- a/scripts/typescript/generate-permit2-url.ts +++ b/scripts/typescript/generate-permit2-url.ts @@ -13,7 +13,7 @@ generate().catch((error) => { process.exitCode = 1; }); -async function generate() { +async function generate(multi = false) { const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_PROVIDER_URL); const myWallet = new ethers.Wallet(process.env.UBIQUIBOT_PRIVATE_KEY || "", provider); @@ -59,9 +59,22 @@ async function generate() { ]; const base64encodedTxData = Buffer.from(JSON.stringify(txData)).toString("base64"); - log.ok("Testing URL:"); - console.log(`${process.env.FRONTEND_URL}?claim=${base64encodedTxData}`); - log.ok("Public URL:"); - console.log(`https://pay.ubq.fi?claim=${base64encodedTxData}`); - console.log(); + + if (multi) { + return `${process.env.FRONTEND_URL}?claim=${base64encodedTxData}`; + } else { + log.ok("Testing URL:"); + console.log(`${process.env.FRONTEND_URL}?claim=${base64encodedTxData}`); + log.ok("Public URL:"); + console.log(`https://pay.ubq.fi?claim=${base64encodedTxData}`); + console.log(); + } +} + +export async function generateMultiERC20Permits() { + for (let i = 0; i < 5; i++) { + const url = await generate(true); + log.ok("Testing URL:"); + console.log(url); + } } From 16a534e609ee478d2b942a9bb9ed94f84f2d270b Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Mon, 26 Feb 2024 08:10:36 +0000 Subject: [PATCH 11/13] fix: no network resolved --- build/esbuild-build.ts | 4 ++-- scripts/typescript/generate-permit2-url.ts | 12 ++---------- scripts/typescript/multi-permits.t.ts | 16 ++++++++++++++++ static/scripts/rewards/constants.ts | 5 +++++ static/scripts/rewards/helpers.ts | 7 +++++++ 5 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 scripts/typescript/multi-permits.t.ts diff --git a/build/esbuild-build.ts b/build/esbuild-build.ts index 6137d6f0..f6dcd5fd 100644 --- a/build/esbuild-build.ts +++ b/build/esbuild-build.ts @@ -14,13 +14,13 @@ export const entries = [...typescriptEntries, ...cssEntries]; const allNetworkUrls: Record = {}; // this flattens all the rpcs into a single object, with key names that match the networkIds. The arrays are just of URLs per network ID. -const blacklist = ["https://xdai-archive.blockscout.com"]; +const blacklist = ["https://xdai-archive.blockscout.com", "https://gnosis.api.onfinality.io/public"]; Object.keys(extraRpcs).forEach((networkId) => { const officialUrls = extraRpcs[networkId].rpcs.filter((rpc) => { if (typeof rpc === "string") { if (blacklist.includes(rpc)) { - return ""; + return null; } else { return rpc; } diff --git a/scripts/typescript/generate-permit2-url.ts b/scripts/typescript/generate-permit2-url.ts index b523bfb4..ff4d4eab 100644 --- a/scripts/typescript/generate-permit2-url.ts +++ b/scripts/typescript/generate-permit2-url.ts @@ -7,13 +7,13 @@ dotenv.config(); const PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3"; // same on all chains -generate().catch((error) => { +generateERC20Permit().catch((error) => { console.error(error); verifyEnvironmentVariables(); process.exitCode = 1; }); -async function generate(multi = false) { +export async function generateERC20Permit(multi = false) { const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_PROVIDER_URL); const myWallet = new ethers.Wallet(process.env.UBIQUIBOT_PRIVATE_KEY || "", provider); @@ -70,11 +70,3 @@ async function generate(multi = false) { console.log(); } } - -export async function generateMultiERC20Permits() { - for (let i = 0; i < 5; i++) { - const url = await generate(true); - log.ok("Testing URL:"); - console.log(url); - } -} diff --git a/scripts/typescript/multi-permits.t.ts b/scripts/typescript/multi-permits.t.ts new file mode 100644 index 00000000..71e89574 --- /dev/null +++ b/scripts/typescript/multi-permits.t.ts @@ -0,0 +1,16 @@ +import { generateERC20Permit } from "./generate-permit2-url"; +import { log, verifyEnvironmentVariables } from "./utils"; + +export async function generateMultiERC20Permits() { + for (let i = 0; i < 5; i++) { + const url = await generateERC20Permit(); + log.ok("Testing URL:"); + console.log(url); + } +} + +generateMultiERC20Permits().catch((error) => { + console.error(error); + verifyEnvironmentVariables(); + process.exitCode = 1; +}); diff --git a/static/scripts/rewards/constants.ts b/static/scripts/rewards/constants.ts index ec0bf5a7..b298c161 100644 --- a/static/scripts/rewards/constants.ts +++ b/static/scripts/rewards/constants.ts @@ -8,6 +8,7 @@ export enum NetworkIds { Mainnet = 1, Goerli = 5, Gnosis = 100, + Anvil = 31337, } export enum Tokens { @@ -19,12 +20,14 @@ export const networkNames = { [NetworkIds.Mainnet]: "Ethereum Mainnet", [NetworkIds.Goerli]: "Goerli Testnet", [NetworkIds.Gnosis]: "Gnosis Chain", + [NetworkIds.Anvil]: "http://127.0.0.1:8545", }; export const networkCurrencies: Record = { [NetworkIds.Mainnet]: { symbol: "ETH", decimals: 18 }, [NetworkIds.Goerli]: { symbol: "GoerliETH", decimals: 18 }, [NetworkIds.Gnosis]: { symbol: "XDAI", decimals: 18 }, + [NetworkIds.Anvil]: { symbol: "XDAI", decimals: 18 }, }; export function getNetworkName(networkId?: number) { @@ -39,12 +42,14 @@ export const networkExplorers: Record = { [NetworkIds.Mainnet]: "https://etherscan.io", [NetworkIds.Goerli]: "https://goerli.etherscan.io", [NetworkIds.Gnosis]: "https://gnosisscan.io", + [NetworkIds.Anvil]: "https://gnosisscan.io", }; 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]: [...(extraRpcs[NetworkIds.Gnosis] || [])], + [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 a75d2fb8..b9c3896e 100644 --- a/static/scripts/rewards/helpers.ts +++ b/static/scripts/rewards/helpers.ts @@ -40,6 +40,13 @@ export async function getErc20Contract(contractAddress: string, provider: JsonRp } export async function getOptimalProvider(networkId: number) { + if (networkId === 31337) + 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) => { try { const startTime = performance.now(); From ea0baff22813549853aff97c24d6b3bdbd3a1fd2 Mon Sep 17 00:00:00 2001 From: molecula451 Date: Mon, 26 Feb 2024 22:54:54 -0400 Subject: [PATCH 12/13] chore: remove blacklist --- build/esbuild-build.ts | 30 ++--------------------------- static/scripts/rewards/constants.ts | 2 +- 2 files changed, 3 insertions(+), 29 deletions(-) diff --git a/build/esbuild-build.ts b/build/esbuild-build.ts index f6dcd5fd..97ebd5e2 100644 --- a/build/esbuild-build.ts +++ b/build/esbuild-build.ts @@ -14,35 +14,9 @@ export const entries = [...typescriptEntries, ...cssEntries]; const allNetworkUrls: Record = {}; // this flattens all the rpcs into a single object, with key names that match the networkIds. The arrays are just of URLs per network ID. -const blacklist = ["https://xdai-archive.blockscout.com", "https://gnosis.api.onfinality.io/public"]; - Object.keys(extraRpcs).forEach((networkId) => { - const officialUrls = extraRpcs[networkId].rpcs.filter((rpc) => { - if (typeof rpc === "string") { - if (blacklist.includes(rpc)) { - return null; - } else { - return rpc; - } - } - }); - const extraUrls: string[] = extraRpcs[networkId].rpcs - .filter((rpc) => rpc.url !== undefined) - .map((rpc) => { - if (typeof rpc === "string") { - if (blacklist.includes(rpc)) { - return ""; - } else { - return rpc; - } - } else { - if (blacklist.includes(rpc.url)) { - return ""; - } else { - return rpc.url; - } - } - }); + const officialUrls = extraRpcs[networkId].rpcs.filter((rpc) => typeof rpc === "string"); + const extraUrls: string[] = extraRpcs[networkId].rpcs.filter((rpc) => rpc.url !== undefined).map((rpc) => rpc.url); allNetworkUrls[networkId] = [...officialUrls, ...extraUrls]; }); diff --git a/static/scripts/rewards/constants.ts b/static/scripts/rewards/constants.ts index b298c161..891a6513 100644 --- a/static/scripts/rewards/constants.ts +++ b/static/scripts/rewards/constants.ts @@ -48,7 +48,7 @@ export const networkExplorers: Record = { 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]: [...(extraRpcs[NetworkIds.Gnosis] || [])], + [NetworkIds.Gnosis]: ["https://rpc.ankr.com/gnosis", ...(extraRpcs[NetworkIds.Gnosis] || [])], [NetworkIds.Anvil]: ["http://127.0.0.1:8545", ""], }; From c7e6459af0a8daee95e2bd7458c6cee4ad512cc9 Mon Sep 17 00:00:00 2001 From: bertan <22911469+barebind@users.noreply.github.com> Date: Wed, 28 Feb 2024 14:32:35 +0900 Subject: [PATCH 13/13] fix: extraRpcs declaration error --- build/esbuild-build.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/build/esbuild-build.ts b/build/esbuild-build.ts index 30d024c6..23d8bb48 100644 --- a/build/esbuild-build.ts +++ b/build/esbuild-build.ts @@ -33,7 +33,7 @@ export const esBuildContext: esbuild.BuildOptions = { ".svg": "dataurl", }, outdir: "static/out", - define: createEnvDefines(["SUPABASE_URL", "SUPABASE_ANON_KEY"], { allNetworkUrls }), + define: createEnvDefines(["SUPABASE_URL", "SUPABASE_ANON_KEY"], { extraRpcs: allNetworkUrls }), }; esbuild @@ -62,6 +62,5 @@ function createEnvDefines(envVarNames: string[], extras: Record defines[key] = JSON.stringify(extras[key]); } } - defines["extraRpcs"] = JSON.stringify(extraRpcs); return defines; }