Skip to content

Commit

Permalink
Run tests for non-18 decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Oct 23, 2024
1 parent fe1fdd1 commit 86785fb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ jobs:
run: yarn test:unit

test-integration:
name: Test (Integration)
name: Test (Integration) - ${{ matrix.config.name }}
runs-on: ubuntu-latest
strategy:
matrix:
config:
- name: Default
args: --tokenbridge --l3node --l3-token-bridge --l3-fee-token
decimals: 18
- name: Custom Decimals
args: --tokenbridge --l3node --l3-token-bridge --l3-fee-token --l3-fee-token-decimals 12
decimals: 12
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -72,7 +81,7 @@ jobs:
uses: OffchainLabs/actions/run-nitro-test-node@feat-simplify
with:
nitro-testnode-ref: release
args: --tokenbridge --l3node --l3-token-bridge --l3-fee-token
args: ${{ matrix.config.args }}

- name: Copy .env
run: cp ./.env.example ./.env
Expand All @@ -81,4 +90,4 @@ jobs:
run: yarn build

- name: Test
run: yarn test:integration
run: DECIMALS=${{decimals}} yarn test:integration
8 changes: 6 additions & 2 deletions src/createRollup.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getInformationFromTestnode,
} from './testHelpers';
import { createRollupFetchTransactionHash } from './createRollupFetchTransactionHash';
import { nativeTokenDecimalsTo18Decimals } from './utils/decimals';

const parentChainPublicClient = createPublicClient({
chain: nitroTestnodeL2,
Expand All @@ -19,6 +20,7 @@ const testnodeAccounts = getNitroTestnodePrivateKeyAccounts();
const l3TokenBridgeDeployer = testnodeAccounts.l3TokenBridgeDeployer;
const batchPosters = [testnodeAccounts.deployer.address];
const validators = [testnodeAccounts.deployer.address];
const nativeTokenDecimals = Number(process.env.DECIMALS) ?? 18;

describe(`create an AnyTrust chain that uses ETH as gas token`, async () => {
const { createRollupConfig, createRollupInformation } = await createRollupHelper({
Expand Down Expand Up @@ -68,7 +70,7 @@ describe(`create an AnyTrust chain that uses a custom gas token`, async () => {
client: parentChainPublicClient,
});

it(`successfully deploys core contracts through rollup creator (18 decimals)`, async () => {
it(`successfully deploys core contracts through rollup creator`, async () => {
// assert all inputs are correct
const [arg] = createRollupInformation.transaction.getInputs();
expect(arg.config).toEqual(createRollupConfig);
Expand All @@ -77,7 +79,9 @@ describe(`create an AnyTrust chain that uses a custom gas token`, async () => {
expect(arg.maxDataSize).toEqual(104_857n);
expect(arg.nativeToken).toEqual(nativeToken);
expect(arg.deployFactoriesToL2).toEqual(true);
expect(arg.maxFeePerGasForRetryables).toEqual(parseGwei('0.1'));
expect(arg.maxFeePerGasForRetryables).toEqual(
nativeTokenDecimalsTo18Decimals({ amount: 100n, decimals: nativeTokenDecimals }),
);

// assert the transaction executed successfully
expect(createRollupInformation.transactionReceipt.status).toEqual('success');
Expand Down
16 changes: 15 additions & 1 deletion src/createRollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from './createRollupPrepareTransaction';
import { CreateRollupParams } from './types/createRollupTypes';
import { validateParentChain } from './types/ParentChain';
import { getNativeTokenDecimals, nativeTokenDecimalsTo18Decimals } from './utils/decimals';
import { defaults as createRollupDefaults } from './createRollupDefaults';

type EnsureCustomGasTokenAllowanceGrantedToRollupCreatorParams<TChain extends Chain | undefined> = {
nativeToken: Address;
Expand Down Expand Up @@ -170,9 +172,21 @@ export async function createRollup<TChain extends Chain | undefined>({
});
}

const decimals = await getNativeTokenDecimals({
publicClient: parentChainPublicClient,
nativeTokenAddress: nativeToken ?? zeroAddress,
});
const maxFeePerGas = nativeTokenDecimalsTo18Decimals({
amount: params.maxFeePerGasForRetryables ?? createRollupDefaults.maxFeePerGasForRetryables,
decimals,
});

// prepare the transaction for deploying the core contracts
const txRequest = await createRollupPrepareTransactionRequest({
params,
params: {
...params,
maxFeePerGasForRetryables: maxFeePerGas,
},
account: account.address,
publicClient: parentChainPublicClient,
});
Expand Down
11 changes: 1 addition & 10 deletions src/createRollupGetRetryablesFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
EstimateGasParameters,
encodeFunctionData,
decodeFunctionResult,
zeroAddress,
} from 'viem';

import { rollupCreatorABI } from './contracts/RollupCreator';
Expand All @@ -16,7 +15,6 @@ import { isCustomFeeTokenAddress } from './utils/isCustomFeeTokenAddress';
import { defaults as createRollupDefaults } from './createRollupDefaults';
import { applyPercentIncrease } from './utils/gasOverrides';
import { createRollupDefaultRetryablesFees } from './constants';
import { getNativeTokenDecimals, scaleToNativeTokenDecimals } from './utils/decimals';

const deployHelperABI = [
{
Expand Down Expand Up @@ -102,14 +100,7 @@ export async function createRollupGetRetryablesFees<TChain extends Chain | undef
const isCustomGasToken = isCustomFeeTokenAddress(nativeToken);

const inbox = isCustomGasToken ? erc20TemplateInbox : ethTemplateInbox;
const decimals = await getNativeTokenDecimals({
publicClient,
nativeTokenAddress: nativeToken ?? zeroAddress,
});
const maxFeePerGas = scaleToNativeTokenDecimals({
amount: maxFeePerGasForRetryables ?? createRollupDefaults.maxFeePerGasForRetryables,
decimals,
});
const maxFeePerGas = maxFeePerGasForRetryables ?? createRollupDefaults.maxFeePerGasForRetryables;

const baseFeeWithBuffer = applyPercentIncrease({
base: await publicClient.getGasPrice(),
Expand Down
19 changes: 12 additions & 7 deletions src/createTokenBridge.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { createTokenBridgePrepareSetWethGatewayTransactionRequest } from './crea
import { createTokenBridgePrepareSetWethGatewayTransactionReceipt } from './createTokenBridgePrepareSetWethGatewayTransactionReceipt';
import { createTokenBridge } from './createTokenBridge';
import { TokenBridgeContracts } from './types/TokenBridgeContracts';
import { scaleToNativeTokenDecimals } from './utils/decimals';

const testnodeAccounts = getNitroTestnodePrivateKeyAccounts();
const l2RollupOwner = testnodeAccounts.l2RollupOwner;
Expand Down Expand Up @@ -104,6 +105,8 @@ async function checkWethGateways(
expect(tokenBridgeContracts.orbitChainContracts.wethGateway).not.toEqual(zeroAddress);
}

const nativeTokenDecimals = Number(process.env.DECIMALS) ?? 18;

describe('createTokenBridge utils function', () => {
it(`successfully deploys token bridge contracts through token bridge creator`, async () => {
const testnodeInformation = getInformationFromTestnode();
Expand Down Expand Up @@ -218,7 +221,10 @@ describe('createTokenBridge utils function', () => {
data: encodeFunctionData({
abi: erc20ABI,
functionName: 'transfer',
args: [l3RollupOwner.address, parseEther('500')],
args: [
l3RollupOwner.address,
scaleToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }),
],
}),
value: BigInt(0),
account: l3TokenBridgeDeployer,
Expand Down Expand Up @@ -368,7 +374,7 @@ describe('createTokenBridge', () => {
checkWethGateways(tokenBridgeContracts, { customFeeToken: false });
});

it('successfully deploys token bridge contracts with a custom fee token (18 decimals)', async () => {
it('successfully deploys token bridge contracts with a custom fee token', async () => {
const testnodeInformation = getInformationFromTestnode();

// deploy a fresh token bridge creator, because it is only possible to deploy one token bridge per rollup per token bridge creator
Expand All @@ -384,7 +390,10 @@ describe('createTokenBridge', () => {
data: encodeFunctionData({
abi: erc20ABI,
functionName: 'transfer',
args: [l3RollupOwner.address, parseEther('500')],
args: [
l3RollupOwner.address,
scaleToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }),
],
}),
value: BigInt(0),
account: l3TokenBridgeDeployer,
Expand Down Expand Up @@ -437,10 +446,6 @@ describe('createTokenBridge', () => {
checkWethGateways(tokenBridgeContracts, { customFeeToken: true });
});

it('successfully deploys token bridge contracts with a custom fee token (non-18 decimals)', async () => {
// Deploy a custom chain with non-18 decimals
});

it('should throw when createTokenBridge is called multiple times', async () => {
const testnodeInformation = getInformationFromTestnode();

Expand Down

0 comments on commit 86785fb

Please sign in to comment.