-
Notifications
You must be signed in to change notification settings - Fork 3
/
allowance.go
72 lines (68 loc) · 2.1 KB
/
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
64
65
66
67
68
69
70
71
72
package go_cowswap
import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
contract_binding "github.com/itsahedge/go-cowswap/contracts/generated"
"math/big"
"strconv"
)
// GetAllowance - Token allowance for owner
func (c *Client) GetAllowance(ctx context.Context, ownerAddress, tokenAddress string) (*big.Int, error) {
if ownerAddress == "" {
return nil, errors.New("must provide an Owner Address")
}
if tokenAddress == "" {
return nil, errors.New("must provide a Token Address")
}
token := common.HexToAddress(tokenAddress)
contract, err := contract_binding.NewErc20(token, c.EthClient)
if err != nil {
return nil, err
}
owner := common.HexToAddress(ownerAddress)
spender := common.HexToAddress(GPv2_Vault_Relayer)
allowance, err := contract.Allowance(&bind.CallOpts{Context: ctx}, owner, spender)
if err != nil {
return nil, err
}
return allowance, nil
}
type ApproveAllowance struct {
TokenAddress string `json:"token_address"`
WalletAddress string `json:"wallet_address"`
}
// SetAllowance - Set token allowance for owner on CowSwap GPv2 Vault Relayer contract
func (c *Client) SetAllowance(ctx context.Context, tokenAddress, tokenAmount string) (*types.Transaction, error) {
if c.TransactionSigner == nil {
return nil, errors.New("transaction signer not initialized")
}
var amountToApprove *big.Int
if tokenAmount == "" {
amountToApprove = new(big.Int).Lsh(big.NewInt(1), 256-7)
}
if tokenAmount != "" {
i, _ := strconv.ParseInt(tokenAmount, 10, 64)
amountToApprove = new(big.Int).Set(big.NewInt(i))
}
auth := c.TransactionSigner.Auth
token := common.HexToAddress(tokenAddress)
contract, err := contract_binding.NewErc20(token, c.EthClient)
if err != nil {
panic(err)
}
spender := common.HexToAddress(GPv2_Vault_Relayer)
opts := &bind.TransactOpts{
Context: ctx,
Signer: auth.Signer,
From: auth.From,
}
tx, err := contract.Approve(opts, spender, amountToApprove)
if err != nil {
return nil, fmt.Errorf("Approve() err:\n%v", err)
}
return tx, nil
}