2018-11-07 14:33:56 +01:00
|
|
|
import Contract from "web3-eth-contract"
|
|
|
|
import ContractHandler from "../../src/keeper/ContractHandler"
|
|
|
|
import Web3Provider from "../../src/keeper/Web3Provider"
|
|
|
|
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate"
|
|
|
|
import Access from "../../src/ocean/ServiceAgreements/Templates/Access"
|
|
|
|
import FitchainCompute from "../../src/ocean/ServiceAgreements/Templates/FitchainCompute"
|
|
|
|
import Logger from "../../src/utils/Logger"
|
|
|
|
|
|
|
|
export default class TestContractHandler extends ContractHandler {
|
|
|
|
|
|
|
|
public static async prepareContracts() {
|
|
|
|
|
|
|
|
const web3 = Web3Provider.getWeb3()
|
|
|
|
const deployerAddress = (await web3.eth.getAccounts())[0]
|
|
|
|
|
|
|
|
// deploy contracts
|
|
|
|
await TestContractHandler.deployContracts(deployerAddress)
|
|
|
|
|
|
|
|
// register templates
|
2018-11-21 12:31:08 +01:00
|
|
|
Logger.log(`Registering Access Template from ${deployerAddress}`)
|
2018-11-07 14:33:56 +01:00
|
|
|
await new ServiceAgreementTemplate(new Access()).register(deployerAddress)
|
2018-11-21 12:31:08 +01:00
|
|
|
Logger.log(`Registering FitchainCompute Template from ${deployerAddress}`)
|
2018-11-07 14:33:56 +01:00
|
|
|
await new ServiceAgreementTemplate(new FitchainCompute()).register(deployerAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async deployContracts(deployerAddress: string) {
|
|
|
|
Logger.log("Trying to deploy contracts")
|
|
|
|
|
2019-01-30 11:26:24 +01:00
|
|
|
const token = await TestContractHandler.deployContract("OceanToken", deployerAddress, [deployerAddress])
|
|
|
|
|
|
|
|
const dispenser = await TestContractHandler.deployContract("Dispenser", deployerAddress, [token.options.address, deployerAddress])
|
|
|
|
|
|
|
|
// Add dispenser as Token minter
|
|
|
|
await token.methods.addMinter(dispenser.options.address)
|
|
|
|
.send({from: deployerAddress})
|
|
|
|
|
|
|
|
const sa = await TestContractHandler.deployContract("ServiceExecutionAgreement", deployerAddress)
|
|
|
|
|
|
|
|
await TestContractHandler.deployContract("AccessConditions", deployerAddress, [sa.options.address])
|
|
|
|
|
|
|
|
await TestContractHandler.deployContract("PaymentConditions", deployerAddress, [sa.options.address, token.options.address])
|
|
|
|
|
|
|
|
await TestContractHandler.deployContract("DIDRegistry", deployerAddress, [deployerAddress])
|
2018-11-07 14:33:56 +01:00
|
|
|
}
|
|
|
|
|
2019-01-30 11:26:24 +01:00
|
|
|
private static async deployContract(name: string, from: string, args: any[] = []): Promise<Contract> {
|
2018-11-07 14:33:56 +01:00
|
|
|
|
|
|
|
// dont redeploy if there is already something loaded
|
|
|
|
if (ContractHandler.has(name)) {
|
2018-11-09 11:16:22 +01:00
|
|
|
return await ContractHandler.get(name)
|
2018-11-07 14:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const web3 = Web3Provider.getWeb3()
|
|
|
|
|
|
|
|
let contractInstance: Contract
|
|
|
|
try {
|
|
|
|
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,
|
|
|
|
gasPrice: 10000000000,
|
2019-01-30 11:26:24 +01:00
|
|
|
}
|
|
|
|
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${name}.development.json`)
|
|
|
|
const tempContract = new web3.eth.Contract(artifact.abi, artifact.address)
|
|
|
|
const isZos = !!tempContract.methods.initialize
|
|
|
|
contractInstance = await tempContract
|
|
|
|
.deploy({
|
|
|
|
data: artifact.bytecode,
|
|
|
|
arguments: isZos ? undefined : args,
|
|
|
|
})
|
|
|
|
.send(sendConfig)
|
|
|
|
if (isZos) {
|
|
|
|
await contractInstance.methods.initialize(...args).send(sendConfig)
|
|
|
|
}
|
2018-11-07 14:33:56 +01:00
|
|
|
TestContractHandler.set(name, contractInstance)
|
|
|
|
// Logger.log("Deployed", name, "at", contractInstance.options.address);
|
|
|
|
} catch (err) {
|
2019-01-30 11:26:24 +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
|
|
|
|
}
|
|
|
|
}
|