contracts/lib/kreditskit.js
Michael Bumann 27a746261c Add JS wrapper for kredits kit
This makes it easier to deploy new DAOs and the deploy script is less
dependent on truffle
2019-05-17 15:54:52 +02:00

50 lines
1.3 KiB
JavaScript

const ethers = require('ethers');
const ABI = require('./abis/KreditsKit.json');
const Addresses = require('./addresses/KreditsKit.json');
class KreditsKit {
constructor(provider, signer, options = {}) {
let { address, abi, ipfsConfig } = options;
this.provider = provider;
this.signer = signer;
this.options = options;
this.address = address
this.abi = abi || ABI;
}
init() {
return this.provider.getNetwork().then((network) => {
this.address = this.address || Addresses[network.chainId.toString()];
this.contract = new ethers.Contract(
this.address,
this.abi,
(this.signer || this.provider)
);
return this;
});
}
appIdFor(contractName) {
// see appIds in KreditsKit.sol for more details
const knownContracts = ['Contribution', 'Contributor', 'Proposal', 'Token'];
return this.contract.functions.appIds(knownContracts.indexOf(contractName));
}
newDAO(options = {}) {
return this.contract.functions.newInstance(options).then(transaction => {
return transaction.wait().then(result => {
const deployEvent = result.events.find(e => e.event === 'DeployInstance');
return {
daoAddress: deployEvent.args.dao,
transactionHash: transaction.hash
}
});
});
}
}
module.exports = KreditsKit;