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

Add Queries Section #11

Merged
merged 123 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 114 commits
Commits
Show all changes
123 commits
Select commit Hold shift + click to select a range
8b87377
fresh slate
themacexpert Aug 3, 2024
da4e2e6
rev
themacexpert Aug 3, 2024
f36ba16
rev
themacexpert Aug 3, 2024
a3d0918
add
themacexpert Aug 3, 2024
6d0ff40
grammarly etc
themacexpert Aug 3, 2024
4eded26
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 7, 2024
08fd5ef
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 7, 2024
b2afa2a
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 7, 2024
b9dc615
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 7, 2024
5747706
rev
themacexpert Aug 7, 2024
cc611f2
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 7, 2024
2673e3c
rev
themacexpert Aug 7, 2024
8529911
Merge branch 'themacexpert/queries' of https://github.com/wormhole-fo…
themacexpert Aug 7, 2024
71cbec3
rev
themacexpert Aug 7, 2024
2491da9
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 7, 2024
109ed82
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 7, 2024
e8e95d5
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 7, 2024
720538a
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 7, 2024
bffd45f
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 7, 2024
f332d78
rev
themacexpert Aug 7, 2024
5f4aa90
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 7, 2024
bd5f19c
remove we reference
themacexpert Aug 7, 2024
f7bf782
rev'
themacexpert Aug 7, 2024
1850e82
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 7, 2024
82f3a13
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 7, 2024
824cb02
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 7, 2024
bbdeec7
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 7, 2024
4b88c12
center content and headings in table
themacexpert Aug 7, 2024
3a4f532
Merge branch 'themacexpert/queries' of https://github.com/wormhole-fo…
themacexpert Aug 7, 2024
0588565
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 7, 2024
7074016
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 7, 2024
ee8fb32
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 7, 2024
eba7581
rev
themacexpert Aug 7, 2024
334540c
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 7, 2024
628f42f
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 7, 2024
1af622a
rev
themacexpert Aug 7, 2024
69ddd33
Merge branch 'themacexpert/queries' of https://github.com/wormhole-fo…
themacexpert Aug 7, 2024
4dda36e
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 7, 2024
d333961
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 7, 2024
ce1025c
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 7, 2024
a0ee34d
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 7, 2024
be37cb4
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 7, 2024
c1f71af
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 7, 2024
023a56e
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 7, 2024
1f8284c
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 7, 2024
bc6937a
rev
themacexpert Aug 7, 2024
44f50b2
Merge branch 'themacexpert/queries' of https://github.com/wormhole-fo…
themacexpert Aug 7, 2024
988a3ed
rev
themacexpert Aug 7, 2024
7c92f08
rev
themacexpert Aug 7, 2024
0e9c886
rev
themacexpert Aug 7, 2024
94165db
rev
themacexpert Aug 7, 2024
6054565
rev
themacexpert Aug 7, 2024
a931a97
rev
themacexpert Aug 7, 2024
1ccacf8
rev
themacexpert Aug 7, 2024
a56a18f
rev
themacexpert Aug 7, 2024
5cd7256
rev
themacexpert Aug 8, 2024
d2dc95c
rev
themacexpert Aug 8, 2024
a62937f
rev
themacexpert Aug 8, 2024
ba4b82b
rev
themacexpert Aug 8, 2024
3afb40b
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 8, 2024
0bc8c1b
prettier
themacexpert Aug 8, 2024
ecd2327
prettier
themacexpert Aug 8, 2024
407f9b7
rev
themacexpert Aug 8, 2024
023bbb5
rev
themacexpert Aug 8, 2024
2b92e2e
revise
themacexpert Aug 14, 2024
998578f
revise
themacexpert Aug 14, 2024
d0d90fd
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 14, 2024
a3ec460
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 14, 2024
f4dc588
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 14, 2024
2d41730
Merge branch 'themacexpert/queries' of https://github.com/wormhole-fo…
themacexpert Aug 14, 2024
387d6e0
Update build/build-multichain-applications/queries/getting-started.md
themacexpert Aug 14, 2024
69ecbc3
revise
themacexpert Aug 14, 2024
31d26a1
Merge branch 'themacexpert/queries' of https://github.com/wormhole-fo…
themacexpert Aug 14, 2024
34d956f
revise
themacexpert Aug 14, 2024
5d78261
revise
themacexpert Aug 14, 2024
fcec103
revise
themacexpert Aug 14, 2024
29a2049
revise
themacexpert Aug 14, 2024
ddc1720
revise
themacexpert Aug 14, 2024
de2c163
revise
themacexpert Aug 14, 2024
595c83d
revise
themacexpert Aug 14, 2024
d39116d
revise
themacexpert Aug 14, 2024
932ecfa
revise
themacexpert Aug 14, 2024
762d0a5
revise
themacexpert Aug 14, 2024
c068035
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 14, 2024
c4ada7c
revise
themacexpert Aug 14, 2024
a01a959
merge
themacexpert Aug 14, 2024
63aea57
merge
themacexpert Aug 14, 2024
5488f7d
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 14, 2024
e6d41c3
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 14, 2024
16fd227
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 14, 2024
6980ee8
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 14, 2024
62253b5
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 14, 2024
3e73d7a
revise
themacexpert Aug 14, 2024
265cf8f
revise
themacexpert Aug 14, 2024
060fbe4
revise
themacexpert Aug 14, 2024
1b3e8fa
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 19, 2024
f015dc7
Merge branch 'themacexpert/queries' of https://github.com/wormhole-fo…
themacexpert Aug 19, 2024
a560e50
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 19, 2024
24ed9d1
Update build/build-multichain-applications/queries/overview.md
themacexpert Aug 19, 2024
b4445f7
rev
themacexpert Aug 19, 2024
b3bce53
Merge branch 'themacexpert/queries' of https://github.com/wormhole-fo…
themacexpert Aug 19, 2024
772eec0
Update build/build-multichain-applications/queries/hands-on-with-quer…
themacexpert Aug 19, 2024
09ad951
Update build/build-multichain-applications/queries/hands-on-with-quer…
themacexpert Aug 19, 2024
a93cd6a
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 19, 2024
86804f4
Update build/build-multichain-applications/queries/faqs.md
themacexpert Aug 19, 2024
b3dd5d4
Update build/build-multichain-applications/queries/hands-on-with-quer…
themacexpert Aug 19, 2024
541a8bd
rev
themacexpert Aug 19, 2024
bed26f6
rev
themacexpert Aug 19, 2024
036f7bb
Update .snippets/code/build/build-multichain-applications/queries/han…
themacexpert Aug 19, 2024
c1f15d2
rev
themacexpert Aug 19, 2024
e4f3929
rev
themacexpert Aug 19, 2024
70ce296
Merge branch 'themacexpert/queries' of https://github.com/wormhole-fo…
themacexpert Aug 19, 2024
72f199a
rev
themacexpert Aug 19, 2024
3ecad49
rev
themacexpert Aug 19, 2024
996a513
rev
themacexpert Aug 23, 2024
86a9bda
rev
themacexpert Aug 23, 2024
b9520e6
rev
themacexpert Aug 23, 2024
4e615e8
relocate and rename everything to build apps
themacexpert Aug 23, 2024
acde59b
change from paragraph to bullet format
themacexpert Aug 23, 2024
acc68ab
changes surfaced in Vale screen
dawnkelly09 Aug 23, 2024
7e5fc72
Vale screen and some additional edits
dawnkelly09 Aug 23, 2024
438598d
rev
themacexpert Aug 23, 2024
18dfd1d
Merge branch 'main' into themacexpert/queries
themacexpert Aug 23, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl https://ethereum.publicnode.com -X POST --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"to":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","data":"0x18160ddd"},"latest"],"id":1}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jsonrpc":"2.0",
"id":1,
"result":"0x000000000000000000000000000000000000000000029fd3d129b582d7949e71"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// contracts/query/QueryDemo.sol
// SPDX-License-Identifier: Apache 2

pragma solidity ^0.8.0;

import "wormhole-solidity-sdk/libraries/BytesParsing.sol";
import "wormhole-solidity-sdk/interfaces/IWormhole.sol";
import "wormhole-solidity-sdk/QueryResponse.sol";

error InvalidOwner();
// @dev for the onlyOwner modifier
error InvalidCaller();
error InvalidCalldata();
error InvalidForeignChainID();
error ObsoleteUpdate();
error StaleUpdate();
error UnexpectedResultLength();
error UnexpectedResultMismatch();

/// @dev QueryDemo is an example of using the QueryResponse library to parse and verify Cross Chain Query (CCQ) responses.
contract QueryDemo is QueryResponse {
using BytesParsing for bytes;

struct ChainEntry {
uint16 chainID;
address contractAddress;
uint256 counter;
uint256 blockNum;
uint256 blockTime;
}

address private immutable owner;
uint16 private immutable myChainID;
mapping(uint16 => ChainEntry) private counters;
uint16[] private foreignChainIDs;

bytes4 public GetMyCounter = bytes4(hex"916d5743");

constructor(address _owner, address _wormhole, uint16 _myChainID) QueryResponse(_wormhole) {
if (_owner == address(0)) {
revert InvalidOwner();
}
owner = _owner;

myChainID = _myChainID;
counters[_myChainID] = ChainEntry(_myChainID, address(this), 0, 0, 0);
}

// updateRegistration should be used to add the other chains and to set / update contract addresses.
function updateRegistration(uint16 _chainID, address _contractAddress) public onlyOwner {
if (counters[_chainID].chainID == 0) {
foreignChainIDs.push(_chainID);
counters[_chainID].chainID = _chainID;
}

counters[_chainID].contractAddress = _contractAddress;
}

// getMyCounter (call signature 916d5743) returns the counter value for this chain. It is meant to be used in a cross chain query.
function getMyCounter() public view returns (uint256) {
return counters[myChainID].counter;
}

// getState() returns this chain's view of all the counters. It is meant to be used in the front end.
function getState() public view returns (ChainEntry[] memory) {
ChainEntry[] memory ret = new ChainEntry[](foreignChainIDs.length + 1);
ret[0] = counters[myChainID];
uint256 length = foreignChainIDs.length;

for (uint256 i = 0; i < length;) {
ret[i + 1] = counters[foreignChainIDs[i]];
unchecked {
++i;
}
}

return ret;
}

// @notice Takes the cross chain query response for the other counters, stores the results for the other chains, and updates the counter for this chain.
function updateCounters(bytes memory response, IWormhole.Signature[] memory signatures) public {
ParsedQueryResponse memory r = parseAndVerifyQueryResponse(response, signatures);
uint256 numResponses = r.responses.length;
if (numResponses != foreignChainIDs.length) {
revert UnexpectedResultLength();
}

for (uint256 i = 0; i < numResponses;) {
// Create a storage pointer for frequently read and updated data stored on the blockchain
ChainEntry storage chainEntry = counters[r.responses[i].chainId];
if (chainEntry.chainID != foreignChainIDs[i]) {
revert InvalidForeignChainID();
}

EthCallQueryResponse memory eqr = parseEthCallQueryResponse(r.responses[i]);

// Validate that update is not obsolete
validateBlockNum(eqr.blockNum, chainEntry.blockNum);

// Validate that update is not stale
validateBlockTime(eqr.blockTime, block.timestamp - 300);

if (eqr.result.length != 1) {
revert UnexpectedResultMismatch();
}

// Validate addresses and function signatures
address[] memory validAddresses = new address[](1);
bytes4[] memory validFunctionSignatures = new bytes4[](1);
validAddresses[0] = chainEntry.contractAddress;
validFunctionSignatures[0] = GetMyCounter;

validateMultipleEthCallData(eqr.result, validAddresses, validFunctionSignatures);

require(eqr.result[0].result.length == 32, "result is not a uint256");

chainEntry.blockNum = eqr.blockNum;
chainEntry.blockTime = eqr.blockTime / 1_000_000;
chainEntry.counter = abi.decode(eqr.result[0].result, (uint256));

unchecked {
++i;
}
}

counters[myChainID].blockNum = block.number;
counters[myChainID].blockTime = block.timestamp;
counters[myChainID].counter += 1;
}

modifier onlyOwner() {
if (owner != msg.sender) {
revert InvalidOwner();
}
_;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const tx = await contract.updateCounters(
`0x${response.data.bytes}`,
signaturesToEvmStruct(response.data.signatures)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const serialized = request.serialize();
const proxyResponse = (await axios.post)<QueryProxyQueryResponse>(
QUERY_URL,
{
bytes: Buffer.from(serialized).toString('hex'),
},
{ headers: { 'X-API-Key': YOUR_API_KEY } }
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
EthCallData,
EthCallQueryRequest,
EthCallQueryResponse,
PerChainQueryRequest,
QueryProxyMock,
QueryRequest,
QueryResponse,
} from '@wormhole-foundation/wormhole-query-sdk';
import axios from 'axios';

const rpc = 'https://ethereum.publicnode.com';
const callData: EthCallData = {
to: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', // WETH
data: '0x18160ddd', // web3.eth.abi.encodeFunctionSignature("totalSupply()")
};

(async () => {
const latestBlock: string = (
await axios.post(rpc, {
method: 'eth_getBlockByNumber',
params: ['latest', false],
id: 1,
jsonrpc: '2.0',
})
).data?.result?.number;
if (!latestBlock) {
console.error(`❌ Invalid block returned`);
return;
}
console.log('Latest Block: ', latestBlock, `(${BigInt(latestBlock)})`);
const targetResponse = await axios.post(rpc, {
method: 'eth_call',
params: [callData, latestBlock],
id: 1,
jsonrpc: '2.0',
});
// console.log(finalizedResponse.data);
if (targetResponse.data.error) {
console.error(`❌ ${targetResponse.data.error.message}`);
}
const targetResult = targetResponse.data?.result;
console.log('Target Result: ', targetResult, `(${BigInt(targetResult)})`);
// Form the query request
const request = new QueryRequest(
0, // Nonce
[
new PerChainQueryRequest(
2, // Ethereum Wormhole Chain ID
new EthCallQueryRequest(latestBlock, [callData])
),
]
);
console.log(JSON.stringify(request, undefined, 2));
const mock = new QueryProxyMock({ 2: rpc });
const mockData = await mock.mock(request);
console.log(mockData);
const mockQueryResponse = QueryResponse.from(mockData.bytes);
const mockQueryResult = (
mockQueryResponse.responses[0].response as EthCallQueryResponse
).results[0];
console.log(
`Mock Query Result: ${mockQueryResult} (${BigInt(mockQueryResult)})`
);
})();

This file was deleted.

This file was deleted.

Loading
Loading