Skip to content

Commit

Permalink
Merge pull request #333 from multiversx/fetch-tokens-list
Browse files Browse the repository at this point in the history
  • Loading branch information
dragos-rebegea authored Sep 6, 2024
2 parents afe1fb1 + d276170 commit 759a9a8
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 130 deletions.
9 changes: 9 additions & 0 deletions clients/multiversx/mxClientDataGetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
getTotalBalances = "getTotalBalances"
getMintBalances = "getMintBalances"
getBurnBalances = "getBurnBalances"
getAllKnownTokens = "getAllKnownTokens"
)

// ArgsMXClientDataGetter is the arguments DTO used in the NewMXClientDataGetter constructor
Expand Down Expand Up @@ -526,6 +527,14 @@ func getStatusFromBuff(buff []byte) (byte, error) {
return buff[len(buff)-1], nil
}

// GetAllKnownTokens returns all registered tokens
func (dataGetter *mxClientDataGetter) GetAllKnownTokens(ctx context.Context) ([][]byte, error) {
builder := dataGetter.createSafeDefaultVmQueryBuilder()
builder.Function(getAllKnownTokens)

return dataGetter.executeQueryFromBuilder(ctx, builder)
}

// IsInterfaceNil returns true if there is no value under the interface
func (dataGetter *mxClientDataGetter) IsInterfaceNil() bool {
return dataGetter == nil
Expand Down
29 changes: 29 additions & 0 deletions clients/multiversx/mxClientDataGetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,35 @@ func TestMXClientDataGetter_GetAllStakedRelayers(t *testing.T) {
assert.Equal(t, providedRelayers, result)
}

func TestMXClientDataGetter_GetAllKnownTokens(t *testing.T) {
t.Parallel()

args := createMockArgsMXClientDataGetter()
providedTokens := [][]byte{[]byte("tkn1"), []byte("tkn2")}
args.Proxy = &interactors.ProxyStub{
ExecuteVMQueryCalled: func(ctx context.Context, vmRequest *data.VmValueRequest) (*data.VmValuesResponseData, error) {
assert.Equal(t, getBech32Address(args.SafeContractAddress), vmRequest.Address)
assert.Equal(t, "", vmRequest.CallValue)
assert.Equal(t, getAllKnownTokens, vmRequest.FuncName)

assert.Nil(t, vmRequest.Args)

return &data.VmValuesResponseData{
Data: &vm.VMOutputApi{
ReturnCode: okCodeAfterExecution,
ReturnData: providedTokens,
},
}, nil
},
}

dg, _ := NewMXClientDataGetter(args)

result, err := dg.GetAllKnownTokens(context.Background())
assert.Nil(t, err)
assert.Equal(t, providedTokens, result)
}

func TestMultiversXClientDataGetter_GetShardCurrentNonce(t *testing.T) {
t.Parallel()

Expand Down
17 changes: 1 addition & 16 deletions cmd/migration/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
[MultiversX]
NetworkAddress = "https://devnet-gateway.multiversx.com" # the network address
MultisigContractAddress = "erd1qqqqqqqqqqqqqpgqtvnswnzxxz8susupesys0hvg7q2z5nawrcjq06qdus"
SafeContractAddress = "erd1qqqqqqqqqqqqqpgqhxkc48lt5uv2hejj4wtjqvugfm4wgv6gyfkqw0uuxl"
[MultiversX.Proxy]
CacherExpirationSeconds = 600 # the caching time in seconds

Expand All @@ -32,19 +33,3 @@
[Logs]
LogFileLifeSpanInSec = 86400 # 24h
LogFileLifeSpanInMB = 1024 # 1GB

[WhitelistedTokens]
List = [
"ETHUSDC-220753",
"ETHUTK-8cdf7a",
"ETHUSDT-9c73c6",
"ETHBUSD-450923",
"ETHHMT-18538a",
"ETHCGG-ee4e0c",
"ETHINFRA-60a3bf",
"ETHWBTC-74e282",
"ETHWETH-e1c126",
"ETHWSDAI-572803",
"ETHWDAI-bd65f9",
"ETHUMB-291202",
]
33 changes: 17 additions & 16 deletions cmd/migration/config/migration.json
Original file line number Diff line number Diff line change
@@ -1,76 +1,77 @@
{
"OldSafeContractAddress": "0x92A26975433A61CF1134802586aa669bAB8B69f3",
"NewSafeContractAddress": "0x37aaaf95887624a363effB7762D489E3C05c2a02",
"BatchID": 3547,
"NewSafeContractAddress": "0xA6504Cc508889bbDBd4B748aFf6EA6b5D0d2684c",
"BatchID": 3548,
"MessageHash": "0x9fad9673d99127cf3bfbcb25fdb271c90009e1199e5ae3bd98f516a6d2b46357",
"DepositsInfo": [
{
"DepositNonce": 4651,
"DepositNonce": 4652,
"Token": "ETHUSDC-220753",
"ContractAddress": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"Amount": "7245293620507"
"Amount": "7091634513581"
},
{
"DepositNonce": 4652,
"DepositNonce": 4653,
"Token": "ETHUTK-8cdf7a",
"ContractAddress": "0xdc9Ac3C20D1ed0B540dF9b1feDC10039Df13F99c",
"Amount": "224564169411881824066539333"
},
{
"DepositNonce": 4653,
"DepositNonce": 4654,
"Token": "ETHUSDT-9c73c6",
"ContractAddress": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
"Amount": "305146464594"
"Amount": "318715112062"
},
{
"DepositNonce": 4654,
"DepositNonce": 4655,
"Token": "ETHBUSD-450923",
"ContractAddress": "0x4Fabb145d64652a948d72533023f6E7A623C7C53",
"Amount": "22294352736330153151984"
},
{
"DepositNonce": 4655,
"DepositNonce": 4656,
"Token": "ETHHMT-18538a",
"ContractAddress": "0xd1ba9BAC957322D6e8c07a160a3A8dA11A0d2867",
"Amount": "435000000000000000000"
},
{
"DepositNonce": 4656,
"DepositNonce": 4657,
"Token": "ETHCGG-ee4e0c",
"ContractAddress": "0x1fE24F25b1Cf609B9c4e7E12D802e3640dFA5e43",
"Amount": "1500138370967581082219795"
},
{
"DepositNonce": 4657,
"DepositNonce": 4658,
"Token": "ETHINFRA-60a3bf",
"ContractAddress": "0x013062189dc3dcc99e9Cee714c513033b8d99e3c",
"Amount": "141172595980399500424091"
},
{
"DepositNonce": 4658,
"DepositNonce": 4659,
"Token": "ETHWBTC-74e282",
"ContractAddress": "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
"Amount": "3898848955"
},
{
"DepositNonce": 4659,
"DepositNonce": 4660,
"Token": "ETHWETH-e1c126",
"ContractAddress": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"Amount": "725921417141355037005"
},
{
"DepositNonce": 4660,
"DepositNonce": 4661,
"Token": "ETHWSDAI-572803",
"ContractAddress": "0x83F20F44975D03b1b09e64809B757c47f942BEeA",
"Amount": "5431516086574385345409"
},
{
"DepositNonce": 4661,
"DepositNonce": 4662,
"Token": "ETHWDAI-bd65f9",
"ContractAddress": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
"Amount": "127608014625114134146189"
},
{
"DepositNonce": 4662,
"DepositNonce": 4663,
"Token": "ETHUMB-291202",
"ContractAddress": "0x6fC13EACE26590B80cCCAB1ba5d51890577D83B2",
"Amount": "4669367702477323508554075"
Expand Down
2 changes: 1 addition & 1 deletion cmd/migration/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
", if set to *:INFO the logs for all packages will have the INFO level. However, if set to *:INFO,api:DEBUG" +
" the logs for all packages will have the INFO level, excepting the api package which will receive a DEBUG" +
" log level.",
Value: "*:" + logger.LogDebug.String(),
Value: "*:" + logger.LogInfo.String(),
}
configurationFile = cli.StringFlag{
Name: "config",
Expand Down
30 changes: 16 additions & 14 deletions cmd/migration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
ethereumClient "github.com/multiversx/mx-bridge-eth-go/clients/ethereum"
"github.com/multiversx/mx-bridge-eth-go/clients/ethereum/contract"
"github.com/multiversx/mx-bridge-eth-go/clients/multiversx"
"github.com/multiversx/mx-bridge-eth-go/clients/multiversx/mappers"
"github.com/multiversx/mx-bridge-eth-go/cmd/migration/disabled"
"github.com/multiversx/mx-bridge-eth-go/config"
"github.com/multiversx/mx-bridge-eth-go/executors/ethereum"
Expand Down Expand Up @@ -59,6 +58,8 @@ func main() {
log.Error(err.Error())
os.Exit(1)
}

log.Info("process finished successfully")
}

func execute(ctx *cli.Context) error {
Expand Down Expand Up @@ -108,9 +109,14 @@ func generateAndSign(ctx *cli.Context, cfg config.MigrationToolConfig) error {
return err
}

safeAddress, err := data.NewAddressFromBech32String(cfg.MultiversX.SafeContractAddress)
if err != nil {
return err
}

argsMXClientDataGetter := multiversx.ArgsMXClientDataGetter{
MultisigContractAddress: multisigAddress,
SafeContractAddress: dummyAddress,
SafeContractAddress: safeAddress,
RelayerAddress: dummyAddress,
Proxy: proxy,
Log: log,
Expand All @@ -120,11 +126,6 @@ func generateAndSign(ctx *cli.Context, cfg config.MigrationToolConfig) error {
return err
}

tokensWrapper, err := mappers.NewMultiversXToErc20Mapper(mxDataGetter)
if err != nil {
return err
}

ethClient, err := ethclient.Dial(cfg.Eth.NetworkAddress)
if err != nil {
return err
Expand All @@ -146,11 +147,11 @@ func generateAndSign(ctx *cli.Context, cfg config.MigrationToolConfig) error {
}

argsCreator := ethereum.ArgsMigrationBatchCreator{
TokensList: cfg.WhitelistedTokens.List,
TokensMapper: tokensWrapper,
MvxDataGetter: mxDataGetter,
Erc20ContractsHolder: erc20ContractsHolder,
SafeContractAddress: safeEthAddress,
SafeContractWrapper: safeInstance,
Logger: log,
}

creator, err := ethereum.NewMigrationBatchCreator(argsCreator)
Expand Down Expand Up @@ -179,16 +180,15 @@ func generateAndSign(ctx *cli.Context, cfg config.MigrationToolConfig) error {
return err
}

log.Info("signing batch", "message hash", batchInfo.MessageHash.String(),
"public key", cryptoHandler.GetAddress().String())

signature, err := cryptoHandler.Sign(batchInfo.MessageHash)
if err != nil {
return err
}

log.Info(string(val))
log.Info("Batch signed",
"public key", cryptoHandler.GetAddress().String(),
"message hash", batchInfo.MessageHash.String(),
"signature", signature)
log.Info("Migration .json file contents: \n" + string(val))

jsonFilename := ctx.GlobalString(migrationJsonFile.Name)
jsonFilename = applyTimestamp(jsonFilename)
Expand All @@ -211,6 +211,8 @@ func generateAndSign(ctx *cli.Context, cfg config.MigrationToolConfig) error {
return err
}

log.Info("Signature .json file contents: \n" + string(val))

return os.WriteFile(sigFilename, val, os.ModePerm)
}

Expand Down
12 changes: 3 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,9 @@ type ScCallsModuleConfig struct {
Logs LogsConfig
}

// WhitelistedTokensConfig will hold the whitelisted tokens config
type WhitelistedTokensConfig struct {
List []string
}

// MigrationToolConfig is the migration tool config struct
type MigrationToolConfig struct {
Eth EthereumConfig
MultiversX MultiversXConfig
Logs LogsConfig
WhitelistedTokens WhitelistedTokensConfig
Eth EthereumConfig
MultiversX MultiversXConfig
Logs LogsConfig
}
20 changes: 3 additions & 17 deletions config/tomlConfigs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ func TestMigrationToolConfig(t *testing.T) {
MultiversX: MultiversXConfig{
NetworkAddress: "https://devnet-gateway.multiversx.com",
MultisigContractAddress: "erd1qqqqqqqqqqqqqpgqzyuaqg3dl7rqlkudrsnm5ek0j3a97qevd8sszj0glf",
SafeContractAddress: "erd1qqqqqqqqqqqqqpgqtvnswnzxxz8susupesys0hvg7q2z5nawrcjq06qdus",
Proxy: ProxyConfig{
CacherExpirationSeconds: 600,
RestAPIEntityType: "observer",
Expand All @@ -494,22 +495,6 @@ func TestMigrationToolConfig(t *testing.T) {
LogFileLifeSpanInSec: 86400,
LogFileLifeSpanInMB: 1024,
},
WhitelistedTokens: WhitelistedTokensConfig{
List: []string{
"ETHUSDC-220753",
"ETHUTK-8cdf7a",
"ETHUSDT-9c73c6",
"ETHBUSD-450923",
"ETHHMT-18538a",
"ETHCGG-ee4e0c",
"ETHINFRA-60a3bf",
"ETHWBTC-74e282",
"ETHWETH-e1c126",
"ETHWSDAI-572803",
"ETHWDAI-bd65f9",
"ETHUMB-291202",
},
},
}

testString := `
Expand All @@ -534,7 +519,8 @@ func TestMigrationToolConfig(t *testing.T) {
[MultiversX]
NetworkAddress = "https://devnet-gateway.multiversx.com" # the network address
MultisigContractAddress = "erd1qqqqqqqqqqqqqpgqzyuaqg3dl7rqlkudrsnm5ek0j3a97qevd8sszj0glf"
MultisigContractAddress = "erd1qqqqqqqqqqqqqpgqzyuaqg3dl7rqlkudrsnm5ek0j3a97qevd8sszj0glf" # the multiversx address for the bridge contract
SafeContractAddress = "erd1qqqqqqqqqqqqqpgqtvnswnzxxz8susupesys0hvg7q2z5nawrcjq06qdus" # the multiversx address for the safe contract
[MultiversX.Proxy]
CacherExpirationSeconds = 600 # the caching time in seconds
Expand Down
10 changes: 6 additions & 4 deletions executors/ethereum/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package ethereum
import "errors"

var (
errEmptyTokensList = errors.New("empty tokens list")
errNilTokensMapper = errors.New("nil MultiversX to Ethereum tokens mapper")
errNilErc20ContractsHolder = errors.New("nil ERC20 contracts holder")
errNilSafeContractWrapper = errors.New("nil safe contract wrapper")
errEmptyTokensList = errors.New("empty tokens list")
errNilMvxDataGetter = errors.New("nil MultiversX data getter")
errNilErc20ContractsHolder = errors.New("nil ERC20 contracts holder")
errNilSafeContractWrapper = errors.New("nil safe contract wrapper")
errWrongERC20AddressResponse = errors.New("wrong ERC20 address response")
errNilLogger = errors.New("nil logger")
)
7 changes: 7 additions & 0 deletions executors/ethereum/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ type SafeContractWrapper interface {
DepositsCount(opts *bind.CallOpts) (uint64, error)
BatchesCount(opts *bind.CallOpts) (uint64, error)
}

// MvxDataGetter defines the operations for the data getter operating on MultiversX chain
type MvxDataGetter interface {
GetAllKnownTokens(ctx context.Context) ([][]byte, error)
GetERC20AddressForTokenId(ctx context.Context, tokenId []byte) ([][]byte, error)
IsInterfaceNil() bool
}
Loading

0 comments on commit 759a9a8

Please sign in to comment.