Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parseUnits usage #294

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions scripts/common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Numeric, Overrides, parseUnits } from 'ethers';
import { ethers, getNamedAccounts } from 'hardhat';

import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers';

import type { Overrides } from 'ethers';

export async function getDeployerSigner(): Promise<SignerWithAddress> {
const deployer = (await getNamedAccounts())['deployer'];
return await ethers.getSigner(deployer);
Expand All @@ -21,3 +20,9 @@ export async function getFeeOverrides(): Promise<Overrides> {
}
return { gasPrice: feeData.gasPrice || 0 };
}

export function getParseUnitsCallback(
unitNames: (string | Numeric)[]
): (value: string, index: number, array: string[]) => bigint {
return (s, i) => parseUnits(s, unitNames[i]);
}
29 changes: 9 additions & 20 deletions scripts/sentinel/sentinel_set_limits.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
import 'hardhat-deploy';

import * as dotenv from 'dotenv';
import { BigNumber } from 'ethers';

import { parseUnits } from '@ethersproject/units';

import { Sentinel__factory } from '../../typechain';
import { getDeployerSigner, getFeeOverrides } from '../common';

import type { ContractTransaction, Overrides } from '@ethersproject/contracts';
import type { BigNumberish } from '@ethersproject/bignumber';
import { TypedContractMethod } from '../../typechain/common';
import { getDeployerSigner, getFeeOverrides, getParseUnitsCallback } from '../common';

import type { AddressLike, BigNumberish, Overrides } from 'ethers';
dotenv.config();

function getParseUnitsCallback(
unitNames: BigNumberish[]
): (value: string, index: number, array: string[]) => BigNumber {
return (s, i) => parseUnits(s, unitNames[i]);
}

async function setLimitIfSpecified(
limitEnv: string,
target: string,
tokens: string[],
decimals: string[],
methodName: string,
method: (
_target: string,
_tokens: string[],
_amounts: BigNumberish[],
overrides?: Overrides & { from?: string }
) => Promise<ContractTransaction>,
method: TypedContractMethod<
[_target: string, _tokens: AddressLike[], _amounts: BigNumberish[]],
[void],
'nonpayable'
>,
feeOverrides: Overrides
): Promise<void> {
if (limitEnv) {
const limitStr = limitEnv.split(',');
if (limitEnv.length > 0 && limitStr.length === decimals.length) {
const limits = limitStr.map(getParseUnitsCallback(decimals));
const limits = limitStr.map(getParseUnitsCallback(decimals.map(Number)));
await (await method(target, tokens, limits, feeOverrides)).wait();
console.log(
methodName,
Expand Down
9 changes: 2 additions & 7 deletions scripts/set_limits.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import 'hardhat-deploy';

import * as dotenv from 'dotenv';
import { parseUnits } from 'ethers';

import { Bridge__factory, OriginalTokenVault__factory, PeggedTokenBridge__factory } from '../typechain';
import { TypedContractMethod } from '../typechain/common';
import { getDeployerSigner, getFeeOverrides } from './common';
import { getDeployerSigner, getFeeOverrides, getParseUnitsCallback } from './common';

import type { AddressLike, BigNumberish, Overrides } from 'ethers';

dotenv.config();

function getParseUnitsCallback(unitNames: string[]): (value: string, index: number, array: string[]) => bigint {
return (s, i) => parseUnits(s, unitNames[i]);
}

async function setLimitIfSpecified(
limitEnv: string,
tokens: string[],
Expand All @@ -26,7 +21,7 @@ async function setLimitIfSpecified(
if (limitEnv) {
const limitStr = limitEnv.split(',');
if (limitEnv.length > 0 && limitStr.length === decimals.length) {
const limits = limitStr.map(getParseUnitsCallback(decimals));
const limits = limitStr.map(getParseUnitsCallback(decimals.map(Number)));
await (await method(tokens, limits, feeOverrides)).wait();
console.log(
methodName,
Expand Down
Loading