Skip to content

Commit

Permalink
refactor: keyrxng fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Mar 9, 2024
1 parent 065b1eb commit c8bfb5c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion static/scripts/rewards/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const networkRpcs: Record<number, string[]> = {
[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";
Expand Down
1 change: 0 additions & 1 deletion static/scripts/rewards/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ export async function insertErc20PermitTableData(
{ name: "Allowance", value: "Loading..." },
]);

renderTokenSymbol({
const params = {
requestedAmountElement,
tokenAddress: permit.permit.permitted.token,
ownerAddress: permit.owner,
amount: permit.transferDetails.requestedAmount,
explorerUrl: networkExplorers[permit.networkId],
symbol,
decimals,
});
};

renderTokenSymbol(params);

// Optimistically rendered what we can so consider it loaded
table.setAttribute(`data-claim`, "ok");
Expand Down
37 changes: 27 additions & 10 deletions static/scripts/rewards/web3/erc20-permit.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
Expand Down Expand Up @@ -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<number> {
try {
const contract = await getErc20Contract(tokenAddress, provider);
return await contract.decimals();
} catch (err) {
throw new Error(`Error fetching decimals for token address: ${tokenAddress}`);
}
}
4 changes: 2 additions & 2 deletions static/scripts/rewards/web3/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", <T>(newNetworkId: T | string) => handleIfOnCorrectNetwork(parseInt(newNetworkId as string, 16), desiredNetworkId));
Expand Down

0 comments on commit c8bfb5c

Please sign in to comment.