Skip to content

Commit 8e1d267

Browse files
Merge pull request #1170 from moonstream-to/update-balances-structure
New balances structure.
2 parents 59a1d14 + 6e6b077 commit 8e1d267

File tree

3 files changed

+81
-34
lines changed

3 files changed

+81
-34
lines changed

nodebalancer/README.md

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,38 @@ The endpoint returns a JSON object with the following structure:
143143

144144
```json
145145
{
146-
"ethereum": {
147-
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2": "1000000000000000000",
148-
"0x6B175474E89094C44Da98b954EedeAC495271d0F": "2000000000000000000",
149-
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48": "3000000000000000"
150-
},
151-
"polygon": {
152-
"0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270": "4000000000000000000",
153-
"0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174": "5000000000000000",
154-
"0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063": "6000000000000000000"
155-
}
146+
"1": {
147+
"chain_id": "1",
148+
"name": "ethereum",
149+
"image_url": "https://example.com/eth.png",
150+
"balances": {
151+
"0x0000000000000000000000000000000000000000": "1000000000000000000",
152+
"0xdac17f958d2ee523a2206206994597c13d831ec7": "2000000000000000000",
153+
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48": "3000000000000000"
154+
}
155+
},
156+
"137": {
157+
"chain_id": "137",
158+
"name": "polygon",
159+
"image_url": "https://example.com/matic.png",
160+
"balances": {
161+
"0x0000000000000000000000000000000000000000": "4000000000000000000",
162+
"0x2791bca1f2de4661ed88a30c99a7a9449aa84174": "5000000000000000",
163+
"0xc2132d05d31c914a87c6611c10748aeb04b58e8f": "6000000000000000000"
164+
}
165+
}
156166
}
157167
```
158168

159169
Where:
160-
- Each blockchain object maps token contract addresses to their respective balances
161-
- All balances are returned as strings in the token's smallest unit (e.g., wei for ETH)
170+
- The top-level keys are chain IDs (e.g. "1" for Ethereum, "137" for Polygon)
171+
- Each chain object contains:
172+
- `chain_id`: The chain identifier as a string
173+
- `name`: The human-readable name of the chain
174+
- `image_url`: URL to the chain's logo/image
175+
- `balances`: Map of token addresses to their balances
176+
- Native token (ETH, MATIC etc) is represented by the zero address: `0x0000000000000000000000000000000000000000`
177+
- All balances are returned as strings in the token's smallest unit (e.g., wei for ETH)
162178

163179
### Features
164180

@@ -172,27 +188,43 @@ Where:
172188
curl "http://localhost:8080/balances?address=0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
173189
```
174190

175-
### Contracts Config
191+
### Contracts Config Structure
192+
193+
The `contracts.json` file should follow this structure:
194+
176195
```json
177196
{
178-
"ethereum": {
179-
"multicall3": "0xcA11bde05977b3631167028862bE2a173976CA11",
180-
"tokens": {
181-
"WETH": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
182-
"DAI": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
183-
"USDC": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
184-
"USDT": "0xdAC17F958D2ee523a2206206994597C13D831ec7"
185-
}
186-
},
187-
"polygon": {
188-
"multicall3": "0xcA11bde05977b3631167028862bE2a173976CA11",
189-
"tokens": {
190-
"WMATIC": "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270",
191-
"USDC": "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
192-
"DAI": "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063",
193-
"WETH": "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619",
194-
"USDT": "0xc2132D05D31c914a87C6611C10748AEb04B58e8F"
195-
}
197+
"ethereum": {
198+
"multicall3": "0xcA11bde05977b3631167028862bE2a173976CA11",
199+
"chain_id": "1",
200+
"name": "Ethereum",
201+
"image_url": "https://example.com/eth.png",
202+
"native_token": "ETH",
203+
"tokens": {
204+
"0xdac17f958d2ee523a2206206994597c13d831ec7": "USDT",
205+
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48": "USDC"
206+
}
207+
},
208+
"polygon": {
209+
"multicall3": "0xcA11bde05977b3631167028862bE2a173976CA11",
210+
"chain_id": "137",
211+
"name": "Polygon",
212+
"image_url": "https://example.com/matic.png",
213+
"native_token": "MATIC",
214+
"tokens": {
215+
"0x2791bca1f2de4661ed88a30c99a7a9449aa84174": "USDC",
216+
"0xc2132d05d31c914a87c6611c10748aeb04b58e8f": "USDT"
196217
}
218+
}
197219
}
198220
```
221+
222+
Where:
223+
- Top-level keys are blockchain identifiers used internally
224+
- Each chain configuration contains:
225+
- `multicall3`: Address of the Multicall3 contract on that chain
226+
- `chain_id`: The chain identifier (e.g. "1" for Ethereum)
227+
- `name`: Human-readable name of the chain
228+
- `image_url`: URL to the chain's logo/image
229+
- `native_token`: Symbol for the chain's native token (ETH, MATIC etc)
230+
- `tokens`: Map of token addresses to their symbols

nodebalancer/cmd/nodebalancer/balances.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ type TokenBalance struct {
2222

2323
type ChainBalances map[string]string
2424

25+
type ChainInfo struct {
26+
ChainID string `json:"chain_id"`
27+
Name string `json:"name"`
28+
ImageURL string `json:"image_url"`
29+
Balances ChainBalances `json:"balances"`
30+
}
31+
2532
// Map of blockchain -> token balances
26-
type BalancesResponse map[string]ChainBalances
33+
type BalancesResponse map[string]ChainInfo
2734

2835
type chainResult struct {
2936
blockchain string
@@ -192,7 +199,12 @@ func getBalances(ctx context.Context, address string) (BalancesResponse, error)
192199
continue
193200
}
194201
if len(result.balances) > 0 {
195-
response[result.blockchain] = result.balances
202+
response[contractsConfig[result.blockchain].ChainID] = ChainInfo{
203+
ChainID: contractsConfig[result.blockchain].ChainID,
204+
Name: result.blockchain,
205+
ImageURL: contractsConfig[result.blockchain].ImageURL,
206+
Balances: result.balances,
207+
}
196208
}
197209
}
198210

nodebalancer/cmd/nodebalancer/configs.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
// TODO(kompotkot): Find out why it cuts out the port
3232
BUGOUT_BROOD_URL = "https://auth.bugout.dev"
3333
// BUGOUT_BROOD_URL = os.Getenv("BUGOUT_BROOD_URL")
34-
NB_BUGOUT_TIMEOUT_SECONDS = 10
34+
NB_BUGOUT_TIMEOUT_SECONDS = 10
3535
NB_BUGOUT_TIMEOUT_SECONDS_RAW = os.Getenv("NB_BUGOUT_TIMEOUT_SECONDS")
3636

3737
// Bugout and application configuration
@@ -180,6 +180,9 @@ type ChainConfig struct {
180180
Multicall3 string `json:"multicall3"`
181181
Tokens TokenConfig `json:"tokens"`
182182
NativeToken string `json:"native_token"`
183+
ChainID string `json:"chain_id"`
184+
Name string `json:"name"`
185+
ImageURL string `json:"image_url"`
183186
}
184187

185188
type ContractsConfig map[string]ChainConfig

0 commit comments

Comments
 (0)