diff --git a/package.json b/package.json index 9e20253..6af1c10 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "witnet-solidity", - "version": "2.0.3", + "version": "2.1.0-beta.0", "description": "Witnet CLI import module for Solidity projects", "author": "Witnet Foundation", "license": "MIT", "repository": { "type": "git", - "url": "https://github.com/witnet/witnet-solidity/tree/2.0" + "url": "https://github.com/witnet/witnet-solidity/" }, "private": false, "type": "commonjs", @@ -46,8 +46,8 @@ "dotenv": "^16.4.1", "inquirer": "^8.0.0", "truffle": "^5.11.5", - "witnet-solidity-bridge": "../witnet-solidity-bridge", - "witnet-toolkit": "../witnet-toolkit" + "witnet-solidity-bridge": "2.1.0-beta.0", + "witnet-toolkit": "^2.0.9" }, "devDependencies": { "cbor": "^9.0.2", diff --git a/src/bin/witnet.js b/src/bin/witnet.js index 19ed487..d220e3a 100644 --- a/src/bin/witnet.js +++ b/src/bin/witnet.js @@ -64,7 +64,7 @@ function showMainUsage () { console.info(" ", "deploy", "\t\t=>", "Deploy Witnet requests and templates defined within the witnet/assets/ folder.") console.info(" ", "ethrpc", "\t\t=>", "Run a local ETH/RPC gateway to the specified chain.") console.info(" ", "test", "\t\t=>", "Dry-run requests and templates defined within the witnet/assets/ folder.") - console.info(" ", "wizard", "\t\t=>", "Helps you to integrate the Witnet Oracle within your smart contracts.") + console.info(" ", "wizard", "\t\t=>", "Helps you to integrate the Wit/Oracle blockchain within your smart contracts.") } function showAvailUsage () { @@ -480,7 +480,7 @@ async function wizard () { ...await inquirer.prompt({ type: "list", name: "callbacks", - message: "How would you like your contract to read data from the Witnet Oracle?", + message: "How would you like your contract to read data from the Wit/Oracle blockchain?", choices: [ "Asynchronously => my contract will eventually read the result from Witnet, when available.", "Synchronously => my contract is to be called as soon as data is reported from Witnet.", @@ -539,11 +539,11 @@ async function wizard () { ...answers, } const artifact = (appKind === "Price" - ? "WitnetPriceFeeds" + ? "WitPriceFeeds" : answers?.randomness?.split(" ")[0] === "Yes" ? "WitnetRandomnessV2" : ( - "WitnetOracle" + "WitOracle" ) ) const findings = [] @@ -564,7 +564,7 @@ async function wizard () { throw Error(`Sorry, required ${artifact} contract is not available on selected ecosystems: ${answers?.ecosystems}`) } } else { - importWitnetMocks = "\nimport \"witnet-solidity-bridge/contracts/mocks/WitnetMockedOracle.sol\";" + importWitnetMocks = "\nimport \"witnet-solidity-bridge/contracts/mocks/WitMockedOracle.sol\";" } } @@ -604,10 +604,10 @@ async function wizard () { templateFile += "_UsingRandomness.tsol" } else { if (!answers?.witnetAddress) { - constructorParams = "WitnetOracle _witnetOracle" - witnetAddress = "_witnetOracle" + constructorParams = "WitOracle _witOracle" + witnetAddress = "_witOracle" } else { - witnetAddress = `WitnetOracle(${answers.witnetAddress})` + witnetAddress = `WitOracle(${answers.witnetAddress})` } templateFile += "_RandomnessRequestConsumer.tsol" } @@ -615,10 +615,10 @@ async function wizard () { } case "Price": { if (!answers?.witnetAddress) { - constructorParams = "WitnetPriceFeeds _witnetPriceFeeds" + constructorParams = "WitPriceFeeds _witnetPriceFeeds" witnetAddress = "_witnetPriceFeeds" } else { - witnetAddress = `WitnetPriceFeeds(${answers.witnetAddress})` + witnetAddress = `WitPriceFeeds(${answers.witnetAddress})` } templateFile += "_UsingPriceFeeds.tsol" break @@ -626,24 +626,24 @@ async function wizard () { case "Public": { if (answers?.dynamic.split(" ")[0] === "Yes") { if (answers.parameterized.split(" ")[0] === "Yes") { - constructorParams = "WitnetRequestTemplate _witnetRequestTemplate" - witnetAddress = "_witnetRequestTemplate" + constructorParams = "WitOracleRequestTemplate _witOracleRequestTemplate" + witnetAddress = "_witOracleRequestTemplate" templateFile += (answers?.callbacks.split(" ")[0] === "Synchronously" ? "_RequestTemplateConsumer.tsol" : "_UsingRequestTemplate.tsol" ) } else { if (!answers?.witnetAddress) { - constructorParams = "WitnetOracle _witnetOracle" - witnetAddress = "_witnetOracle" + constructorParams = "WitOracle _witOracle" + witnetAddress = "_witOracle" } else { - witnetAddress = `WitnetOracle(${answers.witnetAddress})` + witnetAddress = `WitOracle(${answers.witnetAddress})` } templateFile += "_Consumer.tsol" } } else { - constructorParams = "WitnetRequest _witnetRequest" - witnetAddress = "_witnetRequest" + constructorParams = "WitOracleRequest _witOracleRequest" + witnetAddress = "_witOracleRequest" templateFile += (answers?.callbacks.split(" ")[0] === "Synchronously" ? "_RequestConsumer.tsol" : "_UsingRequest.tsol" @@ -757,17 +757,17 @@ function _traceWitnetArtifact (artifact) { if (specs.retrieve[0]?.argsCount) { if (!args || args.length === 0) { console.info(" ", `[1] RETRIEVE:\t\x1b[1;32m${ - utils.getWitnetRequestMethodString(specs.retrieve[0].method) + utils.getWitOracleRequestMethodString(specs.retrieve[0].method) }(\x1b[0;32m<${ specs.retrieve[0].argsCount } args>\x1b[1;32m)\x1b[0m` ) } else { // eslint-disable-next-line - console.info(" ", `[1] RETRIEVE:\t\x1b[1;32m${utils.getWitnetRequestMethodString(specs.retrieve[0].method)}(\x1b[0;32m${JSON.stringify(args[0])}\x1b[1;32m\x1b[0m`) + console.info(" ", `[1] RETRIEVE:\t\x1b[1;32m${utils.getWitOracleRequestMethodString(specs.retrieve[0].method)}(\x1b[0;32m${JSON.stringify(args[0])}\x1b[1;32m\x1b[0m`) } } else { - console.info(" ", `[1] RETRIEVE:\t\x1b[1;32m${utils.getWitnetRequestMethodString(specs.retrieve[0].method)}()\x1b[0m`) + console.info(" ", `[1] RETRIEVE:\t\x1b[1;32m${utils.getWitOracleRequestMethodString(specs.retrieve[0].method)}()\x1b[0m`) } } else { console.info(" ", "[1] RETRIEVE:") @@ -777,14 +777,14 @@ function _traceWitnetArtifact (artifact) { if (value?.argsCount) { if (!args[index] || args[index].length === 0) { // eslint-disable-next-line - console.info(" ", ` [#${index}]\t\t\x1b[1;32m${utils.getWitnetRequestMethodString(value.method)}(\x1b[0;32m<${value.argsCount} args>\x1b[1;32m)\x1b[0m`) + console.info(" ", ` [#${index}]\t\t\x1b[1;32m${utils.getWitOracleRequestMethodString(value.method)}(\x1b[0;32m<${value.argsCount} args>\x1b[1;32m)\x1b[0m`) } else { // eslint-disable-next-line - console.info(" ", ` [#${index}]\t\t\x1b[1;32m${utils.getWitnetRequestMethodString(value.method)}(\x1b[0;32m${JSON.stringify(args[index])}\x1b[1;32m)\x1b[0m`) + console.info(" ", ` [#${index}]\t\t\x1b[1;32m${utils.getWitOracleRequestMethodString(value.method)}(\x1b[0;32m${JSON.stringify(args[index])}\x1b[1;32m)\x1b[0m`) } } else { // eslint-disable-next-line - console.info(" ", ` [#${index}]\t\t\x1b[1;32m${utils.getWitnetRequestMethodString(value.method)}()\x1b[0m`) + console.info(" ", ` [#${index}]\t\t\x1b[1;32m${utils.getWitOracleRequestMethodString(value.method)}()\x1b[0m`) } } _traceWitnetArtifactSource(value) @@ -862,9 +862,9 @@ function _traceWitnetSource (value) { if (value?.method) { if (value?.argsCount) { // eslint-disable-next-line - console.info(" ", `> Method: \x1b[1;32m${utils.getWitnetRequestMethodString(value.method)}(\x1b[0;32m<${value.argsCount} args>\x1b[1;32m)\x1b[0m`) + console.info(" ", `> Method: \x1b[1;32m${utils.getWitOracleRequestMethodString(value.method)}(\x1b[0;32m<${value.argsCount} args>\x1b[1;32m)\x1b[0m`) } else { - console.info(" ", `> Method: \x1b[1;32m${utils.getWitnetRequestMethodString(value.method)}()\x1b[0m`) + console.info(" ", `> Method: \x1b[1;32m${utils.getWitOracleRequestMethodString(value.method)}()\x1b[0m`) } if (value?.url) { console.info(" ", `> URL: \x1b[32m${value.url}\x1b[0m`) diff --git a/witnet/assets/_requests.js b/witnet/assets/_requests.js index cf8a9a5..9e124bb 100644 --- a/witnet/assets/_requests.js +++ b/witnet/assets/_requests.js @@ -14,18 +14,18 @@ const templates = Witnet.Dictionary( module.exports = { /// //// STATIC REQUESTS ///////////////////////////////////////////////////////// // path: { ... path: { - // WitnetRequestXXX: Witnet.StaticRequest({ + // WitOracleRequestXXX: Witnet.StaticRequest({ // retrieve: [ Witnet.Sources.., ..., sources['source-name-x'], ... ], // aggregate?: Witnet.Reducers.., // tally?: Witnet.Reducers.., // }), /// /// REQUESTS FROM TEMPLATE /////////////////////////////////////////////////// - // WitnetRequestYYY: Witnet.RequestFromTemplate( - // templates['WitnetRequestTemplateUniqueNameX'], + // WitOracleRequestYYY: Witnet.RequestFromTemplate( + // templates['WitOracleRequestTemplateUniqueNameX'], // [ [ .. ], .. ], // args: string[][] // ), /// /// REQUESTS FROM RETRIEVALS DICTIONARY ////////////////////////////////////// - // WitnetRequestZZZ: Witnet.RequestFromDictionary({ + // WitOracleRequestZZZ: Witnet.RequestFromDictionary({ // retrieve: { // dict: sources, // tags: { diff --git a/witnet/assets/_templates.js b/witnet/assets/_templates.js index a04d3de..1f4463c 100644 --- a/witnet/assets/_templates.js +++ b/witnet/assets/_templates.js @@ -10,7 +10,7 @@ const sources = Witnet.Dictionary( module.exports = { /// //// REQUEST TEMPLATES /////////////////////////////////////////////////////// // path: { ... path: { - // WitnetRequestTemplateXXX: Witnet.RequestTemplate({ + // WitOracleRequestTemplateXXX: Witnet.RequestTemplate({ // retrieve: [ sources['source-name-x'], ... ], // aggregate?: Witnet.Reducers.., // tally?: Witnet.Reducers.., diff --git a/witnet/assets/requests/index.js b/witnet/assets/requests/index.js index 8a5363f..c668a32 100644 --- a/witnet/assets/requests/index.js +++ b/witnet/assets/requests/index.js @@ -2,7 +2,7 @@ const Witnet = require("witnet-toolkit") module.exports = { legacy: { - WitnetRequestPriceUsdtWit9: Witnet.StaticRequest({ + WitOracleRequestPriceUsdtWit9: Witnet.StaticRequest({ retrieve: [ Witnet.Sources.HttpGet({ url: "https://api-cloud.bitmart.com/spot/v1/ticker?symbol=WIT_USDT", @@ -40,7 +40,7 @@ module.exports = { aggregate: Witnet.Reducers.Median(Witnet.Filters.Stdev(1.4)), tally: Witnet.Reducers.PriceTally(), }), - WitnetRequestRandomness: Witnet.StaticRequest({ + WitOracleRequestRandomness: Witnet.StaticRequest({ retrieve: [Witnet.Sources.RNG()], tally: Witnet.Reducers.ConcatHash(), }), diff --git a/witnet/assets/templates/ccdr.js b/witnet/assets/templates/ccdr.js index 4f1f412..cd9e200 100644 --- a/witnet/assets/templates/ccdr.js +++ b/witnet/assets/templates/ccdr.js @@ -6,46 +6,45 @@ const sources = Witnet.Dictionary( ) module.exports = { - WitnetRequestTemplateEthBlockNumber: Witnet.RequestTemplate({ + WitOracleRequestTemplateEthBlockNumber: Witnet.RequestTemplate({ retrieve: sources["eth-block-number"], tests: { "get block number from sepolia works": [ - "https://provider-url", + "https://worldchain-mainnet.g.alchemy.com/public", ], }, }), - WitnetRequestTemplateEthGasPrice: Witnet.RequestTemplate({ + WitOracleRequestTemplateEthGasPrice: Witnet.RequestTemplate({ retrieve: sources["eth-gas-price"], tests: { "get balance from sepolia works": [ - "https://provider-url", + "https://worldchain-mainnet.g.alchemy.com/public", ], }, }), - WitnetRequestTemplateEthTransactionByHash: Witnet.RequestTemplate({ + WitOracleRequestTemplateEthTransactionByHash: Witnet.RequestTemplate({ retrieve: sources["eth-get-transaction-by-hash"], tests: { "get transaction by hash from sepolia works": [ - "https://provider-url", - "transaction-hash", + "https://worldchain-mainnet.g.alchemy.com/public", + "0xee0a762f4d76f824cd5e7d10fe86ff6c57e8bebf1aab6047be99ac27fe38bfd9", ], }, }), - WitnetRequestTemplateWitSupplyInfo: Witnet.RequestTemplate({ + WitOracleRequestTemplateWitSupplyInfo: Witnet.RequestTemplate({ retrieve: sources["wit-supply-info"], tests: { - "get Witnet supply info": [ - "https://provider-url", - "transaction-hash", + "get witnet supply info": [ + "http://3.133.4.38:21339", ], }, }), - WitnetRequestTemplateWitTransactionByHash: Witnet.RequestTemplate({ + WitOracleRequestTemplateWitTransactionByHash: Witnet.RequestTemplate({ retrieve: sources["wit-get-transaction-by-hash"], tests: { "get transaction by hash from witnet works": [ - "https://provider-url", - "transaction-hash", + "http://3.133.4.38:21339", + "c40ecb835212dfe64feeebb5493fc49066d5207f28372672b219ca43fda08e7d", ], }, }), diff --git a/witnet/contracts/Witnet.sol b/witnet/contracts/Witnet.sol index 6cc2de2..0647f0e 100644 --- a/witnet/contracts/Witnet.sol +++ b/witnet/contracts/Witnet.sol @@ -2,9 +2,9 @@ pragma solidity ^0.8.0; // Witnet Requests deployment dependencies: -import "witnet-solidity-bridge/contracts/apps/UsingWitnet.sol"; -import "witnet-solidity-bridge/contracts/WitnetPriceFeeds.sol"; +import "witnet-solidity-bridge/contracts/mockups/UsingWitOracle.sol"; +import "witnet-solidity-bridge/contracts/WitPriceFeeds.sol"; // Witnet mocking contracts (only for local/unitary testing): -import "witnet-solidity-bridge/contracts/mocks/WitnetMockedOracle.sol"; -import "witnet-solidity-bridge/contracts/mocks/WitnetMockedPriceFeeds.sol"; \ No newline at end of file +import "witnet-solidity-bridge/mockups/tests/WitMockedOracle.sol"; +import "witnet-solidity-bridge/mockups/tests/WitMockedPriceFeeds.sol"; diff --git a/witnet/contracts/_Consumer.tsol b/witnet/contracts/_Consumer.tsol index c0acbfd..25f61d5 100644 --- a/witnet/contracts/_Consumer.tsol +++ b/witnet/contracts/_Consumer.tsol @@ -1,105 +1,80 @@ // SPDX-License-Identifier: MIT -pragma solidity >=0.7.0 <0.9.0; -pragma experimental ABIEncoderV2; +pragma solidity >=0.8.0 <0.9.0; -import "witnet-solidity-bridge/contracts/apps/WitnetConsumer.sol";$_importWitnetMocks +import "witnet-solidity-bridge/contracts/mockups/WitOracleConsumer.sol";$_importWitnetMocks contract $_contractName is - WitnetConsumer + WitOracleConsumer { - using Witnet for Witnet.Result; + using Witnet for Witnet.DataResult; + using WitnetV2 for Witnet.QuerySLA; using WitnetCBOR for WitnetCBOR.CBOR; using WitnetCBOR for WitnetCBOR.CBOR[]; - using WitnetV2 for WitnetV2.RadonSLA; + constructor ($_constructorParams) - UsingWitnet($_witnetAddress) - WitnetConsumer(/* _callbackGasLimit */ uint24($_callbackGasLimit)) + UsingWitOracle($_witOracleAddress) + WitOracleConsumer(/* _callbackGasLimit */ uint24($_callbackGasLimit)) { - __witnetBaseFeeOverheadPercentage = uint16($_baseFeeOverhead); + __witOracleBaseFeeOverheadPercentage = uint16($_baseFeeOverhead); // ... } - function __witnetRequestData( - uint256 _witnetEvmReward, - bytes calldata _witnetDataRequestBytecode + function __witOraclePostQuery( + uint256 _witOracleEvmReward, + bytes calldata _witOracleDataRequestBytecode ) virtual internal returns (uint256) { - return __witnetRequestData( - _witnetEvmReward, - _witnetDataRequestBytecode, - __witnetDefaultSLA + return __witOraclePostQuery( + _witOracleEvmReward, + _witOracleDataRequestBytecode, + __witOracleDefaultSLA ); } - function __witnetRequestData( - uint256 _witnetEvmReward, - bytes calldata _witnetDataRequestBytecode, - WitnetV2.RadonSLA memory _witnetQuerySLA + function __witOraclePostQuery( + uint256 _witOracleEvmReward, + bytes calldata _witOracleDataRequestBytecode, + Witnet.QuerySLA memory _witOracleQuerySLA ) virtual internal returns (uint256) { - return __witnet.postRequestWithCallback{value: _witnetEvmReward}( - _witnetDataRequestBytecode, - _witnetQuerySLA, - __witnetCallbackGasLimit + return __witOracle.postQuery{ + value: _witOracleEvmReward + }( + _witOracleDataRequestBytecode, + _witOracleQuerySLA, + Witnet.QueryCallback({ + consumer: address(this), + gasLimit: __witOracleCallbackGasLimit + }) ); } /// ... Introspect the `WitnetConsumer` and the `UsingWitnet` abstract contracts to learn /// ... about the internal helper methods at your disposal for pulling - /// ... data updates from the Witnet Oracle. + /// ... data updates from the Wit/Oracle blockchain. /// =============================================================================================================== - /// --- Implementing `IWitnetConsumer` interface ------------------------------------------------------------------ + /// --- Implementing `IWitOracleConsumer` interface --------------------------------------------------------------- - /// @notice Method to be called from the WitnetOracle contract as soon as the given `_witnetQueryId` - /// @notice gets reported, if solved with no errors. - /// @param _witnetQueryId The unique identifier of the Witnet query being reported. - /// @param _witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin. - /// @param _witnetResultTimestamp Timestamp at which the reported value was captured by the Witnet blockchain. - /// @param _witnetEvmFinalityBlock EVM block at which the provided data can be considered to be final. - /// @param _witnetResultCborValue The CBOR-encoded resulting value of the Witnet query being reported. - function reportWitnetQueryResult( - uint256 _witnetQueryId, - uint64 _witnetResultTimestamp, - bytes32 _witnetResultTallyHash, - uint256 _witnetEvmFinalityBlock, - WitnetCBOR.CBOR calldata _witnetResultCborValue + /// @notice Method to be called from the WitOracle contract as soon as the given Witnet `queryId` gets reported. + /// @dev It should revert if called from any other address different to `__witOracle`. + /// @param _queryId The unique identifier of the Witnet query being reported. + /// @param _queryResult Struct contaning a Witnet.DataResult metadata and value. + function reportWitOracleQueryResult( + Witnet.QueryId _queryId, + Witnet.DataResult memory _queryResult ) override external - onlyFromWitnet + onlyFromWitOracle { // TODO: this method MUST be implemented // TODO: gas consumed by this method SHOULD NEVER be greater than $_callbackGasLimit units. } - - /// @notice Method to be called from the WitnetOracle contract as soon as the given `_witnetQueryId` - /// @notice gets reported, if solved WITH errors. - /// @param _witnetQueryId The unique identifier of the Witnet query being reported. - /// @param _witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin. - /// @param _witnetResultTimestamp Timestamp at which the reported value was captured by the Witnet blockchain. - /// @param _witnetEvmFinalityBlock EVM block at which the provided data can be considered to be final. - /// @param _errorCode The error code enum identifying the error produced during resolution on the Witnet blockchain. - /// @param _errorArgs Error arguments, if any. An empty buffer is to be passed if no error arguments apply. - function reportWitnetQueryError( - uint256 _witnetQueryId, - uint64 _witnetResultTimestamp, - bytes32 _witnetResultTallyHash, - uint256 _witnetEvmFinalityBlock, - Witnet.ResultErrorCodes _errorCode, - WitnetCBOR.CBOR calldata _errorArgs - ) - override external - onlyFromWitnet - { - // TODO: this method MUST be implemented - // TODO: gas consumed by this method SHOULD NEVER be greater than $_callbackGasLimit gas units. - } - -} \ No newline at end of file +} diff --git a/witnet/contracts/_RandomnessRequestConsumer.tsol b/witnet/contracts/_RandomnessRequestConsumer.tsol index 214dae6..5ce043a 100644 --- a/witnet/contracts/_RandomnessRequestConsumer.tsol +++ b/witnet/contracts/_RandomnessRequestConsumer.tsol @@ -1,28 +1,27 @@ // SPDX-License-Identifier: MIT -pragma solidity >=0.7.0 <0.9.0; -pragma experimental ABIEncoderV2; +pragma solidity >=0.8.0 <0.9.0; -import "witnet-solidity-bridge/contracts/apps/WitnetRandomnessRequestConsumer.sol";$_importWitnetMocks +import "witnet-solidity-bridge/contracts/mockups/WitOracleRandomnessConsumer.sol";$_importWitnetMocks contract $_contractName is - WitnetRandomnessRequestConsumer + WitOracleRandomnessConsumer { + using Witnet for Witnet.QuerySLA; using WitnetCBOR for WitnetCBOR.CBOR; - using WitnetV2 for WitnetV2.RadonSLA; constructor ($_constructorParams) - WitnetRandomnessRequestConsumer( - $_witnetAddress, - /* _witnetBaseFeeOverheadPercentage */ uint16($_baseFeeOverhead), - /* _witnetCallbackGasLimit */ uint24($_callbackGasLimit) + WitOracleRandomnessConsumer( + $_witOracleAddress, + /* _witOracleBaseFeeOverheadPercentage */ uint16($_baseFeeOverhead), + /* _witOracleCallbackGasLimit */ uint24($_callbackGasLimit) ) { // ... } - /// ... Introspect the `WitnetRandomnessConsumer` contract to learn about + /// ... Introspect the `WitOracleRandomnessConsumer` contract to learn about /// ... the internal helper methods at your disposal for requesting new /// ... randomness to Witnet, changing the Witnet SLA parameters to use /// ... on next requests, or using the built-in uniform random number @@ -30,53 +29,21 @@ contract $_contractName /// =============================================================================================================== - /// --- Implementing `IWitnetConsumer` interface ------------------------------------------------------------------ - - /// @notice Method to be called from the WitnetOracle contract as soon as the given `_witnetQueryId` - /// @notice gets reported, if solved with no errors. - /// @param _witnetQueryId The unique identifier of the Witnet query being reported. - /// @param _witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin. - /// @param _witnetResultTimestamp Timestamp at which the reported value was captured by the Witnet blockchain. - /// @param _witnetResultFinalityBlock EVM block at which the provided data can be considered to be final. - /// @param _witnetResultCborValue The CBOR-encoded resulting value of the Witnet query being reported. - function reportWitnetQueryResult( - uint256 _witnetQueryId, - uint64 _witnetResultTimestamp, - bytes32 _witnetResultTallyHash, - uint256 _witnetResultFinalityBlock, - WitnetCBOR.CBOR calldata _witnetResultCborValue + /// --- Implementing `IWitOracleConsumer` interface --------------------------------------------------------------- + + /// @notice Method to be called from the WitOracle contract as soon as the given Witnet `queryId` gets reported. + /// @dev It should revert if called from any other address different to `__witOracle`. + /// @param _queryId The unique identifier of the Witnet query being reported. + /// @param _queryResult Struct contaning a Witnet.DataResult metadata and value. + function reportWitOracleQueryResult( + Witnet.QueryId _queryId, + Witnet.DataResult memory _queryResult ) override external - onlyFromWitnet + onlyFromWitOracle { // TODO: this method MUST be implemented // TODO: gas consumed by this method SHOULD NEVER be greater than $_callbackGasLimit units. - - bytes32 _witnetResultRandomness = _witnetReadRandomizeFromResultValue(_witnetResultCborValue); - // ... - } - - /// @notice Method to be called from the WitnetOracle contract as soon as the given `_witnetQueryId` - /// @notice gets reported, if solved WITH errors. - /// @param _witnetQueryId The unique identifier of the Witnet query being reported. - /// @param _witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin. - /// @param _witnetResultTimestamp Timestamp at which the reported value was captured by the Witnet blockchain. - /// @param _witnetResultFinalityBlock EVM block at which the provided data can be considered to be final. - /// @param _errorCode The error code enum identifying the error produced during resolution on the Witnet blockchain. - /// @param _errorArgs Error arguments, if any. An empty buffer is to be passed if no error arguments apply. - function reportWitnetQueryError( - uint256 _witnetQueryId, - uint64 _witnetResultTimestamp, - bytes32 _witnetResultTallyHash, - uint256 _witnetResultFinalityBlock, - Witnet.ResultErrorCodes _errorCode, - WitnetCBOR.CBOR calldata _errorArgs - ) - override external - onlyFromWitnet - { - // TODO: this method MUST be implemented - // TODO: gas consumed by this method SHOULD NEVER be greater than $_callbackGasLimit gas units. } } \ No newline at end of file diff --git a/witnet/contracts/_RequestConsumer.tsol b/witnet/contracts/_RequestConsumer.tsol index 14f2122..430188e 100644 --- a/witnet/contracts/_RequestConsumer.tsol +++ b/witnet/contracts/_RequestConsumer.tsol @@ -1,79 +1,49 @@ // SPDX-License-Identifier: MIT -pragma solidity >=0.7.0 <0.9.0; -pragma experimental ABIEncoderV2; +pragma solidity >=0.8.0 <0.9.0; -import "witnet-solidity-bridge/contracts/apps/WitnetRequestConsumer.sol";$_importWitnetMocks +import "witnet-solidity-bridge/contracts/mockups/WitOracleRequestConsumer.sol";$_importWitnetMocks contract $_contractName is - WitnetRequestConsumer + WitOracleRequestConsumer { - using Witnet for Witnet.Result; + using Witnet for Witnet.DataResult; + using Witnet for Witnet.QuerySLA; using WitnetCBOR for WitnetCBOR.CBOR; using WitnetCBOR for WitnetCBOR.CBOR[]; - using WitnetV2 for WitnetV2.RadonSLA; - + constructor ($_constructorParams) - WitnetRequestConsumer( - /* _witnetRequest */ $_witnetAddress, - /* _witnetBaseFeeOverheadPercentage */ uint16($_baseFeeOverhead), - /* _witnetCallbackGasLimit */ uint24($_callbackGasLimit) + WitOracleRequestConsumer( + /* _witOracleRequest */ $_witOracleAddress, + /* _witOracleBaseFeeOverheadPercentage */ uint16($_baseFeeOverhead), + /* _witOracleCallbackGasLimit */ uint24($_callbackGasLimit) ) { // ... } - /// ... Introspect the `WitnetRequestConsumer` abstract contract to learn + /// ... Introspect the `WitOracleRequestConsumer` abstract contract to learn /// ... about the internal helper methods at your disposal for pulling - /// ... data updates from the Witnet Oracle. + /// ... data updates from the Wit/Oracle blockchain. /// =============================================================================================================== - /// --- Implementing `IWitnetConsumer` interface ------------------------------------------------------------------ - - /// @notice Method to be called from the WitnetOracle contract as soon as the given `_witnetQueryId` - /// @notice gets reported, if solved with no errors. - /// @param _witnetQueryId The unique identifier of the Witnet query being reported. - /// @param _witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin. - /// @param _witnetResultTimestamp Timestamp at which the reported value was captured by the Witnet blockchain. - /// @param _witnetEvmFinalityBlock EVM block at which the provided data can be considered to be final. - /// @param _witnetResultCborValue The CBOR-encoded resulting value of the Witnet query being reported. - function reportWitnetQueryResult( - uint256 _witnetQueryId, - uint64 _witnetResultTimestamp, - bytes32 _witnetResultTallyHash, - uint256 _witnetEvmFinalityBlock, - WitnetCBOR.CBOR calldata _witnetResultCborValue + /// --- Implementing `IWitOracleConsumer` interface --------------------------------------------------------------- + + /// @notice Method to be called from the WitOracle contract as soon as the given Witnet `queryId` gets reported. + /// @dev It should revert if called from any other address different to `__witOracle`. + /// @param _queryId The unique identifier of the Witnet query being reported. + /// @param _queryResult Struct contaning a Witnet.DataResult metadata and value. + function reportWitOracleQueryResult( + Witnet.QueryId _queryId, + Witnet.DataResult memory _queryResult ) override external - onlyFromWitnet + onlyFromWitOracle { // TODO: this method MUST be implemented // TODO: gas consumed by this method SHOULD NEVER be greater than $_callbackGasLimit units. } - /// @notice Method to be called from the WitnetOracle contract as soon as the given `_witnetQueryId` - /// @notice gets reported, if solved WITH errors. - /// @param _witnetQueryId The unique identifier of the Witnet query being reported. - /// @param _witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin. - /// @param _witnetResultTimestamp Timestamp at which the reported value was captured by the Witnet blockchain. - /// @param _witnetEvmFinalityBlock EVM block at which the provided data can be considered to be final. - /// @param _errorCode The error code enum identifying the error produced during resolution on the Witnet blockchain. - /// @param _errorArgs Error arguments, if any. An empty buffer is to be passed if no error arguments apply. - function reportWitnetQueryError( - uint256 _witnetQueryId, - uint64 _witnetResultTimestamp, - bytes32 _witnetResultTallyHash, - uint256 _witnetEvmFinalityBlock, - Witnet.ResultErrorCodes _errorCode, - WitnetCBOR.CBOR calldata _errorArgs - ) - override external - onlyFromWitnet - { - // TODO: this method MUST be implemented - // TODO: gas consumed by this method SHOULD NEVER be greater than $_callbackGasLimit gas units. - } - } \ No newline at end of file diff --git a/witnet/contracts/_RequestTemplateConsumer.tsol b/witnet/contracts/_RequestTemplateConsumer.tsol index 72e20b1..b138950 100644 --- a/witnet/contracts/_RequestTemplateConsumer.tsol +++ b/witnet/contracts/_RequestTemplateConsumer.tsol @@ -1,79 +1,48 @@ // SPDX-License-Identifier: MIT -pragma solidity >=0.7.0 <0.9.0; -pragma experimental ABIEncoderV2; +pragma solidity >=0.8.0 <0.9.0; -import "witnet-solidity-bridge/contracts/apps/WitnetRequestTemplateConsumer.sol";$_importWitnetMocks +import "witnet-solidity-bridge/contracts/mockups/WitOracleRequestTemplateConsumer.sol";$_importWitnetMocks contract $_contractName is - WitnetRequestTemplateConsumer + WitOracleRequestTemplateConsumer { - using Witnet for Witnet.Result; + using Witnet for Witnet.DataResult; + using Witnet for Witnet.QuerySLA; using WitnetCBOR for WitnetCBOR.CBOR; using WitnetCBOR for WitnetCBOR.CBOR[]; - using WitnetV2 for WitnetV2.RadonSLA; - + constructor ($_constructorParams) - WitnetRequestTemplateConsumer( - /* _witnetRequestTemplate */ $_witnetAddress, - /* _witnetBaseFeeOverheadPercentage */ uint16($_baseFeeOverhead), - /* _witnetCallbackGasLimit */ uint24($_callbackGasLimit) + WitOracleRequestTemplateConsumer( + /* _witOracleRequestTemplate */ $_witOracleAddress, + /* _witOracleBaseFeeOverheadPercentage */ uint16($_baseFeeOverhead), + /* _witOracleCallbackGasLimit */ uint24($_callbackGasLimit) ) { // ... } - /// ... Introspect the `WitnetRequestTemplateConsumer` abstract contract to learn + /// ... Introspect the `WitOracleRequestTemplateConsumer` abstract contract to learn /// ... about the internal helper methods at your disposal for pulling - /// ... data updates from the Witnet Oracle. + /// ... data updates from the Wit/Oracle blockchain. /// =============================================================================================================== - /// --- Implementing `IWitnetConsumer` interface ------------------------------------------------------------------ - - /// @notice Method to be called from the WitnetOracle contract as soon as the given `_witnetQueryId` - /// @notice gets reported, if solved with no errors. - /// @param _witnetQueryId The unique identifier of the Witnet query being reported. - /// @param _witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin. - /// @param _witnetResultTimestamp Timestamp at which the reported value was captured by the Witnet blockchain. - /// @param _witnetEvmFinalityBlock EVM block at which the provided data can be considered to be final. - /// @param _witnetResultCborValue The CBOR-encoded resulting value of the Witnet query being reported. - function reportWitnetQueryResult( - uint256 _witnetQueryId, - uint64 _witnetResultTimestamp, - bytes32 _witnetResultTallyHash, - uint256 _witnetEvmFinalityBlock, - WitnetCBOR.CBOR calldata _witnetResultCborValue + /// --- Implementing `IWitOracleConsumer` interface --------------------------------------------------------------- + + /// @notice Method to be called from the WitOracle contract as soon as the given Witnet `queryId` gets reported. + /// @dev It should revert if called from any other address different to `__witOracle`. + /// @param _queryId The unique identifier of the Witnet query being reported. + /// @param _queryResult Struct contaning a Witnet.DataResult metadata and value. + function reportWitOracleQueryResult( + Witnet.QueryId _queryId, + Witnet.DataResult memory _queryResult ) override external - onlyFromWitnet + onlyFromWitOracle { // TODO: this method MUST be implemented // TODO: gas consumed by this method SHOULD NEVER be greater than $_callbackGasLimit units. } - - /// @notice Method to be called from the WitnetOracle contract as soon as the given `_witnetQueryId` - /// @notice gets reported, if solved WITH errors. - /// @param _witnetQueryId The unique identifier of the Witnet query being reported. - /// @param _witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin. - /// @param _witnetResultTimestamp Timestamp at which the reported value was captured by the Witnet blockchain. - /// @param _witnetEvmFinalityBlock EVM block at which the provided data can be considered to be final. - /// @param _errorCode The error code enum identifying the error produced during resolution on the Witnet blockchain. - /// @param _errorArgs Error arguments, if any. An empty buffer is to be passed if no error arguments apply. - function reportWitnetQueryError( - uint256 _witnetQueryId, - uint64 _witnetResultTimestamp, - bytes32 _witnetResultTallyHash, - uint256 _witnetEvmFinalityBlock, - Witnet.ResultErrorCodes _errorCode, - WitnetCBOR.CBOR calldata _errorArgs - ) - override external - onlyFromWitnet - { - // TODO: this method MUST be implemented - // TODO: gas consumed by this method SHOULD NEVER be greater than $_callbackGasLimit gas units. - } - -} \ No newline at end of file +} diff --git a/witnet/contracts/_UsingPriceFeeds.tsol b/witnet/contracts/_UsingPriceFeeds.tsol index 1c0cc42..5c4082b 100644 --- a/witnet/contracts/_UsingPriceFeeds.tsol +++ b/witnet/contracts/_UsingPriceFeeds.tsol @@ -1,37 +1,36 @@ // SPDX-License-Identifier: MIT -pragma solidity >=0.7.0 <0.9.0; -pragma experimental ABIEncoderV2; +pragma solidity >=0.8.0 <0.9.0; -import "witnet-solidity-bridge/contracts/WitnetPriceFeeds.sol";$_importWitnetMocks +import "witnet-solidity-bridge/contracts/WitPriceFeeds.sol";$_importWitnetMocks contract $_contractName is - IWitnetOracleEvents + IWitOracleEvents { - using WitnetV2 for WitnetV2.RadonSLA; + using Witnet for Witnet.QuerySLA; - WitnetOracle public immutable witnet; - WitnetPriceFeeds internal immutable __PFS; + WitOracle public immutable witOracle; + WitPriceFeeds internal immutable __PFS; constructor ($_constructorParams) { require( - address($_witnetAddress).code.length > 0 - && $_witnetAddress.specs() == type(IWitnetPriceFeeds).interfaceId, - "$_contractName: uncompliant WitnetPriceFeeds appliance" + address($_witOracleAddress).code.length > 0 + && $_witOracleAddress.specs() == type(IWitPriceFeeds).interfaceId, + "$_contractName: uncompliant WitPriceFeeds appliance" ); - __PFS = $_witnetAddress; - witnet = __PFS.witnet(); + __PFS = $_witOracleAddress; + witOracle = __PFS.witOracle(); } /// @dev See ERC-2362 as to understand how price feed captions are composed: - function _witnetPriceFeedIdOf(string memory _caption) + function _erc2362FeedIdOf(string memory _caption) internal pure returns (bytes4) { return bytes4(keccak256(bytes(_caption))); } - function _witnetPriceFeedIdOf( + function _erc2362PriceFeedIdOf( string memory _base, string memory _quote, uint8 _decimals @@ -70,10 +69,9 @@ contract $_contractName } } - /// ... Introspect the `IWitnetPriceFeeds` interface to learn about + /// ... Introspect the `WitPriceFeeds` ABI as to learn about /// ... the methods under `__PFS` for checking the list of supported price feeds, /// ... as well as their current status, or timestamp and audit trail proofs for latest - /// ... updates as resolved by the Witnet Oracle blockhain, or even the actual data sources + /// ... updates as resolved by the Wit/Oracle blockhain, or even the actual data sources /// ... and APIs being used under the hood for every supported price feed. - -} \ No newline at end of file +} diff --git a/witnet/contracts/_UsingRandomness.tsol b/witnet/contracts/_UsingRandomness.tsol index 6d610e4..2cce467 100644 --- a/witnet/contracts/_UsingRandomness.tsol +++ b/witnet/contracts/_UsingRandomness.tsol @@ -1,31 +1,24 @@ // SPDX-License-Identifier: MIT -pragma solidity >=0.7.0 <0.9.0; -pragma experimental ABIEncoderV2; +pragma solidity >=0.8.0 <0.9.0; -import "witnet-solidity-bridge/contracts/apps/UsingWitnetRandomness.sol";$_importWitnetMocks +import "witnet-solidity-bridge/contracts/mockups/UsingWitRandomness.sol";$_importWitnetMocks contract $_contractName is - UsingWitnetRandomness + UsingWitRandomness { - using WitnetV2 for WitnetV2.RadonSLA; + using Witnet for Witnet.QuerySLA; constructor ($_constructorParams) - UsingWitnetRandomness($_witnetAddress) + UsingWitRandomness($_witOracleAddress) { /// ... } - - /// @dev As to accept transfers back from the `WitnetRandomness` appliance - /// @dev when excessive fee is passed over to the `_RNG.randomize()` method. - - receive () external payable {} - - /// ... Introspect the `WitnetRandomness` appliance interface to learn + /// ... Introspect the `WitRandomness` appliance interface to learn /// ... about the public methods at your disposal via `__RNG` for pulling - /// ... unmalleable randomness from the Witnet Oracle blockchain. + /// ... unmalleable randomness from the Wit/Oracle blockchain. /// ... @@ -34,45 +27,48 @@ contract $_contractName // --- Internal helper methods: Random extractions with/without repetitions function _randomExtractionWithReps( - uint32 _range, - uint32 _extractions, - bytes32 _randomness + uint32 _range, + uint32 _extractions, + bytes32 _seed ) internal pure - returns (uint256[] memory _extraction) + returns (uint32[] memory _extraction) { unchecked { _extraction = new uint256[](_extractions); for (uint32 _ix = 0; _ix < _extractions; _ix ++) { - _extraction[_ix] = uint256( - WitnetV2.randomUniformUint32(_range, _ix, _randomness) + _extraction[_ix] = Witnet.randomUniformUint32( + _range, + _ix, + _seed ); } } } function _randomExtractionNoReps( - uint32 _range, - uint32 _extractions, - bytes32 _randomness + uint32 _range, + uint32 _extractions, + bytes32 _seed ) internal pure - returns (uint256[] memory _extraction) + returns (uint32[] memory _extraction) { unchecked { - uint256[] memory _numbers = new uint256[](_extractions); + uint32[] memory _numbers = new uint32[](_extractions); for (uint32 _ix = 0; _ix < _extractions; _ix ++) { _numbers[_ix] = _ix; } _extraction = new uint256[](_extractions); for (uint32 _ix = 0; _ix < _extractions; _ix ++) { - uint256 _random = uint256( - WitnetV2.randomUniformUint32(_range - _ix, _ix, _randomness) + uint256 _random = Witnet.randomUniformUint32( + _range - _ix, + _ix, + _seed ); - _extraction[_ix] = uint256(_numbers[_random]); + _extraction[_ix] = _numbers[_random]; _numbers[_random] = _numbers[_extractions - 1 - _ix]; } } } - -} \ No newline at end of file +} diff --git a/witnet/contracts/_UsingRequest.tsol b/witnet/contracts/_UsingRequest.tsol index 3237dcd..eb7cbc3 100644 --- a/witnet/contracts/_UsingRequest.tsol +++ b/witnet/contracts/_UsingRequest.tsol @@ -1,30 +1,29 @@ // SPDX-License-Identifier: MIT -pragma solidity >=0.7.0 <0.9.0; -pragma experimental ABIEncoderV2; +pragma solidity >=0.8.0 <0.9.0; -import "witnet-solidity-bridge/contracts/apps/UsingWitnetRequest.sol";$_importWitnetMocks +import "witnet-solidity-bridge/contracts/mockups/UsingWitOracleRequest.sol";$_importWitnetMocks contract $_contractName is - UsingWitnetRequest + UsingWitOracleRequest { - using Witnet for Witnet.Result; + using Witnet for Witnet.DataResult; + using Witnet for Witnet.QuerySLA; using WitnetCBOR for WitnetCBOR.CBOR; using WitnetCBOR for WitnetCBOR.CBOR[]; - using WitnetV2 for WitnetV2.RadonSLA; constructor ($_constructorParams) - UsingWitnetRequest( - /* _witnetRequest */ $_witnetAddress, - /* _witnetBaseFeeOverheadPercentage */ uint16($_baseFeeOverhead) + UsingWitOracleRequest( + /* _witOracleRequest */ $_witOracleAddress, + /* _witOracleBaseFeeOverheadPercentage */ uint16($_baseFeeOverhead) ) { // ... } - /// ... Introspect the `UsingWitnetRequestTemplate` abstract contract to learn + /// ... Introspect the `UsingWitOracleRequest` abstract contract to learn /// ... about the internal helper methods at your disposal for pulling - /// ... data updates from the Witnet Oracle via the `_witnet*` internal methods. + /// ... data updates from the Wit/Oracle blockchain via the `_witOracle*` internal methods. } \ No newline at end of file diff --git a/witnet/contracts/_UsingRequestTemplate.tsol b/witnet/contracts/_UsingRequestTemplate.tsol index c75a85a..d36c4d5 100644 --- a/witnet/contracts/_UsingRequestTemplate.tsol +++ b/witnet/contracts/_UsingRequestTemplate.tsol @@ -1,29 +1,28 @@ // SPDX-License-Identifier: MIT -pragma solidity >=0.7.0 <0.9.0; -pragma experimental ABIEncoderV2; +pragma solidity >=0.8.0 <0.9.0; -import "witnet-solidity-bridge/contracts/apps/UsingWitnetRequestTemplate.sol";$_importWitnetMocks +import "witnet-solidity-bridge/contracts/mockups/UsingWitOracleRequestTemplate.sol";$_importWitnetMocks contract $_contractName is - UsingWitnetRequestTemplate + UsingWitOracleRequestTemplate { - using Witnet for Witnet.Result; + using Witnet for Witnet.DataResult; using WitnetCBOR for WitnetCBOR.CBOR; using WitnetCBOR for WitnetCBOR.CBOR[]; constructor ($_constructorParams) - UsingWitnetRequestTemplate( - /* _witnetRequestTemplate */ $_witnetAddress, - /* _witnetBaseFeeOverheadPercentage */ uint16($_baseFeeOverhead) + UsingWitOracleRequestTemplate( + /* _witOracleRequestTemplate */ $_witOracleAddress, + /* _witOracleBaseFeeOverheadPercentage */ uint16($_baseFeeOverhead) ) { // ... } - /// ... Introspect the `UsingWitnetRequestTemplate` abstract contract to learn + /// ... Introspect the `UsingWitOracleRequestTemplate` abstract contract to learn /// ... about the internal helper methods at your disposal for pulling - /// ... data updates from the Witnet Oracle. + /// ... data updates from the Wit/Oracle blockchain. } \ No newline at end of file diff --git a/witnet/scripts/truffle/1_1_witnet_templates.js b/witnet/scripts/truffle/1_1_witnet_templates.js index c81aca0..30676ff 100644 --- a/witnet/scripts/truffle/1_1_witnet_templates.js +++ b/witnet/scripts/truffle/1_1_witnet_templates.js @@ -12,21 +12,21 @@ const templates = (process.argv.includes("--legacy") const utils = require("../utils") const selection = utils.getWitnetArtifactsFromArgs() -const WitnetRequestBytecodes = artifacts.require("WitnetRequestBytecodes") -const WitnetRequestFactory = artifacts.require("WitnetRequestFactory") -const WitnetRequestTemplate = artifacts.require("WitnetRequestTemplate") +const WitOracleRadonRegistry = artifacts.require("WitOracleRadonRegistry") +const WitOracleRequestFactory = artifacts.require("WitOracleRequestFactory") +const WitOracleRequestTemplate = artifacts.require("WitOracleRequestTemplate") -module.exports = async function (_deployer, network, [from, ]) { +module.exports = async function (_deployer, network, [from]) { const isDryRun = utils.isDryRun(network) const addresses = utils.loadAddresses(isDryRun ? `${witnet_module_path}/tests/truffle` : "./witnet") if (!addresses[network]) addresses[network] = {} if (!addresses[network].templates) addresses[network].templates = {} - addresses[network] = await deployWitnetRequestTemplates( - addresses[network], - utils.getFromFromArgs() || from, - isDryRun, + addresses[network] = await deployWitOracleRequestTemplates( + addresses[network], + utils.getFromFromArgs() || from, + isDryRun, templates ) if (Object.keys(addresses[network].templates).length > 0) { @@ -38,40 +38,34 @@ module.exports = async function (_deployer, network, [from, ]) { } } -async function deployWitnetRequestTemplates (addresses, from, isDryRun, templates) { +async function deployWitOracleRequestTemplates (addresses, from, isDryRun, templates) { for (const key in templates) { const template = templates[key] if (template?.specs) { - const targetAddr = addresses?.templates[key] ?? null if ( - (process.argv.includes("--all") || - selection.includes(key) || - (selection.length === 0 && isDryRun) - ) && ( - utils.isNullAddress(targetAddr) || - (await web3.eth.getCode(targetAddr)).length <= 3) + process.argv.includes("--all") + || selection.includes(key) + || (selection.length === 0 && isDryRun) ) { try { - const templateAddr = await utils.deployWitnetRequestTemplate( + const templateAddr = await utils.deployWitOracleRequestTemplate( web3, from, - await WitnetRequestBytecodes.deployed(), - await WitnetRequestFactory.deployed(), + await WitOracleRadonRegistry.deployed(), + await WitOracleRequestFactory.deployed(), template, key, ) - const templateContract = await WitnetRequestTemplate.at(templateAddr) - console.info(" ", "> Template registry: ", await templateContract.registry.call()) - console.info(" ", `> Template address: \x1b[1;37m${templateContract.address}\x1b[0m`) + const templateContract = await WitOracleRequestTemplate.at(templateAddr) + console.info(" ", `> Template address: \x1b[1;37m${templateContract.address}\x1b[0m`) + console.info(" ", "> Template class: ", await templateContract.class.call()) + console.info(" ", "> Template version: ", await templateContract.version.call()) addresses.templates[key] = templateAddr } catch (e) { utils.traceHeader(`Failed '\x1b[1;31m${key}\x1b[0m': ${e}`) process.exit(0) } - } else if (!utils.isNullAddress(targetAddr)) { - utils.traceHeader(`Skipping '\x1b[1;37m${key}\x1b[0m'`) - console.info(" ", `> Template address: \x1b[1;37m${targetAddr}\x1b[0m`) } } else { - addresses = await deployWitnetRequestTemplates(addresses, from, isDryRun, template) + addresses = await deployWitOracleRequestTemplates(addresses, from, isDryRun, template) } } return addresses diff --git a/witnet/scripts/truffle/1_2_witnet_requests.js b/witnet/scripts/truffle/1_2_witnet_requests.js index e71e801..d687c3c 100644 --- a/witnet/scripts/truffle/1_2_witnet_requests.js +++ b/witnet/scripts/truffle/1_2_witnet_requests.js @@ -12,21 +12,21 @@ const requests = (process.argv.includes("--legacy") const utils = require("../utils") const selection = utils.getWitnetArtifactsFromArgs() -const WitnetRequestBytecodes = artifacts.require("WitnetRequestBytecodes") -const WitnetRequestFactory = artifacts.require("WitnetRequestFactory") -const WitnetRequestTemplate = artifacts.require("WitnetRequestTemplate") +const WitOracleRadonRegistry = artifacts.require("WitOracleRadonRegistry") +const WitOracleRequestFactory = artifacts.require("WitOracleRequestFactory") +const WitOracleRequestTemplate = artifacts.require("WitOracleRequestTemplate") -module.exports = async function (_deployer, network, [from, ]) { +module.exports = async function (_deployer, network, [from]) { const isDryRun = utils.isDryRun(network) const addresses = utils.loadAddresses(isDryRun ? `${witnet_module_path}/tests/truffle` : "./witnet") if (!addresses[network]) addresses[network] = {} if (!addresses[network].requests) addresses[network].requests = {} - addresses[network] = await deployWitnetRequests( - addresses[network], - utils.getFromFromArgs() || from, - isDryRun, + addresses[network] = await deployWitOracleRequests( + addresses[network], + utils.getFromFromArgs() || from, + isDryRun, requests ) @@ -40,25 +40,21 @@ module.exports = async function (_deployer, network, [from, ]) { } } -async function deployWitnetRequests (addresses, from, isDryRun, requests) { +async function deployWitOracleRequests (addresses, from, isDryRun, requests) { for (const key in requests) { const request = requests[key] if (request?.specs) { - const targetAddr = addresses.requests[key] ?? null if ( - (process.argv.includes("--all") || - selection.includes(key) || - (selection.length === 0 && isDryRun) - ) && ( - utils.isNullAddress(targetAddr) || - (await web3.eth.getCode(targetAddr)).length <= 3) + process.argv.includes("--all") + || selection.includes(key) + || (selection.length === 0 && isDryRun) ) { try { - const requestAddress = await utils.deployWitnetRequest( + const requestAddress = await utils.deployWitOracleRequest( web3, from, - await WitnetRequestBytecodes.deployed(), - await WitnetRequestFactory.deployed(), - request, WitnetRequestTemplate, key + await WitOracleRadonRegistry.deployed(), + await WitOracleRequestFactory.deployed(), + request, WitOracleRequestTemplate, key ) console.info(" ", `> Request address: \x1b[1;37m${requestAddress}\x1b[0m`) addresses.requests[key] = requestAddress @@ -66,12 +62,9 @@ async function deployWitnetRequests (addresses, from, isDryRun, requests) { utils.traceHeader(`Failed '\x1b[1;31m${key}\x1b[0m': ${e}`) process.exit(0) } - } else if (!utils.isNullAddress(targetAddr)) { - utils.traceHeader(`Skipping '\x1b[1;37m${key}\x1b[0m'`) - console.info(" ", `> Request address: \x1b[1;37m${targetAddr}\x1b[0m`) } } else { - addresses = await deployWitnetRequests(addresses, from, isDryRun, request) + addresses = await deployWitOracleRequests(addresses, from, isDryRun, request) } } return addresses diff --git a/witnet/scripts/truffle/1__witnet.js b/witnet/scripts/truffle/1__witnet.js index 867ce70..aef9d05 100644 --- a/witnet/scripts/truffle/1__witnet.js +++ b/witnet/scripts/truffle/1__witnet.js @@ -9,9 +9,9 @@ const assets = require(`${assets_relative_path}/assets`) const fs = require("fs") const utils = require("../utils") -const WitnetRequestBytecodes = artifacts.require("WitnetRequestBytecodes") -const WitnetOracle = artifacts.require("WitnetOracle") -const WitnetRequestFactory = artifacts.require("WitnetRequestFactory") +const WitOracle = artifacts.require("WitOracle") +const WitOracleRadonRegistry = artifacts.require("WitOracleRadonRegistry") +const WitOracleRequestFactory = artifacts.require("WitOracleRequestFactory") module.exports = async function (deployer, network) { const addresses = assets.getAddresses(network) @@ -19,44 +19,43 @@ module.exports = async function (deployer, network) { if (!isDryRun) { try { - WitnetOracle.address = addresses.WitnetOracle - const wrb = await WitnetOracle.deployed() - WitnetRequestBytecodes.address = await wrb.registry.call() - WitnetRequestFactory.address = await wrb.factory.call() + WitOracle.address = addresses.core?.WitOracle + WitOracleRadonRegistry.address = addresses.core?.WitOracleRadonRegistry + WitOracleRequestFactory.address = addresses.core?.WitOracleRequestFactory; } catch (e) { console.error("Fatal: Witnet Foundation addresses were not provided!", e) process.exit(1) } + } else { - const WitnetEncodingLib = artifacts.require("WitnetEncodingLib") - await deployer.deploy(WitnetEncodingLib) + const WitOracleRadonEncodingLib = artifacts.require("WitOracleRadonEncodingLib") + await deployer.deploy(WitOracleRadonEncodingLib) - const WitnetErrorsLib = artifacts.require("WitnetErrorsLib") - await deployer.deploy(WitnetErrorsLib) + const WitOracleResultStatusLib = artifacts.require("WitOracleResultStatusLib") + await deployer.deploy(WitOracleResultStatusLib) - const WitnetOracleDataLib = artifacts.require("WitnetOracleDataLib") - await deployer.deploy(WitnetOracleDataLib) + const WitOracleDataLib = artifacts.require("WitOracleDataLib") + await deployer.deploy(WitOracleDataLib) - const WitnetMockedRequestBytecodes = artifacts.require("WitnetMockedRequestBytecodes") - await deployer.link(WitnetEncodingLib, WitnetMockedRequestBytecodes) - await deployer.deploy(WitnetMockedRequestBytecodes) - WitnetRequestBytecodes.address = WitnetMockedRequestBytecodes.address + const WitMockedRadonRegistry = artifacts.require("WitMockedRadonRegistry") + await deployer.link(WitOracleRadonEncodingLib, WitMockedRadonRegistry) + await deployer.deploy(WitMockedRadonRegistry) + WitOracleRadonRegistry.address = WitMockedRadonRegistry.address - const WitnetMockedOracle = artifacts.require("WitnetMockedOracle") - await deployer.link(WitnetErrorsLib, WitnetMockedOracle) - await deployer.link(WitnetOracleDataLib, WitnetMockedOracle) - await deployer.deploy(WitnetMockedOracle, WitnetRequestBytecodes.address) - WitnetOracle.address = WitnetMockedOracle.address + const WitMockedOracle = artifacts.require("WitMockedOracle") + await deployer.link(WitOracleResultStatusLib, WitMockedOracle) + await deployer.link(WitOracleDataLib, WitMockedOracle) + await deployer.deploy(WitMockedOracle, WitOracleRadonRegistry.address) + WitOracle.address = WitMockedOracle.address - const WitnetMockedRequestFactory = artifacts.require("WitnetMockedRequestFactory") - await deployer.deploy(WitnetMockedRequestFactory, WitnetOracle.address) - WitnetRequestFactory.address = WitnetMockedRequestFactory.address - await (await WitnetMockedOracle.deployed()).setFactory(WitnetRequestFactory.address) + const WitMockedRequestFactory = artifacts.require("WitMockedRequestFactory") + await deployer.deploy(WitMockedRequestFactory, WitOracle.address) + WitOracleRequestFactory.address = WitMockedRequestFactory.address addresses[network] = { - WitnetRequestBytecodes: WitnetRequestBytecodes.address, - WitnetOracle: WitnetOracle.address, - WitnetRequestFactory: WitnetRequestFactory.address, + WitOracle: WitOracle.address, + WitOracleRadonRegistry: WitOracleRadonRegistry.address, + WitOracleRequestFactory: WitOracleRequestFactory.address, } // create test addresses file if none exists yet: @@ -67,22 +66,22 @@ module.exports = async function (deployer, network) { } utils.traceHeader("Witnet Artifacts:") - if (WitnetOracle.address) { - console.info(" ", "> WitnetOracle: ", - WitnetOracle.address, - `(${isDryRun ? "mocked" : await _readUpgradableArtifactVersion(WitnetOracle)})` + if (WitOracle.address) { + console.info(" ", "> WitOracle: ", + WitOracle.address, + `(${isDryRun ? "mocked" : await _readUpgradableArtifactVersion(WitOracle)})` ) } - if (WitnetRequestBytecodes.address) { - console.info(" ", "> WitnetRequestBytecodes:", - WitnetRequestBytecodes.address, - `(${isDryRun ? "mocked" : await _readUpgradableArtifactVersion(WitnetRequestBytecodes)})` + if (WitOracleRadonRegistry.address) { + console.info(" ", "> WitOracleRadonRegistry: ", + WitOracleRadonRegistry.address, + `(${isDryRun ? "mocked" : await _readUpgradableArtifactVersion(WitOracleRadonRegistry)})` ) } - if (WitnetRequestFactory.address) { - console.info(" ", "> WitnetRequestFactory: ", - WitnetRequestFactory.address, - `(${isDryRun ? "mocked" : await _readUpgradableArtifactVersion(WitnetRequestFactory)})` + if (WitOracleRequestFactory.address) { + console.info(" ", "> WitOracleRequestFactory:", + WitOracleRequestFactory.address, + `(${isDryRun ? "mocked" : await _readUpgradableArtifactVersion(WitOracleRequestFactory)})` ) } } diff --git a/witnet/scripts/utils.js b/witnet/scripts/utils.js index b96f02c..378181b 100644 --- a/witnet/scripts/utils.js +++ b/witnet/scripts/utils.js @@ -4,15 +4,15 @@ const utils = require("witnet-solidity-bridge/utils") const Witnet = require("witnet-toolkit") module.exports = { - deployWitnetRequest, - deployWitnetRequestTemplate, + deployWitOracleRequest, + deployWitOracleRequestTemplate, dryRunBytecode: toolkit.dryRunBytecode, dryRunBytecodeVerbose: toolkit.dryRunBytecodeVerbose, flattenWitnetArtifacts, getFromFromArgs, getRealmNetworkFromArgs: utils.getRealmNetworkFromArgs, getWitnetArtifactsFromArgs: utils.getWitnetArtifactsFromArgs, - getWitnetRequestMethodString: utils.getWitnetRequestMethodString, + getWitOracleRequestMethodString: utils.getWitOracleRequestMethodString, isDryRun: utils.isDryRun, isNullAddress, orderObjectKeys, @@ -23,65 +23,93 @@ module.exports = { overwriteJsonFile: utils.overwriteJsonFile, traceHeader: utils.traceHeader, traceTx, - verifyWitnetRadonReducer, - verifyWitnetRadonRetrieval, + verifyRadonReducer, + verifyRadonRetrieval, } -async function buildWitnetRequestFromTemplate (web3, from, templateContract, args) { +async function buildWitOracleRequestFromTemplate (web3, from, templateContract, args) { // convert all args values to string args = args.map(subargs => subargs.map(v => v.toString())) - const requestAddr = await templateContract.buildRequest.call(args, { from }) + const requestAddr = await templateContract.methods['buildWitOracleRequest(string[][])'].call(args, { from }) if ((await web3.eth.getCode(requestAddr)).length <= 3) { - const tx = await templateContract.buildRequest(args, { from }) + const tx = await templateContract.methods['buildWitOracleRequest(string[][])'](args, { from }) console.info(" ", "> Template settlement hash:", tx.receipt.transactionHash) console.info(" ", "> Template settlement gas: ", tx.receipt.gasUsed) } return requestAddr } -async function deployWitnetRequest (web3, from, registry, factory, request, templateArtifact, key) { - const templateAddr = await deployWitnetRequestTemplate(web3, from, registry, factory, request) - if (key) utils.traceHeader(`Building '\x1b[1;37m${key}\x1b[0m'...`) - console.info(" ", "> Template address: ", templateAddr) - const args = [] - if (request?.args) { - console.info(" ", "> Instance parameters:") - request?.args?.forEach((subargs, index) => { - console.info( - " ", - `Source #${index + 1}: \x1b[1;32m${JSON.stringify(subargs)}\x1b[0m => \x1b[32m${request.specs?.retrieve[index].url} ...\x1b[0m` - ) - args[index] = subargs - }) +async function deployWitOracleRequest (web3, from, registry, factory, request, templateArtifact, key) { + if (request instanceof Witnet.Artifacts.Parameterized) { + const templateAddr = await deployWitOracleRequestTemplate(web3, from, registry, factory, request) + if (key) utils.traceHeader(`Building '\x1b[1;37m${key}\x1b[0m'...`) + console.info(" ", "> Template address: ", templateAddr) + const args = [] + if (request?.args) { + console.info(" ", "> Instance parameters:") + request?.args?.forEach((subargs, index) => { + console.info( + " ", + `Source #${index + 1}: \x1b[1;32m${JSON.stringify(subargs)}\x1b[0m => \x1b[32m${request.specs?.retrieve[index].url} ...\x1b[0m` + ) + args[index] = subargs + }) + } else { + request.specs.retrieve.map(_source => args.push([])) + } + return await buildWitOracleRequestFromTemplate(web3, from, await templateArtifact.at(templateAddr), args) } else { - request.specs.retrieve.map(_source => args.push([])) + const sources = await verifyRadonRetrievals(from, registry, request.specs.retrieve) + if (key) utils.traceHeader(`Building '\x1b[1;37m${key}\x1b[0m'...`) + let requestAddr = await factory.buildWitOracleRequest.call( + sources, + encodeWitnetRadon(request.specs.aggregate), + encodeWitnetRadon(request.specs.tally), + { from } + ) + if (isNullAddress(requestAddr) || (await web3.eth.getCode(requestAddr)).length <= 3) { + const tx = await factory.buildWitOracleRequest( + sources, + encodeWitnetRadon(request.specs.aggregate), + encodeWitnetRadon(request.specs.tally), + { from } + ) + traceTx(tx.receipt) + tx.logs = tx.logs.filter(log => log.event === "WitOracleRequestBuilt") + requestAddr = tx.logs[0].args.request + } + return requestAddr } - return await buildWitnetRequestFromTemplate(web3, from, await templateArtifact.at(templateAddr), args) } -async function deployWitnetRequestTemplate (web3, from, registry, factory, template, key) { - const aggregate = await verifyWitnetRadonReducer(from, registry, template.specs.aggregate) - const tally = await verifyWitnetRadonReducer(from, registry, template.specs.tally) +async function verifyRadonRetrievals (from, registry, retrievals) { const sources = [] - const args = [] - for (let j = 0; j < template?.specs.retrieve.length; j++) { - sources.push(await verifyWitnetRadonRetrieval(from, registry, template.specs.retrieve[j])) - args.push([]) + for (let j = 0; j < retrievals.length; j++) { + sources.push( + await verifyRadonRetrieval(from, registry, retrievals[j]) + ) } + return sources +} + +async function deployWitOracleRequestTemplate (web3, from, registry, factory, template, key) { + const sources = await verifyRadonRetrievals(from, registry, template.specs.retrieve) if (key) utils.traceHeader(`Building '\x1b[1;37m${key}\x1b[0m'...`) - let templateAddr = await factory.buildRequestTemplate.call( - sources, aggregate, tally, - template?.specs?.maxSize || 32, + let templateAddr = await factory.buildWitOracleRequestTemplate.call( + sources, + encodeWitnetRadon(template.specs.aggregate), + encodeWitnetRadon(template.specs.tally), { from } ) if (isNullAddress(templateAddr) || (await web3.eth.getCode(templateAddr)).length <= 3) { - const tx = await factory.buildRequestTemplate( - sources, aggregate, tally, - template?.specs?.maxSize || 32, + const tx = await factory.buildWitOracleRequestTemplate( + sources, + encodeWitnetRadon(template.specs.aggregate), + encodeWitnetRadon(template.specs.tally), { from } ) traceTx(tx.receipt) - tx.logs = tx.logs.filter(log => log.event === "WitnetRequestTemplateBuilt") + tx.logs = tx.logs.filter(log => log.event === "WitOracleRequestTemplateBuilt") templateAddr = tx.logs[0].args.template } return templateAddr @@ -187,7 +215,7 @@ function processDryRunJson (dryrun) { } } -async function verifyWitnetRadonReducer (from, registry, reducer) { +async function verifyRadonReducer (from, registry, reducer) { let hash if (reducer instanceof Witnet.Reducers.Class) { hash = await registry.verifyRadonReducer.call(encodeWitnetRadon(reducer), { from }) @@ -207,7 +235,7 @@ async function verifyWitnetRadonReducer (from, registry, reducer) { return hash }; -async function verifyWitnetRadonRetrieval (from, registry, source) { +async function verifyRadonRetrieval (from, registry, source) { // get actual hash for this data source let hash if (source) { @@ -226,7 +254,7 @@ async function verifyWitnetRadonRetrieval (from, registry, source) { if (source?.url) { console.info(` > URL: \x1b[1;32m${source.url}\x1b[0m`) } - console.info(` > Method: \x1b[1;32m${utils.getWitnetRequestMethodString(source?.method)}\x1b[0m`) + console.info(` > Method: \x1b[1;32m${utils.getWitOracleRequestMethodString(source?.method)}\x1b[0m`) if (source?.body) { console.info(` > Body: \x1b[1;32m${source.body}\x1b[0m`) } diff --git a/witnet/tests/truffle/witnet.requests.spec.js b/witnet/tests/truffle/witnet.requests.spec.js index d6a9487..3b5cdef 100644 --- a/witnet/tests/truffle/witnet.requests.spec.js +++ b/witnet/tests/truffle/witnet.requests.spec.js @@ -1,7 +1,6 @@ const utils = require("../../scripts/utils") -const WitnetRequest = artifacts.require("WitnetRequest") -const WitnetRequestBytecodes = artifacts.require("WitnetRequestBytecodes") +const WitOracleRequest = artifacts.require("WitOracleRequest") contract("witnet-solidity/requests", async () => { const [, network] = utils.getRealmNetworkFromArgs() @@ -30,13 +29,12 @@ contract("witnet-solidity/requests", async () => { await describe(`${craft.key}`, async () => { let bytecode, radHash it("request was actually deployed", async () => { - const request = await WitnetRequest.at(requestAddress) + const request = await WitOracleRequest.at(requestAddress) radHash = await request.radHash.call() }) it("request dryruns successfully", async () => { - const request = await WitnetRequest.at(requestAddress) - const registry = await WitnetRequestBytecodes.at(await request.registry.call()) - bytecode = (await registry.bytecodeOf.call(await request.radHash.call())).slice(2) + const request = await WitOracleRequest.at(requestAddress) + bytecode = (await request.bytecode.call()).slice(2) const output = await utils.dryRunBytecode(bytecode) let json try { diff --git a/witnet/tests/truffle/witnet.templates.spec.js b/witnet/tests/truffle/witnet.templates.spec.js index 968b673..33f8aaa 100644 --- a/witnet/tests/truffle/witnet.templates.spec.js +++ b/witnet/tests/truffle/witnet.templates.spec.js @@ -1,8 +1,7 @@ const utils = require("../../scripts/utils") -const WitnetRequest = artifacts.require("WitnetRequest") -const WitnetRequestBytecodes = artifacts.require("WitnetRequestBytecodes") -const WitnetRequestTemplate = artifacts.require("WitnetRequestTemplate") +const WitOracleRequest = artifacts.require("WitOracleRequest") +const WitOracleRequestTemplate = artifacts.require("WitOracleRequestTemplate") contract("witnet-solidity/templates", async () => { const [, network] = utils.getRealmNetworkFromArgs() @@ -34,18 +33,17 @@ contract("witnet-solidity/templates", async () => { const args = craft.artifact.tests[test] let bytecode it("parameterized request gets built", async () => { - const template = await WitnetRequestTemplate.at(templateAddress) + const template = await WitOracleRequestTemplate.at(templateAddress) await template.verifyRadonRequest(args) }) it("parameterized request dryruns successfully", async () => { - const template = await WitnetRequestTemplate.at(templateAddress) - const tx = await template.buildRequest(args) - const events = tx.logs.filter(log => log.event === "WitnetRequestBuilt") - assert(events.length > 0, "No WitnetRequest contract was built!") - const request = await WitnetRequest.at(events[0].args.request) - const radHash = await request.radHash.call() - const registry = await WitnetRequestBytecodes.at(await request.registry.call()) - bytecode = (await registry.bytecodeOf.call(radHash)).slice(2) + const template = await WitOracleRequestTemplate.at(templateAddress) + const tx = await template.methods["buildWitOracleRequest(string[][])"](args) + const events = tx.logs.filter(log => log.event === "WitOracleRequestBuilt") + assert(events.length > 0, "No WitOracleRequest contract was built!") + const request = await WitOracleRequest.at(events[0].args.request) + // const radHash = await request.radHash.call() + bytecode = (await request.bytecode.call()).slice(2) const output = await utils.dryRunBytecode(bytecode) let json try {