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

Sync master #187

Merged
merged 1 commit into from
Aug 21, 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
8 changes: 8 additions & 0 deletions contract/Forest/ForestContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,13 @@ public override Empty SetMaxBatchCancelListCount(Int32Value input)
State.MaxBatchCancelListCount.Value = input.Value;
return new Empty();
}

public override Empty SetCollectionListTotalCount(SetCollectionListTotalCountInput input)
{
Assert(input != null && input.Address != null && !string.IsNullOrEmpty(input.Symbol) && input.Count >=0, "Invalid input.");

State.ListedNFTTotalAmountMap[input.Symbol][input.Address] = input.Count.ToString();
return new Empty();
}
}
}
26 changes: 17 additions & 9 deletions contract/Forest/ForestContract_Buyers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,25 @@ public override Empty BatchBuyNow(BatchBuyNowInput input)
Assert(nftInfo != null && !string.IsNullOrWhiteSpace(nftInfo.Symbol), "Invalid symbol data");
var userBalanceDic = new Dictionary<string,long>();
var failPriceDic = new Dictionary<long, FailPrice>();
var listOwnerAllowanceDic = new Dictionary<string, long>();

foreach (var fixPrice in input.FixPriceList)
{
SingleMakeOfferForBatchBuyNow(input.Symbol, new FixPrice()
Assert(fixPrice.Quantity > 0, "Invalid param Quantity.");
Assert(fixPrice.Price.Amount > 0, "Invalid price amount.");
Assert(fixPrice.OfferTo != null, "Invalid param OfferTo.");
if(!listOwnerAllowanceDic.ContainsKey(fixPrice.OfferTo.ToBase58()))
{
listOwnerAllowanceDic[fixPrice.OfferTo.ToBase58()] =GetAllowance(fixPrice.OfferTo, input.Symbol);
}

SingleMakeOfferForBatchBuyNow(input.Symbol, new FixPrice()
{
OfferTo = fixPrice.OfferTo,
Quantity = fixPrice.Quantity,
Price = fixPrice.Price,
StartTime = fixPrice.StartTime
}, userBalanceDic, failPriceDic);
}, userBalanceDic, failPriceDic, listOwnerAllowanceDic);
}

Context.Fire(new BatchBuyNowResult
Expand All @@ -255,11 +265,9 @@ public override Empty BatchBuyNow(BatchBuyNowInput input)

private void SingleMakeOfferForBatchBuyNow(string symbol, FixPrice inputFixPrice
, Dictionary<string,long> userBalanceDic
, Dictionary<long, FailPrice> failPriceDic)
, Dictionary<long, FailPrice> failPriceDic
, Dictionary<string, long> listOwnerAllowanceDic)
{
Assert(inputFixPrice.Quantity > 0, "Invalid param Quantity.");
Assert(inputFixPrice.Price.Amount > 0, "Invalid price amount.");
Assert(inputFixPrice.OfferTo != null, "Invalid param OfferTo.");
var nftInfo = State.TokenContract.GetTokenInfo.Call(new GetTokenInfoInput
{
Symbol = symbol,
Expand Down Expand Up @@ -287,14 +295,14 @@ private void SingleMakeOfferForBatchBuyNow(string symbol, FixPrice inputFixPrice
Assert(nftInfo.Supply > 0, "NFT does not exist.");

var dealService = GetDealService();
var toRemove = new ListedNFTInfoList();

var normalPriceDealResultList = dealService.GetDealResultListForBatchBuy(symbol, inputFixPrice,
new ListedNFTInfoList
{
Value = { affordableNftInfoList }
}, failPriceDic).ToList();
Assert(normalPriceDealResultList.Count > 0, "NormalPrice does not exist.");
}, failPriceDic, toRemove, listOwnerAllowanceDic).ToList();

var toRemove = new ListedNFTInfoList();
foreach (var dealResult in normalPriceDealResultList)
{
var listedNftInfo = affordableNftInfoList[dealResult.Index];
Expand Down
11 changes: 10 additions & 1 deletion contract/Forest/ForestContract_Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ private void AssertAllowanceInsufficient(string symbol, Address address, long cu
var totalAmount = amount.Add(currentAmount);
Assert(allowance >= totalAmount, $"The allowance you set is less than required. Please reset it.");
}

private long GetOfferTotalAmount(Address address, string symbol)
{
Assert(address != null, $"Invalid param Address");
Expand All @@ -292,6 +292,14 @@ private long GetEffectiveListedNFTTotalAmount(Address address, string symbol)
Assert(address != null, $"Invalid param Address");
Assert(symbol != null, $"Invalid param Symbol");

var collectionSymbol = TransferCollectionSymbol(symbol);
var collectionListedNFTTotalAmount = State.ListedNFTTotalAmountMap[collectionSymbol][address];

if (collectionListedNFTTotalAmount != null && collectionListedNFTTotalAmount != "")
{
return long.Parse(collectionListedNFTTotalAmount);
}

var listedNftInfoList = State.ListedNFTInfoListMap[symbol][address];
var totalAmount = 0L;
if (listedNftInfoList != null)
Expand All @@ -305,6 +313,7 @@ private long GetEffectiveListedNFTTotalAmount(Address address, string symbol)
}
}
}

return totalAmount;
}

Expand Down
2 changes: 1 addition & 1 deletion contract/Forest/ForestContract_Sellers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public override Empty ListWithFixedPrice(ListWithFixedPriceInput input)
var collectionAllowance = State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender];
if (collectionAllowance == null || collectionAllowance == "")
{
var listedNFTTotalAmount = (allowance >= MaxApproveAllowance) ? input.Quantity : Math.Max(allowance, input.Quantity);
var listedNFTTotalAmount = input.Quantity;
State.ListedNFTTotalAmountMap[collectionSymbol][Context.Sender] = listedNFTTotalAmount.ToString();
}
else
Expand Down
6 changes: 3 additions & 3 deletions contract/Forest/ForestContract_Views.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ public override GetTotalEffectiveListedNFTAmountOutput GetTotalEffectiveListedNF
var totalAmount = GetEffectiveListedNFTTotalAmount(input.Address, input.Symbol);

var collectionSymbol = TransferCollectionSymbol(input.Symbol);
var collectionAllowance = State.ListedNFTTotalAmountMap[collectionSymbol][input.Address];
var collectionListedNFTTotalAmount = State.ListedNFTTotalAmountMap[collectionSymbol][input.Address];
var allowance = GetAllowance(input.Address, input.Symbol);

if (collectionAllowance == null || collectionAllowance == "")
if (collectionListedNFTTotalAmount == null || collectionListedNFTTotalAmount == "")
{
return new GetTotalEffectiveListedNFTAmountOutput()
{
Expand All @@ -121,7 +121,7 @@ public override GetTotalEffectiveListedNFTAmountOutput GetTotalEffectiveListedNF
{
Symbol = input.Symbol,
Allowance = allowance,
TotalAmount = (long.Parse(collectionAllowance) >= MaxApproveAllowance) ? DefaultApproveAllowance : long.Parse(collectionAllowance)
TotalAmount = (long.Parse(collectionListedNFTTotalAmount) >= MaxApproveAllowance) ? DefaultApproveAllowance : long.Parse(collectionListedNFTTotalAmount)
};

return getTotalEffectiveListedNftAmountOutput;
Expand Down
81 changes: 54 additions & 27 deletions contract/Forest/Services/DealService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AElf.Contracts.MultiToken;
using AElf.CSharp.Core;
using AElf.Sdk.CSharp;

Expand Down Expand Up @@ -67,7 +69,8 @@ public IEnumerable<DealResult> GetDealResultList(GetDealResultListInput input)
}

public IEnumerable<DealResult> GetDealResultListForBatchBuy(string symbol, FixPrice inputFixPrice
, ListedNFTInfoList listedNftInfoList, Dictionary<long, FailPrice> failPriceDic)
, ListedNFTInfoList listedNftInfoList, Dictionary<long, FailPrice> failPriceDic, ListedNFTInfoList toRemove,
Dictionary<string, long> listOwnerAllowanceDic)
{

var dealResultList = new List<DealResult>();
Expand All @@ -78,27 +81,62 @@ public IEnumerable<DealResult> GetDealResultListForBatchBuy(string symbol, FixPr
i.Price.Symbol == inputFixPrice.Price.Symbol
&& blockTime >= i.Duration.StartTime
&& blockTime >= i.Duration.PublicTime
).OrderByDescending(i => i.Duration.PublicTime))
).OrderByDescending(i => i.Duration.PublicTime))
{
long failNumber = 0;
if (listedNftInfo.Quantity >= needToDealQuantity)

var defaultAllowance = 0;

if (!listOwnerAllowanceDic.TryGetValue(listedNftInfo.Owner.ToBase58(), out var allowance))
{
var dealResult = new DealResult
{
Symbol = symbol,
Quantity = needToDealQuantity,
PurchaseSymbol = inputFixPrice.Price.Symbol,
PurchaseAmount = listedNftInfo.Price.Amount,
Duration = listedNftInfo.Duration,
Index = currentIndex
};
// Fulfill demands.
dealResultList.Add(dealResult);
needToDealQuantity = 0;
allowance = defaultAllowance;
}

var minAllowance = Math.Min(needToDealQuantity, listedNftInfo.Quantity);
if (allowance < minAllowance)
{
toRemove.Value.Add(listedNftInfo);

failNumber = needToDealQuantity;
}
else
{
failNumber = inputFixPrice.Quantity - listedNftInfo.Quantity;
listOwnerAllowanceDic[listedNftInfo.Owner.ToBase58()] = allowance - minAllowance;
if (listedNftInfo.Quantity >= needToDealQuantity)
{
var dealResult = new DealResult
{
Symbol = symbol,
Quantity = needToDealQuantity,
PurchaseSymbol = inputFixPrice.Price.Symbol,
PurchaseAmount = listedNftInfo.Price.Amount,
Duration = listedNftInfo.Duration,
Index = currentIndex
};
// Fulfill demands.
dealResultList.Add(dealResult);
needToDealQuantity = 0;
}
else
{
failNumber = inputFixPrice.Quantity - listedNftInfo.Quantity;

var dealResult = new DealResult
{
Symbol = symbol,
Quantity = listedNftInfo.Quantity,
PurchaseSymbol = inputFixPrice.Price.Symbol,
PurchaseAmount = listedNftInfo.Price.Amount,
Duration = listedNftInfo.Duration,
Index = currentIndex
};
dealResultList.Add(dealResult);
needToDealQuantity = needToDealQuantity.Sub(listedNftInfo.Quantity);
}
}

if (failNumber != 0)
{
if (failPriceDic.TryGetValue(inputFixPrice.Price.Amount, out var value))
{
value.Quantity += failNumber;
Expand All @@ -115,17 +153,6 @@ public IEnumerable<DealResult> GetDealResultListForBatchBuy(string symbol, FixPr
}
});
}
var dealResult = new DealResult
{
Symbol = symbol,
Quantity = listedNftInfo.Quantity,
PurchaseSymbol = inputFixPrice.Price.Symbol,
PurchaseAmount = listedNftInfo.Price.Amount,
Duration = listedNftInfo.Duration,
Index = currentIndex
};
dealResultList.Add(dealResult);
needToDealQuantity = needToDealQuantity.Sub(listedNftInfo.Quantity);
}

if (needToDealQuantity == 0)
Expand Down
9 changes: 9 additions & 0 deletions protobuf/forest_contract.proto
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ service ForestContract {

rpc SetMaxBatchCancelListCount(google.protobuf.Int32Value) returns (google.protobuf.Empty){
}

rpc SetCollectionListTotalCount(SetCollectionListTotalCountInput) returns (google.protobuf.Empty){
}

// Views.
rpc GetListedNFTInfoList (GetListedNFTInfoListInput) returns (ListedNFTInfoList) {
Expand Down Expand Up @@ -418,6 +421,12 @@ message SetAIServiceFeeInput {
aelf.Address service_fee_receiver = 2;
}

message SetCollectionListTotalCountInput {
aelf.Address address = 1;
int64 count = 2;
string symbol = 3;
}

message CreateArtInput {
string promt = 1;
string negative_prompt = 2;
Expand Down
2 changes: 1 addition & 1 deletion test/Forest.Tests/ForestContractTest_Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public async Task RoyaltyTest()
await InitializeForestContract();
await PrepareNftData();

var res = await Seller1ForestContractStub.SetRoyalty.SendAsync(new SetRoyaltyInput()
var res = await AdminForestContractStub.SetRoyalty.SendAsync(new SetRoyaltyInput()
{
Symbol = NftSymbol,
Royalty = 100,
Expand Down
Loading
Loading