Skip to content

Commit

Permalink
Feat: Add v1 publicActionsChildChain
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Jul 10, 2024
1 parent a8bab3b commit 7e7580e
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,31 @@ export {
nitroTestnodeL2,
nitroTestnodeL3,
};

export const xai = defineChain({
id: 660279,
network: 'Xai Mainnet',
name: 'Xai Mainnet',
nativeCurrency: { name: 'Xai', symbol: 'XAI', decimals: 18 },
rpcUrls: {
public: {
http: ['https://xai-chain.net/rpc'],
},
default: {
http: ['https://xai-chain.net/rpc'],
},
},
blockExplorers: {
default: {
name: 'Blockscout',
url: 'https://explorer.xai-chain.net',
},
},
contracts: {
multicall3: {
address: '0xca11bde05977b3631167028862be2a173976ca11',
blockCreated: 222549,
},
},
testnet: false,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Getters > [getAllChainOwners] Should return all chain owners 1`] = `[]`;

exports[`Getters > [getGasAccountingParams] Should return gas accounting params 1`] = `
[
7000000n,
32000000n,
32000000n,
]
`;

exports[`Getters > [getInfraFeeAccount] Should return infra fee account 1`] = `"0xbF5041Fc07E1c866D15c749156657B8eEd0fb649"`;

exports[`Getters > [getMinimumGasPrice] Should return minimum gas price 1`] = `10000000n`;

exports[`Getters > [getNetworkFeeAccount] Should return network fee account 1`] = `"0x32e7AF5A8151934F3787d0cD59EB6EDd0a736b1d"`;

exports[`Getters > [getParentBaseFeeEstimate] Should return parent base fee estimate 1`] = `106354669n`;

exports[`Getters > [getParentRewardRate] Should return parent reward rate 1`] = `0n`;

exports[`Getters > [getParentRewardRecipient] Should return parent reward recipient 1`] = `"0x2E041280627800801E90E9Ac83532fadb6cAd99A"`;

exports[`Getters > [getScheduledUpgrade] Should return scheduled upgrade 1`] = `
{
"arbosVersion": 0n,
"scheduledForTimestamp": 0n,
}
`;
178 changes: 178 additions & 0 deletions src/decorators/publicActionsChildChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { Chain, PublicClient, Transport } from 'viem';

// Getters
import {
getAllChainOwners,
GetAllChainOwnersParameters,
GetAllChainOwnersReturnType,
} from '../actions/getAllChainOwners';
import {
getInfraFeeAccount,
GetInfraFeeAccountParameters,
GetInfraFeeAccountReturnType,
} from '../actions/getInfraFeeAccount';
import {
getNetworkFeeAccount,
GetNetworkFeeAccountParameters,
GetNetworkFeeAccountReturnType,
} from '../actions/getNetworkFeeAccount';
import {
getScheduledUpgrade,
GetScheduledUpgradeParameters,
GetScheduledUpgradeReturnType,
} from '../actions/getScheduledUpgrade';
import {
isChainOwner,
IsChainOwnerParameters,
IsChainOwnerReturnType,
} from '../actions/isChainOwner';
import {
getGasAccountingParams,
GetGasAccountingParamsParameters,
GetGasAccountingParamsReturnType,
} from '../actions/getGasAccountingParams';
import {
getMinimumGasPrice,
GetMinimumGasPriceParameters,
GetMinimumGasPriceReturnType,
} from '../actions/getMinimumGasPrice';
import {
getParentBaseFeeEstimate,
GetParentBaseFeeEstimateParameters,
GetParentBaseFeeEstimateReturnType,
} from '../actions/getParentBaseFeeEstimate';
import {
getParentRewardRate,
GetParentRewardRateParameters,
GetParentRewardRateReturnType,
} from '../actions/getParentRewardRate';
import {
getParentRewardRecipient,
GetParentRewardRecipientParameters,
GetParentRewardRecipientReturnType,
} from '../actions/getParentRewardRecipient';
// Setters
import {
addChainOwner,
AddChainOwnerParameters,
AddChainOwnerReturnType,
} from '../actions/addChainOwner';
import {
removeChainOwner,
RemoveChainOwnerParameters,
RemoveChainOwnerReturnType,
} from '../actions/removeChainOwner';
import {
setMaxTxGasLimit,
SetMaxTxGasLimitParameters,
SetMaxTxGasLimitReturnType,
} from '../actions/setMaxTxGasLimit';
import {
setParentPricePerUnit,
SetParentPricePerUnitParameters,
SetParentPricePerUnitReturnType,
} from '../actions/setParentPricePerUnit';
import {
setParentPricingRewardRate,
SetParentPricingRewardRateParameters,
SetParentPricingRewardRateReturnType,
} from '../actions/setParentPricingRewardRate';
import {
setParentPricingRewardRecipient,
SetParentPricingRewardRecipientParameters,
SetParentPricingRewardRecipientReturnType,
} from '../actions/setParentPricingRewardRecipient';
import {
setSpeedLimit,
SetSpeedLimitParameters,
SetSpeedLimitReturnType,
} from '../actions/setSpeedLimit';

export type PublicActionsChildChain = {
// Getters
getAllChainOwners: (
parameters: GetAllChainOwnersParameters,
) => Promise<GetAllChainOwnersReturnType>;
getInfraFeeAccount: (
parameters: GetInfraFeeAccountParameters,
) => Promise<GetInfraFeeAccountReturnType>;
getNetworkFeeAccount: (
parameters: GetNetworkFeeAccountParameters,
) => Promise<GetNetworkFeeAccountReturnType>;
getScheduledUpgrade: (
parameters: GetScheduledUpgradeParameters,
) => Promise<GetScheduledUpgradeReturnType>;
isChainOwner: (parameters: IsChainOwnerParameters) => Promise<IsChainOwnerReturnType>;
getGasAccountingParams: (
parameters: GetGasAccountingParamsParameters,
) => Promise<GetGasAccountingParamsReturnType>;
getMinimumGasPrice: (
parameters: GetMinimumGasPriceParameters,
) => Promise<GetMinimumGasPriceReturnType>;
getParentBaseFeeEstimate: (
parameters: GetParentBaseFeeEstimateParameters,
) => Promise<GetParentBaseFeeEstimateReturnType>;
getParentRewardRate: (
parameters: GetParentRewardRateParameters,
) => Promise<GetParentRewardRateReturnType>;
getParentRewardRecipient: (
parameters: GetParentRewardRecipientParameters,
) => Promise<GetParentRewardRecipientReturnType>;
// Setters
addChainOwner: (parameters: AddChainOwnerParameters) => Promise<AddChainOwnerReturnType>;
removeChainOwner: (parameters: RemoveChainOwnerParameters) => Promise<RemoveChainOwnerReturnType>;
setMaxTxGasLimit: (parameters: SetMaxTxGasLimitParameters) => Promise<SetMaxTxGasLimitReturnType>;
setParentPricePerUnit: (
parameters: SetParentPricePerUnitParameters,
) => Promise<SetParentPricePerUnitReturnType>;
setParentPricingRewardRate: (
parameters: SetParentPricingRewardRateParameters,
) => Promise<SetParentPricingRewardRateReturnType>;
setParentPricingRewardRecipient: (
parameters: SetParentPricingRewardRecipientParameters,
) => Promise<SetParentPricingRewardRecipientReturnType>;
setSpeedLimit: (parameters: SetSpeedLimitParameters) => Promise<SetSpeedLimitReturnType>;
};

/**
* Public actions for child chain
*
* @example
* import { createPublicClient, http } from 'viem'
* import { publicActionsChildChain } from '@arbitrum/orbit-sdk'
*
* export const publicClientChildChain = createPublicClient({
* chain: orbitChain,
* transport: http(),
* }).extend(publicActionsChildChain())
*
* const isAChainOwner = await publicClientChildChain.isChainOwner({ address: zeroAddress })
*/
export function publicActionsChildChain<
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
>() {
return (client: PublicClient<TTransport, TChain>) => {
return {
// Getters
getAllChainOwners: () => getAllChainOwners(client),
getInfraFeeAccount: () => getInfraFeeAccount(client),
getNetworkFeeAccount: () => getNetworkFeeAccount(client),
getScheduledUpgrade: () => getScheduledUpgrade(client),
isChainOwner: (args) => isChainOwner(client, args),
getGasAccountingParams: () => getGasAccountingParams(client),
getMinimumGasPrice: () => getMinimumGasPrice(client),
getParentBaseFeeEstimate: () => getParentBaseFeeEstimate(client),
getParentRewardRate: () => getParentRewardRate(client),
getParentRewardRecipient: () => getParentRewardRecipient(client),
// Setters
addChainOwner: (args) => addChainOwner(client, args),
removeChainOwner: (args) => removeChainOwner(client, args),
setMaxTxGasLimit: (args) => setMaxTxGasLimit(client, args),
setParentPricePerUnit: (args) => setParentPricePerUnit(client, args),
setParentPricingRewardRate: (args) => setParentPricingRewardRate(client, args),
setParentPricingRewardRecipient: (args) => setParentPricingRewardRecipient(client, args),
setSpeedLimit: (args) => setSpeedLimit(client, args),
} satisfies PublicActionsChildChain;
};
}
72 changes: 72 additions & 0 deletions src/decorators/publicActionsChildChain.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { it, expect, describe } from 'vitest';

import { createPublicClient, http, zeroAddress } from 'viem';
import { publicActionsChildChain } from './publicActionsChildChain';
import { arbitrum } from 'viem/chains';
import { xai } from '../chains';

const client = createPublicClient({
chain: arbitrum,
transport: http(),
}).extend(publicActionsChildChain());

describe('Getters', () => {
it('[getAllChainOwners] Should return all chain owners', async () => {
const allChainOwners = await client.getAllChainOwners();
expect(allChainOwners).toMatchSnapshot();
});

it('[getInfraFeeAccount] Should return infra fee account', async () => {
const infraFeeAccount = await client.getInfraFeeAccount();
expect(infraFeeAccount).toMatchSnapshot();
});

it('[getNetworkFeeAccount] Should return network fee account', async () => {
const networkFeeAccount = await client.getNetworkFeeAccount();
expect(networkFeeAccount).toMatchSnapshot();
});

it('[getScheduledUpgrade] Should return scheduled upgrade', async () => {
const scheduledUpgrade = await client.getScheduledUpgrade();
expect(scheduledUpgrade).toMatchSnapshot();
});

it('[isChainOwner] Should return if an address is a chain owner', async () => {
const xaiClient = createPublicClient({
chain: xai,
transport: http(),
}).extend(publicActionsChildChain());
const isZeroAddressChainOwner = await xaiClient.isChainOwner({ address: zeroAddress });
expect(isZeroAddressChainOwner).toBeFalsy();
const allChainOwners = await xaiClient.getAllChainOwners();
const isChainOwner = await xaiClient.isChainOwner({
address: allChainOwners[0],
});
expect(isChainOwner).toBeTruthy();
});

it('[getGasAccountingParams] Should return gas accounting params', async () => {
const gasAccountingParams = await client.getGasAccountingParams();
expect(gasAccountingParams).toMatchSnapshot();
});

it('[getMinimumGasPrice] Should return minimum gas price', async () => {
const minimumGasPrice = await client.getMinimumGasPrice();
expect(minimumGasPrice).toMatchSnapshot();
});

it('[getParentBaseFeeEstimate] Should return parent base fee estimate', async () => {
const parentBaseFeeEstimate = await client.getParentBaseFeeEstimate();
expect(parentBaseFeeEstimate).toMatchSnapshot();

Check failure on line 60 in src/decorators/publicActionsChildChain.unit.test.ts

View workflow job for this annotation

GitHub Actions / Test (Unit)

src/decorators/publicActionsChildChain.unit.test.ts > Getters > [getParentBaseFeeEstimate] Should return parent base fee estimate

Error: Snapshot `Getters > [getParentBaseFeeEstimate] Should return parent base fee estimate 1` mismatched - Expected + Received - 106354669n + 131196189n ❯ src/decorators/publicActionsChildChain.unit.test.ts:60:35
});

it('[getParentRewardRate] Should return parent reward rate', async () => {
const parentRewardRate = await client.getParentRewardRate();
expect(parentRewardRate).toMatchSnapshot();
});

it('[getParentRewardRecipient] Should return parent reward recipient', async () => {
const parentRewardRecipient = await client.getParentRewardRecipient();
expect(parentRewardRecipient).toMatchSnapshot();
});
});

0 comments on commit 7e7580e

Please sign in to comment.