-
Notifications
You must be signed in to change notification settings - Fork 44
/
test_support.go
63 lines (53 loc) · 1.72 KB
/
test_support.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2023 The AthanorLabs/atomic-swap Authors
// SPDX-License-Identifier: LGPL-3.0-only
//go:build !prod
package coins
import (
"fmt"
"math/big"
"github.com/cockroachdb/apd/v3"
)
//
// FUNCTIONS ONLY FOR UNIT TESTS
//
// StrToDecimal converts strings to apd.Decimal for tests, panicking on error.
// This function is intended for use with string constants, so panic is arguably
// correct and allows variables to be declared outside a test function.
func StrToDecimal(amount string) *apd.Decimal {
a, _, err := new(apd.Decimal).SetString(amount)
if err != nil {
panic(err)
}
return a
}
// StrToETHAsset converts a string into the EthAssetAmount. Pass nil for the
// token if the asset is ETH.
func StrToETHAsset(amount string, optionalToken *ERC20TokenInfo) EthAssetAmount {
return NewEthAssetAmount(StrToDecimal(amount), optionalToken)
}
// StrToExchangeRate converts strings to ExchangeRate for tests, panicking on error.
func StrToExchangeRate(rate string) *ExchangeRate {
r := new(ExchangeRate)
if err := r.UnmarshalText([]byte(rate)); err != nil {
panic(err) // test only function
}
return r
}
// IntToWei converts some amount of Wei into an WeiAmount for unit tests.
func IntToWei(amount int64) *WeiAmount {
if amount < 0 {
panic(fmt.Sprintf("Wei amount %d is negative", amount)) // test only function
}
return NewWeiAmount(big.NewInt(amount))
}
// Sub returns the value of a-b in a newly allocated WeiAmount variable.
// If a or b is NaN, this function will panic, but we exclude such values
// during input validation.
func (a *WeiAmount) Sub(b *WeiAmount) *WeiAmount {
result := new(WeiAmount)
_, err := decimalCtx.Sub(result.Decimal(), a.Decimal(), b.Decimal())
if err != nil {
panic(err)
}
return result
}