forked from poanetwork/poa-poba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoBAContract.js
68 lines (60 loc) · 1.92 KB
/
PoBAContract.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
import contract from 'truffle-contract'
import pobaArtifact from './artifacts/PoBA.json'
// eslint-disable-line import/no-unresolved
const PoBA = contract(pobaArtifact)
class PoBAContract {
constructor(provider) {
this.contract = null
PoBA.setProvider(provider)
this.init = this.init.bind(this)
this.getVerifiedBankAccounts = this.getVerifiedBankAccounts.bind(this)
this.registerBankAccount = this.registerBankAccount.bind(this)
this.unregisterBankAccount = this.unregisterBankAccount.bind(this)
}
async init() {
this.contract = await PoBA.deployed()
return this
}
async getVerifiedBankAccounts(walletAddress) {
const promises = []
try {
const accountsLengthResult = await this.contract.accountsLength(walletAddress)
const accountsLength = accountsLengthResult.c[0]
for (let index = 0; index < accountsLength; index++) {
promises.push(this.contract.getBankAccounts(walletAddress, index))
}
} catch (e) {
console.error('Error getting verified bank accounts', e)
}
return Promise.all(promises)
}
async registerBankAccount(args, walletAddress) {
return this.contract.register(
args.bankAccount.account,
args.bankAccount.institution,
args.identityNames,
args.v,
args.r,
args.s,
{ from: walletAddress }
)
}
async unregisterBankAccount(args, walletAddress) {
// Default estimation of gas is too low, multiply it by 2
const gasEstimate = await this.contract.unregisterBankAccount.estimateGas(
args.account,
args.bankName,
args.identityNames,
{ from: walletAddress }
)
return this.contract.unregisterBankAccount(args.account, args.bankName, args.identityNames, {
from: walletAddress,
gas: gasEstimate * 2
})
}
}
const getInstance = async provider => {
const instance = new PoBAContract(provider)
return instance.init()
}
export default getInstance