-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract-utils.js
55 lines (48 loc) · 2.04 KB
/
contract-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const {
getDeployedFeeContract,
getDeployedRouterContract,
getDeployedSafeVaultContract,
} = require('./get-deployed-contract')
const { curry } = require('ramda')
const callFxnInContract = (_fxnName, _fxnArgs, _contract) =>
_contract[_fxnName](..._fxnArgs)
const callFxnInContractAndAwaitReceipt = curry((_fxnName, _fxnArgs, _contract) =>
console.info(`✔ Calling '${_fxnName}' function in contract & awaiting mining for the receipt...`) ||
callFxnInContract(_fxnName, _fxnArgs, _contract).then(_tx => _tx.wait())
)
const callReadOnlyFxnInContract = curry((_fxnName, _fxnArgs, _contract) =>
console.info(`✔ Calling '${_fxnName}' function in contract...`) ||
callFxnInContract(_fxnName, _fxnArgs, _contract)
)
const getContractAndCallFunctionAndAwaitReceipt = curry((
_contractType,
_logText,
_contractAddress,
_fxnName,
_fxnArgs
) => {
const contractType = _contractType.toLowerCase()
let getContractFxn
if (contractType === 'router')
getContractFxn = getDeployedRouterContract
else if (contractType === 'fee')
getContractFxn = getDeployedFeeContract
else if (contractType === 'safevault')
getContractFxn = getDeployedSafeVaultContract
else
return Promise.reject(new Error(`Unrecognized contract type: ${_contractType}`))
console.info(_logText)
return getContractFxn(_contractAddress)
.then(_contract => callFxnInContractAndAwaitReceipt(_fxnName, _fxnArgs, _contract))
.then(_receipt => console.info('✔ Success! Transaction receipt:\n', _receipt))
})
const getFeeContractAndCallFunctionAndAwaitReceipt = getContractAndCallFunctionAndAwaitReceipt('fee')
const getRouterContractAndCallFunctionAndAwaitReceipt = getContractAndCallFunctionAndAwaitReceipt('router')
const getSafeVaultContractAndCallFunctionAndAwaitReceipt = getContractAndCallFunctionAndAwaitReceipt('safeVault')
module.exports = {
getSafeVaultContractAndCallFunctionAndAwaitReceipt,
getRouterContractAndCallFunctionAndAwaitReceipt,
getFeeContractAndCallFunctionAndAwaitReceipt,
callFxnInContractAndAwaitReceipt,
callReadOnlyFxnInContract,
}