Skip to content

Commit

Permalink
ERC-721 (#159)
Browse files Browse the repository at this point in the history
* Implement ownerOf, transfer, approve, transferFrom, getApproved, setApprovalForAll, isApprovedForAll of ERC-721
* Add erc721 example
  • Loading branch information
yanganto committed Aug 14, 2021
1 parent a443b84 commit 27e4665
Show file tree
Hide file tree
Showing 11 changed files with 611 additions and 10 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/erc721_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Example

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

workflow_dispatch:

jobs:
erc721-example:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive

- uses: cachix/install-nix-action@v13
with:
nix_path: nixpkgs=channel:nixos-21.05-small

- name: Run test
run: nix-shell --run 'run-example-test erc721'

- name: Deploy test
run: nix-shell --run 'cli-test erc721'
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exclude = [
"examples/hello-contract",
"examples/default-contract",
"examples/erc20-contract",
"examples/erc721-contract",
"examples/kv-contract",
"examples/rdb-contract",
"examples/rusty-contract",
Expand Down
2 changes: 1 addition & 1 deletion examples/erc20-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "erc20-contract"
version = "0.1.0"
authors = []
edition = "2018"
description = "This is based on the previous works of second state, but **Not** an exanple for SewUp, I use the project toe setup developing environment for SewUp."
description = "This is an ERC-20 example for SewUp"

[lib]
path = "src/token.rs"
Expand Down
2 changes: 2 additions & 0 deletions examples/erc721-contract/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.'cfg(target_arch="wasm32")']
rustflags = ["-C", "link-arg=--export-table"]
37 changes: 37 additions & 0 deletions examples/erc721-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "erc721-contract"
version = "0.1.0"
authors = []
edition = "2018"
description = "This is an ERC-721 example for SewUp"

[lib]
path = "src/token.rs"
crate-type = ["cdylib"]

[dependencies]
sewup ={ version = "*", path = "../../sewup", features = [ "token" ] }
sewup-derive = { version = "*", path = "../../sewup-derive" }
anyhow = "1.0.40"

[dev-dependencies]
hex-literal = "0.3.1"

[profile.release]
incremental = false
panic = "abort"
lto = true
opt-level = "z"

[profile.release.package.erc721-contract]
incremental = false
opt-level = "z"

[deploy]
url = "http://localhost:8545"
private = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
address = "0xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

[features]
constructor = []
constructor-test = []
83 changes: 83 additions & 0 deletions examples/erc721-contract/src/token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use sewup_derive::{ewasm_constructor, ewasm_fn, ewasm_fn_sig, ewasm_main, ewasm_test};

#[ewasm_constructor]
fn constructor() {
sewup::token::erc721::mint(
"8663DBF0cC68AaF37fC8BA262F2df4c666a41993",
vec![
"0000000000000000000000000000000000000000000000000000000000000001",
"0000000000000000000000000000000000000000000000000000000000000002",
"0000000000000000000000000000000000000000000000000000000000000003",
],
);
}

#[ewasm_main]
fn main() -> anyhow::Result<()> {
let contract = sewup::primitives::Contract::new()?;
match contract.get_function_selector()? {
sewup::token::erc721::BALANCE_OF_SIG => sewup::token::erc721::balance_of(&contract),
sewup::token::erc721::OWNER_OF_SIG => sewup::token::erc721::owner_of(&contract),
sewup::token::erc721::TRANSFER_SIG => sewup::token::erc721::transfer(&contract),
sewup::token::erc721::TRANSFER_FROM_SIG => sewup::token::erc721::transfer_from(&contract),
sewup::token::erc721::APPROVE_SIG => sewup::token::erc721::approve(&contract),
sewup::token::erc721::GET_APPROVED_SIG => sewup::token::erc721::get_approved(&contract),
sewup::token::erc721::SET_APPROVAL_FOR_ALL_SIG => {
sewup::token::erc721::set_approval_for_all(&contract)
}
sewup::token::erc721::IS_APPROVED_FOR_ALL_SIG => {
sewup::token::erc721::is_approved_for_all(&contract)
}
_ => (),
};
Ok(())
}

#[ewasm_test]
mod tests {
use super::*;
use hex_literal::hex;
use sewup::erc721::{BALANCE_OF_SIG, OWNER_OF_SIG};
use sewup_derive::ewasm_assert_eq;

#[ewasm_test]
fn test_execute_basic_operations() {
let address_input = hex!("8663DBF0cC68AaF37fC8BA262F2df4c666a41993");
let mut input_data = vec![0u8, 0u8, 0u8, 0u8];
input_data.extend_from_slice(&address_input);
ewasm_assert_eq!(
balance_of(input_data),
vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 3
]
);

let token1 = hex!("0000000000000000000000000000000000000000000000000000000000000001");
let token2 = hex!("0000000000000000000000000000000000000000000000000000000000000002");
let token4 = hex!("0000000000000000000000000000000000000000000000000000000000000004");

ewasm_assert_eq!(
owner_of(token1),
vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 134, 99, 219, 240, 204, 104, 170, 243, 127,
200, 186, 38, 47, 45, 244, 198, 102, 164, 25, 147
]
);
ewasm_assert_eq!(
owner_of(token2),
vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 134, 99, 219, 240, 204, 104, 170, 243, 127,
200, 186, 38, 47, 45, 244, 198, 102, 164, 25, 147
]
);

ewasm_assert_eq!(
owner_of(token4),
vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
]
);
}
}
Loading

0 comments on commit 27e4665

Please sign in to comment.