// 修改飞机所属者 // 可用于购买飞机 // Failure 0x11 / 0x12 / 0x16 // Success 0x01 public static byte[] ChangeOwner(BigInteger tokenId, byte[] fromOwner, byte[] toOwner) { byte[] aircraftInfo = Storage.Get(Storage.CurrentContext, tokenId.AsByteArray()); if (aircraftInfo.Length == 0) { ChangeOwnerLog(tokenId, fromOwner, toOwner); return(new byte[] { 0x11 }); } Aircraft aircraft = (Aircraft)Helper.Deserialize(aircraftInfo); if (aircraft.owner != fromOwner) { ChangeOwnerLog(tokenId, fromOwner, toOwner); return(new byte[] { 0x12 }); } BigInteger currentTimestamp = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; if (aircraft.auctionTime < currentTimestamp) { ChangeOwnerLog(tokenId, fromOwner, toOwner); return(new byte[] { 0x16 }); } // 执行所属转移 aircraft.owner = toOwner; aircraft.auctionTime = 1525104000; // 较早的时间戳,清除售卖中状态 byte[] aircraftData = Helper.Serialize(aircraft); Storage.Put(Storage.CurrentContext, tokenId.AsByteArray(), aircraftData); ChangeOwnerLog(tokenId, fromOwner, toOwner); return(new byte[] { 0x01 }); }
/** * 取消拍卖 */ public static bool cancelAuction(byte[] sender, BigInteger dressId) { object[] objInfo = _getAuctionInfo(dressId.AsByteArray()); if (objInfo.Length > 0) { AuctionInfo info = (AuctionInfo)(object)objInfo; byte[] owner = info.owner; if (sender != owner) { return(false); } if (Runtime.CheckWitness(sender)) { object[] args = new object[3] { ExecutionEngine.ExecutingScriptHash, owner, dressId }; bool res = (bool)deleNgcall("transfer_A2P", args); if (res) { Storage.Delete(Storage.CurrentContext, dressId.AsByteArray()); // notify BigInteger oid = _add_oid(); CancelAuctioned(oid, owner, dressId); return(true); } } } return(false); }
public static byte[] InAuction(BigInteger tokenId, byte[] owner, int price) { byte[] aircraftInfo = Storage.Get(Storage.CurrentContext, tokenId.AsByteArray()); if (aircraftInfo.Length == 0) { InAuctionLog(tokenId, owner, price, -1); return(new byte[] { 0x11 }); } Aircraft aircraft = (Aircraft)Helper.Deserialize(aircraftInfo); if (aircraft.owner != owner) { InAuctionLog(tokenId, owner, price, -1); return(new byte[] { 0x12 }); } aircraft.price = price; // 当前时间增加三天,单位为秒 aircraft.auctionTime = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp + 3 * 24 * 60 * 60; byte[] aircraftData = Helper.Serialize(aircraft); Storage.Put(Storage.CurrentContext, tokenId.AsByteArray(), aircraftData); InAuctionLog(tokenId, owner, price, aircraft.auctionTime); return(new byte[] { 0x01 }); }
/// <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); }
public static bool transferFrom(byte[] addrTo, BigInteger tokenID) { if (addrTo.Length != 20) { return(false); } StorageMap tokenMap = Storage.CurrentContext.CreateMap("token"); var data = tokenMap.Get(tokenID.AsByteArray()); if (data.Length > 0) { Token token = Helper.Deserialize(data) as Token; if (!Runtime.CheckWitness(token.approved) && !Runtime.CheckWitness(superAdmin)) { return(false); } var addrFrom = token.owner; token.owner = addrTo; tokenMap.Put(tokenID.AsByteArray(), Helper.Serialize(token)); addrNFTlistRemove(addrFrom, tokenID); addrNFTlistAdd(addrTo, tokenID); onTransfer(addrFrom, addrTo, 1); onNFTTransfer(addrFrom, addrTo, tokenID); return(true); } return(false); }
/// <summary> /// Authorize someone to operate one of their own gladiators /// </summary> /// private static bool Approve(byte[] approvedAddress, BigInteger tokenId, BigInteger duration) { if (approvedAddress.Length != 20) { return(false); } TokenInfo token = DataAccess.GetToken(tokenId.AsByteArray()); if (token.Owner.Length != 20) { Runtime.Log("Token does not exist"); return(false); } if (Runtime.CheckWitness(token.Owner)) { // only one third-party spender can be approved // at any given time for a specific token DataAccess.SetApprovedAddress(tokenId.AsByteArray(), approvedAddress, duration); Events.RaiseApproved(token.Owner, approvedAddress, tokenId); return(true); } Runtime.Log("Incorrect permission"); return(false); }
/// <summary> /// Transfer the token ownership /// </summary> public static bool Transfer(byte[] from, byte[] to, BigInteger tokenId) { if (from.Length != 20 || to.Length != 20) { return(false); } if (from == to) { return(true); } var token = DataAccess.GetToken(tokenId.AsByteArray()); if (token == null) { return(false); } if (from != token.Owner) { return(false); } token.Owner = to; DataAccess.SetToken(tokenId.AsByteArray(), token); DataAccess.RemoveApproval(tokenId.AsByteArray()); Events.RaiseTransfer(from, to, tokenId); return(true); }
/** * 取消拍卖 */ public static bool cancelAuction(byte[] sender, BigInteger tokenId) { object[] objInfo = _getAuctionInfo(tokenId.AsByteArray()); if (objInfo.Length > 0) { AuctionInfo info = (AuctionInfo)(object)objInfo; byte[] tokenOwner = info.owner; if (sender != tokenOwner) { return(false); } if (Runtime.CheckWitness(sender)) { object[] args = new object[3] { ExecutionEngine.ExecutingScriptHash, tokenOwner, tokenId }; bool res = (bool)nftCall("transfer_app", args); if (res) { //Storage.Delete(Storage.CurrentContext, tokenId.AsByteArray()); _delAuctionInfo(tokenId.AsByteArray()); // notify CancelAuctioned(tokenOwner, tokenId); return(true); } } } return(false); }
/// <summary> /// Clone with another token /// </summary> private static bool BreedWith(BigInteger motherId, BigInteger fatherId) { TokenInfo motherToken = DataAccess.GetToken(motherId.AsByteArray()); TokenInfo fatherToken = DataAccess.GetToken(fatherId.AsByteArray()); BigInteger cooldownLevel = motherToken.CooldownLevel; if (cooldownLevel < fatherToken.CooldownLevel) { cooldownLevel = fatherToken.CooldownLevel; } uint nowtime = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; BigInteger cooldown = GetCooldown(cooldownLevel); motherToken.IsPregnant = 1; motherToken.CloneWithId = fatherId; motherToken.CanBreedAfter = nowtime + cooldown; DataAccess.SetToken(motherId.AsByteArray(), motherToken); cooldown = GetCooldown(fatherToken.CooldownLevel); fatherToken.CanBreedAfter = nowtime + cooldown; DataAccess.SetToken(fatherId.AsByteArray(), fatherToken); Events.RaiseTokenCloned(motherToken.Owner, motherId, motherToken.CanBreedAfter, fatherId, fatherToken.CanBreedAfter); return(true); }
public static bool BindAssetHash(byte[] fromAssetHash, BigInteger toChainId, byte[] toAssetHash, BigInteger newAssetLimit, bool isTargetChainAsset) { if (!Runtime.CheckWitness(Operator)) { return(false); } StorageMap assetHash = Storage.CurrentContext.CreateMap(nameof(assetHash)); assetHash.Put(fromAssetHash.Concat(toChainId.AsByteArray()), toAssetHash); // this means the fromAssetHash corresbonds to an asset on the target chain, if (isTargetChainAsset) { var currentLimit = GetCrossedLimit(fromAssetHash, toChainId); if (newAssetLimit < currentLimit) { return(false); } var increment = newAssetLimit - currentLimit; // increment the supply StorageMap crossedAmount = Storage.CurrentContext.CreateMap(nameof(crossedAmount)); crossedAmount.Put(fromAssetHash.Concat(toChainId.ToByteArray()), GetCrossedAmount(fromAssetHash, toChainId) + increment); } StorageMap crossedLimit = Storage.CurrentContext.CreateMap(nameof(crossedLimit)); crossedLimit.Put(fromAssetHash.Concat(toChainId.AsByteArray()), newAssetLimit); return(true); }
// 可以动态发行一个新的资产 private static bool Deploy(BigInteger anftid, BigInteger atype, BigInteger atotal_amount, BigInteger aprice) { if (!Runtime.CheckWitness(Owner)) { return(false); } StorageMap nftinfo = Storage.CurrentContext.CreateMap(nameof(nftinfo)); byte[] ni = nftinfo.Get(anftid.AsByteArray()); if (ni != null && ni.Length != 0) { return(false); } NftInfo info = new NftInfo { nftid = anftid, type = atype, total_amount = atotal_amount, price = aprice, total_supply = 0, canbuy = true }; nftinfo.Put(anftid.AsByteArray(), Helper.Serialize(info)); return(true); }
/// <summary> /// Special case background refresh Gladiator clone status /// </summary> private static bool RefreshToken(BigInteger id) { if (Runtime.CheckWitness(ContractMain.MintOwner)) { var token = DataAccess.GetToken(id.AsByteArray()); if (token == null) { return(false); } if (token.ApprovalExpiration < Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp) { } token.CloneWithId = 0; token.CanBreedAfter = 0; token.IsPregnant = 0; DataAccess.SetToken(id.AsByteArray(), token); return(true); } return(false); }
/// <summary> /// New token is created /// </summary> private static BigInteger GiveBirth(BigInteger motherId) { object[] rawMotherToken = DataAccess.GetTokenAsObjects(motherId.AsByteArray()); if (rawMotherToken.Length > 0) { TokenInfo motherToken = (TokenInfo)(object)rawMotherToken; BigInteger fatherId = motherToken.CloneWithId; uint nowtime = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; if (fatherId <= 0 || nowtime < motherToken.CanBreedAfter) { return(0); } object[] rawFatherToken = DataAccess.GetTokenAsObjects(fatherId.AsByteArray()); if (rawFatherToken.Length > 0) { TokenInfo fatherToken = (TokenInfo)(object)rawFatherToken; object[] rawToken = GetMixGene(motherToken, fatherToken); if (rawToken.Length == 0) { return(0); } TokenInfo token = (TokenInfo)(object)rawToken; token.FatherId = fatherId; token.MotherId = motherId; byte[] tokenId = DataAccess.GetTotalSupplyAsBytes(); BigInteger nextId = tokenId.AsBigInteger() + 1; tokenId = nextId.AsByteArray(); DataAccess.SetToken(tokenId, token); DataAccess.SetTotalSupply(tokenId); motherToken.CloneWithId = 0; motherToken.CanBreedAfter = 0; motherToken.IsBreeding = 0; motherToken.CooldownLevel += 1; DataAccess.SetToken(motherId.AsByteArray(), motherToken); fatherToken.CanBreedAfter = 0; fatherToken.CooldownLevel += 1; DataAccess.SetToken(fatherId.AsByteArray(), fatherToken); //BigInteger tokenId,byte[] owner, BigInteger health, BigInteger mana, BigInteger agility, BigInteger stamina, BigInteger criticalStrike, //BigInteger attackSpeed, BigInteger versatility, BigInteger mastery, BigInteger level Events.RaiseBirthed(tokenId.AsBigInteger(), token.Owner, token.Health, token.Mana, token.Agility, token.Stamina, token.CriticalStrike, token.AttackSpeed, token.Versatility, token.Mastery, token.Level); return(1); } } return(0); }
/// <summary> /// New token is created /// </summary> private static BigInteger GiveBirth(BigInteger motherId) { object[] rawMotherToken = DataAccess.GetTokenAsObjects(motherId.AsByteArray()); if (rawMotherToken.Length > 0) { TokenInfo motherToken = (TokenInfo)(object)rawMotherToken; BigInteger fatherId = motherToken.CloneWithId; uint nowtime = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; if (fatherId <= 0 || nowtime < motherToken.CanBreedAfter) { return(0); } object[] rawFatherToken = DataAccess.GetTokenAsObjects(fatherId.AsByteArray()); if (rawFatherToken.Length > 0) { TokenInfo fatherToken = (TokenInfo)(object)rawFatherToken; object[] rawToken = GetMixGene(motherToken, fatherToken); if (rawToken.Length == 0) { return(0); } TokenInfo token = (TokenInfo)(object)rawToken; token.FatherId = fatherId; token.MotherId = motherId; byte[] tokenId = DataAccess.GetTotalSupplyAsBytes(); BigInteger nextId = tokenId.AsBigInteger() + 1; tokenId = nextId.AsByteArray(); DataAccess.SetToken(tokenId, token); DataAccess.SetTotalSupply(tokenId); motherToken.CloneWithId = 0; motherToken.CanBreedAfter = 0; motherToken.IsPregnant = 0; motherToken.CooldownLevel += 1; DataAccess.SetToken(motherId.AsByteArray(), motherToken); fatherToken.CanBreedAfter = 0; fatherToken.CooldownLevel += 1; DataAccess.SetToken(fatherId.AsByteArray(), fatherToken); Events.RaiseBirthed(tokenId.AsBigInteger(), token.Owner, token.IsPregnant, token.IsReady, token.CooldownLevel, token.CanBreedAfter, token.CloneWithId, token.BirthTime, token.MotherId, token.FatherId, token.Generation); return(1); } } return(0); }
/// <summary> /// Registers a trip with parameters attached and puts it into storage /// All the information about the trip puts with on id + its postfix /// Once trip is registered, its id will never be able to register again on /// All the parameters are required /// works with deposits on its rules /// METHOD USES A LOT OF STORAGE / SHOULD BE SPREAD BY DIVIDED /// IN NEON ATTACH 2 GAS (FOR SOME REASON IT /// DONT ACCEPT LESS THAN 1 AND NON INTEGER VALUES.) /// </summary> /// <param name="id">id of a trip, 21 length byte array</param> /// <param name="date">date in uint representation</param> /// <param name="driver">Drivers script hash (owner of a trip)</param> /// <param name="way">way</param> /// <param name="seatsCount">seats in a trip</param> /// <param name="price">ticket cost for a trip (may be 0)</param> /// <param name="depositDriver">amount of token which driver is to deposit</param> /// <param name="cancelDate">Date after which any cancel will lose deposit</param> /// <returns></returns> public static bool RegisterTrip(byte[] id, byte[] driver, string way, BigInteger date, BigInteger cancelDate, BigInteger seatsCount, BigInteger price, BigInteger depositDriver) { //only driver can register a trip if (!Runtime.CheckWitness(driver)) { return(false); } Runtime.Notify("passed check for driver"); //Validating the input if (id.Length != ID_LENGTH || date < Now() || way.Length < 3 || seatsCount < 1 || cancelDate > date) { return(false); } Runtime.Notify("TripValidated"); //Check if the trip id is not user and was not used before, so //passengers can't get a free seat by mistake if (Storage.Get(Storage.CurrentContext, id).Length != 0) { return(false); } string depositLoc = id.AsString() + POSTFIX_DEPOSIT; //try to transfer deposit if (!Transfer(driver, depositLoc.AsByteArray(), depositDriver)) { return(false); } Runtime.Notify("DEPOSIT", depositLoc, depositDriver); //Put info about the trip in storage //data sets on its postfix //marks trip as true as it is valid Runtime.Notify("PUT", id, REGISTERED); Storage.Put(Storage.CurrentContext, id, REGISTERED); PutOnPostfix(id, date.AsByteArray(), POSTFIX_DATE); PutOnPostfix(id, driver, POSTFIX_DRIVER); PutOnPostfix(id, way.AsByteArray(), POSTFIX_WAY); BigInteger b = seatsCount; PutOnPostfix(id, b.AsByteArray(), POSTFIX_SEAT_NUMBER); PutOnPostfix(id, b.AsByteArray(), POSTFIX_SEAT_AVAILABLE); PutOnPostfix(id, price.AsByteArray(), POSTFIX_PRICE); PutOnPostfix(id, cancelDate.AsByteArray(), POSTFIX_CANCEL_DATE); //Notify clients (for not web app users) Runtime.Notify("REG", driver, id, date, way, seatsCount, price, depositDriver, cancelDate); return(true); }
public static bool transfer(byte[] from, byte[] to, BigInteger tokenId) { if (from.Length != 20 || to.Length != 20) { return(false); } StorageContext ctx = Storage.CurrentContext; if (from == to) { //Runtime.Log("Transfer to self!"); return(true); } object[] objInfo = _getNFTInfo(tokenId.AsByteArray()); if (objInfo.Length == 0) { return(false); } NFTInfo info = (NFTInfo)(object)objInfo; byte[] ownedBy = info.owner; if (from != ownedBy) { //Runtime.Log("Token is not owned by tx sender"); return(false); } //bool res = _subOwnerToken(from, tokenId); //if (!res) //{ // //Runtime.Log("Unable to transfer token"); // return false; //} //_addOwnerToken(to, tokenId); info.owner = to; _putNFTInfo(tokenId.AsByteArray(), info); //remove any existing approvals for this token byte[] approvalKey = "apr/".AsByteArray().Concat(tokenId.AsByteArray()); Storage.Delete(ctx, approvalKey); //记录交易信息 _setTxInfo(from, to, tokenId); Transferred(from, to, tokenId); return(true); }
private static byte[] GetMailboxForCrypt(BigInteger index) { var index_bytes = index.AsByteArray(); var mailbox = mailbox_crypt_prefix.Concat(index_bytes); return(mailbox); }
public static bool Transfer(byte[] from, byte[] to, BigInteger value) { if (value <= 0) { return(false); } //check scripthash if (!Runtime.CheckWitness(from)) { return(false); } if (from == to) { return(true); } //check if the balance is enough to make a transfer BigInteger balance = BalanceOf(from); if (balance < value || balance == null || balance.AsByteArray().Length == 0) { return(false); } Runtime.Notify("Transfer validated"); //decreasing balance Storage.Put(Storage.CurrentContext, from, balance - value); balance = 0; balance = BalanceOf(to); //increasing balance and notify Storage.Put(Storage.CurrentContext, to, balance + value); Transferred(from, to, value); Runtime.Notify("Transferred:", "from", from, "to", to, "value", value); return(true); }
// Creates Loan Request public static bool CreateLoanRequest(byte[] id, byte[] borrower, byte[] borrowedAsset, BigInteger amountBorrowed, BigInteger amountCollateral, BigInteger amountPremium, BigInteger daysToLend) { // ensure invoker is borrower? if (!Runtime.CheckWitness(borrower)) { return(false); } var stat = Storage.Get(Storage.CurrentContext, id); if (stat.Length == LOAN_ID_VALUE_LEN) { if (stat == null) { Runtime.Notify("LENGTH", "Eq", "Null"); } return(false); } // Register Loan Request Storage.Put(Storage.CurrentContext, id, REGISTERED); Runtime.Log("id REGISTERED"); PutOnPostfix(id, borrower, POSTFIX_BORROWER); PutOnPostfix(id, borrowedAsset, POSTFIX_ASSET); PutOnPostfix(id, amountBorrowed.AsByteArray(), POSTFIX_ASSET_AMOUNT); PutOnPostfix(id, amountCollateral.AsByteArray(), POSTFIX_COLLATERAL_AMOUNT); PutOnPostfix(id, amountPremium.AsByteArray(), POSTFIX_PREMIUM_AMOUNT); PutOnPostfix(id, daysToLend.AsByteArray(), POSTFIX_DAYS_TO_LEND); Runtime.Notify("REG", id, borrower, borrowedAsset, amountBorrowed, amountCollateral, amountPremium, daysToLend); return(true); }
// TODO - add email // Will add contributor Address details to Storage. private static bool SaveContributorInfo(string fid, byte[] asset, byte[] contributorSH) { BigInteger bal; BigInteger owed = 0; // If contributorSH storage doesnt exist, adds it. if (SubStorageGet(fid, "contributorSH", contributorSH.AsString()) == null) { // Saving contributorSH to fund SubStoragePut(fid, "contributorSH", contributorSH.AsString(), contributorSH); bal = GetTransactionAmount(asset); AddFundToContributorSH(fid, contributorSH); } // If contributorSH already exists else { bal = GetTransactionAmount(asset) + GetContributorInfo(fid, contributorSH, "balance").AsBigInteger(); } // If Funding has faild and the refund is active, entire balance is set to owed if (IsRefundActive(fid)) { owed = bal; } // Update contributorSH Storage details SubStoragePut(fid, contributorSH.AsString(), "balance", bal.AsByteArray()); SubStoragePut(fid, contributorSH.AsString(), "owed", owed.AsByteArray()); return(true); }
/** * 获取拍卖信息 */ public static AuctionInfo getAuctionById(BigInteger tokenId) { object[] objInfo = _getAuctionInfo(tokenId.AsByteArray()); AuctionInfo info = (AuctionInfo)(object)objInfo; return(info); }
/** * 上架0代角斗士 */ private static bool _saleGen0(byte[] tokenOwner, BigInteger tokenId, BigInteger beginPrice, BigInteger endPrice, BigInteger duration, int sellType) { if (beginPrice < 0 || endPrice < 0 || beginPrice < endPrice) { return(false); } if (endPrice < TX_MIN_FEE) { // 结束价格不能低于最低手续费 return(false); } var nowtime = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; AuctionInfo info = new AuctionInfo(); info.owner = tokenOwner; info.sellType = sellType; info.sellTime = nowtime; info.beginPrice = beginPrice; info.endPrice = endPrice; info.duration = duration; // 入库记录 _putAuctionInfo(tokenId.AsByteArray(), info); // notify Auctioned(tokenOwner, tokenId, beginPrice, endPrice, duration, sellType, nowtime); return(true); }
/** * 使用txid充值 */ public static bool rechargeToken(byte[] owner, byte[] txid) { if (owner.Length != 20) { Runtime.Log("Owner error."); return(false); } //2018/6/5 cwt 修补漏洞 byte[] keytxid = new byte[] { 0x11 }.Concat(txid); byte[] keytowner = new byte[] { 0x11 }.Concat(owner); byte[] txinfo = Storage.Get(Storage.CurrentContext, keytxid); if (txinfo.Length > 0) { // 已经处理过了 return(false); } // 查询交易记录 object[] args = new object[1] { txid }; byte[] sgasHash = Storage.Get(Storage.CurrentContext, "sgas"); deleDyncall dyncall = (deleDyncall)sgasHash.ToDelegate(); object[] res = (object[])dyncall("getTXInfo", args); if (res.Length > 0) { byte[] from = (byte[])res[0]; byte[] to = (byte[])res[1]; BigInteger value = (BigInteger)res[2]; if (from == owner) { if (to == ExecutionEngine.ExecutingScriptHash) { // 标记为处理 Storage.Put(Storage.CurrentContext, keytxid, value); BigInteger nMoney = 0; byte[] ownerMoney = Storage.Get(Storage.CurrentContext, owner); if (ownerMoney.Length > 0) { nMoney = ownerMoney.AsBigInteger(); } nMoney += value; _addTotal(value); // 记账 Storage.Put(Storage.CurrentContext, keytowner, nMoney.AsByteArray()); return(true); } } } return(false); }
private static void setTxInfo(byte[] from, byte[] to, BigInteger value) { //因为testnet 还在2.6,限制 TransferInfo info = new TransferInfo(); info.from = from; info.to = to; info.value = value; //用一个老式实现法 //优化的拼包方法 var data = info.from; var lendata = ((BigInteger)data.Length).AsByteArray().Concat(doublezero).Range(0, 2); //lendata是数据长度得bytearray,因为bigint长度不固定,统一加两个零,然后只取前面两个字节 //为什么要两个字节,因为bigint是含有符号位得,统一加个零安全,要不然长度129取一个字节就是负数了 var txinfo = lendata.Concat(data); data = info.to; lendata = ((BigInteger)data.Length).AsByteArray().Concat(doublezero).Range(0, 2); txinfo = txinfo.Concat(lendata).Concat(data); data = value.AsByteArray(); lendata = ((BigInteger)data.Length).AsByteArray().Concat(doublezero).Range(0, 2); txinfo = txinfo.Concat(lendata).Concat(data); //新式实现方法只要一行 //byte[] txinfo = Helper.Serialize(info); var txid = (ExecutionEngine.ScriptContainer as Transaction).Hash; var keytxid = new byte[] { 0x12 }.Concat(txid); Storage.Put(Storage.CurrentContext, keytxid, txinfo); }
private static void setTxInfo(byte[] from, byte[] to, BigInteger value) { TransferInfo info = new TransferInfo(); info.from = from; info.to = to; info.value = value; var data = info.from; var lendata = ((BigInteger)data.Length).AsByteArray().Concat(doublezero).Range(0, 2); var txinfo = lendata.Concat(data); data = info.to; lendata = ((BigInteger)data.Length).AsByteArray().Concat(doublezero).Range(0, 2); txinfo = txinfo.Concat(lendata).Concat(data); data = value.AsByteArray(); lendata = ((BigInteger)data.Length).AsByteArray().Concat(doublezero).Range(0, 2); txinfo = txinfo.Concat(lendata).Concat(data); var txid = (ExecutionEngine.ScriptContainer as Transaction).Hash; var keytxid = new byte[] { 0x12 }.Concat(txid); Storage.Put(Storage.CurrentContext, keytxid, txinfo); }
/** * 获取渔场信息 */ public static NFTInfo tokenData(BigInteger tokenId) { object[] objInfo = _getNFTInfo(tokenId.AsByteArray()); NFTInfo info = (NFTInfo)(object)objInfo; return(info); }
public static bool Deploy() { BigInteger index = 1; Storage.Put(Storage.CurrentContext, "currentPosition", index.AsByteArray()); return(true); }
public static bool AuctionCrypt(byte[] caller_address, BigInteger index, BigInteger price) { var owner_address = GetCryptOwner(index); if (owner_address == null) { return(false); // no owner } // if (owner_address != caller_address) return false; // FIXME : is this necessary? // only crypt owner can auction crypts if (!Runtime.CheckWitness(owner_address)) { return(false); } var status = GetCryptStatus(index); var canAuction = (status == STATUS_OPERATING || status == STATUS_ABANDONED); if (!canAuction) { return(false); } var index_bytes = index.AsByteArray(); var status_key = crypt_status_prefix.Concat(index_bytes); status = STATUS_SALE; Storage.Put(Storage.CurrentContext, status_key, status); ChangeCryptPrice(index, price); return(true); }
public static bool BuyCommercial(string video) { byte[] sender = GetSender(); BigInteger currentTime = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; BigInteger contribute_value = GetContributedValue(); BigInteger endTime = currentTime + contribute_value * 1000 * 60 * 30; bool isPlace = false; for (int i = 1; i <= num_commercials; i++) { string num = i.ToString(); string key = ("commercial_status_".AsByteArray().Concat(num.AsByteArray())).AsString(); byte[] status = Storage.Get(Storage.CurrentContext, key); if (status == null) { string commercial_key = ("commercial_".AsByteArray().Concat(num.AsByteArray())).AsString(); Storage.Put(Storage.CurrentContext, key, endTime.AsByteArray()); Storage.Put(Storage.CurrentContext, commercial_key, video.AsByteArray()); isPlace = true; break; } } if (!isPlace) { Refund(sender, contribute_value); return(false); } else { return(true); } }
// a new crypt is generated with: // status = STATUS_UNCLAIMED // owner = null // time = null // price = custom // a mailbox for this crypt is also generated using mailbox_crypt_prefix + index public static bool GenerateCrypt(byte[] caller_address, BigInteger index) { // only contract owner can generate crypts if (!Runtime.CheckWitness(caller_address)) { return(false); } // check if crypt already exists var status = GetCryptStatus(index); if (status != STATUS_INVALID) { return(false); } status = STATUS_UNCLAIMED; var index_bytes = index.AsByteArray(); var status_key = crypt_status_prefix.Concat(index_bytes); Storage.Put(Storage.CurrentContext, status_key, status); ChangeCryptPrice(index, CRYPT_DEFAULT_CLAIM_PRICE); var mailbox = GetMailboxForCrypt(index); InitMailbox(caller_address, mailbox); return(true); }