-
Notifications
You must be signed in to change notification settings - Fork 33
/
swapfetcher.go
119 lines (105 loc) · 3.94 KB
/
swapfetcher.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package fetcher
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/synapsecns/sanguine/services/explorer/contracts/metaswap"
"github.com/synapsecns/sanguine/services/explorer/contracts/swap"
)
// SwapService --output=mocks --case=underscore.
type SwapService interface {
// GetTokenMetaData gets the token from the erc20 token contract given a swap contract token id.
GetTokenMetaData(ctx context.Context, tokenAddress common.Address) (*string, *uint8, error)
// GetTokenAddress gets the token address from the swap contract given a swap contract token id.
GetTokenAddress(ctx context.Context, tokenIndex uint8) (*common.Address, error)
}
// SwapFetcher is the fetcher for token data from the swap contract.
type swapFetcher struct {
swap *swap.SwapRef
metaSwap *metaswap.MetaSwapRef
backend bind.ContractBackend
swapAddress common.Address
}
// NewSwapFetcher creates a new swap fetcher.
// Backend must be an archive backend.
func NewSwapFetcher(swapAddress common.Address, backend bind.ContractBackend, isMetaSwap bool) (SwapService, error) {
if isMetaSwap {
metaSwap, err := metaswap.NewMetaSwapRef(swapAddress, backend)
if err != nil {
return nil, fmt.Errorf("could not bind metaswap contract: %w", err)
}
return &swapFetcher{nil, metaSwap, backend, swapAddress}, nil
}
swap, err := swap.NewSwapRef(swapAddress, backend)
if err != nil {
return nil, fmt.Errorf("could not bind swap contract: %w", err)
}
return &swapFetcher{swap, nil, backend, swapAddress}, nil
}
func (s *swapFetcher) GetTokenAddress(ctx context.Context, tokenIndex uint8) (*common.Address, error) {
if s.metaSwap != nil {
tokenAddress, err := s.metaSwap.GetToken(&bind.CallOpts{
Context: ctx,
}, tokenIndex)
if err != nil {
logger.Errorf("could not get metaswap token address %s", s.metaSwap.Address())
return nil, fmt.Errorf("could not get metaswap token address %s", s.metaSwap.Address())
}
return &tokenAddress, nil
}
tokenAddress, err := s.swap.GetToken(&bind.CallOpts{
Context: ctx,
}, tokenIndex)
if err != nil {
logger.Errorf("could not get token address %s", s.swap.Address())
return nil, fmt.Errorf("could not get token address %s", s.swap.Address())
}
return &tokenAddress, nil
}
// GetTokenMetaData gets the token from the erc20 token contract given a swap contract token id.
func (s *swapFetcher) GetTokenMetaData(ctx context.Context, tokenAddress common.Address) (*string, *uint8, error) {
if s.metaSwap != nil {
erc20caller, err := metaswap.NewERC20(tokenAddress, s.backend)
if err != nil {
logger.Errorf("could not bind erc20 contract: %s", err)
return nil, nil, fmt.Errorf("could not bind erc20 contract: %w", err)
}
tokenSymbol, err := erc20caller.Symbol(&bind.CallOpts{
Context: ctx,
})
if err != nil {
logger.Errorf("could not get token symbol: %s", err)
return nil, nil, fmt.Errorf("could not get token symbol: %w", err)
}
tokenDecimals, err := erc20caller.Decimals(&bind.CallOpts{
Context: ctx,
})
if err != nil {
logger.Errorf("could not get token decimals: %s", err)
return nil, nil, fmt.Errorf("could not get token decimal: %w", err)
}
return &tokenSymbol, &tokenDecimals, nil
}
erc20caller, err := swap.NewERC20(tokenAddress, s.backend)
if err != nil {
logger.Errorf("could not bind erc20 contract: %s", err)
return nil, nil, fmt.Errorf("could not bind erc20 contract: %w", err)
}
tokenSymbol, err := erc20caller.Symbol(&bind.CallOpts{
Context: ctx,
})
if err != nil {
logger.Errorf("could not get token symbol: %s", err)
return nil, nil, fmt.Errorf("could not get token symbol: %w", err)
}
tokenDecimals, err := erc20caller.Decimals(&bind.CallOpts{
Context: ctx,
})
if err != nil {
logger.Errorf("could not get token decimals: %s", err)
return nil, nil, fmt.Errorf("could not get token decimal: %w", err)
}
return &tokenSymbol, &tokenDecimals, nil
}
var _ SwapService = &swapFetcher{}