This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: smart contract with improved dx #462
Merged
kratico
merged 21 commits into
main
from
feat/ink-smart-contract-example-my-contract-dx
Dec 22, 2022
Merged
feat: smart contract with improved dx #462
kratico
merged 21 commits into
main
from
feat/ink-smart-contract-example-my-contract-dx
Dec 22, 2022
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kratico
changed the title
Feat/ink smart contract example my contract dx
feat: smart contract with improved dx
Dec 14, 2022
kratico
force-pushed
the
feat/ink-smart-contract-example-my-contract-dx
branch
from
December 14, 2022 18:50
a21b5ee
to
47145bb
Compare
kratico
commented
Dec 14, 2022
examples/smart_contract.ts
Outdated
} | ||
} | ||
|
||
const flipperContract = new SmartContract(contract.metadata, contractAddress) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following contract calls could be simplified without console.logs
like
await flipperContract.call(T.alice.publicKey, "get", []).run()
await flipperContract.tx(T.alice.publicKey, "flip", [], T.alice.sign).run()
await flipperContract.call(T.alice.publicKey, "get", []).run()
await flipperContract.call(T.alice.publicKey, "get_count", []).run()
await flipperContract.tx(T.alice.publicKey, "inc", [], T.alice.sign).run()
await flipperContract.tx(T.alice.publicKey, "inc", [], T.alice.sign).run()
await flipperContract.call(T.alice.publicKey, "get_count", []).run()
await flipperContract.tx(T.alice.publicKey, "inc_by", [3], T.alice.sign).run()
await flipperContract.call(T.alice.publicKey, "get_count", []).run()
await flipperContract.call(T.alice.publicKey, "m1", [2, true]).run()
await flipperContract.call(T.alice.publicKey, "m1", [3, false]).run()
For messages that mutate the state, contract.tx(origin, message, args, sign)
is used.
For other messages, contract.call(origin, message, args)
is used.
The source code for this contract is
#[ink::contract]
mod flipper {
/// Defines an event that is emitted
/// every time inc is invoked.
#[ink(event)]
pub struct Incremented {
from: Option<AccountId>,
count: i32,
}
/// Defines the storage of your contract.
/// Add new fields to the below struct in order
/// to add new static storage fields to your contract.
#[ink(storage)]
pub struct Flipper {
/// Stores a single `bool` value on the storage.
value: bool,
/// Stores count
count: i32,
}
#[derive(Debug, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct M2 {
a: u32,
b: bool,
}
impl Flipper {
/// Constructor that initializes the `bool` value to the given `init_value`.
#[ink(constructor)]
pub fn new(init_value: bool) -> Self {
Self {
value: init_value,
count: 0,
}
}
/// Constructor that initializes the `bool` value to `false`.
///
/// Constructors can delegate to other constructors.
#[ink(constructor)]
pub fn default() -> Self {
Self::new(Default::default())
}
/// A message that can be called on instantiated contracts.
/// This one flips the value of the stored `bool` from `true`
/// to `false` and vice versa.
#[ink(message)]
pub fn flip(&mut self) {
self.value = !self.value;
}
/// Simply returns the current value of our `bool`.
#[ink(message)]
pub fn get(&self) -> bool {
self.value
}
/// multiple arg method returning a tuple.
#[ink(message)]
pub fn method_returning_tuple(&self, a: u32, b: bool) -> (u32, bool) {
(a, b)
}
/// multiple arg method returning a struct.
#[ink(message)]
pub fn method_returning_struct(&self, a: u32, b: bool) -> M2 {
M2 { a, b }
}
/// get the current count.
#[ink(message)]
pub fn get_count(&self) -> i32 {
self.count
}
/// increment current count by 1
#[ink(message)]
pub fn inc(&mut self) {
self.inc_by(1);
}
/// decrement current count by 1
#[ink(message)]
pub fn dec(&mut self) {
self.inc_by(-1);
}
/// increment current count by n
#[ink(message)]
pub fn inc_by(&mut self, n: i32) {
self.count += n;
}
/// increment current count by n and emit an event
#[ink(message)]
pub fn inc_by_with_event(&mut self, n: i32) {
self.inc_by(n);
self.env().emit_event(Incremented {
from: self.env().caller().into(),
count: self.count,
});
}
}
kratico
force-pushed
the
feat/ink-smart-contract-example-my-contract-dx
branch
from
December 15, 2022 15:58
4665fae
to
e3c5785
Compare
kratico
force-pushed
the
feat/ink-smart-contract-example-my-contract-dx
branch
from
December 16, 2022 12:32
3789969
to
c9d9d9c
Compare
kratico
commented
Dec 16, 2022
@@ -182,3 +183,164 @@ export namespace ContractMetadata { | |||
return normalize(contractMetadata).V3.types | |||
} | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following codecs can be removed once paritytech/substrate#11648 is implemented
kratico
force-pushed
the
feat/ink-smart-contract-example-my-contract-dx
branch
from
December 19, 2022 18:14
91a9a90
to
f304910
Compare
kratico
force-pushed
the
feat/ink-smart-contract-example-my-contract-dx
branch
from
December 20, 2022 21:53
2ba2a11
to
383596f
Compare
Co-authored-by: Harry Solovay <[email protected]>
Co-authored-by: Harry Solovay <[email protected]>
kratico
force-pushed
the
feat/ink-smart-contract-example-my-contract-dx
branch
from
December 22, 2022 14:47
383596f
to
1a956c5
Compare
harrysolovay
previously approved these changes
Dec 22, 2022
harrysolovay
approved these changes
Dec 22, 2022
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Add a smart contract deployment and call example.
The example showcase how to