Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 6298a17

Browse files
authored
Added support for batch fetch functions. (#375)
* Added support for batch fetch functions. * Add config on instantiation. * Fix erring test.
1 parent c0a9b57 commit 6298a17

File tree

13 files changed

+387
-34
lines changed

13 files changed

+387
-34
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "setprotocol.js",
3-
"version": "1.2.8-rc7",
3+
"version": "1.2.8-rc8",
44
"description": "A javascript library for interacting with the Set protocol",
55
"keywords": [
66
"setProtocol.js",
@@ -70,7 +70,7 @@
7070
"set-protocol-contracts": "1.4.9-beta",
7171
"set-protocol-oracles": "^1.0.16",
7272
"set-protocol-strategies": "^1.1.39",
73-
"set-protocol-viewers": "^1.0.12",
73+
"set-protocol-viewers": "^1.0.13",
7474
"set-protocol-utils": "^1.1.2",
7575
"timekeeper": "^2.1.2",
7676
"tiny-promisify": "^1.0.0",

src/SetProtocol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class SetProtocol {
147147
this.exchangeIssuance = new ExchangeIssuanceAPI(this.web3, assertions, config);
148148
this.factory = new FactoryAPI(this.web3, this.core, assertions, config);
149149
this.issuance = new IssuanceAPI(this.web3, this.core, assertions);
150-
this.oracle = new OracleAPI(this.web3);
150+
this.oracle = new OracleAPI(this.web3, config);
151151
this.priceFeed = new PriceFeedAPI(this.web3);
152152
this.rebalancing = new RebalancingAPI(this.web3, assertions, this.core, config);
153153
this.rebalancingManager = new RebalancingManagerAPI(this.web3, assertions, config);

src/api/OracleAPI.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ import Web3 from 'web3';
2222
import {
2323
MovingAverageOracleWrapper,
2424
MedianizerWrapper,
25+
ProtocolViewerWrapper,
2526
} from '../wrappers';
2627
import { BigNumber } from '../util';
2728
import {
28-
Address
29+
Address,
30+
SetProtocolConfig
2931
} from '../types/common';
3032

3133
/**
@@ -37,16 +39,18 @@ import {
3739
export class OracleAPI {
3840
private movingAverageOracleWrapper: MovingAverageOracleWrapper;
3941
private medianizer: MedianizerWrapper;
42+
private protocolViewer: ProtocolViewerWrapper;
4043

4144
/**
4245
* Instantiates a new OracleAPI instance that contains methods for interacting with and updating price oracles
4346
*
4447
* @param web3 The Web3.js Provider instance you would like the SetProtocol.js library to use for interacting
4548
* with the Ethereum network
4649
*/
47-
constructor(web3: Web3) {
50+
constructor(web3: Web3, config: SetProtocolConfig) {
4851
this.movingAverageOracleWrapper = new MovingAverageOracleWrapper(web3);
4952
this.medianizer = new MedianizerWrapper(web3);
53+
this.protocolViewer = new ProtocolViewerWrapper(web3, config.protocolViewerAddress);
5054
}
5155

5256
/**
@@ -74,5 +78,15 @@ export class OracleAPI {
7478

7579
return new BigNumber(priceHex);
7680
}
81+
82+
/**
83+
* Returns the current price feed price
84+
*
85+
* @param oracleAddresses Addresseses of oracles to read
86+
* @return Price in 18 decimal of the asset
87+
*/
88+
public async getOraclePricesAsync(oracleAddresses: Address[]): Promise<BigNumber[]> {
89+
return await this.protocolViewer.batchFetchOraclePrices(oracleAddresses);
90+
}
7791
}
7892

src/api/RebalancingAPI.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
RebalancingProgressDetails,
4242
RebalancingProposalDetails,
4343
RebalancingSetDetails,
44+
RebalancingSetStatus,
4445
SetProtocolConfig,
4546
Tx,
4647
TokenFlowsDetails,
@@ -710,6 +711,30 @@ export class RebalancingAPI {
710711
return await this.protocolViewer.batchFetchUnitSharesAsync(rebalancingSetTokenAddresses);
711712
}
712713

714+
/**
715+
* Fetches the liquidators for multiple RebalancingSetToken contracts
716+
*
717+
* @param rebalancingSetTokenAddresses Addressses of the RebalancingSetToken contracts
718+
* @return Array of current unitShares
719+
*/
720+
public async getLiquidatorsAsync(rebalancingSetTokenAddresses: Address[]): Promise<Address[]> {
721+
this.assertGetLiquidatorAsync(rebalancingSetTokenAddresses);
722+
723+
return await this.protocolViewer.batchFetchLiquidator(rebalancingSetTokenAddresses);
724+
}
725+
726+
/**
727+
* Fetches the currentSet and State for multiple RebalancingSetToken contracts
728+
*
729+
* @param rebalancingSetTokenAddresses Addressses of the RebalancingSetToken contracts
730+
* @return Array of current unitShares
731+
*/
732+
public async getRebalanceCompleteStateAsync(
733+
rebalancingSetTokenAddresses: Address[]
734+
): Promise<RebalancingSetStatus[]> {
735+
return await this.protocolViewer.batchFetchStateAndCollateral(rebalancingSetTokenAddresses);
736+
}
737+
713738
/**
714739
* Fetches the current collateral set token address of a rebalancing set
715740
*
@@ -971,6 +996,12 @@ export class RebalancingAPI {
971996
});
972997
}
973998

999+
private assertGetLiquidatorAsync(tokenAddresses: Address[]) {
1000+
tokenAddresses.forEach(tokenAddress => {
1001+
this.assert.schema.isValidAddress('rebalancingSetTokenAddress', tokenAddress);
1002+
});
1003+
}
1004+
9741005
private async assertUpdateManager(rebalancingSetTokenAddress: Address, newManager: Address, txOpts: Tx) {
9751006
this.assert.schema.isValidAddress('rebalancingSetTokenAddress', rebalancingSetTokenAddress);
9761007
this.assert.schema.isValidAddress('newManager', newManager);

src/api/SocialTradingAPI.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ export class SocialTradingAPI {
723723
manager: rbSetInfo.manager,
724724
feeRecipient: rbSetInfo.feeRecipient,
725725
currentSet: rbSetInfo.currentSet,
726+
liquidator: rbSetInfo.liquidator,
726727
poolName: rbSetInfo.name,
727728
poolSymbol: rbSetInfo.symbol,
728729
unitShares: new BigNumber(rbSetInfo.unitShares),
@@ -754,6 +755,7 @@ export class SocialTradingAPI {
754755
manager: rbSetInfo.manager,
755756
feeRecipient: rbSetInfo.feeRecipient,
756757
currentSet: rbSetInfo.currentSet,
758+
liquidator: rbSetInfo.liquidator,
757759
poolName: rbSetInfo.name,
758760
poolSymbol: rbSetInfo.symbol,
759761
unitShares: new BigNumber(rbSetInfo.unitShares),

src/types/common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ export interface Component {
6565
unit: BigNumber;
6666
}
6767

68+
export interface RebalancingSetStatus {
69+
collateralSet: Address;
70+
state: BigNumber;
71+
}
72+
6873
export const RebalancingState = {
6974
DEFAULT: new BigNumber(0),
7075
PROPOSAL: new BigNumber(1),

src/types/strategies.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export interface NewTradingPoolInfo {
135135
manager: Address;
136136
feeRecipient: Address;
137137
currentSet: Address;
138+
liquidator: Address;
138139
poolName: string;
139140
poolSymbol: string;
140141
unitShares: BigNumber;
@@ -154,6 +155,7 @@ export interface NewTradingPoolV2Info {
154155
manager: Address;
155156
feeRecipient: Address;
156157
currentSet: Address;
158+
liquidator: Address;
157159
poolName: string;
158160
poolSymbol: string;
159161
unitShares: BigNumber;

src/wrappers/set_protocol/ProtocolViewerWrapper.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import Web3 from 'web3';
2020

21-
import { Address } from '../../types/common';
21+
import { Address, RebalancingSetStatus } from '../../types/common';
2222
import { BigNumber } from '../../util';
2323
import { ProtocolContractWrapper } from './ProtocolContractWrapper';
2424

@@ -228,6 +228,36 @@ export class ProtocolViewerWrapper {
228228
return await protocolViewerInstance.fetchTradingPoolTWAPRebalanceDetails.callAsync(tradingPoolAddress);
229229
}
230230

231+
/**
232+
* Fetches all liquidator addresses for an array of RebalancingSetTokens
233+
*
234+
* @param rebalancingSetAddresses[] RebalancingSetToken contract instance addresses
235+
*/
236+
public async batchFetchLiquidator(
237+
rebalancingSetAddresses: Address[],
238+
): Promise<string[]> {
239+
const protocolViewerInstance = await this.contracts.loadProtocolViewerContract(
240+
this.protocolViewerAddress
241+
);
242+
243+
return await protocolViewerInstance.batchFetchLiquidator.callAsync(rebalancingSetAddresses);
244+
}
245+
246+
/**
247+
* Fetches rebalanceState and currentSet for an array of RebalancingSetTokens
248+
*
249+
* @param rebalancingSetAddresses[] RebalancingSetToken contract instance addresses
250+
*/
251+
public async batchFetchStateAndCollateral(
252+
rebalancingSetAddresses: Address[],
253+
): Promise<RebalancingSetStatus[]> {
254+
const protocolViewerInstance = await this.contracts.loadProtocolViewerContract(
255+
this.protocolViewerAddress
256+
);
257+
258+
return await protocolViewerInstance.batchFetchStateAndCollateral.callAsync(rebalancingSetAddresses);
259+
}
260+
231261
/**
232262
* Fetches all trading pool operators for an array of trading pools
233263
*
@@ -347,4 +377,19 @@ export class ProtocolViewerWrapper {
347377

348378
return await protocolViewerInstance.batchFetchAssetPairCrossoverTimestamp.callAsync(managerAddresses);
349379
}
380+
381+
/**
382+
* Fetches oracle prices for a passed array of oracle addresses
383+
*
384+
* @param oracleAddresses[] Oracle addresses to read from
385+
*/
386+
public async batchFetchOraclePrices(
387+
oracleAddresses: Address[],
388+
): Promise<BigNumber[]> {
389+
const protocolViewerInstance = await this.contracts.loadProtocolViewerContract(
390+
this.protocolViewerAddress
391+
);
392+
393+
return await protocolViewerInstance.batchFetchOraclePrices.callAsync(oracleAddresses);
394+
}
350395
}

test/integration/api/OracleAPI.spec.ts

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,36 @@ jest.unmock('set-protocol-contracts');
2323
jest.setTimeout(30000);
2424

2525
import * as _ from 'lodash';
26-
import * as ABIDecoder from 'abi-decoder';
2726
import * as chai from 'chai';
2827
import Web3 from 'web3';
2928
import { Address, Web3Utils } from 'set-protocol-utils';
3029
import * as setProtocolUtils from 'set-protocol-utils';
3130
import {
31+
ConstantPriceOracleContract,
3232
HistoricalPriceFeedContract,
3333
MedianContract,
34-
MovingAverageOracleContract
34+
MovingAverageOracleContract,
3535
} from 'set-protocol-oracles';
3636

37+
import { ProtocolViewerContract } from 'set-protocol-viewers';
38+
3739
import ChaiSetup from '@test/helpers/chaiSetup';
3840
import { OracleAPI } from '@src/api';
39-
import { BigNumber } from '@src/util';
41+
import { BigNumber, ether } from '@src/util';
42+
import { SetProtocolConfig } from '@src/types/common';
4043
import { DEFAULT_ACCOUNT } from '@src/constants/accounts';
41-
import { TX_DEFAULTS } from '@src/constants';
44+
import { NULL_ADDRESS } from '@src/constants';
4245
import {
4346
addPriceFeedOwnerToMedianizer,
47+
deployConstantPriceOracleAsync,
4448
deployHistoricalPriceFeedAsync,
4549
deployMedianizerAsync,
4650
deployMovingAverageOracleAsync,
51+
deployProtocolViewerAsync,
4752
updateMedianizerPriceAsync,
4853
} from '@test/helpers';
4954

50-
const Core = require('set-protocol-contracts/dist/artifacts/ts/Core').Core;
51-
5255
ChaiSetup.configure();
53-
const contract = require('truffle-contract');
5456
const web3 = new Web3('http://localhost:8545');
5557
const web3Utils = new Web3Utils(web3);
5658
const { expect } = chai;
@@ -59,13 +61,10 @@ let currentSnapshotId: number;
5961

6062
const { SetProtocolTestUtils: SetTestUtils } = setProtocolUtils;
6163

62-
const coreContract = contract(Core);
63-
coreContract.setProvider(web3.currentProvider);
64-
coreContract.defaults(TX_DEFAULTS);
65-
6664

6765
describe('OracleAPI', () => {
6866
let oracleAPI: OracleAPI;
67+
let protocolViewer: ProtocolViewerContract;
6968

7069
const priceFeedUpdateFrequency: BigNumber = new BigNumber(10);
7170
const initialMedianizerEthPrice: BigNumber = new BigNumber(1000000);
@@ -78,18 +77,27 @@ describe('OracleAPI', () => {
7877
new BigNumber(5000000),
7978
];
8079

81-
beforeAll(() => {
82-
ABIDecoder.addABI(coreContract.abi);
83-
});
84-
85-
afterAll(() => {
86-
ABIDecoder.removeABI(coreContract.abi);
87-
});
88-
8980
beforeEach(async () => {
9081
currentSnapshotId = await web3Utils.saveTestSnapshot();
9182

92-
oracleAPI = new OracleAPI(web3);
83+
protocolViewer = await deployProtocolViewerAsync(web3);
84+
const setProtocolConfig: SetProtocolConfig = {
85+
coreAddress: NULL_ADDRESS,
86+
transferProxyAddress: NULL_ADDRESS,
87+
vaultAddress: NULL_ADDRESS,
88+
setTokenFactoryAddress: NULL_ADDRESS,
89+
rebalancingSetTokenFactoryAddress: NULL_ADDRESS,
90+
kyberNetworkWrapperAddress: NULL_ADDRESS,
91+
rebalanceAuctionModuleAddress: NULL_ADDRESS,
92+
exchangeIssuanceModuleAddress: NULL_ADDRESS,
93+
rebalancingSetIssuanceModule: NULL_ADDRESS,
94+
rebalancingSetEthBidderAddress: NULL_ADDRESS,
95+
rebalancingSetExchangeIssuanceModule: NULL_ADDRESS,
96+
wrappedEtherAddress: NULL_ADDRESS,
97+
protocolViewerAddress: protocolViewer.address,
98+
};
99+
100+
oracleAPI = new OracleAPI(web3, setProtocolConfig);
93101
});
94102

95103
afterEach(async () => {
@@ -176,4 +184,40 @@ describe('OracleAPI', () => {
176184
expect(price).to.bignumber.equal(btcPrice);
177185
});
178186
});
187+
188+
describe('#batchFetchOraclePrices', async () => {
189+
let component1Price: BigNumber;
190+
let component2Price: BigNumber;
191+
192+
let component1Oracle: ConstantPriceOracleContract;
193+
let component2Oracle: ConstantPriceOracleContract;
194+
195+
let subjectOracleAddresses: Address[];
196+
197+
beforeEach(async () => {
198+
component1Price = ether(1);
199+
component2Price = ether(2);
200+
201+
component1Oracle = await deployConstantPriceOracleAsync(web3, component1Price);
202+
component2Oracle = await deployConstantPriceOracleAsync(web3, component2Price);
203+
204+
subjectOracleAddresses = [
205+
component1Oracle.address,
206+
component2Oracle.address,
207+
];
208+
});
209+
210+
async function subject(): Promise<BigNumber[]> {
211+
return oracleAPI.getOraclePricesAsync(
212+
subjectOracleAddresses,
213+
);
214+
}
215+
216+
it('fetches oracle prices', async () => {
217+
const oraclePrices = await subject();
218+
219+
const expectedOraclePrices = [component1Price, component2Price];
220+
expect(JSON.stringify(oraclePrices)).to.equal(JSON.stringify(expectedOraclePrices));
221+
});
222+
});
179223
});

0 commit comments

Comments
 (0)