/
index.js
126 lines (116 loc) · 3.13 KB
/
index.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
import * as oasis from '@oasisprotocol/client';
import { TX_LIST_LENGTH } from "../../../config";
import { commonFetch, getOasisClient } from "./request";
const MAX_LENGTH = 500
/**
* get account balance
*/
export async function getBalance(address) {
let url = "/chain/account/info/" + address
let account = await commonFetch(url).catch(() => { })
if (account && account.code === 0) {
return { ...account.data, address }
} else {
return {}
}
}
/**
* get tx list
* @param {*} address
* @returns
*/
export async function getTransactionList(address) {
let url = "/chain/transactions?address=" + address + "&size=" + TX_LIST_LENGTH
let txList = await commonFetch(url).catch(() => { })
if (txList && txList.code === 0) {
return txList.data
} else {
return []
}
}
/**
* get account delegations list
* @param {*} address
* @returns
*/
export async function getAccountStakeInfo(address) {
let url = "/chain/account/delegations?address=" + address + "&size=" + MAX_LENGTH
let accountStakeInfo = await commonFetch(url).catch(() => [])
return accountStakeInfo
}
/**
* get validator info
* @param {*} address
* @returns
*/
export async function getNodeStakeInfo(address) {
let url = "/validator/info?address=" + address
let validatorInfo = await commonFetch(url).catch(() => { })
return validatorInfo
}
/**
* get account debonding
* @param {*} address
* @returns
*/
export async function getUserDebondInfo(address) {
let url = "/chain/account/debonding?address=" + address + "&size=" + MAX_LENGTH
let userDebondInfo = await commonFetch(url).catch(() => { })
return userDebondInfo
}
/**
* get validator list
* @param {*} address
* @returns
*/
export async function getNodeStakeList() {
let url = "/validator/list" + "?pageSize=" + MAX_LENGTH
let validatorList = await commonFetch(url).catch(() => [])
return validatorList
}
/**
* get tx hash
* @param {*} txHash
* @returns
*/
export async function getSubmitStatus(txhash) {
let url = "/chain/transaction/" + txhash
let txStatus = await commonFetch(url).catch(() => { })
if (txStatus && txStatus.code === 0) {
return txStatus.data
} else {
return {}
}
}
/**
* rpc get avaliable balance
* @param {*} address
* @returns
*/
export async function getRpcBalance(address) {
const oasisClient = getOasisClient()
let shortKey = await oasis.staking.addressFromBech32(address)
let height = oasis.consensus.HEIGHT_LATEST
let account = await oasisClient.stakingAccount({ height: height, owner: shortKey, }).catch((err) => err)
if (account && account.code && account.code !== 0) {
return { err: account }
}
let balance = account?.general?.balance || 0
balance = oasis.quantity.toBigInt(balance).toString()
let nonce = account?.general?.nonce || 0
return { balance, nonce }
}
/**
* rpc get nonce
* @param {*} address
* @returns
*/
export async function getRpcNonce(address) {
const oasisClient = getOasisClient()
let publicKey = await oasis.staking.addressFromBech32(address)
const nonce = await oasisClient.consensusGetSignerNonce({
account_address: publicKey,
height: oasis.consensus.HEIGHT_LATEST
}) ?? 0;
return nonce
}