From 119154c5ae81ebec56081d1aef7dc2ab8773ae2d Mon Sep 17 00:00:00 2001 From: Norman Date: Sat, 4 Jan 2025 11:57:22 +0100 Subject: [PATCH] chore: refacto Signed-off-by: Norman --- examples/gno.land/r/demo/payrolls/models.gno | 17 ++++----- examples/gno.land/r/demo/payrolls/payroll.gno | 35 ++++++++++++------- examples/gno.land/r/demo/payrolls/render.gno | 4 +-- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/examples/gno.land/r/demo/payrolls/models.gno b/examples/gno.land/r/demo/payrolls/models.gno index 0adfef07842..9e7276da3f0 100644 --- a/examples/gno.land/r/demo/payrolls/models.gno +++ b/examples/gno.land/r/demo/payrolls/models.gno @@ -8,22 +8,17 @@ import ( "gno.land/p/demo/seqid" ) +// XXX: should pass a read-only payroll object to those funcs + // DistribFn is a distribution function used to compute and amount of released coins at a particular point in time. // // It must return payrolls [Coins] -type DistribFn func(elapsed time.Duration) std.Coins +type DistribFn func(worked time.Duration) std.Coins // BreakupFn is a function used to compute payroll termination bonuses. // // It must return payrolls [Coins] -type BreakupFn func(elapsed time.Duration, pauseDuration time.Duration, source BreakupSource) std.Coins - -type BreakupSource uint - -const ( - BreakupSourceCreator BreakupSource = iota - BreakupSourceBeneficiary -) +type BreakupFn func(elapsed time.Duration, pauseDuration time.Duration, source CallSource) std.Coins // DistribStep returns a distribution function that will release coins every `per` duration. func DistribStep(coins std.Coins, per time.Duration) DistribFn { @@ -87,8 +82,8 @@ func CreateCDI(namespace string, label string, beneficiary std.Address, amountPe // according to a simplified "Rupture Conventionelle" (conventional breakup) model based on the French CDI employment contract func BreakupCDI(coinsPerMonth std.Coins) BreakupFn { const hoursPerYear = float64(8_670) - return func(elapsed time.Duration, pauseDuration time.Duration, source BreakupSource) std.Coins { - if source != BreakupSourceCreator { + return func(elapsed time.Duration, pauseDuration time.Duration, source CallSource) std.Coins { + if source != CallSourceCreator { return nil } diff --git a/examples/gno.land/r/demo/payrolls/payroll.gno b/examples/gno.land/r/demo/payrolls/payroll.gno index 39cd9daa773..3d66ec3f176 100644 --- a/examples/gno.land/r/demo/payrolls/payroll.gno +++ b/examples/gno.land/r/demo/payrolls/payroll.gno @@ -26,6 +26,13 @@ type payroll struct { stoppedAt time.Time } +type CallSource uint + +const ( + CallSourceCreator CallSource = iota + CallSourceBeneficiary +) + func (p *payroll) freeAt(when time.Time, estimate bool) std.Coins { if p.stopped && p.stoppedAt.Before(when) { when = p.stoppedAt @@ -87,31 +94,33 @@ func (p *payroll) resumeUnsafe() { func (p *payroll) stop(source std.Address) { p.assertNotStopped() + breakupSource := p.assertSource(source) + if p.paused { p.resumeUnsafe() } - - p.breakupCoins = p.getBreakupCoins(source) + p.breakupCoins = p.getBreakupCoins(breakupSource) p.stopped = true p.stoppedAt = time.Now() } -func (p *payroll) getBreakupCoins(source std.Address) std.Coins { - var breakupSource BreakupSource - switch source { +func (p *payroll) getBreakupCoins(source CallSource) std.Coins { + if p.breakup == nil { + return nil + } + + return p.breakup(time.Now().Sub(p.createdAt), p.pauseDuration, source) +} + +func (p *payroll) assertSource(addr std.Address) CallSource { + switch addr { case p.beneficiary: - breakupSource = BreakupSourceBeneficiary + return CallSourceBeneficiary case p.creatorAddr: - breakupSource = BreakupSourceCreator + return CallSourceCreator default: panic(errors.New("unknown source")) } - - if p.breakup == nil { - return nil - } - - return p.breakup(time.Now().Sub(p.createdAt), p.pauseDuration, breakupSource) } func (p *payroll) assertNotStopped() { diff --git a/examples/gno.land/r/demo/payrolls/render.gno b/examples/gno.land/r/demo/payrolls/render.gno index 55ecd954e73..08163015f04 100644 --- a/examples/gno.land/r/demo/payrolls/render.gno +++ b/examples/gno.land/r/demo/payrolls/render.gno @@ -343,8 +343,8 @@ func renderPayroll(p *payroll, pov string) string { if pov == "details" { allCoins = addCoins(allCoins, beneficiaryBreakupCoins) allCoins = addCoins(allCoins, creatorBreakupCoins) - beneficiaryBreakupCoins = p.getBreakupCoins(p.beneficiary) - creatorBreakupCoins = p.getBreakupCoins(p.creatorAddr) + beneficiaryBreakupCoins = p.getBreakupCoins(CallSourceBeneficiary) + creatorBreakupCoins = p.getBreakupCoins(CallSourceCreator) } for _, coin := range allCoins { sb.WriteString(ufmt.Sprintf(" - %s:\n", renderCoinDenom(coin.Denom)))