def buy(id, fromAcc): # 获取倒计时结束时间 并判断 是否已结束 endTimeDeserialize = Get(GetContext(), ENDTIME) endTime = Deserialize(endTimeDeserialize) if GetTime() >= endTime: reset(2, id, fromAcc) else: # 获取所有国家 regionDeserialize = Get(GetContext(), REGION) region = Deserialize(regionDeserialize) for item in region: if item[0] == id: # 拿到目标购买国家进行交易 param = state(Base58ToAddress(fromAcc), Base58ToAddress(item[2]), item[1] - 1) res = Invoke(0, OntContract, "transfer", [param]) if res != b'\x01': Notify("buy error.") return False # 每一次给合约内部转1个币 paramContract = state(Base58ToAddress(fromAcc), selfContractAddress, 1) resContract = Invoke(0, OntContract, 'transfer', [paramContract]) # 倒计时增加用户消耗的币 * 10 秒 endTime = endTime + item[1] * 10 Put(GetContext(), ENDTIME, Serialize(endTime)) # 将购买用户设置为最后一次购买人 Put(GetContext(), LASTBUY, Serialize(fromAcc)) # 更新国家信息以及价格 item[1] = (item[1] - 1) * 2 + 1 item[2] = fromAcc Put(GetContext(), REGION, Serialize(region)) Notify("buy success.") return True
def reset(type, id, fromAcc): # 获取所有国家 regionDeserialize = Get(GetContext(), REGION) region = Deserialize(regionDeserialize) # 获取最后一个购买人 lastBuyDeserialize = Get(GetContext(), LASTBUY) lastBuy = Deserialize(lastBuyDeserialize) # 计算下一次倒计时 endTime = GetTime() + cycle # 获取合约内部的总金额 param = state(selfContractAddress) unboundOngAmount = Invoke(0, OntContract, 'balanceOf', param) # 把合约内的所有币转给最后一个购买人 paramContract = state(selfContractAddress, Base58ToAddress(lastBuy), unboundOngAmount) resContract = Invoke(0, OntContract, 'transfer', [paramContract]) # 重新设置倒计时 Put(GetContext(), ENDTIME, Serialize(endTime)) # 所有地区价格重置为2 for item in region: item[1] = 2 Put(GetContext(), REGION, Serialize(region)) if type == 1: getGlebal() if type == 2: buy(id, fromAcc)
def TransferOntOng(from_acct, to_acct, ont, ong): param = state(from_acct, to_acct, ont) res = Invoke(0, OntContract, "transfer", [param]) if res != b'\x01': Notify("transferONT succeed") raise Exception("tansfer ont error.") param = state(from_acct, to_acct, ong) ret = Invoke(0, OngContract, "transfer", [param]) if ret != b'\x01': Notify("transferONG succeed") raise Exception("tansfer ong error.") return True
def transferOntOng(fromAcct, toAcct, ontAmount, ongAmount): param = state(fromAcct, toAcct, ontAmount) res = Invoke(0, OntContract, "transfer", [param]) if res != b'\x01': raise Exception("transfer ont error.") param = state(fromAcct, toAcct, ongAmount) Notify("transferONT succeed") res = Invoke(0, OngContract, "transfer", [param]) if res != b'\x01': raise Exception("transfer ong error.") Notify("transferONG succeed") return True
def balanceOf(address, tokenType): if tokenType == ONG: param = state(address) res = Invoke(0, OngContract, 'balanceOf', param) return res if tokenType == ONT: param = state(address) res = Invoke(0, OntContract, 'balanceOf', param) return res if tokenType == TNT: return banlanceOEP4(address) if tokenType == TONT: return banlanceTONT(address) return 0
def _transferOntOngtoAccount(_account, _ontAmount, _ongAmount): param = state(selfContractAddress, Base58ToAddress(_account), _ontAmount) res = Invoke(0, OntContractAddress, 'transfer', [param]) if res != b'\x01': Notify('transfer ont error.\n') return False Notify("transferONT succeed") param = state(selfContractAddress, Base58ToAddress(_account), _ongAmount) res = Invoke(0, OngContractAddress, 'transfer', [param]) if res != b'\x01': Notify('transfer ong error.\n') return False Notify("transferONG succeed") return True
def checkBalanceOf(ongFlag, account): param = state(ContractAddress) # do not use [param] res = Invoke(0, ONGAddress, 'balanceOf', param) Notify(["ContractAddress", ContractAddress, res]) param = state(account) if ongFlag == 1: Notify(["ongFlag", ongFlag]) res = Invoke(0, ONGAddress, 'balanceOf', param) else: Notify(["ongFlag", ongFlag]) res = Invoke(0, ONTAddress, 'balanceOf', param) Notify(["checkBalanceOf", account, res]) return res
def RecordShare(owner, signId, symbol, amount, amount2, sponsor): # owner ownerAddress = Base58ToAddress(owner) RequireWitness(ownerAddress) if symbol == 'ONT': contract = OntContract tokenContractAddress = OntContractAddress if symbol == 'ONG': contract = OngContract tokenContractAddress = OngContractAddress param = state(ownerAddress, selfContractAddress, amount) res = Invoke(0, tokenContractAddress, 'transfer', [param]) if res != b'\x01': Notify('RecordShare error.\n') return False share = { "contract": contract, "symbol": symbol, "amount": amount, "amount2": amount, "sponsor": sponsor, } shareKey = concat(owner, signId) _saveShareRecord(shareKey, share) return True
def _transferONGFromContact(toAcct, amount): param = state(ContractAddress, toAcct, amount) res = Invoke(0, ONGAddress, 'transfer', [param]) if res and res == b'\x01': return True else: return False
def withdrawOng(toAcct): param = state(ONTAddress, ContractAddress) unboundOngAmount = Invoke(0, ONGAddress, 'allowance', param) Notify(["unboundOngAmount", unboundOngAmount]) if unboundOngAmount > 0: unboundOngAmount = 147 params = state(ContractAddress, ONTAddress, toAcct, unboundOngAmount) res = Invoke(0, ONGAddress, "transferFrom", params) if res and res == b'\x01': Notify(["withdraw ong successful!"]) return True else: Notify(["withdraw ong failed!"]) return False else: Notify(["Not enough unboundOngAmount", unboundOngAmount]) return False
def transfer_ont(from_acct, to_acct, amount): require_witness(from_acct) transfer_param = state(from_acct, to_acct, amount) res = Invoke(0, ONT_ADDRESS, 'transfer', [transfer_param]) if res == b'\x01': return True else: return False
def transferOngToContract(fromAccount, ongAmount): Notify(["111_transferOngToContract", selfContractAddress]) param = state(fromAccount, selfContractAddress, ongAmount) res = Invoke(0, OngContract, 'transfer', [param]) if res and res == b'\x01': Notify('transfer Ong succeed') return True else: Notify('transfer Ong failed') return False
def checkWithdraw(account): allowanceOng = checkAllowance(account) withdrawOngAmount = allowanceOng / 2 params = state(account, ONTAddress, account, withdrawOngAmount) res = Invoke(0, ONGAddress, 'transferFrom', params) if res and res == b'\x01': Notify(["withdraw ong successful!"]) return True else: Notify(["withdraw ong failed!"]) return False
def makeState(fromacct, toacct, amount): """ make a tranfer state parameter currently due to the compiler problem, must be created as this format :param fromacct: :param toacct: :param amount: :return: """ return state(fromacct, toacct, amount)
def withdraw(_account): """ Withdraw all the caller earning including dividends and referral bonus and the ong balance from selling the keys :param _account: :return: the withdrawn ong amount """ nouse = Require(CheckWitness(_account)) # put present dividend into dividend vault, update profit per token and dividend vault nouse = collectDividendOf(_account) # add dividend vault and referral balance together to get _dividends _dividends = dividendsOf(_account) # _account balance from selling keys _ongBalance = ongBalanceOf(_account) # add two together as earnings _accountEarnings = Add(_dividends, _ongBalance) # make sure _account has some earnings nouse = Require(_accountEarnings > 0) # transfer _dividends ( his ONG ) to _account params = state(selfContractAddr_, _account, _accountEarnings) res = Invoke(0, ONGContractAddress_, "transfer", [params]) if res and res == b'\x01': # emit event OnWithdraw(_account, _accountEarnings) else: raise Exception("withdraw ong error.") # Update dividend Delete(GetContext(), concatKey(_account, DIVIDEND_VAULT_SUFFIX)) # Update referral bonus Delete(GetContext(), concatKey(_account, REFERRAL_BALANCE_SUFFIX)) # # Update ong balance of _account # Delete(GetContext(), concatKey(_account, ONG_BALANCE_SUFFIX)) if balanceOf(_account) > 0: # update the _profitPerToken value after the _account withdraw to count his share till _profitPerToken later _profitPerToken = Get(GetContext(), PROFIT_PER_TOKEN_KEY) Put(GetContext(), concatKey(_account, PROFIT_PER_TOKEN_AFTER_SUFFIX), _profitPerToken) else: Delete(GetContext(), concatKey(_account, PROFIT_PER_TOKEN_AFTER_SUFFIX)) # Update withdrawn earnings ledger _newWithdrawnEarnings = Add(withdrawnEarnings(_account), _accountEarnings) Put(GetContext(), concatKey(_account, WITHDRAWN_EARNINGS_SUFFIX), _newWithdrawnEarnings) # Update ONG balance of this contract (need to be updated only when withdraw() is invoked) _totalOngBalance = Sub(totalOngBalance(), _accountEarnings) Put(GetContext(), TOTAL_ONG_KEY, _totalOngBalance) # Update ONG balance of this contract for key (no need to update since its done within sell method # Put(GetContext(), TOTAL_ONG_FOR_KEY_KEY, Add(totalOngForKey(), _ongBalance)) return _accountEarnings
def invest(account, ontAmount): # make sure the caller is actually the account if not CheckWitness(account): # if the caller is not the account return False # make sure the caller has transferred the correct amount of ONT into this contract(or account) params = state(account, selfContractAddress, ontAmount) res = Invoke(0, OntContract, "transfer", params) if not res: # the account should transfer ontAmount of ONT into this contract as investment, but he failed. return False # then, for example, we can record that the account has transferred amount of ONT into this GetContract return True
def checkAllowance(account): # param = state(ONTAddress, account) # if ongFlag == 1: # Notify(["ongFlag", ongFlag]) # unboundOngAmount = Invoke(0, ONGAddress, 'allowance', param) # else: # Notify(["ongFlag", ongFlag]) # unboundOngAmount = Invoke(0, ONTAddress, 'allowance', param) # Notify(["checkAllowance", account, unboundOngAmount]) param = state(ONTAddress, account) unboundOngAmount = Invoke(0, ONGAddress, 'allowance', param) Notify(["checkAllowance", account, unboundOngAmount]) return unboundOngAmount
def transferONG(fromAcct, toAcct, amount): """ transfer ONG :param fromacct: :param toacct: :param amount: :return: """ RequireWitness(fromAcct) param = state(fromAcct, toAcct, amount) res = Invoke(0, ONGAddress, 'transfer', [param]) if res and res == b'\x01': return True else: return False
def buy_copyright(copyright, address): copyright = sha256(copyright) if check_owner(copyright): return False owner_adr = get_owner(copyright) Notify(['buy', copyright, address]) param = state(address, owner_adr, 10) OntContract = ToScriptHash("AFmseVrdL9f9oyCzZefL9tG6UbvhUMqNMV") res = Invoke(0, OntContract, "transfer", [param]) if res != b'\x01': raise Exception("transfer ont error.") Notify("transferONT succeed") return put_grants(copyright, address, 120)
def transferONT(fromAcct, toAcct, ontAmount): """ transfer ONG :param fromacct: :param toacct: :param amount: :return: """ param = state(fromAcct, toAcct, ontAmount) res = Invoke(0, ONTAddress, 'transfer', [param]) if res and res == b'\x01': Notify(["transfer ONT successful!"]) return True else: Notify(["transfer ONT failed!"]) return False
def migrateContract(code, needStorage, name, version, author, email, description, newContractHash): RequireWitness(Admin) param = state(ContractAddress) totalOngAmount = Invoke(0, ONGAddress, 'balanceOf', param) res = _transferONGFromContact(newContractHash, totalOngAmount) Require(res) if res == True: res = Migrate(code, needStorage, name, version, author, email, description) Require(res) Notify(["Migrate Contract successfully", Admin, GetTime()]) return True else: Notify(["MigrateContractError", "transfer ONG to new contract error"]) return False
def getGlebal(): # 获取 和 判断倒计时是否结束 endTimeDeserialize = Get(GetContext(), ENDTIME) endTime = Deserialize(endTimeDeserialize) if GetTime() >= endTime: reset(1, 1, 1) else: glebal = [] # 获取合约余额 param = state(selfContractAddress) unboundOngAmount = Invoke(0, OntContract, 'balanceOf', param) # 获取最后一次购买人 lastBuyDeserialize = Get(GetContext(), LASTBUY) lastBuy = Deserialize(lastBuyDeserialize) glebal = [endTime, lastBuy, unboundOngAmount] Notify(glebal) return glebal
def createShare(owner, signId, income, referral): # owner RequireScriptHash(owner) RequireWitness(owner) ownerPInfo = _getPlayerInfo(owner) # sign sSign = Get(GetContext(), concat(SIGN_PREFIX, signId)) Require(sSign) sign = Deserialize(sSign) author = sign[0] fissionFactor = sign[1] authorPInfo = _getPlayerInfo(author) # owner sign share Require(not signId in ownerPInfo[2]) param = state(Base58ToAddress(owner), safeHouseAddress, income) res = Invoke(0, OntContract, "transfer", [param]) if res != b'\x01': Notify("createShare error.") return False #paramContract = state(Base58ToAddress(fromAcc), selfContractAddress, 1) #resContract = Invoke(0, OntContract, 'transfer', [paramContract]) income = 0 if referral != '': RequireScriptHash(referral) if referral != owner: referralPInfo = _getPlayerInfo(referral) quota = referralPInfo[2][signId] if quota: delta = quota if quota < income else income SubQuota(referral, referralPInfo, signId, delta) AddShareIncome(referral, referralPInfo, delta) #Notify(referral, "got share income", income, "ONT\n") income -= delta ownerPInfo[2][signId] = Div(Mul(income, fissionFactor), 1000) _savePlayer(owner, ownerPInfo) AddSignIncome(author, authorPInfo, income) #Notify(author, "got sign income", income, "ONT\n") #Notify(owner, "create a share success.\n") return True
def innerWithdraw(toAcct, amount): RequireWitness(Admin) balanceKey = concatkey(BALANCE, toAcct) balance = Get(ctx, balanceKey) if balance < amount: return False _amount = Sub(balance, amount) Put(ctx, balanceKey, _amount) param = state(selfContractAddress, toAcct, amount) res = Invoke(0, OngContract, 'transfer', [param]) if res and res == b'\x01': return amount else: Notify('inner withdraw operation failed') return False
def _transferONGFromContract(toacct, amount): """ transfer ONT from contract :param fromacct: :param toacct: :param amount: :return: """ # ONT native contract address contractAddress = bytearray( b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02' ) _require(amount > 0) param = state(selfAddr, toacct, amount) res = Invoke(0, contractAddress, 'transfer', [param]) if res and res == b'\x01': return True else: return False
def migrateContract(code, needStorage, name, version, author, email, description, newReversedContractHash): RequireWitness(Admin) param = state(ContractAddress) totalOngAmount = Invoke(0, ONGAddress, 'balanceOf', param) if totalOngAmount > 0: res = _transferONGFromContact(newReversedContractHash, totalOngAmount) Require(res) revesedContractAddress = Get(GetContext(), LUCKY_CONTRACT_HASH_KEY) params = [ContractAddress] totalLuckyAmount = DynamicAppCall(revesedContractAddress, "balanceOf", params) if totalLuckyAmount > 0: params = [ContractAddress, newReversedContractHash, totalLuckyAmount] res = DynamicAppCall(revesedContractAddress, "transfer", params) Require(res) res = Migrate(code, needStorage, name, version, author, email, description) Require(res) Notify(["Migrate Contract successfully"]) return True
def withdraw(_account): """ Withdraw all the caller earning """ Require(CheckWitness(_account)) _dividends = _dividendsOf(_account, True) # transfer _dividends ( his ONG ) to _account params = state(selfContractAddr_, _account, _dividends) res = Invoke(0, ONGContractAddress_, "transfer", [params]) if res == b'\x01': # emit event OnWithdraw(_account, _dividends) else: raise Exception("transfer ont error.") # Update dividend Delete(GetContext(), concatKey(_account, REFERRALBALANCE_SUFFIX)) # Update referral bonus Delete(GetContext(), concatKey(_account, REFERRALBALANCE_SUFFIX)) # Update paid earnings ledger Put(GetContext(), concatKey(_account, PAIDEARNINGS_SUFFIX), Add(paidEarnings(_account), _dividends))
def deposit(openId, fromAcct, amount): # entryHash = GetEntryScriptHash() # callerHash = GetCallingScriptHash() RequireWitness(fromAcct) if amount <= 0: return False Notify(['111_deposit', selfContractAddress, openId, fromAcct, amount]) param = state(fromAcct, selfContractAddress, amount) res = Invoke(0, OngContract, 'transfer', [param]) if res and res == b'\x01': Notify('transfer Ong succeed') balanceKey = concatkey(BALANCE, fromAcct) rechargeKey = concatkey(RECHARGE, fromAcct) balance = Get(ctx, balanceKey) Put(ctx, balanceKey, Add(balance, amount)) regBalance = Get(ctx, rechargeKey) Put(ctx, rechargeKey, Add(regBalance, amount)) return True else: Notify('transfer Ong failed') return False
def transferONG(fromacct, toacct, amount): """ transfer ONG :param fromacct: :param toacct: :param amount: :return: """ if CheckWitness(fromacct): param = state(fromacct, toacct, amount) res = Invoke(0, ONGContractAddress_, 'transfer', [param]) Notify(res) if res and res == b'\x01': Notify('transfer ong succeed') return True else: Notify('transfer ong failed') return False else: Notify('checkWitness failed') return False
def _transferONG(fromacct, toacct, amount): """ transfer ONT :param fromacct: :param toacct: :param amount: :return: """ # ONT native contract address contractAddress = bytearray( b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02' ) _require(amount > 0) if CheckWitness(fromacct): param = state(fromacct, toacct, amount) res = Invoke(0, contractAddress, 'transfer', [param]) if res and res == b'\x01': return True else: return False else: return False