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

Approve optimize #174

Merged
merged 6 commits into from
Jul 25, 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
7 changes: 6 additions & 1 deletion contract/Forest/ForestContractConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ 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 = "-";
public const long DefaultApproveAllowance = 10000;
public const long MaxApproveAllowance = 9223372000000000000;


}
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
46 changes: 46 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,22 @@ 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 == "")
{
var listedNFTTotalAmount = (allowance >= MaxApproveAllowance) ? input.Quantity : Math.Max(allowance, input.Quantity);
State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender] = listedNFTTotalAmount.ToString();
}
else
{
var originQuantity = (long.Parse(collectionAllowance) >= MaxApproveAllowance)
? DefaultApproveAllowance
: 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 +228,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() : "0");
}

return new Empty();
}
Expand Down Expand Up @@ -275,6 +306,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 +321,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 = (allowance >= MaxApproveAllowance) ? totalAmount : Math.Max(allowance,totalAmount)
};
}

var getTotalEffectiveListedNftAmountOutput = new GetTotalEffectiveListedNFTAmountOutput()
{
Symbol = input.Symbol,
Allowance = allowance,
TotalAmount = totalAmount
TotalAmount = (long.Parse(collectionAllowance) >= MaxApproveAllowance) ? DefaultApproveAllowance : 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