Example usage of EVM-C

This commit is contained in:
Paweł Bylica 2016-06-24 11:40:06 +02:00
parent 945b2f54ad
commit 57afb98d2c
2 changed files with 39 additions and 0 deletions

2
examples/CMakeLists.txt Normal file
View File

@ -0,0 +1,2 @@
add_library(example-capi STATIC EXCLUDE_FROM_ALL capi.c)
target_include_directories(example-capi PRIVATE ../include)

37
examples/capi.c Normal file
View File

@ -0,0 +1,37 @@
#include "evm.h"
struct evm_uint256 balance(struct evm_env*, struct evm_hash160 address);
union evm_variant query(struct evm_env* env,
enum evm_query_key key,
union evm_variant arg) {
union evm_variant result;
switch (key) {
case EVM_GAS_LIMIT: result.int64 = 314; break;
case EVM_BALANCE:
result.uint256 = balance(env, arg.address);
break;
default: result.int64 = 0; break;
}
return result;
}
/// Example how the API is supposed to be used.
void example() {
struct evm_instance* jit = evm_create(query, 0, 0, 0);
char const code[] = "exec()";
struct evm_bytes_view code_view = {code, sizeof(code)};
struct evm_hash256 code_hash = {{1, 2, 3}};
struct evm_bytes_view input = {"Hello World!", 12};
struct evm_uint256 value = {{1, 0, 0, 0}};
int64_t gas = 200000;
struct evm_result result =
evm_execute(jit, NULL, code_hash, code_view, gas, input, value);
evm_destroy_result(result);
evm_destroy(jit);
}