Source Code
Overview
ETH Balance
0 ETH
Token Holdings
More Info
ContractCreator
Multichain Info
N/A
Latest 1 from a total of 1 transactions
Transaction Hash |
Method
|
Block
|
From
|
To
|
|||||
---|---|---|---|---|---|---|---|---|---|
Calculate Fibona... | 3964639 | 538 days ago | IN | 0 ETH | 0.00008659 |
View more zero value Internal Transactions in Advanced View mode
Advanced mode:
Loading...
Loading
Contract Name:
BonsaiStarter
Compiler Version
v0.8.20+commit.a1b79de6
Contract Source Code (Solidity Standard Json-Input format)
// Copyright 2023 RISC Zero, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.17; import {IBonsaiRelay} from "bonsai/IBonsaiRelay.sol"; import {BonsaiCallbackReceiver} from "bonsai/BonsaiCallbackReceiver.sol"; /// @title A starter application using Bonsai through the on-chain relay. /// @dev This contract demonstrates one pattern for offloading the computation of an expensive // or difficult to implement function to a RISC Zero guest running on Bonsai. contract BonsaiStarter is BonsaiCallbackReceiver { /// @notice Cache of the results calculated by our guest program in Bonsai. /// @dev Using a cache is one way to handle the callback from Bonsai. Upon callback, the /// information from the journal is stored in the cache for later use by the contract. mapping(uint256 => uint256) public fibonacciCache; /// @notice Image ID of the only zkVM binary to accept callbacks from. bytes32 public immutable fibImageId; /// @notice Gas limit set on the callback from Bonsai. /// @dev Should be set to the maximum amount of gas your callback might reasonably consume. uint64 private constant BONSAI_CALLBACK_GAS_LIMIT = 100000; /// @notice Initialize the contract, binding it to a specified Bonsai relay and RISC Zero guest image. constructor(IBonsaiRelay bonsaiRelay, bytes32 _fibImageId) BonsaiCallbackReceiver(bonsaiRelay) { fibImageId = _fibImageId; } event CalculateFibonacciCallback(uint256 indexed n, uint256 result); /// @notice Returns nth number in the Fibonacci sequence. /// @dev The sequence is defined as 1, 1, 2, 3, 5 ... with fibonacci(0) == 1. /// Only precomputed results can be returned. Call calculate_fibonacci(n) to precompute. function fibonacci(uint256 n) external view returns (uint256) { uint256 result = fibonacciCache[n]; require(result != 0, "value not available in cache"); return result; } /// @notice Callback function logic for processing verified journals from Bonsai. function storeResult(uint256 n, uint256 result) external onlyBonsaiCallback(fibImageId) { emit CalculateFibonacciCallback(n, result); fibonacciCache[n] = result; } /// @notice Sends a request to Bonsai to have have the nth Fibonacci number calculated. /// @dev This function sends the request to Bonsai through the on-chain relay. /// The request will trigger Bonsai to run the specified RISC Zero guest program with /// the given input and asynchronously return the verified results via the callback below. function calculateFibonacci(uint256 n) external { bonsaiRelay.requestCallback( fibImageId, abi.encode(n), address(this), this.storeResult.selector, BONSAI_CALLBACK_GAS_LIMIT ); } }
// Copyright 2023 RISC Zero, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.17; /// @notice Data required to authorize a callback to be sent through the relay. struct CallbackAuthorization { /// @notice SNARK proof acting as the cryptographic seal over the execution results. bytes seal; /// @notice Digest of the zkVM SystemState after execution. /// @dev The relay does not additionally check any property of this digest, but needs the /// digest in order to reconstruct the ReceiptMetadata hash to which the proof is linked. bytes32 postStateDigest; } /// @notice Callback data, provided by the Relay service. struct Callback { CallbackAuthorization auth; /// @notice address of the contract to receive the callback. address callbackContract; /// @notice payload containing the callback function selector, journal bytes, and image ID. /// @dev payload is destructured and checked against the authorization data to ensure that /// the journal is a valid execution result of the zkVM guest defined by the image ID. /// The payload is then used directly as the calldata for the callback. bytes payload; /// @notice maximum amount of gas the callback function may use. uint64 gasLimit; } /// @notice The interface for the Bonsai relay contract interface IBonsaiRelay { /// @notice Event emitted upon rceiving a callback request through requestCallback. event CallbackRequest( address account, bytes32 imageId, bytes input, address callbackContract, bytes4 functionSelector, uint64 gasLimit ); /// @notice Submit request to receive a callback. /// @dev This function will usually be called be the Bonsai user's application contract, and /// will log an event that the Bonsai Relay will detect and respond to. function requestCallback( bytes32 imageId, bytes calldata input, address callbackContract, bytes4 functionSelector, uint64 gasLimit ) external; /// @notice Determines if the given athorization is valid for the image ID and journal. /// @dev A (imageId, jounral) pair should be valid, and the respective callback authorized, if /// and only if the journal is the result of the correct execution of the zkVM guest. function callbackIsAuthorized(bytes32 imageId, bytes calldata journal, CallbackAuthorization calldata auth) external view returns (bool); /// @notice Submit a batch of callbacks, authorized by an attached SNARK proof. /// @dev This function is usually called by the Bonsai Relay. Note that this function does not /// revert when one of the inner callbacks reverts. /// @return invocationResults a list of booleans indicated if the calldata succeeded or failed. function invokeCallbacks(Callback[] calldata callbacks) external returns (bool[] memory invocationResults); /// @notice Submit a single callback, authorized by an attached SNARK proof. /// @dev This function is usually called by the Bonsai Relay. This function reverts if the callback fails. function invokeCallback(Callback calldata callback) external; }
// Copyright 2023 RISC Zero, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // SPDX-License-Identifier: Apache-2.0 import {IBonsaiRelay} from "./IBonsaiRelay.sol"; pragma solidity ^0.8.17; /// @notice A base contract for writing a Bonsai app abstract contract BonsaiCallbackReceiver { // Address of the Bonsai relay contract. IBonsaiRelay public immutable bonsaiRelay; // Unexpected image id error error UnexpectedImageId(bytes32 expected, bytes32 found); // Invalid callback source error error UnauthorizedCallbackSource(IBonsaiRelay expected, IBonsaiRelay found); /// @notice Initialize the contract, binding it to a specified Bonsai relay contract constructor(IBonsaiRelay _bonsaiRelay) { bonsaiRelay = _bonsaiRelay; } /// @notice Verify that the call came from the Bonsai relay contract function _verifyMessageSource() internal view { IBonsaiRelay foundRelayAddress = IBonsaiRelay(msg.sender); if (foundRelayAddress != bonsaiRelay) { revert UnauthorizedCallbackSource(bonsaiRelay, foundRelayAddress); } } /// @notice Verify that journal comes from the expected image id function _verifyCallbackImageId(bytes32 expectedImageId) internal pure { bytes32 foundImageId = bytes32(msg.data[msg.data.length - 32:]); if (expectedImageId != foundImageId) { revert UnexpectedImageId(expectedImageId, foundImageId); } } /// @notice Verify this is a callback by the relay contract using a journal from the expected image id modifier onlyBonsaiCallback(bytes32 expectedImageId) { _verifyMessageSource(); _verifyCallbackImageId(expectedImageId); // Pass validations and continue _; } }
{ "remappings": [ "bonsai/=lib/risc0/bonsai/ethereum/contracts/", "ds-test/=lib/forge-std/lib/ds-test/src/", "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/", "forge-std/=lib/forge-std/src/", "murky/=lib/murky/src/", "murky_differential_testing/=lib/murky/differential_testing/", "openzeppelin-contracts/=lib/openzeppelin-contracts/", "openzeppelin/=lib/openzeppelin-contracts/", "risc0/=lib/risc0/", "solidity-bytes-utils/=lib/solidity-bytes-utils/contracts/" ], "optimizer": { "enabled": true, "runs": 200 }, "metadata": { "bytecodeHash": "ipfs", "appendCBOR": true }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "abi" ] } }, "evmVersion": "paris", "libraries": {} }
[{"inputs":[{"internalType":"contract IBonsaiRelay","name":"bonsaiRelay","type":"address"},{"internalType":"bytes32","name":"_fibImageId","type":"bytes32"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"contract IBonsaiRelay","name":"expected","type":"address"},{"internalType":"contract IBonsaiRelay","name":"found","type":"address"}],"name":"UnauthorizedCallbackSource","type":"error"},{"inputs":[{"internalType":"bytes32","name":"expected","type":"bytes32"},{"internalType":"bytes32","name":"found","type":"bytes32"}],"name":"UnexpectedImageId","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"n","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"result","type":"uint256"}],"name":"CalculateFibonacciCallback","type":"event"},{"inputs":[],"name":"bonsaiRelay","outputs":[{"internalType":"contract IBonsaiRelay","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"n","type":"uint256"}],"name":"calculateFibonacci","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"fibImageId","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"n","type":"uint256"}],"name":"fibonacci","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"fibonacciCache","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"n","type":"uint256"},{"internalType":"uint256","name":"result","type":"uint256"}],"name":"storeResult","outputs":[],"stateMutability":"nonpayable","type":"function"}]
Contract Creation Code
60c060405234801561001057600080fd5b506040516105fd3803806105fd83398101604081905261002f91610045565b6001600160a01b0390911660805260a05261007f565b6000806040838503121561005857600080fd5b82516001600160a01b038116811461006f57600080fd5b6020939093015192949293505050565b60805160a0516105376100c660003960008181606c015281816101d5015261027d015260008181610101015281816101a601528181610305015261034201526105376000f3fe608060405234801561001057600080fd5b50600436106100625760003560e01c806313234edc1461006757806361047ff4146100a15780636b370a4c146100b457806399119819146100c95780639f2275c0146100e9578063e70ffd4b146100fc575b600080fd5b61008e7f000000000000000000000000000000000000000000000000000000000000000081565b6040519081526020015b60405180910390f35b61008e6100af3660046103cd565b61013b565b6100c76100c23660046103cd565b6101a4565b005b61008e6100d73660046103cd565b60006020819052908152604090205481565b6100c76100f73660046103e6565b61027b565b6101237f000000000000000000000000000000000000000000000000000000000000000081565b6040516001600160a01b039091168152602001610098565b60008181526020819052604081205480820361019e5760405162461bcd60e51b815260206004820152601c60248201527f76616c7565206e6f7420617661696c61626c6520696e2063616368650000000060448201526064015b60405180910390fd5b92915050565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663e80802a27f00000000000000000000000000000000000000000000000000000000000000008360405160200161020791815260200190565b60408051601f19818403018152908290526001600160e01b031960e085901b1682526102469291309063027c89d760e61b90620186a090600401610408565b600060405180830381600087803b15801561026057600080fd5b505af1158015610274573d6000803e3d6000fd5b5050505050565b7f00000000000000000000000000000000000000000000000000000000000000006102a46102fa565b6102ad8161037a565b827fa457fb5f6917695b86f89831bb36063cb21e8d2f4d103023fbbdb5872b911a91836040516102df91815260200190565b60405180910390a25060009182526020829052604090912055565b336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001681146103775760405163432e033760e11b81526001600160a01b037f00000000000000000000000000000000000000000000000000000000000000008116600483015282166024820152604401610195565b50565b60008036610389602082610498565b6103949282906104b9565b61039d916104e3565b90508082146103c957604051630be5472b60e41b81526004810183905260248101829052604401610195565b5050565b6000602082840312156103df57600080fd5b5035919050565b600080604083850312156103f957600080fd5b50508035926020909101359150565b8581526000602060a08184015286518060a085015260005b8181101561043c5788810183015185820160c001528201610420565b50600060c08286018101919091526001600160a01b03881660408601526001600160e01b031987166060860152601f909101601f1916840101915061047e9050565b67ffffffffffffffff831660808301529695505050505050565b8181038181111561019e57634e487b7160e01b600052601160045260246000fd5b600080858511156104c957600080fd5b838611156104d657600080fd5b5050820193919092039150565b8035602083101561019e57600019602084900360031b1b169291505056fea2646970667358221220332da4c07327bf2f5eece29665bd0006af600c0aad07c60f18eb51af86d64d8c64736f6c63430008140033000000000000000000000000bc4bcb24c7e4a15389d538059c7c3910d6c2b534809634da9360ea63877c054e9bdd4bff08db74872c871cdfeb4e81bbebfa9df2
Deployed Bytecode
0x608060405234801561001057600080fd5b50600436106100625760003560e01c806313234edc1461006757806361047ff4146100a15780636b370a4c146100b457806399119819146100c95780639f2275c0146100e9578063e70ffd4b146100fc575b600080fd5b61008e7f809634da9360ea63877c054e9bdd4bff08db74872c871cdfeb4e81bbebfa9df281565b6040519081526020015b60405180910390f35b61008e6100af3660046103cd565b61013b565b6100c76100c23660046103cd565b6101a4565b005b61008e6100d73660046103cd565b60006020819052908152604090205481565b6100c76100f73660046103e6565b61027b565b6101237f000000000000000000000000bc4bcb24c7e4a15389d538059c7c3910d6c2b53481565b6040516001600160a01b039091168152602001610098565b60008181526020819052604081205480820361019e5760405162461bcd60e51b815260206004820152601c60248201527f76616c7565206e6f7420617661696c61626c6520696e2063616368650000000060448201526064015b60405180910390fd5b92915050565b7f000000000000000000000000bc4bcb24c7e4a15389d538059c7c3910d6c2b5346001600160a01b031663e80802a27f809634da9360ea63877c054e9bdd4bff08db74872c871cdfeb4e81bbebfa9df28360405160200161020791815260200190565b60408051601f19818403018152908290526001600160e01b031960e085901b1682526102469291309063027c89d760e61b90620186a090600401610408565b600060405180830381600087803b15801561026057600080fd5b505af1158015610274573d6000803e3d6000fd5b5050505050565b7f809634da9360ea63877c054e9bdd4bff08db74872c871cdfeb4e81bbebfa9df26102a46102fa565b6102ad8161037a565b827fa457fb5f6917695b86f89831bb36063cb21e8d2f4d103023fbbdb5872b911a91836040516102df91815260200190565b60405180910390a25060009182526020829052604090912055565b336001600160a01b037f000000000000000000000000bc4bcb24c7e4a15389d538059c7c3910d6c2b5341681146103775760405163432e033760e11b81526001600160a01b037f000000000000000000000000bc4bcb24c7e4a15389d538059c7c3910d6c2b5348116600483015282166024820152604401610195565b50565b60008036610389602082610498565b6103949282906104b9565b61039d916104e3565b90508082146103c957604051630be5472b60e41b81526004810183905260248101829052604401610195565b5050565b6000602082840312156103df57600080fd5b5035919050565b600080604083850312156103f957600080fd5b50508035926020909101359150565b8581526000602060a08184015286518060a085015260005b8181101561043c5788810183015185820160c001528201610420565b50600060c08286018101919091526001600160a01b03881660408601526001600160e01b031987166060860152601f909101601f1916840101915061047e9050565b67ffffffffffffffff831660808301529695505050505050565b8181038181111561019e57634e487b7160e01b600052601160045260246000fd5b600080858511156104c957600080fd5b838611156104d657600080fd5b5050820193919092039150565b8035602083101561019e57600019602084900360031b1b169291505056fea2646970667358221220332da4c07327bf2f5eece29665bd0006af600c0aad07c60f18eb51af86d64d8c64736f6c63430008140033
Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)
000000000000000000000000bc4bcb24c7e4a15389d538059c7c3910d6c2b534809634da9360ea63877c054e9bdd4bff08db74872c871cdfeb4e81bbebfa9df2
-----Decoded View---------------
Arg [0] : bonsaiRelay (address): 0xbc4bcb24c7e4A15389D538059C7C3910d6c2b534
Arg [1] : _fibImageId (bytes32): 0x809634da9360ea63877c054e9bdd4bff08db74872c871cdfeb4e81bbebfa9df2
-----Encoded View---------------
2 Constructor Arguments found :
Arg [0] : 000000000000000000000000bc4bcb24c7e4a15389d538059c7c3910d6c2b534
Arg [1] : 809634da9360ea63877c054e9bdd4bff08db74872c871cdfeb4e81bbebfa9df2
Deployed Bytecode Sourcemap
1043:2343:0:-:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1498:35;;;;;;;;160:25:3;;;148:2;133:18;1498:35:0;;;;;;;;2325:198;;;;;;:::i;:::-;;:::i;3174:210::-;;;;;;:::i;:::-;;:::i;:::-;;1367:49;;;;;;:::i;:::-;;;;;;;;;;;;;;;2615:183;;;;;;:::i;:::-;;:::i;856:41:1:-;;;;;;;;-1:-1:-1;;;;;1000:32:3;;;982:51;;970:2;955:18;856:41:1;816:223:3;2325:198:0;2378:7;2414:17;;;;;;;;;;;2449:11;;;2441:52;;;;-1:-1:-1;;;2441:52:0;;1246:2:3;2441:52:0;;;1228:21:3;1285:2;1265:18;;;1258:30;1324;1304:18;;;1297:58;1372:18;;2441:52:0;;;;;;;;;2510:6;2325:198;-1:-1:-1;;2325:198:0:o;3174:210::-;3232:11;-1:-1:-1;;;;;3232:27:0;;3273:10;3296:1;3285:13;;;;;;160:25:3;;148:2;133:18;;14:177;3285:13:0;;;;-1:-1:-1;;3285:13:0;;;;;;;;;;-1:-1:-1;;;;;;3315:25:0;3232:145;;;;;;;;;3308:4;;-1:-1:-1;;;3315:25:0;1747:6;;3232:145;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;3174:210;:::o;2615:183::-;2691:10;2154:22:1;:20;:22::i;:::-;2186:39;2209:15;2186:22;:39::i;:::-;2745:1:0::1;2718:37;2748:6;2718:37;;;;160:25:3::0;;148:2;133:18;;14:177;2718:37:0::1;;;;;;;;-1:-1:-1::0;2765:14:0::1;:17:::0;;;::::1;::::0;;;;;;;:26;2615:183::o;1369:257:1:-;1471:10;-1:-1:-1;;;;;1517:11:1;1496:32;;;1492:128;;1551:58;;-1:-1:-1;;;1551:58:1;;-1:-1:-1;;;;;1578:11:1;2772:15:3;;1551:58:1;;;2754:34:3;2824:15;;2804:18;;;2797:43;2689:18;;1551:58:1;2502:344:3;1492:128:1;1415:211;1369:257::o;1701:277::-;1782:20;;1813:8;1822:20;1840:2;1813:8;1822:20;:::i;:::-;1813:31;;;;;:::i;:::-;1805:40;;;:::i;:::-;1782:63;;1878:12;1859:15;:31;1855:117;;1913:48;;-1:-1:-1;;;1913:48:1;;;;;3851:25:3;;;3892:18;;;3885:34;;;3824:18;;1913:48:1;3677:248:3;1855:117:1;1772:206;1701:277;:::o;196:180:3:-;255:6;308:2;296:9;287:7;283:23;279:32;276:52;;;324:1;321;314:12;276:52;-1:-1:-1;347:23:3;;196:180;-1:-1:-1;196:180:3:o;563:248::-;631:6;639;692:2;680:9;671:7;667:23;663:32;660:52;;;708:1;705;698:12;660:52;-1:-1:-1;;731:23:3;;;801:2;786:18;;;773:32;;-1:-1:-1;563:248:3:o;1617:880::-;1872:6;1861:9;1854:25;1835:4;1898:2;1936:3;1931:2;1920:9;1916:18;1909:31;1969:6;1963:13;2013:6;2007:3;1996:9;1992:19;1985:35;2038:1;2048:141;2062:6;2059:1;2056:13;2048:141;;;2158:14;;;2154:23;;2148:30;2123:17;;;2142:3;2119:27;2112:67;2077:10;;2048:141;;;-1:-1:-1;2239:1:3;2233:3;2209:22;;;2205:32;;2198:43;;;;-1:-1:-1;;;;;2349:32:3;;2344:2;2329:18;;2322:60;-1:-1:-1;;;;;;1466:32:3;;2432:2;2417:18;;1454:45;2302:2;2281:15;;;-1:-1:-1;;2277:29:3;2262:45;;2258:55;;-1:-1:-1;2391:45:3;;-1:-1:-1;1401:104:3;2391:45;1586:18;1575:30;;2486:3;2471:19;;1563:43;1617:880;;;;;;;;:::o;2851:225::-;2918:9;;;2939:11;;;2936:134;;;2992:10;2987:3;2983:20;2980:1;2973:31;3027:4;3024:1;3017:15;3055:4;3052:1;3045:15;3081:331;3186:9;3197;3239:8;3227:10;3224:24;3221:44;;;3261:1;3258;3251:12;3221:44;3290:6;3280:8;3277:20;3274:40;;;3310:1;3307;3300:12;3274:40;-1:-1:-1;;3336:23:3;;;3381:25;;;;;-1:-1:-1;3081:331:3:o;3417:255::-;3537:19;;3576:2;3568:11;;3565:101;;;-1:-1:-1;;3637:2:3;3633:12;;;3630:1;3626:20;3622:33;3611:45;3417:255;;;;:::o
Swarm Source
ipfs://332da4c07327bf2f5eece29665bd0006af600c0aad07c60f18eb51af86d64d8c
Loading...
Loading
[ Download: CSV Export ]
A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.