2018-10-05 12:34:31 +02:00
|
|
|
import Contract from "web3-eth-contract"
|
2018-10-02 10:06:26 +02:00
|
|
|
import Logger from "../utils/Logger"
|
|
|
|
import Web3Helper from "./Web3Helper"
|
2018-10-01 18:10:26 +02:00
|
|
|
|
2018-10-05 12:34:31 +02:00
|
|
|
const contracts: Map<string, Contract> = new Map<string, Contract>()
|
2018-10-01 18:10:26 +02:00
|
|
|
|
|
|
|
export default class ContractHandler {
|
|
|
|
|
2018-10-05 12:34:31 +02:00
|
|
|
public static async get(what: string, web3Helper: Web3Helper): Contract {
|
2018-10-02 10:06:26 +02:00
|
|
|
return contracts.get(what) || await ContractHandler.load(what, web3Helper)
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static async deployContracts(web3Helper: Web3Helper) {
|
2018-10-02 10:06:26 +02:00
|
|
|
Logger.log("Deploying contracts")
|
2018-10-01 18:10:26 +02:00
|
|
|
|
2018-10-02 10:06:26 +02:00
|
|
|
const web3 = web3Helper.getWeb3()
|
2018-10-01 18:10:26 +02:00
|
|
|
|
2018-10-02 10:06:26 +02:00
|
|
|
const deployerAddress = (await web3.eth.getAccounts())[0]
|
2018-10-01 18:10:26 +02:00
|
|
|
|
|
|
|
// deploy libs
|
2018-10-02 10:06:26 +02:00
|
|
|
const dll = await ContractHandler.deployContract(web3, "DLL", deployerAddress)
|
|
|
|
const attributeStore = await ContractHandler.deployContract(web3, "AttributeStore", deployerAddress)
|
2018-10-01 18:10:26 +02:00
|
|
|
|
|
|
|
// deploy contracts
|
2018-10-02 10:06:26 +02:00
|
|
|
const token = await ContractHandler.deployContract(web3, "OceanToken", deployerAddress)
|
2018-10-01 18:10:26 +02:00
|
|
|
const plcrVoting = await ContractHandler.deployContract(web3, "PLCRVoting", deployerAddress, {
|
|
|
|
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-01 18:10:26 +02:00
|
|
|
const registry = await ContractHandler.deployContract(web3, "OceanRegistry", deployerAddress, {
|
|
|
|
args: [token.options.address, plcrVoting.options.address],
|
2018-10-02 10:06:26 +02:00
|
|
|
})
|
2018-10-01 18:10:26 +02:00
|
|
|
const market = await ContractHandler.deployContract(web3, "OceanMarket", deployerAddress, {
|
|
|
|
args: [token.options.address, registry.options.address],
|
2018-10-02 10:06:26 +02:00
|
|
|
})
|
2018-10-01 18:10:26 +02:00
|
|
|
const dispute = await ContractHandler.deployContract(web3, "OceanDispute", deployerAddress, {
|
|
|
|
args: [market.options.address, registry.options.address, plcrVoting.options.address],
|
2018-10-02 10:06:26 +02:00
|
|
|
})
|
2018-10-02 10:37:02 +02:00
|
|
|
await ContractHandler.deployContract(web3, "OceanAuth", deployerAddress, {
|
2018-10-01 18:10:26 +02:00
|
|
|
args: [market.options.address, dispute.options.address],
|
2018-10-02 10:06:26 +02:00
|
|
|
})
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 12:34:31 +02:00
|
|
|
private static async load(what: string, web3Helper: Web3Helper): Promise<Contract> {
|
2018-10-02 10:06:26 +02:00
|
|
|
const where = (await web3Helper.getNetworkName()).toLowerCase()
|
|
|
|
Logger.log("Loading", what, "from", where)
|
2018-10-01 18:10:26 +02:00
|
|
|
try {
|
2018-10-02 10:06:26 +02:00
|
|
|
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}`)
|
2018-10-01 18:10:26 +02:00
|
|
|
// Logger.log('Loaded artifact', artifact)
|
2018-10-02 10:06:26 +02:00
|
|
|
Logger.log("Getting instance of", what, "from", where, "at", artifact.address)
|
|
|
|
const web3 = web3Helper.getWeb3()
|
|
|
|
const contract = new web3.eth.Contract(artifact.abi, artifact.address)
|
|
|
|
Logger.log("Loaded", what, "from", where)
|
|
|
|
contracts.set(what, contract)
|
|
|
|
return contracts.get(what)
|
2018-10-01 18:10:26 +02:00
|
|
|
} catch (err) {
|
2018-10-02 10:06:26 +02:00
|
|
|
Logger.error("Failed to load", what, "from", where, err)
|
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
|
|
|
}
|
|
|
|
// Logger.log(bytecode);
|
|
|
|
|
2018-10-02 10:06:26 +02:00
|
|
|
return bytecode.toString()
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static async deployContract(web3, name, from, params?): Promise<Contract> {
|
|
|
|
|
2018-10-05 12:34:31 +02:00
|
|
|
// dont redeploy if there is already something loaded
|
|
|
|
if (contracts.has(name)) {
|
|
|
|
return contracts.get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|