Commit 1f13c66c authored by Hero Bird's avatar Hero Bird

[examples/model] Add simplified Erc20 token example

parent 7e536a24
[target.wasm32-unknown-unknown]
rustflags = [
"-C", "link-args=-z stack-size=65536 --import-memory"
]
[package]
name = "erc20"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
pdsl_core = { path = "../../../core" }
pdsl_model = { path = "../../../model" }
parity-codec = { version = "3.0", default-features = false, features = ["derive"] }
[lib]
name = "erc20"
crate-type = ["cdylib"]
[features]
default = []
test-env = [
"pdsl_core/test-env",
"pdsl_model/test-env",
]
[profile.release]
panic = "abort"
lto = true
opt-level = "z"
#!/bin/bash
# Deprecated Note:
# The wasm-build executable that is used to tree-shake the wasm binary
# resulting from the cargo build in the first step expects to find it
# under target/release/wasm32-unknown-unknown/ in the cwd.
PROJNAME=erc20
#cargo clean
#rm Cargo.lock
CARGO_INCREMENTAL=0 cargo +nightly build --release --target=wasm32-unknown-unknown --verbose
wasm2wat -o target/$PROJNAME.wat target/wasm32-unknown-unknown/release/$PROJNAME.wasm
cat target/$PROJNAME.wat | sed "s/(import \"env\" \"memory\" (memory (;0;) 2))/(import \"env\" \"memory\" (memory (;0;) 2 16))/" > target/$PROJNAME-fixed.wat
wat2wasm -o target/$PROJNAME.wasm target/$PROJNAME-fixed.wat
wasm-opt -Oz target/$PROJNAME.wasm -o target/$PROJNAME-opt.wasm
#wasm-build target enyzme --target-runtime=substrate --final=adder --save-raw=./target/enzyme-deployed.wasm --target wasm32-unknown-unknown
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of pDSL.
//
// pDSL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// pDSL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with pDSL. If not, see <http://www.gnu.org/licenses/>.
#![no_std]
#![feature(const_str_as_bytes)]
use pdsl_model::{
ContractDecl,
Contract,
state,
messages,
};
use pdsl_core::{
env::{
srml::Address,
srml::Balance,
},
storage,
};
state! {
/// A simple implementation of a rudimentary Erc20 token contract.
struct Erc20Token {
/// The balance for an address.
balances: storage::HashMap<Address, Balance>,
/// The total supply.
total: storage::Value<Balance>
}
}
messages! {
/// Returns the total supply.
0 => TotalSupply() -> Balance;
/// Returns the balance of the given address.
1 => BalanceOf(owner: Address) -> Balance;
/// Transfers balance from the caller to the given address.
///
/// Returns `true` if the transfer was successful.
2 => Transfer(to: Address, amount: Balance) -> bool;
}
fn instantiate() -> impl Contract {
ContractDecl::using::<Erc20Token>()
.on_deploy(|env, init_supply| {
let caller = env.caller();
env.state.balances[&caller] = init_supply;
env.state.total.set(init_supply);
})
.on_msg::<TotalSupply>(|env, _| {
*env.state.total.get()
})
.on_msg::<BalanceOf>(|env, owner| {
env.state.balances[&owner]
})
.on_msg_mut::<Transfer>(|env, (to, amount)| {
// if amount == Address::from(0x0) { // In Substrate we do not have the zero address!
// return false;
// }
let from = env.caller();
let balance_from = env.state.balances[&from];
let balance_to = env.state.balances[&to];
if balance_from >= amount {
env.state.balances[&from] = balance_from - amount;
env.state.balances[&to] = balance_to + amount;
return true
}
false
})
.instantiate()
}
#[no_mangle]
fn deploy() {
instantiate().deploy()
}
#[no_mangle]
fn call() {
instantiate().dispatch()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment