Skip to content

Commit

Permalink
Update test consumer and scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiimk committed Jun 25, 2024
1 parent 220c452 commit 2094985
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down Expand Up @@ -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)'"
}
}
14 changes: 14 additions & 0 deletions script/DeployTestConsumer.s.sol
Original file line number Diff line number Diff line change
@@ -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));
}
}
16 changes: 15 additions & 1 deletion src/TestConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ 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;
using CborReader for CborReader.CBOR;

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");
Expand All @@ -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");
Expand Down

0 comments on commit 2094985

Please sign in to comment.