Skip to content

Commit b675616

Browse files
committed
Update test cases for stable oracle
1 parent b27b782 commit b675616

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

contracts/SecurityTokenRegistry.sol

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,22 @@ contract SecurityTokenRegistry is EternalStorage, Proxy {
214214
/**
215215
* @notice Converts USD fees into POLY amounts
216216
*/
217-
function _takeFee(bytes32 _feeType) internal returns (uint256, uint256){
217+
function _takeFee(bytes32 _feeType) internal returns (uint256, uint256) {
218+
(uint256 usdFee, uint256 polyFee) = getFees(_feeType);
219+
if (polyFee > 0)
220+
require(IERC20(getAddressValue(POLYTOKEN)).transferFrom(msg.sender, address(this), polyFee), "Insufficent allowance");
221+
return (usdFee, polyFee);
222+
}
223+
224+
/**
225+
* @notice Returns the usd & poly fee for a particular feetype
226+
* @param _feeType Key corresponding to fee type
227+
*/
228+
function getFees(bytes32 _feeType) public returns (uint256, uint256) {
218229
address polymathRegistry = getAddressValue(POLYMATHREGISTRY);
219230
uint256 polyRate = IOracle(IPolymathRegistry(polymathRegistry).getAddress(POLY_ORACLE)).getPrice();
220231
uint256 usdFee = getUintValue(_feeType);
221232
uint256 polyFee = DecimalMath.div(usdFee, polyRate);
222-
if (polyFee > 0)
223-
require(IERC20(getAddressValue(POLYTOKEN)).transferFrom(msg.sender, address(this), polyFee), "Insufficent allowance");
224233
return (usdFee, polyFee);
225234
}
226235

contracts/oracles/StableOracle.sol

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ contract StableOracle is IOracle, Ownable {
1515
uint256 public manualPrice;
1616

1717
/*solium-disable-next-line security/no-block-members*/
18-
event ChangeOracle(address _newOracle, address _oldOracle);
18+
event ChangeOracle(address _oldOracle, address _newOracle);
19+
event ChangeEvictPercentage(uint256 _oldEvictPercentage, uint256 _newEvictPercentage);
1920
event SetManualPrice(uint256 _oldPrice, uint256 _newPrice, uint256 _time);
2021
event SetManualOverride(bool _override, uint256 _time);
2122

@@ -36,10 +37,19 @@ contract StableOracle is IOracle, Ownable {
3637
function changeOracle(address _oracle) public onlyOwner {
3738
require(_oracle != address(0), "Invalid oracle");
3839
/*solium-disable-next-line security/no-block-members*/
39-
emit ChangeOracle(_oracle, address(oracle));
40+
emit ChangeOracle(address(oracle), _oracle);
4041
oracle = IOracle(_oracle);
4142
}
4243

44+
/**
45+
* @notice Updates eviction percentage
46+
* @param _evictPercentage Percentage multiplied by 10**16
47+
*/
48+
function changeEvictPercentage(uint256 _evictPercentage) public onlyOwner {
49+
emit ChangeEvictPercentage(evictPercentage, _evictPercentage);
50+
evictPercentage = _evictPercentage;
51+
}
52+
4353
/**
4454
* @notice Returns address of oracle currency (0x0 for ETH)
4555
*/

test/helpers/createInstances.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { encodeProxyCall, encodeModuleCall } from "./encodeCall";
22

33
const PolymathRegistry = artifacts.require("./PolymathRegistry.sol");
44
const MockOracle = artifacts.require("./MockOracle.sol");
5+
const StableOracle = artifacts.require("./StableOracle.sol");
56
const ModuleRegistry = artifacts.require("./ModuleRegistry.sol");
67
const ModuleRegistryProxy = artifacts.require("./ModuleRegistryProxy.sol");
78
const SecurityToken = artifacts.require("./SecurityToken.sol");
@@ -103,6 +104,7 @@ let I_STRGetter;
103104
let I_SignedTransferManagerFactory;
104105
let I_USDOracle;
105106
let I_POLYOracle;
107+
let I_StablePOLYOracle;
106108

107109
// Initial fee for ticker registry and security token registry
108110
const initRegFee = new BN(web3.utils.toWei("250"));
@@ -147,18 +149,24 @@ export async function setUpPolymathNetwork(account_polymath, token_owner) {
147149
I_SecurityTokenRegistry,
148150
I_SecurityTokenRegistryProxy,
149151
I_STRProxied,
150-
I_STRGetter
152+
I_STRGetter,
153+
I_USDOracle,
154+
I_POLYOracle,
155+
I_StablePOLYOracle
151156
);
152157
return Promise.all(tempArray);
153158
}
154159

155160
export async function addOracles(account_polymath) {
156161
let USDETH = new BN(500).mul(new BN(10).pow(new BN(18))); // 500 USD/ETH
157162
let USDPOLY = new BN(25).mul(new BN(10).pow(new BN(16))); // 0.25 USD/POLY
163+
let StableChange = new BN(10).mul(new BN(10).pow(new BN(16))); // 0.25 USD/POLY
158164
I_USDOracle = await MockOracle.new("0x0000000000000000000000000000000000000000", web3.utils.fromAscii("ETH"), web3.utils.fromAscii("USD"), USDETH, { from: account_polymath }); // 500 dollars per POLY
159165
I_POLYOracle = await MockOracle.new(I_PolyToken.address, web3.utils.fromAscii("POLY"), web3.utils.fromAscii("USD"), USDPOLY, { from: account_polymath }); // 25 cents per POLY
166+
I_StablePOLYOracle = await StableOracle.new(I_POLYOracle.address, StableChange, { from: account_polymath });
160167
await I_PolymathRegistry.changeAddress("EthUsdOracle", I_USDOracle.address, { from: account_polymath });
161168
await I_PolymathRegistry.changeAddress("PolyUsdOracle", I_POLYOracle.address, { from: account_polymath });
169+
await I_PolymathRegistry.changeAddress("StablePolyUsdOracle", I_StablePOLYOracle.address, { from: account_polymath });
162170
}
163171

164172
export async function deployPolyRegistryAndPolyToken(account_polymath, token_owner) {

test/n_security_token_registry.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ contract("SecurityTokenRegistry", async (accounts) => {
6161
let I_MRProxied;
6262
let I_STRGetter;
6363
let I_Getter;
64+
let I_USDOracle;
65+
let I_POLYOracle;
66+
let I_StablePOLYOracle;
6467

6568
// SecurityToken Details (Launched ST on the behalf of the issuer)
6669
const name = "Demo Token";
@@ -120,7 +123,10 @@ contract("SecurityTokenRegistry", async (accounts) => {
120123
I_SecurityTokenRegistry,
121124
I_SecurityTokenRegistryProxy,
122125
I_STRProxied,
123-
I_STRGetter
126+
I_STRGetter,
127+
I_USDOracle,
128+
I_POLYOracle,
129+
I_StablePOLYOracle
124130
] = instances;
125131

126132
// STEP 8: Deploy the CappedSTOFactory
@@ -357,6 +363,59 @@ contract("SecurityTokenRegistry", async (accounts) => {
357363
assert.equal(data[3], "");
358364
});
359365

366+
it("Should change ticker price based on oracle", async () => {
367+
let snap_Id = await takeSnapshot();
368+
let origPriceUSD = new BN(web3.utils.toWei("250"));
369+
let origPricePOLY = new BN(web3.utils.toWei("1000"));
370+
let currentRate = await I_POLYOracle.getPrice.call();
371+
console.log("Current Rate: " + currentRate);
372+
let feesTicker = await I_STRProxied.getFees.call("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
373+
let feesToken = await I_STRProxied.getFees.call("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
374+
assert.equal(feesTicker[0].toString(), origPriceUSD.toString());
375+
assert.equal(feesTicker[1].toString(), origPricePOLY.toString());
376+
assert.equal(feesToken[0].toString(), origPriceUSD.toString());
377+
assert.equal(feesToken[1].toString(), origPricePOLY.toString());
378+
await I_POLYOracle.changePrice(new BN(27).mul(new BN(10).pow(new BN(16))));
379+
await I_STRProxied.getFees("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
380+
feesTicker = await I_STRProxied.getFees.call("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
381+
feesToken = await I_STRProxied.getFees.call("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
382+
// No change as difference is less than 10%
383+
assert.equal(feesTicker[0].toString(), origPriceUSD.toString());
384+
assert.equal(feesTicker[1].toString(), origPricePOLY.toString());
385+
assert.equal(feesToken[0].toString(), origPriceUSD.toString());
386+
assert.equal(feesToken[1].toString(), origPricePOLY.toString());
387+
await I_POLYOracle.changePrice(new BN(20).mul(new BN(10).pow(new BN(16))));
388+
await I_STRProxied.getFees("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
389+
feesTicker = await I_STRProxied.getFees.call("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
390+
feesToken = await I_STRProxied.getFees.call("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
391+
let newPricePOLY = new BN(web3.utils.toWei("1250"));
392+
assert.equal(feesTicker[0].toString(), origPriceUSD.toString());
393+
assert.equal(feesTicker[1].toString(), newPricePOLY.toString());
394+
assert.equal(feesToken[0].toString(), origPriceUSD.toString());
395+
assert.equal(feesToken[1].toString(), newPricePOLY.toString());
396+
await I_POLYOracle.changePrice(new BN(21).mul(new BN(10).pow(new BN(16))));
397+
await I_STRProxied.getFees("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
398+
feesTicker = await I_STRProxied.getFees.call("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
399+
feesToken = await I_STRProxied.getFees.call("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
400+
// No change as difference is less than 10%
401+
assert.equal(feesTicker[0].toString(), origPriceUSD.toString());
402+
assert.equal(feesTicker[1].toString(), newPricePOLY.toString());
403+
assert.equal(feesToken[0].toString(), origPriceUSD.toString());
404+
assert.equal(feesToken[1].toString(), newPricePOLY.toString());
405+
await I_StablePOLYOracle.changeEvictPercentage(new BN(10).pow(new BN(16)));
406+
await I_STRProxied.getFees("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
407+
feesTicker = await I_STRProxied.getFees.call("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
408+
feesToken = await I_STRProxied.getFees.call("0x2fcc69711628630fb5a42566c68bd1092bc4aa26826736293969fddcd11cb2d2");
409+
// Change as eviction percentage updated
410+
// newPricePOLY = new BN(web3.utils.toWei("1250"));
411+
//1190.476190476190476190 = 250/0.21
412+
assert.equal(feesTicker[0].toString(), origPriceUSD.toString());
413+
assert.equal(feesTicker[1].toString(), "1190476190476190476190");
414+
assert.equal(feesToken[0].toString(), origPriceUSD.toString());
415+
assert.equal(feesToken[1].toString(), "1190476190476190476190");
416+
await revertToSnapshot(snap_Id);
417+
});
418+
360419
it("Should register the ticker when the tickerRegFee is 0", async () => {
361420
let snap_Id = await takeSnapshot();
362421
await I_STRProxied.changeTickerRegistrationFee(0, { from: account_polymath });

0 commit comments

Comments
 (0)