-
Notifications
You must be signed in to change notification settings - Fork 3
/
allowance_test.go
51 lines (47 loc) · 1.33 KB
/
allowance_test.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
package go_cowswap
import (
"context"
"encoding/json"
"testing"
)
func TestClient_GetAllowance(t *testing.T) {
client, err := NewClient(Options)
if err != nil {
t.Fatal(err)
}
ownerAddress := Options.EthAddress
tokenAddress := TOKEN_ADDRESSES["goerli"]["WETH"]
allowance, err := client.GetAllowance(context.Background(), ownerAddress, tokenAddress)
if err != nil {
t.Fatalf("GetAllowance err: %v", err)
}
result, _ := json.MarshalIndent(allowance, "", " ")
t.Logf("%v token allowance: %v \n", tokenAddress, string(result))
}
func TestClient_SetAllowance(t *testing.T) {
client, err := NewClient(Options)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
// First check the allowance for WETH
tokenAddress := "0x91056D4A53E1faa1A84306D4deAEc71085394bC8"
allowance, err := client.GetAllowance(ctx, Options.EthAddress, tokenAddress)
if err != nil {
t.Fatal(err)
}
// if token allowance: 0
if len(allowance.Bits()) == 0 {
t.Logf("%v token allowance is: %v. Please call Approve() \n", tokenAddress, allowance)
// if allowance is 0, set it.
tokenAmount := ""
setAllowanceTx, err := client.SetAllowance(ctx, tokenAddress, tokenAmount)
if err != nil {
t.Logf("%v", err)
} else {
t.Logf("tx hash: %v", setAllowanceTx.Hash())
}
} else {
t.Logf("%v token allowance: %v \n", tokenAddress, allowance)
}
}