From 12fd4aac0d0ec3fec0c7512f8557bfb2e5c23abc Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Fri, 3 Jan 2020 21:16:40 +0100 Subject: [PATCH 01/23] Test and fix for medium severy issue found during audit --- .../SwaprateMatch/SwaprateMatchBase.sol | 2 +- contracts/test/DummySyntheticIdMock.sol | 45 +++++++ migrations/2_deploy_contract_set_for_tests.js | 27 +++- test/Matching/SwaprateMatch/SwaprateMatch.js | 118 ++++++++++++++++++ test/utils/swaprateOrders.js | 111 ++++++++++++++++ 5 files changed, 298 insertions(+), 5 deletions(-) create mode 100644 contracts/test/DummySyntheticIdMock.sol create mode 100644 test/Matching/SwaprateMatch/SwaprateMatch.js create mode 100644 test/utils/swaprateOrders.js diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol index e565c14..43b8d22 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol @@ -59,7 +59,7 @@ contract SwaprateMatchBase is MatchingErrors, LibSwaprateOrder, usingRegistry, R function withdraw(IERC20 _token) public nonReentrant { uint256 balance = balances[msg.sender][address(_token)]; balances[msg.sender][address(_token)] = 0; - IERC20(address(0)).transfer(msg.sender, balance); + _token.transfer(msg.sender, balance); } /// @notice This function checks whether order was canceled diff --git a/contracts/test/DummySyntheticIdMock.sol b/contracts/test/DummySyntheticIdMock.sol new file mode 100644 index 0000000..9061437 --- /dev/null +++ b/contracts/test/DummySyntheticIdMock.sol @@ -0,0 +1,45 @@ +pragma solidity ^0.5.4; +pragma experimental ABIEncoderV2; + +import "openzeppelin-solidity/contracts/math/SafeMath.sol"; + +import "../Interface/IDerivativeLogic.sol"; + +import "../Helpers/ExecutableByThirdParty.sol"; +import "../Helpers/HasCommission.sol"; + +contract DummySyntheticIdMock is IDerivativeLogic, ExecutableByThirdParty, HasCommission { + using SafeMath for uint256; + + constructor() public { + /* + { + "author": "opium.team", + "type": "synthetic", + "subtype": "none", + "description": "Dummy synthetic for testing purposes" + } + */ + emit MetadataSet("{\"author\":\"opium.team\",\"type\":\"synthetic\",\"subtype\":\"none\",\"description\":\"Dummy synthetic for testing purposes\"}"); + } + + function validateInput(Derivative memory _derivative) public view returns (bool) { + _derivative; + return true; + } + + function getMargin(Derivative memory _derivative) public view returns (uint256 buyerMargin, uint256 sellerMargin) { + buyerMargin = _derivative.margin; + sellerMargin = _derivative.margin; + } + + function getExecutionPayout(Derivative memory _derivative, uint256 _result) public view returns (uint256 buyerPayout, uint256 sellerPayout) { + buyerPayout = _derivative.margin; + sellerPayout = _derivative.margin; + _result; + } + + function isPool() public view returns (bool) { + return false; + } +} \ No newline at end of file diff --git a/migrations/2_deploy_contract_set_for_tests.js b/migrations/2_deploy_contract_set_for_tests.js index 0941327..ae67705 100644 --- a/migrations/2_deploy_contract_set_for_tests.js +++ b/migrations/2_deploy_contract_set_for_tests.js @@ -16,9 +16,11 @@ const TokenMinter = artifacts.require('TokenMinter') const TokenSpender = artifacts.require('TokenSpender') const Match = artifacts.require('Match') +const SwaprateMatch = artifacts.require('SwaprateMatch') // Test helpers const OptionCallSyntheticIdMock = artifacts.require('OptionCallSyntheticIdMock') +const DummySyntheticIdMock = artifacts.require('DummySyntheticIdMock') const TestToken = artifacts.require('TestToken') const WETH = artifacts.require('WETH') @@ -31,9 +33,9 @@ module.exports = async function(deployer, network, accounts) { let registry, core, tokenMinter, oracleAggregator, syntheticAggregator, tokenSpender - let match + let match, swaprateMatch - let optionCallSyntheticIdMock + let optionCallSyntheticIdMock, dummySyntheticIdMock let dai let weth @@ -45,7 +47,8 @@ module.exports = async function(deployer, network, accounts) { Core, TokenMinter, - Match + Match, + SwaprateMatch ]) }) .then(() => { @@ -73,13 +76,21 @@ module.exports = async function(deployer, network, accounts) { console.log('- Match was deployed at', match.address) }) }) + .then(() => { + return deployer + .deploy(SwaprateMatch, registry.address, { from: owner }) + .then(instance => { + swaprateMatch = instance + console.log('- SwaprateMatch was deployed at', swaprateMatch.address) + }) + }) .then(() => { return deployer .deploy(TokenSpender, owner, { from: owner }) .then(instance => { tokenSpender = instance console.log('- TokenSpender was deployed at', tokenSpender.address) - return tokenSpender.proposeWhitelist([ core.address, match.address ], { from: owner }) + return tokenSpender.proposeWhitelist([ core.address, match.address, swaprateMatch.address ], { from: owner }) }) }) .then(() => { @@ -120,6 +131,14 @@ module.exports = async function(deployer, network, accounts) { console.log('OptionCallSyntheticIdMock was deployed at', optionCallSyntheticIdMock.address) }) }) + .then(() => { + return deployer + .deploy(DummySyntheticIdMock, { from: owner }) + .then(instance => { + dummySyntheticIdMock = instance + console.log('DummySyntheticIdMock was deployed at', dummySyntheticIdMock.address) + }) + }) .then(() => { return deployer .deploy(TestToken, 'Opium DAI Token', 'DAI', 18, { from: owner }) diff --git a/test/Matching/SwaprateMatch/SwaprateMatch.js b/test/Matching/SwaprateMatch/SwaprateMatch.js new file mode 100644 index 0000000..bf9ad9d --- /dev/null +++ b/test/Matching/SwaprateMatch/SwaprateMatch.js @@ -0,0 +1,118 @@ +const SwaprateMatch = artifacts.require('SwaprateMatch') +const TestToken = artifacts.require('TestToken') +const DummySyntheticIdMock = artifacts.require('DummySyntheticIdMock') +const TokenSpender = artifacts.require('TokenSpender') +const WETH = artifacts.require('WETH') + +const { zeroAddress } = require('../../utils/addresses') +const { derivativeFactory } = require('../../utils/derivatives') +const swaprateOrders = require('../../utils/swaprateOrders') + +contract('SwaprateMatch', accounts => { + const owner = accounts[0] + const relayer = accounts[1] + const buyer = accounts[2] + const seller = accounts[3] + + let swaprateMatch, testToken, syntheticIdMock, tokenSpender, weth + + let derivative + + let orderFactory, commonOrder + + const big = amount => web3.utils.toWei(amount.toString(), 'ether') + + const big200 = big(200) + const big05 = big(0.5) + + before(async () => { + swaprateMatch = await SwaprateMatch.deployed() + testToken = await TestToken.deployed() + syntheticIdMock = await DummySyntheticIdMock.deployed() + tokenSpender = await TokenSpender.deployed() + weth = await WETH.deployed() + + derivative = derivativeFactory({ + margin: big200, // 200 tokens + endTime: ~~(Date.now() / 1000) + 60 * 10, // + 10 mins + params: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // strikePrice + oracleId: zeroAddress, + token: testToken.address, + syntheticId: syntheticIdMock.address + }) + + orderFactory = order => swaprateOrders.orderFactory({ + order, + relayer, + match: swaprateMatch, + }) + + commonOrder = (order) => orderFactory({ + syntheticId: derivative.syntheticId, + oracleId: derivative.oracleId, + token: derivative.token, + endTime: derivative.endTime, + ...order + }) + }) + + it('should successfully settle order with WETH fee to relayer', async () => { + // Deposit 0.5 eth to WETH to obtain WETH tokens + await web3.eth.sendTransaction({ + from: buyer, + to: WETH.address, + value: big05 + }) + await weth.approve(tokenSpender.address, big05, { from: buyer }) + + // Give buyer and seller enough tokens for trade + await testToken.transfer(buyer, big200, { from: owner }) + await testToken.transfer(seller, big200, { from: owner }) + + await testToken.approve(tokenSpender.address, big200, { from: buyer }) + await testToken.approve(tokenSpender.address, big200, { from: seller }) + + const relayerFeeBalanceBefore = await swaprateMatch.balances.call(relayer, weth.address) + + const leftOrder = await commonOrder({ + makerAddress: buyer, + + feeTokenAddress: weth.address, + + quantity: 1, + partialFill: 1, + + relayerFee: big05, + }) + + const rightOrder = await commonOrder({ + makerAddress: seller, + + quantity: 1, + partialFill: 1, + }) + + await swaprateMatch.create(leftOrder, rightOrder, derivative, { from: relayer }) + + const relayerFeeBalanceAfter = await swaprateMatch.balances.call(relayer, weth.address) + + assert.equal(relayerFeeBalanceBefore, 0, 'Relayer fee balance before is wrong') + assert.equal(relayerFeeBalanceAfter, big05, 'Relayer fee balance after is wrong') + }) + + it('should successfully withdraw relayer fee', async () => { + const relayerFeeBalanceBefore = await swaprateMatch.balances.call(relayer, weth.address) + const relayerWETHBalanceBefore = await weth.balanceOf(relayer) + + await swaprateMatch.withdraw(weth.address, { from: relayer }) + + const relayerFeeBalanceAfter = await swaprateMatch.balances.call(relayer, weth.address) + const relayerWETHBalanceAfter = await weth.balanceOf(relayer) + + assert.equal(relayerFeeBalanceBefore, big05, 'Relayer fee balance before is wrong') + assert.equal(relayerFeeBalanceAfter, 0, 'Relayer fee balance after is wrong') + + assert.equal(relayerWETHBalanceBefore, 0, 'Relayer WETH balance before is wrong') + assert.equal(relayerWETHBalanceAfter, big05, 'Relayer WETH balance after is wrong') + }) +}) diff --git a/test/utils/swaprateOrders.js b/test/utils/swaprateOrders.js new file mode 100644 index 0000000..4fa5e86 --- /dev/null +++ b/test/utils/swaprateOrders.js @@ -0,0 +1,111 @@ +const { zeroAddress } = require('./addresses') +const signature = require('./signature') + +const formOrderMessage = ({ order, match }) => { + return { + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'verifyingContract', type: 'address' }, + ], + Order: [ + { name: 'syntheticId', type: 'address' }, + { name: 'oracleId', type: 'address' }, + { name: 'token', type: 'address' }, + + { name: 'makerAddress', type: 'address' }, + { name: 'takerAddress', type: 'address' }, + + { name: 'senderAddress', type: 'address' }, + + { name: 'relayerAddress', type: 'address' }, + { name: 'affiliateAddress', type: 'address' }, + + { name: 'feeTokenAddress', type: 'address' }, + + { name: 'endTime', type: 'uint256' }, + + { name: 'quantity', type: 'uint256' }, + { name: 'partialFill', type: 'uint256' }, + + { name: 'param0', type: 'uint256' }, + { name: 'param1', type: 'uint256' }, + { name: 'param2', type: 'uint256' }, + { name: 'param3', type: 'uint256' }, + { name: 'param4', type: 'uint256' }, + { name: 'param5', type: 'uint256' }, + { name: 'param6', type: 'uint256' }, + { name: 'param7', type: 'uint256' }, + { name: 'param8', type: 'uint256' }, + { name: 'param9', type: 'uint256' }, + + { name: 'relayerFee', type: 'uint256' }, + { name: 'affiliateFee', type: 'uint256' }, + + { name: 'nonce', type: 'uint256' }, + ], + }, + domain: { + name: 'Opium Network', + version: '1', + verifyingContract: match.address, + }, + primaryType: 'Order', + message: order, + } +} + +const orderFactory = async ({ + order, + relayer, + match +}) => { + const def = { + syntheticId: zeroAddress, + oracleId: zeroAddress, + token: zeroAddress, + + makerAddress: zeroAddress, + takerAddress: zeroAddress, + + senderAddress: relayer, + + relayerAddress: relayer, + affiliateAddress: relayer, + + feeTokenAddress: zeroAddress, + + endTime: 0, + + quantity: 0, + partialFill: 0, + + param0: 0, + param1: 0, + param2: 0, + param3: 0, + param4: 0, + param5: 0, + param6: 0, + param7: 0, + param8: 0, + param9: 0, + + relayerFee: 0, + affiliateFee: 0, + + nonce: 0, + } + + const resultOrder = { + ...def, + ...order + } + + resultOrder.signature = await signature.sign(resultOrder.makerAddress, formOrderMessage({ order: resultOrder, match }), resultOrder.makerAddress) + + return resultOrder +} + +module.exports.orderFactory = orderFactory From 0e409ecc442a3dfec7d70715c3197df23fae3199 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Sun, 5 Jan 2020 15:46:00 +0100 Subject: [PATCH 02/23] Set compiler version to fixed 0.5.7 --- contracts/Core.sol | 2 +- contracts/Errors/CoreErrors.sol | 2 +- contracts/Errors/MatchingErrors.sol | 2 +- contracts/Errors/OracleAggregatorErrors.sol | 2 +- contracts/Errors/RegistryErrors.sol | 2 +- contracts/Errors/SyntheticAggregatorErrors.sol | 2 +- contracts/Errors/usingRegistryErrors.sol | 2 +- contracts/Helpers/ExecutableByThirdParty.sol | 2 +- contracts/Helpers/HasCommission.sol | 2 +- contracts/Interface/IDerivativeLogic.sol | 2 +- contracts/Interface/IOracleId.sol | 2 +- contracts/Lib/LibCommission.sol | 2 +- contracts/Lib/LibDerivative.sol | 2 +- contracts/Lib/LibEIP712.sol | 2 +- contracts/Lib/Whitelisted.sol | 2 +- contracts/Lib/WhitelistedWithGovernance.sol | 2 +- contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol | 2 +- contracts/Lib/usingRegistry.sol | 2 +- contracts/Matching/Match/LibOrder.sol | 2 +- contracts/Matching/Match/Match.sol | 2 +- contracts/Matching/Match/MatchCreate.sol | 2 +- contracts/Matching/Match/MatchLogic.sol | 2 +- contracts/Matching/Match/MatchPool.sol | 2 +- contracts/Matching/Match/MatchSwap.sol | 2 +- contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol | 2 +- contracts/Matching/SwaprateMatch/SwaprateMatch.sol | 2 +- contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol | 2 +- contracts/Migrations.sol | 2 +- contracts/OracleAggregator.sol | 2 +- contracts/Registry.sol | 2 +- contracts/SyntheticAggregator.sol | 2 +- contracts/TokenMinter.sol | 2 +- contracts/TokenSpender.sol | 2 +- contracts/test/DummySyntheticIdMock.sol | 2 +- contracts/test/OptionCallSyntheticIdMock.sol | 2 +- contracts/test/OracleIdMock.sol | 2 +- contracts/test/TestToken.sol | 2 +- contracts/test/WETH.sol | 2 +- 38 files changed, 38 insertions(+), 38 deletions(-) diff --git a/contracts/Core.sol b/contracts/Core.sol index 7509ebb..314f74c 100644 --- a/contracts/Core.sol +++ b/contracts/Core.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; diff --git a/contracts/Errors/CoreErrors.sol b/contracts/Errors/CoreErrors.sol index edc3441..d41fb9b 100644 --- a/contracts/Errors/CoreErrors.sol +++ b/contracts/Errors/CoreErrors.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; contract CoreErrors { string constant internal ERROR_CORE_NOT_POOL = "CORE:NOT_POOL"; diff --git a/contracts/Errors/MatchingErrors.sol b/contracts/Errors/MatchingErrors.sol index 01ea8c9..5fd1ad4 100644 --- a/contracts/Errors/MatchingErrors.sol +++ b/contracts/Errors/MatchingErrors.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; contract MatchingErrors { string constant internal ERROR_MATCH_CANCELLATION_NOT_ALLOWED = "MATCH:CANCELLATION_NOT_ALLOWED"; diff --git a/contracts/Errors/OracleAggregatorErrors.sol b/contracts/Errors/OracleAggregatorErrors.sol index 9c321fa..16d8156 100644 --- a/contracts/Errors/OracleAggregatorErrors.sol +++ b/contracts/Errors/OracleAggregatorErrors.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; contract OracleAggregatorErrors { string constant internal ERROR_ORACLE_AGGREGATOR_NOT_ENOUGH_ETHER = "ORACLE_AGGREGATOR:NOT_ENOUGH_ETHER"; diff --git a/contracts/Errors/RegistryErrors.sol b/contracts/Errors/RegistryErrors.sol index 8a9ad7e..af2408c 100644 --- a/contracts/Errors/RegistryErrors.sol +++ b/contracts/Errors/RegistryErrors.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; contract RegistryErrors { string constant internal ERROR_REGISTRY_ONLY_INITIALIZER = "REGISTRY:ONLY_INITIALIZER"; diff --git a/contracts/Errors/SyntheticAggregatorErrors.sol b/contracts/Errors/SyntheticAggregatorErrors.sol index 37ad171..77b90a3 100644 --- a/contracts/Errors/SyntheticAggregatorErrors.sol +++ b/contracts/Errors/SyntheticAggregatorErrors.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; contract SyntheticAggregatorErrors { string constant internal ERROR_SYNTHETIC_AGGREGATOR_DERIVATIVE_HASH_NOT_MATCH = "SYNTHETIC_AGGREGATOR:DERIVATIVE_HASH_NOT_MATCH"; diff --git a/contracts/Errors/usingRegistryErrors.sol b/contracts/Errors/usingRegistryErrors.sol index dc28d5b..a25594c 100644 --- a/contracts/Errors/usingRegistryErrors.sol +++ b/contracts/Errors/usingRegistryErrors.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; contract usingRegistryErrors { string constant internal ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED = "USING_REGISTRY:ONLY_CORE_ALLOWED"; diff --git a/contracts/Helpers/ExecutableByThirdParty.sol b/contracts/Helpers/ExecutableByThirdParty.sol index 4480857..03de83c 100644 --- a/contracts/Helpers/ExecutableByThirdParty.sol +++ b/contracts/Helpers/ExecutableByThirdParty.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; /// @title Opium.Helpers.ExecutableByThirdParty contract helps to syntheticId development and responsible for getting and setting thirdparty execution settings contract ExecutableByThirdParty { diff --git a/contracts/Helpers/HasCommission.sol b/contracts/Helpers/HasCommission.sol index b57f77f..afbb2a1 100644 --- a/contracts/Helpers/HasCommission.sol +++ b/contracts/Helpers/HasCommission.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; /// @title Opium.Helpers.HasCommission contract helps to syntheticId development and responsible for commission and author address contract HasCommission { diff --git a/contracts/Interface/IDerivativeLogic.sol b/contracts/Interface/IDerivativeLogic.sol index 4b53261..fb89b35 100644 --- a/contracts/Interface/IDerivativeLogic.sol +++ b/contracts/Interface/IDerivativeLogic.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "../Lib/LibDerivative.sol"; diff --git a/contracts/Interface/IOracleId.sol b/contracts/Interface/IOracleId.sol index 9c6cc6f..fd7c342 100644 --- a/contracts/Interface/IOracleId.sol +++ b/contracts/Interface/IOracleId.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; /// @title Opium.Interface.IOracleId contract is an interface that every oracleId should implement interface IOracleId { diff --git a/contracts/Lib/LibCommission.sol b/contracts/Lib/LibCommission.sol index 5884912..c20f1a3 100644 --- a/contracts/Lib/LibCommission.sol +++ b/contracts/Lib/LibCommission.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; /// @title Opium.Lib.LibCommission contract defines constants for Opium commissions contract LibCommission { diff --git a/contracts/Lib/LibDerivative.sol b/contracts/Lib/LibDerivative.sol index 7334a9d..cf26f9b 100644 --- a/contracts/Lib/LibDerivative.sol +++ b/contracts/Lib/LibDerivative.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; /// @title Opium.Lib.LibDerivative contract should be inherited by contracts that use Derivative structure and calculate derivativeHash diff --git a/contracts/Lib/LibEIP712.sol b/contracts/Lib/LibEIP712.sol index f19595d..d4522b4 100644 --- a/contracts/Lib/LibEIP712.sol +++ b/contracts/Lib/LibEIP712.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; /// @title Opium.Lib.LibEIP712 contract implements the domain of EIP712 for meta transactions contract LibEIP712 { diff --git a/contracts/Lib/Whitelisted.sol b/contracts/Lib/Whitelisted.sol index 9810cf6..e715602 100644 --- a/contracts/Lib/Whitelisted.sol +++ b/contracts/Lib/Whitelisted.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; /// @title Opium.Lib.Whitelisted contract implements whitelist with modifier to restrict access to only whitelisted addresses contract Whitelisted { diff --git a/contracts/Lib/WhitelistedWithGovernance.sol b/contracts/Lib/WhitelistedWithGovernance.sol index 3127d76..4a2172d 100644 --- a/contracts/Lib/WhitelistedWithGovernance.sol +++ b/contracts/Lib/WhitelistedWithGovernance.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; import "./Whitelisted.sol"; diff --git a/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol b/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol index 93d13ca..6d6f648 100644 --- a/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol +++ b/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; import "./WhitelistedWithGovernance.sol"; diff --git a/contracts/Lib/usingRegistry.sol b/contracts/Lib/usingRegistry.sol index 1d27847..505d827 100644 --- a/contracts/Lib/usingRegistry.sol +++ b/contracts/Lib/usingRegistry.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; import "../Registry.sol"; diff --git a/contracts/Matching/Match/LibOrder.sol b/contracts/Matching/Match/LibOrder.sol index 940ae6b..4234efb 100644 --- a/contracts/Matching/Match/LibOrder.sol +++ b/contracts/Matching/Match/LibOrder.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "../../Lib/LibEIP712.sol"; diff --git a/contracts/Matching/Match/Match.sol b/contracts/Matching/Match/Match.sol index c54e9bd..74b9751 100644 --- a/contracts/Matching/Match/Match.sol +++ b/contracts/Matching/Match/Match.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "./MatchCreate.sol"; diff --git a/contracts/Matching/Match/MatchCreate.sol b/contracts/Matching/Match/MatchCreate.sol index 86a5e25..e6a2d7a 100644 --- a/contracts/Matching/Match/MatchCreate.sol +++ b/contracts/Matching/Match/MatchCreate.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "../../Lib/LibDerivative.sol"; diff --git a/contracts/Matching/Match/MatchLogic.sol b/contracts/Matching/Match/MatchLogic.sol index 0f8adb7..409bef3 100644 --- a/contracts/Matching/Match/MatchLogic.sol +++ b/contracts/Matching/Match/MatchLogic.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; diff --git a/contracts/Matching/Match/MatchPool.sol b/contracts/Matching/Match/MatchPool.sol index a807746..3e91821 100644 --- a/contracts/Matching/Match/MatchPool.sol +++ b/contracts/Matching/Match/MatchPool.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "../../Lib/LibDerivative.sol"; diff --git a/contracts/Matching/Match/MatchSwap.sol b/contracts/Matching/Match/MatchSwap.sol index b1f0d8a..1301b92 100644 --- a/contracts/Matching/Match/MatchSwap.sol +++ b/contracts/Matching/Match/MatchSwap.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "./MatchLogic.sol"; diff --git a/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol b/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol index 781fa67..0b0f274 100644 --- a/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol +++ b/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "../../Lib/LibEIP712.sol"; diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatch.sol b/contracts/Matching/SwaprateMatch/SwaprateMatch.sol index bd0eddd..adfe7e8 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatch.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatch.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "../../Lib/LibDerivative.sol"; diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol index 43b8d22..61323a9 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; diff --git a/contracts/Migrations.sol b/contracts/Migrations.sol index 89d6ee3..8ad11ad 100644 --- a/contracts/Migrations.sol +++ b/contracts/Migrations.sol @@ -1,4 +1,4 @@ -pragma solidity >=0.4.21 <0.6.0; +pragma solidity 0.5.7; contract Migrations { address public owner; diff --git a/contracts/OracleAggregator.sol b/contracts/OracleAggregator.sol index 059bf0d..a89808f 100644 --- a/contracts/OracleAggregator.sol +++ b/contracts/OracleAggregator.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; import "openzeppelin-solidity/contracts/utils/ReentrancyGuard.sol"; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; diff --git a/contracts/Registry.sol b/contracts/Registry.sol index b43e67e..f412971 100644 --- a/contracts/Registry.sol +++ b/contracts/Registry.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; import "./Errors/RegistryErrors.sol"; diff --git a/contracts/SyntheticAggregator.sol b/contracts/SyntheticAggregator.sol index cbb8fac..22bb950 100644 --- a/contracts/SyntheticAggregator.sol +++ b/contracts/SyntheticAggregator.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/utils/ReentrancyGuard.sol"; diff --git a/contracts/TokenMinter.sol b/contracts/TokenMinter.sol index b53dc7e..e0e2e37 100644 --- a/contracts/TokenMinter.sol +++ b/contracts/TokenMinter.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; import "erc721o/contracts/ERC721OBackwardCompatible.sol"; diff --git a/contracts/TokenSpender.sol b/contracts/TokenSpender.sol index da1fa35..a3b2b78 100644 --- a/contracts/TokenSpender.sol +++ b/contracts/TokenSpender.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; import "openzeppelin-solidity/contracts/token/ERC20/SafeERC20.sol"; diff --git a/contracts/test/DummySyntheticIdMock.sol b/contracts/test/DummySyntheticIdMock.sol index 9061437..ce6f43e 100644 --- a/contracts/test/DummySyntheticIdMock.sol +++ b/contracts/test/DummySyntheticIdMock.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; diff --git a/contracts/test/OptionCallSyntheticIdMock.sol b/contracts/test/OptionCallSyntheticIdMock.sol index c5ba3e9..4bfc7c1 100644 --- a/contracts/test/OptionCallSyntheticIdMock.sol +++ b/contracts/test/OptionCallSyntheticIdMock.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; diff --git a/contracts/test/OracleIdMock.sol b/contracts/test/OracleIdMock.sol index 1f2fb32..829bb17 100644 --- a/contracts/test/OracleIdMock.sol +++ b/contracts/test/OracleIdMock.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; import "../Interface/IOracleId.sol"; import "../Lib/usingRegistry.sol"; diff --git a/contracts/test/TestToken.sol b/contracts/test/TestToken.sol index 0474418..a9a5ddc 100644 --- a/contracts/test/TestToken.sol +++ b/contracts/test/TestToken.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; contract ERC20Basic { uint256 public totalSupply; diff --git a/contracts/test/WETH.sol b/contracts/test/WETH.sol index f0048d7..306e377 100644 --- a/contracts/test/WETH.sol +++ b/contracts/test/WETH.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.5.4; +pragma solidity 0.5.7; contract WETH { string public name = "Wrapped Ether"; From 4973e26792cd24833fb92a72e46b6bd5b8b3d0c2 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Sun, 5 Jan 2020 16:02:32 +0100 Subject: [PATCH 03/23] Gas usage optimization issues --- contracts/Core.sol | 5 ++++- contracts/Lib/Whitelisted.sol | 3 ++- contracts/Lib/WhitelistedWithGovernance.sol | 4 ++-- contracts/Matching/Match/MatchLogic.sol | 14 ++++++++++---- .../Matching/SwaprateMatch/SwaprateMatchBase.sol | 14 ++++++++++---- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/contracts/Core.sol b/contracts/Core.sol index 314f74c..b1dd237 100644 --- a/contracts/Core.sol +++ b/contracts/Core.sol @@ -516,9 +516,12 @@ contract Core is LibDerivative, LibCommission, usingRegistry, CoreErrors, Reentr // authorFee = fee - opiumFee uint256 authorFee = fee.sub(opiumFee); + // Get opium address + address opiumAddress = registry.getOpiumAddress(); + // Update feeVault for Opium team // feesVault[opium][token] += opiumFee - feesVaults[registry.getOpiumAddress()][_derivative.token] = feesVaults[registry.getOpiumAddress()][_derivative.token].add(opiumFee); + feesVaults[opiumAddress][_derivative.token] = feesVaults[opiumAddress][_derivative.token].add(opiumFee); // Update feeVault for `syntheticId` author // feeVault[author][token] += authorFee diff --git a/contracts/Lib/Whitelisted.sol b/contracts/Lib/Whitelisted.sol index e715602..291fa01 100644 --- a/contracts/Lib/Whitelisted.sol +++ b/contracts/Lib/Whitelisted.sol @@ -11,7 +11,8 @@ contract Whitelisted { bool allowed = false; // Going through whitelisted addresses array - for (uint256 i = 0; i < whitelist.length; i++) { + uint256 whitelistLength = whitelist.length; + for (uint256 i = 0; i < whitelistLength; i++) { // If `msg.sender` is met within whitelisted addresses, raise the flag and exit the loop if (whitelist[i] == msg.sender) { allowed = true; diff --git a/contracts/Lib/WhitelistedWithGovernance.sol b/contracts/Lib/WhitelistedWithGovernance.sol index 4a2172d..ee45224 100644 --- a/contracts/Lib/WhitelistedWithGovernance.sol +++ b/contracts/Lib/WhitelistedWithGovernance.sol @@ -50,13 +50,13 @@ contract WhitelistedWithGovernance is Whitelisted { if (!initialized) { initialized = true; whitelist = _whitelist; - emit Committed(whitelist); + emit Committed(_whitelist); // Otherwise save current time as timestamp of proposal, save proposed whitelist and emit event } else { proposalTime = now; proposedWhitelist = _whitelist; - emit Proposed(proposedWhitelist); + emit Proposed(_whitelist); } } diff --git a/contracts/Matching/Match/MatchLogic.sol b/contracts/Matching/Match/MatchLogic.sol index 409bef3..9d5d5a4 100644 --- a/contracts/Matching/Match/MatchLogic.sol +++ b/contracts/Matching/Match/MatchLogic.sol @@ -147,23 +147,29 @@ contract MatchLogic is MatchingErrors, LibOrder, usingRegistry, ReentrancyGuard // Create instance of fee token IERC20 feeToken = IERC20(_order.feeTokenAddress); + // Create instance of TokenSpender + TokenSpender tokenSpender = TokenSpender(registry.getTokenSpender()); + // Check if user has enough token approval to pay the fees - require(feeToken.allowance(_order.makerAddress, registry.getTokenSpender()) >= fees, ERROR_MATCH_NOT_ENOUGH_ALLOWED_FEES); + require(feeToken.allowance(_order.makerAddress, address(tokenSpender)) >= fees, ERROR_MATCH_NOT_ENOUGH_ALLOWED_FEES); // Transfer fee - TokenSpender(registry.getTokenSpender()).claimTokens(feeToken, _order.makerAddress, address(this), fees); + tokenSpender.claimTokens(feeToken, _order.makerAddress, address(this), fees); + + // Get opium address + address opiumAddress = registry.getOpiumAddress(); // Add commission to relayer balance, or to opium balance if relayer is not set if (_order.relayerAddress != address(0)) { balances[_order.relayerAddress][_order.feeTokenAddress] = balances[_order.relayerAddress][_order.feeTokenAddress].add(_order.relayerFee); } else { - balances[registry.getOpiumAddress()][_order.feeTokenAddress] = balances[registry.getOpiumAddress()][_order.feeTokenAddress].add(_order.relayerFee); + balances[opiumAddress][_order.feeTokenAddress] = balances[opiumAddress][_order.feeTokenAddress].add(_order.relayerFee); } // Add commission to affiliate balance, or to opium balance if affiliate is not set if (_order.affiliateAddress != address(0)) { balances[_order.affiliateAddress][_order.feeTokenAddress] = balances[_order.affiliateAddress][_order.feeTokenAddress].add(_order.affiliateFee); } else { - balances[registry.getOpiumAddress()][_order.feeTokenAddress] = balances[registry.getOpiumAddress()][_order.feeTokenAddress].add(_order.affiliateFee); + balances[opiumAddress][_order.feeTokenAddress] = balances[opiumAddress][_order.feeTokenAddress].add(_order.affiliateFee); } // Mark the fee of token as taken diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol index 61323a9..167f733 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol @@ -129,23 +129,29 @@ contract SwaprateMatchBase is MatchingErrors, LibSwaprateOrder, usingRegistry, R // Create instance of fee token IERC20 feeToken = IERC20(_order.feeTokenAddress); + // Create instance of TokenSpender + TokenSpender tokenSpender = TokenSpender(registry.getTokenSpender()); + // Check if user has enough token approval to pay the fees - require(feeToken.allowance(_order.makerAddress, registry.getTokenSpender()) >= fees, ERROR_MATCH_NOT_ENOUGH_ALLOWED_FEES); + require(feeToken.allowance(_order.makerAddress, address(tokenSpender)) >= fees, ERROR_MATCH_NOT_ENOUGH_ALLOWED_FEES); // Transfer fee - TokenSpender(registry.getTokenSpender()).claimTokens(feeToken, _order.makerAddress, address(this), fees); + tokenSpender.claimTokens(feeToken, _order.makerAddress, address(this), fees); + + // Get opium address + address opiumAddress = registry.getOpiumAddress(); // Add commission to relayer balance, or to opium balance if relayer is not set if (_order.relayerAddress != address(0)) { balances[_order.relayerAddress][_order.feeTokenAddress] = balances[_order.relayerAddress][_order.feeTokenAddress].add(_order.relayerFee); } else { - balances[registry.getOpiumAddress()][_order.feeTokenAddress] = balances[registry.getOpiumAddress()][_order.feeTokenAddress].add(_order.relayerFee); + balances[opiumAddress][_order.feeTokenAddress] = balances[opiumAddress][_order.feeTokenAddress].add(_order.relayerFee); } // Add commission to affiliate balance, or to opium balance if affiliate is not set if (_order.affiliateAddress != address(0)) { balances[_order.affiliateAddress][_order.feeTokenAddress] = balances[_order.affiliateAddress][_order.feeTokenAddress].add(_order.affiliateFee); } else { - balances[registry.getOpiumAddress()][_order.feeTokenAddress] = balances[registry.getOpiumAddress()][_order.feeTokenAddress].add(_order.affiliateFee); + balances[opiumAddress][_order.feeTokenAddress] = balances[opiumAddress][_order.feeTokenAddress].add(_order.affiliateFee); } // Mark the fee of token as taken From 207aa4a8e650f50338cc238929f2df0699d7f8e7 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Sun, 5 Jan 2020 16:45:48 +0100 Subject: [PATCH 04/23] Fixed code logic issue with whitelist initialization check --- contracts/Lib/WhitelistedWithGovernance.sol | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/contracts/Lib/WhitelistedWithGovernance.sol b/contracts/Lib/WhitelistedWithGovernance.sol index ee45224..c099b97 100644 --- a/contracts/Lib/WhitelistedWithGovernance.sol +++ b/contracts/Lib/WhitelistedWithGovernance.sol @@ -18,9 +18,6 @@ contract WhitelistedWithGovernance is Whitelisted { // Governor address address public governor; - // Contract initialization flag - bool public initialized = false; - // Timestamp of last proposal uint256 public proposalTime = 0; // Proposed whitelist @@ -46,9 +43,9 @@ contract WhitelistedWithGovernance is Whitelisted { // Restrict empty proposals require(_whitelist.length != 0, "Can't be empty"); + // Consider empty whitelist as not initialized, as proposing of empty whitelists is not allowed // If whitelist has never been initialized, we set whitelist right away without proposal - if (!initialized) { - initialized = true; + if (whitelist.length == 0) { whitelist = _whitelist; emit Committed(_whitelist); From e141be9fe261aedc078557bf1742e03682dbed38 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Sun, 5 Jan 2020 16:53:34 +0100 Subject: [PATCH 05/23] Input validation fixed in governor ad opium addresses --- contracts/Lib/WhitelistedWithGovernance.sol | 1 + contracts/Registry.sol | 1 + 2 files changed, 2 insertions(+) diff --git a/contracts/Lib/WhitelistedWithGovernance.sol b/contracts/Lib/WhitelistedWithGovernance.sol index c099b97..78774c5 100644 --- a/contracts/Lib/WhitelistedWithGovernance.sol +++ b/contracts/Lib/WhitelistedWithGovernance.sol @@ -76,6 +76,7 @@ contract WhitelistedWithGovernance is Whitelisted { /// @notice This function allows governor to transfer governance to a new governor and emits event /// @param _governor address Address of new governor function setGovernor(address _governor) public onlyGovernor { + require(_governor != address(0), "Can't set zero address"); governor = _governor; emit GovernorSet(governor); } diff --git a/contracts/Registry.sol b/contracts/Registry.sol index f412971..dd7b62e 100644 --- a/contracts/Registry.sol +++ b/contracts/Registry.sol @@ -85,6 +85,7 @@ contract Registry is RegistryErrors { /// @param _opiumAddress address New opium commission receiver address function changeOpiumAddress(address _opiumAddress) external { require(opiumAddress == msg.sender, ERROR_REGISTRY_ONLY_OPIUM_ADDRESS_ALLOWED); + require(_opiumAddress != address(0), "Can't set zero address"); opiumAddress = _opiumAddress; } From 29a1bed1925d9789d52c9ed5efb0764202a6188b Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Mon, 6 Jan 2020 14:27:56 +0100 Subject: [PATCH 06/23] Fixed issues with redundand code --- contracts/Lib/WhitelistedWithGovernance.sol | 3 ++- .../Lib/WhitelistedWithGovernanceAndChangableTimelock.sol | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/contracts/Lib/WhitelistedWithGovernance.sol b/contracts/Lib/WhitelistedWithGovernance.sol index 78774c5..4d8c055 100644 --- a/contracts/Lib/WhitelistedWithGovernance.sol +++ b/contracts/Lib/WhitelistedWithGovernance.sol @@ -19,7 +19,8 @@ contract WhitelistedWithGovernance is Whitelisted { address public governor; // Timestamp of last proposal - uint256 public proposalTime = 0; + uint256 public proposalTime; + // Proposed whitelist address[] public proposedWhitelist; diff --git a/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol b/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol index 6d6f648..dff0bec 100644 --- a/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol +++ b/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol @@ -10,9 +10,9 @@ contract WhitelistedWithGovernanceAndChangableTimelock is WhitelistedWithGoverna event Committed(uint256 timelock); // Timestamp of last timelock proposal - uint256 timelockProposalTime = 0; + uint256 timelockProposalTime; // Proposed timelock - uint256 proposedTimelock = 0; + uint256 proposedTimelock; /// @notice Calling this function governor could propose new timelock /// @param _timelock uint256 New timelock value From 64a47f15dd2cfe9b5b59e6255c3d15ad56e3df0e Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Mon, 6 Jan 2020 14:31:06 +0100 Subject: [PATCH 07/23] Visibility for registry --- contracts/Lib/usingRegistry.sol | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contracts/Lib/usingRegistry.sol b/contracts/Lib/usingRegistry.sol index 505d827..113869d 100644 --- a/contracts/Lib/usingRegistry.sol +++ b/contracts/Lib/usingRegistry.sol @@ -23,4 +23,10 @@ contract usingRegistry is usingRegistryErrors { registry = Registry(_registry); emit RegistrySet(_registry); } + + /// @notice Getter for registry variable + /// @return address Address of registry set in current contract + function getRegistry() external view returns (address) { + return address(registry); + } } From 7abc659bac3f3746be185c31aff86f2289c77c22 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Mon, 6 Jan 2020 14:34:38 +0100 Subject: [PATCH 08/23] Fixed missleading function name --- contracts/Matching/Match/MatchCreate.sol | 4 ++-- contracts/Matching/Match/MatchLogic.sol | 2 +- contracts/Matching/Match/MatchPool.sol | 2 +- contracts/Matching/Match/MatchSwap.sol | 4 ++-- contracts/Matching/SwaprateMatch/SwaprateMatch.sol | 4 ++-- contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/contracts/Matching/Match/MatchCreate.sol b/contracts/Matching/Match/MatchCreate.sol index e6a2d7a..7c293f5 100644 --- a/contracts/Matching/Match/MatchCreate.sol +++ b/contracts/Matching/Match/MatchCreate.sol @@ -47,11 +47,11 @@ contract MatchCreate is MatchLogic, LibDerivative { // orderHashes[1] - sellOrderHash bytes32[2] memory orderHashes; orderHashes[0] = hashOrder(_buyOrder); - validateCanceled(orderHashes[0]); + validateNotCanceled(orderHashes[0]); validateSignature(orderHashes[0], _buyOrder); orderHashes[1] = hashOrder(_sellOrder); - validateCanceled(orderHashes[1]); + validateNotCanceled(orderHashes[1]); validateSignature(orderHashes[1], _sellOrder); // Validates counterparty tokens and margin diff --git a/contracts/Matching/Match/MatchLogic.sol b/contracts/Matching/Match/MatchLogic.sol index 9d5d5a4..516b93f 100644 --- a/contracts/Matching/Match/MatchLogic.sol +++ b/contracts/Matching/Match/MatchLogic.sol @@ -72,7 +72,7 @@ contract MatchLogic is MatchingErrors, LibOrder, usingRegistry, ReentrancyGuard /// @notice This function checks whether order was canceled /// @param _hash bytes32 Hash of the order - function validateCanceled(bytes32 _hash) internal view { + function validateNotCanceled(bytes32 _hash) internal view { require(!canceled[_hash], ERROR_MATCH_ORDER_WAS_CANCELED); } diff --git a/contracts/Matching/Match/MatchPool.sol b/contracts/Matching/Match/MatchPool.sol index 3e91821..4dbc324 100644 --- a/contracts/Matching/Match/MatchPool.sol +++ b/contracts/Matching/Match/MatchPool.sol @@ -32,7 +32,7 @@ contract MatchPool is MatchLogic, LibDerivative { // Validate if was canceled bytes32 orderHash; orderHash = hashOrder(_buyOrder); - validateCanceled(orderHash); + validateNotCanceled(orderHash); validateSignature(orderHash, _buyOrder); uint256 margin = calculatePool(_buyOrder, _derivative); diff --git a/contracts/Matching/Match/MatchSwap.sol b/contracts/Matching/Match/MatchSwap.sol index 1301b92..cee903b 100644 --- a/contracts/Matching/Match/MatchSwap.sol +++ b/contracts/Matching/Match/MatchSwap.sol @@ -38,11 +38,11 @@ contract MatchSwap is MatchLogic { // orderHashes[1] - rightOrderHash bytes32[2] memory orderHashes; orderHashes[0] = hashOrder(_leftOrder); - validateCanceled(orderHashes[0]); + validateNotCanceled(orderHashes[0]); validateSignature(orderHashes[0], _leftOrder); orderHashes[1] = hashOrder(_rightOrder); - validateCanceled(orderHashes[1]); + validateNotCanceled(orderHashes[1]); validateSignature(orderHashes[1], _rightOrder); // Validate if values are correct diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatch.sol b/contracts/Matching/SwaprateMatch/SwaprateMatch.sol index adfe7e8..0a45c4f 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatch.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatch.sol @@ -47,11 +47,11 @@ contract SwaprateMatch is SwaprateMatchBase, LibDerivative { // orderHashes[1] - rightOrderHash bytes32[2] memory orderHashes; orderHashes[0] = hashOrder(_leftOrder); - validateCanceled(orderHashes[0]); + validateNotCanceled(orderHashes[0]); validateSignature(orderHashes[0], _leftOrder); orderHashes[1] = hashOrder(_rightOrder); - validateCanceled(orderHashes[1]); + validateNotCanceled(orderHashes[1]); validateSignature(orderHashes[1], _rightOrder); // Calculate derivative hash and get margin diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol index 167f733..4ee5221 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol @@ -64,7 +64,7 @@ contract SwaprateMatchBase is MatchingErrors, LibSwaprateOrder, usingRegistry, R /// @notice This function checks whether order was canceled /// @param _hash bytes32 Hash of the order - function validateCanceled(bytes32 _hash) internal view { + function validateNotCanceled(bytes32 _hash) internal view { require(!canceled[_hash], ERROR_MATCH_ORDER_WAS_CANCELED); } From e8b0aeafa259181c383709b98ac83d6d763e8921 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Mon, 6 Jan 2020 14:43:07 +0100 Subject: [PATCH 09/23] Fixed implicit visibility level --- contracts/Helpers/ExecutableByThirdParty.sol | 2 +- contracts/Lib/LibCommission.sol | 6 +++--- .../Lib/WhitelistedWithGovernanceAndChangableTimelock.sol | 4 ++-- contracts/Matching/SwaprateMatch/SwaprateMatch.sol | 2 +- contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/contracts/Helpers/ExecutableByThirdParty.sol b/contracts/Helpers/ExecutableByThirdParty.sol index 03de83c..b9c5092 100644 --- a/contracts/Helpers/ExecutableByThirdParty.sol +++ b/contracts/Helpers/ExecutableByThirdParty.sol @@ -3,7 +3,7 @@ pragma solidity 0.5.7; /// @title Opium.Helpers.ExecutableByThirdParty contract helps to syntheticId development and responsible for getting and setting thirdparty execution settings contract ExecutableByThirdParty { // Mapping holds whether position owner allows thirdparty execution - mapping (address => bool) thirdpartyExecutionAllowance; + mapping (address => bool) internal thirdpartyExecutionAllowance; /// @notice Getter for thirdparty execution allowance /// @param derivativeOwner Address of position holder that's going to be executed diff --git a/contracts/Lib/LibCommission.sol b/contracts/Lib/LibCommission.sol index c20f1a3..af50a9f 100644 --- a/contracts/Lib/LibCommission.sol +++ b/contracts/Lib/LibCommission.sol @@ -3,11 +3,11 @@ pragma solidity 0.5.7; /// @title Opium.Lib.LibCommission contract defines constants for Opium commissions contract LibCommission { // Represents 100% base for commissions calculation - uint256 constant COMMISSION_BASE = 10000; + uint256 constant public COMMISSION_BASE = 10000; // Represents 100% base for Opium commission - uint256 constant OPIUM_COMMISSION_BASE = 10; + uint256 constant public OPIUM_COMMISSION_BASE = 10; // Represents which part of `syntheticId` author commissions goes to opium - uint256 constant OPIUM_COMMISSION_PART = 1; + uint256 constant public OPIUM_COMMISSION_PART = 1; } diff --git a/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol b/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol index dff0bec..951abcc 100644 --- a/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol +++ b/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol @@ -10,9 +10,9 @@ contract WhitelistedWithGovernanceAndChangableTimelock is WhitelistedWithGoverna event Committed(uint256 timelock); // Timestamp of last timelock proposal - uint256 timelockProposalTime; + uint256 public timelockProposalTime; // Proposed timelock - uint256 proposedTimelock; + uint256 public proposedTimelock; /// @notice Calling this function governor could propose new timelock /// @param _timelock uint256 New timelock value diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatch.sol b/contracts/Matching/SwaprateMatch/SwaprateMatch.sol index 0a45c4f..b42ccfb 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatch.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatch.sol @@ -14,7 +14,7 @@ contract SwaprateMatch is SwaprateMatchBase, LibDerivative { // Orders filled quantity // This mapping holds orders filled quantity // filled[orderHash] => filled - mapping (bytes32 => uint256) filled; + mapping (bytes32 => uint256) public filled; /// @notice Calls constructors of super-contracts /// @param _registry address Address of Opium.registry diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol index 4ee5221..aac9476 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol @@ -28,12 +28,12 @@ contract SwaprateMatchBase is MatchingErrors, LibSwaprateOrder, usingRegistry, R // Canceled orders // This mapping holds hashes of canceled orders // canceled[orderHash] => canceled - mapping (bytes32 => bool) canceled; + mapping (bytes32 => bool) public canceled; // Verified orders // This mapping holds hashes of verified orders to verify only once // verified[orderHash] => verified - mapping (bytes32 => bool) verified; + mapping (bytes32 => bool) public verified; // Vaults for fees // This mapping holds balances of relayers and affiliates fees to withdraw @@ -41,7 +41,7 @@ contract SwaprateMatchBase is MatchingErrors, LibSwaprateOrder, usingRegistry, R mapping (address => mapping (address => uint256)) public balances; // Keeps whether fee was already taken - mapping (bytes32 => bool) feeTaken; + mapping (bytes32 => bool) public feeTaken; /// @notice Calling this function maker of the order could cancel it on-chain /// @param _order SwaprateOrder From 022f92dc290e53f0ef28f83ff2050014e2abd048 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Mon, 6 Jan 2020 14:58:05 +0100 Subject: [PATCH 10/23] Fixed usage of transfer() withour SafeERC20 --- contracts/Core.sol | 2 +- contracts/Matching/Match/MatchLogic.sol | 4 +++- contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/contracts/Core.sol b/contracts/Core.sol index b1dd237..50fd33d 100644 --- a/contracts/Core.sol +++ b/contracts/Core.sol @@ -61,7 +61,7 @@ contract Core is LibDerivative, LibCommission, usingRegistry, CoreErrors, Reentr function withdrawFee(address _tokenAddress) public nonReentrant { uint256 balance = feesVaults[msg.sender][_tokenAddress]; feesVaults[msg.sender][_tokenAddress] = 0; - IERC20(_tokenAddress).transfer(msg.sender, balance); + IERC20(_tokenAddress).safeTransfer(msg.sender, balance); } /// @notice Creates derivative contracts (positions) diff --git a/contracts/Matching/Match/MatchLogic.sol b/contracts/Matching/Match/MatchLogic.sol index 516b93f..04470ad 100644 --- a/contracts/Matching/Match/MatchLogic.sol +++ b/contracts/Matching/Match/MatchLogic.sol @@ -2,6 +2,7 @@ pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; +import "openzeppelin-solidity/contracts/token/ERC20/SafeERC20.sol"; import "openzeppelin-solidity/contracts/utils/ReentrancyGuard.sol"; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; @@ -21,6 +22,7 @@ import "../../SyntheticAggregator.sol"; contract MatchLogic is MatchingErrors, LibOrder, usingRegistry, ReentrancyGuard { using SafeMath for uint256; using LibPosition for bytes32; + using SafeERC20 for IERC20; // Emmitted when order was canceled event Canceled(bytes32 orderHash); @@ -67,7 +69,7 @@ contract MatchLogic is MatchingErrors, LibOrder, usingRegistry, ReentrancyGuard function withdraw(IERC20 _token) public nonReentrant { uint256 balance = balances[msg.sender][address(_token)]; balances[msg.sender][address(_token)] = 0; - _token.transfer(msg.sender, balance); + _token.safeTransfer(msg.sender, balance); } /// @notice This function checks whether order was canceled diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol index aac9476..9c36b35 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol @@ -2,6 +2,7 @@ pragma solidity 0.5.7; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; +import "openzeppelin-solidity/contracts/token/ERC20/SafeERC20.sol"; import "openzeppelin-solidity/contracts/utils/ReentrancyGuard.sol"; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; @@ -21,6 +22,7 @@ import "../../SyntheticAggregator.sol"; contract SwaprateMatchBase is MatchingErrors, LibSwaprateOrder, usingRegistry, ReentrancyGuard { using SafeMath for uint256; using LibPosition for bytes32; + using SafeERC20 for IERC20; // Emmitted when order was canceled event Canceled(bytes32 orderHash); @@ -59,7 +61,7 @@ contract SwaprateMatchBase is MatchingErrors, LibSwaprateOrder, usingRegistry, R function withdraw(IERC20 _token) public nonReentrant { uint256 balance = balances[msg.sender][address(_token)]; balances[msg.sender][address(_token)] = 0; - _token.transfer(msg.sender, balance); + _token.safeTransfer(msg.sender, balance); } /// @notice This function checks whether order was canceled From ccece6055622035a8b97e815483c391edcaf1120 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Mon, 6 Jan 2020 15:05:35 +0100 Subject: [PATCH 11/23] Fixed synthetic author address constant name and modifier --- contracts/Helpers/HasCommission.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/Helpers/HasCommission.sol b/contracts/Helpers/HasCommission.sol index afbb2a1..f276e32 100644 --- a/contracts/Helpers/HasCommission.sol +++ b/contracts/Helpers/HasCommission.sol @@ -5,7 +5,7 @@ contract HasCommission { // Address of syntheticId author address public author; // Commission is in Opium.Lib.LibCommission.COMMISSION_BASE base - uint256 public commission = 25; // 0.25% of profit + uint256 constant public AUTHOR_COMMISSION = 25; // 0.25% of profit /// @notice Sets `msg.sender` as syntheticId author constructor() public { @@ -21,6 +21,6 @@ contract HasCommission { /// @notice Getter for syntheticId author commission /// @return uint26 syntheticId author commission function getAuthorCommission() public view returns (uint256) { - return commission; + return AUTHOR_COMMISSION; } } From 33e2393fa827dde096be55b165e0764393af42c2 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Mon, 6 Jan 2020 15:20:33 +0100 Subject: [PATCH 12/23] Fixed naming convention --- contracts/Core.sol | 8 ++--- contracts/Errors/UsingRegistryErrors.sol | 5 +++ contracts/Errors/usingRegistryErrors.sol | 2 +- contracts/Lib/UsingRegistry.sol | 32 +++++++++++++++++++ contracts/Lib/WhitelistedWithGovernance.sol | 6 ++-- ...stedWithGovernanceAndChangableTimelock.sol | 18 +++++------ contracts/Lib/usingRegistry.sol | 6 ++-- contracts/Matching/Match/Match.sol | 2 +- contracts/Matching/Match/MatchLogic.sol | 4 +-- contracts/Matching/Match/MatchPool.sol | 2 +- .../Matching/SwaprateMatch/SwaprateMatch.sol | 2 +- .../SwaprateMatch/SwaprateMatchBase.sol | 4 +-- contracts/TokenMinter.sol | 6 ++-- contracts/test/OracleIdMock.sol | 6 ++-- 14 files changed, 70 insertions(+), 33 deletions(-) create mode 100644 contracts/Errors/UsingRegistryErrors.sol create mode 100644 contracts/Lib/UsingRegistry.sol diff --git a/contracts/Core.sol b/contracts/Core.sol index 50fd33d..b82ef77 100644 --- a/contracts/Core.sol +++ b/contracts/Core.sol @@ -12,7 +12,7 @@ import "./Interface/IDerivativeLogic.sol"; import "./Errors/CoreErrors.sol"; -import "./Lib/usingRegistry.sol"; +import "./Lib/UsingRegistry.sol"; import "./Lib/LibDerivative.sol"; import "./Lib/LibCommission.sol"; @@ -23,7 +23,7 @@ import "./SyntheticAggregator.sol"; import "./TokenSpender.sol"; /// @title Opium.Core contract creates positions, holds and distributes margin at the maturity -contract Core is LibDerivative, LibCommission, usingRegistry, CoreErrors, ReentrancyGuard { +contract Core is LibDerivative, LibCommission, UsingRegistry, CoreErrors, ReentrancyGuard { using SafeMath for uint256; using LibPosition for bytes32; using SafeERC20 for IERC20; @@ -51,8 +51,8 @@ contract Core is LibDerivative, LibCommission, usingRegistry, CoreErrors, Reentr // Hashes of cancelled tickers mapping (bytes32 => bool) public cancelled; - /// @notice Calls Core.Lib.usingRegistry constructor - constructor(address _registry) public usingRegistry(_registry) {} + /// @notice Calls Core.Lib.UsingRegistry constructor + constructor(address _registry) public UsingRegistry(_registry) {} // PUBLIC FUNCTIONS diff --git a/contracts/Errors/UsingRegistryErrors.sol b/contracts/Errors/UsingRegistryErrors.sol new file mode 100644 index 0000000..982b6ef --- /dev/null +++ b/contracts/Errors/UsingRegistryErrors.sol @@ -0,0 +1,5 @@ +pragma solidity 0.5.7; + +contract UsingRegistryErrors { + string constant internal ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED = "USING_REGISTRY:ONLY_CORE_ALLOWED"; +} diff --git a/contracts/Errors/usingRegistryErrors.sol b/contracts/Errors/usingRegistryErrors.sol index a25594c..982b6ef 100644 --- a/contracts/Errors/usingRegistryErrors.sol +++ b/contracts/Errors/usingRegistryErrors.sol @@ -1,5 +1,5 @@ pragma solidity 0.5.7; -contract usingRegistryErrors { +contract UsingRegistryErrors { string constant internal ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED = "USING_REGISTRY:ONLY_CORE_ALLOWED"; } diff --git a/contracts/Lib/UsingRegistry.sol b/contracts/Lib/UsingRegistry.sol new file mode 100644 index 0000000..dd6e304 --- /dev/null +++ b/contracts/Lib/UsingRegistry.sol @@ -0,0 +1,32 @@ +pragma solidity 0.5.7; + +import "../Registry.sol"; + +import "../Errors/UsingRegistryErrors.sol"; + +/// @title Opium.Lib.UsingRegistry contract should be inherited by contracts, that are going to use Opium.Registry +contract UsingRegistry is UsingRegistryErrors { + // Emitted when registry instance is set + event RegistrySet(address registry); + + // Instance of Opium.Registry contract + Registry internal registry; + + /// @notice This modifier restricts access to functions, which could be called only by Opium.Core + modifier onlyCore() { + require(msg.sender == registry.getCore(), ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED); + _; + } + + /// @notice Defines registry instance and emits appropriate event + constructor(address _registry) public { + registry = Registry(_registry); + emit RegistrySet(_registry); + } + + /// @notice Getter for registry variable + /// @return address Address of registry set in current contract + function getRegistry() external view returns (address) { + return address(registry); + } +} diff --git a/contracts/Lib/WhitelistedWithGovernance.sol b/contracts/Lib/WhitelistedWithGovernance.sol index 4d8c055..759c969 100644 --- a/contracts/Lib/WhitelistedWithGovernance.sol +++ b/contracts/Lib/WhitelistedWithGovernance.sol @@ -13,7 +13,7 @@ contract WhitelistedWithGovernance is Whitelisted { event Committed(address[] whitelist); // Proposal life timelock interval - uint256 public TIME_LOCK_INTERVAL; + uint256 public timeLockInterval; // Governor address address public governor; @@ -34,7 +34,7 @@ contract WhitelistedWithGovernance is Whitelisted { /// @param _timeLockInterval uint256 Initial value for timelock interval /// @param _governor address Initial value for governor constructor(uint256 _timeLockInterval, address _governor) public { - TIME_LOCK_INTERVAL = _timeLockInterval; + timeLockInterval = _timeLockInterval; governor = _governor; emit GovernorSet(governor); } @@ -64,7 +64,7 @@ contract WhitelistedWithGovernance is Whitelisted { require(proposalTime != 0, "Didn't proposed yet"); // Check if timelock interval was passed - require((proposalTime + TIME_LOCK_INTERVAL) < now, "Can't commit yet"); + require((proposalTime + timeLockInterval) < now, "Can't commit yet"); // Set new whitelist and emit event whitelist = proposedWhitelist; diff --git a/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol b/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol index 951abcc..ce48af0 100644 --- a/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol +++ b/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol @@ -10,30 +10,30 @@ contract WhitelistedWithGovernanceAndChangableTimelock is WhitelistedWithGoverna event Committed(uint256 timelock); // Timestamp of last timelock proposal - uint256 public timelockProposalTime; + uint256 public timeLockProposalTime; // Proposed timelock - uint256 public proposedTimelock; + uint256 public proposedTimeLock; /// @notice Calling this function governor could propose new timelock /// @param _timelock uint256 New timelock value function proposeTimelock(uint256 _timelock) public onlyGovernor { - timelockProposalTime = now; - proposedTimelock = _timelock; + timeLockProposalTime = now; + proposedTimeLock = _timelock; emit Proposed(_timelock); } /// @notice Calling this function governor could commit previously proposed new timelock if timelock interval of proposal was passed function commitTimelock() public onlyGovernor { // Check if proposal was made - require(timelockProposalTime != 0, "Didn't proposed yet"); + require(timeLockProposalTime != 0, "Didn't proposed yet"); // Check if timelock interval was passed - require((timelockProposalTime + TIME_LOCK_INTERVAL) < now, "Can't commit yet"); + require((timeLockProposalTime + timeLockInterval) < now, "Can't commit yet"); // Set new timelock and emit event - TIME_LOCK_INTERVAL = proposedTimelock; - emit Committed(proposedTimelock); + timeLockInterval = proposedTimeLock; + emit Committed(proposedTimeLock); // Reset timelock time lock - timelockProposalTime = 0; + timeLockProposalTime = 0; } } diff --git a/contracts/Lib/usingRegistry.sol b/contracts/Lib/usingRegistry.sol index 113869d..dd6e304 100644 --- a/contracts/Lib/usingRegistry.sol +++ b/contracts/Lib/usingRegistry.sol @@ -2,10 +2,10 @@ pragma solidity 0.5.7; import "../Registry.sol"; -import "../Errors/usingRegistryErrors.sol"; +import "../Errors/UsingRegistryErrors.sol"; -/// @title Opium.Lib.usingRegistry contract should be inherited by contracts, that are going to use Opium.Registry -contract usingRegistry is usingRegistryErrors { +/// @title Opium.Lib.UsingRegistry contract should be inherited by contracts, that are going to use Opium.Registry +contract UsingRegistry is UsingRegistryErrors { // Emitted when registry instance is set event RegistrySet(address registry); diff --git a/contracts/Matching/Match/Match.sol b/contracts/Matching/Match/Match.sol index 74b9751..260429b 100644 --- a/contracts/Matching/Match/Match.sol +++ b/contracts/Matching/Match/Match.sol @@ -9,5 +9,5 @@ contract Match is MatchCreate, MatchSwap { /// @notice Calls constructors of super-contracts /// @param _registry address Address of Opium.registry - constructor (address _registry) public usingRegistry(_registry) {} + constructor (address _registry) public UsingRegistry(_registry) {} } diff --git a/contracts/Matching/Match/MatchLogic.sol b/contracts/Matching/Match/MatchLogic.sol index 04470ad..e39827c 100644 --- a/contracts/Matching/Match/MatchLogic.sol +++ b/contracts/Matching/Match/MatchLogic.sol @@ -8,7 +8,7 @@ import "openzeppelin-solidity/contracts/math/SafeMath.sol"; import "erc721o/contracts/Libs/LibPosition.sol"; -import "../../Lib/usingRegistry.sol"; +import "../../Lib/UsingRegistry.sol"; import "../../Errors/MatchingErrors.sol"; @@ -19,7 +19,7 @@ import "../../Core.sol"; import "../../SyntheticAggregator.sol"; /// @title Opium.Matching.MatchLogic contract implements logic for order validation and cancelation -contract MatchLogic is MatchingErrors, LibOrder, usingRegistry, ReentrancyGuard { +contract MatchLogic is MatchingErrors, LibOrder, UsingRegistry, ReentrancyGuard { using SafeMath for uint256; using LibPosition for bytes32; using SafeERC20 for IERC20; diff --git a/contracts/Matching/Match/MatchPool.sol b/contracts/Matching/Match/MatchPool.sol index 4dbc324..7757b0f 100644 --- a/contracts/Matching/Match/MatchPool.sol +++ b/contracts/Matching/Match/MatchPool.sol @@ -9,7 +9,7 @@ import "../../SyntheticAggregator.sol"; import "./MatchLogic.sol"; contract MatchPool is MatchLogic, LibDerivative { - constructor (address _registry) public usingRegistry(_registry) {} + constructor (address _registry) public UsingRegistry(_registry) {} function create(Order memory _buyOrder, Derivative memory _derivative) public nonReentrant { // PROBABLY TODO: Implement subtraction "Relayer" order and subtract before all diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatch.sol b/contracts/Matching/SwaprateMatch/SwaprateMatch.sol index b42ccfb..cbc4f86 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatch.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatch.sol @@ -18,7 +18,7 @@ contract SwaprateMatch is SwaprateMatchBase, LibDerivative { /// @notice Calls constructors of super-contracts /// @param _registry address Address of Opium.registry - constructor (address _registry) public usingRegistry(_registry) {} + constructor (address _registry) public UsingRegistry(_registry) {} /// @notice This function receives left and right orders, derivative related to it /// @param _leftOrder Order diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol index 9c36b35..7c88529 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol @@ -8,7 +8,7 @@ import "openzeppelin-solidity/contracts/math/SafeMath.sol"; import "erc721o/contracts/Libs/LibPosition.sol"; -import "../../Lib/usingRegistry.sol"; +import "../../Lib/UsingRegistry.sol"; import "../../Errors/MatchingErrors.sol"; @@ -19,7 +19,7 @@ import "../../Core.sol"; import "../../SyntheticAggregator.sol"; /// @title Opium.Matching.SwaprateMatchBase contract implements logic for order validation and cancelation -contract SwaprateMatchBase is MatchingErrors, LibSwaprateOrder, usingRegistry, ReentrancyGuard { +contract SwaprateMatchBase is MatchingErrors, LibSwaprateOrder, UsingRegistry, ReentrancyGuard { using SafeMath for uint256; using LibPosition for bytes32; using SafeERC20 for IERC20; diff --git a/contracts/TokenMinter.sol b/contracts/TokenMinter.sol index e0e2e37..fde97b3 100644 --- a/contracts/TokenMinter.sol +++ b/contracts/TokenMinter.sol @@ -2,14 +2,14 @@ pragma solidity 0.5.7; import "erc721o/contracts/ERC721OBackwardCompatible.sol"; -import "./Lib/usingRegistry.sol"; +import "./Lib/UsingRegistry.sol"; /// @title Opium.TokenMinter contract implements ERC721O token standard for minting, burning and transferring position tokens -contract TokenMinter is ERC721OBackwardCompatible, usingRegistry { +contract TokenMinter is ERC721OBackwardCompatible, UsingRegistry { /// @notice Calls constructors of super-contracts /// @param _baseTokenURI string URI for token explorers /// @param _registry address Address of Opium.registry - constructor(string memory _baseTokenURI, address _registry) public ERC721OBackwardCompatible(_baseTokenURI) usingRegistry(_registry) {} + constructor(string memory _baseTokenURI, address _registry) public ERC721OBackwardCompatible(_baseTokenURI) UsingRegistry(_registry) {} /// @notice Mints LONG and SHORT position tokens /// @param _buyer address Address of LONG position receiver diff --git a/contracts/test/OracleIdMock.sol b/contracts/test/OracleIdMock.sol index 829bb17..127daea 100644 --- a/contracts/test/OracleIdMock.sol +++ b/contracts/test/OracleIdMock.sol @@ -1,13 +1,13 @@ pragma solidity 0.5.7; import "../Interface/IOracleId.sol"; -import "../Lib/usingRegistry.sol"; +import "../Lib/UsingRegistry.sol"; import "../OracleAggregator.sol"; -contract OracleIdMock is IOracleId, usingRegistry { +contract OracleIdMock is IOracleId, UsingRegistry { uint256 fetchPrice; - constructor(uint256 _fetchPrice, address _registry) public usingRegistry(_registry) { + constructor(uint256 _fetchPrice, address _registry) public UsingRegistry(_registry) { fetchPrice = _fetchPrice; } From 24d7a0ea3007142e349a1ade3f38459b59916217 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Tue, 7 Jan 2020 15:05:14 +0100 Subject: [PATCH 13/23] Refactoring of testing deployment script --- migrations/2_deploy_contract_set_for_tests.js | 267 +++++++++--------- 1 file changed, 139 insertions(+), 128 deletions(-) diff --git a/migrations/2_deploy_contract_set_for_tests.js b/migrations/2_deploy_contract_set_for_tests.js index ae67705..96f4beb 100644 --- a/migrations/2_deploy_contract_set_for_tests.js +++ b/migrations/2_deploy_contract_set_for_tests.js @@ -16,6 +16,7 @@ const TokenMinter = artifacts.require('TokenMinter') const TokenSpender = artifacts.require('TokenSpender') const Match = artifacts.require('Match') +const MatchPool = artifacts.require('MatchPool') const SwaprateMatch = artifacts.require('SwaprateMatch') // Test helpers @@ -26,136 +27,146 @@ const WETH = artifacts.require('WETH') const baseTokenURI = 'https://explorer.opium.network/erc721o/' -module.exports = async function(deployer, network, accounts) { - const owner = accounts[0] +// Deployment functions +const deployAndLinkLibPosition = async ({ deployer, opiumDeployerAddress }) => { + const libPositionInstance = await deployer.deploy(LibPosition, { from: opiumDeployerAddress }) + + console.log('LibPosition was deployed at', libPositionInstance.address) + + await deployer.link(LibPosition, [ + Core, + TokenMinter, + + Match, + MatchPool, + SwaprateMatch + ]) + + console.log('LibPosition was linked to Core, TokenMinter, Match, MatchPool and SwaprateMatch') + + return libPositionInstance +} - let libPosition +const deployRegistry = async ({ deployer, opiumDeployerAddress }) => { + const registryInstance = await deployer.deploy(Registry, { from: opiumDeployerAddress }) + console.log('Registry was deployed at', registryInstance.address) - let registry, core, tokenMinter, oracleAggregator, syntheticAggregator, tokenSpender + await registryInstance.setOpiumAddress(opiumDeployerAddress, { from: opiumDeployerAddress }) + console.log('Opium deployer address was set in registry') - let match, swaprateMatch + return registryInstance +} + +const deployCore = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const coreInstance = await deployer.deploy(Core, registryInstance.address, { from: opiumDeployerAddress }) + console.log('- Core was deployed at', coreInstance.address) - let optionCallSyntheticIdMock, dummySyntheticIdMock - let dai - let weth - - deployer.deploy(LibPosition, { from: owner }) - .then(instance => { - libPosition = instance - console.log('LibPosition was deployed at', libPosition.address) - return deployer.link(LibPosition, [ - Core, - TokenMinter, - - Match, - SwaprateMatch - ]) - }) - .then(() => { - return deployer - .deploy(Registry, { from: owner }) - .then(instance => { - registry = instance - console.log('Registry was deployed at', registry.address) - }) - }) - .then(() => { - return deployer - .deploy(Core, registry.address, { from: owner }) - .then(instance => { - core = instance - console.log('- Core was deployed at', core.address) - return registry.setCore(core.address, { from: owner }) - }) - }) - .then(() => { - return deployer - .deploy(Match, registry.address, { from: owner }) - .then(instance => { - match = instance - console.log('- Match was deployed at', match.address) - }) - }) - .then(() => { - return deployer - .deploy(SwaprateMatch, registry.address, { from: owner }) - .then(instance => { - swaprateMatch = instance - console.log('- SwaprateMatch was deployed at', swaprateMatch.address) - }) - }) - .then(() => { - return deployer - .deploy(TokenSpender, owner, { from: owner }) - .then(instance => { - tokenSpender = instance - console.log('- TokenSpender was deployed at', tokenSpender.address) - return tokenSpender.proposeWhitelist([ core.address, match.address, swaprateMatch.address ], { from: owner }) - }) - }) - .then(() => { - return registry.setTokenSpender(tokenSpender.address, { from: owner }) - }) - .then(() => { - return deployer - .deploy(TokenMinter, baseTokenURI, registry.address, { from: owner }) - .then(instance => { - tokenMinter = instance - console.log('- TokenMinter was deployed at', tokenMinter.address) - return registry.setMinter(tokenMinter.address, { from: owner }) - }) - }) - .then(() => { - return deployer - .deploy(OracleAggregator, { from: owner }) - .then(instance => { - oracleAggregator = instance - console.log('- OracleAggregator was deployed at', oracleAggregator.address) - return registry.setOracleAggregator(oracleAggregator.address, { from: owner }) - }) - }) - .then(() => { - return deployer - .deploy(SyntheticAggregator, { from: owner }) - .then(instance => { - syntheticAggregator = instance - console.log('- SyntheticAggregator was deployed at', syntheticAggregator.address) - return registry.setSyntheticAggregator(syntheticAggregator.address, { from: owner }) - }) - }) - .then(() => { - return deployer - .deploy(OptionCallSyntheticIdMock, { from: owner }) - .then(instance => { - optionCallSyntheticIdMock = instance - console.log('OptionCallSyntheticIdMock was deployed at', optionCallSyntheticIdMock.address) - }) - }) - .then(() => { - return deployer - .deploy(DummySyntheticIdMock, { from: owner }) - .then(instance => { - dummySyntheticIdMock = instance - console.log('DummySyntheticIdMock was deployed at', dummySyntheticIdMock.address) - }) - }) - .then(() => { - return deployer - .deploy(TestToken, 'Opium DAI Token', 'DAI', 18, { from: owner }) - .then(instance => { - dai = instance - console.log('DAI was deployed at', dai.address) - }) - }) - .then(() => { - return deployer - .deploy(WETH, { from: owner }) - .then(instance => { - weth = instance - console.log('WETH was deployed at', weth.address) - }) - }) - .then(() => { - return registry.setOpiumAddress(owner, { from: owner }) - }) + await registryInstance.setCore(coreInstance.address, { from: opiumDeployerAddress }) + console.log('Core address was set in registry') + + return coreInstance +} + +const deployMatch = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const matchInstance = await deployer.deploy(Match, registryInstance.address, { from: opiumDeployerAddress }) + console.log('- Match was deployed at', matchInstance.address) + + return matchInstance +} + +const deployMatchPool = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const matchPoolInstance = await deployer.deploy(MatchPool, registryInstance.address, { from: opiumDeployerAddress }) + console.log('- MatchPool was deployed at', matchPoolInstance.address) + + return matchPoolInstance +} + +const deploySwaprateMatch = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const swaprateMatchInstance = await deployer.deploy(SwaprateMatch, registryInstance.address, { from: opiumDeployerAddress }) + console.log('- SwaprateMatch was deployed at', swaprateMatchInstance.address) + + return swaprateMatchInstance +} + +const deployTokenSpender = async ({ deployer, opiumDeployerAddress, governor, whitelist, registryInstance }) => { + const tokenSpenderInstance = await deployer.deploy(TokenSpender, governor, { from: opiumDeployerAddress }) + console.log('- TokenSpender was deployed at', tokenSpenderInstance.address) + + await tokenSpenderInstance.proposeWhitelist(whitelist, { from: governor }) + console.log('TokenSpender whitelist was set') + + await registryInstance.setTokenSpender(tokenSpenderInstance.address, { from: opiumDeployerAddress }) + console.log('TokenSpender address was set in registry') + + return tokenSpenderInstance +} + +const deployTokenMinter = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const tokenMinterInstance = await deployer.deploy(TokenMinter, baseTokenURI, registryInstance.address, { from: opiumDeployerAddress }) + console.log('- TokenMinter was deployed at', tokenMinterInstance.address) + + await registryInstance.setMinter(tokenMinterInstance.address, { from: opiumDeployerAddress }) + console.log('TokenMinter address was set in registry') + + return tokenMinterInstance +} + +const deployOracleAggregator = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const oracleAggregatorInstance = await deployer.deploy(OracleAggregator, { from: opiumDeployerAddress }) + console.log('- OracleAggregator was deployed at', oracleAggregatorInstance.address) + + await registryInstance.setOracleAggregator(oracleAggregatorInstance.address, { from: opiumDeployerAddress }) + console.log('OracleAggregator address was set in registry') + + return oracleAggregatorInstance +} + +const deploySyntheticAggregator = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const syntheticAggregatorInstance = await deployer.deploy(SyntheticAggregator, { from: opiumDeployerAddress }) + console.log('- SyntheticAggregator was deployed at', syntheticAggregatorInstance.address) + + await registryInstance.setSyntheticAggregator(syntheticAggregatorInstance.address, { from: opiumDeployerAddress }) + console.log('SyntheticAggregator address was set in registry') + + return syntheticAggregatorInstance +} + +const deployMocks = async ({ deployer, opiumDeployerAddress }) => { + const optionCallSyntheticIdMockInstance = await deployer.deploy(OptionCallSyntheticIdMock, { from: opiumDeployerAddress }) + console.log('**** OptionCallSyntheticIdMock was deployed at', optionCallSyntheticIdMockInstance.address) + + const dummySyntheticIdMockInstance = await deployer.deploy(DummySyntheticIdMock, { from: opiumDeployerAddress }) + console.log('**** DummySyntheticIdMock was deployed at', dummySyntheticIdMockInstance.address) + + const daiInstance = await deployer.deploy(TestToken, 'Opium DAI Token', 'DAI', 18, { from: opiumDeployerAddress }) + console.log('**** DAI was deployed at', daiInstance.address) + + const wethInstance = await deployer.deploy(WETH, { from: opiumDeployerAddress }) + console.log('**** WETH was deployed at', wethInstance.address) +} + +module.exports = async function(deployer, network, accounts) { + const opiumDeployerAddress = accounts[0] + + deployer.then(async () => { + await deployAndLinkLibPosition({ deployer, opiumDeployerAddress }) + + const registryInstance = await deployRegistry({ deployer, opiumDeployerAddress }) + + const coreInstance = await deployCore({ deployer, opiumDeployerAddress, registryInstance }) + const matchInstance = await deployMatch({ deployer, opiumDeployerAddress, registryInstance }) + const matchPoolInstance = await deployMatchPool({ deployer, opiumDeployerAddress, registryInstance }) + const swaprateMatchInstance = await deploySwaprateMatch({ deployer, opiumDeployerAddress, registryInstance }) + + const governor = opiumDeployerAddress + const whitelist = [ coreInstance.address, matchInstance.address, matchPoolInstance.address, swaprateMatchInstance.address ] + await deployTokenSpender({ deployer, opiumDeployerAddress, governor, whitelist, registryInstance }) + + await deployTokenMinter({ deployer, opiumDeployerAddress, registryInstance }) + await deployOracleAggregator({ deployer, opiumDeployerAddress, registryInstance }) + await deploySyntheticAggregator({ deployer, opiumDeployerAddress, registryInstance }) + + // Contracts for testing purpose + await deployMocks({ deployer, opiumDeployerAddress }) + }) } From 1c3552ebb976b879f6061a493b2d70f47aad9bb8 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Tue, 7 Jan 2020 15:55:07 +0100 Subject: [PATCH 14/23] Merged Registry setters into one init function --- contracts/Errors/RegistryErrors.sol | 2 + contracts/Registry.sol | 99 ++++++++++--------- migrations/2_deploy_contract_set_for_tests.js | 47 +++++---- test/OracleAggregator.js | 12 ++- test/Registry.js | 71 +++++++++---- test/TokenMinter.js | 15 ++- 6 files changed, 148 insertions(+), 98 deletions(-) diff --git a/contracts/Errors/RegistryErrors.sol b/contracts/Errors/RegistryErrors.sol index af2408c..6090c00 100644 --- a/contracts/Errors/RegistryErrors.sol +++ b/contracts/Errors/RegistryErrors.sol @@ -3,6 +3,8 @@ pragma solidity 0.5.7; contract RegistryErrors { string constant internal ERROR_REGISTRY_ONLY_INITIALIZER = "REGISTRY:ONLY_INITIALIZER"; string constant internal ERROR_REGISTRY_ONLY_OPIUM_ADDRESS_ALLOWED = "REGISTRY:ONLY_OPIUM_ADDRESS_ALLOWED"; + + string constant internal ERROR_REGISTRY_CANT_BE_ZERO_ADDRESS = "REGISTRY:CANT_BE_ZERO_ADDRESS"; string constant internal ERROR_REGISTRY_ALREADY_SET = "REGISTRY:ALREADY_SET"; } diff --git a/contracts/Registry.sol b/contracts/Registry.sol index dd7b62e..b0bf0fb 100644 --- a/contracts/Registry.sol +++ b/contracts/Registry.sol @@ -17,12 +17,12 @@ contract Registry is RegistryErrors { // Address of Opium.SyntheticAggregator contract address private syntheticAggregator; - // Address of Opium commission receiver - address private opiumAddress; - // Address of Opium.TokenSpender contract address private tokenSpender; + // Address of Opium commission receiver + address private opiumAddress; + // Address of Opium contract set deployer address public initializer; @@ -39,70 +39,71 @@ contract Registry is RegistryErrors { // SETTERS - /// @notice Sets Opium.TokenMinter address and allows to do it only once + /// @notice Sets Opium.TokenMinter, Opium.Core, Opium.OracleAggregator, Opium.SyntheticAggregator, Opium.TokenSpender, Opium commission receiver addresses and allows to do it only once /// @param _minter address Address of Opium.TokenMinter - function setMinter(address _minter) external onlyInitializer { - require(minter == address(0), ERROR_REGISTRY_ALREADY_SET); - minter = _minter; - } - - /// @notice Sets Opium.Core address and allows to do it only once /// @param _core address Address of Opium.Core - function setCore(address _core) external onlyInitializer { - require(core == address(0), ERROR_REGISTRY_ALREADY_SET); - core = _core; - } - - /// @notice Sets Opium.OracleAggregator address and allows to do it only once /// @param _oracleAggregator address Address of Opium.OracleAggregator - function setOracleAggregator(address _oracleAggregator) external onlyInitializer { - require(oracleAggregator == address(0), ERROR_REGISTRY_ALREADY_SET); - oracleAggregator = _oracleAggregator; - } - - /// @notice Sets Opium.SyntheticAggregator address and allows to do it only once /// @param _syntheticAggregator address Address of Opium.SyntheticAggregator - function setSyntheticAggregator(address _syntheticAggregator) external onlyInitializer { - require(syntheticAggregator == address(0), ERROR_REGISTRY_ALREADY_SET); - syntheticAggregator = _syntheticAggregator; - } - - /// @notice Sets Opium commission receiver and allows to do it only once + /// @param _tokenSpender address Address of Opium.TokenSpender /// @param _opiumAddress address Address of Opium commission receiver - function setOpiumAddress(address _opiumAddress) external onlyInitializer { - require(opiumAddress == address(0), ERROR_REGISTRY_ALREADY_SET); - opiumAddress = _opiumAddress; - } + function init( + address _minter, + address _core, + address _oracleAggregator, + address _syntheticAggregator, + address _tokenSpender, + address _opiumAddress + ) external onlyInitializer { + require( + minter == address(0) && + core == address(0) && + oracleAggregator == address(0) && + syntheticAggregator == address(0) && + tokenSpender == address(0) && + opiumAddress == address(0), + ERROR_REGISTRY_ALREADY_SET + ); + + require( + _minter != address(0) && + _core != address(0) && + _oracleAggregator != address(0) && + _syntheticAggregator != address(0) && + _tokenSpender != address(0) && + _opiumAddress != address(0), + ERROR_REGISTRY_CANT_BE_ZERO_ADDRESS + ); - /// @notice Sets Opium.TokenSpender address and allows to do it only once - /// @param _tokenSpender address Address of Opium.TokenSpender - function setTokenSpender(address _tokenSpender) external onlyInitializer { - require(tokenSpender == address(0), ERROR_REGISTRY_ALREADY_SET); + minter = _minter; + core = _core; + oracleAggregator = _oracleAggregator; + syntheticAggregator = _syntheticAggregator; tokenSpender = _tokenSpender; + opiumAddress = _opiumAddress; } /// @notice Allows opium commission receiver address to change itself /// @param _opiumAddress address New opium commission receiver address function changeOpiumAddress(address _opiumAddress) external { require(opiumAddress == msg.sender, ERROR_REGISTRY_ONLY_OPIUM_ADDRESS_ALLOWED); - require(_opiumAddress != address(0), "Can't set zero address"); + require(_opiumAddress != address(0), ERROR_REGISTRY_CANT_BE_ZERO_ADDRESS); opiumAddress = _opiumAddress; } // GETTERS - /// @notice Returns address of Opium.Core - /// @param result address Address of Opium.Core - function getCore() external view returns (address result) { - return core; - } - /// @notice Returns address of Opium.TokenMinter /// @param result address Address of Opium.TokenMinter function getMinter() external view returns (address result) { return minter; } + /// @notice Returns address of Opium.Core + /// @param result address Address of Opium.Core + function getCore() external view returns (address result) { + return core; + } + /// @notice Returns address of Opium.OracleAggregator /// @param result address Address of Opium.OracleAggregator function getOracleAggregator() external view returns (address result) { @@ -115,15 +116,15 @@ contract Registry is RegistryErrors { return syntheticAggregator; } - /// @notice Returns address of Opium commission receiver - /// @param result address Address of Opium commission receiver - function getOpiumAddress() external view returns (address result) { - return opiumAddress; - } - /// @notice Returns address of Opium.TokenSpender /// @param result address Address of Opium.TokenSpender function getTokenSpender() external view returns (address result) { return tokenSpender; } + + /// @notice Returns address of Opium commission receiver + /// @param result address Address of Opium commission receiver + function getOpiumAddress() external view returns (address result) { + return opiumAddress; + } } \ No newline at end of file diff --git a/migrations/2_deploy_contract_set_for_tests.js b/migrations/2_deploy_contract_set_for_tests.js index 96f4beb..91a429a 100644 --- a/migrations/2_deploy_contract_set_for_tests.js +++ b/migrations/2_deploy_contract_set_for_tests.js @@ -51,18 +51,12 @@ const deployRegistry = async ({ deployer, opiumDeployerAddress }) => { const registryInstance = await deployer.deploy(Registry, { from: opiumDeployerAddress }) console.log('Registry was deployed at', registryInstance.address) - await registryInstance.setOpiumAddress(opiumDeployerAddress, { from: opiumDeployerAddress }) - console.log('Opium deployer address was set in registry') - return registryInstance } const deployCore = async ({ deployer, opiumDeployerAddress, registryInstance }) => { const coreInstance = await deployer.deploy(Core, registryInstance.address, { from: opiumDeployerAddress }) console.log('- Core was deployed at', coreInstance.address) - - await registryInstance.setCore(coreInstance.address, { from: opiumDeployerAddress }) - console.log('Core address was set in registry') return coreInstance } @@ -88,16 +82,13 @@ const deploySwaprateMatch = async ({ deployer, opiumDeployerAddress, registryIns return swaprateMatchInstance } -const deployTokenSpender = async ({ deployer, opiumDeployerAddress, governor, whitelist, registryInstance }) => { +const deployTokenSpender = async ({ deployer, opiumDeployerAddress, governor, whitelist }) => { const tokenSpenderInstance = await deployer.deploy(TokenSpender, governor, { from: opiumDeployerAddress }) console.log('- TokenSpender was deployed at', tokenSpenderInstance.address) await tokenSpenderInstance.proposeWhitelist(whitelist, { from: governor }) console.log('TokenSpender whitelist was set') - await registryInstance.setTokenSpender(tokenSpenderInstance.address, { from: opiumDeployerAddress }) - console.log('TokenSpender address was set in registry') - return tokenSpenderInstance } @@ -105,32 +96,36 @@ const deployTokenMinter = async ({ deployer, opiumDeployerAddress, registryInsta const tokenMinterInstance = await deployer.deploy(TokenMinter, baseTokenURI, registryInstance.address, { from: opiumDeployerAddress }) console.log('- TokenMinter was deployed at', tokenMinterInstance.address) - await registryInstance.setMinter(tokenMinterInstance.address, { from: opiumDeployerAddress }) - console.log('TokenMinter address was set in registry') - return tokenMinterInstance } -const deployOracleAggregator = async ({ deployer, opiumDeployerAddress, registryInstance }) => { +const deployOracleAggregator = async ({ deployer, opiumDeployerAddress }) => { const oracleAggregatorInstance = await deployer.deploy(OracleAggregator, { from: opiumDeployerAddress }) console.log('- OracleAggregator was deployed at', oracleAggregatorInstance.address) - - await registryInstance.setOracleAggregator(oracleAggregatorInstance.address, { from: opiumDeployerAddress }) - console.log('OracleAggregator address was set in registry') return oracleAggregatorInstance } -const deploySyntheticAggregator = async ({ deployer, opiumDeployerAddress, registryInstance }) => { +const deploySyntheticAggregator = async ({ deployer, opiumDeployerAddress }) => { const syntheticAggregatorInstance = await deployer.deploy(SyntheticAggregator, { from: opiumDeployerAddress }) console.log('- SyntheticAggregator was deployed at', syntheticAggregatorInstance.address) - await registryInstance.setSyntheticAggregator(syntheticAggregatorInstance.address, { from: opiumDeployerAddress }) - console.log('SyntheticAggregator address was set in registry') - return syntheticAggregatorInstance } +const initializeRegistry = async ({ opiumDeployerAddress, registryInstance, tokenMinterInstance, coreInstance, oracleAggregatorInstance, syntheticAggregatorInstance, tokenSpenderInstance }) => { + await registryInstance.init( + tokenMinterInstance.address, + coreInstance.address, + oracleAggregatorInstance.address, + syntheticAggregatorInstance.address, + tokenSpenderInstance.address, + opiumDeployerAddress, + { from: opiumDeployerAddress } + ) + console.log('Registry was initialized') +} + const deployMocks = async ({ deployer, opiumDeployerAddress }) => { const optionCallSyntheticIdMockInstance = await deployer.deploy(OptionCallSyntheticIdMock, { from: opiumDeployerAddress }) console.log('**** OptionCallSyntheticIdMock was deployed at', optionCallSyntheticIdMockInstance.address) @@ -160,11 +155,13 @@ module.exports = async function(deployer, network, accounts) { const governor = opiumDeployerAddress const whitelist = [ coreInstance.address, matchInstance.address, matchPoolInstance.address, swaprateMatchInstance.address ] - await deployTokenSpender({ deployer, opiumDeployerAddress, governor, whitelist, registryInstance }) + const tokenSpenderInstance = await deployTokenSpender({ deployer, opiumDeployerAddress, governor, whitelist }) + + const tokenMinterInstance = await deployTokenMinter({ deployer, opiumDeployerAddress, registryInstance }) + const oracleAggregatorInstance = await deployOracleAggregator({ deployer, opiumDeployerAddress }) + const syntheticAggregatorInstance = await deploySyntheticAggregator({ deployer, opiumDeployerAddress }) - await deployTokenMinter({ deployer, opiumDeployerAddress, registryInstance }) - await deployOracleAggregator({ deployer, opiumDeployerAddress, registryInstance }) - await deploySyntheticAggregator({ deployer, opiumDeployerAddress, registryInstance }) + await initializeRegistry({ opiumDeployerAddress, registryInstance, tokenMinterInstance, coreInstance, oracleAggregatorInstance, syntheticAggregatorInstance, tokenSpenderInstance }) // Contracts for testing purpose await deployMocks({ deployer, opiumDeployerAddress }) diff --git a/test/OracleAggregator.js b/test/OracleAggregator.js index 46a8e97..37bc075 100644 --- a/test/OracleAggregator.js +++ b/test/OracleAggregator.js @@ -9,6 +9,7 @@ contract('OracleAggregator', accounts => { const core = accounts[1] const author = accounts[2] const oracle = accounts[3] + const randomAddress = accounts[4] let registry, oracleAggregator, oracleIdMock let timestamp, timestampPlusOne, timestampMinusOne, timestampPlusTwo, timestampPlusThree @@ -19,8 +20,15 @@ contract('OracleAggregator', accounts => { registry = await Registry.new({ from: owner }) oracleAggregator = await OracleAggregator.new(registry.address, { from: owner }) - await registry.setOracleAggregator(oracleAggregator.address, { from: owner }) - await registry.setCore(core, { from: owner }) + await registry.init( + randomAddress, + core, + oracleAggregator.address, + randomAddress, + randomAddress, + randomAddress, + { from: owner } + ) // Money related fetchPrice = web3.utils.toWei('0.1', 'ether') diff --git a/test/Registry.js b/test/Registry.js index 5c17bac..e33c56c 100644 --- a/test/Registry.js +++ b/test/Registry.js @@ -1,8 +1,9 @@ const Registry = artifacts.require('Registry') -const Core = artifacts.require('Core') const TokenMinter = artifacts.require('TokenMinter') +const Core = artifacts.require('Core') const OracleAggregator = artifacts.require('OracleAggregator') const SyntheticAggregator = artifacts.require('SyntheticAggregator') +const TokenSpender = artifacts.require('TokenSpender') contract('Registry', accounts => { @@ -10,61 +11,95 @@ contract('Registry', accounts => { const opiumAddress = accounts[1] const thirdPartyAddress = accounts[2] - let registry, core, tokenMinter, oracleAggregator, syntheticAggregator + let registry, core, tokenMinter, oracleAggregator, syntheticAggregator, tokenSpender before(async () => { - registry = await Registry.new() - core = await Core.deployed() + registry = await Registry.new({ from: owner }) tokenMinter = await TokenMinter.deployed() + core = await Core.deployed() oracleAggregator = await OracleAggregator.deployed() syntheticAggregator = await SyntheticAggregator.deployed() + syntheticAggregator = await SyntheticAggregator.deployed() + tokenSpender = await TokenSpender.deployed() }) it('should revert for non initializer address', async () => { try { - await registry.setCore(core.address, { from: thirdPartyAddress }) + await registry.init( + tokenMinter.address, + core.address, + oracleAggregator.address, + syntheticAggregator.address, + tokenSpender.address, + opiumAddress, + { from: thirdPartyAddress } + ) throw null } catch (e) { assert.ok(e.message.match(/REGISTRY:ONLY_INITIALIZER/), 'REGISTRY:ONLY_INITIALIZER') } }) - it('should correctly set core address', async () => { - await registry.setCore(core.address, { from: owner }) - const result = await registry.getCore() - assert.equal(core.address, result, 'Core address does not match') + // Validation + + it('should successfully initialize', async () => { + await registry.init( + tokenMinter.address, + core.address, + oracleAggregator.address, + syntheticAggregator.address, + tokenSpender.address, + opiumAddress, + { from: owner } + ) }) it('should revert secondary call try', async () => { try { - await registry.setCore(core.address, { from: owner }) + await registry.init( + tokenMinter.address, + core.address, + oracleAggregator.address, + syntheticAggregator.address, + tokenSpender.address, + opiumAddress, + { from: owner } + ) throw null } catch (e) { assert.ok(e.message.match(/REGISTRY:ALREADY_SET/), 'REGISTRY:ALREADY_SET') } }) - it('should correctly set token minter address', async () => { - await registry.setMinter(tokenMinter.address, { from: owner }) + it('should correctly get core address', async () => { + const result = await registry.getCore() + assert.equal(core.address, result, 'Core address does not match') + }) + + it('should correctly get token minter address', async () => { const result = await registry.getMinter() assert.equal(tokenMinter.address, result, 'Minter address does not match') }) - it('should correctly set oracle aggregator address', async () => { - await registry.setOracleAggregator(oracleAggregator.address, { from: owner }) + it('should correctly get oracle aggregator address', async () => { const result = await registry.getOracleAggregator() assert.equal(oracleAggregator.address, result, 'OracleAggregator address does not match') }) - it('should correctly set synthetic aggregator address', async () => { - await registry.setSyntheticAggregator(syntheticAggregator.address, { from: owner }) + it('should correctly get synthetic aggregator address', async () => { const result = await registry.getSyntheticAggregator() assert.equal(syntheticAggregator.address, result, 'SyntheticAggregator address does not match') }) - it('should correctly set opium address', async () => { - await registry.setOpiumAddress(opiumAddress, { from: owner }) + it('should correctly get token spender address', async () => { + const result = await registry.getTokenSpender() + assert.equal(tokenSpender.address, result, 'TokenSpender address does not match') + }) + + it('should correctly return opium address', async () => { const result = await registry.getOpiumAddress() assert.equal(result, opiumAddress, 'Opium address does not match') }) + + // change validation + change success }) diff --git a/test/TokenMinter.js b/test/TokenMinter.js index dea1758..f5fb2bc 100644 --- a/test/TokenMinter.js +++ b/test/TokenMinter.js @@ -9,6 +9,7 @@ contract('TokenMinter', accounts => { const core = accounts[1] const buyer = accounts[2] const seller = accounts[3] + const randomAddress = accounts[3] let registry, tokenMinter let hashOne, hashTwo, hashThree @@ -17,12 +18,18 @@ contract('TokenMinter', accounts => { let portfolioHashSellerOne, portfolioHashSellerTwo, portfolioHashSellerThree before(async () => { - registry = await Registry.new() + registry = await Registry.new({ from: owner }) tokenMinter = await TokenMinter.new('https://explorer.opium.network/erc721o/', registry.address) - // Change core to simulate it's actions - registry.setCore(core, { from: owner }) - registry.setMinter(tokenMinter.address, { from: owner }) + await registry.init( + tokenMinter.address, + core, + randomAddress, + randomAddress, + randomAddress, + randomAddress, + { from: owner } + ) hashOne = web3.utils.soliditySha3(10, 20, 30, 40, core, buyer, seller) // Random derivativeHash one hashTwo = web3.utils.soliditySha3(50, 60, 70, 80, seller, core, buyer) // Random derivativeHash two From 2032c03f8f6e171f6588cfe39c36ab2d271bd1c6 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Tue, 7 Jan 2020 16:36:58 +0100 Subject: [PATCH 15/23] Deployment script for production --- .../2_deploy_contract_set_for_production.js | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 scripts/2_deploy_contract_set_for_production.js diff --git a/scripts/2_deploy_contract_set_for_production.js b/scripts/2_deploy_contract_set_for_production.js new file mode 100644 index 0000000..44cae66 --- /dev/null +++ b/scripts/2_deploy_contract_set_for_production.js @@ -0,0 +1,148 @@ +/* +//////////////////////////////////// +//// //// +//// MIGRATIONS FOR PRODUCTION //// +//// //// +//////////////////////////////////// +*/ + +const LibPosition = artifacts.require('LibPosition') + +const Registry = artifacts.require('Registry') +const Core = artifacts.require('Core') +const SyntheticAggregator = artifacts.require('SyntheticAggregator') +const OracleAggregator = artifacts.require('OracleAggregator') +const TokenMinter = artifacts.require('TokenMinter') +const TokenSpender = artifacts.require('TokenSpender') + +const Match = artifacts.require('Match') +const MatchPool = artifacts.require('MatchPool') +const SwaprateMatch = artifacts.require('SwaprateMatch') + +const baseTokenURI = 'https://explorer.opium.network/erc721o/' +const governor = 'SET_GOVERNOR_ADDRESS_HERE' + +// Deployment functions +const deployAndLinkLibPosition = async ({ deployer, opiumDeployerAddress }) => { + const libPositionInstance = await deployer.deploy(LibPosition, { from: opiumDeployerAddress }) + + console.log('LibPosition was deployed at', libPositionInstance.address) + + await deployer.link(LibPosition, [ + Core, + TokenMinter, + + Match, + MatchPool, + SwaprateMatch + ]) + + console.log('LibPosition was linked to Core, TokenMinter, Match, MatchPool and SwaprateMatch') + + return libPositionInstance +} + +const deployRegistry = async ({ deployer, opiumDeployerAddress }) => { + const registryInstance = await deployer.deploy(Registry, { from: opiumDeployerAddress }) + console.log('Registry was deployed at', registryInstance.address) + + return registryInstance +} + +const deployCore = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const coreInstance = await deployer.deploy(Core, registryInstance.address, { from: opiumDeployerAddress }) + console.log('- Core was deployed at', coreInstance.address) + + return coreInstance +} + +const deployMatch = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const matchInstance = await deployer.deploy(Match, registryInstance.address, { from: opiumDeployerAddress }) + console.log('- Match was deployed at', matchInstance.address) + + return matchInstance +} + +const deployMatchPool = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const matchPoolInstance = await deployer.deploy(MatchPool, registryInstance.address, { from: opiumDeployerAddress }) + console.log('- MatchPool was deployed at', matchPoolInstance.address) + + return matchPoolInstance +} + +const deploySwaprateMatch = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const swaprateMatchInstance = await deployer.deploy(SwaprateMatch, registryInstance.address, { from: opiumDeployerAddress }) + console.log('- SwaprateMatch was deployed at', swaprateMatchInstance.address) + + return swaprateMatchInstance +} + +const deployTokenSpender = async ({ deployer, opiumDeployerAddress }) => { + const tokenSpenderInstance = await deployer.deploy(TokenSpender, governor, { from: opiumDeployerAddress }) + console.log('- TokenSpender was deployed at', tokenSpenderInstance.address) + + console.log('=====================================') + console.log('=====================================') + console.log('== !!!SET WHITELIST BY GOVERNOR!!! ==') + console.log('=====================================') + console.log('=====================================') + + return tokenSpenderInstance +} + +const deployTokenMinter = async ({ deployer, opiumDeployerAddress, registryInstance }) => { + const tokenMinterInstance = await deployer.deploy(TokenMinter, baseTokenURI, registryInstance.address, { from: opiumDeployerAddress }) + console.log('- TokenMinter was deployed at', tokenMinterInstance.address) + + return tokenMinterInstance +} + +const deployOracleAggregator = async ({ deployer, opiumDeployerAddress }) => { + const oracleAggregatorInstance = await deployer.deploy(OracleAggregator, { from: opiumDeployerAddress }) + console.log('- OracleAggregator was deployed at', oracleAggregatorInstance.address) + + return oracleAggregatorInstance +} + +const deploySyntheticAggregator = async ({ deployer, opiumDeployerAddress }) => { + const syntheticAggregatorInstance = await deployer.deploy(SyntheticAggregator, { from: opiumDeployerAddress }) + console.log('- SyntheticAggregator was deployed at', syntheticAggregatorInstance.address) + + return syntheticAggregatorInstance +} + +const initializeRegistry = async ({ opiumDeployerAddress, registryInstance, tokenMinterInstance, coreInstance, oracleAggregatorInstance, syntheticAggregatorInstance, tokenSpenderInstance }) => { + await registryInstance.init( + tokenMinterInstance.address, + coreInstance.address, + oracleAggregatorInstance.address, + syntheticAggregatorInstance.address, + tokenSpenderInstance.address, + opiumDeployerAddress, + { from: opiumDeployerAddress } + ) + console.log('Registry was initialized') +} + +module.exports = async function(deployer, network, accounts) { + const opiumDeployerAddress = accounts[0] + + deployer.then(async () => { + await deployAndLinkLibPosition({ deployer, opiumDeployerAddress }) + + const registryInstance = await deployRegistry({ deployer, opiumDeployerAddress }) + + const coreInstance = await deployCore({ deployer, opiumDeployerAddress, registryInstance }) + await deployMatch({ deployer, opiumDeployerAddress, registryInstance }) + await deployMatchPool({ deployer, opiumDeployerAddress, registryInstance }) + await deploySwaprateMatch({ deployer, opiumDeployerAddress, registryInstance }) + + const tokenSpenderInstance = await deployTokenSpender({ deployer, opiumDeployerAddress }) + + const tokenMinterInstance = await deployTokenMinter({ deployer, opiumDeployerAddress, registryInstance }) + const oracleAggregatorInstance = await deployOracleAggregator({ deployer, opiumDeployerAddress }) + const syntheticAggregatorInstance = await deploySyntheticAggregator({ deployer, opiumDeployerAddress }) + + await initializeRegistry({ opiumDeployerAddress, registryInstance, tokenMinterInstance, coreInstance, oracleAggregatorInstance, syntheticAggregatorInstance, tokenSpenderInstance }) + }) +} From 2a47ffe30948f612e51ffe627c7d7e992bf92038 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Tue, 7 Jan 2020 17:32:54 +0100 Subject: [PATCH 16/23] Docs updated after fixes --- docs/index.md | 297 +++++++++++++++++++++++++++----------------------- 1 file changed, 161 insertions(+), 136 deletions(-) diff --git a/docs/index.md b/docs/index.md index 29832fe..f296475 100644 --- a/docs/index.md +++ b/docs/index.md @@ -42,23 +42,25 @@ [OracleAggregatorErrors-ERROR_ORACLE_AGGREGATOR_NOT_ENOUGH_ETHER-string]: #OracleAggregatorErrors-ERROR_ORACLE_AGGREGATOR_NOT_ENOUGH_ETHER-string [OracleAggregatorErrors-ERROR_ORACLE_AGGREGATOR_QUERY_WAS_ALREADY_MADE-string]: #OracleAggregatorErrors-ERROR_ORACLE_AGGREGATOR_QUERY_WAS_ALREADY_MADE-string [OracleAggregatorErrors-ERROR_ORACLE_AGGREGATOR_DATA_DOESNT_EXIST-string]: #OracleAggregatorErrors-ERROR_ORACLE_AGGREGATOR_DATA_DOESNT_EXIST-string +[OracleAggregatorErrors-ERROR_ORACLE_AGGREGATOR_DATA_ALREADY_EXIST-string]: #OracleAggregatorErrors-ERROR_ORACLE_AGGREGATOR_DATA_ALREADY_EXIST-string [RegistryErrors]: #RegistryErrors [RegistryErrors-ERROR_REGISTRY_ONLY_INITIALIZER-string]: #RegistryErrors-ERROR_REGISTRY_ONLY_INITIALIZER-string [RegistryErrors-ERROR_REGISTRY_ONLY_OPIUM_ADDRESS_ALLOWED-string]: #RegistryErrors-ERROR_REGISTRY_ONLY_OPIUM_ADDRESS_ALLOWED-string +[RegistryErrors-ERROR_REGISTRY_CANT_BE_ZERO_ADDRESS-string]: #RegistryErrors-ERROR_REGISTRY_CANT_BE_ZERO_ADDRESS-string [RegistryErrors-ERROR_REGISTRY_ALREADY_SET-string]: #RegistryErrors-ERROR_REGISTRY_ALREADY_SET-string [SyntheticAggregatorErrors]: #SyntheticAggregatorErrors [SyntheticAggregatorErrors-ERROR_SYNTHETIC_AGGREGATOR_DERIVATIVE_HASH_NOT_MATCH-string]: #SyntheticAggregatorErrors-ERROR_SYNTHETIC_AGGREGATOR_DERIVATIVE_HASH_NOT_MATCH-string [SyntheticAggregatorErrors-ERROR_SYNTHETIC_AGGREGATOR_WRONG_MARGIN-string]: #SyntheticAggregatorErrors-ERROR_SYNTHETIC_AGGREGATOR_WRONG_MARGIN-string [SyntheticAggregatorErrors-ERROR_SYNTHETIC_AGGREGATOR_COMMISSION_TOO_BIG-string]: #SyntheticAggregatorErrors-ERROR_SYNTHETIC_AGGREGATOR_COMMISSION_TOO_BIG-string -[usingRegistryErrors]: #usingRegistryErrors -[usingRegistryErrors-ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED-string]: #usingRegistryErrors-ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED-string +[UsingRegistryErrors]: #UsingRegistryErrors +[UsingRegistryErrors-ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED-string]: #UsingRegistryErrors-ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED-string [ExecutableByThirdParty]: #ExecutableByThirdParty [ExecutableByThirdParty-thirdpartyExecutionAllowance-mapping-address----bool-]: #ExecutableByThirdParty-thirdpartyExecutionAllowance-mapping-address----bool- [ExecutableByThirdParty-thirdpartyExecutionAllowed-address-]: #ExecutableByThirdParty-thirdpartyExecutionAllowed-address- [ExecutableByThirdParty-allowThirdpartyExecution-bool-]: #ExecutableByThirdParty-allowThirdpartyExecution-bool- [HasCommission]: #HasCommission [HasCommission-author-address]: #HasCommission-author-address -[HasCommission-commission-uint256]: #HasCommission-commission-uint256 +[HasCommission-AUTHOR_COMMISSION-uint256]: #HasCommission-AUTHOR_COMMISSION-uint256 [HasCommission-constructor--]: #HasCommission-constructor-- [HasCommission-getAuthorAddress--]: #HasCommission-getAuthorAddress-- [HasCommission-getAuthorCommission--]: #HasCommission-getAuthorCommission-- @@ -87,15 +89,20 @@ [LibEIP712-EIP712DOMAIN_TYPEHASH-bytes32]: #LibEIP712-EIP712DOMAIN_TYPEHASH-bytes32 [LibEIP712-DOMAIN_SEPARATOR-bytes32]: #LibEIP712-DOMAIN_SEPARATOR-bytes32 [LibEIP712-hashEIP712Message-bytes32-]: #LibEIP712-hashEIP712Message-bytes32- +[UsingRegistry]: #UsingRegistry +[UsingRegistry-onlyCore--]: #UsingRegistry-onlyCore-- +[UsingRegistry-registry-contract-Registry]: #UsingRegistry-registry-contract-Registry +[UsingRegistry-constructor-address-]: #UsingRegistry-constructor-address- +[UsingRegistry-getRegistry--]: #UsingRegistry-getRegistry-- +[UsingRegistry-RegistrySet-address-]: #UsingRegistry-RegistrySet-address- [Whitelisted]: #Whitelisted [Whitelisted-onlyWhitelisted--]: #Whitelisted-onlyWhitelisted-- [Whitelisted-whitelist-address--]: #Whitelisted-whitelist-address-- [Whitelisted-getWhitelist--]: #Whitelisted-getWhitelist-- [WhitelistedWithGovernance]: #WhitelistedWithGovernance [WhitelistedWithGovernance-onlyGovernor--]: #WhitelistedWithGovernance-onlyGovernor-- -[WhitelistedWithGovernance-TIME_LOCK_INTERVAL-uint256]: #WhitelistedWithGovernance-TIME_LOCK_INTERVAL-uint256 +[WhitelistedWithGovernance-timeLockInterval-uint256]: #WhitelistedWithGovernance-timeLockInterval-uint256 [WhitelistedWithGovernance-governor-address]: #WhitelistedWithGovernance-governor-address -[WhitelistedWithGovernance-initialized-bool]: #WhitelistedWithGovernance-initialized-bool [WhitelistedWithGovernance-proposalTime-uint256]: #WhitelistedWithGovernance-proposalTime-uint256 [WhitelistedWithGovernance-proposedWhitelist-address--]: #WhitelistedWithGovernance-proposedWhitelist-address-- [WhitelistedWithGovernance-constructor-uint256-address-]: #WhitelistedWithGovernance-constructor-uint256-address- @@ -106,17 +113,12 @@ [WhitelistedWithGovernance-Proposed-address---]: #WhitelistedWithGovernance-Proposed-address--- [WhitelistedWithGovernance-Committed-address---]: #WhitelistedWithGovernance-Committed-address--- [WhitelistedWithGovernanceAndChangableTimelock]: #WhitelistedWithGovernanceAndChangableTimelock -[WhitelistedWithGovernanceAndChangableTimelock-timelockProposalTime-uint256]: #WhitelistedWithGovernanceAndChangableTimelock-timelockProposalTime-uint256 -[WhitelistedWithGovernanceAndChangableTimelock-proposedTimelock-uint256]: #WhitelistedWithGovernanceAndChangableTimelock-proposedTimelock-uint256 +[WhitelistedWithGovernanceAndChangableTimelock-timeLockProposalTime-uint256]: #WhitelistedWithGovernanceAndChangableTimelock-timeLockProposalTime-uint256 +[WhitelistedWithGovernanceAndChangableTimelock-proposedTimeLock-uint256]: #WhitelistedWithGovernanceAndChangableTimelock-proposedTimeLock-uint256 [WhitelistedWithGovernanceAndChangableTimelock-proposeTimelock-uint256-]: #WhitelistedWithGovernanceAndChangableTimelock-proposeTimelock-uint256- [WhitelistedWithGovernanceAndChangableTimelock-commitTimelock--]: #WhitelistedWithGovernanceAndChangableTimelock-commitTimelock-- [WhitelistedWithGovernanceAndChangableTimelock-Proposed-uint256-]: #WhitelistedWithGovernanceAndChangableTimelock-Proposed-uint256- [WhitelistedWithGovernanceAndChangableTimelock-Committed-uint256-]: #WhitelistedWithGovernanceAndChangableTimelock-Committed-uint256- -[usingRegistry]: #usingRegistry -[usingRegistry-onlyCore--]: #usingRegistry-onlyCore-- -[usingRegistry-registry-contract-Registry]: #usingRegistry-registry-contract-Registry -[usingRegistry-constructor-address-]: #usingRegistry-constructor-address- -[usingRegistry-RegistrySet-address-]: #usingRegistry-RegistrySet-address- [LibOrder]: #LibOrder [LibOrder-EIP712_ORDER_TYPEHASH-bytes32]: #LibOrder-EIP712_ORDER_TYPEHASH-bytes32 [LibOrder-hashOrder-struct-LibOrder-Order-]: #LibOrder-hashOrder-struct-LibOrder-Order- @@ -135,7 +137,7 @@ [MatchLogic-feeTaken-mapping-bytes32----bool-]: #MatchLogic-feeTaken-mapping-bytes32----bool- [MatchLogic-cancel-struct-LibOrder-Order-]: #MatchLogic-cancel-struct-LibOrder-Order- [MatchLogic-withdraw-contract-IERC20-]: #MatchLogic-withdraw-contract-IERC20- -[MatchLogic-validateCanceled-bytes32-]: #MatchLogic-validateCanceled-bytes32- +[MatchLogic-validateNotCanceled-bytes32-]: #MatchLogic-validateNotCanceled-bytes32- [MatchLogic-validateTakerAddress-struct-LibOrder-Order-struct-LibOrder-Order-]: #MatchLogic-validateTakerAddress-struct-LibOrder-Order-struct-LibOrder-Order- [MatchLogic-validateExpiration-struct-LibOrder-Order-]: #MatchLogic-validateExpiration-struct-LibOrder-Order- [MatchLogic-validateSenderAddress-struct-LibOrder-Order-]: #MatchLogic-validateSenderAddress-struct-LibOrder-Order- @@ -166,7 +168,7 @@ [SwaprateMatchBase-feeTaken-mapping-bytes32----bool-]: #SwaprateMatchBase-feeTaken-mapping-bytes32----bool- [SwaprateMatchBase-cancel-struct-LibSwaprateOrder-SwaprateOrder-]: #SwaprateMatchBase-cancel-struct-LibSwaprateOrder-SwaprateOrder- [SwaprateMatchBase-withdraw-contract-IERC20-]: #SwaprateMatchBase-withdraw-contract-IERC20- -[SwaprateMatchBase-validateCanceled-bytes32-]: #SwaprateMatchBase-validateCanceled-bytes32- +[SwaprateMatchBase-validateNotCanceled-bytes32-]: #SwaprateMatchBase-validateNotCanceled-bytes32- [SwaprateMatchBase-validateTakerAddress-struct-LibSwaprateOrder-SwaprateOrder-struct-LibSwaprateOrder-SwaprateOrder-]: #SwaprateMatchBase-validateTakerAddress-struct-LibSwaprateOrder-SwaprateOrder-struct-LibSwaprateOrder-SwaprateOrder- [SwaprateMatchBase-validateSenderAddress-struct-LibSwaprateOrder-SwaprateOrder-]: #SwaprateMatchBase-validateSenderAddress-struct-LibSwaprateOrder-SwaprateOrder- [SwaprateMatchBase-validateSignature-bytes32-struct-LibSwaprateOrder-SwaprateOrder-]: #SwaprateMatchBase-validateSignature-bytes32-struct-LibSwaprateOrder-SwaprateOrder- @@ -194,19 +196,14 @@ [Registry-onlyInitializer--]: #Registry-onlyInitializer-- [Registry-initializer-address]: #Registry-initializer-address [Registry-constructor--]: #Registry-constructor-- -[Registry-setMinter-address-]: #Registry-setMinter-address- -[Registry-setCore-address-]: #Registry-setCore-address- -[Registry-setOracleAggregator-address-]: #Registry-setOracleAggregator-address- -[Registry-setSyntheticAggregator-address-]: #Registry-setSyntheticAggregator-address- -[Registry-setOpiumAddress-address-]: #Registry-setOpiumAddress-address- -[Registry-setTokenSpender-address-]: #Registry-setTokenSpender-address- +[Registry-init-address-address-address-address-address-address-]: #Registry-init-address-address-address-address-address-address- [Registry-changeOpiumAddress-address-]: #Registry-changeOpiumAddress-address- -[Registry-getCore--]: #Registry-getCore-- [Registry-getMinter--]: #Registry-getMinter-- +[Registry-getCore--]: #Registry-getCore-- [Registry-getOracleAggregator--]: #Registry-getOracleAggregator-- [Registry-getSyntheticAggregator--]: #Registry-getSyntheticAggregator-- -[Registry-getOpiumAddress--]: #Registry-getOpiumAddress-- [Registry-getTokenSpender--]: #Registry-getTokenSpender-- +[Registry-getOpiumAddress--]: #Registry-getOpiumAddress-- [SyntheticAggregator]: #SyntheticAggregator [SyntheticAggregator-buyerMarginByHash-mapping-bytes32----uint256-]: #SyntheticAggregator-buyerMarginByHash-mapping-bytes32----uint256- [SyntheticAggregator-sellerMarginByHash-mapping-bytes32----uint256-]: #SyntheticAggregator-sellerMarginByHash-mapping-bytes32----uint256- @@ -232,6 +229,11 @@ [TokenSpender-constructor-address-]: #TokenSpender-constructor-address- [TokenSpender-claimTokens-contract-IERC20-address-address-uint256-]: #TokenSpender-claimTokens-contract-IERC20-address-address-uint256- [TokenSpender-claimPositions-contract-IERC721O-address-address-uint256-uint256-]: #TokenSpender-claimPositions-contract-IERC721O-address-address-uint256-uint256- +[DummySyntheticIdMock]: #DummySyntheticIdMock +[DummySyntheticIdMock-validateInput-struct-LibDerivative-Derivative-]: #DummySyntheticIdMock-validateInput-struct-LibDerivative-Derivative- +[DummySyntheticIdMock-getMargin-struct-LibDerivative-Derivative-]: #DummySyntheticIdMock-getMargin-struct-LibDerivative-Derivative- +[DummySyntheticIdMock-getExecutionPayout-struct-LibDerivative-Derivative-uint256-]: #DummySyntheticIdMock-getExecutionPayout-struct-LibDerivative-Derivative-uint256- +[DummySyntheticIdMock-isPool--]: #DummySyntheticIdMock-isPool-- [OptionCallSyntheticIdMock]: #OptionCallSyntheticIdMock [OptionCallSyntheticIdMock-BASE_PPT-uint256]: #OptionCallSyntheticIdMock-BASE_PPT-uint256 [OptionCallSyntheticIdMock-validateInput-struct-LibDerivative-Derivative-]: #OptionCallSyntheticIdMock-validateInput-struct-LibDerivative-Derivative- @@ -303,7 +305,7 @@ - [`nonReentrant()`][ReentrancyGuard-nonReentrant--] -- [`onlyCore()`][usingRegistry-onlyCore--] +- [`onlyCore()`][UsingRegistry-onlyCore--] - [`constructor(address _registry)`][Core-constructor-address-] - [`withdrawFee(address _tokenAddress)`][Core-withdrawFee-address-] - [`create(struct LibDerivative.Derivative _derivative, uint256 _quantity, address[2] _addresses)`][Core-create-struct-LibDerivative-Derivative-uint256-address-2--] @@ -313,15 +315,16 @@ - [`execute(address _tokenOwner, uint256[] _tokenIds, uint256[] _quantities, struct LibDerivative.Derivative[] _derivatives)`][Core-execute-address-uint256---uint256---struct-LibDerivative-Derivative---] - [`cancel(uint256 _tokenId, uint256 _quantity, struct LibDerivative.Derivative _derivative)`][Core-cancel-uint256-uint256-struct-LibDerivative-Derivative-] - [`cancel(uint256[] _tokenIds, uint256[] _quantities, struct LibDerivative.Derivative[] _derivatives)`][Core-cancel-uint256---uint256---struct-LibDerivative-Derivative---] +- [`getRegistry()`][UsingRegistry-getRegistry--] - [`getDerivativeHash(struct LibDerivative.Derivative _derivative)`][LibDerivative-getDerivativeHash-struct-LibDerivative-Derivative-] - [`Created(address buyer, address seller, bytes32 derivativeHash, uint256 quantity)`][Core-Created-address-address-bytes32-uint256-] - [`Executed(address tokenOwner, uint256 tokenId, uint256 quantity)`][Core-Executed-address-uint256-uint256-] - [`Canceled(bytes32 derivativeHash)`][Core-Canceled-bytes32-] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] ### `constructor(address _registry)` (public) -Calls Core.Lib.usingRegistry constructor +Calls Core.Lib.UsingRegistry constructor @@ -446,7 +449,7 @@ Cancels tickers, burns positions and returns margins to positions owners in case -## `usingRegistryErrors` +## `UsingRegistryErrors` @@ -676,6 +679,44 @@ Hashes EIP712Message +## `UsingRegistry` + + + + + +- [`onlyCore()`][UsingRegistry-onlyCore--] +- [`constructor(address _registry)`][UsingRegistry-constructor-address-] +- [`getRegistry()`][UsingRegistry-getRegistry--] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] + +### `onlyCore()` + +This modifier restricts access to functions, which could be called only by Opium.Core + + + +### `constructor(address _registry)` (public) + +Defines registry instance and emits appropriate event + + + +### `getRegistry() → address` (external) + +Getter for registry variable + + + + +### `RegistrySet(address registry)` + + + + + + + ## `Whitelisted` @@ -817,36 +858,6 @@ Calling this function governor could commit previously proposed new timelock if -## `usingRegistry` - - - - - -- [`onlyCore()`][usingRegistry-onlyCore--] -- [`constructor(address _registry)`][usingRegistry-constructor-address-] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] - -### `onlyCore()` - -This modifier restricts access to functions, which could be called only by Opium.Core - - - -### `constructor(address _registry)` (public) - -Defines registry instance and emits appropriate event - - - -### `RegistrySet(address registry)` - - - - - - - ## `LibOrder` @@ -880,14 +891,14 @@ Verifies order signature - [`nonReentrant()`][ReentrancyGuard-nonReentrant--] -- [`onlyCore()`][usingRegistry-onlyCore--] +- [`onlyCore()`][UsingRegistry-onlyCore--] - [`constructor(address _registry)`][Match-constructor-address-] - [`swap(struct LibOrder.Order _leftOrder, struct LibOrder.Order _rightOrder)`][MatchSwap-swap-struct-LibOrder-Order-struct-LibOrder-Order-] - [`create(struct LibOrder.Order _buyOrder, struct LibOrder.Order _sellOrder, struct LibDerivative.Derivative _derivative, bool _buyerIsMaker)`][MatchCreate-create-struct-LibOrder-Order-struct-LibOrder-Order-struct-LibDerivative-Derivative-bool-] - [`getDerivativeHash(struct LibDerivative.Derivative _derivative)`][LibDerivative-getDerivativeHash-struct-LibDerivative-Derivative-] - [`cancel(struct LibOrder.Order _order)`][MatchLogic-cancel-struct-LibOrder-Order-] - [`withdraw(contract IERC20 _token)`][MatchLogic-withdraw-contract-IERC20-] -- [`validateCanceled(bytes32 _hash)`][MatchLogic-validateCanceled-bytes32-] +- [`validateNotCanceled(bytes32 _hash)`][MatchLogic-validateNotCanceled-bytes32-] - [`validateTakerAddress(struct LibOrder.Order _leftOrder, struct LibOrder.Order _rightOrder)`][MatchLogic-validateTakerAddress-struct-LibOrder-Order-struct-LibOrder-Order-] - [`validateExpiration(struct LibOrder.Order _order)`][MatchLogic-validateExpiration-struct-LibOrder-Order-] - [`validateSenderAddress(struct LibOrder.Order _order)`][MatchLogic-validateSenderAddress-struct-LibOrder-Order-] @@ -896,13 +907,14 @@ Verifies order signature - [`min(uint256 _a, uint256 _b)`][MatchLogic-min-uint256-uint256-] - [`getDivisionPercentage(uint256 _numerator, uint256 _denominator)`][MatchLogic-getDivisionPercentage-uint256-uint256-] - [`getInitialPercentageValue(uint256 _divisionPercentage, uint256 _denominator)`][MatchLogic-getInitialPercentageValue-uint256-uint256-] +- [`getRegistry()`][UsingRegistry-getRegistry--] - [`hashOrder(struct LibOrder.Order _order)`][LibOrder-hashOrder-struct-LibOrder-Order-] - [`verifySignature(bytes32 _hash, bytes _signature, address _address)`][LibOrder-verifySignature-bytes32-bytes-address-] - [`hashEIP712Message(bytes32 hashStruct)`][LibEIP712-hashEIP712Message-bytes32-] - [`Swap(uint256 leftMakerTokenId, uint256 leftMakerTokenAmount, address leftMakerMarginAddress, uint256 leftMakerMarginAmount, uint256 rightMakerTokenId, uint256 rightMakerTokenAmount, address rightMakerMarginAddress, uint256 rightMakerMarginAmount)`][MatchSwap-Swap-uint256-uint256-address-uint256-uint256-uint256-address-uint256-] - [`Create(bytes32 derivativeHash, address buyerPremiumAddress, uint256 buyerPremiumAmount, address sellerPremiumAddress, uint256 sellerPremiumAmount, uint256 filled)`][MatchCreate-Create-bytes32-address-uint256-address-uint256-uint256-] - [`Canceled(bytes32 orderHash)`][MatchLogic-Canceled-bytes32-] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] ### `constructor(address _registry)` (public) @@ -920,12 +932,12 @@ Calls constructors of super-contracts - [`nonReentrant()`][ReentrancyGuard-nonReentrant--] -- [`onlyCore()`][usingRegistry-onlyCore--] +- [`onlyCore()`][UsingRegistry-onlyCore--] - [`create(struct LibOrder.Order _buyOrder, struct LibOrder.Order _sellOrder, struct LibDerivative.Derivative _derivative, bool _buyerIsMaker)`][MatchCreate-create-struct-LibOrder-Order-struct-LibOrder-Order-struct-LibDerivative-Derivative-bool-] - [`getDerivativeHash(struct LibDerivative.Derivative _derivative)`][LibDerivative-getDerivativeHash-struct-LibDerivative-Derivative-] - [`cancel(struct LibOrder.Order _order)`][MatchLogic-cancel-struct-LibOrder-Order-] - [`withdraw(contract IERC20 _token)`][MatchLogic-withdraw-contract-IERC20-] -- [`validateCanceled(bytes32 _hash)`][MatchLogic-validateCanceled-bytes32-] +- [`validateNotCanceled(bytes32 _hash)`][MatchLogic-validateNotCanceled-bytes32-] - [`validateTakerAddress(struct LibOrder.Order _leftOrder, struct LibOrder.Order _rightOrder)`][MatchLogic-validateTakerAddress-struct-LibOrder-Order-struct-LibOrder-Order-] - [`validateExpiration(struct LibOrder.Order _order)`][MatchLogic-validateExpiration-struct-LibOrder-Order-] - [`validateSenderAddress(struct LibOrder.Order _order)`][MatchLogic-validateSenderAddress-struct-LibOrder-Order-] @@ -935,12 +947,13 @@ Calls constructors of super-contracts - [`getDivisionPercentage(uint256 _numerator, uint256 _denominator)`][MatchLogic-getDivisionPercentage-uint256-uint256-] - [`getInitialPercentageValue(uint256 _divisionPercentage, uint256 _denominator)`][MatchLogic-getInitialPercentageValue-uint256-uint256-] - [`constructor()`][ReentrancyGuard-constructor--] +- [`getRegistry()`][UsingRegistry-getRegistry--] - [`hashOrder(struct LibOrder.Order _order)`][LibOrder-hashOrder-struct-LibOrder-Order-] - [`verifySignature(bytes32 _hash, bytes _signature, address _address)`][LibOrder-verifySignature-bytes32-bytes-address-] - [`hashEIP712Message(bytes32 hashStruct)`][LibEIP712-hashEIP712Message-bytes32-] - [`Create(bytes32 derivativeHash, address buyerPremiumAddress, uint256 buyerPremiumAmount, address sellerPremiumAddress, uint256 sellerPremiumAmount, uint256 filled)`][MatchCreate-Create-bytes32-address-uint256-address-uint256-uint256-] - [`Canceled(bytes32 orderHash)`][MatchLogic-Canceled-bytes32-] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] ### `create(struct LibOrder.Order _buyOrder, struct LibOrder.Order _sellOrder, struct LibDerivative.Derivative _derivative, bool _buyerIsMaker)` (public) @@ -964,10 +977,10 @@ This function receives buy and sell orders, derivative related to it and informa - [`nonReentrant()`][ReentrancyGuard-nonReentrant--] -- [`onlyCore()`][usingRegistry-onlyCore--] +- [`onlyCore()`][UsingRegistry-onlyCore--] - [`cancel(struct LibOrder.Order _order)`][MatchLogic-cancel-struct-LibOrder-Order-] - [`withdraw(contract IERC20 _token)`][MatchLogic-withdraw-contract-IERC20-] -- [`validateCanceled(bytes32 _hash)`][MatchLogic-validateCanceled-bytes32-] +- [`validateNotCanceled(bytes32 _hash)`][MatchLogic-validateNotCanceled-bytes32-] - [`validateTakerAddress(struct LibOrder.Order _leftOrder, struct LibOrder.Order _rightOrder)`][MatchLogic-validateTakerAddress-struct-LibOrder-Order-struct-LibOrder-Order-] - [`validateExpiration(struct LibOrder.Order _order)`][MatchLogic-validateExpiration-struct-LibOrder-Order-] - [`validateSenderAddress(struct LibOrder.Order _order)`][MatchLogic-validateSenderAddress-struct-LibOrder-Order-] @@ -977,11 +990,12 @@ This function receives buy and sell orders, derivative related to it and informa - [`getDivisionPercentage(uint256 _numerator, uint256 _denominator)`][MatchLogic-getDivisionPercentage-uint256-uint256-] - [`getInitialPercentageValue(uint256 _divisionPercentage, uint256 _denominator)`][MatchLogic-getInitialPercentageValue-uint256-uint256-] - [`constructor()`][ReentrancyGuard-constructor--] +- [`getRegistry()`][UsingRegistry-getRegistry--] - [`hashOrder(struct LibOrder.Order _order)`][LibOrder-hashOrder-struct-LibOrder-Order-] - [`verifySignature(bytes32 _hash, bytes _signature, address _address)`][LibOrder-verifySignature-bytes32-bytes-address-] - [`hashEIP712Message(bytes32 hashStruct)`][LibEIP712-hashEIP712Message-bytes32-] - [`Canceled(bytes32 orderHash)`][MatchLogic-Canceled-bytes32-] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] ### `cancel(struct LibOrder.Order _order)` (public) @@ -997,7 +1011,7 @@ Function to withdraw fees from orders for relayer and affiliates -### `validateCanceled(bytes32 _hash)` (internal) +### `validateNotCanceled(bytes32 _hash)` (internal) This function checks whether order was canceled @@ -1077,13 +1091,13 @@ numerator = devP * denominator / 100% - [`nonReentrant()`][ReentrancyGuard-nonReentrant--] -- [`onlyCore()`][usingRegistry-onlyCore--] +- [`onlyCore()`][UsingRegistry-onlyCore--] - [`constructor(address _registry)`][MatchPool-constructor-address-] - [`create(struct LibOrder.Order _buyOrder, struct LibDerivative.Derivative _derivative)`][MatchPool-create-struct-LibOrder-Order-struct-LibDerivative-Derivative-] - [`getDerivativeHash(struct LibDerivative.Derivative _derivative)`][LibDerivative-getDerivativeHash-struct-LibDerivative-Derivative-] - [`cancel(struct LibOrder.Order _order)`][MatchLogic-cancel-struct-LibOrder-Order-] - [`withdraw(contract IERC20 _token)`][MatchLogic-withdraw-contract-IERC20-] -- [`validateCanceled(bytes32 _hash)`][MatchLogic-validateCanceled-bytes32-] +- [`validateNotCanceled(bytes32 _hash)`][MatchLogic-validateNotCanceled-bytes32-] - [`validateTakerAddress(struct LibOrder.Order _leftOrder, struct LibOrder.Order _rightOrder)`][MatchLogic-validateTakerAddress-struct-LibOrder-Order-struct-LibOrder-Order-] - [`validateExpiration(struct LibOrder.Order _order)`][MatchLogic-validateExpiration-struct-LibOrder-Order-] - [`validateSenderAddress(struct LibOrder.Order _order)`][MatchLogic-validateSenderAddress-struct-LibOrder-Order-] @@ -1092,11 +1106,12 @@ numerator = devP * denominator / 100% - [`min(uint256 _a, uint256 _b)`][MatchLogic-min-uint256-uint256-] - [`getDivisionPercentage(uint256 _numerator, uint256 _denominator)`][MatchLogic-getDivisionPercentage-uint256-uint256-] - [`getInitialPercentageValue(uint256 _divisionPercentage, uint256 _denominator)`][MatchLogic-getInitialPercentageValue-uint256-uint256-] +- [`getRegistry()`][UsingRegistry-getRegistry--] - [`hashOrder(struct LibOrder.Order _order)`][LibOrder-hashOrder-struct-LibOrder-Order-] - [`verifySignature(bytes32 _hash, bytes _signature, address _address)`][LibOrder-verifySignature-bytes32-bytes-address-] - [`hashEIP712Message(bytes32 hashStruct)`][LibEIP712-hashEIP712Message-bytes32-] - [`Canceled(bytes32 orderHash)`][MatchLogic-Canceled-bytes32-] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] ### `constructor(address _registry)` (public) @@ -1119,11 +1134,11 @@ numerator = devP * denominator / 100% - [`nonReentrant()`][ReentrancyGuard-nonReentrant--] -- [`onlyCore()`][usingRegistry-onlyCore--] +- [`onlyCore()`][UsingRegistry-onlyCore--] - [`swap(struct LibOrder.Order _leftOrder, struct LibOrder.Order _rightOrder)`][MatchSwap-swap-struct-LibOrder-Order-struct-LibOrder-Order-] - [`cancel(struct LibOrder.Order _order)`][MatchLogic-cancel-struct-LibOrder-Order-] - [`withdraw(contract IERC20 _token)`][MatchLogic-withdraw-contract-IERC20-] -- [`validateCanceled(bytes32 _hash)`][MatchLogic-validateCanceled-bytes32-] +- [`validateNotCanceled(bytes32 _hash)`][MatchLogic-validateNotCanceled-bytes32-] - [`validateTakerAddress(struct LibOrder.Order _leftOrder, struct LibOrder.Order _rightOrder)`][MatchLogic-validateTakerAddress-struct-LibOrder-Order-struct-LibOrder-Order-] - [`validateExpiration(struct LibOrder.Order _order)`][MatchLogic-validateExpiration-struct-LibOrder-Order-] - [`validateSenderAddress(struct LibOrder.Order _order)`][MatchLogic-validateSenderAddress-struct-LibOrder-Order-] @@ -1133,12 +1148,13 @@ numerator = devP * denominator / 100% - [`getDivisionPercentage(uint256 _numerator, uint256 _denominator)`][MatchLogic-getDivisionPercentage-uint256-uint256-] - [`getInitialPercentageValue(uint256 _divisionPercentage, uint256 _denominator)`][MatchLogic-getInitialPercentageValue-uint256-uint256-] - [`constructor()`][ReentrancyGuard-constructor--] +- [`getRegistry()`][UsingRegistry-getRegistry--] - [`hashOrder(struct LibOrder.Order _order)`][LibOrder-hashOrder-struct-LibOrder-Order-] - [`verifySignature(bytes32 _hash, bytes _signature, address _address)`][LibOrder-verifySignature-bytes32-bytes-address-] - [`hashEIP712Message(bytes32 hashStruct)`][LibEIP712-hashEIP712Message-bytes32-] - [`Swap(uint256 leftMakerTokenId, uint256 leftMakerTokenAmount, address leftMakerMarginAddress, uint256 leftMakerMarginAmount, uint256 rightMakerTokenId, uint256 rightMakerTokenAmount, address rightMakerMarginAddress, uint256 rightMakerMarginAmount)`][MatchSwap-Swap-uint256-uint256-address-uint256-uint256-uint256-address-uint256-] - [`Canceled(bytes32 orderHash)`][MatchLogic-Canceled-bytes32-] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] ### `swap(struct LibOrder.Order _leftOrder, struct LibOrder.Order _rightOrder)` (public) @@ -1188,23 +1204,24 @@ Verifies order signature - [`nonReentrant()`][ReentrancyGuard-nonReentrant--] -- [`onlyCore()`][usingRegistry-onlyCore--] +- [`onlyCore()`][UsingRegistry-onlyCore--] - [`constructor(address _registry)`][SwaprateMatch-constructor-address-] - [`create(struct LibSwaprateOrder.SwaprateOrder _leftOrder, struct LibSwaprateOrder.SwaprateOrder _rightOrder, struct LibDerivative.Derivative _derivative)`][SwaprateMatch-create-struct-LibSwaprateOrder-SwaprateOrder-struct-LibSwaprateOrder-SwaprateOrder-struct-LibDerivative-Derivative-] - [`getDerivativeHash(struct LibDerivative.Derivative _derivative)`][LibDerivative-getDerivativeHash-struct-LibDerivative-Derivative-] - [`cancel(struct LibSwaprateOrder.SwaprateOrder _order)`][SwaprateMatchBase-cancel-struct-LibSwaprateOrder-SwaprateOrder-] - [`withdraw(contract IERC20 _token)`][SwaprateMatchBase-withdraw-contract-IERC20-] -- [`validateCanceled(bytes32 _hash)`][SwaprateMatchBase-validateCanceled-bytes32-] +- [`validateNotCanceled(bytes32 _hash)`][SwaprateMatchBase-validateNotCanceled-bytes32-] - [`validateTakerAddress(struct LibSwaprateOrder.SwaprateOrder _leftOrder, struct LibSwaprateOrder.SwaprateOrder _rightOrder)`][SwaprateMatchBase-validateTakerAddress-struct-LibSwaprateOrder-SwaprateOrder-struct-LibSwaprateOrder-SwaprateOrder-] - [`validateSenderAddress(struct LibSwaprateOrder.SwaprateOrder _order)`][SwaprateMatchBase-validateSenderAddress-struct-LibSwaprateOrder-SwaprateOrder-] - [`validateSignature(bytes32 orderHash, struct LibSwaprateOrder.SwaprateOrder _order)`][SwaprateMatchBase-validateSignature-bytes32-struct-LibSwaprateOrder-SwaprateOrder-] - [`takeFees(bytes32 _orderHash, struct LibSwaprateOrder.SwaprateOrder _order)`][SwaprateMatchBase-takeFees-bytes32-struct-LibSwaprateOrder-SwaprateOrder-] - [`min(uint256 _a, uint256 _b)`][SwaprateMatchBase-min-uint256-uint256-] +- [`getRegistry()`][UsingRegistry-getRegistry--] - [`hashOrder(struct LibSwaprateOrder.SwaprateOrder _order)`][LibSwaprateOrder-hashOrder-struct-LibSwaprateOrder-SwaprateOrder-] - [`verifySignature(bytes32 _hash, bytes _signature, address _address)`][LibSwaprateOrder-verifySignature-bytes32-bytes-address-] - [`hashEIP712Message(bytes32 hashStruct)`][LibEIP712-hashEIP712Message-bytes32-] - [`Canceled(bytes32 orderHash)`][SwaprateMatchBase-Canceled-bytes32-] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] ### `constructor(address _registry)` (public) @@ -1229,21 +1246,22 @@ This function receives left and right orders, derivative related to it - [`nonReentrant()`][ReentrancyGuard-nonReentrant--] -- [`onlyCore()`][usingRegistry-onlyCore--] +- [`onlyCore()`][UsingRegistry-onlyCore--] - [`cancel(struct LibSwaprateOrder.SwaprateOrder _order)`][SwaprateMatchBase-cancel-struct-LibSwaprateOrder-SwaprateOrder-] - [`withdraw(contract IERC20 _token)`][SwaprateMatchBase-withdraw-contract-IERC20-] -- [`validateCanceled(bytes32 _hash)`][SwaprateMatchBase-validateCanceled-bytes32-] +- [`validateNotCanceled(bytes32 _hash)`][SwaprateMatchBase-validateNotCanceled-bytes32-] - [`validateTakerAddress(struct LibSwaprateOrder.SwaprateOrder _leftOrder, struct LibSwaprateOrder.SwaprateOrder _rightOrder)`][SwaprateMatchBase-validateTakerAddress-struct-LibSwaprateOrder-SwaprateOrder-struct-LibSwaprateOrder-SwaprateOrder-] - [`validateSenderAddress(struct LibSwaprateOrder.SwaprateOrder _order)`][SwaprateMatchBase-validateSenderAddress-struct-LibSwaprateOrder-SwaprateOrder-] - [`validateSignature(bytes32 orderHash, struct LibSwaprateOrder.SwaprateOrder _order)`][SwaprateMatchBase-validateSignature-bytes32-struct-LibSwaprateOrder-SwaprateOrder-] - [`takeFees(bytes32 _orderHash, struct LibSwaprateOrder.SwaprateOrder _order)`][SwaprateMatchBase-takeFees-bytes32-struct-LibSwaprateOrder-SwaprateOrder-] - [`min(uint256 _a, uint256 _b)`][SwaprateMatchBase-min-uint256-uint256-] - [`constructor()`][ReentrancyGuard-constructor--] +- [`getRegistry()`][UsingRegistry-getRegistry--] - [`hashOrder(struct LibSwaprateOrder.SwaprateOrder _order)`][LibSwaprateOrder-hashOrder-struct-LibSwaprateOrder-SwaprateOrder-] - [`verifySignature(bytes32 _hash, bytes _signature, address _address)`][LibSwaprateOrder-verifySignature-bytes32-bytes-address-] - [`hashEIP712Message(bytes32 hashStruct)`][LibEIP712-hashEIP712Message-bytes32-] - [`Canceled(bytes32 orderHash)`][SwaprateMatchBase-Canceled-bytes32-] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] ### `cancel(struct LibSwaprateOrder.SwaprateOrder _order)` (public) @@ -1259,7 +1277,7 @@ Function to withdraw fees from orders for relayer and affiliates -### `validateCanceled(bytes32 _hash)` (internal) +### `validateNotCanceled(bytes32 _hash)` (internal) This function checks whether order was canceled @@ -1414,19 +1432,14 @@ Getter for dataExist mapping - [`onlyInitializer()`][Registry-onlyInitializer--] - [`constructor()`][Registry-constructor--] -- [`setMinter(address _minter)`][Registry-setMinter-address-] -- [`setCore(address _core)`][Registry-setCore-address-] -- [`setOracleAggregator(address _oracleAggregator)`][Registry-setOracleAggregator-address-] -- [`setSyntheticAggregator(address _syntheticAggregator)`][Registry-setSyntheticAggregator-address-] -- [`setOpiumAddress(address _opiumAddress)`][Registry-setOpiumAddress-address-] -- [`setTokenSpender(address _tokenSpender)`][Registry-setTokenSpender-address-] +- [`init(address _minter, address _core, address _oracleAggregator, address _syntheticAggregator, address _tokenSpender, address _opiumAddress)`][Registry-init-address-address-address-address-address-address-] - [`changeOpiumAddress(address _opiumAddress)`][Registry-changeOpiumAddress-address-] -- [`getCore()`][Registry-getCore--] - [`getMinter()`][Registry-getMinter--] +- [`getCore()`][Registry-getCore--] - [`getOracleAggregator()`][Registry-getOracleAggregator--] - [`getSyntheticAggregator()`][Registry-getSyntheticAggregator--] -- [`getOpiumAddress()`][Registry-getOpiumAddress--] - [`getTokenSpender()`][Registry-getTokenSpender--] +- [`getOpiumAddress()`][Registry-getOpiumAddress--] ### `onlyInitializer()` @@ -1440,44 +1453,9 @@ Sets initializer -### `setMinter(address _minter)` (external) - -Sets Opium.TokenMinter address and allows to do it only once - - - - -### `setCore(address _core)` (external) - -Sets Opium.Core address and allows to do it only once - - - +### `init(address _minter, address _core, address _oracleAggregator, address _syntheticAggregator, address _tokenSpender, address _opiumAddress)` (external) -### `setOracleAggregator(address _oracleAggregator)` (external) - -Sets Opium.OracleAggregator address and allows to do it only once - - - - -### `setSyntheticAggregator(address _syntheticAggregator)` (external) - -Sets Opium.SyntheticAggregator address and allows to do it only once - - - - -### `setOpiumAddress(address _opiumAddress)` (external) - -Sets Opium commission receiver and allows to do it only once - - - - -### `setTokenSpender(address _tokenSpender)` (external) - -Sets Opium.TokenSpender address and allows to do it only once +Sets Opium.TokenMinter, Opium.Core, Opium.OracleAggregator, Opium.SyntheticAggregator, Opium.TokenSpender, Opium commission receiver addresses and allows to do it only once @@ -1489,16 +1467,16 @@ Allows opium commission receiver address to change itself -### `getCore() → address result` (external) +### `getMinter() → address result` (external) -Returns address of Opium.Core +Returns address of Opium.TokenMinter -### `getMinter() → address result` (external) +### `getCore() → address result` (external) -Returns address of Opium.TokenMinter +Returns address of Opium.Core @@ -1517,16 +1495,16 @@ Returns address of Opium.SyntheticAggregator -### `getOpiumAddress() → address result` (external) +### `getTokenSpender() → address result` (external) -Returns address of Opium commission receiver +Returns address of Opium.TokenSpender -### `getTokenSpender() → address result` (external) +### `getOpiumAddress() → address result` (external) -Returns address of Opium.TokenSpender +Returns address of Opium commission receiver @@ -1590,7 +1568,7 @@ Checks whether `syntheticId` implements pooled logic -- [`onlyCore()`][usingRegistry-onlyCore--] +- [`onlyCore()`][UsingRegistry-onlyCore--] - [`nonReentrant()`][ReentrancyGuard-nonReentrant--] - [`isOperatorOrOwner(address _from)`][ERC721OBase-isOperatorOrOwner-address-] - [`constructor(string _baseTokenURI, address _registry)`][TokenMinter-constructor-string-address-] @@ -1601,6 +1579,7 @@ Checks whether `syntheticId` implements pooled logic - [`symbol()`][TokenMinter-symbol--] - [`isApprovedOrOwner(address _spender, address _owner, uint256 _tokenId)`][TokenMinter-isApprovedOrOwner-address-address-uint256-] - [`isOpiumSpender(address _spender)`][TokenMinter-isOpiumSpender-address-] +- [`getRegistry()`][UsingRegistry-getRegistry--] - [`implementsERC721()`][ERC721OBackwardCompatible-implementsERC721--] - [`ownerOf(uint256 _tokenId)`][ERC721OBackwardCompatible-ownerOf-uint256-] - [`balanceOf(address _owner)`][ERC721OBackwardCompatible-balanceOf-address-] @@ -1637,13 +1616,14 @@ Checks whether `syntheticId` implements pooled logic - [`totalSupply()`][ERC721OBase-totalSupply--] - [`tokensOwned(address _owner)`][ERC721OBase-tokensOwned-address-] - [`setApprovalForAll(address _operator, bool _approved)`][ERC721OBase-setApprovalForAll-address-bool-] +- [`permit(address _holder, address _spender, uint256 _nonce, uint256 _expiry, bool _allowed, bytes _signature)`][ERC721OBase-permit-address-address-uint256-uint256-bool-bytes-] - [`approve(address _to, uint256 _tokenId)`][ERC721OBase-approve-address-uint256-] - [`getApproved(uint256 _tokenId, address _tokenOwner)`][ERC721OBase-getApproved-uint256-address-] - [`isApprovedForAll(address _owner, address _operator)`][ERC721OBase-isApprovedForAll-address-address-] - [`_updateTokenBalance(address _from, uint256 _tokenId, uint256 _amount, enum ObjectLib.Operations op)`][ERC721OBase-_updateTokenBalance-address-uint256-uint256-enum-ObjectLib-Operations-] - [`supportsInterface(bytes4 interfaceId)`][ERC165-supportsInterface-bytes4-] - [`_registerInterface(bytes4 interfaceId)`][ERC165-_registerInterface-bytes4-] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] - [`Transfer(address from, address to, uint256 tokenId)`][IERC721-Transfer-address-address-uint256-] - [`Approval(address owner, address approved, uint256 tokenId)`][IERC721-Approval-address-address-uint256-] - [`ApprovalForAll(address owner, address operator, bool approved)`][IERC721-ApprovalForAll-address-address-bool-] @@ -1756,6 +1736,50 @@ Using this function whitelisted contracts could call ERC721O transfers +## `DummySyntheticIdMock` + + + + + +- [`validateInput(struct LibDerivative.Derivative _derivative)`][DummySyntheticIdMock-validateInput-struct-LibDerivative-Derivative-] +- [`getMargin(struct LibDerivative.Derivative _derivative)`][DummySyntheticIdMock-getMargin-struct-LibDerivative-Derivative-] +- [`getExecutionPayout(struct LibDerivative.Derivative _derivative, uint256 _result)`][DummySyntheticIdMock-getExecutionPayout-struct-LibDerivative-Derivative-uint256-] +- [`isPool()`][DummySyntheticIdMock-isPool--] +- [`constructor()`][HasCommission-constructor--] +- [`getAuthorAddress()`][HasCommission-getAuthorAddress--] +- [`getAuthorCommission()`][HasCommission-getAuthorCommission--] +- [`thirdpartyExecutionAllowed(address derivativeOwner)`][ExecutableByThirdParty-thirdpartyExecutionAllowed-address-] +- [`allowThirdpartyExecution(bool allow)`][ExecutableByThirdParty-allowThirdpartyExecution-bool-] +- [`getDerivativeHash(struct LibDerivative.Derivative _derivative)`][LibDerivative-getDerivativeHash-struct-LibDerivative-Derivative-] +- [`MetadataSet(string metadata)`][IDerivativeLogic-MetadataSet-string-] + +### `validateInput(struct LibDerivative.Derivative _derivative) → bool` (public) + + + + + +### `getMargin(struct LibDerivative.Derivative _derivative) → uint256 buyerMargin, uint256 sellerMargin` (public) + + + + + +### `getExecutionPayout(struct LibDerivative.Derivative _derivative, uint256 _result) → uint256 buyerPayout, uint256 sellerPayout` (public) + + + + + +### `isPool() → bool` (public) + + + + + + + ## `OptionCallSyntheticIdMock` @@ -1806,13 +1830,14 @@ Using this function whitelisted contracts could call ERC721O transfers -- [`onlyCore()`][usingRegistry-onlyCore--] +- [`onlyCore()`][UsingRegistry-onlyCore--] - [`constructor(uint256 _fetchPrice, address _registry)`][OracleIdMock-constructor-uint256-address-] - [`triggerCallback(uint256 timestamp, uint256 returnData)`][OracleIdMock-triggerCallback-uint256-uint256-] - [`fetchData(uint256 timestamp)`][OracleIdMock-fetchData-uint256-] - [`recursivelyFetchData(uint256 timestamp, uint256 period, uint256 times)`][OracleIdMock-recursivelyFetchData-uint256-uint256-uint256-] - [`calculateFetchPrice()`][OracleIdMock-calculateFetchPrice--] -- [`RegistrySet(address registry)`][usingRegistry-RegistrySet-address-] +- [`getRegistry()`][UsingRegistry-getRegistry--] +- [`RegistrySet(address registry)`][UsingRegistry-RegistrySet-address-] - [`MetadataSet(string metadata)`][IOracleId-MetadataSet-string-] ### `constructor(uint256 _fetchPrice, address _registry)` (public) From 870d733d1a8e0a7d0dbe0550a65513fd0ba83954 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Mon, 13 Jan 2020 16:50:44 +0100 Subject: [PATCH 17/23] Fixed .add(1) problem --- contracts/Matching/Match/LibOrder.sol | 2 +- contracts/Matching/Match/MatchCreate.sol | 4 +- contracts/Matching/Match/MatchLogic.sol | 7 +- contracts/Matching/Match/MatchSwap.sol | 4 +- .../SwaprateMatch/LibSwaprateOrder.sol | 2 +- test/Matching/Match/MatchCreate.js | 148 ++++++++++++++---- 6 files changed, 128 insertions(+), 39 deletions(-) diff --git a/contracts/Matching/Match/LibOrder.sol b/contracts/Matching/Match/LibOrder.sol index 4234efb..0998fe8 100644 --- a/contracts/Matching/Match/LibOrder.sol +++ b/contracts/Matching/Match/LibOrder.sol @@ -103,7 +103,7 @@ contract LibOrder is LibEIP712 { /// @notice Hashes the order /// @param _order Order Order to hash /// @return hash bytes32 Order hash - function hashOrder(Order memory _order) internal pure returns (bytes32 hash) { + function hashOrder(Order memory _order) public pure returns (bytes32 hash) { hash = keccak256( abi.encodePacked( abi.encodePacked( diff --git a/contracts/Matching/Match/MatchCreate.sol b/contracts/Matching/Match/MatchCreate.sol index 7c293f5..e72d4c2 100644 --- a/contracts/Matching/Match/MatchCreate.sol +++ b/contracts/Matching/Match/MatchCreate.sol @@ -198,8 +198,8 @@ contract MatchCreate is MatchLogic, LibDerivative { // Update filled // If initial takerTokenAmount was 0, set filled to 100% // Otherwise calculate new filled percetage -> (alreadyFilled + fill) / initial * 100% - filled[_leftOrderHash] = leftInitial == 0 ? PERCENTAGE_BASE : getDivisionPercentage(leftAlreadyFilled.add(fillPositions), leftInitial).add(1); - filled[_rightOrderHash] = rightInitial == 0 ? PERCENTAGE_BASE : getDivisionPercentage(rightAlreadyFilled.add(fillPositions), rightInitial).add(1); + filled[_leftOrderHash] = leftInitial == 0 ? PERCENTAGE_BASE : getDivisionPercentage(leftAlreadyFilled.add(fillPositions), leftInitial); + filled[_rightOrderHash] = rightInitial == 0 ? PERCENTAGE_BASE : getDivisionPercentage(rightAlreadyFilled.add(fillPositions), rightInitial); } /// @notice This function distributes premiums, takes margin and approves it to Core diff --git a/contracts/Matching/Match/MatchLogic.sol b/contracts/Matching/Match/MatchLogic.sol index e39827c..8422af6 100644 --- a/contracts/Matching/Match/MatchLogic.sol +++ b/contracts/Matching/Match/MatchLogic.sol @@ -192,7 +192,12 @@ contract MatchLogic is MatchingErrors, LibOrder, UsingRegistry, ReentrancyGuard /// @param _denominator uint256 Denominator of division /// @return divisionPercentage uint256 Percentage of division function getDivisionPercentage(uint256 _numerator, uint256 _denominator) internal pure returns (uint256 divisionPercentage) { - divisionPercentage = _numerator.mul(PERCENTAGE_BASE).div(_denominator); + divisionPercentage = _numerator.mul(PERCENTAGE_BASE).div(_denominator).add(1); + + // In case of numerator > denominator consider as 100% + if (divisionPercentage > PERCENTAGE_BASE) { + divisionPercentage = PERCENTAGE_BASE; + } } /// @notice Helper to recover numerator from percentage of division in base of PERCENTAGE_BASE diff --git a/contracts/Matching/Match/MatchSwap.sol b/contracts/Matching/Match/MatchSwap.sol index cee903b..520982a 100644 --- a/contracts/Matching/Match/MatchSwap.sol +++ b/contracts/Matching/Match/MatchSwap.sol @@ -134,13 +134,13 @@ contract MatchSwap is MatchLogic { leftFilledPercents[0] = leftInitial[0] == 0 ? PERCENTAGE_BASE : getDivisionPercentage(leftAlreadyFilled[0].add(rightFill[0]), leftInitial[0]); leftFilledPercents[1] = leftInitial[1] == 0 ? PERCENTAGE_BASE : getDivisionPercentage(leftAlreadyFilled[1].add(rightFill[1]), leftInitial[1]); - filled[_leftOrderHash] = min(leftFilledPercents[0], leftFilledPercents[1]).add(1); + filled[_leftOrderHash] = min(leftFilledPercents[0], leftFilledPercents[1]); uint256[2] memory rightFilledPercents; rightFilledPercents[0] = rightInitial[0] == 0 ? PERCENTAGE_BASE : getDivisionPercentage(rightAlreadyFilled[0].add(leftFill[0]), rightInitial[0]); rightFilledPercents[1] = rightInitial[1] == 0 ? PERCENTAGE_BASE : getDivisionPercentage(rightAlreadyFilled[1].add(leftFill[1]), rightInitial[1]); - filled[_rightOrderHash] = min(rightFilledPercents[0], rightFilledPercents[1]).add(1); + filled[_rightOrderHash] = min(rightFilledPercents[0], rightFilledPercents[1]); } /// @notice Validate order properties and distribute tokens and margins diff --git a/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol b/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol index 0b0f274..5080f57 100644 --- a/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol +++ b/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol @@ -124,7 +124,7 @@ contract LibSwaprateOrder is LibEIP712 { /// @notice Hashes the order /// @param _order SwaprateOrder Order to hash /// @return hash bytes32 Order hash - function hashOrder(SwaprateOrder memory _order) internal pure returns (bytes32 hash) { + function hashOrder(SwaprateOrder memory _order) public pure returns (bytes32 hash) { hash = keccak256( abi.encodePacked( abi.encodePacked( diff --git a/test/Matching/Match/MatchCreate.js b/test/Matching/Match/MatchCreate.js index 55578ea..37e4bb4 100644 --- a/test/Matching/Match/MatchCreate.js +++ b/test/Matching/Match/MatchCreate.js @@ -569,11 +569,27 @@ contract('Match and MatchCreate', accounts => { } }) - it('should partially fill right (maker) order by 1, and have 2 left (taker)', async () => { + it('should partially fill right (maker) order by 1, and have 2 left', async () => { await testToken.transfer(buyer, dai(9), { from: owner }) - // Seller balance: 600 - // Buyer balance: 9 + // Buyer balance: 9 DAI + 0 LONG position + 0 SHORT position + // Seller balance: 600 DAI + 0 LONG position + 0 SHORT position + + const buyerBalanceBefore = await testToken.balanceOf(buyer) + const buyerLongPositionBalanceBefore = await tokenMinter.balanceOf(buyer, longTokenId) + const buyerShortPositionBalanceBefore = await tokenMinter.balanceOf(buyer, shortTokenId) + const sellerBalanceBefore = await testToken.balanceOf(seller) + const sellerLongPositionBalanceBefore = await tokenMinter.balanceOf(seller, longTokenId) + const sellerShortPositionBalanceBefore = await tokenMinter.balanceOf(seller, shortTokenId) + + assert.equal(buyerBalanceBefore, dai(9), 'Wrong buyer balance before') + assert.equal(buyerLongPositionBalanceBefore, 0, 'Wrong buyer LONG position balance before') + assert.equal(buyerShortPositionBalanceBefore, 0, 'Wrong buyer SHORT position balance before') + + assert.equal(sellerBalanceBefore, dai(600), 'Wrong seller balance before') + assert.equal(sellerLongPositionBalanceBefore, 0, 'Wrong seller LONG position balance before') + assert.equal(sellerShortPositionBalanceBefore, 0, 'Wrong seller SHORT position balance before') + const leftOrder = await orderFactory({ makerMarginAmount: dai(5), @@ -603,20 +619,34 @@ contract('Match and MatchCreate', accounts => { console.log('Gas used during matching creation =', gas) await match.create(leftOrder, rightOrder, derivative, false, { from: relayer }) + // Premium by Seller == 4 DAI per contract + // Buyer -> ( 4 DAI ) -> Seller Premium + // Seller -> ( 200 DAI ) -> Option Margin + + // Buyer balance: 5 DAI + 1 LONG position + 0 SHORT position + // Seller balance: 404 DAI + 0 LONG position + 1 SHORT position + const buyerBalanceAfter = await testToken.balanceOf(buyer) - const buyerPositionBalanceAfter = await tokenMinter.balanceOf(buyer, longTokenId) + const buyerLongPositionBalanceAfter = await tokenMinter.balanceOf(buyer, longTokenId) + const buyerShortPositionBalanceAfter = await tokenMinter.balanceOf(buyer, shortTokenId) const sellerBalanceAfter = await testToken.balanceOf(seller) - const sellerPositionBalanceAfter = await tokenMinter.balanceOf(seller, shortTokenId) + const sellerLongPositionBalanceAfter = await tokenMinter.balanceOf(seller, longTokenId) + const sellerShortPositionBalanceAfter = await tokenMinter.balanceOf(seller, shortTokenId) - // Buyer spent his 8 on premium + // Buyer spent his 4 DAI on premium assert.equal(buyerBalanceAfter, dai(5), 'Wrong buyer balance after') - assert.equal(buyerPositionBalanceAfter, 1, 'Wrong buyer position balance after') - // Seller received 8 as premium and spent 400 as margin for 2 contracts + assert.equal(buyerLongPositionBalanceAfter, 1, 'Wrong buyer LONG position balance after') + assert.equal(buyerShortPositionBalanceAfter, 0, 'Wrong buyer SHORT position balance after') + // Seller received 4 DAI as premium and spent 200 as margin for 1 contract assert.equal(sellerBalanceAfter, dai(404), 'Wrong seller balance after') - assert.equal(sellerPositionBalanceAfter, 1, 'Wrong seller position balance after') + assert.equal(sellerLongPositionBalanceAfter, 0, 'Wrong seller LONG position balance after') + assert.equal(sellerShortPositionBalanceAfter, 1, 'Wrong seller SHORT position balance after') }) it('should partially fill right (taker) order by 1, and have 1 left (maker)', async () => { + // Buyer balance: 5 DAI + 1 LONG position + 0 SHORT position + // Seller balance: 404 DAI + 0 LONG position + 1 SHORT position + const leftOrder = await orderFactory({ makerMarginAmount: dai(5), takerTokenId: longTokenId, @@ -645,17 +675,28 @@ contract('Match and MatchCreate', accounts => { console.log('Gas used during matching creation =', gas) await match.create(leftOrder, rightOrder, derivative, true, { from: relayer }) + // Premium by Buyer == 5 DAI per contract + // Buyer -> ( 5 DAI ) -> Seller Premium + // Seller -> ( 200 DAI ) -> Option Margin + + // Buyer balance: 0 DAI + 2 LONG position + 0 SHORT position + // Seller balance: 209 DAI + 0 LONG position + 2 SHORT position + const buyerBalanceAfter = await testToken.balanceOf(buyer) - const buyerPositionBalanceAfter = await tokenMinter.balanceOf(buyer, longTokenId) + const buyerLongPositionBalanceAfter = await tokenMinter.balanceOf(buyer, longTokenId) + const buyerShortPositionBalanceAfter = await tokenMinter.balanceOf(buyer, shortTokenId) const sellerBalanceAfter = await testToken.balanceOf(seller) - const sellerPositionBalanceAfter = await tokenMinter.balanceOf(seller, shortTokenId) - - // Buyer spent his 8 on premium - assert.equal(buyerBalanceAfter, 0, 'Wrong buyer balance after') - assert.equal(buyerPositionBalanceAfter, 2, 'Wrong buyer position balance after') - // Seller received 8 as premium and spent 400 as margin for 2 contracts + const sellerLongPositionBalanceAfter = await tokenMinter.balanceOf(seller, longTokenId) + const sellerShortPositionBalanceAfter = await tokenMinter.balanceOf(seller, shortTokenId) + + // Buyer spent his 5 DAI on premium + assert.equal(buyerBalanceAfter, dai(0), 'Wrong buyer balance after') + assert.equal(buyerLongPositionBalanceAfter, 2, 'Wrong buyer LONG position balance after') + assert.equal(buyerShortPositionBalanceAfter, 0, 'Wrong buyer SHORT position balance after') + // Seller received 5 DAI as premium and spent 200 as margin for 1 contract assert.equal(sellerBalanceAfter, dai(209), 'Wrong seller balance after') - assert.equal(sellerPositionBalanceAfter, 2, 'Wrong seller position balance after') + assert.equal(sellerLongPositionBalanceAfter, 0, 'Wrong seller LONG position balance after') + assert.equal(sellerShortPositionBalanceAfter, 2, 'Wrong seller SHORT position balance after') }) it('should settle shared order (no senderAddress)', async () => { @@ -665,6 +706,9 @@ contract('Match and MatchCreate', accounts => { await testToken.approve(tokenSpender.address, dai(4), { from: buyer }) await testToken.approve(tokenSpender.address, dai(200), { from: seller }) + // Buyer balance: 4 DAI + 2 LONG position + 0 SHORT position + // Seller balance: 409 DAI + 0 LONG position + 2 SHORT position + const leftOrder = await sharedOrderFactory({ makerMarginAmount: dai(4), takerTokenId: longTokenId, @@ -693,17 +737,28 @@ contract('Match and MatchCreate', accounts => { console.log('Gas used during matching shared creation =', gas) await match.create(leftOrder, rightOrder, derivative, false, { from: relayer }) + // Premium by Seller == 4 DAI per contract + // Buyer -> ( 4 DAI ) -> Seller Premium + // Seller -> ( 200 DAI ) -> Option Margin + + // Buyer balance: 0 DAI + 3 LONG position + 0 SHORT position + // Seller balance: 213 DAI + 0 LONG position + 3 SHORT position + const buyerBalanceAfter = await testToken.balanceOf(buyer) - const buyerPositionBalanceAfter = await tokenMinter.balanceOf(buyer, longTokenId) + const buyerLongPositionBalanceAfter = await tokenMinter.balanceOf(buyer, longTokenId) + const buyerShortPositionBalanceAfter = await tokenMinter.balanceOf(buyer, shortTokenId) const sellerBalanceAfter = await testToken.balanceOf(seller) - const sellerPositionBalanceAfter = await tokenMinter.balanceOf(seller, shortTokenId) - - // Buyer spent his 4 on premium - assert.equal(buyerBalanceAfter, 0, 'Wrong buyer balance after') - assert.equal(buyerPositionBalanceAfter, 3, 'Wrong buyer position balance after') - // Seller received 4 as premium and spent 200 as margin for 1 contract + const sellerLongPositionBalanceAfter = await tokenMinter.balanceOf(seller, longTokenId) + const sellerShortPositionBalanceAfter = await tokenMinter.balanceOf(seller, shortTokenId) + + // Buyer spent his 4 DAI on premium + assert.equal(buyerBalanceAfter, dai(0), 'Wrong buyer balance after') + assert.equal(buyerLongPositionBalanceAfter, 3, 'Wrong buyer LONG position balance after') + assert.equal(buyerShortPositionBalanceAfter, 0, 'Wrong buyer SHORT position balance after') + // Seller received 4 DAI as premium and spent 200 as margin for 1 contract assert.equal(sellerBalanceAfter, dai(213), 'Wrong seller balance after') - assert.equal(sellerPositionBalanceAfter, 3, 'Wrong seller position balance after') + assert.equal(sellerLongPositionBalanceAfter, 0, 'Wrong seller LONG position balance after') + assert.equal(sellerShortPositionBalanceAfter, 3, 'Wrong seller SHORT position balance after') }) it('should revert already filled order', async () => { @@ -741,8 +796,7 @@ contract('Match and MatchCreate', accounts => { }) it('should fully fill right order by 1 buy reselling short position of left\'s', async () => { - // Create one position for test - // guyTwo would have 1 x shortTokenId + // Create one contract for test for guyOne and guyTwo await testToken.approve(tokenSpender.address, derivative.margin, { from: owner }) await core.create(derivative, 1, [ guyOne, guyTwo ], { from: owner }) @@ -750,9 +804,25 @@ contract('Match and MatchCreate', accounts => { await testToken.approve(tokenSpender.address, dai(5), { from: guyTwo }) await testToken.approve(tokenSpender.address, dai(200), { from: seller }) - // GuyOne balance: 0 DAI + 1 LONG position - // GuyTwo balance: 5 DAI + 1 SHORT position - // Seller balance: 213 DAI + 3 SHORT positions + // GuyOne balance: 0 DAI + 1 LONG position + 0 SHORT positions + // GuyTwo balance: 5 DAI + 0 LONG positions + 1 SHORT position + // Seller balance: 213 DAI + 0 LONG position + 3 SHORT position + + const guyOneBalanceBefore = await testToken.balanceOf(guyOne) + const guyOneLongPositionBalanceBefore = await tokenMinter.balanceOf(guyOne, longTokenId) + const guyOneShortPositionBalanceBefore = await tokenMinter.balanceOf(guyOne, shortTokenId) + + const guyTwoBalanceBefore = await testToken.balanceOf(guyTwo) + const guyTwoLongPositionBalanceBefore = await tokenMinter.balanceOf(guyTwo, longTokenId) + const guyTwoShortPositionBalanceBefore = await tokenMinter.balanceOf(guyTwo, shortTokenId) + + assert.equal(guyOneBalanceBefore, dai(0), 'Wrong guyOne balance before') + assert.equal(guyOneLongPositionBalanceBefore, 1, 'Wrong guyOne LONG position balance before') + assert.equal(guyOneShortPositionBalanceBefore, 0, 'Wrong guyOne SHORT position balance before') + + assert.equal(guyTwoBalanceBefore, dai(5), 'Wrong guyTwo balance before') + assert.equal(guyTwoLongPositionBalanceBefore, 0, 'Wrong guyTwo LONG position balance before') + assert.equal(guyTwoShortPositionBalanceBefore, 1, 'Wrong guyTwo SHORT position balance before') const leftOrder = await orderFactory({ makerTokenId: shortTokenId, @@ -784,13 +854,27 @@ contract('Match and MatchCreate', accounts => { console.log('Gas used during matching swap =', gas) await match.swap(leftOrder, rightOrder, { from: relayer }) + // Right takerMarginAmount was filled with 66.(6)% == 8 DAI, so only 4 DAI remaining + + // Buyer -> ( 4 DAI, 1 SHORT ) -> Seller + // Seller -> ( 200 DAI ) -> Buyer + + // GuyTwo balance: 201 DAI + 0 LONG positions + 0 SHORT position + // Seller balance: 17 DAI + 0 LONG position + 4 SHORT position + const guyTwoBalanceAfter = await testToken.balanceOf(guyTwo) + const guyTwoLongPositionBalanceAfter = await tokenMinter.balanceOf(guyTwo, longTokenId) + const guyTwoShortPositionBalanceAfter = await tokenMinter.balanceOf(guyTwo, shortTokenId) const sellerBalanceAfter = await testToken.balanceOf(seller) - const sellerPositionBalanceAfter = await tokenMinter.balanceOf(seller, shortTokenId) + const sellerLongPositionBalanceAfter = await tokenMinter.balanceOf(seller, longTokenId) + const sellerShortPositionBalanceAfter = await tokenMinter.balanceOf(seller, shortTokenId) assert.equal(guyTwoBalanceAfter, dai(201), 'Wrong guy two balance after') + assert.equal(guyTwoLongPositionBalanceAfter, 0, 'Wrong guy two LONG position balance after') + assert.equal(guyTwoShortPositionBalanceAfter, 0, 'Wrong guy two SHORT position balance after') assert.equal(sellerBalanceAfter, dai(17), 'Wrong seller balance after') - assert.equal(sellerPositionBalanceAfter, 4, 'Wrong seller position balance after') + assert.equal(sellerLongPositionBalanceAfter, 0, 'Wrong seller LONG position balance after') + assert.equal(sellerShortPositionBalanceAfter, 4, 'Wrong seller SHORT position balance after') }) it('should revert non-fillable swaps', async () => { From a633c2345f10d42d9886e37d0ca7d6456cb77824 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Mon, 13 Jan 2020 18:12:22 +0100 Subject: [PATCH 18/23] Updated docs after fixes --- docs/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/index.md b/docs/index.md index f296475..81fa80e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -868,7 +868,7 @@ Calling this function governor could commit previously proposed new timelock if - [`verifySignature(bytes32 _hash, bytes _signature, address _address)`][LibOrder-verifySignature-bytes32-bytes-address-] - [`hashEIP712Message(bytes32 hashStruct)`][LibEIP712-hashEIP712Message-bytes32-] -### `hashOrder(struct LibOrder.Order _order) → bytes32 hash` (internal) +### `hashOrder(struct LibOrder.Order _order) → bytes32 hash` (public) Hashes the order @@ -1181,7 +1181,7 @@ This function receives left and right orders, and performs swap of Token + Margi - [`verifySignature(bytes32 _hash, bytes _signature, address _address)`][LibSwaprateOrder-verifySignature-bytes32-bytes-address-] - [`hashEIP712Message(bytes32 hashStruct)`][LibEIP712-hashEIP712Message-bytes32-] -### `hashOrder(struct LibSwaprateOrder.SwaprateOrder _order) → bytes32 hash` (internal) +### `hashOrder(struct LibSwaprateOrder.SwaprateOrder _order) → bytes32 hash` (public) Hashes the order From 171d5909ec2dd590f6c1b101dbcb74ddca1c58b6 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Tue, 14 Jan 2020 09:17:56 +0100 Subject: [PATCH 19/23] Updated compiler version to 0.5.16 (latest) as no tests were broken and few bugfixes introduced --- contracts/Core.sol | 2 +- contracts/Errors/CoreErrors.sol | 2 +- contracts/Errors/MatchingErrors.sol | 2 +- contracts/Errors/OracleAggregatorErrors.sol | 2 +- contracts/Errors/RegistryErrors.sol | 2 +- contracts/Errors/SyntheticAggregatorErrors.sol | 2 +- contracts/Errors/UsingRegistryErrors.sol | 2 +- contracts/Errors/usingRegistryErrors.sol | 2 +- contracts/Helpers/ExecutableByThirdParty.sol | 2 +- contracts/Helpers/HasCommission.sol | 2 +- contracts/Interface/IDerivativeLogic.sol | 2 +- contracts/Interface/IOracleId.sol | 2 +- contracts/Lib/LibCommission.sol | 2 +- contracts/Lib/LibDerivative.sol | 2 +- contracts/Lib/LibEIP712.sol | 2 +- contracts/Lib/UsingRegistry.sol | 2 +- contracts/Lib/Whitelisted.sol | 2 +- contracts/Lib/WhitelistedWithGovernance.sol | 2 +- contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol | 2 +- contracts/Lib/usingRegistry.sol | 2 +- contracts/Matching/Match/LibOrder.sol | 2 +- contracts/Matching/Match/Match.sol | 2 +- contracts/Matching/Match/MatchCreate.sol | 2 +- contracts/Matching/Match/MatchLogic.sol | 2 +- contracts/Matching/Match/MatchPool.sol | 2 +- contracts/Matching/Match/MatchSwap.sol | 2 +- contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol | 2 +- contracts/Matching/SwaprateMatch/SwaprateMatch.sol | 2 +- contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol | 2 +- contracts/Migrations.sol | 2 +- contracts/OracleAggregator.sol | 2 +- contracts/Registry.sol | 2 +- contracts/SyntheticAggregator.sol | 2 +- contracts/TokenMinter.sol | 2 +- contracts/TokenSpender.sol | 2 +- contracts/test/DummySyntheticIdMock.sol | 2 +- contracts/test/OptionCallSyntheticIdMock.sol | 2 +- contracts/test/OracleIdMock.sol | 2 +- contracts/test/TestToken.sol | 2 +- contracts/test/WETH.sol | 2 +- truffle-config.js | 2 +- 41 files changed, 41 insertions(+), 41 deletions(-) diff --git a/contracts/Core.sol b/contracts/Core.sol index b82ef77..4fc3eba 100644 --- a/contracts/Core.sol +++ b/contracts/Core.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; diff --git a/contracts/Errors/CoreErrors.sol b/contracts/Errors/CoreErrors.sol index d41fb9b..6905d62 100644 --- a/contracts/Errors/CoreErrors.sol +++ b/contracts/Errors/CoreErrors.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; contract CoreErrors { string constant internal ERROR_CORE_NOT_POOL = "CORE:NOT_POOL"; diff --git a/contracts/Errors/MatchingErrors.sol b/contracts/Errors/MatchingErrors.sol index 5fd1ad4..c79a195 100644 --- a/contracts/Errors/MatchingErrors.sol +++ b/contracts/Errors/MatchingErrors.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; contract MatchingErrors { string constant internal ERROR_MATCH_CANCELLATION_NOT_ALLOWED = "MATCH:CANCELLATION_NOT_ALLOWED"; diff --git a/contracts/Errors/OracleAggregatorErrors.sol b/contracts/Errors/OracleAggregatorErrors.sol index 16d8156..ce97f96 100644 --- a/contracts/Errors/OracleAggregatorErrors.sol +++ b/contracts/Errors/OracleAggregatorErrors.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; contract OracleAggregatorErrors { string constant internal ERROR_ORACLE_AGGREGATOR_NOT_ENOUGH_ETHER = "ORACLE_AGGREGATOR:NOT_ENOUGH_ETHER"; diff --git a/contracts/Errors/RegistryErrors.sol b/contracts/Errors/RegistryErrors.sol index 6090c00..59fb4bb 100644 --- a/contracts/Errors/RegistryErrors.sol +++ b/contracts/Errors/RegistryErrors.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; contract RegistryErrors { string constant internal ERROR_REGISTRY_ONLY_INITIALIZER = "REGISTRY:ONLY_INITIALIZER"; diff --git a/contracts/Errors/SyntheticAggregatorErrors.sol b/contracts/Errors/SyntheticAggregatorErrors.sol index 77b90a3..5f23abd 100644 --- a/contracts/Errors/SyntheticAggregatorErrors.sol +++ b/contracts/Errors/SyntheticAggregatorErrors.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; contract SyntheticAggregatorErrors { string constant internal ERROR_SYNTHETIC_AGGREGATOR_DERIVATIVE_HASH_NOT_MATCH = "SYNTHETIC_AGGREGATOR:DERIVATIVE_HASH_NOT_MATCH"; diff --git a/contracts/Errors/UsingRegistryErrors.sol b/contracts/Errors/UsingRegistryErrors.sol index 982b6ef..4f6d47f 100644 --- a/contracts/Errors/UsingRegistryErrors.sol +++ b/contracts/Errors/UsingRegistryErrors.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; contract UsingRegistryErrors { string constant internal ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED = "USING_REGISTRY:ONLY_CORE_ALLOWED"; diff --git a/contracts/Errors/usingRegistryErrors.sol b/contracts/Errors/usingRegistryErrors.sol index 982b6ef..4f6d47f 100644 --- a/contracts/Errors/usingRegistryErrors.sol +++ b/contracts/Errors/usingRegistryErrors.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; contract UsingRegistryErrors { string constant internal ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED = "USING_REGISTRY:ONLY_CORE_ALLOWED"; diff --git a/contracts/Helpers/ExecutableByThirdParty.sol b/contracts/Helpers/ExecutableByThirdParty.sol index b9c5092..8ac08e3 100644 --- a/contracts/Helpers/ExecutableByThirdParty.sol +++ b/contracts/Helpers/ExecutableByThirdParty.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; /// @title Opium.Helpers.ExecutableByThirdParty contract helps to syntheticId development and responsible for getting and setting thirdparty execution settings contract ExecutableByThirdParty { diff --git a/contracts/Helpers/HasCommission.sol b/contracts/Helpers/HasCommission.sol index f276e32..58a9d97 100644 --- a/contracts/Helpers/HasCommission.sol +++ b/contracts/Helpers/HasCommission.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; /// @title Opium.Helpers.HasCommission contract helps to syntheticId development and responsible for commission and author address contract HasCommission { diff --git a/contracts/Interface/IDerivativeLogic.sol b/contracts/Interface/IDerivativeLogic.sol index fb89b35..ee21e14 100644 --- a/contracts/Interface/IDerivativeLogic.sol +++ b/contracts/Interface/IDerivativeLogic.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "../Lib/LibDerivative.sol"; diff --git a/contracts/Interface/IOracleId.sol b/contracts/Interface/IOracleId.sol index fd7c342..2169cf3 100644 --- a/contracts/Interface/IOracleId.sol +++ b/contracts/Interface/IOracleId.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; /// @title Opium.Interface.IOracleId contract is an interface that every oracleId should implement interface IOracleId { diff --git a/contracts/Lib/LibCommission.sol b/contracts/Lib/LibCommission.sol index af50a9f..2568ac7 100644 --- a/contracts/Lib/LibCommission.sol +++ b/contracts/Lib/LibCommission.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; /// @title Opium.Lib.LibCommission contract defines constants for Opium commissions contract LibCommission { diff --git a/contracts/Lib/LibDerivative.sol b/contracts/Lib/LibDerivative.sol index cf26f9b..a34a4fb 100644 --- a/contracts/Lib/LibDerivative.sol +++ b/contracts/Lib/LibDerivative.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; /// @title Opium.Lib.LibDerivative contract should be inherited by contracts that use Derivative structure and calculate derivativeHash diff --git a/contracts/Lib/LibEIP712.sol b/contracts/Lib/LibEIP712.sol index d4522b4..658bff9 100644 --- a/contracts/Lib/LibEIP712.sol +++ b/contracts/Lib/LibEIP712.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; /// @title Opium.Lib.LibEIP712 contract implements the domain of EIP712 for meta transactions contract LibEIP712 { diff --git a/contracts/Lib/UsingRegistry.sol b/contracts/Lib/UsingRegistry.sol index dd6e304..fb0fb7e 100644 --- a/contracts/Lib/UsingRegistry.sol +++ b/contracts/Lib/UsingRegistry.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; import "../Registry.sol"; diff --git a/contracts/Lib/Whitelisted.sol b/contracts/Lib/Whitelisted.sol index 291fa01..7a96039 100644 --- a/contracts/Lib/Whitelisted.sol +++ b/contracts/Lib/Whitelisted.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; /// @title Opium.Lib.Whitelisted contract implements whitelist with modifier to restrict access to only whitelisted addresses contract Whitelisted { diff --git a/contracts/Lib/WhitelistedWithGovernance.sol b/contracts/Lib/WhitelistedWithGovernance.sol index 759c969..4d19bbf 100644 --- a/contracts/Lib/WhitelistedWithGovernance.sol +++ b/contracts/Lib/WhitelistedWithGovernance.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; import "./Whitelisted.sol"; diff --git a/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol b/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol index ce48af0..a2a0dac 100644 --- a/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol +++ b/contracts/Lib/WhitelistedWithGovernanceAndChangableTimelock.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; import "./WhitelistedWithGovernance.sol"; diff --git a/contracts/Lib/usingRegistry.sol b/contracts/Lib/usingRegistry.sol index dd6e304..fb0fb7e 100644 --- a/contracts/Lib/usingRegistry.sol +++ b/contracts/Lib/usingRegistry.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; import "../Registry.sol"; diff --git a/contracts/Matching/Match/LibOrder.sol b/contracts/Matching/Match/LibOrder.sol index 0998fe8..ef10ef6 100644 --- a/contracts/Matching/Match/LibOrder.sol +++ b/contracts/Matching/Match/LibOrder.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "../../Lib/LibEIP712.sol"; diff --git a/contracts/Matching/Match/Match.sol b/contracts/Matching/Match/Match.sol index 260429b..7c93769 100644 --- a/contracts/Matching/Match/Match.sol +++ b/contracts/Matching/Match/Match.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "./MatchCreate.sol"; diff --git a/contracts/Matching/Match/MatchCreate.sol b/contracts/Matching/Match/MatchCreate.sol index e72d4c2..ba3c89e 100644 --- a/contracts/Matching/Match/MatchCreate.sol +++ b/contracts/Matching/Match/MatchCreate.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "../../Lib/LibDerivative.sol"; diff --git a/contracts/Matching/Match/MatchLogic.sol b/contracts/Matching/Match/MatchLogic.sol index 8422af6..d07f1d1 100644 --- a/contracts/Matching/Match/MatchLogic.sol +++ b/contracts/Matching/Match/MatchLogic.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; diff --git a/contracts/Matching/Match/MatchPool.sol b/contracts/Matching/Match/MatchPool.sol index 7757b0f..db8d967 100644 --- a/contracts/Matching/Match/MatchPool.sol +++ b/contracts/Matching/Match/MatchPool.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "../../Lib/LibDerivative.sol"; diff --git a/contracts/Matching/Match/MatchSwap.sol b/contracts/Matching/Match/MatchSwap.sol index 520982a..92fb888 100644 --- a/contracts/Matching/Match/MatchSwap.sol +++ b/contracts/Matching/Match/MatchSwap.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "./MatchLogic.sol"; diff --git a/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol b/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol index 5080f57..cc45c4a 100644 --- a/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol +++ b/contracts/Matching/SwaprateMatch/LibSwaprateOrder.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "../../Lib/LibEIP712.sol"; diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatch.sol b/contracts/Matching/SwaprateMatch/SwaprateMatch.sol index cbc4f86..79579be 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatch.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatch.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "../../Lib/LibDerivative.sol"; diff --git a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol index 7c88529..98c71e6 100644 --- a/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol +++ b/contracts/Matching/SwaprateMatch/SwaprateMatchBase.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; diff --git a/contracts/Migrations.sol b/contracts/Migrations.sol index 8ad11ad..ae31da4 100644 --- a/contracts/Migrations.sol +++ b/contracts/Migrations.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; contract Migrations { address public owner; diff --git a/contracts/OracleAggregator.sol b/contracts/OracleAggregator.sol index a89808f..2e4b91f 100644 --- a/contracts/OracleAggregator.sol +++ b/contracts/OracleAggregator.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; import "openzeppelin-solidity/contracts/utils/ReentrancyGuard.sol"; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; diff --git a/contracts/Registry.sol b/contracts/Registry.sol index b0bf0fb..c2044d0 100644 --- a/contracts/Registry.sol +++ b/contracts/Registry.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; import "./Errors/RegistryErrors.sol"; diff --git a/contracts/SyntheticAggregator.sol b/contracts/SyntheticAggregator.sol index 22bb950..3891c93 100644 --- a/contracts/SyntheticAggregator.sol +++ b/contracts/SyntheticAggregator.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/utils/ReentrancyGuard.sol"; diff --git a/contracts/TokenMinter.sol b/contracts/TokenMinter.sol index fde97b3..94fa8ae 100644 --- a/contracts/TokenMinter.sol +++ b/contracts/TokenMinter.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; import "erc721o/contracts/ERC721OBackwardCompatible.sol"; diff --git a/contracts/TokenSpender.sol b/contracts/TokenSpender.sol index a3b2b78..3b09c61 100644 --- a/contracts/TokenSpender.sol +++ b/contracts/TokenSpender.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; import "openzeppelin-solidity/contracts/token/ERC20/SafeERC20.sol"; diff --git a/contracts/test/DummySyntheticIdMock.sol b/contracts/test/DummySyntheticIdMock.sol index ce6f43e..c1bc054 100644 --- a/contracts/test/DummySyntheticIdMock.sol +++ b/contracts/test/DummySyntheticIdMock.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; diff --git a/contracts/test/OptionCallSyntheticIdMock.sol b/contracts/test/OptionCallSyntheticIdMock.sol index 4bfc7c1..cb9e8b3 100644 --- a/contracts/test/OptionCallSyntheticIdMock.sol +++ b/contracts/test/OptionCallSyntheticIdMock.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; pragma experimental ABIEncoderV2; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; diff --git a/contracts/test/OracleIdMock.sol b/contracts/test/OracleIdMock.sol index 127daea..bda58d2 100644 --- a/contracts/test/OracleIdMock.sol +++ b/contracts/test/OracleIdMock.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; import "../Interface/IOracleId.sol"; import "../Lib/UsingRegistry.sol"; diff --git a/contracts/test/TestToken.sol b/contracts/test/TestToken.sol index a9a5ddc..cc6cc5b 100644 --- a/contracts/test/TestToken.sol +++ b/contracts/test/TestToken.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; contract ERC20Basic { uint256 public totalSupply; diff --git a/contracts/test/WETH.sol b/contracts/test/WETH.sol index 306e377..e850b61 100644 --- a/contracts/test/WETH.sol +++ b/contracts/test/WETH.sol @@ -1,4 +1,4 @@ -pragma solidity 0.5.7; +pragma solidity 0.5.16; contract WETH { string public name = "Wrapped Ether"; diff --git a/truffle-config.js b/truffle-config.js index b314dd8..5a87042 100644 --- a/truffle-config.js +++ b/truffle-config.js @@ -19,7 +19,7 @@ module.exports = { compilers: { solc: { - version: '0.5.7', + version: '0.5.16', settings: { optimizer: { enabled: true, From 3b8e5a19f7fe1714faaf6d85b55af2e528e74405 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Thu, 16 Jan 2020 10:21:25 +0100 Subject: [PATCH 20/23] Remove wrong case name file --- contracts/Errors/usingRegistryErrors.sol | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 contracts/Errors/usingRegistryErrors.sol diff --git a/contracts/Errors/usingRegistryErrors.sol b/contracts/Errors/usingRegistryErrors.sol deleted file mode 100644 index 4f6d47f..0000000 --- a/contracts/Errors/usingRegistryErrors.sol +++ /dev/null @@ -1,5 +0,0 @@ -pragma solidity 0.5.16; - -contract UsingRegistryErrors { - string constant internal ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED = "USING_REGISTRY:ONLY_CORE_ALLOWED"; -} From 47497a71dca779ef58ba6765b5e638a4c9612ecc Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Thu, 16 Jan 2020 10:22:28 +0100 Subject: [PATCH 21/23] Remove wrong case name file --- contracts/Lib/usingRegistry.sol | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 contracts/Lib/usingRegistry.sol diff --git a/contracts/Lib/usingRegistry.sol b/contracts/Lib/usingRegistry.sol deleted file mode 100644 index fb0fb7e..0000000 --- a/contracts/Lib/usingRegistry.sol +++ /dev/null @@ -1,32 +0,0 @@ -pragma solidity 0.5.16; - -import "../Registry.sol"; - -import "../Errors/UsingRegistryErrors.sol"; - -/// @title Opium.Lib.UsingRegistry contract should be inherited by contracts, that are going to use Opium.Registry -contract UsingRegistry is UsingRegistryErrors { - // Emitted when registry instance is set - event RegistrySet(address registry); - - // Instance of Opium.Registry contract - Registry internal registry; - - /// @notice This modifier restricts access to functions, which could be called only by Opium.Core - modifier onlyCore() { - require(msg.sender == registry.getCore(), ERROR_USING_REGISTRY_ONLY_CORE_ALLOWED); - _; - } - - /// @notice Defines registry instance and emits appropriate event - constructor(address _registry) public { - registry = Registry(_registry); - emit RegistrySet(_registry); - } - - /// @notice Getter for registry variable - /// @return address Address of registry set in current contract - function getRegistry() external view returns (address) { - return address(registry); - } -} From abd03d778b9b0f3668ab3e603a111974cbafa644 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Thu, 16 Jan 2020 10:30:53 +0100 Subject: [PATCH 22/23] Change HasCommission properties visibility --- contracts/Helpers/HasCommission.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/Helpers/HasCommission.sol b/contracts/Helpers/HasCommission.sol index 58a9d97..2f0e7fd 100644 --- a/contracts/Helpers/HasCommission.sol +++ b/contracts/Helpers/HasCommission.sol @@ -3,9 +3,9 @@ pragma solidity 0.5.16; /// @title Opium.Helpers.HasCommission contract helps to syntheticId development and responsible for commission and author address contract HasCommission { // Address of syntheticId author - address public author; + address internal author; // Commission is in Opium.Lib.LibCommission.COMMISSION_BASE base - uint256 constant public AUTHOR_COMMISSION = 25; // 0.25% of profit + uint256 constant internal AUTHOR_COMMISSION = 25; // 0.25% of profit /// @notice Sets `msg.sender` as syntheticId author constructor() public { From 722e7ed3ca01586dfd7d9594e7816fa2911c57b6 Mon Sep 17 00:00:00 2001 From: Ali Nuraldin Date: Thu, 16 Jan 2020 10:45:14 +0100 Subject: [PATCH 23/23] Update ERC721o version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index c335790..8114683 100644 --- a/package-lock.json +++ b/package-lock.json @@ -847,8 +847,8 @@ } }, "erc721o": { - "version": "git+https://github.com/OpiumProtocol/erc721o.git#e1dc484e3c1a6a4e89dd210a20ca3e9b442fc309", - "from": "git+https://github.com/OpiumProtocol/erc721o.git#e1dc484" + "version": "git+https://github.com/OpiumProtocol/erc721o.git#60723b713e3f3f9d98d71b800673e0b4696fe108", + "from": "git+https://github.com/OpiumProtocol/erc721o.git#60723b7" }, "err-code": { "version": "1.1.2", diff --git a/package.json b/package.json index 2ad7b4d..ba9df3f 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ }, "homepage": "https://github.com/Blockeys/opium-contracts-prep#readme", "dependencies": { - "erc721o": "git+https://github.com/OpiumProtocol/erc721o.git#e1dc484", + "erc721o": "git+https://github.com/OpiumProtocol/erc721o.git#60723b7", "eth-gas-reporter": "^0.2.12", "openzeppelin-solidity": "^2.4.0", "solidity-docgen": "^0.3.13",