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

Usercenter optimize #161

Merged
merged 21 commits into from
Jul 9, 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
18 changes: 18 additions & 0 deletions contract/Forest/ForestContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,23 @@ public override Empty CreateArt(CreateArtInput input)
});
return new Empty();
}

public override Empty SetMaxBatchCancelOfferCount(Int32Value input)
{
AssertSenderIsAdmin();
Assert(input is { Value: > 0 }, "Invalid input.");

State.MaxBatchCancelOfferCount.Value = input.Value;
return new Empty();
}

public override Empty SetMaxBatchCancelListCount(Int32Value input)
{
AssertSenderIsAdmin();
Assert(input is { Value: > 0 }, "Invalid input.");

State.MaxBatchCancelListCount.Value = input.Value;
return new Empty();
}
}
}
2 changes: 2 additions & 0 deletions contract/Forest/ForestContractConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ public partial class ForestContract
public const string DefaultAIImageSize1024 = "1024x1024";
public const string DefaultAIImageSize512 = "512x512";
public const string DefaultAIImageSize256 = "256x256";
public const int DefaultMaxBatchCancelOfferCount= 20;
public const int DefaultMaxBatchCancelListCount= 20;

}
3 changes: 3 additions & 0 deletions contract/Forest/ForestContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public partial class ForestContractState : ContractState
/// Symbol -> Address TxId -> CreateArtInfo
/// </summary>
public MappedState<Address, string, CreateArtInfo> CreateArtInfoMap { get; set; }
public SingletonState<int> MaxBatchCancelOfferCount { get; set; }

public SingletonState<int> MaxBatchCancelListCount { get; set; }

}
}
62 changes: 55 additions & 7 deletions contract/Forest/ForestContract_Buyers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,12 @@ public override Empty CancelOffer(CancelOfferInput input)
public override Empty CancelOfferListByExpireTime(CancelOfferListByExpireTimeInput input)
{
AssertContractInitialized();
CancelOfferList(input);
return new Empty();
}

private Empty CancelOfferList(CancelOfferListByExpireTimeInput input)
{
Assert(Context.Sender != null, "Invalid input data : Context.Sender");
Assert(input != null, "Invalid input data");
Assert(input.Symbol != null, "Invalid input data : Symbol");
Expand All @@ -548,15 +554,26 @@ public override Empty CancelOfferListByExpireTime(CancelOfferListByExpireTimeInp
Assert(offerList?.Value?.Count > 0, "Offer not exists");


var cancelOfferList = offerList?.Value.Where(existOffer => input.CancelOfferList.Any(cannelOffer =>
AreOffersEqual(existOffer, cannelOffer)
)).ToList();
var cancelOfferList = new List<Offer>();
var remainOfferList = new List<Offer>();
foreach (var existOffer in offerList?.Value)
{
foreach (var cannelOffer in input.CancelOfferList)
{
if (AreOffersEqual(existOffer, cannelOffer))
{
cancelOfferList.Add(existOffer);
}
if (!AreOffersEqual(existOffer, cannelOffer))
{
remainOfferList.Add(existOffer);
}
}
}
Assert(cancelOfferList?.Count > 0, "Cannel Offer not exists");

var newOfferList = new OfferList();
var remainOfferList = offerList?.Value.Where(existOffer => !input.CancelOfferList.Any(cannelOffer =>
AreOffersEqual(existOffer, cannelOffer)
)).ToList();

newOfferList.Value.Add(remainOfferList);

var cancelOfferMap = new Dictionary<string, long>();
Expand Down Expand Up @@ -599,7 +616,7 @@ public override Empty CancelOfferListByExpireTime(CancelOfferListByExpireTimeInp
return new Empty();

}

private bool AreOffersEqual(Offer existOffer, CancelOffer cancelOffer)
{
return existOffer.To == cancelOffer.OfferTo &&
Expand Down Expand Up @@ -757,4 +774,35 @@ private void PerformMakeOffer(MakeOfferInput input, GetBalanceOutput originGetBa
Quantity = input.Quantity
});
}

public override Empty BatchCancelOfferList(BatchCancelOfferListInput input)
{
AssertContractInitialized();
RequireMaxBatchCancelOfferCountSet();
Assert(Context.Sender != null, "Invalid input data : Context.Sender");
Assert(input != null, "Invalid input data");
Assert(input.BatchCancelOfferInfo != null, "Invalid input data : BatchCancelOfferInfo");
Assert(input.BatchCancelOfferInfo.CancelOfferList != null, "Invalid input data : CancelOfferList");
var maxBatchCancelOfferCount = State.MaxBatchCancelOfferCount.Value;
Assert(input.BatchCancelOfferInfo.CancelOfferList.Count <= maxBatchCancelOfferCount, "Invalid cancel list count.");
foreach (var cancelOffer in input.BatchCancelOfferInfo.CancelOfferList)
{
var cancelInput = new CancelOfferListByExpireTimeInput()
{
Symbol = cancelOffer.Symbol,
CancelOfferList = {new CancelOffer()
{
ExpireTime = cancelOffer.ExpireTime,
OfferTo = cancelOffer.OfferTo,
Price = new Price()
{
Symbol = cancelOffer.Price.Symbol,
Amount = cancelOffer.Price.Amount
}
}}
};
CancelOfferList(cancelInput);
}
return new Empty();
}
}
18 changes: 18 additions & 0 deletions contract/Forest/ForestContract_Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,22 @@ private void CheckCreateArtParams(CreateArtInput input)
var size = State.AIImageSizeList.Value.Value;
Assert(State.AIImageSizeList.Value.Value.Contains(input.Size), $"Invalid input size");
}

private void RequireMaxBatchCancelOfferCountSet()
{
var maxCount = State.MaxBatchCancelOfferCount.Value;
if (maxCount <= 0)
{
State.MaxBatchCancelOfferCount.Value = DefaultMaxBatchCancelOfferCount;
}
}

private void RequireMaxBatchCancelListCountSet()
{
var maxCount = State.MaxBatchCancelListCount.Value;
if (maxCount <= 0)
{
State.MaxBatchCancelListCount.Value = DefaultMaxBatchCancelListCount;
}
}
}
51 changes: 47 additions & 4 deletions contract/Forest/ForestContract_Sellers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public override Empty ListWithFixedPrice(ListWithFixedPriceInput input)
}

public override Empty Delist(DelistInput input)
{
SingleDelist(input);
return new Empty();
}

private Empty SingleDelist(DelistInput input)
{
Assert(input.Quantity > 0, "Quantity must be a positive integer.");
var listedNftInfoList = State.ListedNFTInfoListMap[input.Symbol][Context.Sender];
Expand All @@ -147,10 +153,18 @@ public override Empty Delist(DelistInput input)
}

Assert(input.Price != null, "Need to specific list record.");
var listedNftInfo = listedNftInfoList.Value.FirstOrDefault(i =>
i.Price.Amount == input.Price.Amount && i.Price.Symbol == input.Price.Symbol &&
i.Owner == Context.Sender &&
(input.StartTime == null ? true : input.StartTime.Seconds == i.Duration.StartTime.Seconds));
ListedNFTInfo listedNftInfo = null;

foreach (var i in listedNftInfoList.Value)
{
if (i.Price.Amount == input.Price.Amount && i.Price.Symbol == input.Price.Symbol &&
i.Owner == Context.Sender &&
(input.StartTime == null ? true : input.StartTime.Seconds == i.Duration.StartTime.Seconds))
{
listedNftInfo = i;
break;
}
}

if (listedNftInfo == null)
{
Expand Down Expand Up @@ -370,4 +384,33 @@ public override Empty Deal(DealInput input)
});
return new Empty();
}

public override Empty BatchCancelList(BatchCancelListInput input)
{
AssertContractInitialized();
RequireMaxBatchCancelListCountSet();
Assert(Context.Sender != null, "Invalid input data : Context.Sender");
Assert(input != null, "Invalid input data");
Assert(input.BatchCancelListInfo != null, "Invalid input data : Symbol");
Assert(input.BatchCancelListInfo.CancelList != null, "Invalid input data : CancelList");
var maxBatchCancelCount = State.MaxBatchCancelListCount.Value;
Assert(input.BatchCancelListInfo.CancelList.Count <= maxBatchCancelCount, "Invalid cancel list count.");
foreach (var cancelInfo in input.BatchCancelListInfo.CancelList)
{
var cancelInput = new DelistInput()
{
Symbol = cancelInfo.Symbol,
Quantity = cancelInfo.Quantity,
Price = new Price()
{
Symbol = cancelInfo.Price.Symbol,
Amount = cancelInfo.Price.Amount
},
StartTime = cancelInfo.StartTime
};
SingleDelist(cancelInput);
}
return new Empty();

}
}
10 changes: 10 additions & 0 deletions contract/Forest/ForestContract_Views.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,14 @@ public override CreateArtInfo GetCreateArtInfo(GetCreateArtInfoInput input)
Assert(input != null, "Invalid TransactionId");
return State.CreateArtInfoMap[input.Address ?? Context.Sender][input.TransactionId];
}

public override Int32Value GetMaxBatchCancelOfferCount(Empty input)
{
return new Int32Value { Value = State.MaxBatchCancelOfferCount.Value };
}

public override Int32Value GetMaxBatchCancelListCount(Empty input)
{
return new Int32Value { Value = State.MaxBatchCancelListCount.Value };
}
}
44 changes: 43 additions & 1 deletion protobuf/forest_contract.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ service ForestContract {
}
rpc CancelOfferListByExpireTime (CancelOfferListByExpireTimeInput) returns (google.protobuf.Empty) {
}

rpc BatchCancelOfferList (BatchCancelOfferListInput) returns (google.protobuf.Empty) {
}
rpc BatchCancelList (BatchCancelListInput) returns (google.protobuf.Empty){
}
rpc BatchBuyNow (BatchBuyNowInput) returns (google.protobuf.Empty){
}

Expand Down Expand Up @@ -74,7 +79,13 @@ service ForestContract {

rpc RemoveAIImageSize (google.protobuf.StringValue) returns (google.protobuf.Empty){
}

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

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

// Views.
rpc GetListedNFTInfoList (GetListedNFTInfoListInput) returns (ListedNFTInfoList) {
option (aelf.is_view) = true;
Expand Down Expand Up @@ -124,6 +135,14 @@ service ForestContract {
rpc GetCreateArtInfo (GetCreateArtInfoInput) returns (CreateArtInfo){
option (aelf.is_view) = true;
}

rpc GetMaxBatchCancelOfferCount(google.protobuf.Empty) returns (google.protobuf.Int32Value){
option (aelf.is_view) = true;
}

rpc GetMaxBatchCancelListCount(google.protobuf.Empty) returns (google.protobuf.Int32Value){
option (aelf.is_view) = true;
}
}

// Structs.
Expand All @@ -141,7 +160,6 @@ message ListedNFTInfoList {
repeated ListedNFTInfo value = 1;
}


message StringList {
repeated string value = 1;
}
Expand Down Expand Up @@ -269,6 +287,30 @@ message CancelOfferListByExpireTimeInput {
repeated CancelOffer cancel_offer_list =2;
}

message BatchCancelOfferListInput {
BatchCancelOfferInfo batch_cancel_offer_info = 1;
}

message BatchCancelOfferInfo{
repeated CancelOfferList cancel_offer_list =1;
}

message CancelOfferList{
string symbol = 1;
google.protobuf.Timestamp expire_time = 2;
aelf.Address offer_to = 3;
Price price = 4;
}

message BatchCancelListInput{
BatchCancelListInfo batch_cancel_list_info = 1;
}

message BatchCancelListInfo{
repeated DelistInput cancel_list =1;
}


message CancelOffer{
google.protobuf.Timestamp expire_time = 1;
aelf.Address offer_to = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<OutputItemType>Contract</OutputItemType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</PackageReference>
<PackageReference Include="AElf.Contracts.MultiToken" Version="1.5.0">
<PackageReference Include="AElf.Contracts.MultiToken" Version="1.7.0">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<OutputItemType>Contract</OutputItemType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<OutputItemType>Contract</OutputItemType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</PackageReference>
<PackageReference Include="AElf.Contracts.MultiToken" Version="1.5.0">
<PackageReference Include="AElf.Contracts.MultiToken" Version="1.7.0">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<OutputItemType>Contract</OutputItemType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
4 changes: 2 additions & 2 deletions test/Forest.Contracts.Auction.Tests/AuctionContractTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public async Task CreateAuctionTests_Fail()
{
Symbol = "SEED-2"
});
result.TransactionResult.Error.ShouldContain("Only support 721 type NFT.");
result.TransactionResult.Error.ShouldContain("Invalid input auction type.");

result = await AuctionContractStub.CreateAuction.SendWithExceptionAsync(new CreateAuctionInput
{
Expand Down Expand Up @@ -904,7 +904,7 @@ await TokenContractStub.Create.SendAsync(new CreateInput
Issuer = DefaultAddress,
Symbol = "SEED-2",
TokenName = "SEED-TEST",
TotalSupply = 2,
TotalSupply = 1,
Decimals = 0,
IsBurnable = true,
LockWhiteList = { TokenContractAddress },
Expand Down
4 changes: 0 additions & 4 deletions test/Forest.Tests/Forest.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,4 @@
<ProjectReference Include="..\..\src\AElf.Boilerplate.TestBase\AElf.Boilerplate.TestBase.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Protobuf\Proto\reference\" />
</ItemGroup>

</Project>
Loading
Loading