From 964363049556c82f53d5c6505f6ac4a0fc360dde Mon Sep 17 00:00:00 2001 From: Jecket1 <134037206+Jecket1@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:20:17 +0800 Subject: [PATCH] Collection approve (#168) * feat: add ListedNFTTotalAmountMap * ut: fix ut * feat: modify GetTotalEffectiveListedNFTAmount --- contract/Forest/ForestContractConstants.cs | 2 + contract/Forest/ForestContractState.cs | 5 +++ contract/Forest/ForestContract_Buyers.cs | 28 ++++++++++++ contract/Forest/ForestContract_Helpers.cs | 25 +++++++++++ contract/Forest/ForestContract_Sellers.cs | 44 +++++++++++++++++++ contract/Forest/ForestContract_Views.cs | 17 ++++++- .../InscriptionContractTests.cs | 2 +- .../Forest.Tests/ForestContractTests_Views.cs | 6 +-- 8 files changed, 124 insertions(+), 5 deletions(-) diff --git a/contract/Forest/ForestContractConstants.cs b/contract/Forest/ForestContractConstants.cs index 561026f1..db5de96e 100644 --- a/contract/Forest/ForestContractConstants.cs +++ b/contract/Forest/ForestContractConstants.cs @@ -24,5 +24,7 @@ public partial class ForestContract public const string DefaultAIImageSize256 = "256x256"; public const int DefaultMaxBatchCancelOfferCount= 20; public const int DefaultMaxBatchCancelListCount= 20; + public const string CollectionSymbolSuffix = "0"; + public const string SymbolSeparator = "-"; } \ No newline at end of file diff --git a/contract/Forest/ForestContractState.cs b/contract/Forest/ForestContractState.cs index c21a6ff0..6e598787 100755 --- a/contract/Forest/ForestContractState.cs +++ b/contract/Forest/ForestContractState.cs @@ -53,6 +53,11 @@ public partial class ForestContractState : ContractState /// public MappedState OfferTotalAmountMap { get; set; } + /// + /// Collection Symbol -> User Address -> Listing Amount * decimal + /// + public MappedState ListedNFTTotalAmountMap { get; set; } + public SingletonState AIServiceFeeConfig { get; set; } public SingletonState
AIServiceFeeReceiver { get; set; } diff --git a/contract/Forest/ForestContract_Buyers.cs b/contract/Forest/ForestContract_Buyers.cs index dd167c35..9a3ab4ab 100644 --- a/contract/Forest/ForestContract_Buyers.cs +++ b/contract/Forest/ForestContract_Buyers.cs @@ -346,6 +346,20 @@ private void SingleMakeOfferForBatchBuyNow(string symbol, FixPrice inputFixPrice Quantity = listedNftInfo.Quantity, Price = listedNftInfo.Price, }); + + var collectionSymbol = TransferCollectionSymbol(listedNftInfo.Symbol); + var collectionAllowance = State.ListedNFTTotalAmountMap[collectionSymbol][listedNftInfo.Owner]; + if (collectionAllowance == null || collectionAllowance == "") + { + State.ListedNFTTotalAmountMap[collectionSymbol][listedNftInfo.Owner] = ""; + } + else + { + var originQuantity = long.Parse(collectionAllowance); + var resultQuantity = originQuantity - dealQuantity; + State.ListedNFTTotalAmountMap[collectionSymbol][listedNftInfo.Owner] = + (resultQuantity >= 0 ? resultQuantity : 0).ToString(); + } } } @@ -361,6 +375,20 @@ private void SingleMakeOfferForBatchBuyNow(string symbol, FixPrice inputFixPrice Owner = info.Owner, Price = info.Price }); + var collectionSymbol = TransferCollectionSymbol(info.Symbol); + var collectionAllowance = State.ListedNFTTotalAmountMap[collectionSymbol][info.Owner]; + if (collectionAllowance == null || collectionAllowance == "") + { + State.ListedNFTTotalAmountMap[collectionSymbol][info.Owner] = ""; + } + else + { + var originQuantity = long.Parse(collectionAllowance); + var resultQuantity = originQuantity - info.Quantity; + State.ListedNFTTotalAmountMap[collectionSymbol][info.Owner] = + (resultQuantity >= 0 ? resultQuantity : 0).ToString(); + } + } } diff --git a/contract/Forest/ForestContract_Helpers.cs b/contract/Forest/ForestContract_Helpers.cs index a6955b1f..fe5dc3cc 100644 --- a/contract/Forest/ForestContract_Helpers.cs +++ b/contract/Forest/ForestContract_Helpers.cs @@ -54,6 +54,7 @@ private void PerformDeal(PerformDealInput performDealInput) PurchaseSymbol = performDealInput.PurchaseSymbol, PurchaseAmount = performDealInput.PurchaseAmount }); + } private struct PerformDealInput @@ -309,6 +310,30 @@ private long GetAllowance(Address address, string symbol) }); return allowance?.Allowance ?? 0; } + + public static string TransferCollectionSymbol(string symbol) + { + if (string.IsNullOrEmpty(symbol)) + return symbol; + + var parts = symbol.Split($"{SymbolSeparator}"); + + if (parts.Length == 1) + { + return parts[0]; + } + + if (parts.Length == 2) + { + var baseString = parts[0]; + if (int.TryParse(parts[1], out int number)) + { + return $"{baseString}{SymbolSeparator}{CollectionSymbolSuffix}"; + } + } + + return symbol; + } private void RequireContractAIServiceFeeConfigSet() { diff --git a/contract/Forest/ForestContract_Sellers.cs b/contract/Forest/ForestContract_Sellers.cs index d816d548..e9904e6c 100644 --- a/contract/Forest/ForestContract_Sellers.cs +++ b/contract/Forest/ForestContract_Sellers.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using AElf; using AElf.Contracts.MultiToken; @@ -120,6 +121,20 @@ public override Empty ListWithFixedPrice(ListWithFixedPriceInput input) Quantity = input.Quantity, WhitelistId = whitelistId }); + var allowance = GetAllowance(Context.Sender, input.Symbol); + var collectionSymbol = TransferCollectionSymbol(input.Symbol); + var collectionAllowance = State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender]; + if (collectionAllowance == null || collectionAllowance == "") + { + State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender] = + Math.Max(allowance, input.Quantity).ToString(); + } + else + { + var originQuantity = long.Parse(collectionAllowance); + State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender] = + (input.Quantity + originQuantity).ToString(); + } State.ListedNFTInfoListMap[input.Symbol][Context.Sender] = listedNftInfoList; @@ -211,6 +226,20 @@ private Empty SingleDelist(DelistInput input) Owner = Context.Sender, Quantity = input.Quantity }); + + var collectionSymbol = TransferCollectionSymbol(input.Symbol); + var collectionAllowance = State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender]; + if (collectionAllowance == null || collectionAllowance == "") + { + State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender] = ""; + } + else + { + var originQuantity = long.Parse(collectionAllowance); + var resultQuantity = originQuantity - input.Quantity; + State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender] = + (resultQuantity >= 0 ? resultQuantity.ToString() : ""); + } return new Empty(); } @@ -275,6 +304,9 @@ public override Empty BatchDeList(BatchDeListInput input) return new Empty(); } + var collectionSymbol = TransferCollectionSymbol(input.Symbol); + var collectionAllowance = State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender]; + foreach (var listedNftInfo in fixedPriceListedNftInfoList) { var projectId = CalculateProjectId(input.Symbol, Context.Sender); @@ -287,6 +319,18 @@ public override Empty BatchDeList(BatchDeListInput input) Owner = listedNftInfo.Owner, Price = listedNftInfo.Price }); + + if (collectionAllowance == null || collectionAllowance == "" || collectionAllowance == "0") + { + continue; + } + + var originQuantity = long.Parse(collectionAllowance); + var resultQuantity = originQuantity - listedNftInfo.Quantity; + State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender] = + (resultQuantity >= 0 ? resultQuantity : 0).ToString(); + collectionAllowance = State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender]; + } return new Empty(); diff --git a/contract/Forest/ForestContract_Views.cs b/contract/Forest/ForestContract_Views.cs index e947dfc9..383756f6 100644 --- a/contract/Forest/ForestContract_Views.cs +++ b/contract/Forest/ForestContract_Views.cs @@ -1,3 +1,4 @@ +using System; using AElf.Types; using Google.Protobuf.WellKnownTypes; @@ -117,12 +118,26 @@ public override GetTotalOfferAmountOutput GetTotalOfferAmount(GetTotalOfferAmoun public override GetTotalEffectiveListedNFTAmountOutput GetTotalEffectiveListedNFTAmount(GetTotalEffectiveListedNFTAmountInput input) { var totalAmount = GetEffectiveListedNFTTotalAmount(input.Address, input.Symbol); + + var collectionSymbol = TransferCollectionSymbol(input.Symbol); + var collectionAllowance = State.ListedNFTTotalAmountMap[collectionSymbol][input.Address]; var allowance = GetAllowance(input.Address, input.Symbol); + + if (collectionAllowance == null || collectionAllowance == "") + { + return new GetTotalEffectiveListedNFTAmountOutput() + { + Symbol = input.Symbol, + Allowance = allowance, + TotalAmount = Math.Max(allowance,totalAmount) + }; + } + var getTotalEffectiveListedNftAmountOutput = new GetTotalEffectiveListedNFTAmountOutput() { Symbol = input.Symbol, Allowance = allowance, - TotalAmount = totalAmount + TotalAmount = long.Parse(collectionAllowance) }; return getTotalEffectiveListedNftAmountOutput; diff --git a/test/Forest.Contracts.Inscription.Tests/InscriptionContractTests.cs b/test/Forest.Contracts.Inscription.Tests/InscriptionContractTests.cs index 0a2b1d6d..dd9da133 100644 --- a/test/Forest.Contracts.Inscription.Tests/InscriptionContractTests.cs +++ b/test/Forest.Contracts.Inscription.Tests/InscriptionContractTests.cs @@ -1620,7 +1620,7 @@ await TokenContractStub.Create.SendAsync(new CreateInput }); var seedOwnedSymbol = "ELFS" + "-0"; - var seedExpTime = "1720590467"; + var seedExpTime = "1752076800"; await TokenContractStub.Create.SendAsync(new CreateInput { Symbol = "SEED-1", diff --git a/test/Forest.Tests/ForestContractTests_Views.cs b/test/Forest.Tests/ForestContractTests_Views.cs index 6417a2d0..a9d62136 100644 --- a/test/Forest.Tests/ForestContractTests_Views.cs +++ b/test/Forest.Tests/ForestContractTests_Views.cs @@ -375,8 +375,8 @@ await UserTokenContractStub.Approve.SendAsync(new ApproveInput() Symbol = NftSymbol, Address = User1Address })); - getTotalEffectiveListedNftAmount.Allowance.ShouldBe(allowanceQuanlity); - getTotalEffectiveListedNftAmount.TotalAmount.ShouldBe(0); + getTotalEffectiveListedNftAmount.Allowance.ShouldBe(1); + getTotalEffectiveListedNftAmount.TotalAmount.ShouldBe(1); } #endregion @@ -1185,7 +1185,7 @@ await BuyerForestContractStub.MakeOffer.SendAsync(new MakeOfferInput() Address = User1Address })); getTotalEffectiveListedNftAmount.Allowance.ShouldBe(0); - getTotalEffectiveListedNftAmount.TotalAmount.ShouldBe(sellQuantity); + getTotalEffectiveListedNftAmount.TotalAmount.ShouldBe(sellQuantity*2); } #endregion