cosmos-explorer/src/libs/fetch.js
2021-08-05 21:11:11 +08:00

172 lines
5.4 KiB
JavaScript

import fetch from 'node-fetch'
import store from '@/store'
import compareVersions from 'compare-versions'
import {
Proposal, ProposalTally, Proposer, StakingPool, Votes, Deposit,
Validator, StakingParameters, Block, ValidatorDistribution, StakingDelegation, WrapStdTx,
} from './data'
function commonProcess(res) {
return res.result
}
// 头像
export function keybase(identity) {
return fetch(`https://keybase.io/_/api/1.0/user/lookup.json?key_suffix=${identity}&fields=pictures`)
.then(res => res.json())
}
async function refetchVersion(chain) {
await fetch(`${chain.api}/node_info`)
.then(res => res.json())
.then(json => {
const sdk = json.application_version.build_deps.find(e => e.startsWith('github.com/cosmos/cosmos-sdk'))
const re = /(\d+(\.\d+)*)/i
const version = sdk.match(re)
return version[0]
})
}
const chainAPI = class ChainFetch {
getSelectedConfig() {
let chain = store.state.chains.selected
const lschains = localStorage.getItem('chains')
if (lschains) {
chain = JSON.parse(lschains)[chain.chain_name]
}
if (!chain.sdk_version) {
chain.sdk_version = refetchVersion(chain)
}
this.config = chain
return this.config
}
async getLatestBlock() {
return this.get('/blocks/latest').then(data => Block.create(data))
}
async getBlockByHeight(height) {
return this.get(`/blocks/${height}`).then(data => Block.create(data))
}
async getTxs(hash) {
const ver = this.getSelectedConfig() ? this.config.sdk_version : '0.41'
// /cosmos/tx/v1beta1/txs/{hash}
if (ver && compareVersions(ver, '0.40') < 1) {
return this.get(`/txs/${hash}`).then(data => WrapStdTx.create(data, ver))
}
return this.get(`/cosmos/tx/v1beta1/txs/${hash}`).then(data => WrapStdTx.create(data, ver))
}
async getValidatorDistribution(address) {
return this.get(`/distribution/validators/${address}`).then(data => {
const ret = ValidatorDistribution.create(commonProcess(data))
ret.versionFixed(this.config.sdk_version)
return ret
})
}
async getStakingDelegatorDelegation(delegatorAddr, validatorAddr) {
return this.get(`/staking/delegators/${delegatorAddr}/delegations/${validatorAddr}`).then(data => StakingDelegation.create(commonProcess(data)))
}
async getBankTotal(denom) {
if (compareVersions(this.config.sdk_version, '0.40') < 0) {
return this.get(`/supply/total/${denom}`).then(data => ({ amount: commonProcess(data), denom }))
}
return this.get(`/bank/total/${denom}`).then(data => commonProcess(data))
}
async getStakingPool() {
return this.get('/staking/pool').then(data => new StakingPool().init(commonProcess(data)))
}
async getMintingInflation() {
return this.get('/minting/inflation').then(data => Number(commonProcess(data)))
}
async getStakingParameters() {
return this.get('/staking/parameters').then(data => new StakingParameters().init(commonProcess(data)))
}
async getValidatorList() {
return this.get('/staking/validators').then(data => commonProcess(data).map(i => new Validator().init(i)))
}
async getStakingValidator(address) {
return this.get(`/staking/validators/${address}`).then(data => new Validator().init(commonProcess(data)))
}
async getSlashingParameters() {
return this.get('/slashing/parameters').then(data => commonProcess(data))
}
async getMintParameters() {
return this.get('/minting/parameters').then(data => commonProcess(data))
}
async getDistributionParameters() {
return this.get('/distribution/parameters').then(data => commonProcess(data))
}
async getGovernanceParameterDeposit() {
return this.get('/gov/parameters/deposit').then(data => commonProcess(data))
}
async getGovernanceParameterTallying() {
return this.get('/gov/parameters/tallying').then(data => commonProcess(data))
}
async getGovernanceParameterVoting() {
return this.get('/gov/parameters/voting').then(data => commonProcess(data))
}
async getGovernanceTally(pid, total) {
return this.get(`/gov/proposals/${pid}/tally`).then(data => new ProposalTally().init(commonProcess(data), total))
}
getGovernance(pid) {
return this.get(`/gov/proposals/${pid}`).then(data => {
const p = new Proposal().init(commonProcess(data), 0)
p.versionFixed(this.config.sdk_version)
return p
})
}
async getGovernanceProposer(pid) {
return this.get(`/gov/proposals/${pid}/proposer`).then(data => new Proposer().init(commonProcess(data)))
}
async getGovernanceDeposits(pid) {
return this.get(`/gov/proposals/${pid}/deposits`).then(data => {
const result = commonProcess(data)
return Array.isArray(result) ? result.reverse().map(d => new Deposit().init(d)) : result
})
}
async getGovernanceVotes(pid) {
return this.get(`/gov/proposals/${pid}/votes`).then(data => commonProcess(data).map(d => new Votes().init(d)))
}
async getGovernanceList() {
return Promise.all([this.get('/gov/proposals'), this.get('/staking/pool')]).then(data => {
const pool = new StakingPool().init(commonProcess(data[1]))
const ret = []
commonProcess(data[0]).forEach(e => {
const g = new Proposal().init(e, pool.bondedToken)
g.versionFixed(this.config.sdk_version)
ret.push(g)
})
return ret
})
}
async get(url) {
this.getSelectedConfig()
const ret = await fetch(this.config.api + url).then(response => response.json())
return ret
}
}
export default chainAPI