2019-11-11 12:27:18 +01:00
|
|
|
import { Contract } from 'web3-eth-contract'
|
2019-06-20 00:20:09 +02:00
|
|
|
import ContractHandler from '../../src/keeper/ContractHandler'
|
|
|
|
import Web3Provider from '../../src/keeper/Web3Provider'
|
|
|
|
import Logger from '../../src/utils/Logger'
|
|
|
|
import config from '../config'
|
2018-11-07 14:33:56 +01:00
|
|
|
|
2019-11-11 12:27:18 +01:00
|
|
|
interface ContractTest extends Contract {
|
|
|
|
testContract?: boolean
|
|
|
|
$initialized?: boolean
|
|
|
|
}
|
|
|
|
|
2018-11-07 14:33:56 +01:00
|
|
|
export default class TestContractHandler extends ContractHandler {
|
2019-03-21 02:56:58 +01:00
|
|
|
public static async prepareContracts() {
|
|
|
|
const web3 = Web3Provider.getWeb3(config)
|
2018-11-07 14:33:56 +01:00
|
|
|
const deployerAddress = (await web3.eth.getAccounts())[0]
|
2019-03-21 02:56:58 +01:00
|
|
|
this.networkId = await web3.eth.net.getId()
|
2018-11-07 14:33:56 +01:00
|
|
|
|
|
|
|
// deploy contracts
|
|
|
|
await TestContractHandler.deployContracts(deployerAddress)
|
|
|
|
}
|
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
private static networkId: number
|
|
|
|
|
2018-11-07 14:33:56 +01:00
|
|
|
private static async deployContracts(deployerAddress: string) {
|
2019-06-20 00:20:09 +02:00
|
|
|
Logger.log('Trying to deploy contracts')
|
2018-11-07 14:33:56 +01:00
|
|
|
|
2019-03-06 15:33:10 +01:00
|
|
|
// Libraries
|
2019-11-15 00:00:10 +01:00
|
|
|
const epochLibrary = await TestContractHandler.deployContract(
|
|
|
|
'EpochLibrary',
|
|
|
|
deployerAddress
|
|
|
|
)
|
|
|
|
const didRegistryLibrary = await TestContractHandler.deployContract(
|
|
|
|
'DIDRegistryLibrary',
|
|
|
|
deployerAddress
|
|
|
|
)
|
2019-03-06 15:33:10 +01:00
|
|
|
|
|
|
|
// Contracts
|
2019-11-15 00:00:10 +01:00
|
|
|
const token = await TestContractHandler.deployContract(
|
|
|
|
'OceanToken',
|
2019-06-20 00:20:09 +02:00
|
|
|
deployerAddress,
|
2019-11-15 00:00:10 +01:00
|
|
|
[deployerAddress, deployerAddress]
|
|
|
|
)
|
2019-01-30 11:26:24 +01:00
|
|
|
|
2019-11-15 00:00:10 +01:00
|
|
|
const dispenser = await TestContractHandler.deployContract(
|
|
|
|
'Dispenser',
|
|
|
|
deployerAddress,
|
|
|
|
[token.options.address, deployerAddress]
|
|
|
|
)
|
2019-01-30 11:26:24 +01:00
|
|
|
|
|
|
|
// Add dispenser as Token minter
|
2019-03-07 15:52:40 +01:00
|
|
|
if (!token.$initialized) {
|
2019-11-15 00:00:10 +01:00
|
|
|
await token.methods
|
|
|
|
.addMinter(dispenser.options.address)
|
|
|
|
.send({ from: deployerAddress })
|
2019-03-07 15:52:40 +01:00
|
|
|
}
|
2019-03-06 15:33:10 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
const didRegistry = await TestContractHandler.deployContract(
|
|
|
|
'DIDRegistry',
|
|
|
|
deployerAddress,
|
|
|
|
[deployerAddress],
|
|
|
|
{
|
|
|
|
DIDRegistryLibrary: didRegistryLibrary.options.address
|
|
|
|
}
|
|
|
|
)
|
2019-03-06 15:33:10 +01:00
|
|
|
|
|
|
|
// Managers
|
2019-11-15 00:00:10 +01:00
|
|
|
const templateStoreManager = await TestContractHandler.deployContract(
|
|
|
|
'TemplateStoreManager',
|
|
|
|
deployerAddress,
|
|
|
|
[deployerAddress]
|
|
|
|
)
|
2019-06-20 00:20:09 +02:00
|
|
|
const conditionStoreManager = await TestContractHandler.deployContract(
|
|
|
|
'ConditionStoreManager',
|
2019-03-06 15:33:10 +01:00
|
|
|
deployerAddress,
|
2019-06-20 00:20:09 +02:00
|
|
|
[deployerAddress],
|
|
|
|
{
|
|
|
|
EpochLibrary: epochLibrary.options.address
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const agreementStoreManager = await TestContractHandler.deployContract(
|
|
|
|
'AgreementStoreManager',
|
2019-03-06 15:33:10 +01:00
|
|
|
deployerAddress,
|
2019-06-20 00:20:09 +02:00
|
|
|
[
|
|
|
|
deployerAddress,
|
|
|
|
conditionStoreManager.options.address,
|
|
|
|
templateStoreManager.options.address,
|
|
|
|
didRegistry.options.address
|
|
|
|
]
|
|
|
|
)
|
2019-03-06 15:33:10 +01:00
|
|
|
|
|
|
|
// Conditions
|
2020-01-17 15:35:11 +01:00
|
|
|
await TestContractHandler.deployContract(
|
2019-11-15 00:00:10 +01:00
|
|
|
'LockRewardCondition',
|
2019-06-20 00:20:09 +02:00
|
|
|
deployerAddress,
|
2019-11-15 00:00:10 +01:00
|
|
|
[
|
|
|
|
deployerAddress,
|
|
|
|
conditionStoreManager.options.address,
|
|
|
|
token.options.address
|
|
|
|
]
|
|
|
|
)
|
2020-01-17 15:35:11 +01:00
|
|
|
await TestContractHandler.deployContract(
|
2019-06-20 00:20:09 +02:00
|
|
|
'AccessSecretStoreCondition',
|
|
|
|
deployerAddress,
|
2019-11-15 00:00:10 +01:00
|
|
|
[
|
|
|
|
deployerAddress,
|
|
|
|
conditionStoreManager.options.address,
|
|
|
|
agreementStoreManager.options.address
|
|
|
|
]
|
2019-06-20 00:20:09 +02:00
|
|
|
)
|
2019-03-06 15:33:10 +01:00
|
|
|
|
|
|
|
// Conditions rewards
|
2020-01-17 15:35:11 +01:00
|
|
|
await TestContractHandler.deployContract(
|
2019-11-15 00:00:10 +01:00
|
|
|
'EscrowReward',
|
2019-06-20 00:20:09 +02:00
|
|
|
deployerAddress,
|
2019-11-15 00:00:10 +01:00
|
|
|
[
|
|
|
|
deployerAddress,
|
|
|
|
conditionStoreManager.options.address,
|
|
|
|
token.options.address
|
|
|
|
]
|
|
|
|
)
|
2019-03-06 15:33:10 +01:00
|
|
|
|
2018-11-07 14:33:56 +01:00
|
|
|
}
|
|
|
|
|
2019-03-06 15:33:10 +01:00
|
|
|
private static async deployContract(
|
|
|
|
name: string,
|
|
|
|
from: string,
|
|
|
|
args: any[] = [],
|
2019-06-20 00:20:09 +02:00
|
|
|
tokens: { [name: string]: string } = {}
|
2019-11-11 12:27:18 +01:00
|
|
|
): Promise<ContractTest> {
|
2019-03-21 02:56:58 +01:00
|
|
|
const where = this.networkId
|
2018-11-07 14:33:56 +01:00
|
|
|
|
|
|
|
// dont redeploy if there is already something loaded
|
2019-03-21 02:56:58 +01:00
|
|
|
if (TestContractHandler.hasContract(name, where)) {
|
2019-11-11 12:27:18 +01:00
|
|
|
const contract: ContractTest = await ContractHandler.getContract(name, where)
|
2019-03-21 02:56:58 +01:00
|
|
|
if (contract.testContract) {
|
2019-11-11 12:27:18 +01:00
|
|
|
return { ...contract, $initialized: true } as any
|
2019-03-21 02:56:58 +01:00
|
|
|
}
|
2018-11-07 14:33:56 +01:00
|
|
|
}
|
|
|
|
|
2019-03-21 02:56:58 +01:00
|
|
|
const web3 = Web3Provider.getWeb3(config)
|
2018-11-07 14:33:56 +01:00
|
|
|
|
2019-11-11 12:27:18 +01:00
|
|
|
let contractInstance: ContractTest
|
2018-11-07 14:33:56 +01:00
|
|
|
try {
|
2019-06-20 00:20:09 +02:00
|
|
|
Logger.log('Deploying', name)
|
2019-01-30 11:26:24 +01:00
|
|
|
const sendConfig = {
|
2018-11-07 14:33:56 +01:00
|
|
|
from,
|
|
|
|
gas: 3000000,
|
2019-11-12 11:28:18 +01:00
|
|
|
gasPrice: '10000000000'
|
2019-01-30 11:26:24 +01:00
|
|
|
}
|
2019-11-12 11:28:18 +01:00
|
|
|
// eslint-disable-next-line security/detect-non-literal-require
|
2019-01-30 11:26:24 +01:00
|
|
|
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${name}.development.json`)
|
2019-09-09 12:18:54 +02:00
|
|
|
const tempContract = new web3.eth.Contract(artifact.abi, artifact.address)
|
2019-01-30 11:26:24 +01:00
|
|
|
const isZos = !!tempContract.methods.initialize
|
2019-03-06 15:33:10 +01:00
|
|
|
|
|
|
|
Logger.debug({
|
2019-06-20 00:20:09 +02:00
|
|
|
name,
|
|
|
|
from,
|
|
|
|
isZos,
|
|
|
|
args,
|
2019-03-06 15:33:10 +01:00
|
|
|
libraries: artifact.bytecode
|
2019-06-20 00:20:09 +02:00
|
|
|
.replace(/(0x)?[a-f0-9]{8}/gi, '')
|
|
|
|
.replace(/__([^_]+)_*[0-9a-f]{2}/g, '|$1')
|
|
|
|
.split('|')
|
|
|
|
.splice(1)
|
2019-03-06 15:33:10 +01:00
|
|
|
})
|
|
|
|
|
2019-01-30 11:26:24 +01:00
|
|
|
contractInstance = await tempContract
|
|
|
|
.deploy({
|
2019-11-15 00:00:10 +01:00
|
|
|
data: TestContractHandler.replaceTokens(
|
|
|
|
artifact.bytecode.toString(),
|
|
|
|
tokens
|
|
|
|
),
|
2019-06-20 00:20:09 +02:00
|
|
|
arguments: isZos ? undefined : args
|
2019-01-30 11:26:24 +01:00
|
|
|
})
|
|
|
|
.send(sendConfig)
|
|
|
|
if (isZos) {
|
2019-09-09 12:18:54 +02:00
|
|
|
await contractInstance.methods.initialize(...args).send(sendConfig)
|
2019-01-30 11:26:24 +01:00
|
|
|
}
|
2019-03-21 02:56:58 +01:00
|
|
|
contractInstance.testContract = true
|
|
|
|
ContractHandler.setContract(name, where, contractInstance)
|
2019-11-11 12:27:18 +01:00
|
|
|
// Logger.log('Deployed', name, 'at', contractInstance.options.address)
|
2018-11-07 14:33:56 +01:00
|
|
|
} catch (err) {
|
2019-11-15 00:00:10 +01:00
|
|
|
Logger.error(
|
|
|
|
'Deployment failed for',
|
|
|
|
name,
|
|
|
|
'with args',
|
|
|
|
JSON.stringify(args, null, 2),
|
|
|
|
err.message
|
|
|
|
)
|
2018-11-07 14:33:56 +01:00
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
|
|
|
return contractInstance
|
|
|
|
}
|
2019-03-06 15:33:10 +01:00
|
|
|
|
2019-11-15 00:00:10 +01:00
|
|
|
private static replaceTokens(
|
|
|
|
bytecode: string,
|
|
|
|
tokens: { [name: string]: string }
|
|
|
|
): string {
|
2019-06-20 00:20:09 +02:00
|
|
|
return Object.entries(tokens).reduce(
|
2019-11-15 00:00:10 +01:00
|
|
|
(acc, [token, address]) =>
|
|
|
|
acc.replace(new RegExp(`_+${token}_+`, 'g'), address.substr(2)),
|
2019-06-20 00:20:09 +02:00
|
|
|
bytecode
|
|
|
|
)
|
2019-03-06 15:33:10 +01:00
|
|
|
}
|
2018-11-07 14:33:56 +01:00
|
|
|
}
|