mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-26 23:46:08 +00:00
Just some name changes, put in a stand in emission amount, and started copying the algo from Karlsen. Not release worthy yet. Therefore Dev branch exists now. Also, for now this is for research purposes only. I got no clue what to build on top of Kaspa yet. Help would be appreciated for ideas and implementations.
112 lines
2.6 KiB
Go
112 lines
2.6 KiB
Go
package util_test
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/zoomy-network/zoomyd/util/difficulty"
|
|
"math"
|
|
"math/big"
|
|
|
|
"github.com/zoomy-network/zoomyd/util"
|
|
)
|
|
|
|
func ExampleAmount() {
|
|
|
|
a := util.Amount(0)
|
|
fmt.Println("Zero Sompi:", a)
|
|
|
|
a = util.Amount(1e8)
|
|
fmt.Println("100,000,000 Sompi:", a)
|
|
|
|
a = util.Amount(1e5)
|
|
fmt.Println("100,000 Sompi:", a)
|
|
// Output:
|
|
// Zero Sompi: 0 KAS
|
|
// 100,000,000 Sompi: 1 KAS
|
|
// 100,000 Sompi: 0.001 KAS
|
|
}
|
|
|
|
func ExampleNewAmount() {
|
|
amountOne, err := util.NewAmount(1)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(amountOne) //Output 1
|
|
|
|
amountFraction, err := util.NewAmount(0.01234567)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(amountFraction) //Output 2
|
|
|
|
amountZero, err := util.NewAmount(0)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(amountZero) //Output 3
|
|
|
|
amountNaN, err := util.NewAmount(math.NaN())
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(amountNaN) //Output 4
|
|
|
|
// Output: 1 KAS
|
|
// 0.01234567 KAS
|
|
// 0 KAS
|
|
// invalid kaspa amount
|
|
}
|
|
|
|
func ExampleAmount_unitConversions() {
|
|
amount := util.Amount(44433322211100)
|
|
|
|
fmt.Println("Sompi to kKAS:", amount.Format(util.AmountKiloKAS))
|
|
fmt.Println("Sompi to KAS:", amount)
|
|
fmt.Println("Sompi to MilliKAS:", amount.Format(util.AmountMilliKAS))
|
|
fmt.Println("Sompi to MicroKAS:", amount.Format(util.AmountMicroKAS))
|
|
fmt.Println("Sompi to Sompi:", amount.Format(util.AmountSompi))
|
|
|
|
// Output:
|
|
// Sompi to kKAS: 444.333222111 kKAS
|
|
// Sompi to KAS: 444333.222111 KAS
|
|
// Sompi to MilliKAS: 444333222.111 mKAS
|
|
// Sompi to MicroKAS: 444333222111 μKAS
|
|
// Sompi to Sompi: 44433322211100 Sompi
|
|
}
|
|
|
|
// This example demonstrates how to convert the compact "bits" in a block header
|
|
// which represent the target difficulty to a big integer and display it using
|
|
// the typical hex notation.
|
|
func ExampleCompactToBig() {
|
|
bits := uint32(419465580)
|
|
targetDifficulty := difficulty.CompactToBig(bits)
|
|
|
|
// Display it in hex.
|
|
fmt.Printf("%064x\n", targetDifficulty.Bytes())
|
|
|
|
// Output:
|
|
// 0000000000000000896c00000000000000000000000000000000000000000000
|
|
}
|
|
|
|
// This example demonstrates how to convert a target difficulty into the compact
|
|
// "bits" in a block header which represent that target difficulty .
|
|
func ExampleBigToCompact() {
|
|
// Convert the target difficulty from block 300000 in the bitcoin
|
|
// main chain to compact form.
|
|
t := "0000000000000000896c00000000000000000000000000000000000000000000"
|
|
targetDifficulty, success := new(big.Int).SetString(t, 16)
|
|
if !success {
|
|
fmt.Println("invalid target difficulty")
|
|
return
|
|
}
|
|
bits := difficulty.BigToCompact(targetDifficulty)
|
|
|
|
fmt.Println(bits)
|
|
|
|
// Output:
|
|
// 419465580
|
|
}
|