This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.js
88 lines (74 loc) · 2.31 KB
/
account.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
export class Account{
constructor(address, api){
this.api = api
this.address = address
}
async build() {
return Promise.all([
this.getPendingRewardBalance(this.address, "uatom"),
this.getStakedBalance(this.address, "uatom"),
this.getAvailableBalance(this.address, "uatom"),
this.api.staking.getAllUsingValidators(this.address)
]).then((data) => {
this.pendingStakingReward = parseFloat(data[0])
this.staked = parseFloat(data[1])
this.available = parseFloat(data[2])
this.totalBalance = this.pendingStakingReward + this.staked + this.available
this.lastUpdate = Date.now()
this.validators = data[3].validators
})
}
/*
getpendingStakingReward(){
}
getStaked(){
}
getAvailable(){
}
getTotalBalance(){
}
getAddress(){
}
getLastUpdate(){
}
*/
async refresh(){
return Promise.all([
this.getPendingRewardBalance(this.address, "uatom"),
this.getStakedBalance(this.address, "uatom"),
this.getAvailableBalance(this.address, "uatom")
]).then((data) => {
this.pendingStakingReward = data[0]
this.staked = data[1]
this.available = data[2]
this.totalBalance = this.pendingStakingReward + this.staked + this.available
this.lastUpdate = Date.now()
})
}
// API CALLS
async getAvailableBalance(address, denom){
return await this.api.bank.getBalance(address, denom)
}
async getPendingRewardBalance(address, denom){
return await this.api.staking.getAllPendingStakingRewards(address)
.then((data) => {
for (const totalBalance of data.total) {
if(totalBalance.denom === denom){
return totalBalance.amount
}
}
return 0
})
}
async getStakedBalance(address, denom){
return await this.api.staking.getAllDelegations(address)
.then((delegations) => {
for (const delegation of delegations) {
if(delegation.balance.denom === denom){
return delegation.balance.amount
}
}
return 0
})
}
}