Evm integration example (#323)

* init repo

* cfg update

* add aqua

* update nottion

* init readme
This commit is contained in:
boneyard93501 2022-06-15 12:21:24 -05:00 committed by GitHub
parent b893628e01
commit 52483833c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 376 additions and 0 deletions

View File

@ -0,0 +1,6 @@
.DS_Store
debug/
target/
Cargo.lock
**/*.bk
**/*.bak

View File

@ -0,0 +1,3 @@
# EVM Integration Examples
Documentation coming soon.

View File

@ -0,0 +1,42 @@
-- import "@fluencelabs/aqua-lib/builtin.aqua"
-- func n_urls(providers:Providers) -> i64:
-- res: *string
-- for provider <- providers.urls:
-- res <<- provider.url
-- n <- Op.array_length(res)
-- <- n
data EVMResult:
stdout: string
stderr: string
data ProviderInfo:
url: string
api_key: string
name: string
data Providers:
urls: []ProviderInfo
service MyOp("op"):
identity(x:string) -> string
array_length(providers: []ProviderInfo) -> i64
service MultiProviderQuery("service-id"):
get_balance(provider: ProviderInfo, account: string, block_height: string) -> EVMResult
get_block_number(provider: ProviderInfo) -> EVMResult
func get_block_heights(providers: Providers, service_id: string, node: string) -> []EVMResult, i64:
block_heights: *EVMResult
-- n <- n_urls(providers)
n <- MyOp.array_length(providers.urls)
if n > 0:
on node:
MultiProviderQuery service_id
for provider <- providers.urls par:
block_heights <- MultiProviderQuery.get_block_number(provider)
join block_heights[n-1]
<- block_heights, n

View File

@ -0,0 +1,14 @@
modules_dir = "artifacts/"
[[module]]
name = "curl_adapter"
max_heap_size = "128 MiB"
logger_enabled = true
[module.mounted_binaries]
curl = "/usr/bin/curl"
[[module]]
name = "multi_provider_query"
max_heap_size = "10 KiB"
logger_enabled = true

View File

@ -0,0 +1,19 @@
{
"multi-provider-query": {
"modules": [
{
"name": "curl_adapter",
"path": "./artifacts/curl_adapter.wasm",
"max_heap_size": "64 MiB",
"mounted_binaries": [["curl", "/usr/bin/curl"]],
"logger_enabled": [true]
},
{
"name": "multi-provider-query",
"path": "./artifacts/multi_provider_query.wasm",
"max_heap_size": "64 MiB",
"logger_enabled": [true]
}
]
}
}

View File

@ -0,0 +1,14 @@
[package]
name = "curl_adapter"
version = "0.1.0"
authors = ["Fluence Labs"]
edition = "2018"
publish = false
[[bin]]
path = "src/main.rs"
name = "curl_adapter"
[dependencies]
marine-rs-sdk = "0.6.14"
log = "0.4.8"

View File

@ -0,0 +1,37 @@
/*
* Copyright 2021 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#![allow(improper_ctypes)]
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
use marine_rs_sdk::MountedBinaryResult;
module_manifest!();
pub fn main() {}
#[marine]
pub fn curl_request(cmd: Vec<String>) -> MountedBinaryResult {
curl(cmd)
}
#[marine]
#[link(wasm_import_module = "host")]
extern "C" {
fn curl(cmd: Vec<String>) -> MountedBinaryResult;
}

View File

@ -0,0 +1,24 @@
[package]
name = "multi_provider_query"
version = "0.1.5"
authors = ["boneyard93501 <4523011+boneyard93501@users.noreply.github.com>"]
edition = "2018"
description = "multi-provider-query, a Marine wasi module"
license = "Apache-2.0"
[[bin]]
name = "multi_provider_query"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version = "0.6.15", features = ["logger"] }
log = "0.4.14"
serde_json = "1.0.81"
serde = "1.0.137"
[dev-dependencies]
marine-rs-sdk-test = "0.6.0"
[dev]
[profile.release]
opt-level = "s"

View File

@ -0,0 +1,200 @@
/*
* Copyright 2022 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use marine_rs_sdk::{marine, module_manifest, MountedBinaryResult};
use serde::{Deserialize, Serialize};
use serde_json;
use std::sync::atomic::{AtomicUsize, Ordering};
pub static NONCE_COUNTER: AtomicUsize = AtomicUsize::new(1);
module_manifest!();
fn main() {}
#[marine]
pub struct ProviderInfo {
pub url: String,
pub api_key: String,
pub name: String,
}
#[marine]
pub struct EVMResult {
pub stdout: String,
pub stderr: String,
}
// fn url_maker(url:String, )
fn get_nonce() -> u64 {
NONCE_COUNTER.fetch_add(1, Ordering::SeqCst) as u64
}
#[derive(Serialize, Deserialize)]
struct RpcData {
jsonrpc: String,
method: String,
params: Vec<String>,
id: u64,
}
#[derive(Serialize, Deserialize, Debug)]
struct RpcResponseError {
code: i32,
message: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct RpcResponse {
jsonrpc: String,
error: Option<RpcResponseError>,
result: Option<String>,
}
impl RpcData {
fn new(method: String, params: Vec<String>) -> Self {
let nonce = get_nonce();
RpcData {
jsonrpc: "2.0".to_owned(),
method: method,
params: params,
id: nonce,
}
}
}
fn get_curl_cmd(url: String, data: String) -> Vec<String> {
let curl_cmd: Vec<String> = vec![
url,
"-X".to_string(),
"POST".to_string(),
"-H".to_string(),
"Accept: application/json".to_string(),
"-d".to_string(),
data,
];
curl_cmd
}
fn get_curl_response(curl_cmd: Vec<String>) -> RpcResponse {
let response = curl_request(curl_cmd);
let response = String::from_utf8(response.stdout).unwrap();
let response: RpcResponse = serde_json::from_str(&response).unwrap();
response
}
#[marine]
// see https://eth.wiki/json-rpc/API#eth_blocknumbers
fn get_block_number(provider: ProviderInfo) -> EVMResult {
let method = "eth_blockNumber";
let params: Vec<String> = vec![];
let url = provider.url;
let data = RpcData::new(method.to_owned(), params);
let data = serde_json::to_string(&data).unwrap();
let curl_cmd = get_curl_cmd(url, data);
let response = get_curl_response(curl_cmd);
if response.error.is_none() {
let raw_response = response.result.unwrap();
let block_height = u64::from_str_radix(raw_response.trim_start_matches("0x"), 16);
let result = match block_height {
Ok(r) => {
let j_res = serde_json::json!({ "block-heigh": r });
EVMResult {
stdout: j_res.to_string(),
stderr: "".to_owned(),
}
}
Err(e) => {
let err = format!("unable to convert {} to u64 with error {}", raw_response, e);
EVMResult {
stdout: "".to_owned(),
stderr: err,
}
}
};
return result;
}
EVMResult {
stdout: "".to_owned(),
stderr: serde_json::to_string(&response.error).unwrap(),
}
}
// see https://eth.wiki/json-rpc/API#eth_getBalance
#[marine]
fn get_balance(provider: ProviderInfo, account: String, block_height: String) -> EVMResult {
let method = "eth_getBalance";
let mut params: Vec<String> = vec![account];
if block_height.len() > 0 {
params.push(block_height)
} else {
params.push("latest".to_string())
}
let url = provider.url;
let data = RpcData::new(method.to_owned(), params);
let data = serde_json::to_string(&data).unwrap();
let curl_cmd = get_curl_cmd(url, data);
let response = get_curl_response(curl_cmd);
if response.error.is_none() {
let raw_response = response.result.unwrap();
let balance = u128::from_str_radix(raw_response.trim_start_matches("0x"), 16);
let result = match balance {
Ok(r) => {
let balance = format!("{}", r);
let j_res = serde_json::json!({ "balance": balance });
EVMResult {
stdout: j_res.to_string(),
stderr: "".to_owned(),
}
}
Err(e) => {
let err = format!("unable to convert {} to u64 with error {}", raw_response, e);
EVMResult {
stdout: "".to_owned(),
stderr: err,
}
}
};
return result;
};
EVMResult {
stdout: "".to_owned(),
stderr: serde_json::to_string(&response.error).unwrap(),
}
}
fn ping() -> String {
format!("pong")
}
}
#[marine]
#[link(wasm_import_module = "curl_adapter")]
extern "C" {
pub fn curl_request(cmd: Vec<String>) -> MountedBinaryResult;
}

View File

@ -0,0 +1,17 @@
#!/usr/bin/env bash -o errexit -o nounset -o pipefail
mkdir -p artifacts
rm -f artifacts/*.wasm
cd curl-adapter
cargo update --aggressive
marine build --release
cp target/wasm32-wasi/release/curl_adapter.wasm ../artifacts/
cd ../multi-provider-query
cargo update --aggressive
marine build --release
cp target/wasm32-wasi/release/multi_provider_query.wasm ../artifacts/
cd ..