-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from balancer/fix-circular-deps
fix: Circular deps.
- Loading branch information
Showing
40 changed files
with
214 additions
and
185 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { parseUnits } from 'viem'; | ||
import { Token } from '../../token'; | ||
import { TokenAmount, BigintIsh } from '../../tokenAmount'; | ||
import { WAD } from '../../../utils/math'; | ||
import { _calcInGivenOut, _calcOutGivenIn } from './fxMath'; | ||
import { HumanAmount } from '../../../data/types'; | ||
import { MathFx } from './helpers'; | ||
|
||
export class FxPoolToken extends TokenAmount { | ||
public readonly index: number; | ||
public readonly latestFXPrice: HumanAmount; | ||
public readonly fxOracleDecimals: number; | ||
public numeraire: bigint; // in 36 decimals | ||
private readonly scalar36 = this.scalar * WAD; | ||
|
||
public constructor( | ||
token: Token, | ||
amount: BigintIsh, | ||
latestFXPrice: HumanAmount, | ||
fxOracleDecimals: number, | ||
index: number, | ||
) { | ||
super(token, amount); | ||
this.latestFXPrice = latestFXPrice; | ||
this.fxOracleDecimals = fxOracleDecimals; | ||
const truncatedNumeraire = MathFx.mulDownFixed( | ||
this.amount, | ||
parseUnits(this.latestFXPrice, this.fxOracleDecimals), | ||
this.fxOracleDecimals, | ||
); | ||
this.numeraire = truncatedNumeraire * this.scalar36; | ||
this.index = index; | ||
} | ||
|
||
public increase(amount: bigint): TokenAmount { | ||
this.amount = this.amount + amount; | ||
this.scale18 = this.amount * this.scalar; | ||
const truncatedNumeraire = MathFx.mulDownFixed( | ||
this.amount, | ||
parseUnits(this.latestFXPrice, this.fxOracleDecimals), | ||
this.fxOracleDecimals, | ||
); | ||
this.numeraire = truncatedNumeraire * this.scalar36; | ||
return this; | ||
} | ||
|
||
public decrease(amount: bigint): TokenAmount { | ||
this.amount = this.amount - amount; | ||
this.scale18 = this.amount * this.scalar; | ||
const truncatedNumeraire = MathFx.mulDownFixed( | ||
this.amount, | ||
parseUnits(this.latestFXPrice, this.fxOracleDecimals), | ||
this.fxOracleDecimals, | ||
); | ||
this.numeraire = truncatedNumeraire * this.scalar36; | ||
return this; | ||
} | ||
|
||
public static fromNumeraire( | ||
poolToken: FxPoolToken, | ||
numeraire: BigintIsh, | ||
divUp?: boolean, | ||
): FxPoolToken { | ||
const truncatedNumeraire = BigInt(numeraire) / poolToken.scalar36; // loss of precision required to match SC implementation | ||
const amount = divUp | ||
? MathFx.divUpFixed( | ||
BigInt(truncatedNumeraire), | ||
parseUnits( | ||
poolToken.latestFXPrice, | ||
poolToken.fxOracleDecimals, | ||
), | ||
poolToken.fxOracleDecimals, | ||
) | ||
: MathFx.divDownFixed( | ||
BigInt(truncatedNumeraire), | ||
parseUnits( | ||
poolToken.latestFXPrice, | ||
poolToken.fxOracleDecimals, | ||
), | ||
poolToken.fxOracleDecimals, | ||
); | ||
return new FxPoolToken( | ||
poolToken.token, | ||
amount, | ||
poolToken.latestFXPrice, | ||
poolToken.fxOracleDecimals, | ||
poolToken.index, | ||
); | ||
} | ||
} |
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,3 +1,5 @@ | ||
export * from './fxFactory'; | ||
export * from './fxPool'; | ||
export * from './fxMath'; | ||
export * from './types'; | ||
export * from './fxPoolToken'; |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { SwapKind } from '../../../types'; | ||
import { FxPoolToken } from './fxPoolToken'; | ||
|
||
export type FxPoolPairData = { | ||
tIn: FxPoolToken; | ||
tOut: FxPoolToken; | ||
alpha: bigint; | ||
beta: bigint; | ||
delta: bigint; | ||
lambda: bigint; | ||
_oGLiq: bigint; | ||
_nGLiq: bigint; | ||
_oBals: bigint[]; | ||
_nBals: bigint[]; | ||
givenToken: FxPoolToken; | ||
swapKind: SwapKind; | ||
}; |
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
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.