Skip to content

Commit

Permalink
Fix logic for fetching whitelistTokens for GMX-based dexés (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunspirit99 authored Dec 17, 2024
1 parent fb49260 commit f4f81bc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
5 changes: 3 additions & 2 deletions pkg/source/gmx-glp/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ const (
vaultMethodTaxBasisPoints = "taxBasisPoints"
vaultMethodTotalTokenWeights = "totalTokenWeights"
vaultMethodUSDG = "usdg"
vaultMethodWhitelistedTokenCount = "whitelistedTokenCount"
vaultMethodMintBurnFeeBasisPoints = "mintBurnFeeBasisPoints"

vaultMethodAllWhitelistedTokens = "allWhitelistedTokens"
vaultMethodAllWhitelistedTokensLength = "allWhitelistedTokensLength"
vaultMethodAllWhitelistedTokens = "allWhitelistedTokens"
vaultMethodWhitelistedTokens = "whitelistedTokens"

vaultMethodPoolAmounts = "poolAmounts"
vaultMethodBufferAmounts = "bufferAmounts"
Expand Down
35 changes: 27 additions & 8 deletions pkg/source/gmx-glp/vault_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *VaultReader) readData(ctx context.Context, address string, vault *Vault
rpcRequest.AddCall(callParamsFactory(vaultMethodTaxBasisPoints, nil), []interface{}{&vault.TaxBasisPoints})
rpcRequest.AddCall(callParamsFactory(vaultMethodTotalTokenWeights, nil), []interface{}{&vault.TotalTokenWeights})
rpcRequest.AddCall(callParamsFactory(vaultMethodUSDG, nil), []interface{}{&vault.USDGAddress})
rpcRequest.AddCall(callParamsFactory(vaultMethodWhitelistedTokenCount, nil), []interface{}{&vault.WhitelistedTokensCount})
rpcRequest.AddCall(callParamsFactory(vaultMethodAllWhitelistedTokensLength, nil), []interface{}{&vault.WhitelistedTokensCount})
rpcRequest.AddCall(callParamsFactory(vaultMethodMintBurnFeeBasisPoints, nil), []interface{}{&vault.MintBurnFeeBasicPoints})

_, err := rpcRequest.TryAggregate()
Expand All @@ -92,7 +92,7 @@ func (r *VaultReader) readWhitelistedTokens(
) error {
tokensLen := int(vault.WhitelistedTokensCount.Int64())

whitelistedTokens := make([]common.Address, tokensLen)
tokenList := make([]common.Address, tokensLen)
rpcRequest := r.ethrpcClient.NewRequest().SetContext(ctx)

for i := 0; i < tokensLen; i++ {
Expand All @@ -101,18 +101,37 @@ func (r *VaultReader) readWhitelistedTokens(
Target: address,
Method: vaultMethodAllWhitelistedTokens,
Params: []interface{}{new(big.Int).SetInt64(int64(i))},
}, []interface{}{&whitelistedTokens[i]})
}, []interface{}{&tokenList[i]})
}
if _, err := rpcRequest.TryAggregate(); err != nil {
res, err := rpcRequest.TryAggregate()
if err != nil {
return err
}

isWhitelistedTokens := make([]bool, len(tokenList))
rpcRequest = r.ethrpcClient.NewRequest().SetContext(ctx).SetBlockNumber(res.BlockNumber)

for i := 0; i < len(tokenList); i++ {
rpcRequest.AddCall(&ethrpc.Call{
ABI: r.abi,
Target: address,
Method: vaultMethodWhitelistedTokens,
Params: []interface{}{tokenList[i]},
}, []interface{}{&isWhitelistedTokens[i]})
}
_, err = rpcRequest.TryAggregate()
if err != nil {
return err
}

tokens := make([]string, tokensLen)
for i := range whitelistedTokens {
tokens[i] = strings.ToLower(whitelistedTokens[i].String())
currentWhiteListTokens := make([]string, 0, tokensLen)
for i := range tokenList {
if isWhitelistedTokens[i] {
currentWhiteListTokens = append(currentWhiteListTokens, strings.ToLower(tokenList[i].String()))
}
}

vault.WhitelistedTokens = tokens
vault.WhitelistedTokens = currentWhiteListTokens

return nil
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/source/gmx/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ const (
vaultMethodTaxBasisPoints = "taxBasisPoints"
vaultMethodTotalTokenWeights = "totalTokenWeights"
vaultMethodUSDG = "usdg"
vaultMethodWhitelistedTokenCount = "whitelistedTokenCount"

vaultMethodAllWhitelistedTokens = "allWhitelistedTokens"
vaultMethodAllWhitelistedTokensLength = "allWhitelistedTokensLength"
vaultMethodAllWhitelistedTokens = "allWhitelistedTokens"
vaultMethodWhitelistedTokens = "whitelistedTokens"

vaultMethodPoolAmounts = "poolAmounts"
vaultMethodBufferAmounts = "bufferAmounts"
Expand Down
35 changes: 27 additions & 8 deletions pkg/source/gmx/vault_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *VaultReader) readData(ctx context.Context, address string, vault *Vault
rpcRequest.AddCall(callParamsFactory(vaultMethodTaxBasisPoints, nil), []interface{}{&vault.TaxBasisPoints})
rpcRequest.AddCall(callParamsFactory(vaultMethodTotalTokenWeights, nil), []interface{}{&vault.TotalTokenWeights})
rpcRequest.AddCall(callParamsFactory(vaultMethodUSDG, nil), []interface{}{&vault.USDGAddress})
rpcRequest.AddCall(callParamsFactory(vaultMethodWhitelistedTokenCount, nil), []interface{}{&vault.WhitelistedTokensCount})
rpcRequest.AddCall(callParamsFactory(vaultMethodAllWhitelistedTokensLength, nil), []interface{}{&vault.WhitelistedTokensCount})

_, err := rpcRequest.TryAggregate()

Expand All @@ -91,7 +91,7 @@ func (r *VaultReader) readWhitelistedTokens(
) error {
tokensLen := int(vault.WhitelistedTokensCount.Int64())

whitelistedTokens := make([]common.Address, tokensLen)
tokenList := make([]common.Address, tokensLen)
rpcRequest := r.ethrpcClient.NewRequest().SetContext(ctx)

for i := 0; i < tokensLen; i++ {
Expand All @@ -100,18 +100,37 @@ func (r *VaultReader) readWhitelistedTokens(
Target: address,
Method: vaultMethodAllWhitelistedTokens,
Params: []interface{}{new(big.Int).SetInt64(int64(i))},
}, []interface{}{&whitelistedTokens[i]})
}, []interface{}{&tokenList[i]})
}
if _, err := rpcRequest.TryAggregate(); err != nil {
res, err := rpcRequest.TryAggregate()
if err != nil {
return err
}

isWhitelistedTokens := make([]bool, len(tokenList))
rpcRequest = r.ethrpcClient.NewRequest().SetContext(ctx).SetBlockNumber(res.BlockNumber)

for i := 0; i < len(tokenList); i++ {
rpcRequest.AddCall(&ethrpc.Call{
ABI: r.abi,
Target: address,
Method: vaultMethodWhitelistedTokens,
Params: []interface{}{tokenList[i]},
}, []interface{}{&isWhitelistedTokens[i]})
}
_, err = rpcRequest.TryAggregate()
if err != nil {
return err
}

tokens := make([]string, tokensLen)
for i := range whitelistedTokens {
tokens[i] = strings.ToLower(whitelistedTokens[i].String())
currentWhiteListTokens := make([]string, 0, tokensLen)
for i := range tokenList {
if isWhitelistedTokens[i] {
currentWhiteListTokens = append(currentWhiteListTokens, strings.ToLower(tokenList[i].String()))
}
}

vault.WhitelistedTokens = tokens
vault.WhitelistedTokens = currentWhiteListTokens

return nil
}
Expand Down

0 comments on commit f4f81bc

Please sign in to comment.