Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
iosh committed Dec 3, 2024
1 parent 9087dd1 commit f66ad95
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
3 changes: 3 additions & 0 deletions __test__/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ export const localChain = defineChain({
},
},
});

// biome-ignore format: minimal abi
export const InternalContractsABI = {adminControl:[{inputs:[{internalType:"address",name:"contractAddr",type:"address"},],name:"getAdmin",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},{internalType:"address",name:"newAdmin",type:"address"},],name:"setAdmin",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},],name:"destroy",outputs:[],stateMutability:"nonpayable",type:"function",},],sponsorWhitelistControl:[{inputs:[{internalType:"address[]",name:"",type:"address[]"}],name:"addPrivilege",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},{internalType:"address[]",name:"addresses",type:"address[]"},],name:"addPrivilegeByAdmin",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},],name:"getSponsorForCollateral",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},],name:"getSponsorForGas",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},],name:"getSponsoredBalanceForCollateral",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},],name:"getSponsoredBalanceForGas",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},],name:"getSponsoredGasFeeUpperBound",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},],name:"isAllWhitelisted",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},{internalType:"address",name:"user",type:"address"},],name:"isWhitelisted",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address[]",name:"",type:"address[]"}],name:"removePrivilege",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},{internalType:"address[]",name:"addresses",type:"address[]"},],name:"removePrivilegeByAdmin",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},],name:"setSponsorForCollateral",outputs:[],stateMutability:"payable",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},{internalType:"uint256",name:"upperBound",type:"uint256"},],name:"setSponsorForGas",outputs:[],stateMutability:"payable",type:"function",},{inputs:[{internalType:"address",name:"contractAddr",type:"address"},],name:"getAvailableStoragePoints",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},],staking:[{inputs:[{internalType:"address",name:"user",type:"address"}],name:"getStakingBalance",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"user",type:"address"},{internalType:"uint256",name:"blockNumber",type:"uint256"},],name:"getLockedStakingBalance",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"user",type:"address"},{internalType:"uint256",name:"blockNumber",type:"uint256"},],name:"getVotePower",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"uint256",name:"amount",type:"uint256"}],name:"deposit",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"uint256",name:"amount",type:"uint256"}],name:"withdraw",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint256",name:"unlockBlockNumber",type:"uint256"},],name:"voteLock",outputs:[],stateMutability:"nonpayable",type:"function",},],confluxContext:[{inputs:[],name:"epochNumber",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},{inputs:[],name:"posHeight",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},{inputs:[],name:"finalizedEpochNumber",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},],poSRegister:[{anonymous:false,inputs:[{indexed:true,internalType:"bytes32",name:"identifier",type:"bytes32",},{indexed:false,internalType:"uint64",name:"votePower",type:"uint64",},],name:"IncreaseStake",type:"event",},{anonymous:false,inputs:[{indexed:true,internalType:"bytes32",name:"identifier",type:"bytes32",},{indexed:false,internalType:"bytes",name:"blsPubKey",type:"bytes",},{indexed:false,internalType:"bytes",name:"vrfPubKey",type:"bytes",},],name:"Register",type:"event",},{anonymous:false,inputs:[{indexed:true,internalType:"bytes32",name:"identifier",type:"bytes32",},{indexed:false,internalType:"uint64",name:"votePower",type:"uint64",},],name:"Retire",type:"event",},{inputs:[{internalType:"bytes32",name:"indentifier",type:"bytes32"},{internalType:"uint64",name:"votePower",type:"uint64"},{internalType:"bytes",name:"blsPubKey",type:"bytes"},{internalType:"bytes",name:"vrfPubKey",type:"bytes"},{internalType:"bytes[2]",name:"blsPubKeyProof",type:"bytes[2]"},],name:"register",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"uint64",name:"votePower",type:"uint64"}],name:"increaseStake",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"uint64",name:"votePower",type:"uint64"}],name:"retire",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"bytes32",name:"identifier",type:"bytes32"},],name:"getVotes",outputs:[{internalType:"uint256",name:"",type:"uint256"},{internalType:"uint256",name:"",type:"uint256"},],stateMutability:"view",type:"function",},{inputs:[{internalType:"bytes32",name:"identifier",type:"bytes32"},],name:"identifierToAddress",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"addr",type:"address"}],name:"addressToIdentifier",outputs:[{internalType:"bytes32",name:"",type:"bytes32"}],stateMutability:"view",type:"function",},],crossSpaceCall:[{anonymous:false,inputs:[{indexed:true,internalType:"bytes20",name:"sender",type:"bytes20",},{indexed:true,internalType:"bytes20",name:"receiver",type:"bytes20",},{indexed:false,internalType:"uint256",name:"value",type:"uint256",},{indexed:false,internalType:"uint256",name:"nonce",type:"uint256",},{indexed:false,internalType:"bytes",name:"data",type:"bytes"},],name:"Call",type:"event",},{anonymous:false,inputs:[{indexed:true,internalType:"bytes20",name:"sender",type:"bytes20",},{indexed:true,internalType:"bytes20",name:"contract_address",type:"bytes20",},{indexed:false,internalType:"uint256",name:"value",type:"uint256",},{indexed:false,internalType:"uint256",name:"nonce",type:"uint256",},{indexed:false,internalType:"bytes",name:"init",type:"bytes"},],name:"Create",type:"event",},{anonymous:false,inputs:[{indexed:false,internalType:"bool",name:"success",type:"bool"},],name:"Outcome",type:"event",},{anonymous:false,inputs:[{indexed:true,internalType:"bytes20",name:"sender",type:"bytes20",},{indexed:true,internalType:"address",name:"receiver",type:"address",},{indexed:false,internalType:"uint256",name:"value",type:"uint256",},{indexed:false,internalType:"uint256",name:"nonce",type:"uint256",},],name:"Withdraw",type:"event",},{inputs:[{internalType:"bytes",name:"init",type:"bytes"}],name:"createEVM",outputs:[{internalType:"bytes20",name:"",type:"bytes20"}],stateMutability:"payable",type:"function",},{inputs:[{internalType:"bytes20",name:"to",type:"bytes20"}],name:"transferEVM",outputs:[{internalType:"bytes",name:"output",type:"bytes"}],stateMutability:"payable",type:"function",},{inputs:[{internalType:"bytes20",name:"to",type:"bytes20"},{internalType:"bytes",name:"data",type:"bytes"},],name:"callEVM",outputs:[{internalType:"bytes",name:"output",type:"bytes"}],stateMutability:"payable",type:"function",},{inputs:[{internalType:"bytes20",name:"to",type:"bytes20"},{internalType:"bytes",name:"data",type:"bytes"},],name:"staticCallEVM",outputs:[{internalType:"bytes",name:"output",type:"bytes"}],stateMutability:"view",type:"function",},{inputs:[],name:"deployEip1820",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"uint256",name:"value",type:"uint256"}],name:"withdrawFromMapped",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[{internalType:"address",name:"addr",type:"address"}],name:"mappedBalance",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"addr",type:"address"}],name:"mappedNonce",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},],paramsControl:[{anonymous:false,inputs:[{indexed:true,internalType:"uint64",name:"vote_round",type:"uint64",},{indexed:true,internalType:"address",name:"addr",type:"address",},{indexed:true,internalType:"uint16",name:"topic_index",type:"uint16",},{indexed:false,internalType:"uint256[3]",name:"votes",type:"uint256[3]",},],name:"CastVote",type:"event",},{anonymous:false,inputs:[{indexed:true,internalType:"uint64",name:"vote_round",type:"uint64",},{indexed:true,internalType:"address",name:"addr",type:"address",},{indexed:true,internalType:"uint16",name:"topic_index",type:"uint16",},{indexed:false,internalType:"uint256[3]",name:"votes",type:"uint256[3]",},],name:"RevokeVote",type:"event",},{inputs:[{internalType:"uint64",name:"vote_round",type:"uint64"},{components:[{internalType:"uint16",name:"topic_index",type:"uint16"},{internalType:"uint256[3]",name:"votes",type:"uint256[3]"},],internalType:"struct ParamsControl.Vote[]",name:"vote_data",type:"tuple[]",},],name:"castVote",outputs:[],stateMutability:"nonpayable",type:"function",},{inputs:[],name:"currentRound",outputs:[{internalType:"uint64",name:"",type:"uint64"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"uint64",name:"",type:"uint64"}],name:"posStakeForVotes",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function",},{inputs:[{internalType:"address",name:"addr",type:"address"}],name:"readVote",outputs:[{components:[{internalType:"uint16",name:"topic_index",type:"uint16"},{internalType:"uint256[3]",name:"votes",type:"uint256[3]"},],internalType:"struct ParamsControl.Vote[]",name:"",type:"tuple[]",},],stateMutability:"view",type:"function",},{inputs:[{internalType:"uint64",name:"vote_round",type:"uint64"}],name:"totalVotes",outputs:[{components:[{internalType:"uint16",name:"topic_index",type:"uint16"},{internalType:"uint256[3]",name:"votes",type:"uint256[3]"},],internalType:"struct ParamsControl.Vote[]",name:"",type:"tuple[]",},],stateMutability:"view",type:"function",},],} as const;
161 changes: 161 additions & 0 deletions __test__/internalContracts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { http, createPublicClient } from "cive";
import { encodeFunctionData, hexAddressToBase32 } from "cive/utils";
import { describe, expect, test } from "vitest";
import { createServer } from "../index";
import {
InternalContractsABI,
TEST_NETWORK_ID,
TEST_PK,
getFreePorts,
} from "./help";

describe("genesis", () => {
test("default", async () => {
const [jsonrpcHttpPort, udpAndTcpPort] = await getFreePorts();
const server = await createServer({
tcpPort: udpAndTcpPort,
udpPort: udpAndTcpPort,
chainId: TEST_NETWORK_ID,
devBlockIntervalMs: 100,
jsonrpcHttpPort: jsonrpcHttpPort,
genesisSecrets: TEST_PK,
});
await server.start();
const client = createPublicClient({
transport: http(`http://127.0.0.1:${jsonrpcHttpPort}`),
});

const adminControlAddress = hexAddressToBase32({
hexAddress: "0x0888000000000000000000000000000000000000",
networkId: TEST_NETWORK_ID,
});

const adminControl = await client.call({
to: adminControlAddress,
data: encodeFunctionData({
abi: InternalContractsABI.adminControl,
functionName: "getAdmin",
args: [adminControlAddress],
}),
});
expect(adminControl).toMatchInlineSnapshot(`
{
"data": "0x0000000000000000000000000000000000000000000000000000000000000000",
}
`);

const sponsorWhitelistControlAddress = hexAddressToBase32({
hexAddress: "0x0888000000000000000000000000000000000001",
networkId: TEST_NETWORK_ID,
});
const sponsorWhitelistControl = await client.call({
to: sponsorWhitelistControlAddress,
data: encodeFunctionData({
abi: InternalContractsABI.sponsorWhitelistControl,
functionName: "getSponsorForGas",
args: [adminControlAddress],
}),
});

expect(sponsorWhitelistControl).toMatchInlineSnapshot(`
{
"data": "0x0000000000000000000000000000000000000000000000000000000000000000",
}
`);

const stakingAddress = hexAddressToBase32({
hexAddress: "0x0888000000000000000000000000000000000002",
networkId: TEST_NETWORK_ID,
});
const staking = await client.call({
to: stakingAddress,
data: encodeFunctionData({
abi: InternalContractsABI.staking,
functionName: "getStakingBalance",
args: [adminControlAddress],
}),
});

expect(staking).toMatchInlineSnapshot(`
{
"data": "0x0000000000000000000000000000000000000000000000000000000000000000",
}
`);

const confluxContextAddress = hexAddressToBase32({
hexAddress: "0x0888000000000000000000000000000000000004",
networkId: TEST_NETWORK_ID,
});

const confluxContext = await client.call({
to: confluxContextAddress,
data: encodeFunctionData({
abi: InternalContractsABI.confluxContext,
functionName: "finalizedEpochNumber",
}),
});

expect(confluxContext).toMatchInlineSnapshot(`
{
"data": "0x0000000000000000000000000000000000000000000000000000000000000000",
}
`);

const poSRegisterAddress = hexAddressToBase32({
hexAddress: "0x0888000000000000000000000000000000000005",
networkId: TEST_NETWORK_ID,
});
const poSRegister = await client.call({
to: poSRegisterAddress,
data: encodeFunctionData({
abi: InternalContractsABI.poSRegister,
functionName: "addressToIdentifier",
args: [adminControlAddress],
}),
});

expect(poSRegister).toMatchInlineSnapshot(`
{
"data": "0x0000000000000000000000000000000000000000000000000000000000000000",
}
`);

const crossSpaceCallAddress = hexAddressToBase32({
hexAddress: "0x0888000000000000000000000000000000000006",
networkId: TEST_NETWORK_ID,
});
const crossSpaceCall = await client.call({
to: crossSpaceCallAddress,
data: encodeFunctionData({
abi: InternalContractsABI.crossSpaceCall,
functionName: "mappedNonce",
args: [adminControlAddress],
}),
});

expect(crossSpaceCall).toMatchInlineSnapshot(`
{
"data": "0x0000000000000000000000000000000000000000000000000000000000000000",
}
`);

const paramsControlAddress = hexAddressToBase32({
hexAddress: "0x0888000000000000000000000000000000000007",
networkId: TEST_NETWORK_ID,
});
const paramsControl = await client.call({
to: paramsControlAddress,
data: encodeFunctionData({
abi: InternalContractsABI.paramsControl,
functionName: "currentRound",
}),
});

expect(paramsControl).toMatchInlineSnapshot(`
{
"data": undefined,
}
`);
await server.stop();
});
});

0 comments on commit f66ad95

Please sign in to comment.