-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from balancer-labs/develop
Release 0.1.27
- Loading branch information
Showing
17 changed files
with
218 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { BalancerSDK } from '../../src'; | ||
|
||
const sdk = new BalancerSDK({ | ||
network: 1, | ||
rpcUrl: 'https://eth-rpc.gateway.pokt.network', | ||
}); | ||
|
||
const { pools } = sdk; | ||
|
||
const main = async () => { | ||
[ | ||
'0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d', | ||
].forEach(async (poolId) => { | ||
const pool = await pools.find(poolId); | ||
if (pool) { | ||
const liquidity = await pools.liquidity(pool); | ||
console.log(pool.totalShares, pool.totalLiquidity, liquidity); | ||
} | ||
}); | ||
}; | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Interface } from '@ethersproject/abi'; | ||
import { Contract } from '@ethersproject/contracts'; | ||
import { Provider } from '@ethersproject/providers'; | ||
import { formatUnits } from '@ethersproject/units'; | ||
import { Multicall } from '@/modules/contracts/multicall'; | ||
import { yieldTokens } from '../token-yields/tokens/aave'; | ||
|
||
const wrappedATokenInterface = new Interface([ | ||
'function rate() view returns (uint256)', | ||
]); | ||
|
||
export class AaveRates { | ||
multicall: Contract; | ||
rates?: Promise<{ [wrappedATokenAddress: string]: number }>; | ||
|
||
constructor(multicallAddress: string, provider: Provider) { | ||
this.multicall = Multicall(multicallAddress, provider); | ||
} | ||
|
||
private async fetch(): Promise<{ [wrappedATokenAddress: string]: number }> { | ||
console.time('Fetching aave rates'); | ||
const addresses = Object.values(yieldTokens); | ||
const payload = addresses.map((wrappedATokenAddress) => [ | ||
wrappedATokenAddress, | ||
wrappedATokenInterface.encodeFunctionData('rate', []), | ||
]); | ||
const [, res] = await this.multicall.aggregate(payload); | ||
|
||
const rates = addresses.reduce((p: { [key: string]: number }, a, i) => { | ||
p[a] ||= res[i] == '0x' ? 0 : parseFloat(formatUnits(res[i], 27)); | ||
return p; | ||
}, {}); | ||
console.timeEnd('Fetching aave rates'); | ||
|
||
return rates; | ||
} | ||
|
||
async getRate(wrappedAToken: string): Promise<number> { | ||
if (!this.rates) { | ||
this.rates = this.fetch(); | ||
} | ||
|
||
return (await this.rates)[wrappedAToken]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './types'; | ||
export * from './static'; | ||
export * from './coingecko'; | ||
export * from './provider'; | ||
export * from './aave-rates'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Findable, Price } from '@/types'; | ||
import { AaveRates } from './aave-rates'; | ||
import { CoingeckoPriceRepository } from './coingecko'; | ||
|
||
export class TokenPriceProvider implements Findable<Price> { | ||
constructor( | ||
private coingeckoRepository: CoingeckoPriceRepository, | ||
private aaveRates: AaveRates | ||
) {} | ||
|
||
async find(address: string): Promise<Price | undefined> { | ||
const price = await this.coingeckoRepository.find(address); | ||
const rate = (await this.aaveRates.getRate(address)) || 1; | ||
if (price && price.usd) { | ||
return { | ||
...price, | ||
usd: (parseFloat(price.usd) * rate).toString(), | ||
}; | ||
} else { | ||
return price; | ||
} | ||
} | ||
|
||
async findBy(attribute: string, value: string): Promise<Price | undefined> { | ||
if (attribute === 'address') { | ||
return this.find(value); | ||
} else { | ||
throw `Token price search by ${attribute} not implemented`; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { TokenPrices } from '@/types'; | ||
import { expect } from 'chai'; | ||
import { StaticTokenPriceProvider } from './static'; | ||
|
||
const TOKENS = { | ||
BAL: '0x9a71012B13CA4d3D0Cdc72A177DF3ef03b0E76A3', | ||
WMATIC: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', | ||
}; | ||
|
||
let staticTokenPriceProvider: StaticTokenPriceProvider; | ||
|
||
describe('static token prices repository', () => { | ||
it('Should store token addresses as lower case internally', async () => { | ||
const tokenPrices: TokenPrices = { | ||
[TOKENS.BAL]: { | ||
usd: '10', | ||
}, | ||
}; | ||
staticTokenPriceProvider = new StaticTokenPriceProvider(tokenPrices); | ||
expect( | ||
await staticTokenPriceProvider.find(TOKENS.BAL.toLowerCase()) | ||
).to.deep.eq({ | ||
usd: '10', | ||
}); | ||
}); | ||
|
||
it('When finding by upper case address it converts to lower case', async () => { | ||
const tokenPrices: TokenPrices = { | ||
[TOKENS.BAL.toLowerCase()]: { | ||
usd: '10', | ||
}, | ||
}; | ||
staticTokenPriceProvider = new StaticTokenPriceProvider(tokenPrices); | ||
expect(await staticTokenPriceProvider.find(TOKENS.BAL)).to.deep.eq({ | ||
usd: '10', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.