Skip to content

Commit

Permalink
Non async SDK math/utils (#126)
Browse files Browse the repository at this point in the history
* non async sdk math/utils
  • Loading branch information
DevRozaDev authored Aug 30, 2024
1 parent 790d884 commit a045cd8
Show file tree
Hide file tree
Showing 45 changed files with 1,591 additions and 1,098 deletions.
170 changes: 0 additions & 170 deletions contracts/math/utils.ral

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions scripts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ const main = async () => {
// load invariant contract
const invariant = await Invariant.load(INVARIANT_ADDRESS, Network.Local)
// load token contract
const token = await FungibleToken.load(Network.Local)
const token = FungibleToken.load(Network.Local)

// set fee tier, make sure that fee tier with specified parameters exists
const feeTier = await newFeeTier(toPercentage(1n, 2n), 1n) // fee: 0.01 = 1%, tick spacing: 1
const feeTier = newFeeTier(toPercentage(1n, 2n), 1n) // fee: 0.01 = 1%, tick spacing: 1

// if the fee tier does not exists, you have to add it
const isAdded = await invariant.feeTierExist(feeTier)
Expand All @@ -60,7 +60,7 @@ const main = async () => {
const initSqrtPrice = priceToSqrtPrice(price)

// set pool key, make sure that pool for these tokens does not exist already
const poolKey = await newPoolKey(TOKEN0_ID, TOKEN1_ID, feeTier)
const poolKey = newPoolKey(TOKEN0_ID, TOKEN1_ID, feeTier)

const createPoolTransactionId = await invariant.createPool(
account,
Expand All @@ -80,7 +80,7 @@ const main = async () => {
const [lowerTickIndex, upperTickIndex] = [-10n, 10n]

// calculate the amount of token x we need to open position
const { amount: tokenXAmount, l: positionLiquidity } = await getLiquidityByY(
const { amount: tokenXAmount, l: positionLiquidity } = getLiquidityByY(
tokenYAmount,
lowerTickIndex,
upperTickIndex,
Expand Down Expand Up @@ -140,7 +140,13 @@ const main = async () => {
// get estimated result of swap - there are 2 ways to do it
// 1. use the quote method
// due to it being computed using blockchain, thus having a latency and being subjected to gas limit, we recommend the second method
// const quoteResult = await invariant.quote(poolKey, true, amount, true, await getMinSqrtPrice(feeTier.tickSpacing))
const quoteResult = await invariant.quote(
poolKey,
true,
amount,
true,
getMinSqrtPrice(feeTier.tickSpacing)
)

// 2. use local simulation of a swap [PREFERRED]
{
Expand All @@ -149,7 +155,7 @@ const main = async () => {

// filtering only serves to reduce the amount of ticks we have to simulate, it is not necessary
// filter tickmap to only have ticks of interest for our swap
const tickmap = await filterTickmap(
const tickmap = filterTickmap(
await invariant.getFullTickmap(poolKey),
poolKey.feeTier.tickSpacing,
pool.currentTickIndex,
Expand All @@ -171,7 +177,7 @@ const main = async () => {
true,
amount,
true,
await getMinSqrtPrice(feeTier.tickSpacing)
getMinSqrtPrice(feeTier.tickSpacing)
)

// you can now use the result of the simulation to make a decision whether to swap or not
Expand Down Expand Up @@ -223,7 +229,7 @@ const main = async () => {
const upperTick: Tick = await invariant.getTick(poolKey, position.upperTickIndex)

// check amount of tokens you are able to claim
const fees = await calculateFee(pool, position, lowerTick, upperTick)
const fees = calculateFee(pool, position, lowerTick, upperTick)

// print amount of unclaimed x and y tokens
console.log(fees)
Expand Down Expand Up @@ -283,7 +289,7 @@ const usingAlphAsToken = async () => {
// ALPH just like any other token has a Contract Id (Token Id), so it can be used in the same way

// load token contract
const token = await FungibleToken.load(Network.Local)
const token = FungibleToken.load(Network.Local)

// get balance of account
const accountBalance = await token.getBalanceOf(account.address, ALPH_TOKEN_ID)
Expand All @@ -297,7 +303,7 @@ const usingFungibleToken = async () => {
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)
const token = FungibleToken.load(Network.Local)

// interact with token 0
const account0Balance = await token.getBalanceOf(account.address, TOKEN0_ID)
Expand Down
2 changes: 1 addition & 1 deletion src/fungible-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class FungibleToken {
return deployResult.contractInstance.contractId
}

static async load(network: Network): Promise<FungibleToken> {
static load(network: Network): FungibleToken {
return new FungibleToken(network)
}

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
getMinTick,
getMaxTick,
toFeeGrowth,
toFixedPoint,
toLiquidity,
toPercentage,
toPrice,
Expand Down Expand Up @@ -70,6 +71,7 @@ export {
GLOBAL_MIN_TICK,
SEARCH_RANGE,
InvariantError,
CHUNK_SIZE,
MAX_FEE_TIERS,
MAX_SWAP_STEPS,
MAX_U256
Expand Down
7 changes: 3 additions & 4 deletions src/invariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
TokenAmount,
unwrapFeeTier,
unwrapPool,
unwrapPoolKey,
unwrapPosition,
unwrapQuoteResult,
unwrapTick,
Expand Down Expand Up @@ -159,7 +158,7 @@ export class Invariant {
feeTier: FeeTier,
initSqrtPrice: SqrtPrice
) {
const initTick = await calculateTick(initSqrtPrice, feeTier.tickSpacing)
const initTick = calculateTick(initSqrtPrice, feeTier.tickSpacing)
const txBytecode = CreatePool.script.buildByteCodeToDeploy({
invariant: this.instance.contractId,
token0: token0Id,
Expand Down Expand Up @@ -648,7 +647,7 @@ export class Invariant {

async getFullTickmap(poolKey: PoolKey): Promise<Tickmap> {
const promises: Promise<[bigint, bigint][]>[] = []
const maxBatch = await getMaxBatch(poolKey.feeTier.tickSpacing)
const maxBatch = getMaxBatch(poolKey.feeTier.tickSpacing)
let currentBatch = 0n

while (currentBatch <= maxBatch) {
Expand Down Expand Up @@ -677,7 +676,7 @@ export class Invariant {
for (let bit = 0n; bit < CHUNK_SIZE; bit++) {
const checkedBit = chunk & (1n << bit)
if (checkedBit) {
const tickIndex = await bitPositionToTick(chunkIndex, bit, poolKey.feeTier.tickSpacing)
const tickIndex = bitPositionToTick(chunkIndex, bit, poolKey.feeTier.tickSpacing)
tickIndexes.push(tickIndex)
}
}
Expand Down
Loading

0 comments on commit a045cd8

Please sign in to comment.