Merge branch 'master' into fallback-provider

# Conflicts:
#	lib/kredits.js
This commit is contained in:
2018-04-21 11:18:56 +02:00
22 changed files with 215 additions and 265 deletions

View File

@@ -1 +1 @@
{"100":"0xa7fc9b1f678c41396b53904f94f50a42ff44d826"}
{"42":"0x205fe1b3dac678b594c5f0535e7d158e38591f93","100":"0xa7fc9b1f678c41396b53904f94f50a42ff44d826"}

View File

@@ -1 +1 @@
{"100":"0x95d3bd7d136bb0b7ac9988097e964236f8a9976e"}
{"42":"0x9fd66ee78a5ebe86006f12b37ff59c63f9caa15b","100":"0x95d3bd7d136bb0b7ac9988097e964236f8a9976e"}

View File

@@ -1 +1 @@
{"100":"0x513f8e80a8fbb9fa188320ecf231efcf2f6da9c5"}
{"42":"0xc270e6ea4fe303df9f1a3d4a132ac425264082e7","100":"0x513f8e80a8fbb9fa188320ecf231efcf2f6da9c5"}

View File

@@ -1 +1 @@
{"100":"0x3fc29fbe40c2d0ca78c7e81342f00226650fe2ad"}
{"42":"0xf71ccf7ab48044ef9ae0b5e6983dbd3266b78b36","100":"0x3fc29fbe40c2d0ca78c7e81342f00226650fe2ad"}

View File

@@ -34,7 +34,7 @@ class Contributor extends Base {
});
}
add(contributorAttr) {
add(contributorAttr, callOptions = {}) {
let json = ContributorSerializer.serialize(contributorAttr);
// TODO: validate against schema
@@ -42,14 +42,14 @@ class Contributor extends Base {
.add(json)
.then((ipfsHashAttr) => {
let contributor = [
contributorAttr.address,
contributorAttr.account,
ipfsHashAttr.hashDigest,
ipfsHashAttr.hashFunction,
ipfsHashAttr.hashSize,
contributorAttr.isCore,
];
return this.functions.addContributor(...contributor);
return this.functions.addContributor(...contributor, callOptions);
});
}
}

View File

@@ -1,5 +1,6 @@
module.exports = {
Contributors: require('./contributor'),
Operator: require('./operator'),
Token: require('./token')
}
Token: require('./token'),
Registry: require('./registry')
};

View File

@@ -34,7 +34,7 @@ class Operator extends Base {
});
}
addProposal(proposalAttr) {
addProposal(proposalAttr, callOptions = {}) {
let json = ContributionSerializer.serialize(proposalAttr);
// TODO: validate against schema
@@ -49,7 +49,7 @@ class Operator extends Base {
ipfsHashAttr.hashSize,
];
return this.functions.addProposal(...proposal);
return this.functions.addProposal(...proposal, callOptions);
});
}
}

View File

@@ -0,0 +1,6 @@
const Base = require('./base');
class Registry extends Base {
}
module.exports = Registry;

View File

@@ -1,17 +1,15 @@
const ethers = require('ethers');
const RSVP = require('rsvp');
const abis = {
const ABIS = {
Contributors: require('./abis/Contributors.json'),
Operator: require('./abis/Operator.json'),
Registry: require('./abis/Registry.json'),
Token: require('./abis/Token.json')
};
const addresses = {
Registry: require('./addresses/Registry.json')
};
const RegistryAddress = require('./addresses/Registry.json');
const contracts = require('./contracts');
const Contracts = require('./contracts');
const IPFS = require('./utils/ipfs')
// Helpers
@@ -21,65 +19,50 @@ function capitalize(word) {
}
class Kredits {
static get contractNames() {
return Object.keys(abis);
}
constructor(provider, signer, addresses) {
this.provider = provider;
this.signer = signer;
// Initialize our registry contract
this.addresses = addresses;
// by default we only need the registry address.
// the rest is loaded from there in the init() function
this.addresses = addresses || {Registry: RegistryAddress[this.provider.chainId.toString()]}; // chaiID must be a string
this.abis = ABIS;
this.contracts = {};
this.ipfs = new IPFS();
}
static setup(provider, signer, ipfsConfig = null) {
let ipfsAPI = new IPFS(ipfsConfig);
init(names) {
let contractsToLoad = names || Object.keys(ABIS);
let addressPromises = contractsToLoad.map((contractName) => {
return this.Registry.functions.getProxyFor(contractName).then((address) => {
this.addresses[contractName] = address;
}).catch((error) => {
throw new Error(`Failed to get address for ${contractName} from registry at ${this.Registry.contract.address}
- correct registry? does it have version entry? - ${error.message}`
);
});
});
return RSVP.all(addressPromises).then(() => { return this });
}
return ipfsAPI._ipfsAPI.id().catch((error) => {
static setup(provider, signer, ipfsConfig = null) {
console.log('Kredits.setup() is deprecated use new Kredits().init() instead');
let ipfs = new IPFS(ipfsConfig);
return ipfs._ipfsAPI.id().catch((error) => {
throw new Error(`IPFS node not available; config: ${JSON.stringify(ipfsConfig)} - ${error.message}`);
}).then(() => {
let registryContract = this.initRegistryContract(provider);
let addresses = Kredits.contractNames.reduce((mem, name) => {
let contractName = capitalize(name);
mem[contractName] = registryContract.functions.getProxyFor(contractName).catch((error) => {
throw new Error(`Failed to get address for ${contractName} from registry at ${registryContract.address}
- correct registry? does it have version entry? - ${error.message}`
);
});
return mem;
}, {});
return RSVP.hash(addresses)
.then((addresses) => {
let kredits = new Kredits(provider, signer, addresses);
kredits.ipfs = ipfsAPI;
return kredits;
});
return new Kredits(provider, signer).init().then((kredits) => {
kredits.ipfs = ipfs;
return kredits;
});
});
}
static initRegistryContract(provider) {
let address = addresses['Registry'][provider.chainId];
if (!address) {
throw new Error(`Registry address not found; invalid network?
requested network: ${provider.chainId}
supported networks: ${Object.keys(addresses['Registry'])}
`);
}
provider.getCode(address).then((code) => {
// not sure if we always get the same return value of the code is not available
// that's why checking if it is < 5 long
if (code === '0x00' || code.length < 5) {
throw new Error(`Registry not found at ${address} on network ${provider.chainId}`);
}
});
let abi = abis['Registry'];
return new ethers.Contract(address, abi, provider);
get Registry() {
return this.contractFor('registry');
}
get Contributor() {
@@ -101,14 +84,16 @@ class Kredits {
return this.contracts[name];
}
let contractName = capitalize(name);
let address = this.addresses[contractName];
if (!address || !abis[contractName]) {
const contractName = capitalize(name);
const address = this.addresses[contractName];
const abi = this.abis[contractName];
if (!address || !abi) {
throw new Error(`Address or ABI not found for ${contractName}`);
}
let signerOrProvider = this.signer || this.provider;
let contract = new ethers.Contract(address, abis[contractName], signerOrProvider);
this.contracts[name] = new contracts[contractName](contract);
let contract = new ethers.Contract(address, abi, signerOrProvider);
this.contracts[name] = new Contracts[contractName](contract);
this.contracts[name].ipfs = this.ipfs;
return this.contracts[name];

View File

@@ -41,6 +41,7 @@ class Contribution {
kind,
description,
url,
details
} = deserialized;
let data = {
@@ -51,7 +52,7 @@ class Contribution {
},
kind,
description,
"details": {}
"details": details || {}
};
if (url) {

View File

@@ -35,6 +35,7 @@ class Contributor {
name,
kind,
url,
accounts,
github_uid,
github_username,
wiki_username,