gpu-compute-chain/x/colinearcore/math/vesting_test.go

132 lines
2.4 KiB
Go

package colinearmath
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: 0
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)
}
// Total: 1000 tokens
// Time: 20 sec in of a 3700sec lease
// Claimed: 1000
// Expected: 0
if res, err := CalcAmountVestableLinear(
big.NewInt(1000),
big.NewInt(1000),
time.Now(),
time.Unix(time.Now().Unix()-20, 0),
time.Unix(time.Now().Unix()+3680, 0),
); err != nil {
panic(err)
} else {
expectEq(res, 5)
}
}
// Compare a bigint output to its expected int value
func expectEq(a *big.Int, b int) {
a2 := int(a.Uint64())
if a2 != b {
panic(fmt.Sprintf("expected %d, got %d", b, a2))
}
}