Skip to content

Commit

Permalink
fix: clean up token whitelist parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Feb 1, 2023
1 parent 966d9c5 commit 6960956
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
41 changes: 21 additions & 20 deletions pkg/config/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,27 @@ func UnmarshalTokenConfig(contents string) ([]configuration.Token, error) {
var outer map[string]interface{}
if err := json.Unmarshal([]byte(contents), &outer); err == nil {
for k, v := range outer {
for t, _ := range v.(map[string]interface{}) {
fmt.Printf("t: %s\n", t)
switch k {
case "Mainnet":
payload = append(payload, configuration.Token{
ChainID: 1,
Address: t,
})
case "Testnet":
payload = append(payload, configuration.Token{
ChainID: 420,
Address: t,
})
case "Goerli":
payload = append(payload, configuration.Token{
ChainID: 420,
Address: t,
})
default:
return nil, fmt.Errorf("unknown chain %s found when parsing json token list", k)
for t, b := range v.(map[string]interface{}) {
if b.(bool) {
switch k {
case "Mainnet":
payload = append(payload, configuration.Token{
ChainID: 1,
Address: t,
})
case "Testnet":
payload = append(payload, configuration.Token{
ChainID: 420,
Address: t,
})
case "Goerli":
payload = append(payload, configuration.Token{
ChainID: 420,
Address: t,
})
default:
return nil, fmt.Errorf("unknown chain %s found when parsing json token list", k)
}
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/config/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ func (testSuite *TokensTestSuite) TestUnmarshalOldTokenConfig() {
}, c)
}

// TestUnmarshalOldTokenConfigExcluded tests unmarshalling the outdated token config json format with excluded tokens.
func (testSuite *TokensTestSuite) TestUnmarshalOldTokenConfigExcluded() {
contents := `{
"Mainnet" : {
"0x4200000000000000000000000000000000000042": true,
"0x7f5c764cbc14f9669b88837ca1490cca17c31607": false
},
"Testnet" : {
"0xda10009cbd5d07dd0cecc66161fc93d7c9000da1": true
},
"Goerli" : {
"0x7f5c764cbc14f9669b88837ca1490cca17c31607": false
}
}`
c, err := config.UnmarshalTokenConfig(contents)
testSuite.NoError(err)
testSuite.Equal([]SdkConfiguration.Token{
{
ChainID: 1,
Address: "0x4200000000000000000000000000000000000042",
},
{
ChainID: 420,
Address: "0xda10009cbd5d07dd0cecc66161fc93d7c9000da1",
},
}, c)
}

// TestUnmarshalTokenConfig tests unmarshalling the token config json format.
func (testSuite *TokensTestSuite) TestUnmarshalTokenConfig() {
contents := `[
Expand Down

0 comments on commit 6960956

Please sign in to comment.