Skip to content

Commit

Permalink
chore: refacto
Browse files Browse the repository at this point in the history
Signed-off-by: Norman <[email protected]>
  • Loading branch information
n0izn0iz committed Jan 4, 2025
1 parent b611fe5 commit 119154c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
17 changes: 6 additions & 11 deletions examples/gno.land/r/demo/payrolls/models.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
35 changes: 22 additions & 13 deletions examples/gno.land/r/demo/payrolls/payroll.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions examples/gno.land/r/demo/payrolls/render.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit 119154c

Please sign in to comment.