2022-02-02 22:50:55 +00:00
|
|
|
package contracts
|
|
|
|
|
2024-10-03 19:59:44 +00:00
|
|
|
//go:generate mockgen -source=contracts.go -destination=mock/contracts.go
|
|
|
|
|
2022-02-02 22:50:55 +00:00
|
|
|
import (
|
2022-10-24 06:45:14 +00:00
|
|
|
"errors"
|
|
|
|
|
2022-02-02 22:50:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-01-27 13:02:12 +00:00
|
|
|
"github.com/status-im/status-go/contracts/balancechecker"
|
2022-06-02 12:17:52 +00:00
|
|
|
"github.com/status-im/status-go/contracts/directory"
|
2022-09-09 06:58:36 +00:00
|
|
|
"github.com/status-im/status-go/contracts/ethscan"
|
2022-12-19 12:37:37 +00:00
|
|
|
"github.com/status-im/status-go/contracts/ierc20"
|
2022-02-02 22:50:55 +00:00
|
|
|
"github.com/status-im/status-go/contracts/registrar"
|
|
|
|
"github.com/status-im/status-go/contracts/resolver"
|
|
|
|
"github.com/status-im/status-go/contracts/snt"
|
|
|
|
"github.com/status-im/status-go/contracts/stickers"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
|
|
|
)
|
|
|
|
|
2024-06-26 08:03:01 +00:00
|
|
|
type ContractMakerIface interface {
|
2024-06-27 21:27:09 +00:00
|
|
|
NewEthScan(chainID uint64) (ethscan.BalanceScannerIface, uint, error)
|
|
|
|
NewERC20(chainID uint64, contractAddr common.Address) (ierc20.IERC20Iface, error)
|
|
|
|
NewERC20Caller(chainID uint64, contractAddr common.Address) (ierc20.IERC20CallerIface, error)
|
2024-06-26 08:03:01 +00:00
|
|
|
// TODO extend with other contracts
|
|
|
|
}
|
|
|
|
|
2022-02-02 22:50:55 +00:00
|
|
|
type ContractMaker struct {
|
2024-06-27 21:27:09 +00:00
|
|
|
RPCClient rpc.ClientInterface
|
2022-02-02 22:50:55 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 21:27:09 +00:00
|
|
|
func NewContractMaker(client rpc.ClientInterface) (*ContractMaker, error) {
|
2023-08-29 12:59:37 +00:00
|
|
|
if client == nil {
|
|
|
|
return nil, errors.New("could not initialize ContractMaker with an rpc client")
|
|
|
|
}
|
|
|
|
return &ContractMaker{RPCClient: client}, nil
|
|
|
|
}
|
|
|
|
|
2022-10-24 10:07:44 +00:00
|
|
|
func (c *ContractMaker) NewRegistryWithAddress(chainID uint64, address common.Address) (*resolver.ENSRegistryWithFallback, error) {
|
2022-02-02 22:50:55 +00:00
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolver.NewENSRegistryWithFallback(
|
2022-10-24 10:07:44 +00:00
|
|
|
address,
|
2022-02-02 22:50:55 +00:00
|
|
|
backend,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-24 10:07:44 +00:00
|
|
|
func (c *ContractMaker) NewRegistry(chainID uint64) (*resolver.ENSRegistryWithFallback, error) {
|
|
|
|
contractAddr, err := resolver.ContractAddress(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.NewRegistryWithAddress(chainID, contractAddr)
|
|
|
|
}
|
|
|
|
|
2022-02-02 22:50:55 +00:00
|
|
|
func (c *ContractMaker) NewPublicResolver(chainID uint64, resolverAddress *common.Address) (*resolver.PublicResolver, error) {
|
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolver.NewPublicResolver(*resolverAddress, backend)
|
|
|
|
}
|
|
|
|
|
2022-08-23 12:29:30 +00:00
|
|
|
func (c *ContractMaker) NewUsernameRegistrar(chainID uint64, contractAddr common.Address) (*registrar.UsernameRegistrar, error) {
|
2022-02-02 22:50:55 +00:00
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return registrar.NewUsernameRegistrar(
|
|
|
|
contractAddr,
|
|
|
|
backend,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-27 21:27:09 +00:00
|
|
|
func (c *ContractMaker) NewERC20(chainID uint64, contractAddr common.Address) (ierc20.IERC20Iface, error) {
|
2022-12-19 12:37:37 +00:00
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ierc20.NewIERC20(
|
|
|
|
contractAddr,
|
|
|
|
backend,
|
|
|
|
)
|
|
|
|
}
|
2024-06-27 21:27:09 +00:00
|
|
|
func (c *ContractMaker) NewERC20Caller(chainID uint64, contractAddr common.Address) (ierc20.IERC20CallerIface, error) {
|
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ierc20.NewIERC20Caller(contractAddr, backend)
|
|
|
|
}
|
2022-12-19 12:37:37 +00:00
|
|
|
|
2022-02-02 22:50:55 +00:00
|
|
|
func (c *ContractMaker) NewSNT(chainID uint64) (*snt.SNT, error) {
|
|
|
|
contractAddr, err := snt.ContractAddress(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return snt.NewSNT(contractAddr, backend)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ContractMaker) NewStickerType(chainID uint64) (*stickers.StickerType, error) {
|
|
|
|
contractAddr, err := stickers.StickerTypeContractAddress(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stickers.NewStickerType(
|
|
|
|
contractAddr,
|
|
|
|
backend,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ContractMaker) NewStickerMarket(chainID uint64) (*stickers.StickerMarket, error) {
|
|
|
|
contractAddr, err := stickers.StickerMarketContractAddress(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stickers.NewStickerMarket(
|
|
|
|
contractAddr,
|
|
|
|
backend,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ContractMaker) NewStickerPack(chainID uint64) (*stickers.StickerPack, error) {
|
|
|
|
contractAddr, err := stickers.StickerPackContractAddress(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stickers.NewStickerPack(
|
|
|
|
contractAddr,
|
|
|
|
backend,
|
|
|
|
)
|
|
|
|
}
|
2022-06-02 12:17:52 +00:00
|
|
|
|
|
|
|
func (c *ContractMaker) NewDirectory(chainID uint64) (*directory.Directory, error) {
|
|
|
|
contractAddr, err := directory.ContractAddress(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return directory.NewDirectory(
|
|
|
|
contractAddr,
|
|
|
|
backend,
|
|
|
|
)
|
|
|
|
}
|
2022-09-06 18:07:22 +00:00
|
|
|
|
2024-06-27 21:27:09 +00:00
|
|
|
func (c *ContractMaker) NewEthScan(chainID uint64) (ethscan.BalanceScannerIface, uint, error) {
|
2022-09-09 06:58:36 +00:00
|
|
|
contractAddr, err := ethscan.ContractAddress(chainID)
|
|
|
|
if err != nil {
|
2023-09-20 08:41:23 +00:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contractCreatedAt, err := ethscan.ContractCreatedAt(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2022-09-09 06:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
2023-09-20 08:41:23 +00:00
|
|
|
return nil, 0, err
|
2022-09-09 06:58:36 +00:00
|
|
|
}
|
2022-10-24 06:45:14 +00:00
|
|
|
|
2023-09-20 08:41:23 +00:00
|
|
|
scanner, err := ethscan.NewBalanceScanner(
|
2022-09-09 06:58:36 +00:00
|
|
|
contractAddr,
|
|
|
|
backend,
|
|
|
|
)
|
2023-09-20 08:41:23 +00:00
|
|
|
|
|
|
|
return scanner, contractCreatedAt, err
|
2022-09-09 06:58:36 +00:00
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
|
2024-01-19 15:57:04 +00:00
|
|
|
func (c *ContractMaker) NewBalanceChecker(chainID uint64) (*balancechecker.BalanceChecker, error) {
|
|
|
|
contractAddr, err := balancechecker.ContractAddress(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backend, err := c.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return balancechecker.NewBalanceChecker(
|
|
|
|
contractAddr,
|
|
|
|
backend,
|
|
|
|
)
|
|
|
|
}
|