Просмотр исходного кода

example" Add example smart contract implementation.

parazyd 4 лет назад
Родитель
Сommit
46d64a5ce3

+ 3 - 0
example/smart-contract/.gitignore

@@ -0,0 +1,3 @@
+target/*
+Cargo.lock
+smart_contract.wasm

+ 23 - 0
example/smart-contract/Cargo.toml

@@ -0,0 +1,23 @@
+[package]
+name = "smart-contract"
+version = "0.1.0"
+edition = "2021"
+
+[workspace]
+
+[lib]
+crate-type = ["cdylib", "rlib"]
+
+[dependencies]
+borsh = "0.9.3"
+drk-sdk = { path = "../../src/sdk" }
+
+[dependencies.pasta_curves]
+git = "https://github.com/darkrenaissance/pasta_curves"
+branch = "serialization-support"
+features = ["borsh"]
+
+[profile.release]
+lto = true
+codegen-units = 1
+overflow-checks = true

+ 16 - 0
example/smart-contract/Makefile

@@ -0,0 +1,16 @@
+.POSIX:
+
+SRC = $(shell find src -type f)
+
+# Cargo binary
+CARGO = cargo
+
+DEPS = smart_contract.wasm
+
+all: $(DEPS)
+
+smart_contract.wasm: $(SRC)
+	$(CARGO) build --release --lib --target wasm32-unknown-unknown
+	cp -f target/wasm32-unknown-unknown/release/$@ $@
+
+.PHONY: all

+ 29 - 0
example/smart-contract/src/lib.rs

@@ -0,0 +1,29 @@
+use borsh::{BorshDeserialize, BorshSerialize};
+use drk_sdk::{
+    entrypoint,
+    error::{ContractError, ContractResult},
+    msg,
+};
+use pasta_curves::pallas;
+
+#[derive(BorshSerialize, BorshDeserialize)]
+pub struct Args {
+    pub a: pallas::Base,
+    pub b: pallas::Base,
+}
+
+entrypoint!(process_instruction);
+fn process_instruction(ix: &[u8]) -> ContractResult {
+    let args = Args::try_from_slice(ix)?;
+
+    if args.a < args.b {
+        return Err(ContractError::Custom(69))
+    }
+
+    let sum = args.a + args.b;
+
+    msg!("Hello from the VM runtime!");
+    msg!("Sum: {:?}", sum);
+
+    Ok(())
+}