CCQ/SDK/js-query: Make tests verify results

This commit is contained in:
Bruce Riley 2023-11-14 10:43:08 -06:00 committed by Evan Gray
parent a2022c77e1
commit 05d7151007
2 changed files with 221 additions and 72 deletions

View File

@ -20,6 +20,15 @@
"call": "0x18160ddd"
}
},
{
"ethCall": {
"note:": "Decimals of WETH on Goerli",
"chain": 2,
"contractAddress": "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6",
"call": "0x313ce567"
}
},
{
"ethCall": {
"note:": "Name of WETH on Devnet",
@ -36,6 +45,14 @@
"call": "0x18160ddd"
}
},
{
"ethCall": {
"note:": "Decimals of WETH on Devnet",
"chain": 2,
"contractAddress": "0xDDb64fE46a91D46ee29420539FC25FD07c5FEa3E",
"call": "0x313ce567"
}
},
{
"ethCallByTimestamp": {
"note:": "Name of WETH on Goerli",
@ -68,6 +85,14 @@
"call": "0x18160ddd"
}
},
{
"ethCallByTimestamp": {
"note:": "Decimals of WETH on Devnet",
"chain": 2,
"contractAddress": "0xDDb64fE46a91D46ee29420539FC25FD07c5FEa3E",
"call": "0x313ce567"
}
},
{
"ethCallWithFinality": {
"note:": "Name of WETH on Goerli",
@ -99,6 +124,14 @@
"contractAddress": "0xDDb64fE46a91D46ee29420539FC25FD07c5FEa3E",
"call": "0x18160ddd"
}
},
{
"ethCallWithFinality": {
"note:": "Decimals of WETH on Devnet",
"chain": 2,
"contractAddress": "0xDDb64fE46a91D46ee29420539FC25FD07c5FEa3E",
"call": "0x313ce567"
}
}
]
},

View File

@ -9,6 +9,7 @@ import {
import Web3, { ETH_DATA_FORMAT } from "web3";
import axios from "axios";
import {
ChainQueryType,
EthCallData,
EthCallQueryRequest,
EthCallByTimestampQueryRequest,
@ -16,6 +17,10 @@ import {
PerChainQueryRequest,
QueryRequest,
sign,
QueryResponse,
EthCallQueryResponse,
EthCallByTimestampQueryResponse,
EthCallWithFinalityQueryResponse,
} from "..";
jest.setTimeout(125000);
@ -98,14 +103,14 @@ describe("eth call", () => {
test("serialize request", () => {
const toAddress = "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270";
const nameCallData = createTestEthCallData(toAddress, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
toAddress,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const ethCall = new EthCallQueryRequest("0x28d9630", [
nameCallData,
totalSupplyCallData,
decimalsCallData,
]);
const chainId = 5;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -113,20 +118,20 @@ describe("eth call", () => {
const request = new QueryRequest(nonce, [ethQuery]);
const serialized = request.serialize();
expect(Buffer.from(serialized).toString("hex")).toEqual(
"0100000001010005010000004600000009307832386439363330020d500b1d8e8ef31e21c99d1db9a6444d3adf12700000000406fdde030d500b1d8e8ef31e21c99d1db9a6444d3adf12700000000418160ddd"
"0100000001010005010000004600000009307832386439363330020d500b1d8e8ef31e21c99d1db9a6444d3adf12700000000406fdde030d500b1d8e8ef31e21c99d1db9a6444d3adf127000000004313ce567"
);
});
test("successful query", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const blockNumber = await web3.eth.getBlockNumber(ETH_DATA_FORMAT);
const ethCall = new EthCallQueryRequest(blockNumber, [
nameCallData,
totalSupplyCallData,
decimalsCallData,
]);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -144,21 +149,46 @@ describe("eth call", () => {
{ headers: { "X-API-Key": "my_secret_key" } }
);
expect(response.status).toBe(200);
const queryResponse = QueryResponse.from(response.data.bytes);
expect(queryResponse.version).toEqual(1);
expect(queryResponse.requestChainId).toEqual(0);
expect(queryResponse.request.version).toEqual(1);
expect(queryResponse.request.requests.length).toEqual(1);
expect(queryResponse.request.requests[0].chainId).toEqual(2);
expect(queryResponse.request.requests[0].query.type()).toEqual(
ChainQueryType.EthCall
);
const ecr = queryResponse.responses[0].response as EthCallQueryResponse;
expect(ecr.blockNumber).toEqual(BigInt(blockNumber));
expect(ecr.blockHash).toEqual(
(await web3.eth.getBlock(BigInt(blockNumber))).hash
);
expect(ecr.results.length).toEqual(2);
expect(ecr.results[0]).toEqual(
// Name
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d5772617070656420457468657200000000000000000000000000000000000000"
);
expect(ecr.results[1]).toEqual(
// Decimals
"0x0000000000000000000000000000000000000000000000000000000000000012"
);
});
// TODO: This test works in Goerli testnet but not devnet. Try it again after PR #3395 lands.
test.skip("get block by hash should work", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const blockNumber = await web3.eth.getBlockNumber(ETH_DATA_FORMAT);
const block = await web3.eth.getBlock(BigInt(blockNumber));
if (block.hash != undefined) {
const ethCall = new EthCallQueryRequest(block.hash?.toString(), [
nameCallData,
totalSupplyCallData,
decimalsCallData,
]);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -180,15 +210,15 @@ describe("eth call", () => {
});
test("missing api-key should fail", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const blockNumber = await web3.eth.getBlockNumber(ETH_DATA_FORMAT);
const ethCall = new EthCallQueryRequest(blockNumber, [
nameCallData,
totalSupplyCallData,
decimalsCallData,
]);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -212,15 +242,15 @@ describe("eth call", () => {
});
test("invalid api-key should fail", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const blockNumber = await web3.eth.getBlockNumber(ETH_DATA_FORMAT);
const ethCall = new EthCallQueryRequest(blockNumber, [
nameCallData,
totalSupplyCallData,
decimalsCallData,
]);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -248,15 +278,15 @@ describe("eth call", () => {
});
test("unauthorized call should fail", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const blockNumber = await web3.eth.getBlockNumber(ETH_DATA_FORMAT);
const ethCall = new EthCallQueryRequest(blockNumber, [
nameCallData,
totalSupplyCallData, // API key "my_secret_key_2" is not authorized to do total supply.
decimalsCallData, // API key "my_secret_key_2" is not authorized to do total supply.
]);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -279,22 +309,22 @@ describe("eth call", () => {
err = true;
expect(error.response.status).toBe(400);
expect(error.response.data).toBe(
`call "ethCall:2:000000000000000000000000ddb64fe46a91d46ee29420539fc25fd07c5fea3e:18160ddd" not authorized\n`
`call "ethCall:2:000000000000000000000000ddb64fe46a91d46ee29420539fc25fd07c5fea3e:313ce567" not authorized\n`
);
});
expect(err).toBe(true);
});
test("unsigned query should fail if not allowed", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const blockNumber = await web3.eth.getBlockNumber(ETH_DATA_FORMAT);
const ethCall = new EthCallQueryRequest(blockNumber, [
nameCallData,
totalSupplyCallData,
decimalsCallData,
]);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -366,16 +396,16 @@ describe("eth call", () => {
test("serialize eth_call_by_timestamp request", () => {
const toAddress = "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270";
const nameCallData = createTestEthCallData(toAddress, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
toAddress,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const ethCall = new EthCallByTimestampQueryRequest(
BigInt(1697216322000000),
"0x28d9630",
"0x28d9631",
[nameCallData, totalSupplyCallData]
[nameCallData, decimalsCallData]
);
const chainId = 5;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -383,15 +413,15 @@ describe("eth call", () => {
const request = new QueryRequest(nonce, [ethQuery]);
const serialized = request.serialize();
expect(Buffer.from(serialized).toString("hex")).toEqual(
"0100000001010005020000005b0006079bf7fad4800000000930783238643936333000000009307832386439363331020d500b1d8e8ef31e21c99d1db9a6444d3adf12700000000406fdde030d500b1d8e8ef31e21c99d1db9a6444d3adf12700000000418160ddd"
"0100000001010005020000005b0006079bf7fad4800000000930783238643936333000000009307832386439363331020d500b1d8e8ef31e21c99d1db9a6444d3adf12700000000406fdde030d500b1d8e8ef31e21c99d1db9a6444d3adf127000000004313ce567"
);
});
test("successful eth_call_by_timestamp query with block hints", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const [targetBlockTime, targetBlockNumber, followingBlockNumber] =
await getEthCallByTimestampArgs();
@ -399,7 +429,7 @@ describe("eth call", () => {
targetBlockTime,
targetBlockNumber.toString(16),
followingBlockNumber.toString(16),
[nameCallData, totalSupplyCallData]
[nameCallData, decimalsCallData]
);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -417,13 +447,43 @@ describe("eth call", () => {
{ headers: { "X-API-Key": "my_secret_key" } }
);
expect(response.status).toBe(200);
const queryResponse = QueryResponse.from(response.data.bytes);
expect(queryResponse.version).toEqual(1);
expect(queryResponse.requestChainId).toEqual(0);
expect(queryResponse.request.version).toEqual(1);
expect(queryResponse.request.requests.length).toEqual(1);
expect(queryResponse.request.requests[0].chainId).toEqual(2);
expect(queryResponse.request.requests[0].query.type()).toEqual(
ChainQueryType.EthCallByTimeStamp
);
const ecr = queryResponse.responses[0]
.response as EthCallByTimestampQueryResponse;
expect(ecr.targetBlockNumber).toEqual(BigInt(targetBlockNumber));
expect(ecr.targetBlockHash).toEqual(
(await web3.eth.getBlock(BigInt(targetBlockNumber))).hash
);
expect(ecr.followingBlockNumber).toEqual(BigInt(followingBlockNumber));
expect(ecr.followingBlockHash).toEqual(
(await web3.eth.getBlock(BigInt(followingBlockNumber))).hash
);
expect(ecr.results.length).toEqual(2);
expect(ecr.results[0]).toEqual(
// Name
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d5772617070656420457468657200000000000000000000000000000000000000"
);
expect(ecr.results[1]).toEqual(
// Decimals
"0x0000000000000000000000000000000000000000000000000000000000000012"
);
});
test("successful eth_call_by_timestamp query without block hints", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const [targetBlockTime, targetBlockNumber, followingBlockNumber] =
await getEthCallByTimestampArgs();
@ -431,7 +491,7 @@ describe("eth call", () => {
targetBlockTime + BigInt(5000),
"",
"",
[nameCallData, totalSupplyCallData]
[nameCallData, decimalsCallData]
);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -449,13 +509,43 @@ describe("eth call", () => {
{ headers: { "X-API-Key": "my_secret_key" } }
);
expect(response.status).toBe(200);
const queryResponse = QueryResponse.from(response.data.bytes);
expect(queryResponse.version).toEqual(1);
expect(queryResponse.requestChainId).toEqual(0);
expect(queryResponse.request.version).toEqual(1);
expect(queryResponse.request.requests.length).toEqual(1);
expect(queryResponse.request.requests[0].chainId).toEqual(2);
expect(queryResponse.request.requests[0].query.type()).toEqual(
ChainQueryType.EthCallByTimeStamp
);
const ecr = queryResponse.responses[0]
.response as EthCallByTimestampQueryResponse;
expect(ecr.targetBlockNumber).toEqual(BigInt(targetBlockNumber));
expect(ecr.targetBlockHash).toEqual(
(await web3.eth.getBlock(BigInt(targetBlockNumber))).hash
);
expect(ecr.followingBlockNumber).toEqual(BigInt(followingBlockNumber));
expect(ecr.followingBlockHash).toEqual(
(await web3.eth.getBlock(BigInt(followingBlockNumber))).hash
);
expect(ecr.results.length).toEqual(2);
expect(ecr.results[0]).toEqual(
// Name
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d5772617070656420457468657200000000000000000000000000000000000000"
);
expect(ecr.results[1]).toEqual(
// Decimals
"0x0000000000000000000000000000000000000000000000000000000000000012"
);
});
test("eth_call_by_timestamp query without target timestamp", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const followingBlockNum = await web3.eth.getBlockNumber(ETH_DATA_FORMAT);
const followingBlock = await web3.eth.getBlock(BigInt(followingBlockNum));
@ -466,7 +556,7 @@ describe("eth call", () => {
BigInt(0),
targetBlock.number.toString(16),
followingBlock.number.toString(16),
[nameCallData, totalSupplyCallData]
[nameCallData, decimalsCallData]
);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -496,10 +586,10 @@ describe("eth call", () => {
});
test("eth_call_by_timestamp query with following hint but not target hint should fail", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const followingBlockNum = await web3.eth.getBlockNumber(ETH_DATA_FORMAT);
const followingBlock = await web3.eth.getBlock(BigInt(followingBlockNum));
@ -511,7 +601,7 @@ describe("eth call", () => {
targetBlockTime,
"",
followingBlock.number.toString(16),
[nameCallData, totalSupplyCallData]
[nameCallData, decimalsCallData]
);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -541,10 +631,10 @@ describe("eth call", () => {
});
test("eth_call_by_timestamp query with target hint but not following hint should fail", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const followingBlockNum = await web3.eth.getBlockNumber(ETH_DATA_FORMAT);
const targetBlock = await web3.eth.getBlock(
@ -555,7 +645,7 @@ describe("eth call", () => {
targetBlockTime,
targetBlock.number.toString(16),
"",
[nameCallData, totalSupplyCallData]
[nameCallData, decimalsCallData]
);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -586,15 +676,15 @@ describe("eth call", () => {
test("serialize eth_call_with_finality request", () => {
const toAddress = "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270";
const nameCallData = createTestEthCallData(toAddress, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
toAddress,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const ethCall = new EthCallWithFinalityQueryRequest(
"0x28d9630",
"finalized",
[nameCallData, totalSupplyCallData]
[nameCallData, decimalsCallData]
);
const chainId = 5;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -602,15 +692,15 @@ describe("eth call", () => {
const request = new QueryRequest(nonce, [ethQuery]);
const serialized = request.serialize();
expect(Buffer.from(serialized).toString("hex")).toEqual(
"01000000010100050300000053000000093078323864393633300000000966696e616c697a6564020d500b1d8e8ef31e21c99d1db9a6444d3adf12700000000406fdde030d500b1d8e8ef31e21c99d1db9a6444d3adf12700000000418160ddd"
"01000000010100050300000053000000093078323864393633300000000966696e616c697a6564020d500b1d8e8ef31e21c99d1db9a6444d3adf12700000000406fdde030d500b1d8e8ef31e21c99d1db9a6444d3adf127000000004313ce567"
);
});
test("successful eth_call_with_finality query", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
// Jump into the future a bit so the watcher has to wait for finality.
const blockNumber =
@ -618,7 +708,7 @@ describe("eth call", () => {
const ethCall = new EthCallWithFinalityQueryRequest(
blockNumber.toString(16),
"finalized",
[nameCallData, totalSupplyCallData]
[nameCallData, decimalsCallData]
);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -636,18 +726,44 @@ describe("eth call", () => {
{ headers: { "X-API-Key": "my_secret_key" } }
);
expect(response.status).toBe(200);
const queryResponse = QueryResponse.from(response.data.bytes);
expect(queryResponse.version).toEqual(1);
expect(queryResponse.requestChainId).toEqual(0);
expect(queryResponse.request.version).toEqual(1);
expect(queryResponse.request.requests.length).toEqual(1);
expect(queryResponse.request.requests[0].chainId).toEqual(2);
expect(queryResponse.request.requests[0].query.type()).toEqual(
ChainQueryType.EthCallWithFinality
);
const ecr = queryResponse.responses[0]
.response as EthCallWithFinalityQueryResponse;
expect(ecr.blockNumber).toEqual(BigInt(blockNumber));
expect(ecr.blockHash).toEqual(
(await web3.eth.getBlock(BigInt(blockNumber))).hash
);
expect(ecr.results.length).toEqual(2);
expect(ecr.results[0]).toEqual(
// Name
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d5772617070656420457468657200000000000000000000000000000000000000"
);
expect(ecr.results[1]).toEqual(
// Decimals
"0x0000000000000000000000000000000000000000000000000000000000000012"
);
});
test("eth_call_with_finality query without finality should fail", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const ethCall = new EthCallWithFinalityQueryRequest(
"0x28d9630",
"" as any,
[nameCallData, totalSupplyCallData]
[nameCallData, decimalsCallData]
);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);
@ -677,15 +793,15 @@ describe("eth call", () => {
});
test("eth_call_with_finality query with bad finality should fail", async () => {
const nameCallData = createTestEthCallData(WETH_ADDRESS, "name", "string");
const totalSupplyCallData = createTestEthCallData(
const decimalsCallData = createTestEthCallData(
WETH_ADDRESS,
"totalSupply",
"uint256"
"decimals",
"uint8"
);
const ethCall = new EthCallWithFinalityQueryRequest(
"0x28d9630",
"HelloWorld" as any,
[nameCallData, totalSupplyCallData]
[nameCallData, decimalsCallData]
);
const chainId = 2;
const ethQuery = new PerChainQueryRequest(chainId, ethCall);