[][src]Crate concordium_std

This library provides the core API that can be used to write smart contracts for the Concordium blockchain. It aims to provide safe wrappers around the core primitives exposed by the chain and accessible to smart contracts.

By default the library will be linked with the std crate, the rust standard library, however to minimize code size this library supports toggling compilation with the #![no_std] attribute via the feature std which is enabled by default. Compilation without the std feature requires a nightly version of rust.

To use this library without the std feature you have to disable it, which can be done, for example, as follows.

[dependencies.concordium-sc-base]
default-features = false

In your project's Cargo.toml file.

The library is meant to be used as a standard library for developing smart contracts. For this reason it re-exports a number of definitions from other libraries.

Global allocator.

Importing this library has a side-effect of setting the allocator to wee_alloc which is a memory allocator aimed at small code footprint. This allocator is designed to be used in contexts where there are a few large allocations up-front, and the memory later used during the lifetime of the program. Frequent small allocations will have bad performance, and should be avoided.

Panic handler

When compiled without the std feature this crate sets the panic handler to a no-op.

Modules

collections

Re-export.

convert

Re-export.

mem

Re-export.

schema

Types related to contract schemas. These are optional annotations in modules that allow the users of smart contracts to interact with them in a way that is better than constructing raw bytes as parameters.

test_infrastructure

The test infrastructure module provides alternative implementations of HasInitContext, HasReceiveContext, HasParameter, HasActions, and HasContractState traits intended for testing.

Macros

bail

The bail macro can be used for cleaner error handling. If the function has result type Result invoking bail will terminate execution early with an error. If an argument is supplied, this will be used as the error, otherwise it requires the type E in Result<_, E> to implement the Default trait.

claim

The claim macro is used for testing as a substitute for the assert macro. It checks the condition and if false it reports back an error. Used only in testing.

claim_eq

Ensure the first two arguments are equal, just like assert_eq!, otherwise reports an error. Used only in testing.

claim_ne

Ensure the first two arguments are not equal, just like assert_ne!, otherwise reports an error. Used only in testing.

ensure

The ensure macro can be used for cleaner error handling. It is analogous to assert, but instead of panicking it uses bail to terminate execution of the function early.

ensure_eq

Variants of ensure for ease of use in certain contexts.

ensure_ne

Ensure the first two arguments are not equal, using bail otherwise.

fail

The fail macro is used for testing as a substitute for the panic macro. It reports back error information to the host. Used only in testing.

Structs

AccountAddress

Address of an account, as raw bytes.

Action

Actions that can be produced at the end of a contract execution. This type is deliberately not cloneable so that we can enforce that and_then and or_else can only be used when more than one event is created.

Amount

The type of amounts on the chain

ChainMetaExtern
ChainMetadata

Chain metadata accessible to both receive and init methods.

ContractAddress

Address of a contract.

ContractState

A type representing the constract state bytes.

Cursor

Add offset tracking inside a data structure.

InitContext

Chain context accessible to the init methods.

InitContextExtern
Logger

A type representing the logger.

Parameter

A type representing the parameter to init and receive methods.

ParseError

Zero-sized type to represent an error when reading bytes and deserializing.

ReceiveContext

Chain context accessible to the receive methods.

ReceiveContextExtern
Reject

A non-descript error message, signalling rejection of a smart contract invocation.

Enums

Address

Either an address of an account, or contract.

AmountParseError

An error indicating why parsing of an amount failed. Since amount parsing is typically a user-facing activity this is fairly precise, so we can notify the user why we failed, and what they can do to fix it.

SeekFrom

This is the equivalent to the SeekFrom type from the rust standard library, but reproduced here to avoid dependency on std::io.

Constants

ACCOUNT_ADDRESS_SIZE

Size of an account address when serialized in binary. NB: This is different from the Base58 representation.

Traits

Deserial

The Deserial trait provides a means of reading structures from byte-sinks (Read).

Get

A more convenient wrapper around Deserial that makes it easier to write deserialization code. It has a blanked implementation for any read and serialize pair. The key idea is that the type to deserialize is inferred from the context, enabling one to write, for example,

HasActions

An object that can serve to construct actions.

HasChainMetadata

Objects which can access chain metadata.

HasContractState

A type that can serve as the contract state type.

HasInitContext

Types which can act as init contexts.

HasLogger

Objects which can serve as loggers.

HasParameter

Objects which can access parameters to contracts.

HasReceiveContext

Types which can act as receive contexts.

Read

The Read trait provides a means of reading from byte streams.

Seek

The Seek trait provides a cursor which can be moved within a stream of bytes. This is essentially a copy of std::io::Seek, but avoiding its dependency on std::io::Error, and the associated code size increase.

Serial

The Serial trait provides a means of writing structures into byte-sinks (Write).

Serialize

The Serialize trait provides a means of writing structures into byte-sinks (Write) or reading structures from byte sources (Read).

Write

The Write trait provides functionality for writing to byte streams.

Functions

deserial_map_no_length

Read a Map as an list of key-value pairs given some length. NB: This ensures there are no duplicates, hence the specialized type. Moreover this will only succeed if keys are listed in order.

deserial_map_no_length_no_order_check

Read a Map as an list of key-value pairs given some length. Slightly faster version of deserial_map_no_length as it is skipping the order checking

deserial_set_no_length

Read a Set as an list of keys, given some length. NB: This ensures there are no duplicates, hence the specialized type. Moreover this will only succeed if keys are listed in order.

deserial_set_no_length_no_order_check

Read a Set as an list of key-value pairs given some length. Slightly faster version of deserial_set_no_length as it is skipping the order checking.

deserial_vector_no_length

Read a vector given a length.

from_bytes

Dual to to_bytes.

put_in_memory

Allocates a Vec of bytes prepended with its length as a u32 into memory, and prevents them from being dropped. Returns the pointer. Used to pass bytes from a Wasm module to its host.

serial_map_no_length

Write a Map as a list of key-value pairs ordered by the key, without the length information.

serial_set_no_length

Write a Set as an list of keys ordered, without the length information.

serial_vector_no_length

Write a vector without including length information.

to_bytes

Serialize the given value to a freshly allocated vector of bytes using the provided Serial instance.

trap

Terminate execution immediately without panicking. When the std feature is enabled this is just std::process::abort. When std is not present and the target architecture is wasm32 this will simply emit the unreachable instruction.

Type Definitions

BlockHeight

Height of the block.

FinalizedHeight

Finalized height. In the context of chain metadata this is the height of the block which is explicitly recorded as the last finalized block in the block under consideration.

InitResult

The expected return type of the init method of the smart contract, parametrized by the state type of the smart contract.

ParseResult

A type alias used to indicate that the value is a result of parsing from binary via the Serial instance.

ReceiveResult

The expected return type of the receive method of a smart contract.

SlotNumber

Sequential slot number

SlotTime

Time at the beginning of the current slot, in miliseconds.

Attribute Macros

concordium_cfg_test

Sets the cfg for testing targeting either Wasm and native.

concordium_test

Derive the appropriate export for an annotated test function, when feature "wasm-test" is enabled, otherwise behaves like #test.

contract_state
init

Derive the appropriate export for an annotated init function.

receive

Derive the appropriate export for an annotated receive function.

Derive Macros

Deserial

Derive the Deserial trait. See the documentation of derive(Serial) for details and limitations.

SchemaType
Serial

Derive the Serial trait for the type.

Serialize

A helper macro to derive both the Serial and Deserial traits. [derive(Serialize)] is equivalent to [derive(Serial,Deserial)], see documentation of the latter two for details and options.