1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/src/keeper/ContractHandler.ts

143 lines
5.4 KiB
TypeScript
Raw Normal View History

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"
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-05 12:34:31 +02:00
const contracts: Map<string, Contract> = new Map<string, Contract>()
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 {
return contracts.get(what) || await ContractHandler.load(what, where)
} catch (err) {
Logger.error("Failed to load", what, "from", where, err)
throw err
}
}
2018-10-16 14:56:18 +02:00
public static async deployContracts() {
2018-10-10 11:02:00 +02:00
Logger.log("Trying to deploy contracts")
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]
// 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
*/
// 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, {
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, {
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: [],
})
await ContractHandler.deployContract("AccessConditions", deployerAddress, {
args: [sa.options.address],
})
await ContractHandler.deployContract("PaymentConditions", deployerAddress, {
args: [sa.options.address, token.options.address],
})
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, {
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-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-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)
contracts.set(what, contract)
return contracts.get(what)
}
2018-10-18 12:02:40 +02:00
// todo: reactivate for tethys
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-10 11:02:00 +02:00
// Logger.log(bytecode)
2018-10-02 10:06:26 +02:00
return bytecode.toString()
}
2018-10-18 09:19:10 +02:00
private static async deployContract(name: string, from: string, 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)
}
2018-10-18 09:19:10 +02:00
const web3 = Web3Provider.getWeb3()
2018-10-05 12:34:31 +02:00
let contractInstance: Contract
try {
2018-10-02 10:06:26 +02:00
Logger.log("Deploying", name)
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)
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)
// 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-02 10:06:26 +02:00
return contractInstance
}
}