Smart Contract Tutorials And Examples

Hello World

function logtest()
	chainhelper:log('hello world')
	chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time()))
end

Acquire Block Header Time

function timetest() 
	public_data.time=date('%Y-%m-%dT%H:%M:%S', chainhelper:time()) 
	write_list={public_data={time=true}} 
	chainhelper:write_chain() 
end

Transfer

function transfer(amount,symbol,islog) 
	chainhelper:transfer_from_caller(contract_base_info.owner,amount,symbol,islog) --Transfer homogeneous assets from contract owners to accounts
	chainhelper:transfer_from_owner(contract_base_info.caller,amount,symbol,islog) --Transfer homogeneous assets from contract callers to accounts
end

Assets Lock-Up Configuration

function init()
	assert(chainhelper:is_owner(),'chainhelper:is_owner()')
	read_list={public_data={is_init=true}}
	chainhelper:read_chain()
	assert(public_data.is_init==nil,'public_data.is_init==nil')
	public_data.locked=0
	public_data.is_init=true
	chainhelper:write_chain()
end

function pay_with_lock(amount,symbol)
	assert(amount>0,'amount > 0')
	read_list={public_data={}}
	chainhelper:read_chain()
	assert(public_data.locked+amount>public_data.locked,'public_data.locked+amount>public_data.locked')
	public_data.locked=public_data.locked+amount;  --[[Adjust lock-up record]] 
	assert(public_data.locked<=chainhelper:number_max(),'public_data.locked<=chainhelper:number_max()')
	chainhelper:transfer_from_caller(contract_base_info.owner,amount,symbol,true)
--[[Deposit token]]
	chainhelper:adjust_lock_asset(symbol,amount)  --[[Lock up token]]
	write_list=read_list
	chainhelper:write_chain()
end	

function adjustment_lock_and_transfer(to,amount,symbol)
	assert(chainhelper:is_owner(),'chainhelper:is_owner()')
	read_list={public_data={}}
	chainhelper:read_chain()
	assert(amount > 0 and public_data.lockedamount >= 0, 'amount>0 and public_data.locked-amount>=0')
	public_data.locked=public_data.locked-amount  --[[Adjust lock-up record]]
	chainhelper:adjust_lock_asset(symbol,-amount)	  --[[Unlock token]]
	if(to~=contract_base_info.owner)then
		chainhelper:transfer_from_owner(to,amount,symbol,true)  --[[Withdraw token]]
	end
	write_list=read_list
	chainhelper:write_chain()
end

Generate/ Verify Hash

function set_hash(source) 
	public_data.hash256=chainhelper:hash256(source) 
	public_data.hash512=chainhelper:hash512(source) 
	chainhelper:write_chain() --Default:write_list={public_data={},private_data={}} ,Update all the data, this contract contains only 2 pieces of data: hash256 and hash512
end 
function check_hash(source) 
	chainhelper:read_chain() --Default:read_list={public_data={},private_data={}} ,Load in all the data,this contract contains only 2 pieces of data: hash256 and hash512
	assert(public_data.hash256==chainhelper:hash256(source),'hash256 check error') 
	assert(public_data.hash512==chainhelper:hash512(source),'hash512 check error') 
end

Describe Modified Non-Homogeneous Assets

--Modify non-homogeneous assets description through json string
function my_nht_describe_change( nht_hash_or_id,values_json) 
	local values=cjson.decode(values_json) 
	for key,value in pairs(values)  do  
		chainhelper:log('key:'..key..',value:'..value)   
		chainhelper:nht_describe_change( nht_hash_or_id,key,value,true)  
	end  
end

--Modify non-homogeneous assets description through lua table
function nht_change_with_table( nht_hash_or_id,values_table) 
	for key,value in pairs(values_table)  do  
		chainhelper:log('key:'..key..',value:'..value)   
		chainhelper:nht_describe_change( nht_hash_or_id,key,value,true)  
	end  
end

Random Function Examples

--A function example is with ten times of random accumulations as well as output
function sum_public_by_rand()
    read_list={public_data={sum_rand_10=true}}
chainhelper:read_chain()
--Refuse callers’ interface permission through assert and is_owner
	assert(chainhelper:is_owner(),'You`re not the contract`s owner')
    local i=0
    while(i<10)do
        i=i+1
        if(public_data.sum_rand_10==nil)then
            public_data.sum_rand_10=0
            public_data.sum_rand_10=public_data.sum_rand_10+chainhelper:random()%100
--The value of % after random() indicates randomness range, for instance, randomness ranges from 0 to 99 in this example. 
        else
            public_data.sum_rand_10=public_data.sum_rand_10+chainhelper:random()%100
        end
    end
    write_list={public_data={sum_rand_10=true}}
    chainhelper:write_chain()
	chainhelper:log('date:'..date('%Y-%m-%dT%H:%M:%S', chainhelper:time())..',sum:'..public_data.sum_rand_10) end
end

Lucky Draw Game Examples

--Set prize pool
function set_ticket(ticket)  	  		
  assert(chainhelper:is_owner(),'You`re not the contract`s owner')    		
  read_list={public_data={ticket_pool_1=true,ticket_pool_2=true}}  
  chainhelper:read_chain()  
  if(public_data.ticket_pool_1==nil)then  
	public_data.ticket_pool_1={}  
	public_data.ticket_pool_2={}  
	if (public_data.ticket_pool_1[ticket]==nil) then    		
		public_data.ticket_pool_2[#public_data.ticket_pool_2+1]=ticket  
		public_data.ticket_pool_1[ticket]=#public_data.ticket_pool_2  
	end     
  else
	if (public_data.ticket_pool_1[ticket]==nil) then  
		public_data.ticket_pool_2[#public_data.ticket_pool_2+1]=ticket  
		public_data.ticket_pool_1[ticket]=#public_data.ticket_pool_2  
	end     
  end
  assert(#public_data.ticket_pool_2<=12)
  write_list={public_data={ticket_pool_1={[ticket]=true},ticket_pool_2={[#public_data.ticket_pool_2]=true}}}  
  chainhelper:write_chain()  
  end

--Raffle
function luck_draw(ticket,amount,asset)  
  assert(amount>=1000000,'amount should not be less than 1000000')  
  assert(asset=='COCOS','asset error')  
  read_list={public_data={ticket_pool_1=true,ticket_pool_2=true}}  
  chainhelper:read_chain()  	
  local total=chainhelper:get_account_balance(contract_base_info.owner,asset)  	
  assert((#public_data.ticket_pool_2)*amount<=total,'Current maximum compensation ratio:'..#public_data.ticket_pool_2..'The biggest bet at present:'..total/(#public_data.ticket_pool_2))  
  chainhelper:transfer_from_caller(contract_base_info.owner,amount,asset,true)  
  local index=chainhelper:random()%(#public_data.ticket_pool_2)+1  	
  local rate = chainhelper:random()%(#public_data.ticket_pool_2)+1
  chainhelper:log('luck:'..public_data.ticket_pool_2[index]..',rate:'..rate)
  if(public_data.ticket_pool_2[index]==ticket)then  
	chainhelper:transfer_from_owner(contract_base_info.caller,rate*amount,asset,true)  	
  end
 end
 --Reset prize pool
function reset_ticket() 
	write_list_list={public_data={ticket_pool_1=false,ticket_pool_2=false}}
	chainhelper:write_chain()
end

Smart Contract Tutorials And Examples


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.