sophia/test/contracts/environment.aes
Ulf Norell 5aed8b3ef5 Check stateful annotations
Functions must be annotated as `stateful` in order to
- Update the contract state (using `put`)
- Call `Chain.spend` or other primitive functions that cost tokens
- Call an Oracle or AENS function that requires a signature
- Make a remote call with a non-zero value
- Construct a lambda calling a stateful function

It does not need to be stateful to
- Read the contract state
- Call another contract with value=0, even when the remote function is stateful
2019-05-13 13:39:17 +02:00

70 lines
1.7 KiB
Plaintext

// Testing primitives for accessing the block chain environment
contract Interface =
function contract_address : () => address
function call_origin : () => address
function call_caller : () => address
function call_value : () => int
contract Environment =
record state = {remote : Interface}
function init(remote) = {remote = remote}
stateful function set_remote(remote) = put({remote = remote})
// -- Information about the this contract ---
// Address
function contract_address() : address = Contract.address
function nested_address(who) : address =
who.contract_address(gas = 1000)
// Balance
function contract_balance() : int = Contract.balance
// -- Information about the current call ---
// Origin
function call_origin() : address = Call.origin
function nested_origin() : address =
state.remote.call_origin()
// Caller
function call_caller() : address = Call.caller
function nested_caller() : address =
state.remote.call_caller()
// Value
function call_value() : int = Call.value
stateful function nested_value(value : int) : int =
state.remote.call_value(value = value / 2)
// Gas price
function call_gas_price() : int = Call.gas_price
// -- Information about the chain ---
// Account balances
function get_balance(acct : address) : int = Chain.balance(acct)
// Block hash
function block_hash(height : int) : int = Chain.block_hash(height)
// Coinbase
function coinbase() : address = Chain.coinbase
// Block timestamp
function timestamp() : int = Chain.timestamp
// Block height
function block_height() : int = Chain.block_height
// Difficulty
function difficulty() : int = Chain.difficulty
// Gas limit
function gas_limit() : int = Chain.gas_limit