-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from eforest-finance/release/2.26.0
Mini app
- Loading branch information
Showing
6 changed files
with
483 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
using AElf; | ||
using AElf.Contracts.MultiToken; | ||
using AElf.Sdk.CSharp; | ||
using Google.Protobuf.WellKnownTypes; | ||
|
||
namespace Forest; | ||
|
||
public partial class ForestContract | ||
{ | ||
public override Empty AddTreePoints(AddTreePointsInput 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(Context.Sender == input.Address, "Param Address is not Sender"); | ||
|
||
var requestStr = string.Concat(input.Address.ToBase58(), input.Points, input.PointsType, input.OpTime); | ||
CheckPointsRequestHash(requestStr, input.RequestHash); | ||
|
||
var lastAddTime = State.TreePointsAddTimeMap[input.Address]; | ||
Assert(input.OpTime > lastAddTime, "Invalid param OpTime"); | ||
|
||
var treePointsInfo = State.TreePointsMap[input.Address]; | ||
if (treePointsInfo != null) | ||
{ | ||
treePointsInfo.Points += input.Points; | ||
} | ||
else | ||
{ | ||
treePointsInfo = new TreePointsInfo() | ||
{ | ||
Owner = input.Address, | ||
Points = input.Points, | ||
Level = 1 | ||
}; | ||
} | ||
|
||
State.TreePointsMap[input.Address] = treePointsInfo; | ||
State.TreePointsAddTimeMap[input.Address] = input.OpTime; | ||
|
||
//event | ||
Context.Fire(new TreePointsAdded() | ||
{ | ||
Owner = input.Address, | ||
Points = input.Points, | ||
PointsType = input.PointsType, | ||
OpTime = input.OpTime, | ||
TotalPoints = treePointsInfo.Points, | ||
RequestHash = input.RequestHash | ||
}); | ||
return new Empty(); | ||
} | ||
public override Empty ClaimTreePoints(ClaimTreePointsInput 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.ActivityId), "Invalid param ActivityId"); | ||
Assert(!string.IsNullOrEmpty(input.RequestHash), "Invalid param RequestHash"); | ||
Assert(input.OpTime != null && input.OpTime > 0, "Invalid param OpTime"); | ||
Assert(input.Reward != null && !string.IsNullOrEmpty(input.Reward.Symbol) && input.Reward.Amount > 0, "Invalid param Reward"); | ||
Assert(Context.Sender == input.Address, "Param Address is not Sender"); | ||
|
||
var requestStr = string.Concat(input.Address.ToBase58(), input.ActivityId,input.Points, input.OpTime); | ||
requestStr = string.Concat(requestStr, input.Reward.Symbol, input.Reward.Amount); | ||
CheckPointsRequestHash(requestStr, input.RequestHash); | ||
|
||
var lastOpTime = State.TreePointsActivityClaimTimeMap[input.Address][input.ActivityId]; | ||
Assert(lastOpTime == 0, "You have already participated in this activity"); | ||
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.TreePointsActivityClaimTimeMap[input.Address][input.ActivityId] = input.OpTime; | ||
|
||
//transfer award | ||
var balance = State.TokenContract.GetBalance.Call(new GetBalanceInput | ||
{ | ||
Symbol = input.Reward.Symbol, | ||
Owner = Context.Self | ||
}); | ||
Assert(balance.Balance >= input.Reward.Amount,$"The platform does not have enough {input.Reward.Symbol}"); | ||
State.TokenContract.Transfer.Send(new TransferInput | ||
{ | ||
To = input.Address, | ||
Symbol = input.Reward.Symbol, | ||
Amount = input.Reward.Amount | ||
}); | ||
//event | ||
Context.Fire(new TreePointsClaimed() | ||
{ | ||
Owner = input.Address, | ||
Points = input.Points, | ||
ActivityId = input.ActivityId, | ||
OpTime = input.OpTime, | ||
RewardSymbol = input.Reward.Symbol, | ||
RewardAmount = input.Reward.Amount, | ||
TotalPoints = treePointsInfo.Points, | ||
RequestHash = input.RequestHash | ||
}); | ||
return new Empty(); | ||
} | ||
public override Empty SetTreePointsHashVerifyKey(StringValue input) | ||
{ | ||
AssertContractInitialized(); | ||
Assert(input != null && !string.IsNullOrEmpty(input.Value), "Invalid param key"); | ||
var key = State.TreePointsHashVerifyKey.Value; | ||
if (string.IsNullOrEmpty(key)) | ||
{ | ||
State.TreePointsHashVerifyKey.Value = input.Value; | ||
return new Empty(); | ||
} | ||
AssertSenderIsAdmin(); | ||
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.ToBase58(), 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"); | ||
Assert(treePointsInfo.Level < input.UpgradeLevel, $"You are already level{input.UpgradeLevel}"); | ||
|
||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.