Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v3' into v3
Browse files Browse the repository at this point in the history
  • Loading branch information
yorhodes committed Nov 16, 2023
2 parents 3fc2c9c + 346cbd1 commit 874f366
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 139 deletions.
2 changes: 1 addition & 1 deletion typescript/infra/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hyperlane-xyz/infra",
"description": "Infrastructure utilities for the Hyperlane Network",
"version": "3.1.2",
"version": "3.1.3",
"dependencies": {
"@arbitrum/sdk": "^3.0.0",
"@aws-sdk/client-iam": "^3.74.0",
Expand Down
14 changes: 8 additions & 6 deletions typescript/infra/src/config/gas-oracle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BigNumber, ethers } from 'ethers';

import { ChainMap, ChainName } from '@hyperlane-xyz/sdk';
import { convertDecimalsEthersBigNumber } from '@hyperlane-xyz/utils';
import { convertDecimals } from '@hyperlane-xyz/utils';

import { mustGetChainNativeTokenDecimals } from '../utils/utils';

Expand Down Expand Up @@ -77,15 +77,17 @@ export function getTokenExchangeRateFromValues(
localValue: BigNumber,
remote: ChainName,
remoteValue: BigNumber,
) {
): BigNumber {
// This does not yet account for decimals!
const exchangeRate = remoteValue
.mul(TOKEN_EXCHANGE_RATE_MULTIPLIER)
.div(localValue);

return convertDecimalsEthersBigNumber(
mustGetChainNativeTokenDecimals(remote),
mustGetChainNativeTokenDecimals(local),
exchangeRate,
return BigNumber.from(
convertDecimals(
mustGetChainNativeTokenDecimals(remote),
mustGetChainNativeTokenDecimals(local),
exchangeRate.toString(),
),
);
}
1 change: 1 addition & 0 deletions typescript/sdk/logos/black/base.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions typescript/sdk/logos/black/polygonzkevm.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions typescript/sdk/logos/black/scroll.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions typescript/sdk/logos/color/base.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions typescript/sdk/logos/color/polygonzkevm.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions typescript/sdk/logos/color/scroll.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion typescript/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export {
} from './src/addresses';
export {
convertDecimals,
convertDecimalsEthersBigNumber,
eqAmountApproximate,
fromWei,
fromWeiRounded,
Expand Down
2 changes: 1 addition & 1 deletion typescript/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"clean": "rm -rf ./dist",
"check": "tsc --noEmit",
"prettier": "prettier --write ./src",
"test:unit": "mocha --config .mocharc.json './src/**/*.test.ts'"
"test": "mocha --config .mocharc.json './src/**/*.test.ts'"
},
"sideEffects": false,
"types": "dist/index.d.ts",
Expand Down
54 changes: 54 additions & 0 deletions typescript/utils/src/amount.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect } from 'chai';

import { eqAmountApproximate, fromWei, fromWeiRounded, toWei } from './amount';

describe('fromWei', () => {
it('parses and converts correctly', () => {
expect(fromWei(1, 0)).to.equal('1');
expect(fromWei('1000000', 6)).to.equal('1');
expect(fromWei('1000000000000000000')).to.equal('1');
expect(fromWei('1000000000000000000.1234')).to.equal('1');
});
});

describe('fromWeiRounded', () => {
it('parses and converts correctly', () => {
expect(fromWeiRounded(1, 0)).to.equal('1.0000');
expect(fromWeiRounded('1000000', 6)).to.equal('1.0000');
expect(fromWeiRounded('1000000000000000000')).to.equal('1.0000');
expect(fromWeiRounded('1000000000000000000.1234')).to.equal('1.0000');
});

it('rounds correctly', () => {
expect(fromWeiRounded(1234567890, 6, 2)).to.equal('1234.56');
expect(fromWeiRounded('1234567890', 6, 4)).to.equal('1234.5678');
expect(fromWeiRounded('10000000000000000000')).to.equal('10.0000');
expect(fromWeiRounded('10000000000000000000', 18, 0)).to.equal('10');
});

it('can drop decimals for large numbers', () => {
expect(fromWeiRounded('10001000000000000000000')).to.equal('10001.00');
expect(fromWeiRounded('10001000000000000000', 15, 4)).to.equal(
'10001.0000',
);
});
});

describe('toWei', () => {
it('parses and converts correctly', () => {
expect(toWei(1, 0)).to.equal('1');
expect(toWei('1', 6)).to.equal('1000000');
expect(toWei('123.456')).to.equal('123456000000000000000');
expect(toWei('1.00000000000000000001')).to.equal('1000000000000000000');
expect(toWei('1.00000000000000000001', 6)).to.equal('1000000');
});
});

describe('eqAmountApproximate', () => {
it('compares correctly', () => {
expect(eqAmountApproximate(1, 1.001, 0.001)).to.be.true;
expect(eqAmountApproximate(9, 9.001, 0.01)).to.be.true;
expect(eqAmountApproximate('9876543210', '9876543210', '1')).to.be.true;
expect(eqAmountApproximate('9876543210', '9876543212', '1')).to.be.false;
});
});
57 changes: 10 additions & 47 deletions typescript/utils/src/amount.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { formatUnits, parseUnits } from '@ethersproject/units';
import BigNumber from 'bignumber.js';
import { ethers } from 'ethers';

const DEFAULT_MIN_ROUNDED_VALUE = 0.00001;
const DEFAULT_DISPLAY_DECIMALS = 4;
const DEFAULT_TOKEN_DECIMALS = 18;

// Use toString(10) on bignumber.js to prevent ethers.js bigNumber error
// when parsing exponential string over e21

/**
* Convert the given Wei value to Ether value
* @param value The value to convert.
Expand All @@ -34,21 +29,14 @@ export function fromWei(
export function fromWeiRounded(
value: BigNumber.Value | null | undefined,
decimals = DEFAULT_TOKEN_DECIMALS,
roundDownIfSmall = true,
displayDecimals?: number,
): string {
if (!value) return '0';
const flooredValue = BigNumber(value).toFixed(0, BigNumber.ROUND_FLOOR);
const amount = BigNumber(formatUnits(flooredValue, decimals));
if (amount.isZero()) return '0';

// If amount is less than min value
if (amount.lt(DEFAULT_MIN_ROUNDED_VALUE)) {
if (roundDownIfSmall) return '0';
return amount.toString(10);
}

const displayDecimals = amount.gte(10000) ? 2 : DEFAULT_DISPLAY_DECIMALS;
return amount.toFixed(displayDecimals);
displayDecimals ??= amount.gte(10000) ? 2 : DEFAULT_DISPLAY_DECIMALS;
return amount.toFixed(displayDecimals, BigNumber.ROUND_FLOOR);
}

/**
Expand Down Expand Up @@ -101,17 +89,17 @@ export function tryParseAmount(
/**
* Checks if an amount is equal of nearly equal to balance within a small margin of error
* Necessary because amounts in the UI are often rounded
* @param amountInWei1 The amount to compare.
* @param amountInWei2 The amount to compare.
* @param amount1 The amount to compare.
* @param amount2 The amount to compare.
* @returns true/false.
*/
export function eqAmountApproximate(
amountInWei1: BigNumber.Value,
amountInWei2: BigNumber.Value,
amount1: BigNumber.Value,
amount2: BigNumber.Value,
maxDifference: BigNumber.Value,
): boolean {
const minValueWei = toWei(DEFAULT_MIN_ROUNDED_VALUE);
// Is difference btwn amount and balance less than min amount shown for token
return BigNumber(amountInWei1).minus(amountInWei2).abs().lt(minValueWei);
// Is difference btwn amounts less than maxDifference
return BigNumber(amount1).minus(amount2).abs().lte(maxDifference);
}

/**
Expand Down Expand Up @@ -143,28 +131,3 @@ export function convertDecimals(
return amount.times(BigNumber(10).pow(difference)).toString(10);
}
}

/**
* Converts a value with `fromDecimals` decimals to a value with `toDecimals` decimals.
* Incurs a loss of precision when `fromDecimals` > `toDecimals`.
* @param fromDecimals The number of decimals `value` has.
* @param toDecimals The number of decimals to convert `value` to.
* @param value The value to convert.
* @returns `value` represented with `toDecimals` decimals.
*/
export function convertDecimalsEthersBigNumber(
fromDecimals: number,
toDecimals: number,
value: ethers.BigNumber,
) {
if (fromDecimals === toDecimals) return value;
else if (fromDecimals > toDecimals) {
const difference = fromDecimals - toDecimals;
return value.div(ethers.BigNumber.from('10').pow(difference));
}
// fromDecimals < toDecimals
else {
const difference = toDecimals - fromDecimals;
return value.mul(ethers.BigNumber.from('10').pow(difference));
}
}
Loading

0 comments on commit 874f366

Please sign in to comment.