Example 1: Setting Prizing Pool


assert(is_owner(),'You`re not the contract`s owner')
    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 -Note: the subscript of array lua starts with 1
            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
end

lucky draw,ticket:prize ticket,amount:token amount,asset: token type


    assert(amount>=1000000,'amount should not be less than 1000000')
    assert(asset=='COCOS','asset error')
local total=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))
    transfer_from_caller(contract_base_info.owner,amount,asset,true)
    local index=random()%(#public_data.ticket_pool_2)+1  --Note: the subscript of array lua starts with 1
local rate=0
    if(public_data.ticket_pool_2[index]==ticket)then
        while(true)do
rate =random()%(#public_data.ticket_pool_2)
if(rate*amount<=total) then
transfer_from_owner(contract_base_info.caller,rate*amount,asset,true)
break
end
end
    end
log('ticket:'..public_data.ticket_pool_2[index]..',rate:'..rate)
end

Random accumulation function, used to verify on-chain random number (for testing)

function sum_public_by_rand()
assert(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+random()%100
        else
            public_data.sum_rand_10=public_data.sum_rand_10+random()%100
        end
    end
end

Example 1: Setting Prizing Pool


Suggested Edits are limited on API Reference Pages

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