ExamplesΒΆ

-- Hello, this is a example contract

-- Type of storage that defines the contract
type Storage = {
    name: string,
    age: int,
    money: number,
    is_man: bool,
    int_table: Map<int>,
    int_array: Array<int>
}

-- Declare a variable M for the type of contract
let M = Contract<Storage>()

-- Define an initialization function for the contract, where M stands for the variable of the contract
function M:init()
    pprint('contract demo init')
    self.storage['name'] = 'zhangsan'  -- Initialize the storage properties of the contract. The following code is similar.
    self.storage['age'] = 16
    self.storage['money'] = 1.1345
    self.storage['is_man'] = true
    self.storage['int_table'] = {a: 1,b: 2, c: 3,d: 4}
    self.storage['int_array'] = [5,6,7,8]
end

-- Defines an API named 'set' for the contract, which has a string type parameter 'name'
function M:set(name: string)
    pprint('contract demo set')
    self.storage['name'] = 'lisi'
    self.storage['age'] = 14
    self.storage['money'] = 5.3456
    self.storage['is_man'] = false
    self.storage['int_table'].a = 15
    self.storage['int_table'].f = 10
    table.remove(self.storage['int_array'], 1)
    pprint('after remove')
    self.storage['int_array'][1] = 99
    pprint('after set array by index')
    pprint(self.storage.int_array)
    self.storage.int_array[#self.storage.int_array+1] = 20
    table.insert(self.storage['int_array'], 19)
    pprint('insert value to array')
-- Perform an operation from a contract account to a user account
    transfer_from_contract_to_address("ACTB3MmTLBoh8KSokqdk1pcN6zxBKxaVeUeRffffffffffffffffffffffffffffffff", "ACT", 10000)
end

function M:get(arg: string)
    pprint('test3 contract demo get')
    pprint(self.storage['name'])  -- Read the property 'name' in the storage of contract and output
    pprint(self.storage['age'])
    pprint(self.storage['money'])
    pprint(self.storage['is_man'])
    pprint(self.storage['int_table'])
    pprint(self.storage['int_array'])
end

return M