Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Doc/update receiver docs #77

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions pallets/fulfillment-receiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,87 @@ 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<AccountId, ()>,
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::<DefaultEnvironment>();
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.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,
destination,
payload,
{
refTime: "3951114240",
proofSize: "629760",
},
(opHash) => {
print("Succeeded: " + opHash)
},
(err) => {
print("Failed: " + err)
},
)
```