-
Notifications
You must be signed in to change notification settings - Fork 4
/
set-allowance.go
63 lines (60 loc) · 1.72 KB
/
set-allowance.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
package main
import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
cowswap "github.com/itsahedge/go-cowswap"
"log"
)
func main() {
// Setting the allowance for an Address requires private key to be set
network := "goerli"
rpc := "https://eth-goerli-rpc.gateway.pokt.network"
host := "https://api.cow.fi/goerli/api/v1"
privateKey, err := crypto.GenerateKey()
if err != nil {
log.Fatal(err)
}
privateKeyBytes := crypto.FromECDSA(privateKey)
privateKeyString := hexutil.Encode(privateKeyBytes)[2:]
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
options := cowswap.ConfigOpts{
Network: network,
Host: host,
RpcUrl: rpc,
EthAddress: address,
PrivateKey: privateKeyString,
}
client, err := cowswap.NewClient(options)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// First check the allowance for WETH
tokenAddress := "0x91056D4A53E1faa1A84306D4deAEc71085394bC8"
allowance, err := client.GetAllowance(ctx, address, tokenAddress)
if err != nil {
log.Fatal(err)
}
// if token allowance: 0
if len(allowance.Bits()) == 0 {
fmt.Printf("%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 {
fmt.Printf("setting allowance err: %v", err)
} else {
fmt.Printf("tx hash: %v", setAllowanceTx.Hash())
}
} else {
fmt.Printf("%v token allowance: %v \n", tokenAddress, allowance)
}
}