diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..1ceaeea --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(example-capi STATIC EXCLUDE_FROM_ALL capi.c) +target_include_directories(example-capi PRIVATE ../include) \ No newline at end of file diff --git a/examples/capi.c b/examples/capi.c new file mode 100644 index 0000000..6419ade --- /dev/null +++ b/examples/capi.c @@ -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); +}