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

feat: rename and export scaling functions #219

Merged
merged 2 commits into from
Oct 29, 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
4 changes: 2 additions & 2 deletions src/createRollupEnoughCustomFeeTokenAllowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getRollupCreatorAddress } from './utils/getRollupCreatorAddress';
import { Prettify } from './types/utils';
import { WithRollupCreatorAddressOverride } from './types/createRollupTypes';
import { createRollupGetRetryablesFeesWithDefaults } from './createRollupGetRetryablesFees';
import { scaleToNativeTokenDecimals } from './utils/decimals';
import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals';

export type CreateRollupEnoughCustomFeeTokenAllowanceParams<TChain extends Chain | undefined> =
Prettify<
Expand Down Expand Up @@ -43,5 +43,5 @@ export async function createRollupEnoughCustomFeeTokenAllowance<TChain extends C
publicClient,
});

return allowance >= scaleToNativeTokenDecimals({ amount: fees, decimals });
return allowance >= scaleFrom18DecimalsToNativeTokenDecimals({ amount: fees, decimals });
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getRollupCreatorAddress } from './utils/getRollupCreatorAddress';
import { Prettify } from './types/utils';
import { WithRollupCreatorAddressOverride } from './types/createRollupTypes';
import { createRollupGetRetryablesFeesWithDefaults } from './createRollupGetRetryablesFees';
import { scaleToNativeTokenDecimals } from './utils/decimals';
import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals';

export type CreateRollupPrepareCustomFeeTokenApprovalTransactionRequestParams<
TChain extends Chain | undefined,
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function createRollupPrepareCustomFeeTokenApprovalTransactionReques
address: nativeToken,
owner: account,
spender: rollupCreatorAddressOverride ?? getRollupCreatorAddress(publicClient),
amount: amount ?? scaleToNativeTokenDecimals({ amount: fees, decimals }),
amount: amount ?? scaleFrom18DecimalsToNativeTokenDecimals({ amount: fees, decimals }),
publicClient,
});

Expand Down
6 changes: 3 additions & 3 deletions src/createTokenBridge.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { createTokenBridgePrepareSetWethGatewayTransactionRequest } from './crea
import { createTokenBridgePrepareSetWethGatewayTransactionReceipt } from './createTokenBridgePrepareSetWethGatewayTransactionReceipt';
import { createTokenBridge } from './createTokenBridge';
import { TokenBridgeContracts } from './types/TokenBridgeContracts';
import { scaleToNativeTokenDecimals } from './utils/decimals';
import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals';

const testnodeAccounts = getNitroTestnodePrivateKeyAccounts();
const l2RollupOwner = testnodeAccounts.l2RollupOwner;
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('createTokenBridge utils function', () => {
functionName: 'transfer',
args: [
l3RollupOwner.address,
scaleToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }),
scaleFrom18DecimalsToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }),
],
}),
value: BigInt(0),
Expand Down Expand Up @@ -392,7 +392,7 @@ describe('createTokenBridge', () => {
functionName: 'transfer',
args: [
l3RollupOwner.address,
scaleToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }),
scaleFrom18DecimalsToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }),
],
}),
value: BigInt(0),
Expand Down
4 changes: 2 additions & 2 deletions src/createTokenBridgeEnoughCustomFeeTokenAllowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createTokenBridgeDefaultRetryablesFees } from './constants';
import { Prettify } from './types/utils';
import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes';
import { getTokenBridgeCreatorAddress } from './utils/getTokenBridgeCreatorAddress';
import { scaleToNativeTokenDecimals } from './utils/decimals';
import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals';

export type CreateTokenBridgeEnoughCustomFeeTokenAllowanceParams<TChain extends Chain | undefined> =
Prettify<
Expand Down Expand Up @@ -39,7 +39,7 @@ export async function createTokenBridgeEnoughCustomFeeTokenAllowance<

return (
allowance >=
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: createTokenBridgeDefaultRetryablesFees,
decimals,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { validateParentChain } from './types/ParentChain';
import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes';
import { getTokenBridgeCreatorAddress } from './utils/getTokenBridgeCreatorAddress';
import { createTokenBridgeDefaultRetryablesFees } from './constants';
import { scaleToNativeTokenDecimals } from './utils/decimals';
import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals';

export type CreateTokenBridgePrepareCustomFeeTokenApprovalTransactionRequestParams<
TChain extends Chain | undefined,
Expand Down Expand Up @@ -42,7 +42,7 @@ export async function createTokenBridgePrepareCustomFeeTokenApprovalTransactionR
spender: tokenBridgeCreatorAddressOverride ?? getTokenBridgeCreatorAddress(publicClient),
amount:
amount ??
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: createTokenBridgeDefaultRetryablesFees,
decimals,
}),
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ import {
} from './wasmModuleRoot';
import { registerCustomParentChain } from './chains';
export * from './actions';
import {
scaleFrom18DecimalsToNativeTokenDecimals,
scaleFromNativeTokenDecimalsTo18Decimals,
} from './utils/decimals';

export {
arbOwnerPublicActions,
Expand Down Expand Up @@ -267,4 +271,7 @@ export {
GetConsensusReleaseByWasmModuleRoot,
//
registerCustomParentChain,
//
scaleFrom18DecimalsToNativeTokenDecimals,
scaleFromNativeTokenDecimalsTo18Decimals,
};
33 changes: 26 additions & 7 deletions src/utils/decimals.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
import { BigNumber } from 'ethers';
import utils from '@arbitrum/sdk/dist/lib/utils/lib';
import {
scaleToNativeTokenDecimals as ethers_scaleToNativeTokenDecimals,
nativeTokenDecimalsTo18Decimals as ethers_nativeTokenDecimalsTo18Decimals,
} from '@arbitrum/sdk/dist/lib/utils/lib';

export function scaleToNativeTokenDecimals({
/**
* Scales a value from 18 decimals to the number of decimals of the native token.
*
* @param param.amount The value to scale.
* @param param.decimals The number of decimals of the native token.
*
* @returns The scaled value.
*/
export function scaleFrom18DecimalsToNativeTokenDecimals({
amount,
decimals,
}: {
amount: bigint;
decimals: number;
}) {
}): bigint {
const amountBigNumber = BigNumber.from(amount);
const result = utils.scaleToNativeTokenDecimals({ amount: amountBigNumber, decimals });
const result = ethers_scaleToNativeTokenDecimals({ amount: amountBigNumber, decimals });
return BigInt(result.toString());
}

export function nativeTokenDecimalsTo18Decimals({
/**
* Scales a value from the number of decimals of the native token to 18 decimals.
*
* @param param.amount The value to scale.
* @param param.decimals The number of decimals of the native token.
*
* @returns The scaled value.
*/
export function scaleFromNativeTokenDecimalsTo18Decimals({
amount,
decimals,
}: {
amount: bigint;
decimals: number;
}) {
}): bigint {
const amountBigNumber = BigNumber.from(amount);
const result = utils.nativeTokenDecimalsTo18Decimals({ amount: amountBigNumber, decimals });
const result = ethers_nativeTokenDecimalsTo18Decimals({ amount: amountBigNumber, decimals });
return BigInt(result.toString());
}
Loading