-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GSW-1845 refactor: gns, calculate amount to mint
- Loading branch information
Showing
8 changed files
with
302 additions
and
230 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,60 @@ | ||
package gns | ||
|
||
// calculateAmountToMint calculates the amount of gns to mint | ||
// It calculates the amount of gns to mint for each halving year for block range. | ||
// It also handles the left emission amount if the current block range includes halving year end block. | ||
func calculateAmountToMint(fromHeight, toHeight int64) uint64 { | ||
fromYear := GetHalvingYearByHeight(fromHeight) | ||
toYear := GetHalvingYearByHeight(toHeight) | ||
|
||
amountToMint := uint64(0) | ||
|
||
for i := fromYear; i <= toYear; i++ { | ||
yearEndHeight := halvingYearBlock[i] | ||
mintUntilHeight := yearEndHeight | ||
|
||
if toHeight < mintUntilHeight { | ||
mintUntilHeight = toHeight | ||
} | ||
|
||
// how many blocks to calculate | ||
numBlock := uint64(mintUntilHeight-fromHeight) + 1 | ||
|
||
// amount of gns to mint for each block for current year | ||
singleBlockAmount := GetAmountByHeight(yearEndHeight) | ||
|
||
// total amount of gns to mint for current year | ||
amountToMint += singleBlockAmount * numBlock | ||
|
||
// if last block of halving year, handle left emission amount | ||
if isLastBlockOfHalvingYear(mintUntilHeight) { | ||
amountToMint += handleLeftEmissionAmount(i, amountToMint) | ||
} | ||
|
||
// update halving year mint amount | ||
halvingYearMintAmount[i] += amountToMint | ||
|
||
// update fromHeight for next year (if necessary) | ||
fromHeight = mintUntilHeight + 1 | ||
} | ||
|
||
return amountToMint | ||
} | ||
|
||
// isLastBlockOfHalvingYear returns true if the current block is the last block of a halving year. | ||
func isLastBlockOfHalvingYear(height int64) bool { | ||
year := GetHalvingYearByHeight(height) | ||
lastBlock := halvingYearBlock[year] | ||
|
||
return height == lastBlock | ||
} | ||
|
||
// handleLeftEmissionAmount handles the left emission amount for a halving year. | ||
// It calculates the left emission amount by subtracting the halving year mint amount from the halving year amount. | ||
func handleLeftEmissionAmount(year int64, amount uint64) uint64 { | ||
return halvingYearAmount[year] - halvingYearMintAmount[year] - amount | ||
} | ||
|
||
func setLeftEmissionAmount(amount uint64) { | ||
leftEmissionAmount = amount | ||
} |
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,97 @@ | ||
package gns | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"gno.land/p/demo/uassert" | ||
) | ||
|
||
func TestCalculateAmountToMint(t *testing.T) { | ||
// back up previous data | ||
prevHalvingYearAmount := halvingYearMintAmount | ||
|
||
t.Run("1 block for same year 01", func(t *testing.T) { | ||
amount := calculateAmountToMint(10, 10) | ||
uassert.Equal(t, amountPerBlockPerHalvingYear[1], amount) | ||
}) | ||
|
||
t.Run("entire block for year 01 + 1 block for year 02", func(t *testing.T) { | ||
amount := calculateAmountToMint(halvingYearBlock[1], halvingYearBlock[1]+2) | ||
uassert.Equal(t, halvingYearAmount[1]+amountPerBlockPerHalvingYear[2], amount) | ||
}) | ||
|
||
// restore previous data | ||
halvingYearMintAmount = prevHalvingYearAmount | ||
} | ||
|
||
func TestIsLastBlockOfHalvingYear(t *testing.T) { | ||
tests := make([]struct { | ||
name string | ||
height int64 | ||
want bool | ||
}, 0, 24) | ||
|
||
for i := int64(1); i <= 12; i++ { | ||
tests = append(tests, struct { | ||
name string | ||
height int64 | ||
want bool | ||
}{ | ||
name: fmt.Sprintf("last block of halving year %d", i), | ||
height: halvingYearBlock[i], | ||
want: true, | ||
}) | ||
|
||
tests = append(tests, struct { | ||
name string | ||
height int64 | ||
want bool | ||
}{ | ||
name: fmt.Sprintf("not last block of halving year %d", i), | ||
height: halvingYearBlock[i] - 1, | ||
want: false, | ||
}) | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
uassert.Equal(t, tt.want, isLastBlockOfHalvingYear(tt.height)) | ||
}) | ||
} | ||
} | ||
|
||
func TestHandleLeftEmissionAmount(t *testing.T) { | ||
tests := make([]struct { | ||
name string | ||
year int64 | ||
amount uint64 | ||
want uint64 | ||
}, 0, 24) | ||
|
||
for i := int64(1); i <= 12; i++ { | ||
tests = append(tests, struct { | ||
name string | ||
year int64 | ||
amount uint64 | ||
want uint64 | ||
}{ | ||
name: fmt.Sprintf("handle left emission amount for year %d, non minted", i), | ||
year: i, | ||
amount: 0, | ||
want: halvingYearAmount[i], | ||
}) | ||
|
||
tests = append(tests, struct { | ||
name string | ||
year int64 | ||
amount uint64 | ||
want uint64 | ||
}{ | ||
name: fmt.Sprintf("handle left emission amount for year %d, minted", i), | ||
year: i, | ||
amount: uint64(123456), | ||
want: halvingYearAmount[i] - uint64(123456), | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1 @@ | ||
module gno.land/r/gnoswap/v1/gns | ||
|
||
require ( | ||
gno.land/p/demo/grc/grc20 v0.0.0-latest | ||
gno.land/p/demo/json v0.0.0-latest | ||
gno.land/p/demo/ownable v0.0.0-latest | ||
gno.land/p/demo/ufmt v0.0.0-latest | ||
gno.land/p/demo/users v0.0.0-latest | ||
gno.land/r/demo/users v0.0.0-latest | ||
gno.land/r/demo/grc20reg v0.0.0-latest | ||
gno.land/r/gnoswap/v1/common v0.0.0-latest | ||
gno.land/r/gnoswap/v1/consts v0.0.0-latest | ||
) |
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
Oops, something went wrong.