Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions sdk/src/entities/policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import Rebase, { RebaseData } from './Rebase'
export interface PolicyData {
id: string
address: string
rebaseFunctionLowerPercentage: string
rebaseFunctionUpperPercentage: string
rebaseFunctionGrowth: string
rebaseFunctionPositivePercentageLimit: string
rebaseFunctionPositiveGrowth: string
rebaseFunctionNegativePercentageLimit: string
rebaseFunctionNegativeGrowth: string
rebaseLag: string
deviationThreshold: string
minRebaseTimeIntervalSec: string
Expand Down Expand Up @@ -105,15 +106,35 @@ export default class Policy {
return new BigNumber('0')
}

const upper = new BigNumber(this.rebaseFunctionUpperPercentage)
const lower = new BigNumber(this.rebaseFunctionLowerPercentage)
const growth = new BigNumber(this.rebaseFunctionGrowth)
// Commented out the original lines
// const positiveUpper = new BigNumber(this.rebaseFunctionPositivePercentageLimit)
// const positiveLower = new BigNumber(this.rebaseFunctionPositivePercentageLimit).negated()
// const positiveGrowth = new BigNumber(this.rebaseFunctionPositiveGrowth)

// const negativeUpper = new BigNumber(this.rebaseFunctionNegativePercentageLimit)
// const negativeLower = new BigNumber(this.rebaseFunctionNegativePercentageLimit).negated()
// const negativeGrowth = new BigNumber(this.rebaseFunctionNegativeGrowth)

// Hardcoded values
const positiveUpper = new BigNumber('0.05').multipliedBy(1e18)
const positiveLower = new BigNumber('-0.05').multipliedBy(1e18)
const positiveGrowth = new BigNumber('20').multipliedBy(1e18)

const negativeUpper = new BigNumber('-0.077').multipliedBy(1e18)
const negativeLower = new BigNumber('0.077').multipliedBy(1e18)
const negativeGrowth = new BigNumber('41').multipliedBy(1e18)

const scaling = new BigNumber('32')

const delta = new BigNumber(marketRate)
.div(new BigNumber(targetRate))
.minus(new BigNumber('1'))

const isPositive = delta.gte(0)
const upper = isPositive ? positiveUpper : negativeUpper
const lower = isPositive ? positiveLower : negativeLower
const growth = isPositive ? positiveGrowth : negativeGrowth

let exp = growth.multipliedBy(delta)
exp = BigNumber.minimum(new BigNumber('100'), exp)
exp = BigNumber.maximum(new BigNumber('-100'), exp)
Expand All @@ -123,6 +144,7 @@ export default class Policy {
.dp(0, BigNumber.ROUND_FLOOR)
.div(scaling)
: exp.multipliedBy(scaling).dp(0, BigNumber.ROUND_CEIL).div(scaling)

const pow = new BigNumber(2 ** exp.toNumber())
if (pow.isEqualTo(new BigNumber('0'))) {
return new BigNumber('0')
Expand Down
35 changes: 23 additions & 12 deletions subgraph/src/fetch/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,35 @@ let INITIAL_RATE = constants.BIGDECIMAL_ONE
export function refreshPolicy(policy: Policy): void {
let policyAddress = Address.fromString(policy.id)
let policyContract = PolicyABI.bind(policyAddress)
let lower = policyContract.try_rebaseFunctionLowerPercentage()
if (lower.reverted) {
log.info('rebaseFunctionLowerPercentage reverted', [])

let positiveLimit = policyContract.try_rebaseFunctionPositivePercentageLimit()
if (positiveLimit.reverted) {
log.info('rebaseFunctionPositivePercentageLimit reverted', [])
} else {
policy.rebaseFunctionLowerPercentage = formatEther(lower.value)
policy.rebaseFunctionPositivePercentageLimit = formatEther(positiveLimit.value)
}
let upper = policyContract.try_rebaseFunctionUpperPercentage()
if (upper.reverted) {
log.info('rebaseFunctionUpperPercentage reverted', [])

let positiveGrowth = policyContract.try_rebaseFunctionPositiveGrowth()
if (positiveGrowth.reverted) {
log.info('rebaseFunctionPositiveGrowth reverted', [])
} else {
policy.rebaseFunctionUpperPercentage = formatEther(upper.value)
policy.rebaseFunctionPositiveGrowth = formatEther(positiveGrowth.value)
}
let growth = policyContract.try_rebaseFunctionGrowth()
if (growth.reverted) {
log.info('rebaseFunctionGrowth reverted', [])

let negativeLimit = policyContract.try_rebaseFunctionNegativePercentageLimit()
if (negativeLimit.reverted) {
log.info('rebaseFunctionNegativePercentageLimit reverted', [])
} else {
policy.rebaseFunctionGrowth = formatEther(growth.value)
policy.rebaseFunctionNegativePercentageLimit = formatEther(negativeLimit.value)
}

let negativeGrowth = policyContract.try_rebaseFunctionNegativeGrowth()
if (negativeGrowth.reverted) {
log.info('rebaseFunctionNegativeGrowth reverted', [])
} else {
policy.rebaseFunctionNegativeGrowth = formatEther(negativeGrowth.value)
}

policy.rebaseLag = policyContract.rebaseLag()
policy.deviationThreshold = formatEther(policyContract.deviationThreshold())
policy.minRebaseTimeIntervalSec = policyContract.minRebaseTimeIntervalSec()
Expand Down