Skip to content

Commit

Permalink
Collection approve (#168)
Browse files Browse the repository at this point in the history
* feat: add ListedNFTTotalAmountMap
* ut: fix ut
* feat: modify GetTotalEffectiveListedNFTAmount
  • Loading branch information
Jecket1 authored Jul 19, 2024
1 parent c384b51 commit 9643630
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 5 deletions.
2 changes: 2 additions & 0 deletions contract/Forest/ForestContractConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "-";

}
5 changes: 5 additions & 0 deletions contract/Forest/ForestContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public partial class ForestContractState : ContractState
/// </summary>
public MappedState<Address, string, long> OfferTotalAmountMap { get; set; }

/// <summary>
/// Collection Symbol -> User Address -> Listing Amount * decimal
/// </summary>
public MappedState<string, Address, string> ListedNFTTotalAmountMap { get; set; }

public SingletonState<Price> AIServiceFeeConfig { get; set; }
public SingletonState<Address> AIServiceFeeReceiver { get; set; }

Expand Down
28 changes: 28 additions & 0 deletions contract/Forest/ForestContract_Buyers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand All @@ -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();
}

}
}

Expand Down
25 changes: 25 additions & 0 deletions contract/Forest/ForestContract_Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private void PerformDeal(PerformDealInput performDealInput)
PurchaseSymbol = performDealInput.PurchaseSymbol,
PurchaseAmount = performDealInput.PurchaseAmount
});

}

private struct PerformDealInput
Expand Down Expand Up @@ -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()
{
Expand Down
44 changes: 44 additions & 0 deletions contract/Forest/ForestContract_Sellers.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using AElf;
using AElf.Contracts.MultiToken;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
17 changes: 16 additions & 1 deletion contract/Forest/ForestContract_Views.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using AElf.Types;
using Google.Protobuf.WellKnownTypes;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions test/Forest.Tests/ForestContractTests_Views.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9643630

Please sign in to comment.