Skip to content

Commit 9a14aaf

Browse files
committed
go!
1 parent 3be327c commit 9a14aaf

File tree

5 files changed

+177
-883
lines changed

5 files changed

+177
-883
lines changed

contracts/metadata/src/lib.rs

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
use ssvm_wasi_helper::ssvm_wasi_helper::_initialize;
2-
1+
use sdk::*;
2+
mod sdk;
33
extern crate juniper;
44

55
#[macro_use]
66
extern crate juniper_codegen;
7-
use std::env;
87
use base64::*;
98
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
109
use hex::{FromHex, ToHex};
1110
use juniper::{
1211
graphql_object, EmptyMutation, EmptySubscription, FieldError, GraphQLEnum, GraphQLValue,
1312
RootNode, Variables,
1413
};
14+
use serde::{Deserialize, Serialize};
1515
use serde_json::json;
16+
1617
use std::collections::HashMap;
18+
use std::env;
1719

18-
use serde_hex::utils::fromhex;
19-
use std::convert::TryInto;
20-
// use std::convert::From::from;
21-
use std::fmt::Display;
22-
use std::future::*;
23-
use std::io::Cursor;
2420
use std::str;
2521
use std::vec::*;
2622
use wasm_bindgen::prelude::*;
@@ -31,13 +27,13 @@ struct Context {
3127

3228
impl juniper::Context for Context {}
3329

34-
#[derive(GraphQLObject, Clone, Debug)]
30+
#[derive(GraphQLObject, Clone, Debug, Serialize, Deserialize)]
3531
struct DagLink {
3632
path: String,
3733
cid: String,
3834
}
3935

40-
#[derive(Clone, Debug)]
36+
#[derive(Clone, Debug, Serialize, Deserialize)]
4137
struct Ancon721Metadata {
4238
name: String,
4339
description: String,
@@ -72,7 +68,7 @@ impl Ancon721Metadata {
7268
vec![]
7369
}
7470
}
75-
#[derive(Clone, Debug)]
71+
#[derive(Clone, Debug, Serialize, Deserialize)]
7672
struct DagContractTrusted {
7773
data: DagLink,
7874
payload: Ancon721Metadata,
@@ -95,23 +91,15 @@ struct Query;
9591

9692
#[graphql_object(context = Context)]
9793
impl Query {
98-
9994
fn api_version() -> &'static str {
10095
"0.1"
10196
}
10297

103-
fn metadata(context: &Context,cid: String, path: String) -> Ancon721Metadata {
104-
105-
106-
unsafe {let metadata = read_dag_block(&cid, &path);}
107-
108-
Ancon721Metadata {
109-
name: "test".to_string(),
110-
description: "description".to_string(),
111-
image: "http://ipfs.io/ipfs/".to_string(),
112-
owner: "".to_string(),
113-
parent: "".to_string(),
114-
sources: [].to_vec(),
98+
fn metadata(context: &Context, cid: String, path: String) -> Ancon721Metadata {
99+
unsafe {
100+
let metadata = read_dag_block(&cid, &path);
101+
let res = serde_json::from_slice(&metadata);
102+
res.unwrap()
115103
}
116104
}
117105
}
@@ -156,8 +144,6 @@ fn schema() -> Schema {
156144
Schema::new(Query, Mutation, EmptySubscription::<Context>::new())
157145
}
158146

159-
160-
161147
#[wasm_bindgen()]
162148
pub fn execute(query: &str) -> String {
163149
// Create a context object.
@@ -182,18 +168,22 @@ pub fn execute(query: &str) -> String {
182168
json!({
183169
"data":data.to_string(),
184170
"errors": errors,
185-
}).to_string()
171+
})
172+
.to_string()
186173
}
187-
extern {
188-
// #[no_mangle]
189-
pub fn write_store(key: String, value: String);
190174

191-
// #[no_mangle]
192-
pub fn read_store(key: String) -> String;
175+
#[wasm_bindgen]
176+
pub fn store(data: &str) -> Vec<u8> {
177+
let payload = Ancon721Metadata {
178+
name: "test".to_string(),
179+
description: "description".to_string(),
180+
image: "http://ipfs.io/ipfs/".to_string(),
181+
owner: "".to_string(),
182+
parent: "".to_string(),
183+
sources: [].to_vec(),
184+
};
193185

194-
// #[no_mangle]
195-
pub fn write_dag_block(data: String) -> String;
186+
let json_payload = serde_json::to_string_pretty(&payload).unwrap();
196187

197-
#[no_mangle]
198-
pub fn read_dag_block(cid: &str, path: &str) -> String;
188+
unsafe { write_dag_block(&json_payload).to_vec() }
199189
}

contracts/metadata/src/sdk.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
1+
2+
extern "C" {
3+
#[no_mangle]
4+
pub fn write_store(key: &str, value: &str) -> [u8;1024];
5+
6+
#[no_mangle]
7+
pub fn read_store(key: &str) -> [u8;1024];
8+
9+
#[no_mangle]
10+
pub fn write_dag_block(data: &str) -> [u8;1024];
11+
12+
#[no_mangle]
13+
pub fn read_dag_block(cid: &str, path: &str) -> [u8;1024];
14+
}

main.go

Lines changed: 17 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
func main() {
1515

16+
1617
dataFolder := ".ancon"
1718
anconstorage := sdk.NewStorage(dataFolder)
1819
db := dbm.NewMemDB()
@@ -36,98 +37,34 @@ func main() {
3637
[]string{".:."}, /// The mapping preopens
3738
)
3839

39-
4040
// a := wasmedge.NewImportObject("ancon")
4141
/// Instantiate wasm
4242
file := "/home/rogelio/Code/ancon-contracts/contracts/metadata/pkg/metadata_lib_bg.wasm"
4343
vm.LoadWasmFile(file)
44-
45-
var type1 = wasmedge.NewFunctionType(
46-
[]wasmedge.ValType{
47-
48-
49-
wasmedge.ValType_I32,
50-
wasmedge.ValType_I32,
51-
}, []wasmedge.ValType{
52-
wasmedge.ValType_I32,
53-
})
54-
var type2 = wasmedge.NewFunctionType(
55-
[]wasmedge.ValType{
56-
57-
wasmedge.ValType_I32,
58-
wasmedge.ValType_I32,
59-
wasmedge.ValType_I32,
60-
wasmedge.ValType_I32,
61-
wasmedge.ValType_I32,
62-
63-
}, []wasmedge.ValType{
64-
// wasmedge.ValType_I32,
65-
})
66-
n := wasmedge.NewImportObject("env")
67-
fn1 := wasmedge.NewFunction(type2, host.WriteStore, nil, 0)
68-
n.AddFunction("write_store", fn1)
69-
70-
fn2 := wasmedge.NewFunction(type1, host.ReadStore, nil, 0)
71-
n.AddFunction("read_store", fn2)
72-
73-
fn3 := wasmedge.NewFunction(type2, host.ReadDagBlock, nil, 0)
74-
n.AddFunction("read_dag_block", fn3)
75-
76-
77-
fn4 := wasmedge.NewFunction(type1, host.WriteDagBlock, nil, 0)
78-
n.AddFunction("write_dag_block", fn4)
79-
vm.RegisterImport(n)
44+
vm.RegisterImport(host.GetImports())
8045
// wasi.InitWasi(
8146

8247
vm.Validate()
8348
vm.Instantiate()
84-
85-
f, e := vm.GetFunctionList()
86-
fmt.Println("%v", f)
87-
fmt.Println("%v", e) /// Run bindgen functions
8849
var res interface{}
8950
var err error
90-
// /// create_line: array, array, array -> array (inputs are JSON stringified)
91-
// res, err = vm.ExecuteBindgen("create_line", wasmedge.Bindgen_return_array, []byte("{\"x\":1.5,\"y\":3.8}"), []byte("{\"x\":2.5,\"y\":5.8}"), []byte("A thin red line"))
92-
// if err == nil {
93-
// fmt.Println("Run bindgen -- create_line:", string(res.([]byte)))
94-
// } else {
95-
// fmt.Println("Run bindgen -- create_line FAILED")
96-
// }
97-
// /// say: array -> array
98-
// res, err = vm.ExecuteBindgen("say", wasmedge.Bindgen_return_array, []byte("bindgen funcs test"))
99-
// if err == nil {
100-
// fmt.Println("Run bindgen -- say:", string(res.([]byte)))
101-
// } else {
102-
// fmt.Println("Run bindgen -- say FAILED")
103-
// }
104-
// /// obfusticate: array -> array
105-
// res, err = vm.ExecuteBindgen("obfusticate", wasmedge.Bindgen_return_array, []byte("A quick brown fox jumps over the lazy dog"))
106-
// if err == nil {
107-
// fmt.Println("Run bindgen -- obfusticate:", string(res.([]byte)))
108-
// } else {
109-
// fmt.Println("Run bindgen -- obfusticate FAILED")
110-
// }
111-
// /// lowest_common_multiple: i32, i32 -> i32
112-
// res, err = vm.ExecuteBindgen("lowest_common_multiple", wasmedge.Bindgen_return_i32, int32(123), int32(2))
113-
// if err == nil {
114-
// fmt.Println("Run bindgen -- lowest_common_multiple:", res.(int32))
115-
// } else {
116-
// fmt.Println("Run bindgen -- lowest_common_multiple FAILED")
117-
// }
118-
// /// sha3_digest: array -> array
119-
// res, err = vm.ExecuteBindgen("sha3_digest", wasmedge.Bindgen_return_array, []byte("This is an important message"))
120-
// if err == nil {
121-
// fmt.Println("Run bindgen -- sha3_digest:", res.([]byte))
122-
// } else {
123-
// fmt.Println("Run bindgen -- sha3_digest FAILED")
124-
// }
125-
/// keccak_digest: array -> array
126-
res, err = vm.ExecuteBindgen("execute", wasmedge.Bindgen_return_array, []byte(`query { metadata(cid:"babfy",path:"/") {image}}`))
51+
52+
53+
payload := []byte(`{ "name":"" , "image":"", "description":""}`)
54+
res, err = vm.ExecuteBindgen("store", wasmedge.Bindgen_return_array, payload)
55+
if err == nil {
56+
fmt.Println("Run bindgen -- store:", string(res.([]byte)))
57+
} else {
58+
fmt.Println("Run bindgen -- store FAILED")
59+
}
60+
61+
62+
q := []byte(`query { metadata(cid:"babfy",path:"/") {image}}`)
63+
res, err = vm.ExecuteBindgen("execute", wasmedge.Bindgen_return_array, q)
12764
if err == nil {
128-
fmt.Println("Run bindgen -- query:", string(res.([]byte)))
65+
fmt.Println("Run bindgen -- execute:", string(res.([]byte)))
12966
} else {
130-
fmt.Println("Run bindgen -- query FAILED")
67+
fmt.Println("Run bindgen -- execute FAILED")
13168
}
13269

13370
vm.Release()

0 commit comments

Comments
 (0)