Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Member app endpoints #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/addresses/CoinsenceKit.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"35475823": "0x3dc13327c065cb2e79656ef45b6af0bed76132f8",
"38301683": "0x3dfb393eca923681e803dd4eafa09c17de40fef1",
"40722062": "0x650121ee1d4c3ea8faeba138f88d674d07fca918",
"49857146": "0x87fe1061b0f97a6ea260781647b7704e0f4ea7a2",
"89511784": "0x8b9a1ce7d34aa289652750a2539b99e7bff95cf1",
"91724461": "0x6fe2039c2dc1b6409f5522c8b7fa686570388403"
}
1 change: 1 addition & 0 deletions lib/http/apps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module.exports = {
wallet: require('./wallet'),
space: require('./space'),
coin: require('./coin'),
member: require('./member'),
generic: require('./generic')
}
44 changes: 44 additions & 0 deletions lib/http/apps/member.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const express = require('express');
const app = express();

app.post(`/addMember`, (req, res) => {
let address = req.body.address;
let ipfsHash = req.body.ipfsHash;

res.locals.memberApp.contract.functions.addMember(address, ipfsHash, { gasLimit: 5000000 }).then(transaction => {
const txHash = transaction.hash;
res.status(201).json(txHash);
}).catch(e => {
console.log(e);
res.sendStatus(500);
})
});

app.post(`/updateMemberAccount`, (req, res) => {
let id = req.body.id;
let oldAccount = req.body.oldAccount;
let newAccount = req.body.newAccount;

res.locals.memberApp.contract.functions.updateMemberAccount(id, oldAccount, newAccount, { gasLimit: 5000000 }).then(transaction => {
const txHash = transaction.hash;
res.status(201).json(txHash);
}).catch(e => {
console.log(e);
res.sendStatus(500);
})
});

app.post(`/updateMemberIpfsHash`, (req, res) => {
let id = req.body.id;
let ipfsHash = req.body.ipfsHash;

res.locals.memberApp.contract.functions.updateMemberIpfsHash(id, ipfsHash, { gasLimit: 5000000 }).then(transaction => {
const txHash = transaction.hash;
res.status(201).json(txHash);
}).catch(e => {
console.log(e);
res.sendStatus(500);
})
});

module.exports = app;
11 changes: 10 additions & 1 deletion lib/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const router = express.Router();
const walletApp = require('./apps/wallet');
const daoApp = require('./apps/dao');
const spaceApp = require('./apps/space');
const coinApp = require('./apps/coin');;
const coinApp = require('./apps/coin');
const memberApp = require('./apps/member');
const genericApp = require('./apps/generic');

let ethProvider;
Expand Down Expand Up @@ -60,6 +61,14 @@ app.use(
],
coinApp
);
app.use(
'/member',
[
middlewares.requireMemberApp,
middlewares.loadMemberApp({ password, ethProvider })
],
memberApp
);
app.use(
'/',
[
Expand Down
4 changes: 3 additions & 1 deletion lib/http/middlewares/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ module.exports = {
loadCoinsenceKit: require('./load-coinsence-kit'),
requireDao: require('./require-dao'),
setEthProvider: require('./set-eth-provider'),
monitoring: require('./monitoring')
monitoring: require('./monitoring'),
loadMemberApp: require('./load-member-app'),
requireMemberApp: require('./require-member-app')
}
19 changes: 19 additions & 0 deletions lib/http/middlewares/load-member-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const Member = require('../../member');
const loadWallet = require('../utils/load-wallet');

module.exports = function (options = {}) {
let { password, ethProvider } = options;

return function (req, res, next) {
loadWallet(res.locals.accountId, password).then(wallet => {
const signer = wallet.connect(ethProvider);
new Member(ethProvider, signer, {address: res.locals.memberAppAddress}).init().then(memberApp => {
res.locals.memberApp = memberApp;
next();
}).catch(e => {
console.log(e);
res.sendStatus(500);
});
});
}
}
11 changes: 11 additions & 0 deletions lib/http/middlewares/require-member-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function (req, res, next) {
const memberAppAddress = req.body.memberApp || req.query.memberApp;
if (!memberAppAddress) {
res.status(400).json({error: 'member app is missing'});
} else {
res.locals.memberAppAddress = memberAppAddress;
next();
}
}


44 changes: 44 additions & 0 deletions lib/member.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const ethers = require('ethers');

const ABI = require('./abis/Member.json');

const KitAddress = require('./addresses/CoinsenceKit.json');

const IPFS = require('./utils/ipfs');

class Member {

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;
this.ipfs = new IPFS(ipfsConfig);
}

static withRpcProvider(url, options = {}) {
const provider = new ethers.providers.JsonRpcProvider(url);
const signer = options.signer || provider.getSigner();
return new Member(provider, signer, options);
}

async init() {
return this.provider.getNetwork().then((network) => {
this.address = this.address;

this.contract = new ethers.Contract(
this.address,
this.abi,
(this.signer || this.provider)
);

return this;
});
}

}

module.exports = Member;