Skip to content

Commit

Permalink
test: execute
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Dec 24, 2024
1 parent a1582a2 commit 9b8dd7a
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 32 deletions.
263 changes: 234 additions & 29 deletions gov/governance/execute2.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ import (
"gno.land/p/demo/ufmt"
"gno.land/r/gnoswap/v1/common"
"gno.land/r/gnoswap/v1/consts"

"gno.land/r/gnoswap/v1/gns"

cp "gno.land/r/gnoswap/v1/community_pool"
en "gno.land/r/gnoswap/v1/emission"
pl "gno.land/r/gnoswap/v1/pool"
pf "gno.land/r/gnoswap/v1/protocol_fee"
rr "gno.land/r/gnoswap/v1/router"
sr "gno.land/r/gnoswap/v1/staker"
)

var (
Expand Down Expand Up @@ -61,6 +67,7 @@ func makeHandlerKey(pkgPath, function string) string {
}

///////////////////// EXECUTION /////////////////////
// region: Execute

type ExecutionContext struct {
ProposalId uint64
Expand All @@ -72,19 +79,44 @@ type ExecutionContext struct {
}

func Execute2(proposalId uint64) error {
// ctx, err := prepareExecution(proposalId)
// if err != nil {
// panic(err)
// }
ctx, err := prepareExecution(proposalId)
if err != nil {
panic(err)
}

if err := validateVotes(ctx.Proposal); err != nil {
panic(err)
}

if err := validateCommunityPoolToken(ctx.Proposal); err != nil {
panic(err)
}

registry := createParameterHandlers()
if err := executeProposal(ctx, registry); err != nil {
panic(err)
}

updateProposalState(ctx)

prevAddr, prevPkgPath := getPrev()
std.Emit(
"Execute",
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"proposalId", strconv.Itoa(int(proposalId)),
)

return nil
}

func executeProposal(ctx *ExecutionContext, registry *ParameterRegistry) error {
// TODO: change hard-code string value to enum
if ctx.Proposal.ProposalType == ParameterChange {
switch ctx.Proposal.ProposalType {
case ParameterChange:
return executeParameterChange2(ctx.Proposal.Execution.Msgs, registry)
default:
return nil
}
return nil
}

func executeParameterChange2(msgs []string, registry *ParameterRegistry) error {
Expand Down Expand Up @@ -119,47 +151,173 @@ func parseMessage(msg string) (pkgPath string, function string, params []string,
func createParameterHandlers() *ParameterRegistry {
registry := NewParameterRegistry()

// Community pool
// region: Common path
registry.Register(consts.COMMON_PATH, "SetHalt", func(params []string) error {
if err := hasDesiredParams(params, 1); err != nil {
return err
}
common.SetHalt(parseBool(params[0])) // halt
return nil
})

// region: Community pool
registry.Register(consts.COMMUNITY_POOL_PATH, "TransferToken", func(params []string) error {
if len(params) != 3 {
return ufmt.Errorf("invalid parameters for TransferToken. expected 3 but got %d", len(params))
if err := hasDesiredParams(params, 3); err != nil {
return err
}
cp.TransferToken(params[0], std.Address(params[1]), parseUint64(params[2]))
cp.TransferToken(
params[0], // pkgPath
std.Address(params[1]), // to
parseUint64(params[2]), // amount
)
return nil
})

// Emission
// region: Emission
registry.Register(consts.EMISSION_PATH, "ChangeDistributionPct", func(params []string) error {
if len(params) != 8 {
return ufmt.Errorf("invalid parameters for ChangeDistributionPct. expected 8 but got %d", len(params))
if err := hasDesiredParams(params, 8); err != nil {
return err
}
en.ChangeDistributionPct(
parseInt(params[0]),
parseUint64(params[1]),
parseInt(params[2]),
parseUint64(params[3]),
parseInt(params[4]),
parseUint64(params[5]),
parseInt(params[6]),
parseUint64(params[7]),
parseInt(params[0]), // target01
parseUint64(params[1]), // pct01
parseInt(params[2]), // target02
parseUint64(params[3]), // pct02
parseInt(params[4]), // target03
parseUint64(params[5]), // pct03
parseInt(params[6]), // target04
parseUint64(params[7]), // pct04
)
return nil
})

// region: GNS Path
registry.Register(consts.GNS_PATH, "SetAvgBlockTimeInMs", func(params []string) error {
if err := hasDesiredParams(params, 1); err != nil {
return err
}
gns.SetAvgBlockTimeInMs(int64(parseInt(params[0]))) // ms
return nil
})

// region: Governance Path
registry.Register(consts.GOV_GOVERNANCE_PATH, "Reconfigure", func(params []string) error {
if err := hasDesiredParams(params, 7); err != nil {
return err
}
reconfigure(
parseUint64(params[0]), // votingStartDelay
parseUint64(params[1]), // votingPeriod
parseUint64(params[2]), // votingWeightSmoothingDuration
parseUint64(params[3]), // quorum
parseUint64(params[4]), // proposalCreationhold
parseUint64(params[5]), // executionDelay
parseUint64(params[6]), // executionWindow
)
return nil
})

// Pool
// region: Pool Path
registry.Register(consts.POOL_PATH, "SetFeeProtocol", func(params []string) error {
if len(params) != 2 {
return ufmt.Errorf("invalid parameters for SetFeeProtocol. expected 2 but got %d", len(params))
if err := hasDesiredParams(params, 2); err != nil {
return err
}
pl.SetFeeProtocol(uint8(parseUint64(params[0])), uint8(parseUint64(params[1])))
pl.SetFeeProtocol(
uint8(parseUint64(params[0])), // feeProtocol0
uint8(parseUint64(params[1])), // feeProtocol1
)
return nil
})

registry.Register(consts.POOL_PATH, "SetPoolCreationFee", func(params []string) error {
if len(params) != 1 {
return ufmt.Errorf("invalid parameters for SetPoolCreationFee. expected 1 but got %d", len(params))
if err := hasDesiredParams(params, 1); err != nil {
return err
}
pl.SetPoolCreationFee(parseUint64(params[0])) // fee
return nil
})

registry.Register(consts.POOL_PATH, "SetWithdrawalFee", func(params []string) error {
if err := hasDesiredParams(params, 1); err != nil {
return err
}
pl.SetWithdrawalFee(parseUint64(params[0])) // fee
return nil
})

// region: Protocol fee
registry.Register(consts.PROTOCOL_FEE_PATH, "SetDevOpsPct", func(params []string) error {
if err := hasDesiredParams(params, 1); err != nil {
return err
}
pf.SetDevOpsPct(parseUint64(params[0])) // pct
return nil
})

// region: Router
registry.Register(consts.ROUTER_PATH, "SetSwapFee", func(params []string) error {
if err := hasDesiredParams(params, 1); err != nil {
return err
}
rr.SetSwapFee(parseUint64(params[0])) // fee
return nil
})

// region: Staker
registry.Register(consts.STAKER_PATH, "SetDepositGnsAmount", func(params []string) error {
if err := hasDesiredParams(params, 1); err != nil {
return err
}
sr.SetDepositGnsAmount(parseUint64(params[0])) // amount
return nil
})

registry.Register(consts.STAKER_PATH, "SetPoolTier", func(params []string) error {
if err := hasDesiredParams(params, 2); err != nil {
return err
}
sr.SetPoolTier(
params[0], // pool
parseUint64(params[1]), // tier
)
return nil
})

registry.Register(consts.STAKER_PATH, "ChangePoolTier", func(params []string) error {
if err := hasDesiredParams(params, 2); err != nil {
return err
}
sr.ChangePoolTier(
params[0], // pool
parseUint64(params[1]), // tier
)
return nil
})

registry.Register(consts.STAKER_PATH, "RemovePoolTier", func(params []string) error {
if err := hasDesiredParams(params, 1); err != nil {
return err
}
sr.RemovePoolTier(params[0]) // pool
return nil
})

registry.Register(consts.STAKER_PATH, "SetUnstakingFee", func(params []string) error {
if err := hasDesiredParams(params, 1); err != nil {
return err
}
sr.SetUnstakingFee(parseUint64(params[0]))
return nil
})

registry.Register(consts.STAKER_PATH, "SetWarmUp", func(params []string) error {
if err := hasDesiredParams(params, 2); err != nil {
return err
}
pl.SetPoolCreationFee(parseUint64(params[0]))
sr.SetWarmUp(
int64(parseInt(params[0])), // percent
int64(parseInt(params[1])), // block
)
return nil
})

Expand Down Expand Up @@ -287,3 +445,50 @@ func validateExecutionWindow(now, windowStart, windowEnd uint64) error {

return nil
}

func validateVotes(pp *ProposalInfo) error {
yea := pp.Yea.Unit64()
nea := pp.Nay.Unit64()
quorum := pp.QuorumAmount

if yea < quorum {
return ufmt.Errorf("quorum not met (yes(%d) < quorum(%d))", yea, quorum)
}

if yea < nea {
return ufmt.Errorf("no majority (yes(%d) < no(%d))", yea, nea)
}

return nil
}

func updateProposalState(ctx *ExecutionContext) {
ctx.Proposal.ExecutionState.Executed = true
ctx.Proposal.ExecutionState.ExecutedAt = ctx.Now
ctx.Proposal.ExecutionState.Upcoming = false
ctx.Proposal.ExecutionState.Active = false
proposals[ctx.ProposalId] = *ctx.Proposal
}

func validateCommunityPoolToken(pp *ProposalInfo) error {
if pp.ProposalType != CommunityPoolSpend {
return nil
}

registered := cp.GetRegisteredTokens()
commPath := pp.CommunityPoolSpend.TokenPath
if !contains(registered, commPath) {
return ufmt.Errorf("token(%s) is not registered to community pool",
commPath,
)
}

return nil
}

func hasDesiredParams(params []string, expected int) error {
if len(params) != expected {
return ufmt.Errorf("invalid parameters for %s. expected %d but got %d", params, expected, len(params))
}
return nil
}
Loading

0 comments on commit 9b8dd7a

Please sign in to comment.