-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from balancer-labs/develop
Version 0.1.25
- Loading branch information
Showing
44 changed files
with
1,081 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
import { expect } from 'chai'; | ||
import { | ||
BalancerAPIArgsFormatter, | ||
GraphQLArgsBuilder, | ||
SubgraphArgsFormatter, | ||
} from './args-builder'; | ||
|
||
describe('Pool Query', () => { | ||
it('should be able to assemble a query for the subgraph', () => { | ||
const query = new GraphQLArgsBuilder({ | ||
first: 10, | ||
skip: 0, | ||
orderBy: 'totalLiquidity', | ||
orderDirection: 'desc', | ||
where: { | ||
totalShares: { | ||
gt: 0.01, | ||
}, | ||
id: { | ||
not_in: ['0xBAD', '0xDEF'], | ||
}, | ||
tokensList: { | ||
contains: ['0xBAL'], | ||
}, | ||
poolType: { | ||
not_in: ['Linear'], | ||
}, | ||
}, | ||
}); | ||
|
||
const expectedSubgraphQuery = { | ||
first: 10, | ||
skip: 0, | ||
orderBy: 'totalLiquidity', | ||
orderDirection: 'desc', | ||
where: { | ||
totalShares_gt: 0.01, | ||
id_not_in: ['0xBAD', '0xDEF'], | ||
tokensList_contains: ['0xBAL'], | ||
poolType_not_in: ['Linear'], | ||
}, | ||
}; | ||
|
||
const result = query.format(new SubgraphArgsFormatter()); | ||
expect(result).to.deep.equal(expectedSubgraphQuery); | ||
}); | ||
|
||
it('should be able to assemble a query for the Balancer API', () => { | ||
const query = new GraphQLArgsBuilder({ | ||
first: 10, | ||
skip: 0, | ||
orderBy: 'totalLiquidity', | ||
orderDirection: 'desc', | ||
where: { | ||
totalShares: { | ||
gt: 0.01, | ||
}, | ||
id: { | ||
not_in: ['0xBAD', '0xDEF'], | ||
}, | ||
tokensList: { | ||
contains: ['0xBAL'], | ||
}, | ||
poolType: { | ||
not_in: ['Linear'], | ||
}, | ||
}, | ||
}); | ||
|
||
const expectedSubgraphQuery = { | ||
first: 10, | ||
skip: 0, | ||
orderBy: 'totalLiquidity', | ||
orderDirection: 'desc', | ||
where: { | ||
totalShares: { | ||
gt: 0.01, | ||
}, | ||
id: { | ||
not_in: ['0xBAD', '0xDEF'], | ||
}, | ||
tokensList: { | ||
contains: ['0xBAL'], | ||
}, | ||
poolType: { | ||
not_in: ['Linear'], | ||
}, | ||
}, | ||
}; | ||
|
||
const result = query.format(new BalancerAPIArgsFormatter()); | ||
expect(result).to.deep.equal(expectedSubgraphQuery); | ||
}); | ||
|
||
describe('merge', () => { | ||
it('Should merge both query arguments into one', () => { | ||
const query = new GraphQLArgsBuilder({ | ||
first: 10, | ||
orderBy: 'totalLiquidity', | ||
where: { | ||
totalShares: { | ||
gt: 0.01, | ||
}, | ||
id: { | ||
not_in: ['0xBAD', '0xDEF'], | ||
}, | ||
}, | ||
}); | ||
|
||
const queryToMerge = new GraphQLArgsBuilder({ | ||
skip: 20, | ||
orderDirection: 'asc', | ||
where: { | ||
tokensList: { | ||
contains: ['0xBAL'], | ||
}, | ||
poolType: { | ||
not_in: ['Linear'], | ||
}, | ||
}, | ||
}); | ||
|
||
const expectedMergedQuery = new GraphQLArgsBuilder({ | ||
first: 10, | ||
orderBy: 'totalLiquidity', | ||
skip: 20, | ||
orderDirection: 'asc', | ||
where: { | ||
totalShares: { | ||
gt: 0.01, | ||
}, | ||
id: { | ||
not_in: ['0xBAD', '0xDEF'], | ||
}, | ||
tokensList: { | ||
contains: ['0xBAL'], | ||
}, | ||
poolType: { | ||
not_in: ['Linear'], | ||
}, | ||
}, | ||
}); | ||
|
||
const mergedQuery = query.merge(queryToMerge); | ||
|
||
expect(mergedQuery).to.deep.equal(expectedMergedQuery); | ||
}); | ||
|
||
it('Should overwrite query arguments from the query being merged in, and merge arrays', () => { | ||
const query = new GraphQLArgsBuilder({ | ||
first: 10, | ||
orderBy: 'totalLiquidity', | ||
orderDirection: 'desc', | ||
where: { | ||
totalShares: { | ||
gt: 0.01, | ||
}, | ||
id: { | ||
not_in: ['0xBAD', '0xDEF'], | ||
}, | ||
}, | ||
}); | ||
|
||
const queryToMerge = new GraphQLArgsBuilder({ | ||
first: 20, | ||
orderBy: 'totalShares', | ||
orderDirection: 'asc', | ||
where: { | ||
tokensList: { | ||
contains: ['0xBAL'], | ||
}, | ||
id: { | ||
not_in: ['0xNEW'], | ||
}, | ||
}, | ||
}); | ||
|
||
const expectedMergedQuery = new GraphQLArgsBuilder({ | ||
first: 20, | ||
orderBy: 'totalShares', | ||
orderDirection: 'asc', | ||
where: { | ||
totalShares: { | ||
gt: 0.01, | ||
}, | ||
tokensList: { | ||
contains: ['0xBAL'], | ||
}, | ||
id: { | ||
not_in: ['0xBAD', '0xDEF', '0xNEW'], | ||
}, | ||
}, | ||
}); | ||
|
||
const mergedQuery = query.merge(queryToMerge); | ||
|
||
expect(mergedQuery).to.deep.equal(expectedMergedQuery); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { mergeWith } from 'lodash'; | ||
import { GraphQLArgs, GraphQLArgsFormatter } from './types'; | ||
|
||
export * from './formatters'; | ||
|
||
export class GraphQLArgsBuilder { | ||
constructor(readonly args: GraphQLArgs) {} | ||
|
||
merge(other: GraphQLArgsBuilder): GraphQLArgsBuilder { | ||
const mergedArgs = mergeWith( | ||
this.args, | ||
other.args, | ||
(objValue: unknown, srcValue: unknown) => { | ||
if (Array.isArray(objValue)) { | ||
return objValue.concat(srcValue); | ||
} | ||
} | ||
); | ||
|
||
return new GraphQLArgsBuilder(mergedArgs); | ||
} | ||
|
||
format(formatter: GraphQLArgsFormatter): unknown { | ||
return formatter.format(this.args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { GraphQLArgs, GraphQLArgsFormatter } from '../types'; | ||
|
||
export class BalancerAPIArgsFormatter implements GraphQLArgsFormatter { | ||
format(args: GraphQLArgs): GraphQLArgs { | ||
return args; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './balancer-api'; | ||
export * from './subgraph'; |
Oops, something went wrong.