-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
156 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package launchpad | ||
|
||
|
||
import ( | ||
"std" | ||
|
||
u256 "gno.land/p/gnoswap/uint256" | ||
"gno.land/p/demo/avl" | ||
) | ||
|
||
// DepositStore stores deposit-related metadata | ||
type DepositStore struct { | ||
// all deposits | ||
deposits *avl.Tree // key: deposit ID, value: *Deposit | ||
|
||
// deposits by project | ||
depositsByProject *avl.Tree // key: <project ID, tier>, value: []string(depositIds) | ||
|
||
// deposits by user | ||
depositsByUser *avl.Tree // key: user address, value: []string(depositIds) | ||
|
||
// deposits by user & project | ||
depositsByUserProject *avl.Tree // key: <user address, project ID>, value: []string(depositIds) | ||
} | ||
|
||
// DepositManager implements the deposit manager | ||
type DepositManager interface { | ||
// AddDeposit adds a deposit to the store | ||
AddDeposit(projectId string, user std.Address, amount uint64, tier string) (string, error) | ||
|
||
// GetDeposit returns the deposit by depositId | ||
GetDeposit(depositId string) (*Deposit, error) | ||
|
||
// GetUserDeposits retrieves all of the user's deposits | ||
GetUserDeposits(user std.Address) ([]*Deposit, error) | ||
|
||
// GetProjectDeposits retrieves all of the project's deposits | ||
GetProjectDeposits(projectId string) ([]*Deposit, error) | ||
|
||
// CollectDeposit collects the deposit | ||
CollectDeposit(depositId string, user std.Address) (uint64, error) | ||
|
||
// UpdateDepositReward updates the deposit's reward | ||
UpdateDepositReward(depositId string, reward *u256.Uint) error | ||
} |
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,29 @@ | ||
package launchpad | ||
|
||
import ( | ||
"std" | ||
u256 "gno.land/p/gnoswap/uint256" | ||
"gno.land/p/demo/avl" | ||
) | ||
|
||
// ProjectStore stores project-related metadata | ||
type ProjectStore struct { | ||
// all projects | ||
projects *avl.Tree // key: project ID, value: *Project | ||
|
||
// project information by tier | ||
projectsByTier *avl.Tree // key: tier, value: []string(projectIds) | ||
|
||
// project information by status | ||
projectsByStatus *avl.Tree // key: status, value: []string(projectIds) | ||
} | ||
|
||
type ProjectManager interface { | ||
CreateProject(name string, tokenPath string, recipient std.Address, params ProjectParams) (string, error) | ||
GetProject(projectId string) (*Project, error) | ||
UpdateProject(projectId string, params ProjectParams) error | ||
GetProjectTiers(projectId string) (map[string]*Tier, error) // TODO: change type | ||
IsProjectActive(projectId string) bool | ||
GetProjectsByTier(tier string) ([]*Project, error) | ||
GetProjectsByStatus(status ProjectStatus) ([]*Project, error) | ||
} |
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,82 @@ | ||
package launchpad | ||
|
||
import ( | ||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/ufmt" | ||
|
||
u256 "gno.land/p/gnoswap/uint256" | ||
) | ||
|
||
var ( | ||
rewardStore = NewRewardStore() // TODO: init? | ||
) | ||
|
||
// RewardStore stores reward-related metadata | ||
type RewardStore struct { | ||
accumulatedRewards *avl.Tree // key: project ID, value: *u256.Uint | ||
lastCalculatedHeight *avl.Tree // key: project ID, value: uint64 | ||
rewardSnapshots *avl.Tree // key: <project ID, height>, value: *RewardSnapshot | ||
} | ||
|
||
func NewRewardStore() *RewardStore { | ||
return &RewardStore{ | ||
accumulatedRewards: avl.NewTree(), | ||
lastCalculatedHeight: avl.NewTree(), | ||
rewardSnapshots: avl.NewTree(), | ||
} | ||
} | ||
|
||
type RewardSnapshot struct { | ||
Height uint64 | ||
AccumulatedRewards *u256.Uint | ||
TierRewards *avl.Tree // key: tier, value: *u256.Uint | ||
} | ||
|
||
func (rs *RewardStore) CalculateRewards(pid string, height uint64) (*u256.Uint, error) { | ||
lastHeight, exists := rs.lastCalculatedHeight.Get(pid) | ||
if !exists { | ||
lastHeight = uint64(0) | ||
} | ||
|
||
lstHeight := lastHeight.(uint64) | ||
if height <= lstHeight { | ||
// return cached value if height already calculated | ||
rwd, exists := rs.accumulatedRewards.Get(pid) | ||
if !exists { | ||
return u256.NewUint(0), nil | ||
} | ||
return rwd.(*u256.Uint), nil | ||
} | ||
|
||
project, exists := projects[pid] // TODO: avl later | ||
if !exists { | ||
return nil, ufmt.Errorf("project(%s) not found", pid) | ||
} | ||
|
||
// not started yet | ||
if project.startHeight > height { | ||
return u256.Zero(), nil | ||
} | ||
|
||
// calculate rewards | ||
totalReward := calculateProjectReward(&project, lstHeight, height) | ||
|
||
// update accumulated rewards | ||
rs.accumulatedRewards.Set(pid, totalReward) | ||
rs.lastCalculatedHeight.Set(pid, height) | ||
|
||
return totalReward, nil | ||
} | ||
|
||
// func calculateProjectReward(proj *Project, startHeight, endHeight uint64) *u256.Uint { | ||
// totalReward := u256.Zero() | ||
|
||
// tiers := []struct{ | ||
// tier *Tier | ||
// ratio uint64 | ||
// }{ | ||
// {&proj.tier30, proj.tier30Ratio}, | ||
// {&proj.tier90, proj.tier90Ratio}, | ||
// {&proj.tier180, proj.tier180Ratio}, | ||
// } | ||
// } |