gpu-compute-chain/x/colinear-core/math/vesting_test.go

116 lines
2.0 KiB
Go
Raw Normal View History

2022-09-01 15:35:10 -07:00
package math
import (
"fmt"
"math/big"
"testing"
"time"
)
func TestLinearVesting(t *testing.T) {
// Total: 1000 tokens
// Time: halfway through
// Claimed: 0
// Expected: 500
if res, err := CalcAmountVestableLinear(
big.NewInt(1000),
big.NewInt(1000),
time.Now(),
time.Unix(time.Now().Unix()-1000, 0),
time.Unix(time.Now().Unix()+1000, 0),
); err != nil {
panic(err)
} else {
expectEq(res, 500)
}
// Total: 1000 tokens
// Time: beginning through
// Claimed: 0
// Expected: 0
if res, err := CalcAmountVestableLinear(
big.NewInt(1000),
big.NewInt(1000),
time.Now(),
time.Now(),
time.Unix(time.Now().Unix()+1000, 0),
); err != nil {
panic(err)
} else {
expectEq(res, 0)
}
// Total: 1000 tokens
// Time: end
// Claimed: 0
// Expected: 1000
if res, err := CalcAmountVestableLinear(
big.NewInt(1000),
big.NewInt(1000),
time.Now(),
time.Unix(time.Now().Unix()-1000, 0),
time.Now(),
); err != nil {
panic(err)
} else {
expectEq(res, 1000)
}
// Total: 1000 tokens
// Time: end
// Claimed: 1000
// Expected: 1000
if res, err := CalcAmountVestableLinear(
big.NewInt(1000),
big.NewInt(0),
time.Now(),
time.Unix(time.Now().Unix()-1000, 0),
time.Now(),
); err != nil {
panic(err)
} else {
expectEq(res, 0)
}
// Total: 1000 tokens
// Time: 33%
// Claimed: 250 (25%)
// Expected: 333-250 = 83
if res, err := CalcAmountVestableLinear(
big.NewInt(1000),
big.NewInt(750),
time.Now(),
time.Unix(time.Now().Unix()-1000, 0),
time.Unix(time.Now().Unix()+2000, 0),
); err != nil {
panic(err)
} else {
expectEq(res, 83)
}
// Total: 1000 tokens
// Time: 66%
// Claimed: 250 (25%)
// Expected: 666-250 = 416
if res, err := CalcAmountVestableLinear(
big.NewInt(1000),
big.NewInt(750),
time.Now(),
time.Unix(time.Now().Unix()-2000, 0),
time.Unix(time.Now().Unix()+1000, 0),
); err != nil {
panic(err)
} else {
expectEq(res, 416)
}
}
func expectEq(a *big.Int, b int) {
a2 := int(a.Uint64())
if a2 != b {
panic(fmt.Sprintf("expected %d, got %d", b, a2))
}
}