-
Notifications
You must be signed in to change notification settings - Fork 443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple Mapping Storage Primitive #946
Changes from 4 commits
61d564d
8ae8047
3a9d97e
3e29a25
d1387f7
9e48c18
b7cf4dc
dc87fba
728462a
5b2a8af
a5d016e
ad524c6
e6293a3
b55a24e
9c6f853
299ba86
78e69f3
00ce6ea
a43e0de
7c55dbf
84576ed
a5d01e4
14c4347
5508fc1
baa2c32
cede033
e5c6ef5
0135977
ad26982
644ab71
2ed985f
b954162
7e55e56
507cf5b
c23e679
334a9da
408bf81
b625a0a
2184cd6
1854dcf
c4a951c
5e5a918
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,9 @@ use ink_lang as ink; | |
|
||
#[ink::contract] | ||
mod erc20 { | ||
use ink_primitives::{ | ||
Key, | ||
KeyPtr, | ||
}; | ||
use ink_storage::{ | ||
collections::mapping::Mapping, | ||
lazy::Lazy, | ||
traits::SpreadAllocate, | ||
}; | ||
|
||
/// A simple ERC-20 contract. | ||
|
@@ -60,25 +55,11 @@ mod erc20 { | |
/// The ERC-20 result type. | ||
pub type Result<T> = core::result::Result<T, Error>; | ||
|
||
impl SpreadAllocate for Erc20 { | ||
fn allocate_spread(ptr: &mut KeyPtr) -> Self { | ||
Self { | ||
total_supply: SpreadAllocate::allocate_spread(ptr), | ||
balances: SpreadAllocate::allocate_spread(ptr), | ||
allowances: SpreadAllocate::allocate_spread(ptr), | ||
} | ||
} | ||
} | ||
|
||
impl Erc20 { | ||
/// Creates a new ERC-20 contract with the specified initial supply. | ||
#[ink(constructor)] | ||
pub fn new(initial_supply: Balance) -> Self { | ||
let root_key = Key::from([0x00; 32]); | ||
let mut key_ptr = KeyPtr::from(root_key); | ||
let mut instance = Self::allocate_spread(&mut key_ptr); | ||
instance.new_init(initial_supply); | ||
instance | ||
ink_lang::codegen::initialize_contract(|contract| Self::new_init(contract, initial_supply)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Robbepop using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The actual plan is still that we provide some useful and well designed syntactic sugar so that this line of code won't be necessary but maybe that's not well thought through and maybe writing out this line of code actually isn't so bad. Then we could put this into some other module or into root. |
||
} | ||
|
||
/// Default initializes the ERC-20 contract with the specified initial supply. | ||
|
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.
First: You cannot do this since not all
#[ink(storage)]
structs should implementSpreadAllocate
which is why it is separate fromSpreadLayout
and has not been integrated into the already existing traits.Second: If this was actually useful (which it isn't) it should reside in another PR.
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.
For ink! codegen to automatically implement
SpreadAllocate
optionally we'd need support for Rust trivial trait bounds which currently are unstable.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.
My bad. I thought that the idea behind merging #995 before the
Mapping
was so we could deriveSpreadAllocate
for storage types in the codegen instead of manually deriving itThere 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.
If Rust had trivial trait bounds we actually could do exactly that ... :(