Skip to content

Commit

Permalink
Branded types (#125)
Browse files Browse the repository at this point in the history
* branded types
  • Loading branch information
DevRozaDev authored Aug 30, 2024
1 parent 2d77e1e commit 790d884
Show file tree
Hide file tree
Showing 46 changed files with 1,435 additions and 1,263 deletions.
11 changes: 7 additions & 4 deletions scripts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
priceToSqrtPrice,
simulateInvariantSwap,
Tick,
TokenAmount,
toPercentage,
toPrice,
toTokenAmount
Expand All @@ -32,8 +33,9 @@ let TOKEN1_ID: string
async function setupEssentials() {
account = await getSigner(1000n * ONE_ALPH)
INVARIANT_ADDRESS = (await Invariant.deploy(account, Network.Local)).address
TOKEN0_ID = await FungibleToken.deploy(account, 10n ** 30n, 'Coin', 'COIN', 12n)
TOKEN1_ID = await FungibleToken.deploy(account, 10n ** 30n, 'Coin', 'COIN', 12n)
const initMint = (10n ** 30n) as TokenAmount
TOKEN0_ID = await FungibleToken.deploy(account, initMint, 'Coin', 'COIN', 12n)
TOKEN1_ID = await FungibleToken.deploy(account, initMint, 'Coin', 'COIN', 12n)
}

const main = async () => {
Expand Down Expand Up @@ -290,8 +292,9 @@ const usingAlphAsToken = async () => {

const usingFungibleToken = async () => {
// deploy token, it will return token ids
const TOKEN0_ID = await FungibleToken.deploy(account, 500n, 'CoinA', 'ACOIN', 12n)
const TOKEN1_ID = await FungibleToken.deploy(account, 500n, 'CoinB', 'BCOIN', 12n)
const initMint = 500n as TokenAmount
const TOKEN0_ID = await FungibleToken.deploy(account, initMint, 'CoinA', 'ACOIN', 12n)
const TOKEN1_ID = await FungibleToken.deploy(account, initMint, 'CoinB', 'BCOIN', 12n)

// load token by passing its address (you can use existing one), it allows you to interact with it
const token = await FungibleToken.load(Network.Local)
Expand Down
32 changes: 30 additions & 2 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import { CLAMM, Invariant } from '../artifacts/ts'
import { SQRT_PRICE_DENOMINATOR, SQRT_PRICE_SCALE } from '../artifacts/ts/constants'
import {
MIN_SQRT_PRICE as _MIN_SQRT_PRICE,
MAX_SQRT_PRICE as _MAX_SQRT_PRICE,
SQRT_PRICE_DENOMINATOR as _SQRT_PRICE_DENOMINATOR,
FEE_GROWTH_DENOMINATOR as _FEE_GROWTH_DENOMINATOR,
LIQUIDITY_DENOMINATOR as _LIQUIDITY_DENOMINATOR,
TOKEN_AMOUNT_DENOMINATOR as _TOKEN_AMOUNT_DENOMINATOR,
PERCENTAGE_DENOMINATOR as _PERCENTAGE_DENOMINATOR,
FIXED_POINT_DENOMINATOR as _FIXED_POINT_DENOMINATOR,
SQRT_PRICE_SCALE
} from '../artifacts/ts/constants'
import {
FeeGrowth,
FixedPoint,
Liquidity,
Percentage,
Price,
SqrtPrice,
TokenAmount
} from './types'

export * from '../artifacts/ts/constants'

export const MIN_SQRT_PRICE = _MIN_SQRT_PRICE as SqrtPrice
export const MAX_SQRT_PRICE = _MAX_SQRT_PRICE as SqrtPrice
export const SQRT_PRICE_DENOMINATOR = _SQRT_PRICE_DENOMINATOR as SqrtPrice
export const FEE_GROWTH_DENOMINATOR = _FEE_GROWTH_DENOMINATOR as FeeGrowth
export const LIQUIDITY_DENOMINATOR = _LIQUIDITY_DENOMINATOR as Liquidity
export const TOKEN_AMOUNT_DENOMINATOR = _TOKEN_AMOUNT_DENOMINATOR as TokenAmount
export const PERCENTAGE_DENOMINATOR = _PERCENTAGE_DENOMINATOR as Percentage
export const FIXED_POINT_DENOMINATOR = _FIXED_POINT_DENOMINATOR as FixedPoint

export const { SEARCH_RANGE, CHUNKS_PER_BATCH, HALF_CHUNK_SIZE, CHUNK_SIZE } = Invariant.consts

export const {
Expand All @@ -18,7 +46,7 @@ export const {
} = CLAMM.consts

export const PRICE_SCALE = SQRT_PRICE_SCALE
export const PRICE_DENOMINATOR = SQRT_PRICE_DENOMINATOR
export const PRICE_DENOMINATOR = _SQRT_PRICE_DENOMINATOR as Price

export const MAX_BATCHES_QUERIED = 18n
export const MAX_POOL_KEYS_QUERIED = 117n
Expand Down
11 changes: 6 additions & 5 deletions src/fungible-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import { Network } from './network'
import { TokenFaucet, Withdraw } from '../artifacts/ts'
import { balanceOf, getNodeUrl, signAndSend, waitTxConfirmed } from './utils'
import { MAX_U256 } from './consts'
import { TokenAmount } from './types'

export type TokenMetaData = {
symbol: string
name: string
decimals: bigint
totalSupply: bigint
totalSupply: TokenAmount
}

export class FungibleToken {
Expand All @@ -32,7 +33,7 @@ export class FungibleToken {

static async deploy(
signer: SignerProvider,
supply: bigint = 0n,
supply = 0n as TokenAmount,
name: string = '',
symbol: string = '',
decimals: bigint = 0n
Expand Down Expand Up @@ -65,7 +66,7 @@ export class FungibleToken {
return new FungibleToken(network)
}

async mintTx(signer: SignerProvider, value: bigint, tokenId: string) {
async mintTx(signer: SignerProvider, value: TokenAmount, tokenId: string) {
const tokenAddress = addressFromContractId(tokenId)
const tokenFaucet = TokenFaucet.at(tokenAddress)
const bytecode = Withdraw.script.buildByteCodeToDeploy({
Expand All @@ -82,7 +83,7 @@ export class FungibleToken {
return unsignedTxBuild
}

async mint(signer: SignerProvider, value: bigint, tokenId: string) {
async mint(signer: SignerProvider, value: TokenAmount, tokenId: string) {
const tx = await this.mintTx(signer, value, tokenId)
return await signAndSend(signer, tx)
}
Expand All @@ -109,7 +110,7 @@ export class FungibleToken {
symbol: hexToString(metadata.symbol),
name: hexToString(metadata.name),
decimals: BigInt(metadata.decimals),
totalSupply: BigInt(metadata.totalSupply)
totalSupply: BigInt(metadata.totalSupply) as TokenAmount
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export {
} from './math'

export type {
SqrtPrice,
Price,
FeeGrowth,
Liquidity,
TokenAmount,
Percentage,
FixedPoint,
Pool,
Position,
FeeTier,
Expand Down
52 changes: 28 additions & 24 deletions src/invariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ import {
decodePosition,
decodeTick,
FeeTier,
Liquidity,
LiquidityTick,
Percentage,
Pool,
PoolKey,
Position,
QuoteResult,
SqrtPrice,
Tick,
Tickmap,
TokenAmount,
unwrapFeeTier,
unwrapPool,
unwrapPoolKey,
Expand Down Expand Up @@ -86,7 +90,7 @@ export class Invariant {
static async deploy(
signer: SignerProvider,
network: Network,
protocolFee: bigint = 0n
protocolFee: Percentage = 0n as Percentage
): Promise<Invariant> {
const account = await signer.getSelectedAccount()
const clamm = await deployCLAMM(signer)
Expand Down Expand Up @@ -153,7 +157,7 @@ export class Invariant {
token0Id: string,
token1Id: string,
feeTier: FeeTier,
initSqrtPrice: bigint
initSqrtPrice: SqrtPrice
) {
const initTick = await calculateTick(initSqrtPrice, feeTier.tickSpacing)
const txBytecode = CreatePool.script.buildByteCodeToDeploy({
Expand All @@ -177,7 +181,7 @@ export class Invariant {
token0Id: string,
token1Id: string,
feeTier: FeeTier,
initSqrtPrice: bigint
initSqrtPrice: SqrtPrice
): Promise<string> {
const tx = await this.createPoolTx(signer, token0Id, token1Id, feeTier, initSqrtPrice)
return await signAndSend(signer, tx)
Expand Down Expand Up @@ -246,11 +250,11 @@ export class Invariant {
poolKey: PoolKey,
lowerTick: bigint,
upperTick: bigint,
liquidityDelta: bigint,
approvedTokensX: bigint,
approvedTokensY: bigint,
slippageLimitLower: bigint,
slippageLimitUpper: bigint
liquidityDelta: Liquidity,
approvedTokensX: TokenAmount,
approvedTokensY: TokenAmount,
slippageLimitLower: SqrtPrice,
slippageLimitUpper: SqrtPrice
) {
const txBytecode = CreatePosition.script.buildByteCodeToDeploy({
invariant: this.instance.contractId,
Expand Down Expand Up @@ -289,11 +293,11 @@ export class Invariant {
poolKey: PoolKey,
lowerTick: bigint,
upperTick: bigint,
liquidityDelta: bigint,
approvedTokensX: bigint,
approvedTokensY: bigint,
slippageLimitLower: bigint,
slippageLimitUpper: bigint
liquidityDelta: Liquidity,
approvedTokensX: TokenAmount,
approvedTokensY: TokenAmount,
slippageLimitLower: SqrtPrice,
slippageLimitUpper: SqrtPrice
): Promise<string> {
const tx = await this.createPositionTx(
signer,
Expand Down Expand Up @@ -371,9 +375,9 @@ export class Invariant {
signer: SignerProvider,
poolKey: PoolKey,
xToY: boolean,
amount: bigint,
amount: TokenAmount,
byAmountIn: boolean,
sqrtPriceLimit: bigint,
sqrtPriceLimit: SqrtPrice,
approvedAmount = amount
) {
const tokenId = xToY ? poolKey.tokenX : poolKey.tokenY
Expand Down Expand Up @@ -402,9 +406,9 @@ export class Invariant {
signer: SignerProvider,
poolKey: PoolKey,
xToY: boolean,
amount: bigint,
amount: TokenAmount,
byAmountIn: boolean,
sqrtPriceLimit: bigint,
sqrtPriceLimit: SqrtPrice,
approvedAmount = amount
): Promise<string> {
const tx = await this.swapTx(
Expand All @@ -423,10 +427,10 @@ export class Invariant {
signer: SignerProvider,
poolKey: PoolKey,
xToY: boolean,
amount: bigint,
amount: TokenAmount,
byAmountIn: boolean,
estimatedSqrtPrice: bigint,
slippage: bigint
estimatedSqrtPrice: SqrtPrice,
slippage: Percentage
) {
const sqrtPriceAfterSlippage = calculateSqrtPriceAfterSlippage(
estimatedSqrtPrice,
Expand All @@ -440,18 +444,18 @@ export class Invariant {
xToY,
amount,
byAmountIn,
xToY ? sqrtPriceAfterSlippage - 1n : sqrtPriceAfterSlippage + 1n
(xToY ? sqrtPriceAfterSlippage - 1n : sqrtPriceAfterSlippage + 1n) as SqrtPrice
)
}

async swapWithSlippage(
signer: SignerProvider,
poolKey: PoolKey,
xToY: boolean,
amount: bigint,
amount: TokenAmount,
byAmountIn: boolean,
estimatedSqrtPrice: bigint,
slippage: bigint
estimatedSqrtPrice: SqrtPrice,
slippage: Percentage
): Promise<string> {
const tx = await this.swapWithSlippageTx(
signer,
Expand Down
Loading

0 comments on commit 790d884

Please sign in to comment.