-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update query state with stagate client
- Loading branch information
1 parent
28ead90
commit e820c90
Showing
19 changed files
with
389 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,116 @@ | ||
import Cosmos from "../cosmos"; | ||
import { BaseProvider } from "../providers"; | ||
import Cosmos from '../cosmos'; | ||
import { BaseProvider } from '../providers'; | ||
|
||
export default class APRCalCulator { | ||
cosmos: Cosmos | ||
provider: BaseProvider | any; | ||
constructor(cosmos: any, provider: BaseProvider | any) { | ||
this.cosmos = cosmos; | ||
this.provider = provider; | ||
} | ||
async stakingAPR(decimal: number): Promise<number> { | ||
let inflation = await this.inflation(decimal); | ||
let communityTax = await this.communityTax(decimal); | ||
let bondedTokensRatio = await this.bondedTokensRatio(); | ||
return (inflation * (1 - communityTax)) / bondedTokensRatio; | ||
} | ||
async annualProvisions(decimal: number): Promise<number> { | ||
let annualProvisions = await this.cosmos.mint.query.AnnualProvisions({}); | ||
return uint8ArrayStringToNumber(annualProvisions.annualProvisions, decimal); | ||
} | ||
async actualStakingAPR(decimal: number): Promise<number> { | ||
let stakingAPR = await this.stakingAPR(decimal); | ||
let actualProvisionsRatio = await this.actualProvisionsRatio(); | ||
return stakingAPR * actualProvisionsRatio; | ||
} | ||
async finalStakingAPR(validatorAddress: string, mintDecimal: number, distributionDecimal: number): Promise<number> { | ||
let actualStakingAPR = await this.actualStakingAPR(mintDecimal); | ||
let validatorCommission = await this.validatorCommission(validatorAddress, distributionDecimal); | ||
return actualStakingAPR * (1 - validatorCommission); | ||
} | ||
async inflation(decimal: number): Promise<number> { | ||
let data = await this.cosmos.mint.query.Inflation({}); | ||
let inflation = uint8ArrayStringToNumber(data.inflation, decimal); | ||
return inflation; | ||
} | ||
async communityTax(decimal: number): Promise<number> { | ||
let data = await this.cosmos.distribution.query.Params({}); | ||
let communityTax = uint8ArrayStringToNumber(data.params.communityTax, decimal); | ||
return communityTax | ||
} | ||
async bondedTokens(): Promise<number> { | ||
let data = await this.cosmos.staking.query.Pool({}); | ||
let bondedTokens = uint8ArrayStringToNumber(data.pool.bondedTokens, 0); | ||
return bondedTokens | ||
} | ||
async notBondedTokens(): Promise<number> { | ||
let data = await this.cosmos.staking.query.Pool({}); | ||
let notBondedTokens = uint8ArrayStringToNumber(data.pool.notBondedTokens, 0); | ||
return notBondedTokens | ||
} | ||
async bondedTokensRatio(): Promise<number> { | ||
let mintDenom = (await this.cosmos.mint.query.Params({})).params.mintDenom; | ||
let currentSupply: any = (await this.cosmos.bank.query.SupplyOf({ denom: mintDenom })).amount.amount; | ||
currentSupply = parseFloat(currentSupply); | ||
let bondedTokens = await this.bondedTokens(); | ||
let bondedTokensRatio = bondedTokens / currentSupply; | ||
return bondedTokensRatio; | ||
} | ||
async actualProvisionsRatio(): Promise<number> { | ||
let estBlockPerYear = (await this.cosmos.mint.query.Params({})).params.blocksPerYear.toNumber(); | ||
let estBlockTime = 86400 * 365.25 / estBlockPerYear; | ||
let height = await this.provider.batchQueryClient.getHeight(); | ||
let block = await this.provider.batchQueryClient.getBlock(height); | ||
let preBlock = await this.provider.batchQueryClient.getBlock(height - 1000); | ||
let currentBlockTime = Date.parse(block.header.time); | ||
let preBlockTime = Date.parse(preBlock.header.time); | ||
let statBlockTime = (currentBlockTime - preBlockTime) / 1000000 | ||
let actualProvisionsRatio = estBlockTime / statBlockTime; | ||
return actualProvisionsRatio; | ||
} | ||
async validatorCommission(validatorAddress: string, decimal: number): Promise<number> { | ||
let data = await this.cosmos.distribution.query.ValidatorCommission({ validatorAddress: validatorAddress }) | ||
let validatorCommission = uint8ArrayStringToNumber(data.commission.commission[0].amount, decimal) | ||
return validatorCommission; | ||
} | ||
cosmos: Cosmos; | ||
provider: BaseProvider | any; | ||
constructor(cosmos: any, provider: BaseProvider | any) { | ||
this.cosmos = cosmos; | ||
this.provider = provider; | ||
} | ||
async stakingAPR(decimal: number): Promise<number> { | ||
const inflation = await this.inflation(decimal); | ||
const communityTax = await this.communityTax(decimal); | ||
const bondedTokensRatio = await this.bondedTokensRatio(); | ||
return (inflation * (1 - communityTax)) / bondedTokensRatio; | ||
} | ||
async annualProvisions(decimal: number): Promise<number> { | ||
const annualProvisions = await this.cosmos.mint.query.AnnualProvisions({}); | ||
return uint8ArrayStringToNumber(annualProvisions.annualProvisions, decimal); | ||
} | ||
async actualStakingAPR(decimal: number): Promise<number> { | ||
const stakingAPR = await this.stakingAPR(decimal); | ||
const actualProvisionsRatio = await this.actualProvisionsRatio(); | ||
return stakingAPR * actualProvisionsRatio; | ||
} | ||
async finalStakingAPR( | ||
validatorAddress: string, | ||
mintDecimal: number, | ||
distributionDecimal: number | ||
): Promise<number> { | ||
const actualStakingAPR = await this.actualStakingAPR(mintDecimal); | ||
const validatorCommission = await this.validatorCommission( | ||
validatorAddress, | ||
distributionDecimal | ||
); | ||
return actualStakingAPR * (1 - validatorCommission); | ||
} | ||
async inflation(decimal: number): Promise<number> { | ||
const data = await this.cosmos.mint.query.Inflation({}); | ||
const inflation = uint8ArrayStringToNumber(data.inflation, decimal); | ||
return inflation; | ||
} | ||
async communityTax(decimal: number): Promise<number> { | ||
const data = await this.cosmos.distribution.query.Params({}); | ||
const communityTax = uint8ArrayStringToNumber( | ||
data.params.communityTax, | ||
decimal | ||
); | ||
return communityTax; | ||
} | ||
async bondedTokens(): Promise<number> { | ||
const data = await this.cosmos.staking.query.Pool({}); | ||
const bondedTokens = uint8ArrayStringToNumber(data.pool.bondedTokens, 0); | ||
return bondedTokens; | ||
} | ||
async notBondedTokens(): Promise<number> { | ||
const data = await this.cosmos.staking.query.Pool({}); | ||
const notBondedTokens = uint8ArrayStringToNumber( | ||
data.pool.notBondedTokens, | ||
0 | ||
); | ||
return notBondedTokens; | ||
} | ||
async bondedTokensRatio(): Promise<number> { | ||
const mintDenom = (await this.cosmos.mint.query.Params({})).params.mintDenom; | ||
let currentSupply: any = ( | ||
await this.cosmos.bank.query.SupplyOf({ denom: mintDenom }) | ||
).amount.amount; | ||
currentSupply = parseFloat(currentSupply); | ||
const bondedTokens = await this.bondedTokens(); | ||
const bondedTokensRatio = bondedTokens / currentSupply; | ||
return bondedTokensRatio; | ||
} | ||
async actualProvisionsRatio(): Promise<number> { | ||
const estBlockPerYear = ( | ||
await this.cosmos.mint.query.Params({}) | ||
).params.blocksPerYear.toNumber(); | ||
const estBlockTime = (86400 * 365.25) / estBlockPerYear; | ||
const height = await this.provider.batchQueryClient.getHeight(); | ||
const block = await this.provider.batchQueryClient.getBlock(height); | ||
const preBlock = await this.provider.batchQueryClient.getBlock(height - 1000); | ||
const currentBlockTime = Date.parse(block.header.time); | ||
const preBlockTime = Date.parse(preBlock.header.time); | ||
const statBlockTime = (currentBlockTime - preBlockTime) / 1000000; | ||
const actualProvisionsRatio = estBlockTime / statBlockTime; | ||
return actualProvisionsRatio; | ||
} | ||
async validatorCommission( | ||
validatorAddress: string, | ||
decimal: number | ||
): Promise<number> { | ||
const data = await this.cosmos.distribution.query.ValidatorCommission({ | ||
validatorAddress: validatorAddress, | ||
}); | ||
const validatorCommission = uint8ArrayStringToNumber( | ||
data.commission.commission[0].amount, | ||
decimal | ||
); | ||
return validatorCommission; | ||
} | ||
} | ||
function uint8ArrayStringToNumber(x: Uint8Array | string, decimal: number): number { | ||
let xStr = Buffer.from(x).toString(); | ||
let xlen = xStr.length; | ||
if (xlen < decimal) { | ||
xStr = "." + "0".repeat(decimal - xlen) + xStr; | ||
} | ||
else if (xlen === decimal) { | ||
xStr = "." + xStr; | ||
} | ||
else { | ||
xStr = xStr.slice(0, xlen - decimal) + "." + xStr.slice(xlen - decimal); | ||
} | ||
return parseFloat(xStr); | ||
function uint8ArrayStringToNumber( | ||
x: Uint8Array | string, | ||
decimal: number | ||
): number { | ||
let xStr = Buffer.from(x).toString(); | ||
const xlen = xStr.length; | ||
if (xlen < decimal) { | ||
xStr = '.' + '0'.repeat(decimal - xlen) + xStr; | ||
} else if (xlen === decimal) { | ||
xStr = '.' + xStr; | ||
} else { | ||
xStr = xStr.slice(0, xlen - decimal) + '.' + xStr.slice(xlen - decimal); | ||
} | ||
return parseFloat(xStr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.