-
Notifications
You must be signed in to change notification settings - Fork 4
/
kredits.js
135 lines (111 loc) · 3.65 KB
/
kredits.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const ethers = require('ethers');
const Preflight = require('./utils/preflight');
const deprecate = require('./utils/deprecate');
const ABIS = {
Contributor: require('./abis/Contributor.json'),
Contribution: require('./abis/Contribution.json'),
Reimbursement: require('./abis/Reimbursement.json'),
Token: require('./abis/Token.json'),
};
// const APP_CONTRACTS = [
// 'Contributor',
// 'Contribution',
// 'Token',
// 'Reimbursement',
// ];
const Addresses = require('./addresses.json');
const Contracts = require('./contracts');
const IPFS = require('./utils/ipfs');
// Helpers
function capitalize (word) {
let [first, ...rest] = word;
return `${first.toUpperCase()}${rest.join('')}`;
}
class Kredits {
constructor (provider, signer, options = {}) {
const { addresses, abis, ipfsConfig } = options;
this.provider = provider;
this.signer = signer;
this.options = options;
this.addresses = addresses || {};
this.abis = abis || ABIS;
this.ipfs = new IPFS(ipfsConfig);
this.contracts = {};
}
init (/* names */) {
// TODO implement
// const contractsToLoad = names || APP_CONTRACTS;
return this.provider.getNetwork().then(network => {
if (Object.keys(this.addresses).length === 0) {
this.addresses = Addresses[network.chainId.toString()];
}
return this;
});
}
static setup (provider, signer, ipfsConfig = null) {
deprecate('Kredits.setup() is deprecated use new Kredits().init() instead');
return new Kredits(provider, signer, { ipfsConfig: ipfsConfig }).init();
}
static for (connectionOptions, kreditsOptions) {
let { network, rpcUrl, wallet } = connectionOptions;
if (!rpcUrl && network === 'local') { rpcUrl = 'http://localhost:8545'; }
let ethProvider, signer;
if (rpcUrl) {
ethProvider = new ethers.providers.JsonRpcProvider(rpcUrl);
} else {
ethProvider = new ethers.getDefaultProvider(network);
}
if (wallet) {
signer = wallet.connect(ethProvider);
} else if (ethProvider.getSigner) {
// Only useful for reading data, not writing. The (unused) address is
// necessary because without an address, ethers.js will try to look up
// the provider's account 0, which doesn't work on our public RSK nodes.
signer = ethProvider.getSigner('0xfa77675540E550b911a6AABF3805ac17C6641ec1');
}
return new Kredits(ethProvider, signer, kreditsOptions);
}
static availableNetworks () {
return Object.keys(Addresses);
}
get Contributor () {
return this.contractFor('Contributor');
}
get Contributors () {
deprecate('Contributors is deprecated use Contributor instead');
return this.contractFor('Contributor');
}
get Operator () {
return this.Proposal;
}
get Token () {
return this.contractFor('Token');
}
get Contribution () {
return this.contractFor('Contribution');
}
get Reimbursement () {
return this.contractFor('Reimbursement');
}
// Should be private
contractFor (name) {
if (this.contracts[name]) {
return this.contracts[name];
}
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}`);
}
const signerOrProvider = this.signer || this.provider;
const contract = new ethers.Contract(address, abi, signerOrProvider);
this.contracts[name] = new Contracts[contractName](contract);
this.contracts[name].ipfs = this.ipfs;
return this.contracts[name];
}
preflightChecks () {
return new Preflight(this).check();
}
}
module.exports = Kredits;