/// <summary> /// Cancel the auction /// </summary> private static bool CancelAuction(byte[] sender, BigInteger tokenId) { object[] rawAuction = DataAccess.GetAuctionAsObjects(tokenId.AsByteArray()); if (rawAuction.Length > 0) { MarketAuction auction = (MarketAuction)(object)rawAuction; bool isAdmin = Runtime.CheckWitness(ContractMain.ContractOwner); if (sender != auction.Owner && isAdmin == false) { return(false); } if (Runtime.CheckWitness(sender) || isAdmin) { object[] args = new object[] { ExecutionEngine.ExecutingScriptHash, auction.Owner, tokenId }; bool appCallSuccess = (bool)ContractMain.NftContractCall("transfer_app", args); if (appCallSuccess) { DataAccess.DeleteAuction(tokenId.AsByteArray()); Events.RaiseCancelAuctioned(auction.Owner, tokenId); return(true); } } } return(false); }
/// <summary> /// Release 0 generation gladiators to the auction house /// </summary> private static bool CreateGenerationZeroAuction(byte strength, byte power, byte agile, byte speed, byte generation) { byte[] tokenOwner = ExecutionEngine.ExecutingScriptHash; if (Runtime.CheckWitness(ContractMain.MintOwner)) { object[] args = new object[] { tokenOwner, strength, power, agile, speed, generation }; BigInteger tokenId = (BigInteger)ContractMain.NftContractCall("createGenerationZeroFromAuction_app", args); if (tokenId == 0) { return(false); } BigInteger auctionStartPrice = GenerationZeroMaxPrice; BigInteger auctionEndPrice = GenerationZeroMinPrice; BigInteger auctionDuration = GenerationZeroAuctionDuration; object[] rawPrice = DataAccess.GetGenerationZeroPricesAsObjects(); if (rawPrice.Length > 0) { GenerationZeroPrice price = (GenerationZeroPrice)(object)rawPrice; auctionStartPrice = price.MaxPrice; auctionEndPrice = price.MinPrice; auctionDuration = price.Duration; } return(SaleGenerationZero( ContractMain.ContractOwner, tokenId, auctionStartPrice, auctionEndPrice, auctionDuration, 0)); } return(false); }
/// <summary> /// sellType:0 Auction 1 clone auction /// </summary> private static bool SaleItemWithType( byte[] tokenOwner, BigInteger tokenId, BigInteger startPrice, BigInteger endPrice, BigInteger duration, int sellType) { Runtime.Notify("Initiating SaleItemWithType"); if (tokenOwner.Length != 20) { Runtime.Log("Owner error."); return(false); } if (startPrice < 0 || endPrice < 0 || startPrice < endPrice) { return(false); } Runtime.Notify("Price validation passed"); if (endPrice < ContractMain.MinTxFee) { // End price cannot be lower than the minimum handling fee return(false); } Runtime.Notify("Fee validation passed."); //if (Runtime.CheckWitness(tokenOwner)) // Items are placed in the auction house object[] args = new object[3] { tokenOwner, ExecutionEngine.ExecutingScriptHash, tokenId }; Runtime.Notify("Initiating app call"); bool appCallSuccess = (bool)ContractMain.NftContractCall("transferFrom_app", args); Runtime.Notify(appCallSuccess ? "app call success" : "app call failed"); if (appCallSuccess) { var nowtime = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; MarketAuction auction = new MarketAuction { Owner = tokenOwner, SellType = sellType, SellTime = nowtime, BeginPrice = startPrice, EndPrice = endPrice, Duration = duration }; DataAccess.SetAuction(tokenId.AsByteArray(), auction); Events.RaiseAuctioned(tokenOwner, tokenId, startPrice, endPrice, duration, sellType, nowtime); return(true); } return(false); }
/// <summary> /// Clone with your own gladiator /// </summary> private static bool BreedWithMy(byte[] sender, BigInteger motherId, BigInteger fatherId) { if (!Runtime.CheckWitness(sender)) { return(false); } byte[] senderBalanceKey = Keys.UserBalanceKey(sender); BigInteger senderBalance = Storage.Get(Storage.CurrentContext, senderBalanceKey).AsBigInteger(); var fee = ContractMain.MinTxFee; if (senderBalance < fee) { return(false); } // Start cloning object[] args = new object[] { sender, motherId, fatherId }; bool appCallSuccess = (bool)ContractMain.NftContractCall("breedWithMy_app", args); if (appCallSuccess) { senderBalance -= fee; Storage.Put(Storage.CurrentContext, senderBalanceKey, senderBalance); DecreaseCGasBalance(fee); var nowtime = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; Events.RaiseMyClone(sender, motherId, fatherId, fee, nowtime); return(true); } return(false); }
/// <summary> /// Buy auction clones /// </summary> private static bool CloneOnAuction(byte[] sender, BigInteger motherId, BigInteger fatherId) { if (!Runtime.CheckWitness(sender)) { return(false); } object[] rawFatherAuction = DataAccess.GetAuctionAsObjects(fatherId.AsByteArray()); if (rawFatherAuction.Length > 0) { MarketAuction fatherAuction = (MarketAuction)(object)rawFatherAuction; byte[] owner = fatherAuction.Owner; if (fatherAuction.SellType == 1) { byte[] senderBalanceKey = Keys.UserBalanceKey(sender); var nowtime = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; var secondPass = nowtime - fatherAuction.SellTime; //var secondPass = (nowtime - fatherInfo.sellTime) / 1000; BigInteger senderBalance = Storage.Get(Storage.CurrentContext, senderBalanceKey).AsBigInteger(); BigInteger currentBuyPrice = ComputeCurrentPrice( fatherAuction.BeginPrice, fatherAuction.EndPrice, fatherAuction.Duration, secondPass); var fee = currentBuyPrice * 199 / 10000; if (fee < ContractMain.MinTxFee) { fee = ContractMain.MinTxFee; } if (currentBuyPrice < fee) { currentBuyPrice = fee; } if (senderBalance < currentBuyPrice) { return(false); } object[] args = new object[] { sender, motherId, fatherId }; bool appCallSuccess = (bool)ContractMain.NftContractCall("bidOnClone_app", args); if (!appCallSuccess) { return(false); } Storage.Put(Storage.CurrentContext, senderBalanceKey, senderBalance - currentBuyPrice); currentBuyPrice -= fee; DecreaseCGasBalance(fee); BigInteger newBalance = 0; byte[] ownerBalanceKey = Keys.UserBalanceKey(owner); byte[] sellerBalance = Storage.Get(Storage.CurrentContext, ownerBalanceKey); if (sellerBalance.Length > 0) { newBalance = sellerBalance.AsBigInteger(); } newBalance = newBalance + currentBuyPrice; Storage.Put(Storage.CurrentContext, ownerBalanceKey, newBalance); //// Do not delete the auction record //Storage.Delete(Storage.CurrentContext, tokenId.AsByteArray()); // transaction record /*AuctionRecord record = new AuctionRecord(); * record.tokenId = fatherGlaId; * record.seller = owner; * record.buyer = sender; * record.sellType = 1; * record.sellPrice = curBuyPrice + fee; * record.sellTime = nowtime; * * _putAuctionRecord(fatherGlaId.AsByteArray(), record);*/ Events.RaiseAuctionClone(sender, motherId, fatherId, currentBuyPrice, fee, nowtime); return(true); } } return(false); }
/// <summary> /// Buy from the auction house, put the money under the contract name, and give the goods to the buyer. /// </summary> private static bool BuyOnAuction(byte[] sender, BigInteger tokenId) { if (!Runtime.CheckWitness(sender)) { return(false); } object[] rawAuction = DataAccess.GetAuctionAsObjects(tokenId.AsByteArray()); if (rawAuction.Length > 0) { MarketAuction info = (MarketAuction)(object)rawAuction; var nowtime = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; var secondPass = nowtime - info.SellTime; //var secondPass = (nowtime - info.sellTime) / 1000; byte[] senderBalanceKey = Keys.UserBalanceKey(sender); byte[] ownerBalanceKey = Keys.UserBalanceKey(info.Owner); BigInteger senderBalance = Storage.Get(Storage.CurrentContext, senderBalanceKey).AsBigInteger(); BigInteger currentPrice = ComputeCurrentPrice(info.BeginPrice, info.EndPrice, info.Duration, secondPass); var fee = currentPrice * 199 / 10000; if (fee < ContractMain.MinTxFee) { fee = ContractMain.MinTxFee; } if (currentPrice < fee) { currentPrice = fee; } if (senderBalance < currentPrice) { // Not enough money return(false); } object[] args = new object[] { ExecutionEngine.ExecutingScriptHash, sender, tokenId }; bool appCallSuccess = (bool)ContractMain.NftContractCall("transfer_app", args); if (!appCallSuccess) { return(false); } Storage.Put(Storage.CurrentContext, senderBalanceKey, senderBalance - currentPrice); BigInteger sellPrice = currentPrice - fee; DecreaseCGasBalance(fee); // The money is in the name of the seller/ BigInteger newBalance = 0; byte[] sellerBalance = Storage.Get(Storage.CurrentContext, ownerBalanceKey); if (sellerBalance.Length > 0) { newBalance = sellerBalance.AsBigInteger(); } newBalance = newBalance + sellPrice; Storage.Put(Storage.CurrentContext, ownerBalanceKey, newBalance); Storage.Delete(Storage.CurrentContext, tokenId.AsByteArray()); // transaction record /*AuctionRecord record = new AuctionRecord(); * record.tokenId = tokenId; * record.seller = owner; * record.buyer = sender; * record.sellType = 0; * record.sellPrice = curBuyPrice; * record.sellTime = nowtime; * * _putAuctionRecord(tokenId.AsByteArray(), record); * * if(owner == ContractOwner) * { * Gene0Record gene0Record; * byte[] v = (byte[])Storage.Get(Storage.CurrentContext, "gene0Record"); * if(v.Length==0) * { * gene0Record = new Gene0Record(); * } * else * { * object[] infoRec = (object[])Helper.Deserialize(v); * gene0Record = (Gene0Record)(object)infoRec; * } * int idx = (int)gene0Record.totalSellCount % 5; * if (idx == 0) * { * gene0Record.lastPrice0 = curBuyPrice; * } * else if (idx == 1) * { * gene0Record.lastPrice1 = curBuyPrice; * } * else if (idx == 2) * { * gene0Record.lastPrice1 = curBuyPrice; * } * else if (idx == 3) * { * gene0Record.lastPrice1 = curBuyPrice; * } * else if (idx == 4) * { * gene0Record.lastPrice1 = curBuyPrice; * } * * gene0Record.totalSellCount += 1; * * // * byte[] infoRec2 = Helper.Serialize(gene0Record); * Storage.Put(Storage.CurrentContext, "gene0Record", infoRec2); * }*/ Events.RaiseAuctionBuy(sender, tokenId, currentPrice, fee, nowtime); return(true); } return(false); }