def interest_injection(amount): # 利息注入 sender = GetTxSender() interest_balance = query_interest_balance() # 查询盒子注入利息 user_interest_amount = query_user_inject_interest(sender) # 用户对盒子注入的利息 user_inject_key = concat(KEY_USER_INJECT_INTEREST, sender) if BalanceOf(sender, [GARD_DENOM])[0] <= amount: raise Exception("余额不足") if query_box_status() != ISSUE_STATUS: raise Exception("当前不处于发行期") if amount > BOX_INTEREST: raise Exception("大于最大应存利息数") if not interest_balance: Put(KEY_BOX_INTEREST, amount) # 记录利息 else: sub = BOX_INTEREST - interest_balance if amount > sub: raise Exception("超过可存入的利息总额") else: Put(KEY_BOX_INTEREST, interest_balance + amount) if not user_interest_amount: # 提交用户注入利息 Put(user_inject_key, amount) else: Put(user_inject_key, user_interest_amount + amount) ContractBalanceInject(sender, GARD_DENOM, amount) # 转账利息到合约地址 return True
def creat_box(): # 创建盒子,即 init if Get(KEY_OWNER): raise Exception("已经创建过") Put(KEY_OWNER, OWNER) # 合约 owner Put(KEY_BOX_TRANSFER_ON, BOX_TRANSFER_ON) # 用户存款后的存款凭证是否可以进行交易 return True
def upload_data(score, km, data, time): # 1.1 upload [score, km, data, time] for a round-user round = getRound(time) addr = GetTxSender() roundUserKey = IntToStr(round) + addr record = [score, km, data, time] recordList = Get(roundUserKey) if len(recordList) == 0: Put(roundUserKey, [record]) else: recordList[len(recordList)] = record Put(roundUserKey, recordList) # 1.2 upload total score totalScore = Get(KEY_TOTAL_SCORE) Put(KEY_TOTAL_SCORE, totalScore + score) # 1.3 upload address list for a round addressList = Get(round) if len(addressList) == 0: Put(round, [addr]) else: is_existed = False for a in addressList: if a == addr: is_existed = True break if not is_existed: addressList[len(addressList)] = addr Put(round, addressList) return True
def bingo(call): # 用户中奖按兑奖 bingo sender = GetTxSender() now_period = issue() # 当期 user_table = query_user_table_generation(sender, now_period) add_user_grade(call) if len(user_table) == 0: raise Exception("当期没有购买卡单") if query_period_bingo_status(now_period): raise Exception("当期已经有人中奖了,无法 BINGO, 请等待当期结束,下次手速快点哦!") if not draw_rules(sender, now_period): raise Exception("当期没有中奖") bingo_key = concat(KEY_GAME_BINGO_STATUS, now_period) Put(bingo_key, True) # 将当期改为 bingo 为 true 的状态 multiple_key = concat(concat(KEY_MULTIPLE, sender), now_period) # 用户该期的倍数 multiple = Get(multiple_key) amount = multiple * 10000 * GARD_FACTOR withdraw_amount_key = concat(KEY_USER_WITHDRAWS_AMOUNT, sender) withdraw_user_amount = query_user_withdraw_amount(sender) # 查询用户兑奖的奖励 if not withdraw_user_amount: Put(withdraw_amount_key, amount) else: withdraw_user_amount = withdraw_user_amount + amount Put(withdraw_amount_key, withdraw_user_amount) # Put 用户兑奖的奖励 ContractBalanceSend(sender, GARD_DENOM, amount) # 给用户转钱 return True
def withdraw(period): # 用户bingo以往的,场景:用户提交标记号码,中奖了,可能没有及时bingo sender = GetTxSender() grade_list = query_user_grade(sender, period) # 用户标记的号码 if len(grade_list) == 0: raise Exception("用户该期没有标记过号码") if query_period_bingo_status(period): raise Exception("当期已经有人中奖了,无法 BINGO, 请等待当期结束,下次手速快点哦!") user_table = query_user_table_generation(sender, period) if len(user_table) == 0: raise Exception("当期没有购买卡单") if not draw_rules(sender, period): raise Exception("当期没有中奖") bingo_key = concat(KEY_GAME_BINGO_STATUS, period) Put(bingo_key, True) # 将当期改为 bingo 为 true 的状态 multiple_key = concat(concat(KEY_MULTIPLE, sender), period) # 用户该期的倍数 multiple = Get(multiple_key) amount = multiple * 10000 * GARD_FACTOR withdraw_amount_key = concat(KEY_USER_WITHDRAWS_AMOUNT, sender) withdraw_user_amount = query_user_withdraw_amount(sender) # 查询用户兑奖的奖励 if not withdraw_user_amount: Put(withdraw_amount_key, amount) else: withdraw_user_amount = withdraw_user_amount + amount Put(withdraw_amount_key, withdraw_user_amount) # Put 用户兑奖的奖励 ContractBalanceSend(sender, GARD_DENOM, amount) # 给用户转账 return True
def deposit(amount): # 存款 sender = GetTxSender() if BalanceOf(sender, [GARD_DENOM])[0] <= amount: raise Exception("余额不足") if amount % BOX_PRICE != 0: raise Exception("存款必须是最少所存量的倍数") if query_box_status() != DEPOSIT_STATUS: raise Exception("当前不处于存款吸纳期,无法存款") if amount > BOX_CEILING: raise Exception("大于最大允许的存款量") deposit_amount = query_deposit_amount() # 盒子存款总量 if not deposit_amount: Put(KEY_BOX_DEPOSIT_AMOUNT, amount) else: sub = BOX_CEILING - deposit_amount if amount > sub: raise Exception("超过可存入的最大存款数") else: Put(KEY_BOX_DEPOSIT_AMOUNT, deposit_amount + amount) # 提交存款盒子总量 user_deposit_amount = query_user_deposit_amount(sender) user_deposit_key = concat(KEY_USER_DEPOSIT_AMOUNT, sender) if not user_deposit_amount: Put(user_deposit_key, amount) else: Put(user_deposit_key, user_deposit_amount + amount) # 提交用户的的存款量 ContractBalanceInject(sender, GARD_DENOM, amount) # 给盒子转账 return True
def description(var_org, var_website, var_logo, var_intro): # 添加描述 sender = GetTxSender() if Get(KEY_OWNER) != sender: raise Exception("请使用 owner 账户给盒子更改描述") Put(KEY_ORG, var_org) Put(KEY_WEBSITE, var_website) Put(KEY_LOGO, var_logo) Put(KEY_INTRO, var_intro) return True
def inject_system_pool(amount): # 系统奖池注入 sender = GetTxSender() system_amount = syspool() if not system_amount: Put(KEY_SYSTEM_POOL, amount) else: system_amount = system_amount + amount Put(KEY_SYSTEM_POOL, system_amount) ContractBalanceInject(sender, GARD_DENOM, amount)
def inject_stake_pool(amount): # 用户奖池注入 sender = GetTxSender() stake_amount = stakepool() if not stake_amount: Put(KEY_STAKE_POOL, amount) else: stake_amount = stake_amount + amount Put(KEY_STAKE_POOL, stake_amount) ContractBalanceInject(sender, GARD_DENOM, amount) return True
def init(): if Get(KEY_OWNER): raise Exception("已经初始化过") time = GetTime() Put(KEY_OWNER, OWNER) # Put 合约的 owenr now_period = get_period_generation() Put(KEY_GAME_PERIOD, now_period) # Put 最开始的期号 time_key = concat(KEY_GAME_TIME, now_period) Put(time_key, time) # Put 最开始的期号时间戳 period_ls = [now_period] PutArray(KEY_GAME_LIST, period_ls) # Put 期数列表
def init(): # check if token inited if Get(KEY_OWNER): return False Put(KEY_OWNER, OWNER) total = TOTAL_SUPPLY * FACTOR Put(KEY_SUPPLY, total) TokenInit(OWNER, total, TOKEN_PREFIX) return True
def stake(game_type, operation_type): # 用户购买 sender = GetTxSender() user_list = emoji_random() # 随机表情列表 period = get_period_generation() # 当期期号 if query_user_time_condition(sender): # 查询是否过去5秒 stake_info_key = concat(concat(KEY_USER_STAKE_INFO, sender), period) PutArray(stake_info_key, user_list) # Put 用户购买后产生的列表 # Put 用户的投注列表,最多保持24个记录 stake_list = query_user_stake_list(sender) stake_list_key = concat(KEY_USER_STAKE_LIST, sender) if len(stake_list) == 0: PutArray(stake_list_key, [period]) if len(stake_list) == 24: new_stake_list = [] for i in range(len(stake_list)): if i != len(stake_list) - 1: new_stake_list.append(stake_list[i + 1]) new_stake_list.append(period) PutArray(stake_list_key, new_stake_list) else: new_stake_list = [] for i in range(len(stake_list)): new_stake_list.append(stake_list[i]) new_stake_list.append(period) PutArray(stake_list_key, new_stake_list) period_type_key = concat(concat(KEY_USER_PERIOD_TYPE, sender), period) Put(period_type_key, game_type) # Put 用户当期期数的类型,即表情包 amount = 200 * GARD_FACTOR if BalanceOf(sender, [GARD_DENOM])[0] < amount: raise Exception("余额不足") inject_stake_pool(amount) # 给合约地址转钱 add_user_stake_amount(sender, amount) # 加入自己的消费额中 time = GetTime() period_time_key = concat(concat(KEY_USER_PERIOD_TIME, sender), period) Put(period_time_key, time) # Put 用户期数的时间戳 save_period_key = concat(KEY_SAVE_PERIOD, sender) if operation_type == 1: # 1为 直接stake save_period_key = concat(KEY_SAVE_PERIOD, sender) PutArray(save_period_key, []) # 把用户保存的表清空 if operation_type != 1: # 0 为继续 save_period_list = GetArray(save_period_key) # 查询用户保存的期号列表 if len(save_period_list) == 0: raise Exception("当期不是继续的场景") return True else: raise Exception("请等待时间")
def invitation_code_generation(): # 邀请码生成 sender = GetTxSender() # 获取当前操作人 for i in range(5, len(sender)): invitation_code = concat(concat(sender[i], sender[i + 1]), concat(sender[i + 2], sender[i + 3])) if query_invitation_code_user(invitation_code): # 判断该邀请码是否已经有归属地址 continue else: key = concat(KEY_INVITATION_CODE, sender) Put(key, invitation_code) # 提交用户的邀请码信息 key = concat(KEY_INVITATION_CODE, invitation_code) Put(key, sender) # 提交邀请码对应的人的信息 break return True
def set_event_pool(amount): # 设立活动奖池 sender_address = GetTxSender() if BalanceOf(sender_address, [GARD_DENOM])[0] < amount: # 判断余额是否足够初始化 raise Exception("余额不足") event_pool = Get(KEY_EVENT_POOL) if not event_pool: ContractBalanceInject(sender_address, GARD_DENOM, amount) Put(KEY_EVENT_POOL, amount) return True balance_amount = event_pool + amount Put(KEY_EVENT_POOL, balance_amount) ContractBalanceInject(sender_address, GARD_DENOM, balance_amount) return True
def system_prize_pool_inject(amount): # 给系统奖池授予额度 sender = GetTxSender() if BalanceOf(sender, [GARD_DENOM])[0] < amount: # 判断余额是否足够初始化 raise Exception("余额不足") sys_pool = Get(KEY_SYSTEM_POOL) if not sys_pool: ContractBalanceInject(sender, GARD_DENOM, amount) Put(KEY_SYSTEM_POOL, amount) # 记录系统奖池额度 return True balance_amount = sys_pool + amount ContractBalanceInject(sender, GARD_DENOM, balance_amount) Put(KEY_SYSTEM_POOL, balance_amount) # 记录总的奖池额度 return True
def init(): if Get(KEY_OWNER): return False now_time = GetTime() Put(KEY_OWNER, OWNER) # 添加owner 地址 Put(KEY_NUMBER_DRAWS, get_period_generation()) # 当前期数 ,格式:20190925 draws_time_key = concat(Get(KEY_NUMBER_DRAWS), KEY_PERIODS_TIME) Put(draws_time_key, now_time) # 当前期号对应的时间戳 note_key = concat(Get(KEY_NUMBER_DRAWS), KEY_CALUCLATION_NOTE) Put(note_key, 0) # 投注数 key = concat(Get(KEY_NUMBER_DRAWS), KEY_DRAWS_PID) Put(key, get_pid()) # 初始期数的pid PutArray(KEY_PERIODS_LIST, [Get(KEY_NUMBER_DRAWS)]) # 存放一天内的期号列表 return True
def transfer_from(spender, holder, to, amount): # check if spender_address is tx sender if not IsValid(spender) or not IsValid(holder) or not IsValid(to): raise Exception("Address length error.") if GetTxSender() != spender: raise Exception("Please use the operator account.") if amount < 0: raise Exception("Please enter the correct amount.") # check allowance key = concat(concat(KEY_APPROVE, holder), spender) approved = Get(key) if approved < amount: raise Exception("The authorized amount is less than the account balance.") # check balance of holder if balance_of(holder) < amount: raise Exception("Insufficient balance.") # send token and update allowance TokenSend(holder, to, amount) if approved == amount: Delete(key) if approved > amount: Put(key, approved - amount) return True
def change_owner(address): # 改变 owner 地址 if not IsValid(address): raise Exception("地址格式错误") if GetTxSender() != Get(KEY_OWNER): raise Exception("请使用 owner 地址调用") Put(KEY_OWNER, address) return True
def issue(total, symbol, symbol_des): sender = GetTxSender() # 获取操作者地址 if issue_owner(symbol): raise Exception("此积分已发行") AssetInit(sender, total, symbol) # 给操作者发行积分 issue_num = issue_number() if not issue_num: # 如果通过该合约暂时没有积分 Put(KEY_ISSUE_NUMBER, 1) else: Put(KEY_ISSUE_NUMBER, issue_num + 1) des_key = concat(KEY_DESCRIPTION, symbol) Put(des_key, symbol_des) # Put 此积分的描述 return True
def init(): # check if contract inited if Get(KEY_OWNER): return False Put(KEY_OWNER, OWNER) return True
def describe(operator, info): # check if operator is token owner if not IsValid(operator): raise Exception("Address length error.") if GetTxSender() != operator: raise Exception("Please use the operator account.") if operator != Get(KEY_OWNER): raise Exception("Please use the owner address.") Put(KEY_DESCRIPTION, info) return True
def redeem(amount): # 赎回,取出存款 sender = GetTxSender() box_deposit_amount = query_deposit_amount() # 查询盒子的总存款 user_deposit_amount = query_user_deposit_amount(sender) # 查询该用户的存款 user_deposit_key = concat(KEY_USER_DEPOSIT_AMOUNT, sender) box_stauts = query_box_status() if box_stauts != BOX_FAILED and box_stauts != DEPOSIT_STATUS: raise Exception("当前无法取出存款") if not user_deposit_amount or user_deposit_amount == 0: raise Exception("没有存款,无法取出") if amount > user_deposit_amount: raise Exception("剩余存款额度不足取出") Put(KEY_BOX_DEPOSIT_AMOUNT, box_deposit_amount - amount) # 提交新的剩余存款额度 Put(user_deposit_key, user_deposit_amount - amount) # 提交新的用户存款额度 ContractBalanceSend(sender, GARD_DENOM, amount) # 给其转账 return True
def box_transfer(to_address): # 存款盒子凭证交易 if not IsValid(to_address): raise Exception("请填写正确的地址") sender = GetTxSender() if not transfer_on(): raise Exception("不允许存款凭证交易") box_status = query_box_status() if box_status != LOCK_STATUS: raise Exception("当前不允许交易存款凭证") sender_deposit_amount = query_user_deposit_amount(sender) # 用户的存款 if not sender_deposit_amount or sender_deposit_amount == 0: raise Exception("当前用户没有存款,无法转让") sender_deposit_key = concat(KEY_USER_DEPOSIT_AMOUNT, sender) # 转让人 to_address_deposit_key = concat(KEY_USER_DEPOSIT_AMOUNT, to_address) # 接收人 Put(sender_deposit_key, 0) # 提交转让人的额度变成 0 Put(to_address_deposit_key, sender_deposit_amount) # 提交接收人的额度 return True
def update_box_transfer_on(): # 更改存款凭证是否可以交易 sender = GetTxSender() if sender != Get(KEY_OWNER): raise Exception("请使用创建盒子的账户") transfer_status = transfer_on() if transfer_status: raise Exception("当前已经打开存款凭证可以交易状态,无法更改") Put(KEY_BOX_TRANSFER_ON, True) # 更改为true return True
def interest_withdraw(amount): # 利息取回 sender = GetTxSender() user_interest_amount = query_user_inject_interest(sender) # 用户对盒子注入的利息 interest_balance = query_interest_balance() # 盒子总利息 user_inject_key = concat(KEY_USER_INJECT_INTEREST, sender) if not user_interest_amount or user_interest_amount == 0: raise Exception("没有存入利息") box_stauts = query_box_status() if box_stauts != BOX_FAILED and box_stauts != ISSUE_STATUS: raise Exception("当前盒子状态无法取回注入利息") if amount > user_interest_amount: raise Exception("取出金额大于注入的利息") Put(user_inject_key, user_interest_amount - amount) # 更改用户的利息 Put(KEY_BOX_INTEREST, interest_balance - amount) ContractBalanceSend(sender, GARD_DENOM, amount) # 给其转入取出的利息 return True
def withdraw_stake_pool(amount): # 取回用户奖池额度 if GetTxSender() != Get(KEY_OWNER): raise Exception("请使用合约 owner 地址调用") if amount < 0: raise Exception("请输入正确的金额") balance_amount = stakepool() - amount if balance_amount < 0: raise Exception("系统奖池余额不足") ContractBalanceSend(Get(KEY_OWNER), GARD_DENOM, amount) Put(KEY_STAKE_POOL, balance_amount) return True
def approve(holder, spender, amount): # check if holder_address is tx sender if not IsValid(holder) or not IsValid(spender): raise Exception("Address length error.") if GetTxSender() != holder: raise Exception("Please use the operator account.") if amount < 0: raise Exception("Please enter the correct amount.") key = concat(concat(KEY_APPROVE, holder), spender) Put(key, amount) return True
def withdraw_system_pool(amount): # 取回系统奖池额度 if GetTxSender() != Get(KEY_OWNER): raise Exception("请使用合约 owner 地址调用") if amount < 0: raise Exception("请输入正确的金额") balance_amount = syspool() - amount if balance_amount < 0: raise Exception("系统奖池余额不足") ContractBalanceSend(Get(KEY_OWNER), GARD_DENOM, amount) key = concat(KEY_SYSTEM_POOL, ContractAccAddressGet()) Put(key, balance_amount) return True