Skip to content

Commit

Permalink
add rpc TreeLevelUpgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
fyannch committed Oct 29, 2024
1 parent fc636cf commit 7181e43
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
2 changes: 2 additions & 0 deletions contract/Forest/ForestContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public partial class ForestContractState : ContractState
public MappedState<Address, long> TreePointsAddTimeMap { get; set; }
/// Address -> activityId -> timestamp 13
public MappedState<Address, string, long> TreePointsActivityClaimTimeMap { get; set; }
/// Address -> timestamp 13
public MappedState<Address, long> TreePointsLevelUpgradeTimeMap { get; set; }


}
Expand Down
40 changes: 38 additions & 2 deletions contract/Forest/ForestContract_Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ public override Empty ClaimTreePoints(ClaimTreePointsInput input)
Amount = input.Reward.Amount
});
//event
Context.Fire(new TreePointsRemoved()
Context.Fire(new TreePointsClaimed()
{
Owner = input.Address,
Points = input.Points,
PointsType = input.PointsType,
ActivityId = input.ActivityId,
OpTime = input.OpTime,
RewardSymbol = input.Reward.Symbol,
Expand All @@ -119,4 +118,41 @@ public override Empty SetTreePointsHashVerifyKey(StringValue input)
State.TreePointsHashVerifyKey.Value = input.Value;
return new Empty();
}

public override Empty TreeLevelUpgrade(TreeLevelUpgradeInput input)
{
AssertContractInitialized();
Assert(input != null, "Invalid param");
Assert(!input.Address.Value.IsNullOrEmpty(), "Invalid param Address");
Assert(input.Points > 0, "Invalid param Points");
Assert(!string.IsNullOrEmpty(input.RequestHash), "Invalid param RequestHash");
Assert(input.OpTime != null && input.OpTime > 0, "Invalid param OpTime");
Assert(input.UpgradeLevel > 0, $"Invalid UpgradeLevel, Should be greater than 0");
Assert(Context.Sender == input.Address, "Param Address is not Sender");

var requestStr = string.Concat(input.Address, input.Points, input.OpTime, input.UpgradeLevel);
CheckPointsRequestHash(requestStr, input.RequestHash);

var lastOpTime = State.TreePointsLevelUpgradeTimeMap[input.Address];
Assert(input.OpTime > lastOpTime, "Invalid param OpTime");

var treePointsInfo = State.TreePointsMap[input.Address];
Assert(treePointsInfo != null, "your points is zero");
Assert(treePointsInfo.Points >= input.Points, "You don't have enough points");
treePointsInfo.Points -= input.Points;
State.TreePointsMap[input.Address] = treePointsInfo;
State.TreePointsLevelUpgradeTimeMap[input.Address] = input.OpTime;

//event
Context.Fire(new TreeLevelUpgraded()
{
Owner = input.Address,
Points = input.Points,
OpTime = input.OpTime,
UpgradeLevel = input.UpgradeLevel,
TotalPoints = treePointsInfo.Points,
RequestHash = input.RequestHash
});
return new Empty();
}
}
37 changes: 29 additions & 8 deletions protobuf/forest_contract.proto
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ service ForestContract {
rpc ClaimTreePoints(ClaimTreePointsInput) returns (google.protobuf.Empty){
}

rpc TreeLevelUpgrade(TreeLevelUpgradeInput) returns (google.protobuf.Empty){
}

rpc GetTreePoints(aelf.Address) returns (TreePointsInfo){
option (aelf.is_view) = true;
}
Expand Down Expand Up @@ -496,6 +499,14 @@ message ClaimTreePointsInput {
string request_hash = 7;
}

message TreeLevelUpgradeInput {
aelf.Address address = 1;
int64 points = 2;
int64 op_time = 3;
int32 upgrade_level = 4;
string request_hash = 5;
}

message TreeReward {
string symbol = 1;
int64 amount = 2;
Expand Down Expand Up @@ -688,17 +699,27 @@ message TreePointsAdded {
string request_hash = 6;
}

message TreePointsRemoved {
message TreePointsClaimed {
option (aelf.is_event) = true;
aelf.Address owner = 1;
int64 points = 2;
int32 points_type = 3;
string activity_id = 4;
int64 op_time = 5;
string reward_symbol = 6;
int64 reward_amount = 7;
int64 total_points = 8;
string request_hash = 9;
string activity_id = 3;
int64 op_time = 4;
string reward_symbol = 5;
int64 reward_amount = 6;
int64 total_points = 7;
string request_hash = 8;
}

message TreeLevelUpgraded {
option (aelf.is_event) = true;
aelf.Address owner = 1;
int64 points = 2;
int32 upgrade_level = 3;
int64 op_time = 4;
int64 total_points = 5;
string request_hash = 6;
}



0 comments on commit 7181e43

Please sign in to comment.