{"id":13523064,"url":"https://github.com/SaorsaLabs/Template_Canister","last_synced_at":"2025-04-01T00:30:47.931Z","repository":{"id":222965127,"uuid":"755331272","full_name":"SaorsaLabs/Template_Canister","owner":"SaorsaLabs","description":null,"archived":false,"fork":false,"pushed_at":"2024-02-26T19:46:04.000Z","size":45,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-08-02T06:14:49.273Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SaorsaLabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-02-09T22:45:50.000Z","updated_at":"2024-02-26T19:59:34.000Z","dependencies_parsed_at":"2024-02-26T19:46:31.825Z","dependency_job_id":"9a1fb83d-e0af-4d42-bfd8-b8cb5fe2cf59","html_url":"https://github.com/SaorsaLabs/Template_Canister","commit_stats":null,"previous_names":["saorsalabs/template_canister"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaorsaLabs%2FTemplate_Canister","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaorsaLabs%2FTemplate_Canister/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaorsaLabs%2FTemplate_Canister/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaorsaLabs%2FTemplate_Canister/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SaorsaLabs","download_url":"https://codeload.github.com/SaorsaLabs/Template_Canister/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222688173,"owners_count":17023297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:00:55.147Z","updated_at":"2024-11-02T07:31:12.598Z","avatar_url":"https://github.com/SaorsaLabs.png","language":"Rust","funding_links":[],"categories":["Courses, Tutorials, and Samples"],"sub_categories":["Tutorials and Samples"],"readme":"# Example Canister\n\nThis repo provides an example canister (Smart Contract) which can be used on the [Internet Computer Protocol](https://internetcomputer.org/). The canister is designed to allow developers to build on top of a core set of features while optionally including bolt-on features such as HTTP Outcalls, Timers and Stable Memory Storage. \nThe minimum code required to compile the canister is the core module. On top of this you can add your own modules or use any/ all of the example modules. The example canister is based on canister smart contracts used by [221Bravo.App](https://github.com/SaorsaLabs/221BravoApp_v3), a fully on-chain explorer for a selection of top ICP tokens. \n\n## Getting Started\nIf you have not deployed a smart contact on the Internet Computer before, check out this guide to building on the IC using rust [here]( https://internetcomputer.org/docs/current/developer-docs/backend/rust/)\n\n1. Clone this repository and place it in your folder of choice. \n2. You can choose to either deploy the canister on a local testnet or on the ICP mainnet. Note the inter-canister-call method and HTTP outcall method will only work on mainnet. \n\n#### To deploy locally\nfirst start a local dfx environment \n\n```bash\ndfx start --background --clean\n``` \n\nThis will start a clean version of dfx running in the background. In order to deploy the example canister you will need to know the principal of your developer identity. You can get this by calling `dfx identity get-principal` Simply add this to the argument in the deploy script below\n\n```bash\ndfx deploy example_canister --argument '(xxx-xxx-xxxx-xxxx)'\n```\n\n#### To deploy to mainnet\nIn order to deploy to mainnet you will need ‘cycles’ in your wallet. You can find more information about this [here]( https://internetcomputer.org/docs/current/developer-docs/getting-started/deploy/mainnet) . For main-net you can use the code below – remember to change the argument to your own developer principal. \n\n```bash\ndfx deploy example_canister --network ic --argument '(xxxx-xxxx-xxx-xxxx-xxxx)'\n```\n\n## Making calls to the canister\nOnce deployed on mainnet there are a couple of way to interact and test the canister. You can continue to make calls through your DFX terminal or you can interact with it through the official [Internet Computer Blockchain Dashboard]( https://dashboard.internetcomputer.org/)\n\nTo use the blockchain explorer UI – first get your canister ID from the canister_ids.json file and then search for this on the IC Dashboard. This will show you the publicly visible methods defined in the project’s .did file. Calling most of these methods will result in an ‘unauthorized’ error because the canister implements admin/ authorised user ‘gates’. \n\nIf you want to call all methods from the dashboard you can add the ‘anonymous principal’ as an admin and authorised user. NOTE - this does not completely remove the gate and other callers might still be blocked (for example other canister smart contracts who don't use the 'anonymous principal'). To have a method open to everyone, simply remove the gate code completely (see get_cycles_balance method in core/api). \n\nExample of adding the 'anonymous principal' as admin and authorised:\n\n```bash\ndfx canister call example_canister --network ic add_admin '(\"2vxsx-fae\")'\ndfx canister call example_canister --network ic add_authorised '(\"2vxsx-fae\")'\n``` \n\n## Test methods\nThe core methods in the example_canister should be self explanatory. We have added a number of other methods to demo some of the other functionality added with the HTTP, timer and custom modules.  \n\n#### Write to Stable Storage\nThe internet computer allows canisters to store data in volatile or stable memory. For ease, most of the data associated in the core functions is stored in volatile memory. It should be noted that because the canister is replicated multiple times across a subnet, volatile memory is not at risk of being lost if a node goes ‘down’. There is however only a limited amount of volatile memory a canister can use (currently 4gb). \n\nTo store more data, canisters can use stable memory (currently up to 400gb). There are two rust crates that assist in writing data to stable memory (ic-stable-structures and ic-stable-memory). The example_canister uses [ic-stable-structures]( https://docs.rs/ic-stable-structures/latest/ic_stable_structures/) which was created by Dfinity. \n\nIn this canister, the example_custom_module (btree_logic.rs) defines the logic for interacting with a Btreemap stored in stable memory. The BTreeMap is defined in stable_memory.rs in the core module. \n\nAdd entry to the Map. Input is a String (Name), String (Nickname), u64 (age). \n```bash\ndfx canister call example_canister --network ic add_to_btree_map '(\"Jonathan\", \"J-dawg\", 28: nat64)'\n```\n\nLookup entry in the Map\n```bash\ndfx canister call example_canister --network ic get_value_btree_map '(\"Jonathan\")'\n```\n\nRemove entry from the Map\n```bash\ndfx canister call example_canister --network ic remove_from_btree_map '(\"Jonathan\")'\n```\n\n#### Call another canister \nWithin the core module is a function to allow users to easily call other canister smart contracts on the Internet Computer (see utils.rs). An example of this being used is in example_custom_modlue/logic.rs which calls the ckBTC minter to fetch the estimated withdrawal fee.\n\n```bash \ndfx canister call example_canister --network ic make_call_to_ckbtc_minter  \n``` \n\n#### Make a HTTP outcall\n\nPart of what makes the Internet Computer unlike any other blockchain is it’s ability to make HTTP calls to ‘off-chain’ endpoints and also serve HTTP web content directly from the canister smart contract.  \n\nThe HTTP module in the example canister provides basic examples of how to perform both inbound and outbound HTTP calls. \nOutbound call to Coinbase (fetching latest ICP price) \n\n```bash\ndfx canister call example_canister --network ic  test_http_outcall\n```\n\nTo test an inbound HTTP request to the text canister you can replace the xxxx’s with your deployed canister id. The route /data will redirect you to 221Bravo.App, /ok will display a Hello World message and /err will display another message\n\n```html\nhttps://xxxxx-xxxxx-xxxxxx-xxxxx-xxxx.icp0.io/data\nhttps://xxxxx-xxxxx-xxxxxx-xxxxx-xxxx.icp0.io/ok\nhttps://xxxxx-xxxxx-xxxxxx-xxxxx-xxxx.icp0.io/err\n```\n\n#### Start and Stop Timers\n\nTimers are an incredibly useful feature of the Internet Computer. Timers can be used to call functions within your smart contract at a set interval. This can be really useful for making periodic calls to other smart contracts or even ‘self-calling’ a method on the canister using the timer. \nStart a timer (this text example logs a message to the canister logs every 30 seconds)\n\n```bash\ndfx canister call example_canister --network ic start_test_timer '(30: nat64)'\n```\n\nStop all timers\n```bash\ndfx canister call example_canister --network ic stop_all_timers\n```\n\n#### Note\nTimers are not persisted through upgrades. It is recommended to stop any timers before an upgrade and then restart them once the upgrade has been completed.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSaorsaLabs%2FTemplate_Canister","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSaorsaLabs%2FTemplate_Canister","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSaorsaLabs%2FTemplate_Canister/lists"}