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

Add batch deal amount #193

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 15 additions & 7 deletions contract/Forest/ForestContract_Buyers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ 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 batchDealAmount = 0l;
foreach (var fixPrice in input.FixPriceList)
{
SingleMakeOfferForBatchBuyNow(input.Symbol, new FixPrice()
Expand All @@ -237,7 +238,8 @@ public override Empty BatchBuyNow(BatchBuyNowInput input)
Quantity = fixPrice.Quantity,
Price = fixPrice.Price,
StartTime = fixPrice.StartTime
}, userBalanceDic, failPriceDic);
}, userBalanceDic, failPriceDic,out var dealTotalAmount);
batchDealAmount += dealTotalAmount;
}

Context.Fire(new BatchBuyNowResult
Expand All @@ -247,16 +249,19 @@ public override Empty BatchBuyNow(BatchBuyNowInput input)
FailPriceList = new FailPriceList()
{
Value = { failPriceDic?.Values?.ToList() }
}
},
TotalDealAmountPrice = batchDealAmount
});

return new Empty();
}

private void SingleMakeOfferForBatchBuyNow(string symbol, FixPrice inputFixPrice
, Dictionary<string,long> userBalanceDic
, Dictionary<long, FailPrice> failPriceDic)
, Dictionary<long, FailPrice> failPriceDic,
out long dealTotalAmount)
{
dealTotalAmount = 0;
Assert(inputFixPrice.Quantity > 0, "Invalid param Quantity.");
Assert(inputFixPrice.Price.Amount > 0, "Invalid price amount.");
Assert(inputFixPrice.OfferTo != null, "Invalid param OfferTo.");
Expand All @@ -266,7 +271,7 @@ private void SingleMakeOfferForBatchBuyNow(string symbol, FixPrice inputFixPrice
});
Assert(nftInfo != null && !string.IsNullOrWhiteSpace(nftInfo.Symbol), "Invalid symbol data");
Assert(inputFixPrice.Quantity <= nftInfo?.TotalSupply, "Offer quantity beyond totalSupply");

var balance = State.TokenContract.GetBalance.Call(new GetBalanceInput
{
Symbol = inputFixPrice.Price.Symbol,
Expand Down Expand Up @@ -300,8 +305,8 @@ private void SingleMakeOfferForBatchBuyNow(string symbol, FixPrice inputFixPrice
var listedNftInfo = affordableNftInfoList[dealResult.Index];

TryDealWithFixedPriceForBatch(sender, symbol, inputFixPrice, dealResult
, listedNftInfo, userBalanceDic, out var dealQuantity,nftInfo.Decimals);

, listedNftInfo, userBalanceDic, out var dealQuantity,nftInfo.Decimals, out var dealAmount);
dealTotalAmount += dealAmount;
long realFail = dealResult.Quantity - dealQuantity;
if (realFail > 0)
{
Expand Down Expand Up @@ -681,7 +686,7 @@ private bool TryDealWithFixedPrice(Address sender, MakeOfferInput input, DealRes
/// Sender is buyer.
/// </summary>
private bool TryDealWithFixedPriceForBatch(Address sender, string symbol, FixPrice input, DealResult dealResult,
ListedNFTInfo listedNftInfo, Dictionary<string,long> userBalanceDic ,out long actualQuantity,int decimals)
ListedNFTInfo listedNftInfo, Dictionary<string,long> userBalanceDic ,out long actualQuantity,int decimals, out long dealAmount)
{
var userBalanceKey = symbol + input.OfferTo;
long senderBalanceCount;
Expand All @@ -703,6 +708,7 @@ private bool TryDealWithFixedPriceForBatch(Address sender, string symbol, FixPri
if (senderBalanceCount== 0)
{
actualQuantity = 0;
dealAmount = 0;
return false;
}
var usePrice = input.Price.Clone();
Expand All @@ -712,13 +718,15 @@ private bool TryDealWithFixedPriceForBatch(Address sender, string symbol, FixPri

if (actualQuantity == 0)
{
dealAmount = 0;
return false;
}

senderBalanceCount -= actualQuantity;
userBalanceDic[userBalanceKey]=senderBalanceCount;

var totalAmount = usePrice.Amount.Mul(NumberHelper.DivideByPowerOfTen(actualQuantity, decimals));
dealAmount = totalAmount;
PerformDeal(new PerformDealInput
{
NFTFrom = input.OfferTo,
Expand Down
1 change: 1 addition & 0 deletions protobuf/forest_contract.proto
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ message BatchBuyNowResult{
string symbol = 1;
bool all_success_flag = 2;
FailPriceList fail_price_list = 3;
int64 total_deal_amount_price = 4;
}

message FailPriceList {
Expand Down
Loading