From 7687fd41079bcb527b3e8d24654e0b4dea1c12df Mon Sep 17 00:00:00 2001 From: Mike Godenzi Date: Tue, 11 Apr 2023 09:26:11 +0200 Subject: [PATCH 1/2] doc: added additional description for direct fulfills on wasm smart contract --- pallets/fulfillment-receiver/README.md | 83 ++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/pallets/fulfillment-receiver/README.md b/pallets/fulfillment-receiver/README.md index 97f2453a..89c0a756 100644 --- a/pallets/fulfillment-receiver/README.md +++ b/pallets/fulfillment-receiver/README.md @@ -287,3 +287,86 @@ mod receiver { } } ``` + +## WASM Smart Contract direct integration + +It is also possible to directly fulfill to a WASM smart contract without goign through a pallet first. For example, given the following smart contract: + +```rust +#![cfg_attr(not(feature = "std"), no_std)] + +use ink; + +#[ink::contract] +mod receiver { + + use ink::{ + env::{caller, DefaultEnvironment}, + storage::Mapping, + }; + + #[ink(storage)] + pub struct Receiver { + allowed_processors: Mapping, + price: u128, + } + + impl Receiver { + + #[ink(constructor)] + pub fn default() -> Self { + Self { + allowed_processors: Default::default(), + price: Default::default(), + } + } + + #[ink(message)] + pub fn fulfill(&mut self, price: u128) { + let caller = caller::(); + if self.allowed_processors.contains(&caller) { + self.price = price; + } + } + + #[ink(message)] + pub fn get_price(&self) -> u128 { + self.price + } + + #[ink(message)] + pub fn add_processor(&mut self, processor: AccountId) { + self.allowed_processors.insert(processor, &()); + } + + #[ink(message)] + pub fn remove_processor(&mut self, processor: AccountId) { + self.allowed_processors.remove(&processor); + } + } +} +``` + +It is possible to directly call the fulfill method directly from the processors. The example script below shows how to call the above smart contract deployed on the Shibuya parachain: + +```javascript +const callIndex = '0x4606'; // the call index for the "call" extrinsic of pallet-contracts +const payload = _STD_.chains.substrate.codec.encodeUnsignedNumber(2, 128); // encoding the price value (2) as an u128 +const destination = "XTj3CLB3G6WnwPMgYo1PM2xi9BkDegtGw5WPd7X36G515La"; // the smart contract address +_STD_.chains.substrate.contract.fulfill( + "https://shibuya-rpc.dwellir.com", // the parachain rpc endpoint + callIndex, + destination, + payload, + { + refTime: "3951114240", + proofSize: "629760", + }, + (opHash) => { + print("Succeeded: " + opHash) + }, + (err) => { + print("Failed: " + err) + }, +) +``` From 08c3c57b20a497be60f26920519c06da4a00426f Mon Sep 17 00:00:00 2001 From: Mike Godenzi Date: Wed, 12 Apr 2023 16:39:49 +0200 Subject: [PATCH 2/2] doc: updated script for wasm contract --- pallets/fulfillment-receiver/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/pallets/fulfillment-receiver/README.md b/pallets/fulfillment-receiver/README.md index 89c0a756..042c2598 100644 --- a/pallets/fulfillment-receiver/README.md +++ b/pallets/fulfillment-receiver/README.md @@ -353,6 +353,7 @@ It is possible to directly call the fulfill method directly from the processors. const callIndex = '0x4606'; // the call index for the "call" extrinsic of pallet-contracts const payload = _STD_.chains.substrate.codec.encodeUnsignedNumber(2, 128); // encoding the price value (2) as an u128 const destination = "XTj3CLB3G6WnwPMgYo1PM2xi9BkDegtGw5WPd7X36G515La"; // the smart contract address +_STD_.chains.substrate.signer.setSigner("SECP256K1"); // select which curve to use for the signature _STD_.chains.substrate.contract.fulfill( "https://shibuya-rpc.dwellir.com", // the parachain rpc endpoint callIndex,