Crate piecrust_uplink
source ·Expand description
Piecrust Uplink is the crate for writing WASM smart contracts in Rust targeting the Piecrust virtual machine.
A smart contract is a program exposing a collection of functions that operate on a shared state. These functions can perform a variety of operations, such as reading or modifying the state, or interacting with the host.
§State Model and ABI
Contracts targeting the Piecrust VM represent their state as a WASM memory. The contract is free to represent its data as it sees fit, and may allocate at will.
To communicate with the host, both the contract and the host can emplace data in a special region of this memory called the argument buffer. The argument buffer is used as both the input from the host and as the output of the contract. All functions exposed by the contract must follow the convention:
// A function compatible with the piecrust ABI takes in the number of bytes
// written by the host to the argument buffer and returns the number of
// bytes written by the contract.
uint32_t fname(uint32_t arg_len);
The contract also has some functions available to it, offered by the host through WASM imports. Examples of these functions include, but are not limited to:
The functions in this crate are wrappers around a particular way of calling the WASM imports. Take a look at the externs for a full view of what is available.
§Examples
Some of the contracts used for testing purposes function as good examples. Take a look at the contracts/ directory.
§Features
By default, this crate will include no features and build only the types and
functions available when the ABI is not present. To write a contract one
must use the abi feature:
abifor writing contractsdlmallocto using the builtin allocatordebugfor writing contracts with debug capabilities such as thedebug!macro, and logging panics to stdout
Modules§
- arg_buf
abi
Macros§
- debug
debugandabiMacro to format and send debug output to the host
Structs§
- An archived
Event - ArgbufWriter
abiA small struct that canfmt::Writeto the argument buffer. - ID to identify the wasm contracts after they have been deployed
- The resolver for an archived
ContractId - And event emitted by a contract.
- The resolver for an archived
Event
Enums§
- An archived
ContractError - The error possibly returned on an inter-contract-call.
- The resolver for an archived
ContractError
Constants§
- The size of the argument buffer in bytes
- The length of
ContractIdin bytes - How many bytes to use for scratch space when serializing
Functions§
- call
abiCalls acontract’sfn_namefunction with the given argumentfn_arg. The contract will have93%of the remaining gas available to spend. - call_raw
abiCalls the function with namefn_nameof the givencontractusingfn_argas argument. - Calls the function with name
fn_nameof the givencontractusingfn_argas argument, allowing it to spend the givengas_limit. - Calls a
contract’sfn_namefunction with the given argumentfn_arg, allowing it to spend the givengas_limit. - caller
abiReturn the ID of the calling contract. The returned id will be uninitialized if there is no caller - meaning this is the first contract to be called. - emit
abiEmits an event with the given data. - feed
abiFeeds the host with data. - hdebug⚠
debugandabi - host_query
abiExecute some code that the host provides under the given name. - limit
abiReturns the gas limit with which the contact was called. - meta_data
abiReturns data made available by the host under the given name. The typeDmust be correctly specified, otherwise undefined behavior will occur. - owner
abiReturn the given contract’s owner, if the contract exists. - self_id
abiReturn the current contract’s id. - self_owner
abiReturns the current contract’s owner. - spent
abiReturns the amount of gas the contact has spent. - wrap_call
abiWrap a call with its respective (de)serializers.
Type Aliases§
- Type with
rkyvserialization capabilities for specific types.