Skip to content

Commit

Permalink
idl and ts code updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ckamm committed Nov 14, 2023
1 parent c8e7b6f commit 4385230
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 154 deletions.
56 changes: 55 additions & 1 deletion mango_v4.json
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,34 @@
"type": {
"option": "f32"
}
},
{
"name": "maintWeightShiftStartOpt",
"type": {
"option": "u64"
}
},
{
"name": "maintWeightShiftEndOpt",
"type": {
"option": "u64"
}
},
{
"name": "maintWeightShiftAssetTargetOpt",
"type": {
"option": "f32"
}
},
{
"name": "maintWeightShiftLiabTargetOpt",
"type": {
"option": "f32"
}
},
{
"name": "maintWeightShiftAbort",
"type": "bool"
}
]
},
Expand Down Expand Up @@ -7079,12 +7107,38 @@
"name": "depositsInSerum",
"type": "i64"
},
{
"name": "maintWeightShiftStart",
"type": "u64"
},
{
"name": "maintWeightShiftEnd",
"type": "u64"
},
{
"name": "maintWeightShiftDurationInv",
"type": {
"defined": "I80F48"
}
},
{
"name": "maintWeightShiftAssetTarget",
"type": {
"defined": "I80F48"
}
},
{
"name": "maintWeightShiftLiabTarget",
"type": {
"defined": "I80F48"
}
},
{
"name": "reserved",
"type": {
"array": [
"u8",
2072
2008
]
}
}
Expand Down
66 changes: 63 additions & 3 deletions ts/client/src/accounts/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface BankForHealth {
scaledInitLiabWeight(price: I80F48): I80F48;
nativeDeposits(): I80F48;
nativeBorrows(): I80F48;
maintWeights(): [I80F48, I80F48];

depositWeightScaleStartQuote: number;
borrowWeightScaleStartQuote: number;
Expand Down Expand Up @@ -75,6 +76,9 @@ export class Bank implements BankForHealth {
public maintLiabWeight: I80F48;
public liquidationFee: I80F48;
public dust: I80F48;
public maintWeightShiftDurationInv: I80F48;
public maintWeightShiftAssetTarget: I80F48;
public maintWeightShiftLiabTarget: I80F48;

static from(
publicKey: PublicKey,
Expand Down Expand Up @@ -126,6 +130,14 @@ export class Bank implements BankForHealth {
tokenConditionalSwapTakerFeeRate: number;
tokenConditionalSwapMakerFeeRate: number;
flashLoanSwapFeeRate: number;
interestTargetUtilization: number;
interestCurveScaling: number;
depositsInSerum: BN;
maintWeightShiftStart: BN;
maintWeightShiftEnd: BN;
maintWeightShiftDurationInv: I80F48Dto;
maintWeightShiftAssetTarget: I80F48Dto;
maintWeightShiftLiabTarget: I80F48Dto;
},
): Bank {
return new Bank(
Expand Down Expand Up @@ -177,6 +189,14 @@ export class Bank implements BankForHealth {
obj.tokenConditionalSwapTakerFeeRate,
obj.tokenConditionalSwapMakerFeeRate,
obj.flashLoanSwapFeeRate,
obj.interestTargetUtilization,
obj.interestCurveScaling,
obj.depositsInSerum,
obj.maintWeightShiftStart,
obj.maintWeightShiftEnd,
obj.maintWeightShiftDurationInv,
obj.maintWeightShiftAssetTarget,
obj.maintWeightShiftLiabTarget,
);
}

Expand Down Expand Up @@ -229,6 +249,14 @@ export class Bank implements BankForHealth {
public tokenConditionalSwapTakerFeeRate: number,
public tokenConditionalSwapMakerFeeRate: number,
public flashLoanSwapFeeRate: number,
public interestTargetUtilization: number,
public interestCurveScaling: number,
public depositsInSerum: BN,
public maintWeightShiftStart: BN,
public maintWeightShiftEnd: BN,
maintWeightShiftDurationInv: I80F48Dto,
maintWeightShiftAssetTarget: I80F48Dto,
maintWeightShiftLiabTarget: I80F48Dto,
) {
this.name = utf8.decode(new Uint8Array(name)).split('\x00')[0];
this.oracleConfig = {
Expand All @@ -255,6 +283,9 @@ export class Bank implements BankForHealth {
this.initLiabWeight = I80F48.from(initLiabWeight);
this.liquidationFee = I80F48.from(liquidationFee);
this.dust = I80F48.from(dust);
this.maintWeightShiftDurationInv = I80F48.from(maintWeightShiftDurationInv);
this.maintWeightShiftAssetTarget = I80F48.from(maintWeightShiftAssetTarget);
this.maintWeightShiftLiabTarget = I80F48.from(maintWeightShiftLiabTarget);
this._price = undefined;
this._uiPrice = undefined;
this._oracleLastUpdatedSlot = undefined;
Expand Down Expand Up @@ -376,6 +407,32 @@ export class Bank implements BankForHealth {
);
}

maintWeights(): [I80F48, I80F48] {
const nowTs = new BN(Date.now() / 1000);
if (
this.maintWeightShiftDurationInv.isZero() ||
nowTs.lte(this.maintWeightShiftStart)
) {
return [this.maintAssetWeight, this.maintLiabWeight];
} else if (nowTs.gte(this.maintWeightShiftEnd)) {
return [
this.maintWeightShiftAssetTarget,
this.maintWeightShiftLiabTarget,
];
} else {
const scale = I80F48.fromU64(nowTs.sub(this.maintWeightShiftStart)).mul(
this.maintWeightShiftDurationInv,
);
const asset = this.maintAssetWeight.add(
this.maintWeightShiftAssetTarget.sub(this.maintAssetWeight).mul(scale),
);
const liab = this.maintLiabWeight.add(
this.maintWeightShiftLiabTarget.sub(this.maintLiabWeight).mul(scale),
);
return [asset, liab];
}
}

getAssetPrice(): I80F48 {
return this.price.min(I80F48.fromNumber(this.stablePriceModel.stablePrice));
}
Expand Down Expand Up @@ -449,19 +506,22 @@ export class Bank implements BankForHealth {
}

const utilization = totalBorrows.div(totalDeposits);
const scaling = I80F48.fromNumber(
this.interestCurveScaling == 0.0 ? 1.0 : this.interestCurveScaling,
);
if (utilization.lt(this.util0)) {
const slope = this.rate0.div(this.util0);
return slope.mul(utilization);
return slope.mul(utilization).mul(scaling);
} else if (utilization.lt(this.util1)) {
const extraUtil = utilization.sub(this.util0);
const slope = this.rate1.sub(this.rate0).div(this.util1.sub(this.util0));
return this.rate0.add(slope.mul(extraUtil));
return this.rate0.add(slope.mul(extraUtil)).mul(scaling);
} else {
const extraUtil = utilization.sub(this.util1);
const slope = this.maxRate
.sub(this.rate1)
.div(I80F48.fromNumber(1).sub(this.util1));
return this.rate1.add(slope.mul(extraUtil));
return this.rate1.add(slope.mul(extraUtil)).mul(scaling);
}
}

Expand Down
Loading

0 comments on commit 4385230

Please sign in to comment.