Hello wrapper library

This commit is contained in:
2018-04-18 18:51:27 +02:00
parent 38a25caaa4
commit d5e68e1639
20 changed files with 694 additions and 244 deletions

27
lib/contracts/base.js Normal file
View File

@@ -0,0 +1,27 @@
class Base {
constructor(contract) {
this.contract = contract;
}
get functions() {
return this.contract.functions;
}
get ipfs() {
if (!this._ipfsAPI) { throw new Error('IPFS API not configured; please set an ipfs instance'); }
return this._ipfsAPI;
}
set ipfs(ipfsAPI) {
this._ipfsAPI = ipfsAPI;
}
on(type, callback) {
let eventMethod = `on${type.toLowerCase()}`;
// Don't use this.contract.events here. Seems to be a bug in ethers.js
this.contract[eventMethod] = callback;
return this;
}
}
module.exports = Base;

View File

@@ -0,0 +1,58 @@
const ethers = require('ethers');
const RSVP = require('rsvp');
const ContributorSerializer = require('../serializers/contributor');
const Base = require('./base');
class Contributor extends Base {
all() {
return this.functions.contributorsCount()
.then((count) => {
count = count.toNumber();
let contributors = [];
for (let id = 1; id <= count; id++) {
contributors.push(this.getById(id));
}
return RSVP.all(contributors);
});
}
getById(id) {
id = ethers.utils.bigNumberify(id);
return this.functions.getContributorById(id)
.then((data) => {
// TODO: remove when naming updated on the contract
data.hashDigest = data.ipfsHash;
return data;
})
// Fetch IPFS data if available
.then((data) => {
return this.ipfs.catAndMerge(data, ContributorSerializer.deserialize);
});
}
add(contributorAttr) {
let json = ContributorSerializer.serialize(contributorAttr);
// TODO: validate against schema
return this.ipfs
.add(json)
.then((ipfsHashAttr) => {
let contributor = [
contributorAttr.address,
ipfsHashAttr.hashDigest,
ipfsHashAttr.hashFunction,
ipfsHashAttr.hashSize,
contributorAttr.isCore,
];
console.log('[kredits] addContributor', ...contributor);
return this.functions.addContributor(...contributor);
});
}
}
module.exports = Contributor;

5
lib/contracts/index.js Normal file
View File

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

58
lib/contracts/operator.js Normal file
View File

@@ -0,0 +1,58 @@
const ethers = require('ethers');
const RSVP = require('rsvp');
const ContributionSerializer = require('../serializers/contribution');
const Base = require('./base');
class Operator extends Base {
all() {
return this.functions.proposalsCount()
.then((count) => {
count = count.toNumber();
let proposals = [];
for (let id = 1; id <= count; id++) {
proposals.push(this.getById(id));
}
return RSVP.all(proposals);
});
}
getById(id) {
id = ethers.utils.bigNumberify(id);
return this.functions.getProposal(id)
.then((data) => {
// TODO: remove when naming updated on the contract
data.hashDigest = data.ipfsHash;
return data;
})
// Fetch IPFS data if available
.then((data) => {
return this.ipfs.catAndMerge(data, ContributionSerializer.deserialize);
});
}
addProposal(proposalAttr) {
let json = ContributionSerializer.serialize(proposalAttr);
// TODO: validate against schema
return this.ipfs
.add(json)
.then((ipfsHashAttr) => {
let proposal = [
proposalAttr.contributorId,
proposalAttr.amount,
ipfsHashAttr.hashDigest,
ipfsHashAttr.hashFunction,
ipfsHashAttr.hashSize,
];
console.log('[kredits] addProposal', ...proposal);
return this.functions.addProposal(...proposal);
});
}
}
module.exports = Operator;

7
lib/contracts/token.js Normal file
View File

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