@@ -3,27 +3,30 @@ const Main = require("../index.js");
3
3
const winston = require ( "winston" ) ;
4
4
const sinon = require ( "sinon" ) ;
5
5
6
+ const { SpyTransport, spyLogIncludes, spyLogLevel } = require ( "@uma/financial-templates-lib" ) ;
7
+ const { interfaceName, RegistryRolesEnum } = require ( "@uma/common" ) ;
8
+ const { getAddress } = require ( "@uma/contracts-node" ) ;
9
+ const { assert } = require ( "chai" ) ;
10
+ const { deployments, getContract, web3, getChainId } = require ( "hardhat" ) ;
11
+
6
12
const { toWei, utf8ToHex, padRight } = web3 . utils ;
7
13
8
- const { SpyTransport, spyLogIncludes, spyLogLevel } = require ( "@uma/financial-templates-lib" ) ;
9
- const { addGlobalHardhatTestingAddress, interfaceName, RegistryRolesEnum } = require ( "@uma/common" ) ;
10
- const { getTruffleContract } = require ( "@uma/core" ) ;
11
-
12
- const PerpetualLib = getTruffleContract ( "PerpetualLib" , web3 ) ;
13
- const PerpetualCreator = getTruffleContract ( "PerpetualCreator" , web3 ) ;
14
- const IdentifierWhitelist = getTruffleContract ( "IdentifierWhitelist" , web3 ) ;
15
- const Token = getTruffleContract ( "ExpandedERC20" , web3 ) ;
16
- const AddressWhitelist = getTruffleContract ( "AddressWhitelist" , web3 ) ;
17
- const Timer = getTruffleContract ( "Timer" , web3 ) ;
18
- const TokenFactory = getTruffleContract ( "TokenFactory" , web3 ) ;
19
- const Finder = getTruffleContract ( "Finder" , web3 ) ;
20
- const Registry = getTruffleContract ( "Registry" , web3 ) ;
21
- const OptimisticOracle = getTruffleContract ( "OptimisticOracle" , web3 ) ;
22
- const Store = getTruffleContract ( "Store" , web3 ) ;
23
- const MulticallMock = getTruffleContract ( "MulticallMock" , web3 ) ;
24
-
25
- contract ( "index.js" , function ( accounts ) {
26
- const deployer = accounts [ 0 ] ;
14
+ const PerpetualLib = getContract ( "PerpetualLib" ) ;
15
+ const PerpetualCreator = getContract ( "PerpetualCreator" ) ;
16
+ const IdentifierWhitelist = getContract ( "IdentifierWhitelist" ) ;
17
+ const Token = getContract ( "ExpandedERC20" ) ;
18
+ const AddressWhitelist = getContract ( "AddressWhitelist" ) ;
19
+ const Timer = getContract ( "Timer" ) ;
20
+ const TokenFactory = getContract ( "TokenFactory" ) ;
21
+ const Finder = getContract ( "Finder" ) ;
22
+ const Registry = getContract ( "Registry" ) ;
23
+ const OptimisticOracle = getContract ( "OptimisticOracle" ) ;
24
+ const Store = getContract ( "Store" ) ;
25
+ const MulticallMock = getContract ( "MulticallMock" ) ;
26
+
27
+ describe ( "index.js" , function ( ) {
28
+ // Accounts
29
+ let deployer ;
27
30
28
31
// Contracts
29
32
let perpFactory ;
@@ -71,63 +74,93 @@ contract("index.js", function (accounts) {
71
74
const optimisticOracleLiveness = 100 ;
72
75
73
76
before ( async function ( ) {
74
- const timer = await Timer . new ( ) ;
75
- const tokenFactory = await TokenFactory . new ( ) ;
76
- const finder = await Finder . new ( ) ;
77
- multicall = await MulticallMock . new ( ) ;
77
+ [ deployer ] = await web3 . eth . getAccounts ( ) ;
78
+
79
+ const timer = await Timer . new ( ) . send ( { from : deployer } ) ;
80
+ const tokenFactory = await TokenFactory . new ( ) . send ( { from : deployer } ) ;
81
+ const finder = await Finder . new ( ) . send ( { from : deployer } ) ;
82
+ multicall = await MulticallMock . new ( ) . send ( { from : deployer } ) ;
78
83
79
84
// Whitelist an initial identifier so we can deploy.
80
- identifierWhitelist = await IdentifierWhitelist . new ( ) ;
81
- await identifierWhitelist . addSupportedIdentifier ( defaultCreationParams . priceFeedIdentifier ) ;
82
- await identifierWhitelist . addSupportedIdentifier ( defaultCreationParams . fundingRateIdentifier ) ;
83
- await finder . changeImplementationAddress ( utf8ToHex ( interfaceName . IdentifierWhitelist ) , identifierWhitelist . address ) ;
85
+ identifierWhitelist = await IdentifierWhitelist . new ( ) . send ( { from : deployer } ) ;
86
+ await identifierWhitelist . methods
87
+ . addSupportedIdentifier ( defaultCreationParams . priceFeedIdentifier )
88
+ . send ( { from : deployer } ) ;
89
+ await identifierWhitelist . methods
90
+ . addSupportedIdentifier ( defaultCreationParams . fundingRateIdentifier )
91
+ . send ( { from : deployer } ) ;
92
+ await finder . methods
93
+ . changeImplementationAddress ( utf8ToHex ( interfaceName . IdentifierWhitelist ) , identifierWhitelist . options . address )
94
+ . send ( { from : deployer } ) ;
84
95
85
96
// Deploy new registry so perp factory can register contracts.
86
- const registry = await Registry . new ( ) ;
87
- await finder . changeImplementationAddress ( utf8ToHex ( interfaceName . Registry ) , registry . address ) ;
97
+ const registry = await Registry . new ( ) . send ( { from : deployer } ) ;
98
+ await finder . methods
99
+ . changeImplementationAddress ( utf8ToHex ( interfaceName . Registry ) , registry . options . address )
100
+ . send ( { from : deployer } ) ;
88
101
89
102
// Store is neccessary to set up because contracts will need to read final fees before allowing
90
103
// a proposal.
91
- const store = await Store . new ( { rawValue : "0" } , { rawValue : "0" } , timer . address ) ;
92
- await finder . changeImplementationAddress ( utf8ToHex ( interfaceName . Store ) , store . address ) ;
104
+ const store = await Store . new ( { rawValue : "0" } , { rawValue : "0" } , timer . options . address ) . send ( { from : deployer } ) ;
105
+ await finder . methods
106
+ . changeImplementationAddress ( utf8ToHex ( interfaceName . Store ) , store . options . address )
107
+ . send ( { from : deployer } ) ;
93
108
94
109
// Funding rates are proposed to an OptimisticOracle.
95
- const optimisticOracle = await OptimisticOracle . new ( optimisticOracleLiveness , finder . address , timer . address ) ;
96
- await registry . addMember ( RegistryRolesEnum . CONTRACT_CREATOR , optimisticOracle . address ) ;
97
- await finder . changeImplementationAddress ( utf8ToHex ( interfaceName . OptimisticOracle ) , optimisticOracle . address ) ;
110
+ const optimisticOracle = await OptimisticOracle . new (
111
+ optimisticOracleLiveness ,
112
+ finder . options . address ,
113
+ timer . options . address
114
+ ) . send ( { from : deployer } ) ;
115
+ await registry . methods
116
+ . addMember ( RegistryRolesEnum . CONTRACT_CREATOR , optimisticOracle . options . address )
117
+ . send ( { from : deployer } ) ;
118
+ await finder . methods
119
+ . changeImplementationAddress ( utf8ToHex ( interfaceName . OptimisticOracle ) , optimisticOracle . options . address )
120
+ . send ( { from : deployer } ) ;
98
121
99
122
// Whitelist collateral and use the same collateral for all contracts.
100
- collateral = await Token . new ( "Wrapped Ether" , "WETH" , "18" ) ;
101
- collateralWhitelist = await AddressWhitelist . new ( ) ;
102
- await collateralWhitelist . addToWhitelist ( collateral . address ) ;
103
- defaultCreationParams = { ...defaultCreationParams , collateralAddress : collateral . address } ;
104
- await finder . changeImplementationAddress ( utf8ToHex ( interfaceName . CollateralWhitelist ) , collateralWhitelist . address ) ;
105
-
106
- await PerpetualCreator . link ( await PerpetualLib . new ( ) ) ;
107
- perpFactory = await PerpetualCreator . new ( finder . address , tokenFactory . address , timer . address ) ;
108
- await registry . addMember ( RegistryRolesEnum . CONTRACT_CREATOR , perpFactory . address ) ;
109
- // Set the address in the global name space to enable proposer's index.js to access it via `core/getAddressTest`.
110
- addGlobalHardhatTestingAddress ( "PerpetualCreator" , perpFactory . address ) ;
111
-
112
- // Deploy new Perp
113
- const perpAddress = await perpFactory . createPerpetual . call ( defaultCreationParams , configStoreParams , {
114
- from : deployer ,
115
- } ) ;
116
- const perpCreation = await perpFactory . createPerpetual ( defaultCreationParams , configStoreParams , {
123
+ collateral = await Token . new ( "Wrapped Ether" , "WETH" , "18" ) . send ( { from : deployer } ) ;
124
+ collateralWhitelist = await AddressWhitelist . new ( ) . send ( { from : deployer } ) ;
125
+ await collateralWhitelist . methods . addToWhitelist ( collateral . options . address ) . send ( { from : deployer } ) ;
126
+ defaultCreationParams = { ...defaultCreationParams , collateralAddress : collateral . options . address } ;
127
+ await finder . methods
128
+ . changeImplementationAddress ( utf8ToHex ( interfaceName . CollateralWhitelist ) , collateralWhitelist . options . address )
129
+ . send ( { from : deployer } ) ;
130
+
131
+ // Deploy new Perpetual factory such that it is retrievable by the client via getAddress
132
+ // Note: use hre.deployments.deploy method to link libraries.
133
+ const perpetualLib = await PerpetualLib . new ( ) . send ( { from : deployer } ) ;
134
+ await deployments . deploy ( "PerpetualCreator" , {
117
135
from : deployer ,
136
+ args : [ finder . options . address , tokenFactory . options . address , timer . options . address ] ,
137
+ libraries : { PerpetualLib : perpetualLib . options . address } ,
118
138
} ) ;
139
+ perpFactory = await PerpetualCreator . at ( await getAddress ( "PerpetualCreator" , parseInt ( await getChainId ( ) ) ) ) ;
140
+ await registry . methods
141
+ . addMember ( RegistryRolesEnum . CONTRACT_CREATOR , perpFactory . options . address )
142
+ . send ( { from : deployer } ) ;
143
+
144
+ // Deploy new Perp
145
+ const perpAddress = await perpFactory . methods
146
+ . createPerpetual ( defaultCreationParams , configStoreParams )
147
+ . call ( { from : deployer } ) ;
148
+ const perpCreation = await perpFactory . methods
149
+ . createPerpetual ( defaultCreationParams , configStoreParams )
150
+ . send ( { from : deployer } ) ;
119
151
perpsCreated . push ( { transaction : perpCreation , address : perpAddress } ) ;
152
+
120
153
// This is the time that the funding rate applier's update time is initialized to:
121
- let contractStartTime = await timer . getCurrentTime ( ) ;
154
+ let contractStartTime = await timer . methods . getCurrentTime ( ) . call ( { from : deployer } ) ;
122
155
123
156
// Set the pricefeed's latestUpdateTime to be +1 second from the funding rate applier's
124
157
// initialized update time, otherwise any proposals will fail because the new proposal timestamp
125
158
// must always be > than the last update time.
126
- let lastUpdateTime = contractStartTime . toNumber ( ) + 1 ;
159
+ let lastUpdateTime = parseInt ( contractStartTime ) + 1 ;
127
160
commonPriceFeedConfig = { ...commonPriceFeedConfig , lastUpdateTime } ;
128
161
// Advance the perpetual contract forward in time to match pricefeed's update time, otherwise
129
162
// the proposal will fail because it is "in the future".
130
- await timer . setCurrentTime ( lastUpdateTime ) ;
163
+ await timer . methods . setCurrentTime ( lastUpdateTime ) . send ( { from : deployer } ) ;
131
164
} ) ;
132
165
it ( "Completes one iteration without logging any errors" , async function ( ) {
133
166
// We will create a new spy logger, listening for debug events because success logs are tagged with the
@@ -145,7 +178,7 @@ contract("index.js", function (accounts) {
145
178
errorRetries,
146
179
errorRetriesTimeout,
147
180
commonPriceFeedConfig,
148
- multicallAddress : multicall . address ,
181
+ multicallAddress : multicall . options . address ,
149
182
isTest : true , // Need to set this to true so that proposal uses correct request timestamp for test environment
150
183
} ) ;
151
184
0 commit comments