From 20949856838489b375854868b0cf445eced5b55d Mon Sep 17 00:00:00 2001 From: Sergii Mikhtoniuk Date: Tue, 25 Jun 2024 16:28:51 -0700 Subject: [PATCH] Update test consumer and scripts --- package.json | 20 ++++++++++++-------- script/DeployTestConsumer.s.sol | 14 ++++++++++++++ src/TestConsumer.sol | 16 +++++++++++++++- 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 script/DeployTestConsumer.s.sol diff --git a/package.json b/package.json index 864086a..0482164 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@opendatafabric/contracts", "description": "Set of Solidity smart contracts for interacting with Open Data Fabric network", - "version": "0.1.5", + "version": "0.1.6", "author": { "name": "Open Data Fabric", "url": "https://github.com/open-data-fabric/" @@ -41,12 +41,16 @@ "test": "forge test", "test:coverage": "forge coverage", "test:coverage:report": "forge coverage --report lcov && genhtml lcov.info --branch-coverage --output-dir coverage", - "deploy:local": "export $(cat .env.local | xargs) && forge script script/Deploy.s.sol --fork-url http://localhost:8545 --private-key $PRIVATE_KEY --broadcast", - "deploy:sepolia": "export $(cat .env.test.sepolia | xargs) && forge script script/Deploy.s.sol -vvvv --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --verify", - "send:add-provider:local": "export $(cat .env.local | xargs) && cast send --rpc-url $RPC_URL --private-key $PRIVATE_KEY $ORACLE_CONTRACT_ADDR 'addProvider(address)' $PROVIDER_ADDR", - "send:add-provider:sepolia": "export $(cat .env.test.sepolia | xargs) && cast send --rpc-url $RPC_URL --private-key $PRIVATE_KEY $ORACLE_CONTRACT_ADDR 'addProvider(address)' $PROVIDER_ADDR", - "send:oracle:local": "export $(cat .env.local | xargs) && cast send --rpc-url $RPC_URL --private-key $PRIVATE_KEY $CONSUMER_CONTRACT_ADDR 'initiateQuery()'", - "send:oracle:sepolia": "export $(cat .env.test.sepolia | xargs) && cast send --rpc-url $RPC_URL --private-key $PRIVATE_KEY $CONSUMER_CONTRACT_ADDR 'initiateQuery()'", - "call:consumer-state:local": "export $(cat .env.local | xargs) && cast call --rpc-url $RPC_URL $CONSUMER_CONTRACT_ADDR 'province() returns(string)' && cast call --rpc-url $RPC_URL $CONSUMER_CONTRACT_ADDR 'totalCases() returns(uint64)'" + "deploy:local": "source ./.env.local && forge script script/Deploy.s.sol --fork-url http://localhost:8545 --private-key $PRIVATE_KEY --broadcast", + "deploy:sepolia": "source ./.env.test.sepolia && forge script script/Deploy.s.sol -vvvv --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --verify", + "deploy:consumer:local": "source ./.env.local && forge script script/DeployTestConsumer.s.sol --fork-url http://localhost:8545 --private-key $PRIVATE_KEY --broadcast", + "deploy:consumer:sepolia": "source ./.env.test.sepolia && forge script script/DeployTestConsumer.s.sol -vvvv --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --verify", + "send:add-provider:local": "source ./.env.local && cast send --rpc-url $RPC_URL --private-key $PRIVATE_KEY $ORACLE_CONTRACT_ADDR 'addProvider(address)' $PROVIDER_ADDR", + "send:add-provider:sepolia": "source ./.env.test.sepolia && cast send --rpc-url $RPC_URL --private-key $PRIVATE_KEY $ORACLE_CONTRACT_ADDR 'addProvider(address)' $PROVIDER_ADDR", + "send:initiate:local": "source ./.env.local && cast send --rpc-url $RPC_URL --private-key $PRIVATE_KEY $CONSUMER_CONTRACT_ADDR 'initiateQuery()'", + "send:initiate:sepolia": "source ./.env.test.sepolia && cast send --rpc-url $RPC_URL --private-key $PRIVATE_KEY $CONSUMER_CONTRACT_ADDR 'initiateQuery()'", + "send:initiate-generic:sepolia": "source ./.env.test.sepolia && cast send --rpc-url $RPC_URL --private-key $PRIVATE_KEY $CONSUMER_CONTRACT_ADDR 'initiateQueryGeneric(string sql, string alias, string id)' \"$QUERY_GENERIC_SQL\" \"$QUERY_GENERIC_ALIAS\" \"$QUERY_GENERIC_ID\"", + "call:consumer-state:local": "source ./.env.local && cast call --rpc-url $RPC_URL $CONSUMER_CONTRACT_ADDR 'province() returns(string)' && cast call --rpc-url $RPC_URL $CONSUMER_CONTRACT_ADDR 'totalCases() returns(uint64)'", + "call:consumer-state:sepolia": "source ./.env.test.sepolia && cast call --rpc-url $RPC_URL $CONSUMER_CONTRACT_ADDR 'province() returns(string)' && cast call --rpc-url $RPC_URL $CONSUMER_CONTRACT_ADDR 'totalCases() returns(uint64)'" } } diff --git a/script/DeployTestConsumer.s.sol b/script/DeployTestConsumer.s.sol new file mode 100644 index 0000000..12eda0d --- /dev/null +++ b/script/DeployTestConsumer.s.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.25; + +import { OdfOracle } from "../src/OdfOracle.sol"; +import { TestConsumer } from "../src/TestConsumer.sol"; + +import { BaseScript } from "./Base.s.sol"; + +contract DeployTestConsumer is BaseScript { + function run() public broadcast { + OdfOracle oracle = OdfOracle(vm.envAddress("ORACLE_CONTRACT_ADDR")); + new TestConsumer(address(oracle)); + } +} diff --git a/src/TestConsumer.sol b/src/TestConsumer.sol index ba0cc9c..492f3ee 100644 --- a/src/TestConsumer.sol +++ b/src/TestConsumer.sol @@ -3,6 +3,8 @@ pragma solidity ^0.8.25; import { OdfRequest, OdfResponse, IOdfClient, CborReader } from "./Odf.sol"; +// This sample contract makes an ODF oracle query to calculate +// a Canadian province with the most recorded COVID-19 cases contract TestConsumer { using OdfRequest for OdfRequest.Req; using OdfResponse for OdfResponse.Res; @@ -10,10 +12,12 @@ contract TestConsumer { IOdfClient private oracle; + // Initialize contract with the oracle address constructor(address oracleAddr) { oracle = IOdfClient(oracleAddr); } + // Important: Make sure only the ODF oracle can supply you data modifier onlyOracle() { // solhint-disable-next-line custom-errors require(msg.sender == address(oracle), "Can only be called by oracle"); @@ -23,22 +27,32 @@ contract TestConsumer { // Specific query function initiateQuery() public { OdfRequest.Req memory req = OdfRequest.init(); + + // Specify ID of the dataset(s) we will be querying. + // Repeat this call for multiple inputs. req.dataset( "kamu/covid19.canada.case-details", - "did:odf:fed014895afeb476d5d94c1af0668f30ab661c8561760bba6744e43225ba52e099595" + "did:odf:fed01c8788dc7825dc95dfaa6c67f989b758d3ebcb1efcb9f47ea914470bd1f7f2bbb" ); + + // Specify an arbitrary complex SQL query. + // Queries can include even JOINs req.sql( "with by_provice as (" "select province, count(*) as count from 'kamu/covid19.canada.case-details' group by 1)," "ranked as (select row_number() over (order by count desc) as rank, province, count from by_provice)" "select province, count from ranked where rank = 1" ); + + // Send request to the oracle and specify a callback oracle.sendRequest(req, this.onResult); } string public province; uint64 public totalCases; + // This function will be called by the oracle when + // request is fulfilled by some data provider function onResult(OdfResponse.Res memory result) external onlyOracle { // solhint-disable-next-line custom-errors require(result.numRecords() == 1, "Expected one record");