# Examples
-- Hello, this is a example contract
-- Storage Type
type Storage = {
name: string,
age: int,
money: number,
is_man: bool,
int_table: Map<int>,
int_array: Array<int>
}
-- Announce a contract type M
let M = Contract<Storage>()
-- init function, M is the variance
function M:init()
pprint('contract demo init')
self.storage['name'] = 'zhangsan' -- Here is to 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
-- Define an API named set for the contract, and the parameter is a string-type parameter named 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 the transfer from the contract account to the user account
transfer_from_contract_to_address(user_address_here, get_system_asset_symbol(), 10000)
end
function M:get(arg: string)
pprint('test3 contract demo get')
pprint(self.storage['name']) -- Here is to read the value of the name attribute in the storage of the 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
← local scripts HOWTO →