-
Notifications
You must be signed in to change notification settings - Fork 8
/
defisdk.go
78 lines (65 loc) · 2.07 KB
/
defisdk.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
package defisdk
import (
"log"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
registry "github.com/zeriontech/defi-sdk-go/adapter-registry"
)
type DefiSDK struct {
registryAddress common.Address
client *ethclient.Client
registry *registry.AdapterRegistry
}
const registryAddressString string = "0x06fe76b2f432fdfecaef1a7d4f6c3d41b5861672"
func New(nodeURL string) DefiSDK {
client, err := ethclient.Dial(nodeURL)
if err != nil {
log.Fatal(err)
}
registryAddress := common.HexToAddress(registryAddressString)
instance, err := registry.NewAdapterRegistry(registryAddress, client)
if err != nil {
log.Fatal(err)
}
sdk := DefiSDK{registryAddress, client, instance}
return sdk
}
func (sdk DefiSDK) GetSupportedProtocols() []string {
supportedProtocols, err := sdk.registry.GetProtocolNames(&bind.CallOpts{})
if err != nil {
log.Fatal(err)
}
return supportedProtocols
}
func (sdk DefiSDK) GetTokenAdapterNames() []string {
tokenAdapters, err := sdk.registry.GetTokenAdapterNames(&bind.CallOpts{})
if err != nil {
log.Fatal(err)
}
return tokenAdapters
}
func (sdk DefiSDK) GetBalances(userAddress string) []registry.ProtocolBalance {
address := common.HexToAddress(userAddress)
protocolBalances, err := sdk.registry.GetBalances(&bind.CallOpts{}, address)
if err != nil {
log.Fatal(err)
}
return protocolBalances
}
func (sdk DefiSDK) GetProtocolBalance(userAddress string, protocolName string) registry.ProtocolBalance {
address := common.HexToAddress(userAddress)
protocolNames := []string{protocolName}
protocolBalances, err := sdk.registry.GetProtocolBalances(&bind.CallOpts{}, address, protocolNames)
if err != nil {
log.Fatal(err)
}
return protocolBalances[0]
}
func (sdk DefiSDK) GetTokenComponents(tokenType string, tokenAddress string) []registry.TokenBalance {
tokenBalance, err := sdk.registry.GetFullTokenBalance(&bind.CallOpts{}, tokenType, common.HexToAddress(tokenAddress))
if err != nil {
log.Fatal(err)
}
return tokenBalance.Underlying
}