Skip to content

Latest commit

 

History

History
55 lines (43 loc) · 2.2 KB

积分兑换设计.md

File metadata and controls

55 lines (43 loc) · 2.2 KB

积分兑换合约设计

积分都是由用户发行,因此兑换是 c2c 的行为,由此可联系到,需要挂单,匹配

A --> 发起积分兑换单 ----> 链上可查 --->B 进行买单成交

由此可设计函数

交易函数基于发行合约的交易指令 transfer_issue() ,传参 from_address, to_address, issue_id, amount

def exchange_from(from_token,to_token,amount):		#发起订单
    sender = GetTxSender() # 获取当前操作者
    contract_address = contractAccount() # 获取此兑换合约资金账户
    contract = ContractAccAddressGet() # 兑换合约地址
	if not if_issue(from_token):
        raise Exception("没有该积分,无法发起订单")
    if contract_balance(sender,from_token) < amount:
        raise Exception("余额不足")
    order_list = []
    order_str = concat(concat(concat(concat(concat(concat(sender,"_"),from_token),"_"), to_token),"_"),str(amount)	# 格式:address_fromtoken_totoken_amount
    key = "ORDER_FROM"
    order = GetArry(key)
    if len(order) == 0:
    	PutArry(key,[order_str])
     else:
     	for i in rang(len(order)):
            order_list.append(order[i])
        order_list.append(str)
    	PutArry(key,order_list)			# Put 到链上的订单表
    transfer_issue(sender,contract_address, from_token, amount)	#先转移此积分作为保证金

def exchange_to(to_address,from_token,to_token,amount):		#兑换订单
    sender = GetTxSender() # 获取当前操作者
	if not if_issue(to_token):
        raise Exception("没有该积分,无法兑换")
    if contract_balance(sender,to_token) < amount:
        raise Exception("余额不足")
  	transfer_issue(sender,to_address, to_token, amount)	#转移此积分到订单发起人账户
    ContractBalanceSend(sender, from_token, amount)	#将合约的此积分转入到该兑换人
    order_str = concat(concat(concat(concat(concat(concat(to_address,"_"),from_token),"_"), to_token),"_"),str(amount)	# 格式:address_fromtoken_totoken_amount
    key = "ORDER_FROM"
    order_list = GetArry(key)
    new_order_list = list_remove_elt(order_list, order_str)		# 返回一个移除了此订单信息的列表
    PutArry(key,new_order_list)	# 重新 Put