Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "setprotocol.js",
"version": "1.2.8-rc7",
"version": "1.2.8-rc8",
"description": "A javascript library for interacting with the Set protocol",
"keywords": [
"setProtocol.js",
Expand Down Expand Up @@ -70,7 +70,7 @@
"set-protocol-contracts": "1.4.9-beta",
"set-protocol-oracles": "^1.0.16",
"set-protocol-strategies": "^1.1.39",
"set-protocol-viewers": "^1.0.12",
"set-protocol-viewers": "^1.0.13",
"set-protocol-utils": "^1.1.2",
"timekeeper": "^2.1.2",
"tiny-promisify": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/SetProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class SetProtocol {
this.exchangeIssuance = new ExchangeIssuanceAPI(this.web3, assertions, config);
this.factory = new FactoryAPI(this.web3, this.core, assertions, config);
this.issuance = new IssuanceAPI(this.web3, this.core, assertions);
this.oracle = new OracleAPI(this.web3);
this.oracle = new OracleAPI(this.web3, config);
this.priceFeed = new PriceFeedAPI(this.web3);
this.rebalancing = new RebalancingAPI(this.web3, assertions, this.core, config);
this.rebalancingManager = new RebalancingManagerAPI(this.web3, assertions, config);
Expand Down
18 changes: 16 additions & 2 deletions src/api/OracleAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import Web3 from 'web3';
import {
MovingAverageOracleWrapper,
MedianizerWrapper,
ProtocolViewerWrapper,
} from '../wrappers';
import { BigNumber } from '../util';
import {
Address
Address,
SetProtocolConfig
} from '../types/common';

/**
Expand All @@ -37,16 +39,18 @@ import {
export class OracleAPI {
private movingAverageOracleWrapper: MovingAverageOracleWrapper;
private medianizer: MedianizerWrapper;
private protocolViewer: ProtocolViewerWrapper;

/**
* Instantiates a new OracleAPI instance that contains methods for interacting with and updating price oracles
*
* @param web3 The Web3.js Provider instance you would like the SetProtocol.js library to use for interacting
* with the Ethereum network
*/
constructor(web3: Web3) {
constructor(web3: Web3, config: SetProtocolConfig) {
this.movingAverageOracleWrapper = new MovingAverageOracleWrapper(web3);
this.medianizer = new MedianizerWrapper(web3);
this.protocolViewer = new ProtocolViewerWrapper(web3, config.protocolViewerAddress);
}

/**
Expand Down Expand Up @@ -74,5 +78,15 @@ export class OracleAPI {

return new BigNumber(priceHex);
}

/**
* Returns the current price feed price
*
* @param oracleAddresses Addresseses of oracles to read
* @return Price in 18 decimal of the asset
*/
public async getOraclePricesAsync(oracleAddresses: Address[]): Promise<BigNumber[]> {
return await this.protocolViewer.batchFetchOraclePrices(oracleAddresses);
}
}

31 changes: 31 additions & 0 deletions src/api/RebalancingAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
RebalancingProgressDetails,
RebalancingProposalDetails,
RebalancingSetDetails,
RebalancingSetStatus,
SetProtocolConfig,
Tx,
TokenFlowsDetails,
Expand Down Expand Up @@ -710,6 +711,30 @@ export class RebalancingAPI {
return await this.protocolViewer.batchFetchUnitSharesAsync(rebalancingSetTokenAddresses);
}

/**
* Fetches the liquidators for multiple RebalancingSetToken contracts
*
* @param rebalancingSetTokenAddresses Addressses of the RebalancingSetToken contracts
* @return Array of current unitShares
*/
public async getLiquidatorsAsync(rebalancingSetTokenAddresses: Address[]): Promise<Address[]> {
this.assertGetLiquidatorAsync(rebalancingSetTokenAddresses);

return await this.protocolViewer.batchFetchLiquidator(rebalancingSetTokenAddresses);
}

/**
* Fetches the currentSet and State for multiple RebalancingSetToken contracts
*
* @param rebalancingSetTokenAddresses Addressses of the RebalancingSetToken contracts
* @return Array of current unitShares
*/
public async getRebalanceCompleteStateAsync(
rebalancingSetTokenAddresses: Address[]
): Promise<RebalancingSetStatus[]> {
return await this.protocolViewer.batchFetchStateAndCollateral(rebalancingSetTokenAddresses);
}

/**
* Fetches the current collateral set token address of a rebalancing set
*
Expand Down Expand Up @@ -971,6 +996,12 @@ export class RebalancingAPI {
});
}

private assertGetLiquidatorAsync(tokenAddresses: Address[]) {
tokenAddresses.forEach(tokenAddress => {
this.assert.schema.isValidAddress('rebalancingSetTokenAddress', tokenAddress);
});
}

private async assertUpdateManager(rebalancingSetTokenAddress: Address, newManager: Address, txOpts: Tx) {
this.assert.schema.isValidAddress('rebalancingSetTokenAddress', rebalancingSetTokenAddress);
this.assert.schema.isValidAddress('newManager', newManager);
Expand Down
2 changes: 2 additions & 0 deletions src/api/SocialTradingAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ export class SocialTradingAPI {
manager: rbSetInfo.manager,
feeRecipient: rbSetInfo.feeRecipient,
currentSet: rbSetInfo.currentSet,
liquidator: rbSetInfo.liquidator,
poolName: rbSetInfo.name,
poolSymbol: rbSetInfo.symbol,
unitShares: new BigNumber(rbSetInfo.unitShares),
Expand Down Expand Up @@ -754,6 +755,7 @@ export class SocialTradingAPI {
manager: rbSetInfo.manager,
feeRecipient: rbSetInfo.feeRecipient,
currentSet: rbSetInfo.currentSet,
liquidator: rbSetInfo.liquidator,
poolName: rbSetInfo.name,
poolSymbol: rbSetInfo.symbol,
unitShares: new BigNumber(rbSetInfo.unitShares),
Expand Down
5 changes: 5 additions & 0 deletions src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export interface Component {
unit: BigNumber;
}

export interface RebalancingSetStatus {
collateralSet: Address;
state: BigNumber;
}

export const RebalancingState = {
DEFAULT: new BigNumber(0),
PROPOSAL: new BigNumber(1),
Expand Down
2 changes: 2 additions & 0 deletions src/types/strategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export interface NewTradingPoolInfo {
manager: Address;
feeRecipient: Address;
currentSet: Address;
liquidator: Address;
poolName: string;
poolSymbol: string;
unitShares: BigNumber;
Expand All @@ -154,6 +155,7 @@ export interface NewTradingPoolV2Info {
manager: Address;
feeRecipient: Address;
currentSet: Address;
liquidator: Address;
poolName: string;
poolSymbol: string;
unitShares: BigNumber;
Expand Down
47 changes: 46 additions & 1 deletion src/wrappers/set_protocol/ProtocolViewerWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import Web3 from 'web3';

import { Address } from '../../types/common';
import { Address, RebalancingSetStatus } from '../../types/common';
import { BigNumber } from '../../util';
import { ProtocolContractWrapper } from './ProtocolContractWrapper';

Expand Down Expand Up @@ -228,6 +228,36 @@ export class ProtocolViewerWrapper {
return await protocolViewerInstance.fetchTradingPoolTWAPRebalanceDetails.callAsync(tradingPoolAddress);
}

/**
* Fetches all liquidator addresses for an array of RebalancingSetTokens
*
* @param rebalancingSetAddresses[] RebalancingSetToken contract instance addresses
*/
public async batchFetchLiquidator(
rebalancingSetAddresses: Address[],
): Promise<string[]> {
const protocolViewerInstance = await this.contracts.loadProtocolViewerContract(
this.protocolViewerAddress
);

return await protocolViewerInstance.batchFetchLiquidator.callAsync(rebalancingSetAddresses);
}

/**
* Fetches rebalanceState and currentSet for an array of RebalancingSetTokens
*
* @param rebalancingSetAddresses[] RebalancingSetToken contract instance addresses
*/
public async batchFetchStateAndCollateral(
rebalancingSetAddresses: Address[],
): Promise<RebalancingSetStatus[]> {
const protocolViewerInstance = await this.contracts.loadProtocolViewerContract(
this.protocolViewerAddress
);

return await protocolViewerInstance.batchFetchStateAndCollateral.callAsync(rebalancingSetAddresses);
}

/**
* Fetches all trading pool operators for an array of trading pools
*
Expand Down Expand Up @@ -347,4 +377,19 @@ export class ProtocolViewerWrapper {

return await protocolViewerInstance.batchFetchAssetPairCrossoverTimestamp.callAsync(managerAddresses);
}

/**
* Fetches oracle prices for a passed array of oracle addresses
*
* @param oracleAddresses[] Oracle addresses to read from
*/
public async batchFetchOraclePrices(
oracleAddresses: Address[],
): Promise<BigNumber[]> {
const protocolViewerInstance = await this.contracts.loadProtocolViewerContract(
this.protocolViewerAddress
);

return await protocolViewerInstance.batchFetchOraclePrices.callAsync(oracleAddresses);
}
}
84 changes: 64 additions & 20 deletions test/integration/api/OracleAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,36 @@ jest.unmock('set-protocol-contracts');
jest.setTimeout(30000);

import * as _ from 'lodash';
import * as ABIDecoder from 'abi-decoder';
import * as chai from 'chai';
import Web3 from 'web3';
import { Address, Web3Utils } from 'set-protocol-utils';
import * as setProtocolUtils from 'set-protocol-utils';
import {
ConstantPriceOracleContract,
HistoricalPriceFeedContract,
MedianContract,
MovingAverageOracleContract
MovingAverageOracleContract,
} from 'set-protocol-oracles';

import { ProtocolViewerContract } from 'set-protocol-viewers';

import ChaiSetup from '@test/helpers/chaiSetup';
import { OracleAPI } from '@src/api';
import { BigNumber } from '@src/util';
import { BigNumber, ether } from '@src/util';
import { SetProtocolConfig } from '@src/types/common';
import { DEFAULT_ACCOUNT } from '@src/constants/accounts';
import { TX_DEFAULTS } from '@src/constants';
import { NULL_ADDRESS } from '@src/constants';
import {
addPriceFeedOwnerToMedianizer,
deployConstantPriceOracleAsync,
deployHistoricalPriceFeedAsync,
deployMedianizerAsync,
deployMovingAverageOracleAsync,
deployProtocolViewerAsync,
updateMedianizerPriceAsync,
} from '@test/helpers';

const Core = require('set-protocol-contracts/dist/artifacts/ts/Core').Core;

ChaiSetup.configure();
const contract = require('truffle-contract');
const web3 = new Web3('http://localhost:8545');
const web3Utils = new Web3Utils(web3);
const { expect } = chai;
Expand All @@ -59,13 +61,10 @@ let currentSnapshotId: number;

const { SetProtocolTestUtils: SetTestUtils } = setProtocolUtils;

const coreContract = contract(Core);
coreContract.setProvider(web3.currentProvider);
coreContract.defaults(TX_DEFAULTS);


describe('OracleAPI', () => {
let oracleAPI: OracleAPI;
let protocolViewer: ProtocolViewerContract;

const priceFeedUpdateFrequency: BigNumber = new BigNumber(10);
const initialMedianizerEthPrice: BigNumber = new BigNumber(1000000);
Expand All @@ -78,18 +77,27 @@ describe('OracleAPI', () => {
new BigNumber(5000000),
];

beforeAll(() => {
ABIDecoder.addABI(coreContract.abi);
});

afterAll(() => {
ABIDecoder.removeABI(coreContract.abi);
});

beforeEach(async () => {
currentSnapshotId = await web3Utils.saveTestSnapshot();

oracleAPI = new OracleAPI(web3);
protocolViewer = await deployProtocolViewerAsync(web3);
const setProtocolConfig: SetProtocolConfig = {
coreAddress: NULL_ADDRESS,
transferProxyAddress: NULL_ADDRESS,
vaultAddress: NULL_ADDRESS,
setTokenFactoryAddress: NULL_ADDRESS,
rebalancingSetTokenFactoryAddress: NULL_ADDRESS,
kyberNetworkWrapperAddress: NULL_ADDRESS,
rebalanceAuctionModuleAddress: NULL_ADDRESS,
exchangeIssuanceModuleAddress: NULL_ADDRESS,
rebalancingSetIssuanceModule: NULL_ADDRESS,
rebalancingSetEthBidderAddress: NULL_ADDRESS,
rebalancingSetExchangeIssuanceModule: NULL_ADDRESS,
wrappedEtherAddress: NULL_ADDRESS,
protocolViewerAddress: protocolViewer.address,
};

oracleAPI = new OracleAPI(web3, setProtocolConfig);
});

afterEach(async () => {
Expand Down Expand Up @@ -176,4 +184,40 @@ describe('OracleAPI', () => {
expect(price).to.bignumber.equal(btcPrice);
});
});

describe('#batchFetchOraclePrices', async () => {
let component1Price: BigNumber;
let component2Price: BigNumber;

let component1Oracle: ConstantPriceOracleContract;
let component2Oracle: ConstantPriceOracleContract;

let subjectOracleAddresses: Address[];

beforeEach(async () => {
component1Price = ether(1);
component2Price = ether(2);

component1Oracle = await deployConstantPriceOracleAsync(web3, component1Price);
component2Oracle = await deployConstantPriceOracleAsync(web3, component2Price);

subjectOracleAddresses = [
component1Oracle.address,
component2Oracle.address,
];
});

async function subject(): Promise<BigNumber[]> {
return oracleAPI.getOraclePricesAsync(
subjectOracleAddresses,
);
}

it('fetches oracle prices', async () => {
const oraclePrices = await subject();

const expectedOraclePrices = [component1Price, component2Price];
expect(JSON.stringify(oraclePrices)).to.equal(JSON.stringify(expectedOraclePrices));
});
});
});
Loading