status-go/services/ens/contracts.go

87 lines
2.3 KiB
Go
Raw Normal View History

2021-12-21 15:05:09 +00:00
package ens
import (
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/rpc"
2022-01-14 11:17:31 +00:00
"github.com/status-im/status-go/services/ens/erc20"
2021-12-21 15:05:09 +00:00
"github.com/status-im/status-go/services/ens/registrar"
"github.com/status-im/status-go/services/ens/resolver"
)
var errorNotAvailableOnChainID = errors.New("not available for chainID")
var resolversByChainID = map[uint64]common.Address{
1: common.HexToAddress("0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"), // mainnet
2022-01-03 09:07:33 +00:00
3: common.HexToAddress("0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"), // ropsten
2021-12-21 15:05:09 +00:00
}
var usernameRegistrarsByChainID = map[uint64]common.Address{
1: common.HexToAddress("0xDB5ac1a559b02E12F29fC0eC0e37Be8E046DEF49"), // mainnet
3: common.HexToAddress("0xdaae165beb8c06e0b7613168138ebba774aff071"), // ropsten
}
2022-01-14 11:17:31 +00:00
var sntByChainID = map[uint64]common.Address{
1: common.HexToAddress("0x744d70fdbe2ba4cf95131626614a1763df805b9e"), // mainnet
3: common.HexToAddress("0xc55cf4b03948d7ebc8b9e8bad92643703811d162"), // ropsten
}
2021-12-21 15:05:09 +00:00
type contractMaker struct {
2022-01-14 11:17:31 +00:00
RPCClient *rpc.Client
2021-12-21 15:05:09 +00:00
}
func (c *contractMaker) newRegistry(chainID uint64) (*resolver.ENSRegistryWithFallback, error) {
if _, ok := resolversByChainID[chainID]; !ok {
return nil, errorNotAvailableOnChainID
}
2022-01-14 11:17:31 +00:00
backend, err := c.RPCClient.EthClient(chainID)
2021-12-21 15:05:09 +00:00
if err != nil {
return nil, err
}
return resolver.NewENSRegistryWithFallback(
resolversByChainID[chainID],
backend,
)
}
func (c *contractMaker) newPublicResolver(chainID uint64, resolverAddress *common.Address) (*resolver.PublicResolver, error) {
2022-01-14 11:17:31 +00:00
backend, err := c.RPCClient.EthClient(chainID)
2021-12-21 15:05:09 +00:00
if err != nil {
return nil, err
}
return resolver.NewPublicResolver(*resolverAddress, backend)
}
func (c *contractMaker) newUsernameRegistrar(chainID uint64) (*registrar.UsernameRegistrar, error) {
if _, ok := usernameRegistrarsByChainID[chainID]; !ok {
return nil, errorNotAvailableOnChainID
}
2022-01-14 11:17:31 +00:00
backend, err := c.RPCClient.EthClient(chainID)
2021-12-21 15:05:09 +00:00
if err != nil {
return nil, err
}
return registrar.NewUsernameRegistrar(
usernameRegistrarsByChainID[chainID],
backend,
)
}
2022-01-14 11:17:31 +00:00
func (c *contractMaker) newSNT(chainID uint64) (*erc20.SNT, error) {
if _, ok := sntByChainID[chainID]; !ok {
return nil, errorNotAvailableOnChainID
}
backend, err := c.RPCClient.EthClient(chainID)
if err != nil {
return nil, err
}
return erc20.NewSNT(sntByChainID[chainID], backend)
}