Documentation
¶
Overview ¶
Package types contains shared type definitions and constants used by both the host environment and WebAssembly contracts
Index ¶
- Constants
- type Address
- type BlockchainContext
- type CallParams
- type CallResult
- type Context
- type DeleteObjectParams
- type ExecutionResult
- type GetObjectFieldParams
- type GetObjectParams
- type GetObjectWithOwnerParams
- type HandleContractCallParams
- type Hash
- type LogParams
- type Object
- type ObjectID
- type SetObjectFieldParams
- type SetOwnerParams
- type TransferParams
- type VMObject
- type WasmFunctionID
Constants ¶
const HostBufferSize int32 = 2048
HostBufferSize defines the size of the buffer used for data exchange between host and contract
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BlockchainContext ¶
type BlockchainContext interface {
// set block info and transaction info
SetBlockInfo(height uint64, time int64, hash Hash) error
SetTransactionInfo(hash Hash, from Address, to Address, value uint64) error
// Blockchain information related
BlockHeight() uint64 // Get current block height
BlockTime() int64 // Get current block timestamp
ContractAddress() Address // Get current contract address
TransactionHash() Hash // Get current transaction hash
SetGasLimit(limit int64) // Set gas limit
GetGas() int64 // Get used gas
// Account operations related
Sender() Address // Get transaction sender or contract caller
Balance(addr Address) uint64 // Get account balance
Transfer(contract, from, to Address, amount uint64) error // Transfer operation
// Object storage related - Basic state operations use panic instead of returning error
CreateObject(contract Address) (VMObject, error) // Create new object
CreateObjectWithID(contract Address, id ObjectID) (VMObject, error) // Create new object
GetObject(contract Address, id ObjectID) (VMObject, error) // Get specified object
GetObjectWithOwner(contract Address, owner Address) (VMObject, error) // Get object by owner
DeleteObject(contract Address, id ObjectID) error // Delete object
// Cross-contract calls
Call(caller Address, contract Address, function string, args ...any) ([]byte, error)
// Logs and events
Log(contract Address, eventName string, keyValues ...any) // Log event
}
Context 是合约与区块链环境交互的主要接口
type CallParams ¶
type CallResult ¶
type Context ¶ added in v0.1.2
type Context interface {
// Blockchain information related
BlockHeight() uint64 // Get current block height
BlockTime() int64 // Get current block timestamp
ContractAddress() Address // Get current contract address
// Account operations related
Sender() Address // Get transaction sender or contract caller
Balance(addr Address) uint64 // Get account balance
Transfer(from, to Address, amount uint64) error // Transfer operation
// Object storage related - Basic state operations use panic instead of returning error
CreateObject() Object // Create new object, panic on failure
GetObject(id ObjectID) (Object, error) // Get specified object, may return error
GetObjectWithOwner(owner Address) (Object, error) // Get object by owner, may return error
DeleteObject(id ObjectID) // Delete object, panic on failure
// Cross-contract calls
Call(contract Address, function string, args ...any) ([]byte, error)
// Logs and events
Log(eventName string, keyValues ...any) // Log event
}
Context 是合约与区块链环境交互的主要接口
type DeleteObjectParams ¶
type ExecutionResult ¶
type GetObjectFieldParams ¶
type GetObjectParams ¶
type Object ¶ added in v0.1.2
type Object interface {
ID() ObjectID // Get object ID
Owner() Address // Get object owner
Contract() Address // Get object's contract
SetOwner(addr Address) // Set object owner, panic on failure
// Field operations
Get(field string, value any) error // Get field value
Set(field string, value any) error // Set field value
}
Object 接口用于管理区块链状态对象
type SetObjectFieldParams ¶
type SetOwnerParams ¶
type TransferParams ¶
type VMObject ¶
type VMObject interface {
ID() ObjectID // Get object ID
Owner() Address // Get object owner
Contract() Address // Get object's contract
SetOwner(contract, sender, addr Address) error // Set object owner
// Field operations
Get(contract Address, field string) ([]byte, error) // Get field value
Set(contract, sender Address, field string, value []byte) error // Set field value
}
Object 接口用于管理区块链状态对象
type WasmFunctionID ¶
type WasmFunctionID int32
WasmFunctionID defines constants for function IDs used in host-contract communication These constants must be used on both sides (host and contract) to ensure compatibility
IMPORTANT: These function IDs are critical for the communication between the host environment and WebAssembly contracts. Any mismatch in the function ID values between the host and contract will result in undefined behavior or system failures.
Always import and use these constants in both host and contract code, rather than defining separate constants in different parts of the codebase. This ensures consistent communication and prevents hard-to-debug errors due to mismatched function IDs.
Example usage in host code:
const FuncGetSender = int32(types.FuncGetSender)
Example usage in contract code:
const FuncGetSender = int32(types.FuncGetSender)
const ( // FuncGetSender returns the address of the sender (caller) of the current transaction FuncGetSender WasmFunctionID = iota + 1 // 1 // FuncGetContractAddress returns the address of the current contract FuncGetContractAddress // 2 // FuncTransfer transfers tokens from the contract to a recipient FuncTransfer // 3 // FuncCreateObject creates a new state object FuncCreateObject // 4 // FuncCall calls a function on another contract FuncCall // 5 // FuncGetObject retrieves a state object by ID FuncGetObject // 6 // FuncGetObjectWithOwner retrieves objects owned by a specific address FuncGetObjectWithOwner // 7 // FuncDeleteObject removes a state object FuncDeleteObject // 8 // FuncLog logs a message to the blockchain's event system FuncLog // 9 // FuncGetObjectOwner gets the owner of a state object FuncGetObjectOwner // 10 // FuncSetObjectOwner changes the owner of a state object FuncSetObjectOwner // 11 // FuncGetObjectField retrieves a specific field from a state object FuncGetObjectField // 12 // FuncSetObjectField updates a specific field in a state object FuncSetObjectField // 13 // FuncGetObjectContract gets the contract of a state object FuncGetObjectContract // 14 )