From b62ab16bab92e6fe6b830ef75310953faacc806a Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Thu, 21 Sep 2023 19:26:43 -0300 Subject: [PATCH 01/18] Creating a provider to fetch data from the Balancer API; --- .gitignore | 2 + .../balancer-api-provider/client/index.ts | 22 +++++ .../providers/balancer-api-provider/index.ts | 16 ++++ .../modules/join/index.ts | 88 +++++++++++++++++++ .../modules/join/types.ts | 14 +++ 5 files changed, 142 insertions(+) create mode 100644 src/data/providers/balancer-api-provider/client/index.ts create mode 100644 src/data/providers/balancer-api-provider/index.ts create mode 100644 src/data/providers/balancer-api-provider/modules/join/index.ts create mode 100644 src/data/providers/balancer-api-provider/modules/join/types.ts diff --git a/.gitignore b/.gitignore index 3e819cac..c1308de5 100644 --- a/.gitignore +++ b/.gitignore @@ -80,3 +80,5 @@ typechain-types cache artifacts +#IDE +.idea diff --git a/src/data/providers/balancer-api-provider/client/index.ts b/src/data/providers/balancer-api-provider/client/index.ts new file mode 100644 index 00000000..d1bd5ab9 --- /dev/null +++ b/src/data/providers/balancer-api-provider/client/index.ts @@ -0,0 +1,22 @@ +export class BalancerApiClient { + subgraphUrl: string; + chainId: number; + constructor(subgraphUrl: string, chainId: number) { + this.subgraphUrl = subgraphUrl; + this.chainId = chainId; + } + + async fetch(operationName: string, query: any, variables: any) { + const requestQuery = { + operationName, + query, + variables, + }; + const response = await fetch(this.subgraphUrl, { + method: 'post', + body: JSON.stringify(requestQuery), + headers: { 'Content-Type': 'application/json', ChainId: this.chainId.toString() }, + }); + return response.json(); + } +} diff --git a/src/data/providers/balancer-api-provider/index.ts b/src/data/providers/balancer-api-provider/index.ts new file mode 100644 index 00000000..5468e7d1 --- /dev/null +++ b/src/data/providers/balancer-api-provider/index.ts @@ -0,0 +1,16 @@ +import { JoinData } from "./modules/join"; +import { BalancerApiClient } from "./client"; + +export default class BalancerApi { + + balancerApiClient: BalancerApiClient; + joinData: JoinData; + + + constructor(balancerApiUrl: string, chainId: number){ + this.balancerApiClient = new BalancerApiClient(balancerApiUrl, chainId); + this.joinData = new JoinData(this.balancerApiClient); + } + + +} \ No newline at end of file diff --git a/src/data/providers/balancer-api-provider/modules/join/index.ts b/src/data/providers/balancer-api-provider/modules/join/index.ts new file mode 100644 index 00000000..60a9c8cc --- /dev/null +++ b/src/data/providers/balancer-api-provider/modules/join/index.ts @@ -0,0 +1,88 @@ +import { BalancerApiClient } from '../../client'; +import { PoolState } from './types'; + +export class JoinData { + readonly poolStateQuery = `query GetPool($id: String!){ + poolGetPool(id:$id) { + id + address + name + type + version + ... on GqlPoolWeighted { + tokens { + ... on GqlPoolTokenBase { + address + decimals + index + } + } + } + ... on GqlPoolStable { + tokens { + ... on GqlPoolTokenBase { + address + decimals + index + } + } + } + ... on GqlPoolPhantomStable { + tokens { + ... on GqlPoolTokenBase { + address + decimals + index + } + } + } + ... on GqlPoolGyro { + tokens { + ... on GqlPoolTokenBase { + address + decimals + index + } + } + } + ... on GqlPoolLiquidityBootstrapping { + tokens { + ... on GqlPoolTokenBase { + address + decimals + index + } + } + } + ... on GqlPoolElement { + tokens { + ... on GqlPoolTokenBase { + address + decimals + index + } + } + } + ... on GqlPoolLiquidityBootstrapping { + tokens { + ... on GqlPoolTokenBase { + address + decimals + index + } + } + } + } +}`; + + constructor(private readonly balancerApiClient: BalancerApiClient) {} + + async fetchPoolState(id: string): Promise { + const { + data: { poolGetPool }, + } = await this.balancerApiClient.fetch('GetPool', this.poolStateQuery, { + id, + }); + return poolGetPool; + } +} diff --git a/src/data/providers/balancer-api-provider/modules/join/types.ts b/src/data/providers/balancer-api-provider/modules/join/types.ts new file mode 100644 index 00000000..5b23f242 --- /dev/null +++ b/src/data/providers/balancer-api-provider/modules/join/types.ts @@ -0,0 +1,14 @@ +import { Address } from "viem"; + +export type PoolState = { + id: Address; + address: Address; + type: string; + version: string; + tokens: { + address: Address; + decimals: number; + index: number; + }; +}[]; + From 5702561423d095a7ea1c535597498ec58d5e66ab Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Mon, 25 Sep 2023 10:22:23 -0300 Subject: [PATCH 02/18] Adding Example; Changing variable names; Changing class names; --- README.md | 60 +++++++++++++++++++ .../client/index.ts | 8 +-- .../index.ts | 6 +- .../modules/pool-state}/index.ts | 4 +- .../modules/pool-state}/types.ts | 0 src/entities/join/index.ts | 2 +- src/entities/join/parser.ts | 2 +- test/lib/utils/helper.ts | 2 +- test/weightedJoin.integration.test.ts | 20 +++---- 9 files changed, 82 insertions(+), 22 deletions(-) rename src/data/providers/{balancer-api-provider => balancer-api}/client/index.ts (72%) rename src/data/providers/{balancer-api-provider => balancer-api}/index.ts (69%) rename src/data/providers/{balancer-api-provider/modules/join => balancer-api/modules/pool-state}/index.ts (95%) rename src/data/providers/{balancer-api-provider/modules/join => balancer-api/modules/pool-state}/types.ts (100%) diff --git a/README.md b/README.md index 0c0e32ce..3e79f119 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,63 @@ If your platform does not support one of the required features, it is also possi Testing requires access to an archive node for onchain quote comparisons. This can be done using Infura. `pnpm test` + +## Examples + +### Balancer Api Provider + +Joining with pool state: +```ts + import BalancerApi from "@balancer/sdk/data/providers/balancer-api"; + ... + const joinInput: ProportionalJoinInput = { + bptOut, + chainId, + rpcUrl, + kind: JoinKind.Proportional, + }; + + const balancerApi = new BalancerApi('https://api-v3.balancer.fi/', 1); + const poolState = await balancerApi.pools.fetchSimplePoolState('0x5f1d6874cb1e7156e79a7563d2b61c6cbce03150000200000000000000000586'); + const joinParser = new JoinParser(); + const poolJoin = joinParser.getJoin(poolState.type); + const queryResult = await weightedJoin.query(joinInput, poolState); + +``` + +Exiting with pool state: +```ts +import BalancerApi from "@balancer/sdk/data/providers/balancer-api"; +... +const joinInput: ProportionalJoinInput = { + bptOut, + chainId, + rpcUrl, + kind: JoinKind.Proportional, +}; + +const balancerApi = new BalancerApi('https://api-v3.balancer.fi/', 1); +const poolState = await balancerApi.pools.fetchSimplePoolState('0x5f1d6874cb1e7156e79a7563d2b61c6cbce03150000200000000000000000586'); +const joinParser = new JoinParser(); +const poolJoin = joinParser.getJoin(poolState.type); +const queryResult = await weightedJoin.query(joinInput, poolState); +const slippage = Slippage.fromPercentage('1'); // 1% +const { call, to, value, maxAmountsIn, minBptOut } = + weightedJoin.buildCall({ + ...queryResult, + slippage, + sender, + recipient, + }); +const client = createClient({ + ... +}) + +await client.sendTransaction({ + account, + chain: client.chain, + data, + to, + value, +}); +``` \ No newline at end of file diff --git a/src/data/providers/balancer-api-provider/client/index.ts b/src/data/providers/balancer-api/client/index.ts similarity index 72% rename from src/data/providers/balancer-api-provider/client/index.ts rename to src/data/providers/balancer-api/client/index.ts index d1bd5ab9..40ac1ab3 100644 --- a/src/data/providers/balancer-api-provider/client/index.ts +++ b/src/data/providers/balancer-api/client/index.ts @@ -1,8 +1,8 @@ export class BalancerApiClient { - subgraphUrl: string; + apiUrl: string; chainId: number; - constructor(subgraphUrl: string, chainId: number) { - this.subgraphUrl = subgraphUrl; + constructor(apiUrl: string, chainId: number) { + this.apiUrl = apiUrl; this.chainId = chainId; } @@ -12,7 +12,7 @@ export class BalancerApiClient { query, variables, }; - const response = await fetch(this.subgraphUrl, { + const response = await fetch(this.apiUrl, { method: 'post', body: JSON.stringify(requestQuery), headers: { 'Content-Type': 'application/json', ChainId: this.chainId.toString() }, diff --git a/src/data/providers/balancer-api-provider/index.ts b/src/data/providers/balancer-api/index.ts similarity index 69% rename from src/data/providers/balancer-api-provider/index.ts rename to src/data/providers/balancer-api/index.ts index 5468e7d1..d854c6ba 100644 --- a/src/data/providers/balancer-api-provider/index.ts +++ b/src/data/providers/balancer-api/index.ts @@ -1,15 +1,15 @@ -import { JoinData } from "./modules/join"; +import { Pools } from "./modules/pool-state"; import { BalancerApiClient } from "./client"; export default class BalancerApi { balancerApiClient: BalancerApiClient; - joinData: JoinData; + pools: Pools; constructor(balancerApiUrl: string, chainId: number){ this.balancerApiClient = new BalancerApiClient(balancerApiUrl, chainId); - this.joinData = new JoinData(this.balancerApiClient); + this.pools = new Pools(this.balancerApiClient); } diff --git a/src/data/providers/balancer-api-provider/modules/join/index.ts b/src/data/providers/balancer-api/modules/pool-state/index.ts similarity index 95% rename from src/data/providers/balancer-api-provider/modules/join/index.ts rename to src/data/providers/balancer-api/modules/pool-state/index.ts index 60a9c8cc..b78aa25b 100644 --- a/src/data/providers/balancer-api-provider/modules/join/index.ts +++ b/src/data/providers/balancer-api/modules/pool-state/index.ts @@ -1,7 +1,7 @@ import { BalancerApiClient } from '../../client'; import { PoolState } from './types'; -export class JoinData { +export class Pools { readonly poolStateQuery = `query GetPool($id: String!){ poolGetPool(id:$id) { id @@ -77,7 +77,7 @@ export class JoinData { constructor(private readonly balancerApiClient: BalancerApiClient) {} - async fetchPoolState(id: string): Promise { + async fetchSimplePoolState(id: string): Promise { const { data: { poolGetPool }, } = await this.balancerApiClient.fetch('GetPool', this.poolStateQuery, { diff --git a/src/data/providers/balancer-api-provider/modules/join/types.ts b/src/data/providers/balancer-api/modules/pool-state/types.ts similarity index 100% rename from src/data/providers/balancer-api-provider/modules/join/types.ts rename to src/data/providers/balancer-api/modules/pool-state/types.ts diff --git a/src/entities/join/index.ts b/src/entities/join/index.ts index b968ca7a..bff8b24e 100644 --- a/src/entities/join/index.ts +++ b/src/entities/join/index.ts @@ -45,7 +45,7 @@ export type JoinInput = | SingleAssetJoinInput | ProportionalJoinInput; -// Returned from a join query +// Returned from a pool-state query export type JoinQueryResult = { poolId: Hex; joinKind: JoinKind; diff --git a/src/entities/join/parser.ts b/src/entities/join/parser.ts index 7506e603..c9f81a98 100644 --- a/src/entities/join/parser.ts +++ b/src/entities/join/parser.ts @@ -1,7 +1,7 @@ import { BaseJoin } from '.'; import { WeightedJoin } from './weighted/weightedJoin'; -/*********************** Basic Helper to get join class from pool type *************/ +/*********************** Basic Helper to get pool-state class from pool type *************/ export type JoinConfig = { customPoolJoins: Record; }; diff --git a/test/lib/utils/helper.ts b/test/lib/utils/helper.ts index 9b3ffa22..1571aa18 100644 --- a/test/lib/utils/helper.ts +++ b/test/lib/utils/helper.ts @@ -291,7 +291,7 @@ export const forkSetup = async ( } for (let i = 0; i < tokens.length; i++) { - // Set initial account balance for each token that will be used to join pool + // Set initial account balance for each token that will be used to pool-state pool await setTokenBalance( client, accountAddress, diff --git a/test/weightedJoin.integration.test.ts b/test/weightedJoin.integration.test.ts index 7ee6ffa9..26f91e6e 100644 --- a/test/weightedJoin.integration.test.ts +++ b/test/weightedJoin.integration.test.ts @@ -41,7 +41,7 @@ const slippage = Slippage.fromPercentage('1'); // 1% const poolId = '0x68e3266c9c8bbd44ad9dca5afbfe629022aee9fe000200000000000000000512'; // Balancer 50COMP-50wstETH -describe('weighted join test', () => { +describe('weighted pool-state test', () => { let api: MockApi; let client: Client & PublicActions & TestActions & WalletActions; let poolFromApi: PoolState; @@ -63,7 +63,7 @@ describe('weighted join test', () => { // get pool state from api poolFromApi = await api.getPool(poolId); - // setup join helper + // setup pool-state helper const joinParser = new JoinParser(); weightedJoin = joinParser.getJoin(poolFromApi.type); @@ -86,7 +86,7 @@ describe('weighted join test', () => { ); }); - test('unbalanced join', async () => { + test('unbalanced pool-state', async () => { const poolTokens = poolFromApi.tokens.map( (t) => new Token(chainId, t.address, t.decimals), ); @@ -94,7 +94,7 @@ describe('weighted join test', () => { TokenAmount.fromHumanAmount(t, '1'), ); - // perform join query to get expected bpt out + // perform pool-state query to get expected bpt out const joinInput: UnbalancedJoinInput = { amountsIn, chainId, @@ -127,7 +127,7 @@ describe('weighted join test', () => { expect(expectedMaxAmountsIn).to.deep.eq(maxAmountsIn); }); - test('native asset join', async () => { + test('native asset pool-state', async () => { const poolTokens = poolFromApi.tokens.map( (t) => new Token(chainId, t.address, t.decimals), ); @@ -135,7 +135,7 @@ describe('weighted join test', () => { TokenAmount.fromHumanAmount(t, '1'), ); - // perform join query to get expected bpt out + // perform pool-state query to get expected bpt out const joinInput: UnbalancedJoinInput = { amountsIn, chainId, @@ -171,11 +171,11 @@ describe('weighted join test', () => { expect(expectedMaxAmountsIn).to.deep.eq(maxAmountsIn); }); - test('single asset join', async () => { + test('single asset pool-state', async () => { const bptOut = TokenAmount.fromHumanAmount(bpt, '1'); const tokenIn = '0x198d7387fa97a73f05b8578cdeff8f2a1f34cd1f'; - // perform join query to get expected bpt out + // perform pool-state query to get expected bpt out const joinInput: SingleAssetJoinInput = { bptOut, tokenIn, @@ -214,10 +214,10 @@ describe('weighted join test', () => { expect(minBptOut).to.eq(bptOut.amount); }); - test('proportional join', async () => { + test('proportional pool-state', async () => { const bptOut = TokenAmount.fromHumanAmount(bpt, '1'); - // perform join query to get expected bpt out + // perform pool-state query to get expected bpt out const joinInput: ProportionalJoinInput = { bptOut, chainId, From 16376b789dcada6c0fa4d9bd6668c1122ad770eb Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Mon, 25 Sep 2023 10:35:31 -0300 Subject: [PATCH 03/18] Fixing wrong replacements --- .../client/index.ts | 0 .../index.ts | 0 .../modules/pool-state/index.ts | 0 .../modules/pool-state/types.ts | 0 src/entities/join/index.ts | 2 +- src/entities/join/parser.ts | 2 +- test/lib/utils/helper.ts | 2 +- test/weightedJoin.integration.test.ts | 20 +++++++++---------- 8 files changed, 13 insertions(+), 13 deletions(-) rename src/data/providers/{balancer-api => balancer-api-provider}/client/index.ts (100%) rename src/data/providers/{balancer-api => balancer-api-provider}/index.ts (100%) rename src/data/providers/{balancer-api => balancer-api-provider}/modules/pool-state/index.ts (100%) rename src/data/providers/{balancer-api => balancer-api-provider}/modules/pool-state/types.ts (100%) diff --git a/src/data/providers/balancer-api/client/index.ts b/src/data/providers/balancer-api-provider/client/index.ts similarity index 100% rename from src/data/providers/balancer-api/client/index.ts rename to src/data/providers/balancer-api-provider/client/index.ts diff --git a/src/data/providers/balancer-api/index.ts b/src/data/providers/balancer-api-provider/index.ts similarity index 100% rename from src/data/providers/balancer-api/index.ts rename to src/data/providers/balancer-api-provider/index.ts diff --git a/src/data/providers/balancer-api/modules/pool-state/index.ts b/src/data/providers/balancer-api-provider/modules/pool-state/index.ts similarity index 100% rename from src/data/providers/balancer-api/modules/pool-state/index.ts rename to src/data/providers/balancer-api-provider/modules/pool-state/index.ts diff --git a/src/data/providers/balancer-api/modules/pool-state/types.ts b/src/data/providers/balancer-api-provider/modules/pool-state/types.ts similarity index 100% rename from src/data/providers/balancer-api/modules/pool-state/types.ts rename to src/data/providers/balancer-api-provider/modules/pool-state/types.ts diff --git a/src/entities/join/index.ts b/src/entities/join/index.ts index bff8b24e..b968ca7a 100644 --- a/src/entities/join/index.ts +++ b/src/entities/join/index.ts @@ -45,7 +45,7 @@ export type JoinInput = | SingleAssetJoinInput | ProportionalJoinInput; -// Returned from a pool-state query +// Returned from a join query export type JoinQueryResult = { poolId: Hex; joinKind: JoinKind; diff --git a/src/entities/join/parser.ts b/src/entities/join/parser.ts index c9f81a98..7506e603 100644 --- a/src/entities/join/parser.ts +++ b/src/entities/join/parser.ts @@ -1,7 +1,7 @@ import { BaseJoin } from '.'; import { WeightedJoin } from './weighted/weightedJoin'; -/*********************** Basic Helper to get pool-state class from pool type *************/ +/*********************** Basic Helper to get join class from pool type *************/ export type JoinConfig = { customPoolJoins: Record; }; diff --git a/test/lib/utils/helper.ts b/test/lib/utils/helper.ts index 1571aa18..9b3ffa22 100644 --- a/test/lib/utils/helper.ts +++ b/test/lib/utils/helper.ts @@ -291,7 +291,7 @@ export const forkSetup = async ( } for (let i = 0; i < tokens.length; i++) { - // Set initial account balance for each token that will be used to pool-state pool + // Set initial account balance for each token that will be used to join pool await setTokenBalance( client, accountAddress, diff --git a/test/weightedJoin.integration.test.ts b/test/weightedJoin.integration.test.ts index 26f91e6e..7ee6ffa9 100644 --- a/test/weightedJoin.integration.test.ts +++ b/test/weightedJoin.integration.test.ts @@ -41,7 +41,7 @@ const slippage = Slippage.fromPercentage('1'); // 1% const poolId = '0x68e3266c9c8bbd44ad9dca5afbfe629022aee9fe000200000000000000000512'; // Balancer 50COMP-50wstETH -describe('weighted pool-state test', () => { +describe('weighted join test', () => { let api: MockApi; let client: Client & PublicActions & TestActions & WalletActions; let poolFromApi: PoolState; @@ -63,7 +63,7 @@ describe('weighted pool-state test', () => { // get pool state from api poolFromApi = await api.getPool(poolId); - // setup pool-state helper + // setup join helper const joinParser = new JoinParser(); weightedJoin = joinParser.getJoin(poolFromApi.type); @@ -86,7 +86,7 @@ describe('weighted pool-state test', () => { ); }); - test('unbalanced pool-state', async () => { + test('unbalanced join', async () => { const poolTokens = poolFromApi.tokens.map( (t) => new Token(chainId, t.address, t.decimals), ); @@ -94,7 +94,7 @@ describe('weighted pool-state test', () => { TokenAmount.fromHumanAmount(t, '1'), ); - // perform pool-state query to get expected bpt out + // perform join query to get expected bpt out const joinInput: UnbalancedJoinInput = { amountsIn, chainId, @@ -127,7 +127,7 @@ describe('weighted pool-state test', () => { expect(expectedMaxAmountsIn).to.deep.eq(maxAmountsIn); }); - test('native asset pool-state', async () => { + test('native asset join', async () => { const poolTokens = poolFromApi.tokens.map( (t) => new Token(chainId, t.address, t.decimals), ); @@ -135,7 +135,7 @@ describe('weighted pool-state test', () => { TokenAmount.fromHumanAmount(t, '1'), ); - // perform pool-state query to get expected bpt out + // perform join query to get expected bpt out const joinInput: UnbalancedJoinInput = { amountsIn, chainId, @@ -171,11 +171,11 @@ describe('weighted pool-state test', () => { expect(expectedMaxAmountsIn).to.deep.eq(maxAmountsIn); }); - test('single asset pool-state', async () => { + test('single asset join', async () => { const bptOut = TokenAmount.fromHumanAmount(bpt, '1'); const tokenIn = '0x198d7387fa97a73f05b8578cdeff8f2a1f34cd1f'; - // perform pool-state query to get expected bpt out + // perform join query to get expected bpt out const joinInput: SingleAssetJoinInput = { bptOut, tokenIn, @@ -214,10 +214,10 @@ describe('weighted pool-state test', () => { expect(minBptOut).to.eq(bptOut.amount); }); - test('proportional pool-state', async () => { + test('proportional join', async () => { const bptOut = TokenAmount.fromHumanAmount(bpt, '1'); - // perform pool-state query to get expected bpt out + // perform join query to get expected bpt out const joinInput: ProportionalJoinInput = { bptOut, chainId, From 581478e5cdee26887d539764268b91201bf36c2d Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Mon, 25 Sep 2023 11:21:06 -0300 Subject: [PATCH 04/18] Changing folder name "balancer-api-provider" to "balancer-api --- package.json | 3 ++- .../{balancer-api-provider => balancer-api}/client/index.ts | 0 .../providers/{balancer-api-provider => balancer-api}/index.ts | 0 .../modules/pool-state/index.ts | 0 .../modules/pool-state/types.ts | 0 5 files changed, 2 insertions(+), 1 deletion(-) rename src/data/providers/{balancer-api-provider => balancer-api}/client/index.ts (100%) rename src/data/providers/{balancer-api-provider => balancer-api}/index.ts (100%) rename src/data/providers/{balancer-api-provider => balancer-api}/modules/pool-state/index.ts (100%) rename src/data/providers/{balancer-api-provider => balancer-api}/modules/pool-state/types.ts (100%) diff --git a/package.json b/package.json index f6ccb0ba..f04ddbd2 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "test:ci": "vitest run", "changeset": "changeset", "changeset:release": "pnpm build && changeset publish", - "node": "npx hardhat node --tsconfig tsconfig.testing.json --fork $(. ./.env && echo $ETHEREUM_RPC_URL)" + "node": "npx hardhat node --tsconfig tsconfig.testing.json --fork $(. ./.env && echo $ETHEREUM_RPC_URL)", + "example": "npx ts-node -P tsconfig.testing.json -r tsconfig-paths/register" }, "dependencies": { "async-retry": "^1.3.3", diff --git a/src/data/providers/balancer-api-provider/client/index.ts b/src/data/providers/balancer-api/client/index.ts similarity index 100% rename from src/data/providers/balancer-api-provider/client/index.ts rename to src/data/providers/balancer-api/client/index.ts diff --git a/src/data/providers/balancer-api-provider/index.ts b/src/data/providers/balancer-api/index.ts similarity index 100% rename from src/data/providers/balancer-api-provider/index.ts rename to src/data/providers/balancer-api/index.ts diff --git a/src/data/providers/balancer-api-provider/modules/pool-state/index.ts b/src/data/providers/balancer-api/modules/pool-state/index.ts similarity index 100% rename from src/data/providers/balancer-api-provider/modules/pool-state/index.ts rename to src/data/providers/balancer-api/modules/pool-state/index.ts diff --git a/src/data/providers/balancer-api-provider/modules/pool-state/types.ts b/src/data/providers/balancer-api/modules/pool-state/types.ts similarity index 100% rename from src/data/providers/balancer-api-provider/modules/pool-state/types.ts rename to src/data/providers/balancer-api/modules/pool-state/types.ts From 8f52f44a2f68449ba3c9fdacedcd30131cca6110 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Mon, 25 Sep 2023 14:16:48 -0300 Subject: [PATCH 05/18] [WIP] Creating example for api integration; Changing function name; --- examples/join.ts | 12 ++++++++++++ package.json | 1 + src/data/providers/balancer-api/index.ts | 2 +- .../balancer-api/modules/pool-state/index.ts | 2 +- 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 examples/join.ts diff --git a/examples/join.ts b/examples/join.ts new file mode 100644 index 00000000..262f1df0 --- /dev/null +++ b/examples/join.ts @@ -0,0 +1,12 @@ + +import { BalancerApi } from "../src/data/providers/balancer-api"; + +const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; +const poolId = '0x481c5fc05d63a58aa2f0f2aa417c021b5d419cb200000000000000000000056a'; +const join = async () => { + const balancerApi = new BalancerApi(balancerApiUrl, 1); + const poolState = await balancerApi.pools.fetchPoolState(poolId); + console.log(poolState); +} + +join(); \ No newline at end of file diff --git a/package.json b/package.json index f04ddbd2..5418c9a7 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "pino-pretty": "^10.0.0", "rome": "12.1.3", "ts-node": "^10.9.1", + "tsconfig-paths": "^4.2.0", "tsup": "^6.6.0", "typescript": "^5.0.4", "vite": "^4.4.2", diff --git a/src/data/providers/balancer-api/index.ts b/src/data/providers/balancer-api/index.ts index d854c6ba..13f85a0a 100644 --- a/src/data/providers/balancer-api/index.ts +++ b/src/data/providers/balancer-api/index.ts @@ -1,7 +1,7 @@ import { Pools } from "./modules/pool-state"; import { BalancerApiClient } from "./client"; -export default class BalancerApi { +export class BalancerApi { balancerApiClient: BalancerApiClient; pools: Pools; diff --git a/src/data/providers/balancer-api/modules/pool-state/index.ts b/src/data/providers/balancer-api/modules/pool-state/index.ts index b78aa25b..2f3a273d 100644 --- a/src/data/providers/balancer-api/modules/pool-state/index.ts +++ b/src/data/providers/balancer-api/modules/pool-state/index.ts @@ -77,7 +77,7 @@ export class Pools { constructor(private readonly balancerApiClient: BalancerApiClient) {} - async fetchSimplePoolState(id: string): Promise { + async fetchPoolState(id: string): Promise { const { data: { poolGetPool }, } = await this.balancerApiClient.fetch('GetPool', this.poolStateQuery, { From 5e01345e1ddef86442583c504ddff0f34275db20 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Mon, 25 Sep 2023 23:43:41 -0300 Subject: [PATCH 06/18] Changing "Weighted" to "WEIGHTED" (format that comes from the API); Creating example for Join Weighted Pool; --- examples/join.ts | 12 --- examples/join/weighted.ts | 94 +++++++++++++++++++ .../balancer-api/modules/pool-state/index.ts | 2 +- .../balancer-api/modules/pool-state/types.ts | 4 +- src/entities/exit/parser.ts | 2 +- src/entities/join/parser.ts | 2 +- test/weightedExit.integration.test.ts | 3 +- test/weightedJoin.integration.test.ts | 2 +- 8 files changed, 102 insertions(+), 19 deletions(-) delete mode 100644 examples/join.ts create mode 100644 examples/join/weighted.ts diff --git a/examples/join.ts b/examples/join.ts deleted file mode 100644 index 262f1df0..00000000 --- a/examples/join.ts +++ /dev/null @@ -1,12 +0,0 @@ - -import { BalancerApi } from "../src/data/providers/balancer-api"; - -const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; -const poolId = '0x481c5fc05d63a58aa2f0f2aa417c021b5d419cb200000000000000000000056a'; -const join = async () => { - const balancerApi = new BalancerApi(balancerApiUrl, 1); - const poolState = await balancerApi.pools.fetchPoolState(poolId); - console.log(poolState); -} - -join(); \ No newline at end of file diff --git a/examples/join/weighted.ts b/examples/join/weighted.ts new file mode 100644 index 00000000..7869847c --- /dev/null +++ b/examples/join/weighted.ts @@ -0,0 +1,94 @@ +import dotenv from 'dotenv'; +dotenv.config(); + +import { BalancerApi } from "../../src/data/providers/balancer-api"; +import { ChainId, CHAINS, JoinKind, Slippage, Token, TokenAmount, UnbalancedJoinInput } from "../../src"; +import { + Client, + createTestClient, + http, + parseUnits, + PublicActions, + publicActions, + TestActions, WalletActions, + walletActions +} from "viem"; +import { PoolState } from "../../src/data/providers/balancer-api/modules/pool-state/types"; +import { forkSetup, sendTransactionGetBalances } from "../../test/lib/utils/helper"; +import { JoinParser } from "../../src/entities/join/parser"; + +const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; +const poolId = '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014'; // 80BAL-20WETH +const chainId = ChainId.MAINNET; +const rpcUrl = 'http://127.0.0.1:8545/'; +const blockNumber = BigInt(18043296); +const testAddress = '0x10A19e7eE7d7F8a52822f6817de8ea18204F2e4f'; // Balancer DAO Multisig +const slippage = Slippage.fromPercentage('1'); // 1% + +const join = async () => { + const balancerApi = new BalancerApi(balancerApiUrl, 1); + const poolState: PoolState = await balancerApi.pools.fetchPoolState(poolId); + let client: Client & PublicActions & TestActions & WalletActions; + client = createTestClient({ + mode: 'hardhat', + chain: CHAINS[chainId], + transport: http(rpcUrl), + }) + .extend(publicActions) + .extend(walletActions); + + await forkSetup( + client, + testAddress, + [...poolState.tokens.map((t) => t.address), poolState.address], + undefined, // TODO: hardcode these values to improve test performance + [ + ...poolState.tokens.map((t) => parseUnits('100', t.decimals)), + parseUnits('100', 18), + ], + process.env.ETHEREUM_RPC_URL as string, + blockNumber, + ); + + + const joinParser = new JoinParser(); + const weightedJoin = joinParser.getJoin(poolState.type); + + const poolTokens = poolState.tokens.map( + (t) => new Token(chainId, t.address, t.decimals), + ); + const amountsIn = poolTokens.map((t) => + TokenAmount.fromHumanAmount(t, '1'), + ); + + // perform join query to get expected bpt out + const joinInput: UnbalancedJoinInput = { + amountsIn, + chainId, + rpcUrl, + kind: JoinKind.Unbalanced, + }; + + const queryResult = await weightedJoin.query(joinInput, poolState); + + const { call, to, value, maxAmountsIn, minBptOut } = + weightedJoin.buildCall({ + ...queryResult, + slippage, + sender: testAddress, + recipient: testAddress, + }); + const { transactionReceipt, balanceDeltas } = + await sendTransactionGetBalances( + [...poolState.tokens.map(({address})=>address), poolState.address /*BPT*/], + client, + testAddress, + to, + call, + value, + ); + console.log(transactionReceipt); + console.log(balanceDeltas); +} + +join(); \ No newline at end of file diff --git a/src/data/providers/balancer-api/modules/pool-state/index.ts b/src/data/providers/balancer-api/modules/pool-state/index.ts index 2f3a273d..938c1329 100644 --- a/src/data/providers/balancer-api/modules/pool-state/index.ts +++ b/src/data/providers/balancer-api/modules/pool-state/index.ts @@ -77,7 +77,7 @@ export class Pools { constructor(private readonly balancerApiClient: BalancerApiClient) {} - async fetchPoolState(id: string): Promise { + async fetchPoolState(id: string): Promise { const { data: { poolGetPool }, } = await this.balancerApiClient.fetch('GetPool', this.poolStateQuery, { diff --git a/src/data/providers/balancer-api/modules/pool-state/types.ts b/src/data/providers/balancer-api/modules/pool-state/types.ts index 5b23f242..88f908da 100644 --- a/src/data/providers/balancer-api/modules/pool-state/types.ts +++ b/src/data/providers/balancer-api/modules/pool-state/types.ts @@ -9,6 +9,6 @@ export type PoolState = { address: Address; decimals: number; index: number; - }; -}[]; + }[]; +}; diff --git a/src/entities/exit/parser.ts b/src/entities/exit/parser.ts index b2cf670b..ff9627fa 100644 --- a/src/entities/exit/parser.ts +++ b/src/entities/exit/parser.ts @@ -9,7 +9,7 @@ export class ExitParser { constructor(config?: ExitConfig) { const { customPoolExits } = config || {}; this.poolExits = { - Weighted: new WeightedExit(), + WEIGHTED: new WeightedExit(), // custom pool Exits take precedence over base Exits ...customPoolExits, }; diff --git a/src/entities/join/parser.ts b/src/entities/join/parser.ts index 7506e603..2dfd87b8 100644 --- a/src/entities/join/parser.ts +++ b/src/entities/join/parser.ts @@ -12,7 +12,7 @@ export class JoinParser { constructor(config?: JoinConfig) { const { customPoolJoins } = config || {}; this.poolJoins = { - Weighted: new WeightedJoin(), + WEIGHTED: new WeightedJoin(), // custom pool Joins take precedence over base Joins ...customPoolJoins, }; diff --git a/test/weightedExit.integration.test.ts b/test/weightedExit.integration.test.ts index 95aeff1b..c3fd4512 100644 --- a/test/weightedExit.integration.test.ts +++ b/test/weightedExit.integration.test.ts @@ -93,6 +93,7 @@ describe('weighted exit test', () => { tokenOut, kind: ExitKind.SINGLE_ASSET, }; + const { queryResult, maxBptIn, minAmountsOut } = await doTransaction( exitInput, poolFromApi.tokens.map((t) => t.address), @@ -317,7 +318,7 @@ export class MockApi { return { id, address: getPoolAddress(id) as Address, - type: 'Weighted', + type: 'WEIGHTED', tokens, }; } diff --git a/test/weightedJoin.integration.test.ts b/test/weightedJoin.integration.test.ts index 7ee6ffa9..d993f511 100644 --- a/test/weightedJoin.integration.test.ts +++ b/test/weightedJoin.integration.test.ts @@ -323,7 +323,7 @@ export class MockApi { return { id, address: getPoolAddress(id) as Address, - type: 'Weighted', + type: 'WEIGHTED', tokens, }; } From d0c111623a080f6e7883c810ab6f33ec14aae1e4 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Tue, 26 Sep 2023 19:39:54 -0300 Subject: [PATCH 07/18] Added example for exit; --- examples/exit/weighted.ts | 99 +++++++++++++++++++++++++++++++++++++++ examples/join/weighted.ts | 6 +-- tsconfig.json | 2 +- 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 examples/exit/weighted.ts diff --git a/examples/exit/weighted.ts b/examples/exit/weighted.ts new file mode 100644 index 00000000..9839e601 --- /dev/null +++ b/examples/exit/weighted.ts @@ -0,0 +1,99 @@ +import dotenv from 'dotenv'; +dotenv.config(); + +import { BalancerApi } from "../../src/data/providers/balancer-api"; +import { + ChainId, + CHAINS, ExitKind, + SingleAssetExitInput, + Slippage, + Token, + TokenAmount, +} from "../../src"; +import { + Client, + createTestClient, + http, + parseUnits, + PublicActions, + publicActions, + TestActions, WalletActions, + walletActions +} from "viem"; +import { PoolState } from "../../src/data/providers/balancer-api/modules/pool-state/types"; +import { forkSetup, sendTransactionGetBalances } from "../../test/lib/utils/helper"; +import { ExitParser } from "../../src/entities/exit/parser"; + +const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; +const poolId = '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014'; // 80BAL-20WETH +const chainId = ChainId.MAINNET; +const rpcUrl = 'http://127.0.0.1:8545/'; +const blockNumber = BigInt(18043296); +const testAddress = '0x10A19e7eE7d7F8a52822f6817de8ea18204F2e4f'; // Balancer DAO Multisig +const slippage = Slippage.fromPercentage('1'); // 1% + +const exit = async () => { + const balancerApi = new BalancerApi(balancerApiUrl, 1); + const poolState: PoolState = await balancerApi.pools.fetchPoolState(poolId); + let client: Client & PublicActions & TestActions & WalletActions; + let bpt: Token; + // setup BPT token + bpt = new Token(chainId, poolState.address, 18, 'BPT'); + client = createTestClient({ + mode: 'hardhat', + chain: CHAINS[chainId], + transport: http(rpcUrl), + }) + .extend(publicActions) + .extend(walletActions); + + await forkSetup( + client, + testAddress, + [poolState.address], + [0], + [ + parseUnits('100', 18), + ], + process.env.ETHEREUM_RPC_URL as string, + blockNumber, + ); + + + const exitParser = new ExitParser(); + const weightedExit = exitParser.getExit(poolState.type); + + const bptIn = TokenAmount.fromHumanAmount(bpt, '1'); + const tokenOut = '0xba100000625a3754423978a60c9317c58a424e3D'; // BAL + + const exitInput: SingleAssetExitInput = { + chainId, + rpcUrl, + bptIn, + tokenOut, + kind: ExitKind.SINGLE_ASSET, + }; + + const queryResult = await weightedExit.query(exitInput, poolState); + + const { call, to, value, maxBptIn, minAmountsOut } = + weightedExit.buildCall({ + ...queryResult, + slippage, + sender: testAddress, + recipient: testAddress, + }); + const { transactionReceipt, balanceDeltas } = + await sendTransactionGetBalances( + [...poolState.tokens.map(({address})=>address), bpt.address], + client, + testAddress, + to, + call, + value, + ); + console.log("transaction status: " + transactionReceipt.status); + console.log("token amounts deltas per token: " + balanceDeltas); +} + +exit(); \ No newline at end of file diff --git a/examples/join/weighted.ts b/examples/join/weighted.ts index 7869847c..d87a5431 100644 --- a/examples/join/weighted.ts +++ b/examples/join/weighted.ts @@ -41,7 +41,7 @@ const join = async () => { client, testAddress, [...poolState.tokens.map((t) => t.address), poolState.address], - undefined, // TODO: hardcode these values to improve test performance + [1,3,0], [ ...poolState.tokens.map((t) => parseUnits('100', t.decimals)), parseUnits('100', 18), @@ -87,8 +87,8 @@ const join = async () => { call, value, ); - console.log(transactionReceipt); - console.log(balanceDeltas); + console.log("transaction status: " + transactionReceipt.status); + console.log("token amounts deltas per token: " + balanceDeltas); } join(); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 21a749b5..1bbfcb59 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["src"], + "include": ["src", "examples"], "compilerOptions": { "target": "ESNext", "module": "ESNext", From 55eed3b68f98431984934d69d126389e42650821 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Wed, 27 Sep 2023 20:15:14 -0300 Subject: [PATCH 08/18] Removing Parser; Replacing Type PoolState for PoolStateInput; Updating README.md examples section; --- README.md | 76 ++++++++++++------- examples/exit/weighted.ts | 5 +- examples/join/weighted.ts | 16 +++- src/data/index.ts | 1 + src/data/providers/balancer-api/index.ts | 2 +- .../balancer-api/modules/pool-state/index.ts | 4 +- src/entities/exit/parser.ts | 0 src/entities/join/parser.ts | 0 8 files changed, 67 insertions(+), 37 deletions(-) delete mode 100644 src/entities/exit/parser.ts delete mode 100644 src/entities/join/parser.ts diff --git a/README.md b/README.md index 3e79f119..a94a8c8f 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,18 @@ Testing requires access to an archive node for onchain quote comparisons. This c `pnpm test` -## Examples +## Balancer Api Provider -### Balancer Api Provider +The Balancer API Provider is a provider that facilitates +data fetching from the Balancer API, +it can be used for: +- Fetch Pool State for Joins; +- Fetch Pool State for Exits. + +### Usage for Joining Pool -Joining with pool state: ```ts - import BalancerApi from "@balancer/sdk/data/providers/balancer-api"; + import { BalancerApi, PoolJoin } from "@balancer/sdk"; ... const joinInput: ProportionalJoinInput = { bptOut, @@ -37,47 +42,64 @@ Joining with pool state: kind: JoinKind.Proportional, }; - const balancerApi = new BalancerApi('https://api-v3.balancer.fi/', 1); - const poolState = await balancerApi.pools.fetchSimplePoolState('0x5f1d6874cb1e7156e79a7563d2b61c6cbce03150000200000000000000000586'); - const joinParser = new JoinParser(); - const poolJoin = joinParser.getJoin(poolState.type); - const queryResult = await weightedJoin.query(joinInput, poolState); - + const balancerApi = new BalancerApi('https://backend-v3-canary.beets-ftm-node.com/graphql', 1); + const poolState = await balancerApi.pools.fetchPoolState('0x5f1d6874cb1e7156e79a7563d2b61c6cbce03150000200000000000000000586'); + const poolJoin = new PoolJoin(); + const queryResult = await poolJoin.query(joinInput, poolState); + const { call, to, value, maxAmountsIn, minBptOut } = + poolJoin.buildCall({ + ...queryResult, + slippage, + sender: signerAddress, + recipient: signerAddress, + }); + const client = createClient({ + ... + }) + + await client.sendTransaction({ + account: signerAddress, + chain: client.chain, + data: call, + to, + value, + }); ``` +Full working join example: [examples/join/weighted.ts](./examples/join/weighted.ts) -Exiting with pool state: +### Usage for Exiting Pool ```ts -import BalancerApi from "@balancer/sdk/data/providers/balancer-api"; +import { BalancerApi, PoolExit } from "@balancer/sdk"; ... -const joinInput: ProportionalJoinInput = { - bptOut, +const exitInput: SingleAssetExitInput = { chainId, rpcUrl, - kind: JoinKind.Proportional, + bptIn, + tokenOut, + kind: ExitKind.SINGLE_ASSET, }; -const balancerApi = new BalancerApi('https://api-v3.balancer.fi/', 1); -const poolState = await balancerApi.pools.fetchSimplePoolState('0x5f1d6874cb1e7156e79a7563d2b61c6cbce03150000200000000000000000586'); -const joinParser = new JoinParser(); -const poolJoin = joinParser.getJoin(poolState.type); -const queryResult = await weightedJoin.query(joinInput, poolState); -const slippage = Slippage.fromPercentage('1'); // 1% +const balancerApi = new BalancerApi('https://backend-v3-canary.beets-ftm-node.com/graphql', 1); +const poolState = await balancerApi.pools.fetchPoolState('0x5f1d6874cb1e7156e79a7563d2b61c6cbce03150000200000000000000000586'); +const poolExit = new PoolExit(); +const queryResult = await poolExit.query(exitInput, poolState); const { call, to, value, maxAmountsIn, minBptOut } = - weightedJoin.buildCall({ + poolExit.buildCall({ ...queryResult, slippage, - sender, - recipient, + sender: signerAddress, + recipient: signerAddress, }); const client = createClient({ ... }) await client.sendTransaction({ - account, + account: signerAddress, chain: client.chain, - data, + data: call, to, value, }); -``` \ No newline at end of file +``` +Full working exit example: [examples/exit/weighted.ts](./examples/exit/weighted.ts) diff --git a/examples/exit/weighted.ts b/examples/exit/weighted.ts index a25857fe..e7691dda 100644 --- a/examples/exit/weighted.ts +++ b/examples/exit/weighted.ts @@ -4,7 +4,7 @@ dotenv.config(); import { BalancerApi } from "../../src/data/providers/balancer-api"; import { ChainId, - CHAINS, ExitKind, PoolExit, + CHAINS, ExitKind, PoolExit, PoolStateInput, SingleAssetExitInput, Slippage, Token, @@ -20,7 +20,6 @@ import { TestActions, WalletActions, walletActions } from "viem"; -import { PoolState } from "../../src/data/providers/balancer-api/modules/pool-state/types"; import { forkSetup, sendTransactionGetBalances } from "../../test/lib/utils/helper"; const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; @@ -33,7 +32,7 @@ const slippage = Slippage.fromPercentage('1'); // 1% const exit = async () => { const balancerApi = new BalancerApi(balancerApiUrl, 1); - const poolState: PoolState = await balancerApi.pools.fetchPoolState(poolId); + const poolState: PoolStateInput = await balancerApi.pools.fetchPoolState(poolId); let client: Client & PublicActions & TestActions & WalletActions; let bpt: Token; // setup BPT token diff --git a/examples/join/weighted.ts b/examples/join/weighted.ts index f972aff3..b1aed8f5 100644 --- a/examples/join/weighted.ts +++ b/examples/join/weighted.ts @@ -1,8 +1,17 @@ import dotenv from 'dotenv'; dotenv.config(); -import { BalancerApi } from "../../src/data/providers/balancer-api"; -import { ChainId, CHAINS, JoinKind, PoolJoin, Slippage, Token, TokenAmount, UnbalancedJoinInput } from "../../src"; +import { + BalancerApi, + ChainId, + CHAINS, + JoinKind, + PoolJoin, PoolStateInput, + Slippage, + Token, + TokenAmount, + UnbalancedJoinInput +} from "../../src"; import { Client, createTestClient, @@ -13,7 +22,6 @@ import { TestActions, WalletActions, walletActions } from "viem"; -import { PoolState } from "../../src/data/providers/balancer-api/modules/pool-state/types"; import { forkSetup, sendTransactionGetBalances } from "../../test/lib/utils/helper"; const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; @@ -26,7 +34,7 @@ const slippage = Slippage.fromPercentage('1'); // 1% const join = async () => { const balancerApi = new BalancerApi(balancerApiUrl, 1); - const poolState: PoolState = await balancerApi.pools.fetchPoolState(poolId); + const poolState: PoolStateInput = await balancerApi.pools.fetchPoolState(poolId); let client: Client & PublicActions & TestActions & WalletActions; client = createTestClient({ mode: 'hardhat', diff --git a/src/data/index.ts b/src/data/index.ts index e02a4aa2..ed1dbc6a 100644 --- a/src/data/index.ts +++ b/src/data/index.ts @@ -1,3 +1,4 @@ export * from './enrichers/onChainPoolDataEnricher'; export * from './providers/subgraphPoolProvider'; +export * from './providers/balancer-api'; export * from './types'; diff --git a/src/data/providers/balancer-api/index.ts b/src/data/providers/balancer-api/index.ts index 13f85a0a..bc18ae52 100644 --- a/src/data/providers/balancer-api/index.ts +++ b/src/data/providers/balancer-api/index.ts @@ -13,4 +13,4 @@ export class BalancerApi { } -} \ No newline at end of file +} diff --git a/src/data/providers/balancer-api/modules/pool-state/index.ts b/src/data/providers/balancer-api/modules/pool-state/index.ts index 938c1329..38ca9403 100644 --- a/src/data/providers/balancer-api/modules/pool-state/index.ts +++ b/src/data/providers/balancer-api/modules/pool-state/index.ts @@ -1,5 +1,5 @@ import { BalancerApiClient } from '../../client'; -import { PoolState } from './types'; +import { PoolStateInput } from "../../../../../entities"; export class Pools { readonly poolStateQuery = `query GetPool($id: String!){ @@ -77,7 +77,7 @@ export class Pools { constructor(private readonly balancerApiClient: BalancerApiClient) {} - async fetchPoolState(id: string): Promise { + async fetchPoolState(id: string): Promise { const { data: { poolGetPool }, } = await this.balancerApiClient.fetch('GetPool', this.poolStateQuery, { diff --git a/src/entities/exit/parser.ts b/src/entities/exit/parser.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/src/entities/join/parser.ts b/src/entities/join/parser.ts deleted file mode 100644 index e69de29b..00000000 From 36e684f43c46f7595f2bbb978cc72c5955a230c4 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Mon, 2 Oct 2023 21:07:09 +0200 Subject: [PATCH 09/18] fixing lint errors --- examples/exit/weighted.ts | 13 +++++-------- examples/join/weighted.ts | 9 ++++----- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/examples/exit/weighted.ts b/examples/exit/weighted.ts index e7691dda..0f7e4a92 100644 --- a/examples/exit/weighted.ts +++ b/examples/exit/weighted.ts @@ -33,17 +33,14 @@ const slippage = Slippage.fromPercentage('1'); // 1% const exit = async () => { const balancerApi = new BalancerApi(balancerApiUrl, 1); const poolState: PoolStateInput = await balancerApi.pools.fetchPoolState(poolId); - let client: Client & PublicActions & TestActions & WalletActions; - let bpt: Token; - // setup BPT token - bpt = new Token(chainId, poolState.address, 18, 'BPT'); - client = createTestClient({ + const client: Client & PublicActions & TestActions & WalletActions = createTestClient({ mode: 'hardhat', chain: CHAINS[chainId], transport: http(rpcUrl), }) .extend(publicActions) .extend(walletActions); + const bpt = new Token(chainId, poolState.address, 18, 'BPT'); await forkSetup( client, @@ -72,7 +69,7 @@ const exit = async () => { const queryResult = await poolExit.query(exitInput, poolState); - const { call, to, value, maxBptIn, minAmountsOut } = + const { call, to, value } = poolExit.buildCall({ ...queryResult, slippage, @@ -88,8 +85,8 @@ const exit = async () => { call, value, ); - console.log("transaction status: " + transactionReceipt.status); - console.log("token amounts deltas per token: " + balanceDeltas); + console.log(`transaction status: ${transactionReceipt.status}`); + console.log(`token amounts deltas per token: ${balanceDeltas}`); } exit(); \ No newline at end of file diff --git a/examples/join/weighted.ts b/examples/join/weighted.ts index b1aed8f5..72d38c0e 100644 --- a/examples/join/weighted.ts +++ b/examples/join/weighted.ts @@ -35,8 +35,7 @@ const slippage = Slippage.fromPercentage('1'); // 1% const join = async () => { const balancerApi = new BalancerApi(balancerApiUrl, 1); const poolState: PoolStateInput = await balancerApi.pools.fetchPoolState(poolId); - let client: Client & PublicActions & TestActions & WalletActions; - client = createTestClient({ + const client: Client & PublicActions & TestActions & WalletActions = createTestClient({ mode: 'hardhat', chain: CHAINS[chainId], transport: http(rpcUrl), @@ -76,7 +75,7 @@ const join = async () => { const queryResult = await poolJoin.query(joinInput, poolState); - const { call, to, value, maxAmountsIn, minBptOut } = + const { call, to, value } = poolJoin.buildCall({ ...queryResult, slippage, @@ -92,8 +91,8 @@ const join = async () => { call, value, ); - console.log("transaction status: " + transactionReceipt.status); - console.log("token amounts deltas per token: " + balanceDeltas); + console.log(`transaction status: ${transactionReceipt.status}`); + console.log(`token amounts deltas per token: ${balanceDeltas}`); } join(); \ No newline at end of file From e1b71c7ab1af58837bffd45b8ecf3c53c9b40dd2 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Sun, 22 Oct 2023 20:05:10 -0300 Subject: [PATCH 10/18] Changing "chainId" type to ChainId instead of number; clarifying query and variables parameters type on fetch function; --- .../providers/balancer-api/client/index.ts | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/data/providers/balancer-api/client/index.ts b/src/data/providers/balancer-api/client/index.ts index 40ac1ab3..a9502be8 100644 --- a/src/data/providers/balancer-api/client/index.ts +++ b/src/data/providers/balancer-api/client/index.ts @@ -1,22 +1,31 @@ +import { ChainId } from '../../../../utils'; + export class BalancerApiClient { - apiUrl: string; - chainId: number; - constructor(apiUrl: string, chainId: number) { - this.apiUrl = apiUrl; - this.chainId = chainId; - } + apiUrl: string; + chainId: ChainId; + constructor(apiUrl: string, chainId: number) { + this.apiUrl = apiUrl; + this.chainId = chainId; + } - async fetch(operationName: string, query: any, variables: any) { - const requestQuery = { - operationName, - query, - variables, - }; - const response = await fetch(this.apiUrl, { - method: 'post', - body: JSON.stringify(requestQuery), - headers: { 'Content-Type': 'application/json', ChainId: this.chainId.toString() }, - }); - return response.json(); - } + async fetch( + operationName: string, + query: string, + variables: { id: string }, + ) { + const requestQuery = { + operationName, + query, + variables, + }; + const response = await fetch(this.apiUrl, { + method: 'post', + body: JSON.stringify(requestQuery), + headers: { + 'Content-Type': 'application/json', + ChainId: this.chainId.toString(), + }, + }); + return response.json(); + } } From e9dde1ce1c07bc01161a80f533215094f467b3a3 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Sun, 22 Oct 2023 20:21:21 -0300 Subject: [PATCH 11/18] Removing unused types file; --- .../balancer-api/modules/pool-state/types.ts | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 src/data/providers/balancer-api/modules/pool-state/types.ts diff --git a/src/data/providers/balancer-api/modules/pool-state/types.ts b/src/data/providers/balancer-api/modules/pool-state/types.ts deleted file mode 100644 index 88f908da..00000000 --- a/src/data/providers/balancer-api/modules/pool-state/types.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Address } from "viem"; - -export type PoolState = { - id: Address; - address: Address; - type: string; - version: string; - tokens: { - address: Address; - decimals: number; - index: number; - }[]; -}; - From 8a3b8d6cfcf118c3c9b1b7cd2ba92e2f05fd27c8 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Sun, 22 Oct 2023 20:22:56 -0300 Subject: [PATCH 12/18] Updating pnpm-lock.yaml after merge; --- pnpm-lock.yaml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 01d6fe62..1b4858ef 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -43,6 +43,9 @@ devDependencies: ts-node: specifier: ^10.9.1 version: 10.9.1(@types/node@18.15.11)(typescript@5.1.3) + tsconfig-paths: + specifier: ^4.2.0 + version: 4.2.0 tsup: specifier: ^6.6.0 version: 6.7.0(ts-node@10.9.1)(typescript@5.1.3) @@ -2153,6 +2156,12 @@ packages: resolution: {integrity: sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==} dev: true + /json5@2.2.3: + resolution: {integrity: sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==} + engines: {node: '>=6'} + hasBin: true + dev: true + /jsonc-parser@3.2.0: resolution: {integrity: sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==} dev: true @@ -3249,6 +3258,15 @@ packages: yn: 3.1.1 dev: true + /tsconfig-paths@4.2.0: + resolution: {integrity: sha512-NoZ4roiN7LnbKn9QqE1amc9DJfzvZXxF4xDavcOWt1BPkdx+m+0gJuPM+S0vCe7zTJMYUP0R8pO2XMr+Y8oLIg==} + engines: {node: '>=6'} + dependencies: + json5: 2.2.3 + minimist: 1.2.8 + strip-bom: 3.0.0 + dev: true + /tsup@6.7.0(ts-node@10.9.1)(typescript@5.1.3): resolution: {integrity: sha512-L3o8hGkaHnu5TdJns+mCqFsDBo83bJ44rlK7e6VdanIvpea4ArPcU3swWGsLVbXak1PqQx/V+SSmFPujBK+zEQ==} engines: {node: '>=14.18'} From e9b3b32939f103933d9ab82b74b7a7f87e70f179 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Mon, 23 Oct 2023 19:00:23 -0300 Subject: [PATCH 13/18] Changing examples to anvil; Changing chainId types to ChainId instead of number; Adding a resolution to install get-port in the version 5.1.2, to accept non ESM imports, since @viem/anvil import this package using require; --- examples/exit/weighted.ts | 9 ++++----- examples/join/weighted.ts | 13 +++++++------ package.json | 12 +++++++++--- pnpm-lock.yaml | 18 ++++++++++++++---- .../providers/balancer-api/client/index.ts | 11 ++--------- src/data/providers/balancer-api/index.ts | 3 ++- .../balancer-api/modules/pool-state/index.ts | 6 ++++-- 7 files changed, 42 insertions(+), 30 deletions(-) diff --git a/examples/exit/weighted.ts b/examples/exit/weighted.ts index 0f7e4a92..07901d23 100644 --- a/examples/exit/weighted.ts +++ b/examples/exit/weighted.ts @@ -21,20 +21,21 @@ import { walletActions } from "viem"; import { forkSetup, sendTransactionGetBalances } from "../../test/lib/utils/helper"; +import anvilGlobalSetup from "../../test/anvil/anvil-global-setup"; const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; const poolId = '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014'; // 80BAL-20WETH const chainId = ChainId.MAINNET; const rpcUrl = 'http://127.0.0.1:8545/'; -const blockNumber = BigInt(18043296); const testAddress = '0x10A19e7eE7d7F8a52822f6817de8ea18204F2e4f'; // Balancer DAO Multisig const slippage = Slippage.fromPercentage('1'); // 1% const exit = async () => { + await anvilGlobalSetup(); const balancerApi = new BalancerApi(balancerApiUrl, 1); const poolState: PoolStateInput = await balancerApi.pools.fetchPoolState(poolId); const client: Client & PublicActions & TestActions & WalletActions = createTestClient({ - mode: 'hardhat', + mode: 'anvil', chain: CHAINS[chainId], transport: http(rpcUrl), }) @@ -49,9 +50,7 @@ const exit = async () => { [0], [ parseUnits('100', 18), - ], - process.env.ETHEREUM_RPC_URL as string, - blockNumber, + ] ); const bptIn = TokenAmount.fromHumanAmount(bpt, '1'); diff --git a/examples/join/weighted.ts b/examples/join/weighted.ts index 72d38c0e..f55af9ff 100644 --- a/examples/join/weighted.ts +++ b/examples/join/weighted.ts @@ -1,5 +1,5 @@ -import dotenv from 'dotenv'; -dotenv.config(); +import { config } from 'dotenv'; +config(); import { BalancerApi, @@ -23,6 +23,7 @@ import { walletActions } from "viem"; import { forkSetup, sendTransactionGetBalances } from "../../test/lib/utils/helper"; +import anvilGlobalSetup from "../../test/anvil/anvil-global-setup"; const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; const poolId = '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014'; // 80BAL-20WETH @@ -33,10 +34,11 @@ const testAddress = '0x10A19e7eE7d7F8a52822f6817de8ea18204F2e4f'; // Balancer DA const slippage = Slippage.fromPercentage('1'); // 1% const join = async () => { + await anvilGlobalSetup(); const balancerApi = new BalancerApi(balancerApiUrl, 1); const poolState: PoolStateInput = await balancerApi.pools.fetchPoolState(poolId); const client: Client & PublicActions & TestActions & WalletActions = createTestClient({ - mode: 'hardhat', + mode: 'anvil', chain: CHAINS[chainId], transport: http(rpcUrl), }) @@ -52,8 +54,6 @@ const join = async () => { ...poolState.tokens.map((t) => parseUnits('100', t.decimals)), parseUnits('100', 18), ], - process.env.ETHEREUM_RPC_URL as string, - blockNumber, ); @@ -93,6 +93,7 @@ const join = async () => { ); console.log(`transaction status: ${transactionReceipt.status}`); console.log(`token amounts deltas per token: ${balanceDeltas}`); + return; } -join(); \ No newline at end of file +join().then(()=>{}); \ No newline at end of file diff --git a/package.json b/package.json index 9dd091f8..1add6c53 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,9 @@ "module": "dist/index.mjs", "types": "dist/index.d.ts", "typings": "dist/index.d.ts", - "files": ["dist/"], + "files": [ + "dist/" + ], "scripts": { "build": "tsup", "format": "rome format .", @@ -22,7 +24,8 @@ "test": "vitest dev", "test:ci": "vitest run", "changeset": "changeset", - "changeset:release": "pnpm build && changeset publish" + "changeset:release": "pnpm build && changeset publish", + "example": "npx ts-node -P tsconfig.testing.json -r tsconfig-paths/register" }, "dependencies": { "async-retry": "^1.3.3", @@ -45,5 +48,8 @@ "vite": "^4.4.2", "vitest": "^0.34.6" }, - "packageManager": "^pnpm@8.6.0" + "packageManager": "^pnpm@8.6.0", + "resolutions": { + "get-port": "5.1.2" + } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1b4858ef..23436a11 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,9 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false +overrides: + get-port: 5.0.0 + dependencies: async-retry: specifier: ^1.3.3 @@ -892,7 +895,7 @@ packages: resolution: {integrity: sha512-OjKR/+FVwzuygXYFqP8MBal1SXG8bT2gbZwqqB0XuLw81LNBBvmE/Repm6+5kkBh4IUj0PhYdrqOsnayS14Gtg==} dependencies: execa: 7.2.0 - get-port: 6.1.2 + get-port: 5.0.0 http-proxy: 1.18.1 ws: 8.14.2 transitivePeerDependencies: @@ -1754,9 +1757,11 @@ packages: has-symbols: 1.0.3 dev: true - /get-port@6.1.2: - resolution: {integrity: sha512-BrGGraKm2uPqurfGVj/z97/zv8dPleC6x9JBNRTrDNtCkkRF4rPwrQXFgL7+I+q8QSdU4ntLQX2D7KIxSy8nGw==} - engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} + /get-port@5.0.0: + resolution: {integrity: sha512-imzMU0FjsZqNa6BqOjbbW6w5BivHIuQKopjpPqcnx0AVHJQKCxK1O+Ab3OrVXhrekqfVMjwA9ZYu062R+KcIsQ==} + engines: {node: '>=8'} + dependencies: + type-fest: 0.3.1 dev: true /get-stream@6.0.1: @@ -3327,6 +3332,11 @@ packages: engines: {node: '>=10'} dev: true + /type-fest@0.3.1: + resolution: {integrity: sha512-cUGJnCdr4STbePCgqNFbpVNCepa+kAVohJs1sLhxzdH+gnEoOd8VhbYa7pD3zZYGiURWM2xzEII3fQcRizDkYQ==} + engines: {node: '>=6'} + dev: true + /type-fest@0.6.0: resolution: {integrity: sha512-q+MB8nYR1KDLrgr4G5yemftpMC7/QLqVndBmEEdqzmNj5dcFOO4Oo8qlwZE3ULT3+Zim1F8Kq4cBnikNhlCMlg==} engines: {node: '>=8'} diff --git a/src/data/providers/balancer-api/client/index.ts b/src/data/providers/balancer-api/client/index.ts index a9502be8..aeb6218a 100644 --- a/src/data/providers/balancer-api/client/index.ts +++ b/src/data/providers/balancer-api/client/index.ts @@ -3,21 +3,14 @@ import { ChainId } from '../../../../utils'; export class BalancerApiClient { apiUrl: string; chainId: ChainId; - constructor(apiUrl: string, chainId: number) { + constructor(apiUrl: string, chainId: ChainId) { this.apiUrl = apiUrl; this.chainId = chainId; } async fetch( - operationName: string, - query: string, - variables: { id: string }, + requestQuery: {operationName?:string, query:string, variables?:any} ) { - const requestQuery = { - operationName, - query, - variables, - }; const response = await fetch(this.apiUrl, { method: 'post', body: JSON.stringify(requestQuery), diff --git a/src/data/providers/balancer-api/index.ts b/src/data/providers/balancer-api/index.ts index bc18ae52..a00216e3 100644 --- a/src/data/providers/balancer-api/index.ts +++ b/src/data/providers/balancer-api/index.ts @@ -1,5 +1,6 @@ import { Pools } from "./modules/pool-state"; import { BalancerApiClient } from "./client"; +import { ChainId } from "../../../utils"; export class BalancerApi { @@ -7,7 +8,7 @@ export class BalancerApi { pools: Pools; - constructor(balancerApiUrl: string, chainId: number){ + constructor(balancerApiUrl: string, chainId: ChainId){ this.balancerApiClient = new BalancerApiClient(balancerApiUrl, chainId); this.pools = new Pools(this.balancerApiClient); } diff --git a/src/data/providers/balancer-api/modules/pool-state/index.ts b/src/data/providers/balancer-api/modules/pool-state/index.ts index 38ca9403..3941bae0 100644 --- a/src/data/providers/balancer-api/modules/pool-state/index.ts +++ b/src/data/providers/balancer-api/modules/pool-state/index.ts @@ -80,9 +80,11 @@ export class Pools { async fetchPoolState(id: string): Promise { const { data: { poolGetPool }, - } = await this.balancerApiClient.fetch('GetPool', this.poolStateQuery, { + } = await this.balancerApiClient.fetch({ + query: this.poolStateQuery, + variables: { id, - }); + }}); return poolGetPool; } } From eee4ac144c9d32ce9daf2f29b4c87394cd6237d8 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Mon, 23 Oct 2023 19:10:06 -0300 Subject: [PATCH 14/18] removed unused blockNumber variable; --- examples/join/weighted.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/join/weighted.ts b/examples/join/weighted.ts index f55af9ff..5d660056 100644 --- a/examples/join/weighted.ts +++ b/examples/join/weighted.ts @@ -29,7 +29,6 @@ const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; const poolId = '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014'; // 80BAL-20WETH const chainId = ChainId.MAINNET; const rpcUrl = 'http://127.0.0.1:8545/'; -const blockNumber = BigInt(18043296); const testAddress = '0x10A19e7eE7d7F8a52822f6817de8ea18204F2e4f'; // Balancer DAO Multisig const slippage = Slippage.fromPercentage('1'); // 1% From 7789d6d6a4e4d918691f1b5ca0418819ed6a110e Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Mon, 23 Oct 2023 19:28:00 -0300 Subject: [PATCH 15/18] Fixing get-port version and pnpm-lock file; --- package.json | 2 +- pnpm-lock.yaml | 15 ++++----------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 1add6c53..eac9bb04 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,6 @@ }, "packageManager": "^pnpm@8.6.0", "resolutions": { - "get-port": "5.1.2" + "get-port": "5.1.1" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 23436a11..cbf0e776 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,7 +5,7 @@ settings: excludeLinksFromLockfile: false overrides: - get-port: 5.0.0 + get-port: 5.1.1 dependencies: async-retry: @@ -895,7 +895,7 @@ packages: resolution: {integrity: sha512-OjKR/+FVwzuygXYFqP8MBal1SXG8bT2gbZwqqB0XuLw81LNBBvmE/Repm6+5kkBh4IUj0PhYdrqOsnayS14Gtg==} dependencies: execa: 7.2.0 - get-port: 5.0.0 + get-port: 5.1.1 http-proxy: 1.18.1 ws: 8.14.2 transitivePeerDependencies: @@ -1757,11 +1757,9 @@ packages: has-symbols: 1.0.3 dev: true - /get-port@5.0.0: - resolution: {integrity: sha512-imzMU0FjsZqNa6BqOjbbW6w5BivHIuQKopjpPqcnx0AVHJQKCxK1O+Ab3OrVXhrekqfVMjwA9ZYu062R+KcIsQ==} + /get-port@5.1.1: + resolution: {integrity: sha512-g/Q1aTSDOxFpchXC4i8ZWvxA1lnPqx/JHqcpIw0/LX9T8x/GBbi6YnlN5nhaKIFkT8oFsscUKgDJYxfwfS6QsQ==} engines: {node: '>=8'} - dependencies: - type-fest: 0.3.1 dev: true /get-stream@6.0.1: @@ -3332,11 +3330,6 @@ packages: engines: {node: '>=10'} dev: true - /type-fest@0.3.1: - resolution: {integrity: sha512-cUGJnCdr4STbePCgqNFbpVNCepa+kAVohJs1sLhxzdH+gnEoOd8VhbYa7pD3zZYGiURWM2xzEII3fQcRizDkYQ==} - engines: {node: '>=6'} - dev: true - /type-fest@0.6.0: resolution: {integrity: sha512-q+MB8nYR1KDLrgr4G5yemftpMC7/QLqVndBmEEdqzmNj5dcFOO4Oo8qlwZE3ULT3+Zim1F8Kq4cBnikNhlCMlg==} engines: {node: '>=8'} From 82e507fa1606d1bb33b63997fbae4936d44a3f2e Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem De Liz Date: Tue, 24 Oct 2023 11:36:15 -0300 Subject: [PATCH 16/18] Running rome in the client/index.ts file; replacing ts-node by tsx; removing tsconfig-paths; removing resolutions; updating pnpm-lock.yaml; --- package.json | 8 ++------ pnpm-lock.yaml | 11 ++++------- src/data/providers/balancer-api/client/index.ts | 8 +++++--- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index eac9bb04..561a2913 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "test:ci": "vitest run", "changeset": "changeset", "changeset:release": "pnpm build && changeset publish", - "example": "npx ts-node -P tsconfig.testing.json -r tsconfig-paths/register" + "example": "npx tsx" }, "dependencies": { "async-retry": "^1.3.3", @@ -42,14 +42,10 @@ "pino-pretty": "^10.0.0", "rome": "12.1.3", "ts-node": "^10.9.1", - "tsconfig-paths": "^4.2.0", "tsup": "^6.6.0", "typescript": "^5.0.4", "vite": "^4.4.2", "vitest": "^0.34.6" }, - "packageManager": "^pnpm@8.6.0", - "resolutions": { - "get-port": "5.1.1" - } + "packageManager": "^pnpm@8.6.0" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cbf0e776..1b4858ef 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,9 +4,6 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false -overrides: - get-port: 5.1.1 - dependencies: async-retry: specifier: ^1.3.3 @@ -895,7 +892,7 @@ packages: resolution: {integrity: sha512-OjKR/+FVwzuygXYFqP8MBal1SXG8bT2gbZwqqB0XuLw81LNBBvmE/Repm6+5kkBh4IUj0PhYdrqOsnayS14Gtg==} dependencies: execa: 7.2.0 - get-port: 5.1.1 + get-port: 6.1.2 http-proxy: 1.18.1 ws: 8.14.2 transitivePeerDependencies: @@ -1757,9 +1754,9 @@ packages: has-symbols: 1.0.3 dev: true - /get-port@5.1.1: - resolution: {integrity: sha512-g/Q1aTSDOxFpchXC4i8ZWvxA1lnPqx/JHqcpIw0/LX9T8x/GBbi6YnlN5nhaKIFkT8oFsscUKgDJYxfwfS6QsQ==} - engines: {node: '>=8'} + /get-port@6.1.2: + resolution: {integrity: sha512-BrGGraKm2uPqurfGVj/z97/zv8dPleC6x9JBNRTrDNtCkkRF4rPwrQXFgL7+I+q8QSdU4ntLQX2D7KIxSy8nGw==} + engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} dev: true /get-stream@6.0.1: diff --git a/src/data/providers/balancer-api/client/index.ts b/src/data/providers/balancer-api/client/index.ts index aeb6218a..91cd0d2d 100644 --- a/src/data/providers/balancer-api/client/index.ts +++ b/src/data/providers/balancer-api/client/index.ts @@ -8,9 +8,11 @@ export class BalancerApiClient { this.chainId = chainId; } - async fetch( - requestQuery: {operationName?:string, query:string, variables?:any} - ) { + async fetch(requestQuery: { + operationName?: string; + query: string; + variables?: any; + }) { const response = await fetch(this.apiUrl, { method: 'post', body: JSON.stringify(requestQuery), From 097ac9e5fde2fca967c130c45075731d9e4d7d96 Mon Sep 17 00:00:00 2001 From: johngrantuk Date: Wed, 25 Oct 2023 13:00:37 +0100 Subject: [PATCH 17/18] refactor: Fix formatting. --- examples/exit/weighted.ts | 152 ++++++++-------- examples/join/weighted.ts | 166 ++++++++++-------- src/data/providers/balancer-api/index.ts | 24 ++- .../balancer-api/modules/pool-state/index.ts | 27 +-- 4 files changed, 192 insertions(+), 177 deletions(-) diff --git a/examples/exit/weighted.ts b/examples/exit/weighted.ts index 07901d23..e7e4650e 100644 --- a/examples/exit/weighted.ts +++ b/examples/exit/weighted.ts @@ -1,91 +1,99 @@ import dotenv from 'dotenv'; dotenv.config(); -import { BalancerApi } from "../../src/data/providers/balancer-api"; +import { BalancerApi } from '../../src/data/providers/balancer-api'; import { - ChainId, - CHAINS, ExitKind, PoolExit, PoolStateInput, - SingleAssetExitInput, - Slippage, - Token, - TokenAmount, -} from "../../src"; + ChainId, + CHAINS, + ExitKind, + PoolExit, + PoolStateInput, + SingleAssetExitInput, + Slippage, + Token, + TokenAmount, +} from '../../src'; import { - Client, - createTestClient, - http, - parseUnits, - PublicActions, - publicActions, - TestActions, WalletActions, - walletActions -} from "viem"; -import { forkSetup, sendTransactionGetBalances } from "../../test/lib/utils/helper"; -import anvilGlobalSetup from "../../test/anvil/anvil-global-setup"; + Client, + createTestClient, + http, + parseUnits, + PublicActions, + publicActions, + TestActions, + WalletActions, + walletActions, +} from 'viem'; +import { + forkSetup, + sendTransactionGetBalances, +} from '../../test/lib/utils/helper'; +import anvilGlobalSetup from '../../test/anvil/anvil-global-setup'; const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; -const poolId = '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014'; // 80BAL-20WETH +const poolId = + '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014'; // 80BAL-20WETH const chainId = ChainId.MAINNET; const rpcUrl = 'http://127.0.0.1:8545/'; const testAddress = '0x10A19e7eE7d7F8a52822f6817de8ea18204F2e4f'; // Balancer DAO Multisig const slippage = Slippage.fromPercentage('1'); // 1% const exit = async () => { - await anvilGlobalSetup(); - const balancerApi = new BalancerApi(balancerApiUrl, 1); - const poolState: PoolStateInput = await balancerApi.pools.fetchPoolState(poolId); - const client: Client & PublicActions & TestActions & WalletActions = createTestClient({ - mode: 'anvil', - chain: CHAINS[chainId], - transport: http(rpcUrl), - }) - .extend(publicActions) - .extend(walletActions); - const bpt = new Token(chainId, poolState.address, 18, 'BPT'); + await anvilGlobalSetup(); + const balancerApi = new BalancerApi(balancerApiUrl, 1); + const poolState: PoolStateInput = await balancerApi.pools.fetchPoolState( + poolId, + ); + const client: Client & PublicActions & TestActions & WalletActions = + createTestClient({ + mode: 'anvil', + chain: CHAINS[chainId], + transport: http(rpcUrl), + }) + .extend(publicActions) + .extend(walletActions); + const bpt = new Token(chainId, poolState.address, 18, 'BPT'); + + await forkSetup( + client, + testAddress, + [poolState.address], + [0], + [parseUnits('100', 18)], + ); + + const bptIn = TokenAmount.fromHumanAmount(bpt, '1'); + const tokenOut = '0xba100000625a3754423978a60c9317c58a424e3D'; // BAL - await forkSetup( - client, - testAddress, - [poolState.address], - [0], - [ - parseUnits('100', 18), - ] - ); + const poolExit = new PoolExit(); - const bptIn = TokenAmount.fromHumanAmount(bpt, '1'); - const tokenOut = '0xba100000625a3754423978a60c9317c58a424e3D'; // BAL - - const poolExit = new PoolExit(); - - const exitInput: SingleAssetExitInput = { - chainId, - rpcUrl, - bptIn, - tokenOut, - kind: ExitKind.SINGLE_ASSET, - }; + const exitInput: SingleAssetExitInput = { + chainId, + rpcUrl, + bptIn, + tokenOut, + kind: ExitKind.SINGLE_ASSET, + }; - const queryResult = await poolExit.query(exitInput, poolState); + const queryResult = await poolExit.query(exitInput, poolState); - const { call, to, value } = - poolExit.buildCall({ - ...queryResult, - slippage, - sender: testAddress, - recipient: testAddress, + const { call, to, value } = poolExit.buildCall({ + ...queryResult, + slippage, + sender: testAddress, + recipient: testAddress, }); - const { transactionReceipt, balanceDeltas } = - await sendTransactionGetBalances( - [...poolState.tokens.map(({address})=>address), bpt.address], - client, - testAddress, - to, - call, - value, - ); - console.log(`transaction status: ${transactionReceipt.status}`); - console.log(`token amounts deltas per token: ${balanceDeltas}`); -} + const { transactionReceipt, balanceDeltas } = + await sendTransactionGetBalances( + [...poolState.tokens.map(({ address }) => address), bpt.address], + client, + testAddress, + to, + call, + value, + ); + console.log(`transaction status: ${transactionReceipt.status}`); + console.log(`token amounts deltas per token: ${balanceDeltas}`); +}; -exit(); \ No newline at end of file +exit(); diff --git a/examples/join/weighted.ts b/examples/join/weighted.ts index 5d660056..51b3dd42 100644 --- a/examples/join/weighted.ts +++ b/examples/join/weighted.ts @@ -2,97 +2,107 @@ import { config } from 'dotenv'; config(); import { - BalancerApi, - ChainId, - CHAINS, - JoinKind, - PoolJoin, PoolStateInput, - Slippage, - Token, - TokenAmount, - UnbalancedJoinInput -} from "../../src"; + BalancerApi, + ChainId, + CHAINS, + JoinKind, + PoolJoin, + PoolStateInput, + Slippage, + Token, + TokenAmount, + UnbalancedJoinInput, +} from '../../src'; import { - Client, - createTestClient, - http, - parseUnits, - PublicActions, - publicActions, - TestActions, WalletActions, - walletActions -} from "viem"; -import { forkSetup, sendTransactionGetBalances } from "../../test/lib/utils/helper"; -import anvilGlobalSetup from "../../test/anvil/anvil-global-setup"; + Client, + createTestClient, + http, + parseUnits, + PublicActions, + publicActions, + TestActions, + WalletActions, + walletActions, +} from 'viem'; +import { + forkSetup, + sendTransactionGetBalances, +} from '../../test/lib/utils/helper'; +import anvilGlobalSetup from '../../test/anvil/anvil-global-setup'; const balancerApiUrl = 'https://backend-v3-canary.beets-ftm-node.com/graphql'; -const poolId = '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014'; // 80BAL-20WETH +const poolId = + '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014'; // 80BAL-20WETH const chainId = ChainId.MAINNET; const rpcUrl = 'http://127.0.0.1:8545/'; const testAddress = '0x10A19e7eE7d7F8a52822f6817de8ea18204F2e4f'; // Balancer DAO Multisig const slippage = Slippage.fromPercentage('1'); // 1% const join = async () => { - await anvilGlobalSetup(); - const balancerApi = new BalancerApi(balancerApiUrl, 1); - const poolState: PoolStateInput = await balancerApi.pools.fetchPoolState(poolId); - const client: Client & PublicActions & TestActions & WalletActions = createTestClient({ - mode: 'anvil', - chain: CHAINS[chainId], - transport: http(rpcUrl), - }) - .extend(publicActions) - .extend(walletActions); - - await forkSetup( - client, - testAddress, - [...poolState.tokens.map((t) => t.address), poolState.address], - [1,3,0], - [ - ...poolState.tokens.map((t) => parseUnits('100', t.decimals)), - parseUnits('100', 18), - ], - ); + await anvilGlobalSetup(); + const balancerApi = new BalancerApi(balancerApiUrl, 1); + const poolState: PoolStateInput = await balancerApi.pools.fetchPoolState( + poolId, + ); + const client: Client & PublicActions & TestActions & WalletActions = + createTestClient({ + mode: 'anvil', + chain: CHAINS[chainId], + transport: http(rpcUrl), + }) + .extend(publicActions) + .extend(walletActions); + await forkSetup( + client, + testAddress, + [...poolState.tokens.map((t) => t.address), poolState.address], + [1, 3, 0], + [ + ...poolState.tokens.map((t) => parseUnits('100', t.decimals)), + parseUnits('100', 18), + ], + ); - const poolJoin = new PoolJoin(); - const poolTokens = poolState.tokens.map( - (t) => new Token(chainId, t.address, t.decimals), - ); - const amountsIn = poolTokens.map((t) => - TokenAmount.fromHumanAmount(t, '1'), - ); + const poolJoin = new PoolJoin(); + const poolTokens = poolState.tokens.map( + (t) => new Token(chainId, t.address, t.decimals), + ); + const amountsIn = poolTokens.map((t) => + TokenAmount.fromHumanAmount(t, '1'), + ); - // perform join query to get expected bpt out - const joinInput: UnbalancedJoinInput = { - amountsIn, - chainId, - rpcUrl, - kind: JoinKind.Unbalanced, - }; + // perform join query to get expected bpt out + const joinInput: UnbalancedJoinInput = { + amountsIn, + chainId, + rpcUrl, + kind: JoinKind.Unbalanced, + }; - const queryResult = await poolJoin.query(joinInput, poolState); + const queryResult = await poolJoin.query(joinInput, poolState); - const { call, to, value } = - poolJoin.buildCall({ - ...queryResult, - slippage, - sender: testAddress, - recipient: testAddress, + const { call, to, value } = poolJoin.buildCall({ + ...queryResult, + slippage, + sender: testAddress, + recipient: testAddress, }); - const { transactionReceipt, balanceDeltas } = - await sendTransactionGetBalances( - [...poolState.tokens.map(({address})=>address), poolState.address /*BPT*/], - client, - testAddress, - to, - call, - value, - ); - console.log(`transaction status: ${transactionReceipt.status}`); - console.log(`token amounts deltas per token: ${balanceDeltas}`); - return; -} + const { transactionReceipt, balanceDeltas } = + await sendTransactionGetBalances( + [ + ...poolState.tokens.map(({ address }) => address), + poolState.address /*BPT*/, + ], + client, + testAddress, + to, + call, + value, + ); + console.log(`transaction status: ${transactionReceipt.status}`); + console.log(`token amounts deltas per token: ${balanceDeltas}`); + return; +}; -join().then(()=>{}); \ No newline at end of file +join().then(() => {}); diff --git a/src/data/providers/balancer-api/index.ts b/src/data/providers/balancer-api/index.ts index a00216e3..1dc16831 100644 --- a/src/data/providers/balancer-api/index.ts +++ b/src/data/providers/balancer-api/index.ts @@ -1,17 +1,13 @@ -import { Pools } from "./modules/pool-state"; -import { BalancerApiClient } from "./client"; -import { ChainId } from "../../../utils"; +import { Pools } from './modules/pool-state'; +import { BalancerApiClient } from './client'; +import { ChainId } from '../../../utils'; export class BalancerApi { - - balancerApiClient: BalancerApiClient; - pools: Pools; - - - constructor(balancerApiUrl: string, chainId: ChainId){ - this.balancerApiClient = new BalancerApiClient(balancerApiUrl, chainId); - this.pools = new Pools(this.balancerApiClient); - } - - + balancerApiClient: BalancerApiClient; + pools: Pools; + + constructor(balancerApiUrl: string, chainId: ChainId) { + this.balancerApiClient = new BalancerApiClient(balancerApiUrl, chainId); + this.pools = new Pools(this.balancerApiClient); + } } diff --git a/src/data/providers/balancer-api/modules/pool-state/index.ts b/src/data/providers/balancer-api/modules/pool-state/index.ts index 3941bae0..eeab4cd7 100644 --- a/src/data/providers/balancer-api/modules/pool-state/index.ts +++ b/src/data/providers/balancer-api/modules/pool-state/index.ts @@ -1,8 +1,8 @@ import { BalancerApiClient } from '../../client'; -import { PoolStateInput } from "../../../../../entities"; +import { PoolStateInput } from '../../../../../entities'; export class Pools { - readonly poolStateQuery = `query GetPool($id: String!){ + readonly poolStateQuery = `query GetPool($id: String!){ poolGetPool(id:$id) { id address @@ -75,16 +75,17 @@ export class Pools { } }`; - constructor(private readonly balancerApiClient: BalancerApiClient) {} + constructor(private readonly balancerApiClient: BalancerApiClient) {} - async fetchPoolState(id: string): Promise { - const { - data: { poolGetPool }, - } = await this.balancerApiClient.fetch({ - query: this.poolStateQuery, - variables: { - id, - }}); - return poolGetPool; - } + async fetchPoolState(id: string): Promise { + const { + data: { poolGetPool }, + } = await this.balancerApiClient.fetch({ + query: this.poolStateQuery, + variables: { + id, + }, + }); + return poolGetPool; + } } From 73b19fccec020095c544341003b3bcadca9007e6 Mon Sep 17 00:00:00 2001 From: johngrantuk Date: Wed, 25 Oct 2023 13:11:19 +0100 Subject: [PATCH 18/18] chore: Add changeset. --- .changeset/neat-glasses-brush.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/neat-glasses-brush.md diff --git a/.changeset/neat-glasses-brush.md b/.changeset/neat-glasses-brush.md new file mode 100644 index 00000000..bad55acf --- /dev/null +++ b/.changeset/neat-glasses-brush.md @@ -0,0 +1,5 @@ +--- +"@balancer/sdk": minor +--- + +Adds Balancer API Provider. A utility module designed to fetch pool data from [API](https://github.com/beethovenxfi/beethovenx-backend/blob/v3-main/README.md#branching-and-deployment-environments).