Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch Approve #170

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 All @@ -226,7 +255,7 @@
AssertContractInitialized();
Assert(input.Price != null, "Need to specific list price.");
Assert(input.Price.Amount > 0, "Incorrect listing price.");
Assert(input.BatchDelistType != null, "Incorrect listing batchDelistType.");

Check warning on line 258 in contract/Forest/ForestContract_Sellers.cs

View workflow job for this annotation

GitHub Actions / static-code-analysis

The result of the expression is always 'true' since a value of type 'int' is never equal to 'null' of type 'int?'
var tokenInfo = State.TokenContract.GetTokenInfo.Call(new GetTokenInfoInput
{
Symbol = input.Symbol
Expand Down Expand Up @@ -275,6 +304,9 @@
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 @@
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
Loading