Skip to content

Commit

Permalink
Merge pull request #262 from balancer-labs/develop
Browse files Browse the repository at this point in the history
Release 0.1.44
  • Loading branch information
johngrantuk authored Jan 10, 2023
2 parents f53bb7d + b196435 commit f1ae90c
Show file tree
Hide file tree
Showing 34 changed files with 391 additions and 92 deletions.
21 changes: 21 additions & 0 deletions balancer-js/examples/pools/emissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Display weekly BAL emissiosn for a pool
* Run command: yarn examples:run ./examples/pools/emissions.ts
*/
import { BalancerSDK } from '@/.'

const sdk = new BalancerSDK({
network: 1,
rpcUrl: 'https://rpc.ankr.com/eth',
})

const { pools } = sdk

const main = async () => {
if (pools.emissionsService) {
const emissions = await pools.emissionsService.weekly('0x334c96d792e4b26b841d28f53235281cec1be1f200020000000000000000038a')
console.log(emissions)
}
}

main()
26 changes: 8 additions & 18 deletions balancer-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@balancer-labs/sdk",
"version": "0.1.43",
"version": "0.1.44",
"description": "JavaScript SDK for interacting with the Balancer Protocol V2",
"license": "GPL-3.0-only",
"homepage": "https://github.com/balancer-labs/balancer-sdk#readme",
Expand Down Expand Up @@ -32,14 +32,6 @@
"node:goerli": "npx hardhat --tsconfig tsconfig.testing.json --config hardhat.config.goerli.ts node --fork $(grep ALCHEMY_URL_GOERLI .env | cut -d '=' -f2 | tail -1) --port 8000"
},
"devDependencies": {
"@ethersproject/abi": "^5.4.0",
"@ethersproject/abstract-signer": "^5.4.0",
"@ethersproject/address": "^5.4.0",
"@ethersproject/bignumber": "^5.4.0",
"@ethersproject/bytes": "^5.4.0",
"@ethersproject/constants": "^5.4.0",
"@ethersproject/contracts": "^5.4.0",
"@ethersproject/providers": "^5.4.5",
"@ethersproject/solidity": "^5.6.1",
"@ethersproject/units": "^5.7.0",
"@ethersproject/wallet": "^5.5.0",
Expand Down Expand Up @@ -89,22 +81,20 @@
"typescript": "^4.0.2"
},
"dependencies": {
"@balancer-labs/sor": "^4.0.1-beta.16",
"@balancer-labs/sor": "^4.0.1-beta.18",
"@balancer-labs/typechain": "^1.0.0",
"axios": "^0.24.0",
"graphql": "^15.6.1",
"graphql-request": "^3.5.0",
"json-to-graphql-query": "^2.2.4",
"lodash": "^4.17.21"
},
"peerDependencies": {
"@ethersproject/abi": "^5.4.0",
"@ethersproject/abstract-signer": "^5.4.0",
"@ethersproject/address": "^5.4.0",
"@ethersproject/bignumber": "^5.4.0",
"@ethersproject/bytes": "^5.4.0",
"@ethersproject/constants": "^5.4.0",
"@ethersproject/contracts": "^5.4.0",
"@ethersproject/providers": "^5.4.5"
"@ethersproject/providers": "^5.4.5",
"axios": "^0.24.0",
"graphql": "^15.6.1",
"graphql-request": "^3.5.0",
"json-to-graphql-query": "^2.2.4",
"lodash": "^4.17.21"
}
}
3 changes: 1 addition & 2 deletions balancer-js/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import dts from 'rollup-plugin-dts';
import pkg from './package.json';

const external = [
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies),
...Object.keys(pkg.dependencies)
];

export default [
Expand Down
20 changes: 20 additions & 0 deletions balancer-js/src/lib/constants/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,26 @@ export const BALANCER_NETWORK_CONFIG: Record<Network, BalancerNetworkConfig> = {
},
pools: {},
},
[Network.GNOSIS]: {
chainId: Network.GNOSIS, //100
addresses: {
contracts: {
vault: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
multicall: '0xb5b692a88bdfc81ca69dcb1d924f59f0413a602a',
relayerV4: '0xeF606F58A4FD0fCcb066c6203d0994694d3eB2D3',
balancerHelpers: '0x8E9aa87E45e92bad84D5F8DD1bff34Fb92637dE9',
},
tokens: {
wrappedNativeAsset: '0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d',
},
},
urls: {
subgraph:
'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-gnosis-chain-v2',
gaugesSubgraph: '',
},
pools: {},
},
};

export const networkAddresses = (
Expand Down
1 change: 1 addition & 0 deletions balancer-js/src/lib/constants/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum Network {
GÖRLI = 5,
OPTIMISM = 10,
KOVAN = 42,
GNOSIS = 100,
POLYGON = 137,
ARBITRUM = 42161,
}
24 changes: 22 additions & 2 deletions balancer-js/src/modules/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ export * from './protocol-fees/provider';
export * from './token-yields/repository';
export * from './block-number';

import { BalancerNetworkConfig, BalancerDataRepositories } from '@/types';
import {
BalancerNetworkConfig,
BalancerDataRepositories,
GraphQLQuery,
} from '@/types';
import { PoolsSubgraphRepository } from './pool/subgraph';
import { PoolSharesRepository } from './pool-shares/repository';
import { PoolJoinExitRepository } from './pool-joinExit/repository';
import { PoolsSubgraphOnChainRepository } from './pool/subgraphOnChain';
import { PoolGaugesRepository } from './pool-gauges/repository';
import { GaugeSharesRepository } from './gauge-shares/repository';
import { BlockNumberRepository } from './block-number';
Expand All @@ -43,6 +48,7 @@ import { SubgraphPriceRepository } from './token-prices/subgraph';

export class Data implements BalancerDataRepositories {
pools;
poolsOnChain;
yesterdaysPools;
poolShares;
poolGauges;
Expand All @@ -58,12 +64,25 @@ export class Data implements BalancerDataRepositories {
blockNumbers;
poolJoinExits;

constructor(networkConfig: BalancerNetworkConfig, provider: Provider) {
constructor(
networkConfig: BalancerNetworkConfig,
provider: Provider,
subgraphQuery?: GraphQLQuery
) {
this.pools = new PoolsSubgraphRepository({
url: networkConfig.urls.subgraph,
chainId: networkConfig.chainId,
});

this.poolsOnChain = new PoolsSubgraphOnChainRepository({
url: networkConfig.urls.subgraph,
chainId: networkConfig.chainId,
provider: provider,
multicall: networkConfig.addresses.contracts.multicall,
vault: networkConfig.addresses.contracts.vault,
query: subgraphQuery,
});

this.poolShares = new PoolSharesRepository(
networkConfig.urls.subgraph,
networkConfig.chainId
Expand Down Expand Up @@ -116,6 +135,7 @@ export class Data implements BalancerDataRepositories {
);

const subgraphPriceRepository = new SubgraphPriceRepository(
networkConfig.urls.subgraph,
networkConfig.chainId
);

Expand Down
1 change: 1 addition & 0 deletions balancer-js/src/modules/data/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './balancer-api';
export * from './fallback';
export * from './static';
export * from './subgraph';
export * from './subgraphOnChain';
2 changes: 1 addition & 1 deletion balancer-js/src/modules/data/pool/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class PoolsSubgraphRepository
eq: true,
},
totalShares: {
gt: 0,
gt: 0.000000000001,
},
},
};
Expand Down
112 changes: 112 additions & 0 deletions balancer-js/src/modules/data/pool/subgraphOnChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Findable, Searchable } from '../types';
import { Provider } from '@ethersproject/providers';
import { PoolAttribute, PoolsRepositoryFetchOptions } from './types';
import { GraphQLQuery, Pool } from '@/types';
import { Network } from '@/lib/constants/network';
import { getOnChainBalances } from '../../../modules/sor/pool-data/onChainData';
import { PoolsSubgraphRepository } from './subgraph';

interface PoolsSubgraphOnChainRepositoryOptions {
url: string;
chainId: Network;
provider: Provider;
multicall: string;
vault: string;
blockHeight?: () => Promise<number | undefined>;
query?: GraphQLQuery;
}

/**
* Access pools using generated subgraph client and multicall.
*/
export class PoolsSubgraphOnChainRepository
implements Findable<Pool, PoolAttribute>, Searchable<Pool>
{
private poolsSubgraph: PoolsSubgraphRepository;
private provider: Provider;
private pools?: Promise<Pool[]>;
private multicall: string;
private vault: string;
public skip = 0;

/**
* Repository using multicall to get onchain data.
*
* @param url subgraph URL
* @param chainId current network, needed for L2s logic
* @param blockHeight lazy loading blockHeigh resolver
* @param multicall multicall address
* @param valt vault address
*/
constructor(options: PoolsSubgraphOnChainRepositoryOptions) {
this.poolsSubgraph = new PoolsSubgraphRepository({
url: options.url,
chainId: options.chainId,
blockHeight: options.blockHeight,
query: options.query,
});
this.provider = options.provider;
this.multicall = options.multicall;
this.vault = options.vault;
}

/**
* We need a list of all the pools, for calculating APRs (nested pools), and for SOR (path finding).
* All the pools are fetched on page load and cachced for speedy lookups.
*
* @returns Promise resolving to pools list
*/
private async fetchDefault(): Promise<Pool[]> {
console.time('fetching pools SG');
const pools = await this.poolsSubgraph.fetch();
console.timeEnd('fetching pools SG');
console.time('fetching pools onchain');
const onchainPools = await getOnChainBalances(
pools,
this.multicall,
this.vault,
this.provider
);
console.timeEnd('fetching pools onchain');

return onchainPools;
}

async fetch(options?: PoolsRepositoryFetchOptions): Promise<Pool[]> {
const pools = await this.poolsSubgraph.fetch(options);
const onchainPools = await getOnChainBalances(
pools,
this.multicall,
this.vault,
this.provider
);
return onchainPools;
}

async find(id: string): Promise<Pool | undefined> {
return await this.findBy('id', id);
}

async findBy(param: PoolAttribute, value: string): Promise<Pool | undefined> {
if (!this.pools) {
this.pools = this.fetchDefault();
}

return (await this.pools).find((pool) => pool[param] == value);
}

async all(): Promise<Pool[]> {
if (!this.pools) {
this.pools = this.fetchDefault();
}
return this.pools;
}

async where(filter: (pool: Pool) => boolean): Promise<Pool[]> {
if (!this.pools) {
this.pools = this.fetchDefault();
}

return (await this.pools).filter(filter);
}
}
2 changes: 1 addition & 1 deletion balancer-js/src/modules/data/token-prices/subgraph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const mockedResponse = {

const addresses = mockedResponse.data.tokens.map((t) => t.address);

const repository = new SubgraphPriceRepository(1);
const repository = new SubgraphPriceRepository(url, 1);

describe('subgraph price repository', () => {
let mock: MockAdapter;
Expand Down
5 changes: 1 addition & 4 deletions balancer-js/src/modules/data/token-prices/subgraph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { Price, Findable, TokenPrices, Network } from '@/types';
import axios from 'axios';
import { BALANCER_NETWORK_CONFIG } from '@/lib/constants/config';
import { Debouncer, tokenAddressForPricing } from '@/lib/utils';

interface SubgraphPricesResponse {
Expand All @@ -16,12 +15,10 @@ interface SubgraphPricesResponse {
}

export class SubgraphPriceRepository implements Findable<Price> {
private subgraphUrl: string;
prices: { [key: string]: Promise<Price> } = {};
debouncer: Debouncer<TokenPrices, string>;

constructor(private chainId: Network = 1) {
this.subgraphUrl = BALANCER_NETWORK_CONFIG[chainId].urls.subgraph;
constructor(private subgraphUrl: string, private chainId: Network = 1) {
this.debouncer = new Debouncer<TokenPrices, string>(
this.fetch.bind(this),
200
Expand Down
1 change: 1 addition & 0 deletions balancer-js/src/modules/data/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

export { LiquidityGauge } from './liquidity-gauges/provider';
export { RewardData } from './liquidity-gauges/multicall';
export { PoolAttribute } from './pool/types';
export { TokenAttribute } from './token/types';
export { ProtocolFees } from './protocol-fees/provider';
Expand Down
Loading

0 comments on commit f1ae90c

Please sign in to comment.