Skip to content

Commit 7230a5b

Browse files
authored
Merge pull request #173 from balancer-labs/develop
Version 0.1.28
2 parents b6e2abe + 57f47cb commit 7230a5b

File tree

15 files changed

+261
-295
lines changed

15 files changed

+261
-295
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Display APRs for pool ids hardcoded under `const ids`
3+
* Run command: yarn examples:run ./examples/pools/aprs.arbitrum.ts
4+
*/
5+
import dotenv from 'dotenv';
6+
import { BalancerSDK } from '../../src/modules/sdk.module';
7+
8+
dotenv.config();
9+
10+
const sdk = new BalancerSDK({
11+
network: 42161,
12+
rpcUrl: process.env.ARBITRUM_RPC_URL || 'https://rpc.ankr.com/arbitrum', // WARNING: ankr fails for multicall
13+
});
14+
15+
const { pools } = sdk;
16+
17+
const main = async () => {
18+
const pool = await pools.find(
19+
'0xfb5e6d0c1dfed2ba000fbc040ab8df3615ac329c000000000000000000000159'
20+
);
21+
22+
if (pool) {
23+
const apr = await pools.apr(pool);
24+
console.log(pool.id, apr);
25+
}
26+
};
27+
28+
main();

balancer-js/examples/pools/liquidity.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ const sdk = new BalancerSDK({
77

88
const { pools } = sdk;
99

10-
const main = async () => {
10+
(() => {
1111
[
1212
'0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d',
13+
// '0x2f4eb100552ef93840d5adc30560e5513dfffacb000000000000000000000334',
1314
].forEach(async (poolId) => {
1415
const pool = await pools.find(poolId);
1516
if (pool) {
1617
const liquidity = await pools.liquidity(pool);
17-
console.log(pool.totalShares, pool.totalLiquidity, liquidity);
18+
console.table([
19+
{
20+
totalShares: pool.totalShares,
21+
liquidity: liquidity,
22+
bptPrice: parseFloat(pool.totalShares) / parseFloat(liquidity),
23+
},
24+
]);
1825
}
1926
});
20-
};
21-
22-
main();
27+
})();

balancer-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@balancer-labs/sdk",
3-
"version": "0.1.27",
3+
"version": "0.1.28",
44
"description": "JavaScript SDK for interacting with the Balancer Protocol V2",
55
"license": "GPL-3.0-only",
66
"homepage": "https://github.com/balancer-labs/balancer-sdk/balancer-js#readme",

balancer-js/src/modules/data/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export class Data implements BalancerDataRepositories {
7979

8080
const aaveRates = new AaveRates(
8181
networkConfig.addresses.contracts.multicall,
82-
provider
82+
provider,
83+
networkConfig.chainId
8384
);
8485

8586
this.tokenPrices = new TokenPriceProvider(coingeckoRepository, aaveRates);
@@ -125,6 +126,6 @@ export class Data implements BalancerDataRepositories {
125126
);
126127
}
127128

128-
this.tokenYields = new TokenYieldsRepository();
129+
this.tokenYields = new TokenYieldsRepository(networkConfig.chainId);
129130
}
130131
}

balancer-js/src/modules/data/token-prices/aave-rates.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Provider } from '@ethersproject/providers';
44
import { formatUnits } from '@ethersproject/units';
55
import { Multicall } from '@/modules/contracts/multicall';
66
import { yieldTokens } from '../token-yields/tokens/aave';
7+
import { Network } from '@/types';
78

89
const wrappedATokenInterface = new Interface([
910
'function rate() view returns (uint256)',
@@ -13,13 +14,19 @@ export class AaveRates {
1314
multicall: Contract;
1415
rates?: Promise<{ [wrappedATokenAddress: string]: number }>;
1516

16-
constructor(multicallAddress: string, provider: Provider) {
17+
constructor(
18+
multicallAddress: string,
19+
provider: Provider,
20+
private network: Network
21+
) {
1722
this.multicall = Multicall(multicallAddress, provider);
1823
}
1924

20-
private async fetch(): Promise<{ [wrappedATokenAddress: string]: number }> {
25+
private async fetch(
26+
network: Network.MAINNET | Network.POLYGON
27+
): Promise<{ [wrappedATokenAddress: string]: number }> {
2128
console.time('Fetching aave rates');
22-
const addresses = Object.values(yieldTokens);
29+
const addresses = Object.values(yieldTokens[network]);
2330
const payload = addresses.map((wrappedATokenAddress) => [
2431
wrappedATokenAddress,
2532
wrappedATokenInterface.encodeFunctionData('rate', []),
@@ -36,8 +43,11 @@ export class AaveRates {
3643
}
3744

3845
async getRate(wrappedAToken: string): Promise<number> {
46+
if (this.network != Network.MAINNET && this.network != Network.POLYGON) {
47+
return 1;
48+
}
3949
if (!this.rates) {
40-
this.rates = this.fetch();
50+
this.rates = this.fetch(this.network);
4151
}
4252

4353
return (await this.rates)[wrappedAToken];

balancer-js/src/modules/data/token-prices/coingecko.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-empty-function */
2-
import { Price, Findable, TokenPrices } from '@/types';
2+
import { Price, Findable, TokenPrices, Network } from '@/types';
33
import { wrappedTokensMap as aaveWrappedMap } from '../token-yields/tokens/aave';
44
import axios from 'axios';
55

@@ -37,10 +37,10 @@ export class CoingeckoPriceRepository implements Findable<Price> {
3737
timeout?: ReturnType<typeof setTimeout>;
3838
debounceCancel = (): void => {}; // Allow to cancel mid-flight requests
3939

40-
constructor(tokenAddresses: string[], chainId = 1) {
40+
constructor(tokenAddresses: string[], private chainId: Network = 1) {
4141
this.baseTokenAddresses = tokenAddresses
4242
.map((a) => a.toLowerCase())
43-
.map((a) => unwrapToken(a));
43+
.map((a) => unwrapToken(a, this.chainId));
4444
this.urlBase = `https://api.coingecko.com/api/v3/simple/token_price/${this.platform(
4545
chainId
4646
)}?vs_currencies=usd,eth`;
@@ -97,7 +97,7 @@ export class CoingeckoPriceRepository implements Findable<Price> {
9797

9898
async find(address: string): Promise<Price | undefined> {
9999
const lowercaseAddress = address.toLowerCase();
100-
const unwrapped = unwrapToken(lowercaseAddress);
100+
const unwrapped = unwrapToken(lowercaseAddress, this.chainId);
101101
if (!this.prices[unwrapped]) {
102102
try {
103103
let init = false;
@@ -154,11 +154,14 @@ export class CoingeckoPriceRepository implements Findable<Price> {
154154
}
155155
}
156156

157-
const unwrapToken = (wrappedAddress: string) => {
157+
const unwrapToken = (wrappedAddress: string, chainId: Network) => {
158158
const lowercase = wrappedAddress.toLocaleLowerCase();
159159

160-
if (Object.keys(aaveWrappedMap).includes(lowercase)) {
161-
return aaveWrappedMap[lowercase as keyof typeof aaveWrappedMap].aToken;
160+
const aaveChain = chainId as keyof typeof aaveWrappedMap;
161+
if (Object.keys(aaveWrappedMap[aaveChain]).includes(lowercase)) {
162+
return aaveWrappedMap[aaveChain][
163+
lowercase as keyof typeof aaveWrappedMap[typeof aaveChain]
164+
].aToken;
162165
} else {
163166
return lowercase;
164167
}

balancer-js/src/modules/data/token-yields/repository.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
import { lido, yieldTokens as lidoTokens } from './tokens/lido';
2-
import { aave, yieldTokens as aaveTokens } from './tokens/aave';
2+
import {
3+
lidoPolygon,
4+
yieldTokens as lidoPolygonTokens,
5+
} from './tokens/lido-polygon';
6+
import { aave, allYieldTokens as aaveTokens } from './tokens/aave';
37
import { overnight, yieldTokens as overnightTokens } from './tokens/overnight';
4-
import { Findable } from '../types';
8+
import { Network, Findable } from '@/types';
59

610
/**
711
* Common interface for fetching APR from external sources
812
*
913
* @param address is optional, used when same source, eg: aave has multiple tokens and all of them can be fetched in one call.
1014
*/
1115
export interface AprFetcher {
12-
(): Promise<{ [address: string]: number }>;
16+
(network?: Network): Promise<{ [address: string]: number }>;
1317
}
1418

1519
const yieldSourceMap: { [address: string]: AprFetcher } = Object.fromEntries([
1620
...Object.values(lidoTokens).map((k) => [k, lido]),
21+
...Object.values(lidoPolygonTokens).map((k) => [k, lidoPolygon]),
1722
...Object.values(aaveTokens).map((k) => [k, aave]),
1823
...Object.values(overnightTokens).map((k) => [k, overnight]),
1924
]);
2025

2126
export class TokenYieldsRepository implements Findable<number> {
2227
private yields: { [address: string]: number } = {};
2328

24-
constructor(private sources = yieldSourceMap) {}
29+
constructor(private network: Network, private sources = yieldSourceMap) {}
2530

2631
async fetch(address: string): Promise<void> {
27-
const tokenYields = await this.sources[address]();
32+
const tokenYields = await this.sources[address](this.network);
2833
this.yields = {
2934
...this.yields,
3035
...tokenYields,

balancer-js/src/modules/data/token-yields/tokens/aave.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const mockedResponse = {
77
data: {
88
reserves: [
99
{
10-
underlyingAsset: wrappedTokensMap[yieldTokens.waUSDT].underlying,
10+
underlyingAsset: wrappedTokensMap[1][yieldTokens[1].waUSDT].underlying,
1111
liquidityRate: '16633720952291480781459657',
1212
},
1313
],
@@ -29,7 +29,7 @@ describe('aave apr', () => {
2929
});
3030

3131
it('is getting fetched', async () => {
32-
const apr = (await aave())[yieldTokens.waUSDT];
32+
const apr = (await aave(1))[yieldTokens[1].waUSDT];
3333
expect(apr).to.eq(166);
3434
});
3535
});

0 commit comments

Comments
 (0)