Skip to content

Commit

Permalink
add more iter
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Dec 13, 2024
1 parent 16c1305 commit cdf960a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 71 deletions.
14 changes: 7 additions & 7 deletions staker/_GET_qa_internal_emission.gno
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func GetPrintInfo() string {
emissionDebug.GnsProtocolFee = gns.BalanceOf(a2u(consts.PROTOCOL_FEE_ADDR))
emissionDebug.GnsADMIN = gns.BalanceOf(a2u(consts.ADMIN))

for poolPath, internal := range poolTiers {
tier := internal.tier
poolTiers.Iter(func(poolPath string, internalTier InternalTier) {
tier := internalTier.tier
pool := ApiEmissionDebugPool{}
pool.PoolPath = poolPath
pool.Tier = tier
Expand Down Expand Up @@ -158,7 +158,7 @@ func GetPrintInfo() string {
}

emissionDebug.Pool = append(emissionDebug.Pool, pool)
}
})

node := json.ObjectNode("", map[string]*json.Node{
"height": json.NumberNode("", float64(emissionDebug.Height)),
Expand All @@ -185,10 +185,10 @@ func GetPrintInfo() string {
func makePoolsNode(emissionPool []ApiEmissionDebugPool) []*json.Node {
pools := make([]*json.Node, 0)

for poolPath, internal := range poolTiers {
poolTiers.Iter(func(poolPath string, internalTier InternalTier) {
numTier1, numTier2, numTier3 := getNumPoolTiers()
numPoolSameTier := uint64(0)
tier := internal.tier
tier := internalTier.tier
if tier == 1 {
numPoolSameTier = numTier1
} else if tier == 2 {
Expand All @@ -199,13 +199,13 @@ func makePoolsNode(emissionPool []ApiEmissionDebugPool) []*json.Node {

pools = append(pools, json.ObjectNode("", map[string]*json.Node{
"poolPath": json.StringNode("poolPath", poolPath),
"startTimestamp": json.NumberNode("startTimestamp", float64(internal.startTimestamp)),
"startTimestamp": json.NumberNode("startTimestamp", float64(internalTier.startTimestamp)),
"tier": json.NumberNode("tier", float64(tier)),
"numPoolSameTier": json.NumberNode("numPoolSameTier", float64(numPoolSameTier)),
"poolReward": json.NumberNode("poolReward", float64(poolGns[poolPath])),
"position": json.ArrayNode("", makePositionsNode(poolPath)),
}))
}
})

return pools
}
Expand Down
30 changes: 15 additions & 15 deletions staker/_RPC_api_incentive.gno
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,14 @@ func ApiGetInternalIncentives() string {

apiInternalIncentives := []ApiInternalIncentive{}

for poolPath, internal := range poolTiers {
poolTiers.Iter(func(poolPath string, internalTier InternalTier) {
apiInternalIncentives = append(apiInternalIncentives, ApiInternalIncentive{
PoolPath: poolPath,
Tier: internal.tier,
StartTimestamp: internal.startTimestamp,
Tier: internalTier.tier,
StartTimestamp: internalTier.startTimestamp,
RewardPerBlock: calculateInternalRewardPerBlockByPoolPath(poolPath),
})
}
})

// STAT NODE
_stat := json.ObjectNode("", map[string]*json.Node{
Expand Down Expand Up @@ -551,18 +551,18 @@ func ApiGetInternalIncentivesByPoolPath(targetPoolPath string) string {

apiInternalIncentives := []ApiInternalIncentive{}

for poolPath, internal := range poolTiers {
poolTiers.Iter(func(poolPath string, internalTier InternalTier) {
if poolPath != targetPoolPath {
continue
return
}

apiInternalIncentives = append(apiInternalIncentives, ApiInternalIncentive{
PoolPath: poolPath,
Tier: internal.tier,
StartTimestamp: internal.startTimestamp,
Tier: internalTier.tier,
StartTimestamp: internalTier.startTimestamp,
RewardPerBlock: calculateInternalRewardPerBlockByPoolPath(poolPath),
})
}
})

// STAT NODE
_stat := json.ObjectNode("", map[string]*json.Node{
Expand Down Expand Up @@ -608,18 +608,18 @@ func ApiGetInternalIncentivesByTiers(targetTier uint64) string {

apiInternalIncentives := []ApiInternalIncentive{}

for poolPath, internal := range poolTiers {
if internal.tier != targetTier {
continue
poolTiers.Iter(func(poolPath string, internalTier InternalTier) {
if internalTier.tier != targetTier {
return
}

apiInternalIncentives = append(apiInternalIncentives, ApiInternalIncentive{
PoolPath: poolPath,
Tier: internal.tier,
StartTimestamp: internal.startTimestamp,
Tier: internalTier.tier,
StartTimestamp: internalTier.startTimestamp,
RewardPerBlock: calculateInternalRewardPerBlockByPoolPath(poolPath),
})
}
})

// STAT NODE
_stat := json.ObjectNode("", map[string]*json.Node{
Expand Down
11 changes: 5 additions & 6 deletions staker/calculate_pool_position_reward.gno
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ func CalcPoolPosition() {
}

// Repeat for the number of internal emission target pools
for poolPath, internal := range poolTiers {
tier := internal.tier
poolTiers.Iter(func(poolPath string, internalTier InternalTier) {
tier := internalTier.tier

tierAmount := uint64(0)
if tier == 1 {
Expand Down Expand Up @@ -356,7 +356,7 @@ func CalcPoolPosition() {
lastCalculatedBalance = _stakerGnsBalance - totalExternalGns

}
}
})

for tokenId, deposit := range deposits {
poolPath := deposit.targetPoolPath
Expand Down Expand Up @@ -389,17 +389,16 @@ func CalcPoolPosition() {
lastCalculatedBalance = gnsBalance(consts.STAKER_ADDR) - totalExternalGns // latest balance

// Repeat for the number of internal emission target pools (clean up)
for poolPath, _ := range poolTiers {
poolTiers.Iter(func(poolPath string, _ InternalTier) {
amount := poolLastTmpGns[poolPath]
if amount > 0 {
if poolCurrentBlockGns[poolPath] >= amount {
poolLastTmpGns[poolPath] = poolCurrentBlockGns[poolPath] - amount
} else {
poolCurrentBlockGns[poolPath] = 0
}
} else {
}
}
})

// clear(poolCurrentBlockGns) // gno doesn't support `clear` keyword yet
poolCurrentBlockGns = make(map[string]uint64)
Expand Down
14 changes: 7 additions & 7 deletions staker/manage_pool_tiers.gno
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type ApiPoolWithEmissionGnsAmount struct {
// GetPoolsWithTier returns a list of string that consists of pool path and tier
func GetPoolsWithTier() []string {
var pools []string
for pool, tier := range poolTiers {
pools = append(pools, ufmt.Sprintf("%s_%d", pool, tier.tier))
}
poolTiers.Iter(func(poolPath string, internalTier InternalTier) {
pools = append(pools, ufmt.Sprintf("%s_%d", poolPath, internalTier.tier))
})
return pools
}

Expand All @@ -45,8 +45,8 @@ func GetPoolsWithEmissionGnsAmount() string {

tier1Num, tier2Num, tier3Num := getNumPoolTiers()

for poolPath, internal := range poolTiers {
tier := internal.tier
poolTiers.Iter(func(poolPath string, internalTier InternalTier) {
tier := internalTier.tier
tierAmount := uint64(0)

if tier == 1 {
Expand All @@ -61,10 +61,10 @@ func GetPoolsWithEmissionGnsAmount() string {
internalIncentive.PoolPath = poolPath
internalIncentive.Tier = tier
internalIncentive.Amount = tierAmount
internalIncentive.StartTimestamp = internal.startTimestamp
internalIncentive.StartTimestamp = internalTier.startTimestamp

internals = append(internals, internalIncentive)
}
})

// STAT NODE
_stat := json.ObjectNode("", map[string]*json.Node{
Expand Down
32 changes: 16 additions & 16 deletions staker/staker.gno
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ func StakeToken(tokenId uint64) (string, string, string) {
CalcPoolPosition()
}

// check whether tokenId already staked or not
deposit, exist := deposits.Get(tokenId)
if exist {
panic(addDetailToError(
errAlreadyStaked,
ufmt.Sprintf("staker.gno__StakeToken() || tokenId(%d) already staked", tokenId),
))
}

owner := gnft.OwnerOf(tid(tokenId))
caller := std.PrevRealm().Addr()
if err := requireTokenOwnership(owner, caller); err != nil {
Expand All @@ -66,22 +75,13 @@ func StakeToken(tokenId uint64) (string, string, string) {
panic(err)
}

// check whether tokenId already staked or not
deposit, exist := deposits.Get(tokenId)
if exist {
panic(addDetailToError(
errAlreadyStaked,
ufmt.Sprintf("staker.gno__StakeToken() || tokenId(%d) already staked", tokenId),
))
}

// TODO: extract as function
// staked status
deposit.SetOwner(std.PrevRealm().Addr())
deposit.SetNumberOfStakes(deposit.NumberOfStakes() + 1)
deposit.SetStakeTimestamp(time.Now().Unix())
deposit.SetStakeHeight(std.GetHeight())
deposit.SetTargetPoolPath(poolPath)
deposit = newDeposit(
owner,
deposit.NumberOfStakes() + 1,
time.Now().Unix(),
std.GetHeight(),
poolPath,
)
deposits.Set(tokenId, deposit)

// if caller is owner, transfer NFT ownership to staker contract
Expand Down
6 changes: 6 additions & 0 deletions staker/staker_type.gno
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func (p PoolTiers) Remove(poolPath string) {
delete(p, poolPath)
}

func (p PoolTiers) Iter(fn func(poolPath string, internalTier InternalTier)) {
for poolPath, internalTier := range p {
fn(poolPath, internalTier)
}
}

// poolIncentives maps pool paths to their associated incentive IDs
type PoolIncentives map[string][]string

Expand Down
36 changes: 16 additions & 20 deletions staker/type.gno
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ type Deposit struct {
targetPoolPath string // staked position's pool path
}

func newDeposit(
owner std.Address,
numberOfStakes uint64,
stakeTimestamp int64,
stakeHeight int64,
targetPoolPath string,
) Deposit {
return Deposit{
owner: owner,
numberOfStakes: numberOfStakes,
stakeTimestamp: stakeTimestamp,
stakeHeight: stakeHeight,
targetPoolPath: targetPoolPath,
}
}

func (d Deposit) NumberOfStakes() uint64 {
return d.numberOfStakes
}
Expand All @@ -98,23 +114,3 @@ func (d Deposit) StakeTimestamp() int64 {
func (d Deposit) TargetPoolPath() string {
return d.targetPoolPath
}

func (d Deposit) SetOwner(owner std.Address) {
d.owner = owner
}

func (d Deposit) SetNumberOfStakes(numberOfStakes uint64) {
d.numberOfStakes = numberOfStakes
}

func (d Deposit) SetStakeTimestamp(stakeTimestamp int64) {
d.stakeTimestamp = stakeTimestamp
}

func (d Deposit) SetStakeHeight(stakeHeight int64) {
d.stakeHeight = stakeHeight
}

func (d Deposit) SetTargetPoolPath(targetPoolPath string) {
d.targetPoolPath = targetPoolPath
}

0 comments on commit cdf960a

Please sign in to comment.