witnet-solidity-bridge
Version:
Witnet Solidity Bridge contracts for EVM-compatible chains
443 lines • 804 kB
JSON
{
"contractName": "Witnet",
"abi": [],
"metadata": "{\"compiler\":{\"version\":\"0.8.25+commit.b61c2a91\"},\"language\":\"Solidity\",\"output\":{\"abi\":[],\"devdoc\":{\"kind\":\"dev\",\"methods\":{},\"version\":1},\"userdoc\":{\"kind\":\"user\",\"methods\":{},\"version\":1}},\"settings\":{\"compilationTarget\":{\"project:/contracts/libs/Witnet.sol\":\"Witnet\"},\"evmVersion\":\"paris\",\"libraries\":{},\"metadata\":{\"bytecodeHash\":\"ipfs\"},\"optimizer\":{\"enabled\":true,\"runs\":200},\"remappings\":[]},\"sources\":{\"project:/contracts/libs/Witnet.sol\":{\"keccak256\":\"0x65a87375dd79d63a83fb454b7199b6c999bd59c50b3b59d521c5c4d45a7d3cc3\",\"license\":\"MIT\",\"urls\":[\"bzz-raw://ca865b681d810c2fc5c3672ea6343c3bdf6fd71764ab824d25994744dc85866b\",\"dweb:/ipfs/QmPGcP3xGTNZfsQ9GSKdujNLRVs8dWDdubyUko1rbQqJNv\"]},\"project:/contracts/libs/WitnetBuffer.sol\":{\"keccak256\":\"0xa14570492eb5a313ddbacae0185c850ec99c67211eb33989a5e21d31bf06a150\",\"license\":\"MIT\",\"urls\":[\"bzz-raw://e83c11edb49cab6a767c0b685825bc22ece0d3d2897e0d54fe1923df5cc76ba5\",\"dweb:/ipfs/QmdLDgCc3tnKbgRrXwfNzsg6uUDirNmjvBB8V3iMmnD69a\"]},\"project:/contracts/libs/WitnetCBOR.sol\":{\"keccak256\":\"0xb346547ff731163beea2c657c52675cdf7936691d566a76a045577cf9c34ade0\",\"license\":\"MIT\",\"urls\":[\"bzz-raw://6d4b5b6424a033584b41f1204d635db98fda9ca9bd2a614c9d82539a3e4e6529\",\"dweb:/ipfs/QmW6Qy3wWpzHSECYaCPaf9LWGfPqWDKVoP2kPSNNQu7LMQ\"]}},\"version\":1}",
"bytecode": "0x60566037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212200395a8070e2ff7ff017b95bb42096ae8c9b66f9e85390b353ad8d1c795a659c264736f6c63430008190033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212200395a8070e2ff7ff017b95bb42096ae8c9b66f9e85390b353ad8d1c795a659c264736f6c63430008190033",
"immutableReferences": {},
"generatedSources": [],
"deployedGeneratedSources": [],
"sourceMap": "135:31790:64:-:0;;;;;;;;;;;;;;;-1:-1:-1;;;135:31790:64;;;;;;;;;;;;;;;;;",
"deployedSourceMap": "135:31790:64:-:0;;;;;;;;",
"source": "// SPDX-License-Identifier: MIT\r\n\r\npragma solidity >=0.7.0 <0.9.0;\r\npragma experimental ABIEncoderV2;\r\n\r\nimport \"./WitnetCBOR.sol\";\r\n\r\nlibrary Witnet {\r\n\r\n using WitnetBuffer for WitnetBuffer.Buffer;\r\n using WitnetCBOR for WitnetCBOR.CBOR;\r\n using WitnetCBOR for WitnetCBOR.CBOR[];\r\n\r\n /// Struct containing both request and response data related to every query posted to the Witnet Request Board\r\n struct Query {\r\n Request request;\r\n Response response;\r\n address from; // Address from which the request was posted.\r\n }\r\n\r\n /// Possible status of a Witnet query.\r\n enum QueryStatus {\r\n Unknown,\r\n Posted,\r\n Reported,\r\n Deleted\r\n }\r\n\r\n /// Data kept in EVM-storage for every Request posted to the Witnet Request Board.\r\n struct Request {\r\n address addr; // Address of the (deprecated) IWitnetRequest contract containing Witnet data request raw bytecode.\r\n bytes32 slaHash; // Radon SLA hash of the Witnet data request.\r\n bytes32 radHash; // Radon radHash of the Witnet data request.\r\n uint256 gasprice; // Minimum gas price the DR resolver should pay on the solving tx.\r\n uint256 reward; // Escrowed reward to be paid to the DR resolver.\r\n }\r\n\r\n /// Data kept in EVM-storage containing the Witnet-provided response metadata and CBOR-encoded result.\r\n struct Response {\r\n address reporter; // Address from which the result was reported.\r\n uint256 timestamp; // Timestamp of the Witnet-provided result.\r\n bytes32 drTxHash; // Hash of the Witnet transaction that solved the queried Data Request.\r\n bytes cborBytes; // Witnet-provided result CBOR-bytes to the queried Data Request.\r\n }\r\n\r\n /// Data struct containing the Witnet-provided result to a Data Request.\r\n struct Result {\r\n bool success; // Flag stating whether the request could get solved successfully, or not.\r\n WitnetCBOR.CBOR value; // Resulting value, in CBOR-serialized bytes.\r\n }\r\n\r\n /// Final query's result status from a requester's point of view.\r\n enum ResultStatus {\r\n Void,\r\n Awaiting,\r\n Ready,\r\n Error\r\n }\r\n\r\n /// Data struct describing an error when trying to fetch a Witnet-provided result to a Data Request.\r\n struct ResultError {\r\n ResultErrorCodes code;\r\n string reason;\r\n }\r\n\r\n enum ResultErrorCodes {\r\n /// 0x00: Unknown error. Something went really bad!\r\n Unknown, \r\n \r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// Source-specific format error sub-codes ============================================================================\r\n /// 0x01: At least one of the source scripts is not a valid CBOR-encoded value.\r\n SourceScriptNotCBOR, \r\n /// 0x02: The CBOR value decoded from a source script is not an Array.\r\n SourceScriptNotArray,\r\n /// 0x03: The Array value decoded form a source script is not a valid Data Request.\r\n SourceScriptNotRADON,\r\n /// 0x04: The request body of at least one data source was not properly formated.\r\n SourceRequestBody,\r\n /// 0x05: The request headers of at least one data source was not properly formated.\r\n SourceRequestHeaders,\r\n /// 0x06: The request URL of at least one data source was not properly formated.\r\n SourceRequestURL,\r\n /// Unallocated\r\n SourceFormat0x07, SourceFormat0x08, SourceFormat0x09, SourceFormat0x0A, SourceFormat0x0B, SourceFormat0x0C,\r\n SourceFormat0x0D, SourceFormat0x0E, SourceFormat0x0F, \r\n \r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// Complexity error sub-codes ========================================================================================\r\n /// 0x10: The request contains too many sources.\r\n RequestTooManySources,\r\n /// 0x11: The script contains too many calls.\r\n ScriptTooManyCalls,\r\n /// Unallocated\r\n Complexity0x12, Complexity0x13, Complexity0x14, Complexity0x15, Complexity0x16, Complexity0x17, Complexity0x18,\r\n Complexity0x19, Complexity0x1A, Complexity0x1B, Complexity0x1C, Complexity0x1D, Complexity0x1E, Complexity0x1F,\r\n\r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// Lack of support error sub-codes ===================================================================================\r\n /// 0x20: Some Radon operator code was found that is not supported (1+ args).\r\n UnsupportedOperator,\r\n /// 0x21: Some Radon filter opcode is not currently supported (1+ args).\r\n UnsupportedFilter,\r\n /// 0x22: Some Radon request type is not currently supported (1+ args).\r\n UnsupportedHashFunction,\r\n /// 0x23: Some Radon reducer opcode is not currently supported (1+ args)\r\n UnsupportedReducer,\r\n /// 0x24: Some Radon hash function is not currently supported (1+ args).\r\n UnsupportedRequestType, \r\n /// 0x25: Some Radon encoding function is not currently supported (1+ args).\r\n UnsupportedEncodingFunction,\r\n /// Unallocated\r\n Operator0x26, Operator0x27, \r\n /// 0x28: Wrong number (or type) of arguments were passed to some Radon operator.\r\n WrongArguments,\r\n /// Unallocated\r\n Operator0x29, Operator0x2A, Operator0x2B, Operator0x2C, Operator0x2D, Operator0x2E, Operator0x2F,\r\n \r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// Retrieve-specific circumstantial error sub-codes ================================================================================\r\n /// 0x30: A majority of data sources returned an HTTP status code other than 200 (1+ args):\r\n HttpErrors,\r\n /// 0x31: A majority of data sources timed out:\r\n RetrievalsTimeout,\r\n /// Unallocated\r\n RetrieveCircumstance0x32, RetrieveCircumstance0x33, RetrieveCircumstance0x34, RetrieveCircumstance0x35,\r\n RetrieveCircumstance0x36, RetrieveCircumstance0x37, RetrieveCircumstance0x38, RetrieveCircumstance0x39,\r\n RetrieveCircumstance0x3A, RetrieveCircumstance0x3B, RetrieveCircumstance0x3C, RetrieveCircumstance0x3D,\r\n RetrieveCircumstance0x3E, RetrieveCircumstance0x3F,\r\n \r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// Scripting-specific runtime error sub-code =========================================================================\r\n /// 0x40: Math operator caused an underflow.\r\n MathUnderflow,\r\n /// 0x41: Math operator caused an overflow.\r\n MathOverflow,\r\n /// 0x42: Math operator tried to divide by zero.\r\n MathDivisionByZero, \r\n /// 0x43:Wrong input to subscript call.\r\n WrongSubscriptInput,\r\n /// 0x44: Value cannot be extracted from input binary buffer.\r\n BufferIsNotValue,\r\n /// 0x45: Value cannot be decoded from expected type.\r\n Decode,\r\n /// 0x46: Unexpected empty array.\r\n EmptyArray,\r\n /// 0x47: Value cannot be encoded to expected type.\r\n Encode,\r\n /// 0x48: Failed to filter input values (1+ args).\r\n Filter,\r\n /// 0x49: Failed to hash input value.\r\n Hash,\r\n /// 0x4A: Mismatching array ranks.\r\n MismatchingArrays,\r\n /// 0x4B: Failed to process non-homogenous array.\r\n NonHomegeneousArray,\r\n /// 0x4C: Failed to parse syntax of some input value, or argument.\r\n Parse,\r\n /// 0x4E: Parsing logic limits were exceeded.\r\n ParseOverflow,\r\n /// 0x4F: Unallocated\r\n ScriptError0x4F,\r\n \r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// Actual first-order result error codes =============================================================================\r\n /// 0x50: Not enough reveals were received in due time:\r\n InsufficientReveals,\r\n /// 0x51: No actual reveal majority was reached on tally stage:\r\n InsufficientMajority,\r\n /// 0x52: Not enough commits were received before tally stage:\r\n InsufficientCommits,\r\n /// 0x53: Generic error during tally execution (to be deprecated after WIP #0028)\r\n TallyExecution,\r\n /// 0x54: A majority of data sources could either be temporarily unresponsive or failing to report the requested data:\r\n CircumstantialFailure,\r\n /// 0x55: At least one data source is inconsistent when queried through multiple transports at once:\r\n InconsistentSources,\r\n /// 0x56: Any one of the (multiple) Retrieve, Aggregate or Tally scripts were badly formated:\r\n MalformedDataRequest,\r\n /// 0x57: Values returned from a majority of data sources don't match the expected schema:\r\n MalformedResponses,\r\n /// Unallocated: \r\n OtherError0x58, OtherError0x59, OtherError0x5A, OtherError0x5B, OtherError0x5C, OtherError0x5D, OtherError0x5E, \r\n /// 0x5F: Size of serialized tally result exceeds allowance:\r\n OversizedTallyResult,\r\n\r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// Inter-stage runtime error sub-codes ===============================================================================\r\n /// 0x60: Data aggregation reveals could not get decoded on the tally stage:\r\n MalformedReveals,\r\n /// 0x61: The result to data aggregation could not get encoded:\r\n EncodeReveals, \r\n /// 0x62: A mode tie ocurred when calculating some mode value on the aggregation or the tally stage:\r\n ModeTie, \r\n /// Unallocated:\r\n OtherError0x63, OtherError0x64, OtherError0x65, OtherError0x66, OtherError0x67, OtherError0x68, OtherError0x69, \r\n OtherError0x6A, OtherError0x6B, OtherError0x6C, OtherError0x6D, OtherError0x6E, OtherError0x6F,\r\n \r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// Runtime access error sub-codes ====================================================================================\r\n /// 0x70: Tried to access a value from an array using an index that is out of bounds (1+ args):\r\n ArrayIndexOutOfBounds,\r\n /// 0x71: Tried to access a value from a map using a key that does not exist (1+ args):\r\n MapKeyNotFound,\r\n /// 0X72: Tried to extract value from a map using a JSON Path that returns no values (+1 args):\r\n JsonPathNotFound,\r\n /// Unallocated:\r\n OtherError0x73, OtherError0x74, OtherError0x75, OtherError0x76, OtherError0x77, OtherError0x78, \r\n OtherError0x79, OtherError0x7A, OtherError0x7B, OtherError0x7C, OtherError0x7D, OtherError0x7E, OtherError0x7F, \r\n OtherError0x80, OtherError0x81, OtherError0x82, OtherError0x83, OtherError0x84, OtherError0x85, OtherError0x86, \r\n OtherError0x87, OtherError0x88, OtherError0x89, OtherError0x8A, OtherError0x8B, OtherError0x8C, OtherError0x8D, \r\n OtherError0x8E, OtherError0x8F, OtherError0x90, OtherError0x91, OtherError0x92, OtherError0x93, OtherError0x94, \r\n OtherError0x95, OtherError0x96, OtherError0x97, OtherError0x98, OtherError0x99, OtherError0x9A, OtherError0x9B,\r\n OtherError0x9C, OtherError0x9D, OtherError0x9E, OtherError0x9F, OtherError0xA0, OtherError0xA1, OtherError0xA2, \r\n OtherError0xA3, OtherError0xA4, OtherError0xA5, OtherError0xA6, OtherError0xA7, OtherError0xA8, OtherError0xA9, \r\n OtherError0xAA, OtherError0xAB, OtherError0xAC, OtherError0xAD, OtherError0xAE, OtherError0xAF, OtherError0xB0,\r\n OtherError0xB1, OtherError0xB2, OtherError0xB3, OtherError0xB4, OtherError0xB5, OtherError0xB6, OtherError0xB7,\r\n OtherError0xB8, OtherError0xB9, OtherError0xBA, OtherError0xBB, OtherError0xBC, OtherError0xBD, OtherError0xBE,\r\n OtherError0xBF, OtherError0xC0, OtherError0xC1, OtherError0xC2, OtherError0xC3, OtherError0xC4, OtherError0xC5,\r\n OtherError0xC6, OtherError0xC7, OtherError0xC8, OtherError0xC9, OtherError0xCA, OtherError0xCB, OtherError0xCC,\r\n OtherError0xCD, OtherError0xCE, OtherError0xCF, OtherError0xD0, OtherError0xD1, OtherError0xD2, OtherError0xD3,\r\n OtherError0xD4, OtherError0xD5, OtherError0xD6, OtherError0xD7, OtherError0xD8, OtherError0xD9, OtherError0xDA,\r\n OtherError0xDB, OtherError0xDC, OtherError0xDD, OtherError0xDE, OtherError0xDF,\r\n \r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// Inter-client generic error codes ==================================================================================\r\n /// Data requests that cannot be relayed into the Witnet blockchain should be reported\r\n /// with one of these errors. \r\n /// 0xE0: Requests that cannot be parsed must always get this error as their result.\r\n BridgeMalformedDataRequest,\r\n /// 0xE1: Witnesses exceeds 100\r\n BridgePoorIncentives,\r\n /// 0xE2: The request is rejected on the grounds that it may cause the submitter to spend or stake an\r\n /// amount of value that is unjustifiably high when compared with the reward they will be getting\r\n BridgeOversizedTallyResult,\r\n \r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// Unallocated =======================================================================================================\r\n OtherError0xE3, OtherError0xE4, OtherError0xE5, OtherError0xE6, OtherError0xE7, OtherError0xE8, OtherError0xE9,\r\n OtherError0xEA, OtherError0xEB, OtherError0xEC, OtherError0xED, OtherError0xEE, OtherError0xEF, OtherError0xF0,\r\n OtherError0xF1, OtherError0xF2, OtherError0xF3, OtherError0xF4, OtherError0xF5, OtherError0xF6, OtherError0xF7,\r\n OtherError0xF8, OtherError0xF9, OtherError0xFA, OtherError0xFB, OtherError0xFC, OtherError0xFD, OtherError0xFE,\r\n \r\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\r\n /// 0xFF: Some tally error is not intercepted but it should (0+ args)\r\n UnhandledIntercept\r\n }\r\n\r\n function isCircumstantial(ResultErrorCodes self) internal pure returns (bool) {\r\n return (self == ResultErrorCodes.CircumstantialFailure);\r\n }\r\n\r\n function lackOfConsensus(ResultErrorCodes self) internal pure returns (bool) {\r\n return (\r\n self == ResultErrorCodes.InsufficientCommits\r\n || self == ResultErrorCodes.InsufficientMajority\r\n || self == ResultErrorCodes.InsufficientReveals\r\n );\r\n }\r\n\r\n function isRetriable(ResultErrorCodes self) internal pure returns (bool) {\r\n return (\r\n lackOfConsensus(self)\r\n || isCircumstantial(self)\r\n || poorIncentives(self)\r\n );\r\n }\r\n\r\n function poorIncentives(ResultErrorCodes self) internal pure returns (bool) {\r\n return (\r\n self == ResultErrorCodes.OversizedTallyResult\r\n || self == ResultErrorCodes.InsufficientCommits\r\n || self == ResultErrorCodes.BridgePoorIncentives\r\n || self == ResultErrorCodes.BridgeOversizedTallyResult\r\n );\r\n }\r\n \r\n\r\n /// Possible Radon data request methods that can be used within a Radon Retrieval. \r\n enum RadonDataRequestMethods {\r\n /* 0 */ Unknown,\r\n /* 1 */ HttpGet,\r\n /* 2 */ RNG,\r\n /* 3 */ HttpPost,\r\n /* 4 */ HttpHead\r\n }\r\n\r\n /// Possible types either processed by Witnet Radon Scripts or included within results to Witnet Data Requests.\r\n enum RadonDataTypes {\r\n /* 0x00 */ Any, \r\n /* 0x01 */ Array,\r\n /* 0x02 */ Bool,\r\n /* 0x03 */ Bytes,\r\n /* 0x04 */ Integer,\r\n /* 0x05 */ Float,\r\n /* 0x06 */ Map,\r\n /* 0x07 */ String,\r\n Unused0x08, Unused0x09, Unused0x0A, Unused0x0B,\r\n Unused0x0C, Unused0x0D, Unused0x0E, Unused0x0F,\r\n /* 0x10 */ Same,\r\n /* 0x11 */ Inner,\r\n /* 0x12 */ Match,\r\n /* 0x13 */ Subscript\r\n }\r\n\r\n /// Structure defining some data filtering that can be applied at the Aggregation or the Tally stages\r\n /// within a Witnet Data Request resolution workflow.\r\n struct RadonFilter {\r\n RadonFilterOpcodes opcode;\r\n bytes args;\r\n }\r\n\r\n /// Filtering methods currently supported on the Witnet blockchain. \r\n enum RadonFilterOpcodes {\r\n /* 0x00 */ Reserved0x00, //GreaterThan,\r\n /* 0x01 */ Reserved0x01, //LessThan,\r\n /* 0x02 */ Reserved0x02, //Equals,\r\n /* 0x03 */ Reserved0x03, //AbsoluteDeviation,\r\n /* 0x04 */ Reserved0x04, //RelativeDeviation\r\n /* 0x05 */ StandardDeviation,\r\n /* 0x06 */ Reserved0x06, //Top,\r\n /* 0x07 */ Reserved0x07, //Bottom,\r\n /* 0x08 */ Mode,\r\n /* 0x09 */ Reserved0x09 //LessOrEqualThan\r\n }\r\n\r\n /// Structure defining the array of filters and reducting function to be applied at either the Aggregation\r\n /// or the Tally stages within a Witnet Data Request resolution workflow.\r\n struct RadonReducer {\r\n RadonReducerOpcodes opcode;\r\n RadonFilter[] filters;\r\n }\r\n\r\n /// Reducting functions currently supported on the Witnet blockchain.\r\n enum RadonReducerOpcodes {\r\n /* 0x00 */ Reserved0x00, //Minimum,\r\n /* 0x01 */ Reserved0x01, //Maximum,\r\n /* 0x02 */ Mode,\r\n /* 0x03 */ AverageMean,\r\n /* 0x04 */ Reserved0x04, //AverageMeanWeighted,\r\n /* 0x05 */ AverageMedian,\r\n /* 0x06 */ Reserved0x06, //AverageMedianWeighted,\r\n /* 0x07 */ StandardDeviation,\r\n /* 0x08 */ Reserved0x08, //AverageDeviation,\r\n /* 0x09 */ Reserved0x09, //MedianDeviation,\r\n /* 0x0A */ Reserved0x10, //MaximumDeviation,\r\n /* 0x0B */ ConcatenateAndHash\r\n }\r\n\r\n /// Structure containing all the parameters that fully describe a Witnet Radon Retrieval within a Witnet Data Request.\r\n struct RadonRetrieval {\r\n uint8 argsCount;\r\n RadonDataRequestMethods method;\r\n RadonDataTypes resultDataType;\r\n string url;\r\n string body;\r\n string[2][] headers;\r\n bytes script;\r\n }\r\n\r\n /// Structure containing the Retrieve-Attestation-Delivery parts of a Witnet Data Request.\r\n struct RadonRAD {\r\n RadonRetrieval[] retrieve;\r\n RadonReducer aggregate;\r\n RadonReducer tally;\r\n }\r\n\r\n /// Structure containing the Service Level Aggreement parameters of a Witnet Data Request.\r\n struct RadonSLA {\r\n uint8 numWitnesses;\r\n uint8 minConsensusPercentage;\r\n uint64 witnessReward;\r\n uint64 witnessCollateral;\r\n uint64 minerCommitRevealFee;\r\n }\r\n\r\n\r\n /// ===============================================================================================================\r\n /// --- 'uint*' helper methods ------------------------------------------------------------------------------------\r\n\r\n /// @notice Convert a `uint8` into a 2 characters long `string` representing its two less significant hexadecimal values.\r\n function toHexString(uint8 _u)\r\n internal pure\r\n returns (string memory)\r\n {\r\n bytes memory b2 = new bytes(2);\r\n uint8 d0 = uint8(_u / 16) + 48;\r\n uint8 d1 = uint8(_u % 16) + 48;\r\n if (d0 > 57)\r\n d0 += 7;\r\n if (d1 > 57)\r\n d1 += 7;\r\n b2[0] = bytes1(d0);\r\n b2[1] = bytes1(d1);\r\n return string(b2);\r\n }\r\n\r\n /// @notice Convert a `uint8` into a 1, 2 or 3 characters long `string` representing its.\r\n /// three less significant decimal values.\r\n function toString(uint8 _u)\r\n internal pure\r\n returns (string memory)\r\n {\r\n if (_u < 10) {\r\n bytes memory b1 = new bytes(1);\r\n b1[0] = bytes1(uint8(_u) + 48);\r\n return string(b1);\r\n } else if (_u < 100) {\r\n bytes memory b2 = new bytes(2);\r\n b2[0] = bytes1(uint8(_u / 10) + 48);\r\n b2[1] = bytes1(uint8(_u % 10) + 48);\r\n return string(b2);\r\n } else {\r\n bytes memory b3 = new bytes(3);\r\n b3[0] = bytes1(uint8(_u / 100) + 48);\r\n b3[1] = bytes1(uint8(_u % 100 / 10) + 48);\r\n b3[2] = bytes1(uint8(_u % 10) + 48);\r\n return string(b3);\r\n }\r\n }\r\n\r\n /// @notice Convert a `uint` into a string` representing its value.\r\n function toString(uint v)\r\n internal pure \r\n returns (string memory)\r\n {\r\n uint maxlength = 100;\r\n bytes memory reversed = new bytes(maxlength);\r\n uint i = 0;\r\n do {\r\n uint8 remainder = uint8(v % 10);\r\n v = v / 10;\r\n reversed[i ++] = bytes1(48 + remainder);\r\n } while (v != 0);\r\n bytes memory buf = new bytes(i);\r\n for (uint j = 1; j <= i; j ++) {\r\n buf[j - 1] = reversed[i - j];\r\n }\r\n return string(buf);\r\n }\r\n\r\n\r\n /// ===============================================================================================================\r\n /// --- 'bytes' helper methods ------------------------------------------------------------------------------------\r\n\r\n /// @dev Transform given bytes into a Witnet.Result instance.\r\n /// @param cborBytes Raw bytes representing a CBOR-encoded value.\r\n /// @return A `Witnet.Result` instance.\r\n function toWitnetResult(bytes memory cborBytes)\r\n internal pure\r\n returns (Witnet.Result memory)\r\n {\r\n WitnetCBOR.CBOR memory cborValue = WitnetCBOR.fromBytes(cborBytes);\r\n return _resultFromCborValue(cborValue);\r\n }\r\n\r\n function toAddress(bytes memory _value) internal pure returns (address) {\r\n return address(toBytes20(_value));\r\n }\r\n\r\n function toBytes4(bytes memory _value) internal pure returns (bytes4) {\r\n return bytes4(toFixedBytes(_value, 4));\r\n }\r\n \r\n function toBytes20(bytes memory _value) internal pure returns (bytes20) {\r\n return bytes20(toFixedBytes(_value, 20));\r\n }\r\n \r\n function toBytes32(bytes memory _value) internal pure returns (bytes32) {\r\n return toFixedBytes(_value, 32);\r\n }\r\n\r\n function toFixedBytes(bytes memory _value, uint8 _numBytes)\r\n internal pure\r\n returns (bytes32 _bytes32)\r\n {\r\n assert(_numBytes <= 32);\r\n unchecked {\r\n uint _len = _value.length > _numBytes ? _numBytes : _value.length;\r\n for (uint _i = 0; _i < _len; _i ++) {\r\n _bytes32 |= bytes32(_value[_i] & 0xff) >> (_i * 8);\r\n }\r\n }\r\n }\r\n\r\n\r\n /// ===============================================================================================================\r\n /// --- 'string' helper methods -----------------------------------------------------------------------------------\r\n\r\n function toLowerCase(string memory str)\r\n internal pure\r\n returns (string memory)\r\n {\r\n bytes memory lowered = new bytes(bytes(str).length);\r\n unchecked {\r\n for (uint i = 0; i < lowered.length; i ++) {\r\n uint8 char = uint8(bytes(str)[i]);\r\n if (char >= 65 && char <= 90) {\r\n lowered[i] = bytes1(char + 32);\r\n } else {\r\n lowered[i] = bytes1(char);\r\n }\r\n }\r\n }\r\n return string(lowered);\r\n }\r\n\r\n /// @notice Converts bytes32 into string.\r\n function toString(bytes32 _bytes32)\r\n internal pure\r\n returns (string memory)\r\n {\r\n bytes memory _bytes = new bytes(_toStringLength(_bytes32));\r\n for (uint _i = 0; _i < _bytes.length;) {\r\n _bytes[_i] = _bytes32[_i];\r\n unchecked {\r\n _i ++;\r\n }\r\n }\r\n return string(_bytes);\r\n }\r\n\r\n function tryUint(string memory str)\r\n internal pure\r\n returns (uint res, bool)\r\n {\r\n unchecked {\r\n for (uint256 i = 0; i < bytes(str).length; i++) {\r\n if (\r\n (uint8(bytes(str)[i]) - 48) < 0\r\n || (uint8(bytes(str)[i]) - 48) > 9\r\n ) {\r\n return (0, false);\r\n }\r\n res += (uint8(bytes(str)[i]) - 48) * 10 ** (bytes(str).length - i - 1);\r\n }\r\n return (res, true);\r\n }\r\n }\r\n \r\n\r\n /// ===============================================================================================================\r\n /// --- 'Witnet.Result' helper methods ----------------------------------------------------------------------------\r\n\r\n modifier _isReady(Result memory result) {\r\n require(result.success, \"Witnet: tried to decode value from errored result.\");\r\n _;\r\n }\r\n\r\n /// @dev Decode an address from the Witnet.Result's CBOR value.\r\n function asAddress(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (address)\r\n {\r\n if (result.value.majorType == uint8(WitnetCBOR.MAJOR_TYPE_BYTES)) {\r\n return toAddress(result.value.readBytes());\r\n } else {\r\n // TODO\r\n revert(\"WitnetLib: reading address from string not yet supported.\");\r\n }\r\n }\r\n\r\n /// @dev Decode a `bool` value from the Witnet.Result's CBOR value.\r\n function asBool(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (bool)\r\n {\r\n return result.value.readBool();\r\n }\r\n\r\n /// @dev Decode a `bytes` value from the Witnet.Result's CBOR value.\r\n function asBytes(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns(bytes memory)\r\n {\r\n return result.value.readBytes();\r\n }\r\n\r\n /// @dev Decode a `bytes4` value from the Witnet.Result's CBOR value.\r\n function asBytes4(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (bytes4)\r\n {\r\n return toBytes4(asBytes(result));\r\n }\r\n\r\n /// @dev Decode a `bytes32` value from the Witnet.Result's CBOR value.\r\n function asBytes32(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (bytes32)\r\n {\r\n return toBytes32(asBytes(result));\r\n }\r\n\r\n /// @notice Returns the Witnet.Result's unread CBOR value.\r\n function asCborValue(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (WitnetCBOR.CBOR memory)\r\n {\r\n return result.value;\r\n }\r\n\r\n /// @notice Decode array of CBOR values from the Witnet.Result's CBOR value. \r\n function asCborArray(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (WitnetCBOR.CBOR[] memory)\r\n {\r\n return result.value.readArray();\r\n }\r\n\r\n /// @dev Decode a fixed16 (half-precision) numeric value from the Witnet.Result's CBOR value.\r\n /// @dev Due to the lack of support for floating or fixed point arithmetic in the EVM, this method offsets all values.\r\n /// by 5 decimal orders so as to get a fixed precision of 5 decimal positions, which should be OK for most `fixed16`.\r\n /// use cases. In other words, the output of this method is 10,000 times the actual value, encoded into an `int32`.\r\n function asFixed16(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (int32)\r\n {\r\n return result.value.readFloat16();\r\n }\r\n\r\n /// @dev Decode an array of fixed16 values from the Witnet.Result's CBOR value.\r\n function asFixed16Array(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (int32[] memory)\r\n {\r\n return result.value.readFloat16Array();\r\n }\r\n\r\n /// @dev Decode an `int64` value from the Witnet.Result's CBOR value.\r\n function asInt(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (int)\r\n {\r\n return result.value.readInt();\r\n }\r\n\r\n /// @dev Decode an array of integer numeric values from a Witnet.Result as an `int[]` array.\r\n /// @param result An instance of Witnet.Result.\r\n /// @return The `int[]` decoded from the Witnet.Result.\r\n function asIntArray(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (int[] memory)\r\n {\r\n return result.value.readIntArray();\r\n }\r\n\r\n /// @dev Decode a `string` value from the Witnet.Result's CBOR value.\r\n /// @param result An instance of Witnet.Result.\r\n /// @return The `string` decoded from the Witnet.Result.\r\n function asText(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns(string memory)\r\n {\r\n return result.value.readString();\r\n }\r\n\r\n /// @dev Decode an array of strings from the Witnet.Result's CBOR value.\r\n /// @param result An instance of Witnet.Result.\r\n /// @return The `string[]` decoded from the Witnet.Result.\r\n function asTextArray(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (string[] memory)\r\n {\r\n return result.value.readStringArray();\r\n }\r\n\r\n /// @dev Decode a `uint64` value from the Witnet.Result's CBOR value.\r\n /// @param result An instance of Witnet.Result.\r\n /// @return The `uint` decoded from the Witnet.Result.\r\n function asUint(Witnet.Result memory result)\r\n internal pure\r\n _isReady(result)\r\n returns (uint)\r\n {\r\n return result.value.readUint();\r\n }\r\n\r\n /// @dev Decode an array of `uint64` values from the Witnet.Result's CBOR value.\r\n /// @param result An instance of Witnet.Result.\r\n /// @return The `uint[]` decoded from the Witnet.Result.\r\n function asUintArray(Witnet.Result memory result)\r\n internal pure\r\n returns (uint[] memory)\r\n {\r\n return result.value.readUintArray();\r\n }\r\n\r\n\r\n /// ===============================================================================================================\r\n /// --- Witnet library private methods ----------------------------------------------------------------------------\r\n\r\n /// @dev Decode a CBOR value into a Witnet.Result instance.\r\n function _resultFromCborValue(WitnetCBOR.CBOR memory cbor)\r\n private pure\r\n returns (Witnet.Result memory) \r\n {\r\n // Witnet uses CBOR tag 39 to represent RADON error code identifiers.\r\n // [CBOR tag 39] Identifiers for CBOR: https://github.com/lucas-clemente/cbor-specs/blob/master/id.md\r\n bool success = cbor.tag != 39;\r\n return Witnet.Result(success, cbor);\r\n }\r\n\r\n /// @dev Calculate length of string-equivalent to given bytes32.\r\n function _toStringLength(bytes32 _bytes32)\r\n private pure\r\n returns (uint _length)\r\n {\r\n for (; _length < 32; ) {\r\n if (_bytes32[_length] == 0) {\r\n break;\r\n }\r\n unchecked {\r\n _length ++;\r\n }\r\n }\r\n }\r\n}",
"sourcePath": "C:\\Users\\guill\\github\\witnet\\witnet-solidity-bridge\\contracts\\libs\\Witnet.sol",
"ast": {
"absolutePath": "project:/contracts/libs/Witnet.sol",
"exportedSymbols": {
"Witnet": [
17557
],
"WitnetBuffer": [
19191
],
"WitnetCBOR": [
20734
]
},
"id": 17558,
"license": "MIT",
"nodeType": "SourceUnit",
"nodes": [
{
"id": 15982,
"literals": [
"solidity",
">=",
"0.7",
".0",
"<",
"0.9",
".0"
],
"nodeType": "PragmaDirective",
"src": "35:31:64"
},
{
"id": 15983,
"literals": [
"experimental",
"ABIEncoderV2"
],
"nodeType": "PragmaDirective",
"src": "68:33:64"
},
{
"absolutePath": "project:/contracts/libs/WitnetCBOR.sol",
"file": "./WitnetCBOR.sol",
"id": 15984,
"nameLocation": "-1:-1:-1",
"nodeType": "ImportDirective",
"scope": 17558,
"sourceUnit": 20735,
"src": "105:26:64",
"symbolAliases": [],
"unitAlias": ""
},
{
"abstract": false,
"baseContracts": [],
"canonicalName": "Witnet",
"contractDependencies": [],
"contractKind": "library",
"fullyImplemented": true,
"id": 17557,
"linearizedBaseContracts": [
17557
],
"name": "Witnet",
"nameLocation": "143:6:64",
"nodeType": "ContractDefinition",
"nodes": [
{
"global": false,
"id": 15988,
"libraryName": {
"id": 15985,
"name": "WitnetBuffer",
"nameLocations": [
"165:12:64"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 19191,
"src": "165:12:64"
},
"nodeType": "UsingForDirective",
"src": "159:43:64",
"typeName": {
"id": 15987,
"nodeType": "UserDefinedTypeName",
"pathNode": {
"id": 15986,
"name": "WitnetBuffer.Buffer",
"nameLocations": [
"182:12:64",
"195:6:64"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 17580,
"src": "182:19:64"
},
"referencedDeclaration": 17580,
"src": "182:19:64",
"typeDescriptions": {
"typeIdentifier": "t_struct$_Buffer_$17580_storage_ptr",
"typeString": "struct WitnetBuffer.Buffer"
}
}
},
{
"global": false,
"id": 15992,
"libraryName": {
"id": 15989,
"name": "WitnetCBOR",
"nameLocations": [
"214:10:64"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 20734,
"src": "214:10:64"
},
"nodeType": "UsingForDirective",
"src": "208:37:64",
"typeName": {
"id": 15991,
"nodeType": "UserDefinedTypeName",
"pathNode": {
"id": 15990,
"name": "WitnetCBOR.CBOR",
"nameLocations": [
"229:10:64",
"240:4:64"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 19218,
"src": "229:15:64"
},
"referencedDeclaration": 19218,
"src": "229:15:64",
"typeDescriptions": {
"typeIdentifier": "t_struct$_CBOR_$19218_storage_ptr",
"typeString": "struct WitnetCBOR.CBOR"
}
}
},
{
"global": false,
"id": 15997,
"libraryName": {
"id": 15993,
"name": "WitnetCBOR",
"nameLocations": [
"257:10:64"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 20734,
"src": "257:10:64"
},
"nodeType": "UsingForDirective",
"src": "251:39:64",
"typeName": {
"baseType": {
"id": 15995,
"nodeType": "UserDefinedTypeName",
"pathNode": {
"id": 15994,
"name": "WitnetCBOR.CBOR",
"nameLocations": [
"272:10:64",
"283:4:64"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 19218,
"src": "272:15:64"
},
"referencedDeclaration": 19218,
"src": "272:15:64",
"typeDescriptions": {
"typeIdentifier": "t_struct$_CBOR_$19218_storage_ptr",
"typeString": "struct WitnetCBOR.CBOR"
}
},
"id": 15996,
"nodeType": "ArrayTypeName",
"src": "272:17:64",
"typeDescriptions": {
"typeIdentifier": "t_array$_t_struct$_CBOR_$19218_storage_$dyn_storage_ptr",
"typeString": "struct WitnetCBOR.CBOR[]"
}
}
},
{
"canonicalName": "Witnet.Query",
"documentation": {
"id": 15998,
"nodeType": "StructuredDocumentation",
"src": "298:110:64",
"text": "Struct containing both request and response data related to every query posted to the Witnet Request Board"
},
"id": 16007,
"members": [
{
"constant": false,
"id": 16001,
"mutability": "mutable",
"name": "request",
"nameLocation": "446:7:64",
"nodeType": "VariableDeclaration",
"scope": 16007,
"src": "438:15:64",
"stateVariable": false,
"storageLocation": "default",
"typeDescriptions": {
"typeIdentifier": "t_struct$_Request_$16025_storage_ptr",
"typeString": "struct Witnet.Request"
},
"typeName": {
"id": 16000,
"nodeType": "UserDefinedTypeName",
"pathNode": {
"id": 15999,
"name": "Request",
"nameLocations": [
"438:7:64"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 16025,
"src": "438:7:64"
},
"referencedDeclaration": 16025,
"src": "438:7:64",
"typeDescriptions": {
"typeIdentifier": "t_struct$_Request_$16025_storage_ptr",
"typeString": "struct Witnet.Request"
}
},
"visibility": "internal"
},
{
"constant": false,
"id": 16004,
"mutability": "mutable",
"name": "response",
"nameLocation": "473:8:64",
"nodeType": "VariableDeclaration",
"scope": 16007,
"src": "464:17:64",
"stateVariable": false,
"storageLocation": "default",
"typeDescriptions": {
"typeIdentifier": "t_struct$_Response_$16035_storage_ptr",
"typeString": "struct Witnet.Response"
},
"typeName": {
"id": 16003,
"nodeType": "UserDefinedTypeName",
"pathNode": {
"id": 16002,
"name": "Response",
"nameLocations": [
"464:8:64"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 16035,
"src": "464:8:64"
},
"referencedDeclaration": 16035,
"src": "464:8:64",
"typeDescriptions": {
"typeIdentifier": "t_struct$_Response_$16035_storage_ptr",
"typeString": "struct Witnet.Response"
}
},
"visibility": "internal"
},
{
"constant": false,
"id": 16006,
"mutability": "mutable",
"name": "from",
"nameLocation": "500:4:64",
"nodeType": "VariableDeclaration",
"scope": 16007,
"src": "492:12:64",
"stateVariable": false,
"storageLocation": "default",
"typeDescriptions": {
"typeIdentifier": "t_address",
"typeString": "address"
},
"typeName": {
"id": 16005,
"name": "address",
"nodeType": "ElementaryTypeName",
"src": "492:7:64",
"stateMutability": "nonpayable",
"typeDescriptions": {
"typeIdentifier": "t_address",
"typeString": "address"
}
},
"visibility": "internal"
}
],
"name": "Query",
"nameLocation": "421:5:64",
"nodeType": "StructDefinition",
"scope": 17557,
"src": "414:149:64",
"visibility": "public"
},
{
"canonicalName": "Witnet.QueryStatus",
"documentation": {
"id": 16008,
"nodeType": "StructuredDocumentation",
"src": "571:38:64",
"text": "Possible status of a Witnet query."
},
"id": 16013,
"members": [
{
"id": 16009,
"name": "Unknown",
"nameLocation": "643:7:64",
"nodeType": "EnumValue",
"src": "643:7:64"
},
{
"id": 16010,
"name": "Posted",
"nameLocation": "661:6:64",
"nodeType": "EnumValue",
"src": "661:6:64"
},
{
"id": 16011,
"name": "Reported",
"nameLocation": "678:8:64",
"nodeType": "EnumValue",
"src": "678:8:64"
},
{
"id": 16012,
"name": "Deleted",
"nameLocation": "697:7:64",
"nodeType": "EnumValue",
"src": "697:7:64"
}
],
"name": "QueryStatus",
"nameLocation": "620:11:64",
"nodeType": "EnumDefinition",
"src": "615:96:64"
},
{
"canonicalName": "Witnet.Request",
"documentation": {
"id": 16014,
"nodeType": "StructuredDocumentation",
"src": "719:82:64",
"text": "Data kept in EVM-storage for every Request posted to the Witnet Request Board."
},
"id": 16025,
"members": [
{
"constant": false,
"id": 16016,
"mutability": "mutable",
"name": "addr",
"nameLocation": "841:4:64",
"nodeType": "VariableDeclaration",
"scope": 16025,
"src": "833:12:64",
"stateVariable": false,
"storageLocation": "default",
"typeDescriptions": {
"typeIdentifier": "t_address",
"typeString": "address"
},
"typeName": {
"id": 16015,
"name": "address",
"nodeType": "ElementaryTypeName",
"src": "833:7:64",
"stateMutability": "nonpayable",
"typeDescriptions": {
"typeIdentifier": "t_address",
"typeString": "address"
}
},
"visibility": "internal"
},
{
"constant": false,
"id": 16018,
"mutability": "mutable",
"name": "slaHash",
"nameLocation": "970:7:64",
"nodeType": "VariableDeclaration",
"scope": 16025,
"src": "962:15:64",
"stateVariable": false,
"storageLocation": "default",
"typeDescriptions": {
"typeIdentifier": "t_bytes32",
"typeString": "bytes32"
},
"typeName": {
"id": 16017,
"name": "bytes32",
"nodeType": "ElementaryTypeName",
"src": "962:7:64",
"typeDescriptions": {
"typeIdentifier": "t_bytes32",
"typeString": "bytes32"
}
},
"visibility": "internal"
},
{
"constant": false,
"id": 16020,
"mutability": "mutable",
"name": "radHash",
"nameLocation": "1045:7:64",
"nodeType": "VariableDeclaration",
"scope": 16025,
"src": "1037:15:64",
"stateVariable": false,
"storageLocation": "default",
"typeDescriptions": {
"typeIdentifier": "t_bytes32",
"typeString": "bytes32"