2018-10-05 12:34:31 +02:00
|
|
|
import Contract from "web3-eth-contract"
|
2018-11-06 16:13:12 +01:00
|
|
|
import ServiceAgreementTemplate from "../ocean/ServiceAgreements/ServiceAgreementTemplate"
|
|
|
|
import Access from "../ocean/ServiceAgreements/Templates/Access"
|
|
|
|
import FitchainCompute from "../ocean/ServiceAgreements/Templates/FitchainCompute"
|
2018-10-02 10:06:26 +02:00
|
|
|
import Logger from "../utils/Logger"
|
2018-10-16 14:56:18 +02:00
|
|
|
import Keeper from "./Keeper"
|
2018-10-16 15:02:44 +02:00
|
|
|
import Web3Provider from "./Web3Provider"
|
2018-10-01 18:10:26 +02:00
|
|
|
|
|
|
|
export default class ContractHandler {
|
|
|
|
|
2018-10-16 14:56:18 +02:00
|
|
|
public static async get(what: string): Contract {
|
2018-10-17 18:24:01 +02:00
|
|
|
const where = (await (await Keeper.getInstance()).getNetworkName()).toLowerCase()
|
|
|
|
try {
|
2018-11-01 08:31:21 +01:00
|
|
|
return ContractHandler.contracts.get(what) || await ContractHandler.load(what, where)
|
2018-10-17 18:24:01 +02:00
|
|
|
} catch (err) {
|
|
|
|
Logger.error("Failed to load", what, "from", where, err)
|
|
|
|
throw err
|
|
|
|
}
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 16:13:12 +01:00
|
|
|
public static async prepareContracts() {
|
2018-10-01 18:10:26 +02:00
|
|
|
|
2018-10-16 14:56:18 +02:00
|
|
|
const web3 = Web3Provider.getWeb3()
|
2018-10-02 10:06:26 +02:00
|
|
|
const deployerAddress = (await web3.eth.getAccounts())[0]
|
2018-10-01 18:10:26 +02:00
|
|
|
|
2018-11-06 16:13:12 +01:00
|
|
|
// deploy contracts
|
|
|
|
await ContractHandler.deployContracts(deployerAddress)
|
|
|
|
|
|
|
|
// register templates
|
|
|
|
await new ServiceAgreementTemplate(new Access()).register(deployerAddress)
|
|
|
|
await new ServiceAgreementTemplate(new FitchainCompute()).register(deployerAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
private static contracts: Map<string, Contract> = new Map<string, Contract>()
|
|
|
|
|
|
|
|
private static async deployContracts(deployerAddress: string) {
|
|
|
|
Logger.log("Trying to deploy contracts")
|
|
|
|
|
2018-10-01 18:10:26 +02:00
|
|
|
// deploy libs
|
2018-10-18 12:02:40 +02:00
|
|
|
/* not part of trilobite
|
2018-10-18 09:19:10 +02:00
|
|
|
const dll = await ContractHandler.deployContract("DLL", deployerAddress)
|
|
|
|
const attributeStore = await ContractHandler.deployContract("AttributeStore", deployerAddress)
|
2018-10-18 12:02:40 +02:00
|
|
|
*/
|
2018-10-01 18:10:26 +02:00
|
|
|
// deploy contracts
|
2018-10-18 09:19:10 +02:00
|
|
|
const token = await ContractHandler.deployContract("OceanToken", deployerAddress)
|
2018-10-18 12:02:40 +02:00
|
|
|
/* not part of trilobite
|
2018-10-18 09:19:10 +02:00
|
|
|
const plcrVoting = await ContractHandler.deployContract("PLCRVoting", deployerAddress, {
|
2018-10-01 18:10:26 +02:00
|
|
|
args: [token.options.address],
|
|
|
|
tokens: [
|
|
|
|
{
|
|
|
|
name: "DLL", address: dll.options.address,
|
|
|
|
}, {
|
|
|
|
name: "AttributeStore", address: attributeStore.options.address,
|
|
|
|
},
|
|
|
|
],
|
2018-10-02 10:06:26 +02:00
|
|
|
})
|
2018-10-18 12:02:40 +02:00
|
|
|
/* not part of trilobite
|
2018-10-18 09:19:10 +02:00
|
|
|
const registry = await ContractHandler.deployContract("OceanRegistry", deployerAddress, {
|
2018-10-01 18:10:26 +02:00
|
|
|
args: [token.options.address, plcrVoting.options.address],
|
2018-10-02 10:06:26 +02:00
|
|
|
})
|
2018-10-18 12:02:40 +02:00
|
|
|
*/
|
2018-10-18 09:19:10 +02:00
|
|
|
const market = await ContractHandler.deployContract("OceanMarket", deployerAddress, {
|
2018-10-18 12:02:40 +02:00
|
|
|
args: [token.options.address],
|
2018-10-02 10:06:26 +02:00
|
|
|
})
|
2018-10-29 17:28:40 +01:00
|
|
|
|
|
|
|
const sa = await ContractHandler.deployContract("ServiceAgreement", deployerAddress, {
|
|
|
|
args: [],
|
|
|
|
})
|
2018-11-01 08:31:21 +01:00
|
|
|
|
2018-10-29 17:28:40 +01:00
|
|
|
await ContractHandler.deployContract("AccessConditions", deployerAddress, {
|
|
|
|
args: [sa.options.address],
|
|
|
|
})
|
|
|
|
|
|
|
|
await ContractHandler.deployContract("PaymentConditions", deployerAddress, {
|
|
|
|
args: [sa.options.address, token.options.address],
|
|
|
|
})
|
|
|
|
|
2018-10-29 10:53:22 +01:00
|
|
|
await ContractHandler.deployContract("DIDRegistry", deployerAddress, {})
|
2018-10-18 12:02:40 +02:00
|
|
|
/* not part of trilobite
|
2018-10-18 09:19:10 +02:00
|
|
|
const dispute = await ContractHandler.deployContract("OceanDispute", deployerAddress, {
|
2018-10-01 18:10:26 +02:00
|
|
|
args: [market.options.address, registry.options.address, plcrVoting.options.address],
|
2018-10-02 10:06:26 +02:00
|
|
|
})
|
2018-10-18 12:02:40 +02:00
|
|
|
*/
|
2018-10-18 09:19:10 +02:00
|
|
|
await ContractHandler.deployContract("OceanAuth", deployerAddress, {
|
2018-10-18 12:02:40 +02:00
|
|
|
args: [market.options.address],
|
2018-10-02 10:06:26 +02:00
|
|
|
})
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 18:24:01 +02:00
|
|
|
private static async load(what: string, where: string): Promise<Contract> {
|
2018-10-16 14:56:18 +02:00
|
|
|
const web3 = Web3Provider.getWeb3()
|
2018-10-17 18:24:01 +02:00
|
|
|
// Logger.log("Loading", what, "from", where)
|
|
|
|
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}`)
|
|
|
|
// Logger.log('Loaded artifact', artifact)
|
|
|
|
const code = await web3.eth.getCode(artifact.address)
|
|
|
|
if (code === "0x0") {
|
|
|
|
// no code in the blockchain dude
|
|
|
|
throw new Error(`No code deployed at address ${artifact.address}, sorry.`)
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
2018-10-17 18:24:01 +02:00
|
|
|
// Logger.log("Getting instance of", what, "from", where, "at", artifact.address)
|
|
|
|
const contract = new web3.eth.Contract(artifact.abi, artifact.address)
|
|
|
|
Logger.log("Loaded", what, "from", where)
|
2018-11-01 08:31:21 +01:00
|
|
|
ContractHandler.contracts.set(what, contract)
|
|
|
|
return ContractHandler.contracts.get(what)
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 12:02:40 +02:00
|
|
|
// todo: reactivate for tethys
|
2018-10-01 18:10:26 +02:00
|
|
|
private static replaceTokens(bytecode: string, tokens: any[]) {
|
|
|
|
|
|
|
|
for (const token of tokens) {
|
|
|
|
|
|
|
|
bytecode = bytecode.replace(
|
|
|
|
new RegExp(`_+${token.name}_+`, "g"),
|
2018-10-02 10:06:26 +02:00
|
|
|
token.address.replace("0x", ""))
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
2018-10-10 11:02:00 +02:00
|
|
|
// Logger.log(bytecode)
|
2018-10-01 18:10:26 +02:00
|
|
|
|
2018-10-02 10:06:26 +02:00
|
|
|
return bytecode.toString()
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 09:19:10 +02:00
|
|
|
private static async deployContract(name: string, from: string, params?): Promise<Contract> {
|
2018-10-01 18:10:26 +02:00
|
|
|
|
2018-10-05 12:34:31 +02:00
|
|
|
// dont redeploy if there is already something loaded
|
2018-11-01 08:31:21 +01:00
|
|
|
if (ContractHandler.contracts.has(name)) {
|
|
|
|
return ContractHandler.contracts.get(name)
|
2018-10-05 12:34:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 09:19:10 +02:00
|
|
|
const web3 = Web3Provider.getWeb3()
|
|
|
|
|
2018-10-05 12:34:31 +02:00
|
|
|
let contractInstance: Contract
|
2018-10-01 18:10:26 +02:00
|
|
|
try {
|
2018-10-02 10:06:26 +02:00
|
|
|
Logger.log("Deploying", name)
|
2018-10-01 18:10:26 +02:00
|
|
|
|
2018-10-02 10:06:26 +02:00
|
|
|
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${name}.development.json`)
|
|
|
|
const tempContract = new web3.eth.Contract(artifact.abi, artifact.address)
|
2018-10-01 18:10:26 +02:00
|
|
|
contractInstance = await tempContract.deploy({
|
|
|
|
data: params && params.tokens ?
|
|
|
|
ContractHandler.replaceTokens(artifact.bytecode.toString(), params.tokens) :
|
|
|
|
artifact.bytecode,
|
|
|
|
arguments: params && params.args ? params.args : null,
|
|
|
|
}).send({
|
|
|
|
from,
|
|
|
|
gas: 3000000,
|
|
|
|
gasPrice: 10000000000,
|
2018-10-02 10:06:26 +02:00
|
|
|
})
|
2018-11-01 08:31:21 +01:00
|
|
|
ContractHandler.contracts.set(name, contractInstance)
|
2018-10-01 18:10:26 +02:00
|
|
|
// Logger.log("Deployed", name, "at", contractInstance.options.address);
|
|
|
|
} catch (err) {
|
2018-10-02 10:06:26 +02:00
|
|
|
Logger.error("Deployment failed for", name, "with params", JSON.stringify(params, null, 2), err.message)
|
|
|
|
throw err
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
2018-10-02 10:06:26 +02:00
|
|
|
return contractInstance
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
}
|