Skip to content

Commit

Permalink
Merge pull request #24 from invariant-labs/transfer-tokens-swap
Browse files Browse the repository at this point in the history
Transfering tokens with swap
  • Loading branch information
Sniezka1927 authored Feb 25, 2024
2 parents de6d2ae + aeae27e commit ca35b7e
Show file tree
Hide file tree
Showing 14 changed files with 508 additions and 349 deletions.
2 changes: 1 addition & 1 deletion contracts/collections/ticks.ral
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Contract Ticks(tickTemplateContractId: ByteVec) {
enum TicksError {
TickAlreadyExist = 1002
TickAlreadyExist = 500
}

@using(preapprovedAssets = true, checkExternalCaller = false)
Expand Down
19 changes: 16 additions & 3 deletions contracts/invariant.ral
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ Contract Invariant(
return SwapUtils(swapContractId).wrappedQuote(token0, token1, fee, tickSpacing, xToY, amount, byAmountIn, sqrtPriceLimit)
}

@using(updateFields = false, checkExternalCaller = false)
@using(assetsInContract = true, preapprovedAssets = true, checkExternalCaller = false)
pub fn swap(
token0: ByteVec,
token1: ByteVec,
Expand All @@ -324,9 +324,22 @@ Contract Invariant(
byAmountIn: Bool,
sqrtPriceLimit: U256
) -> (U256, U256, U256, U256, U256) {
let caller = callerAddress!()
let poolKey = generatePoolKey(token0, token1, fee, tickSpacing)

let (amountIn, amountOut, startingSqrtPrice, targetSqrtPrice, feeAmount) = SwapUtils(swapContractId).wrappedSwap(token0, token1, fee, tickSpacing, xToY, amount, byAmountIn, sqrtPriceLimit)
// Emit crossed ticks event
// transfer tokens

let tokenXId = Pools(poolsContractId).getTokenX(poolKey)
let tokenYId = Pools(poolsContractId).getTokenY(poolKey)

if (xToY) {
transferTokenToSelf!(caller, tokenXId, amountIn)
transferTokenFromSelf!(caller, tokenYId, amountOut)
} else {
transferTokenFromSelf!(caller, tokenXId, amountOut)
transferTokenToSelf!(caller, tokenYId, amountIn)
}

return amountIn, amountOut, startingSqrtPrice, targetSqrtPrice, feeAmount
}

Expand Down
14 changes: 10 additions & 4 deletions contracts/invariant_tx.ral
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ TxScript CreatePosition(
invariant: Invariant,
token0: ByteVec,
token1: ByteVec,
token0Amount: U256,
token1Amount: U256,
approvedTokens0: U256,
approvedTokens1: U256,
index: U256,
fee: U256,
tickSpacing: U256,
Expand All @@ -41,7 +41,7 @@ TxScript CreatePosition(
slippageLimitLower: U256,
slippageLimitUpper: U256
) {
invariant.createPosition{callerAddress!() -> token0: token0Amount, token1: token1Amount}
invariant.createPosition{callerAddress!() -> token0: approvedTokens0, token1: approvedTokens1}
(index, token0, token1, fee, tickSpacing, lowerTick, upperTick, liquidityDelta, slippageLimitLower, slippageLimitUpper)
}

Expand All @@ -54,5 +54,11 @@ TxScript ClaimFee(invariant: Invariant, index: U256) {
}

TxScript Swap(invariant: Invariant, token0: ByteVec, token1: ByteVec, fee: U256, tickSpacing: U256, xToY: Bool, amount: U256, byAmountIn: Bool, sqrtPriceLimit: U256) {
invariant.swap(token0, token1, fee, tickSpacing, xToY, amount, byAmountIn, sqrtPriceLimit)
let poolKey = invariant.generatePoolKey(token0, token1, fee, tickSpacing)
let (tokenX, tokenY) = invariant.extractTokensFromPoolKey(poolKey)
if(xToY) {
invariant.swap{callerAddress!() -> tokenX: amount}(token0, token1, fee, tickSpacing, xToY, amount, byAmountIn, sqrtPriceLimit)
} else {
invariant.swap{callerAddress!() -> tokenY: amount}(token0, token1, fee, tickSpacing, xToY, amount, byAmountIn, sqrtPriceLimit)
}
}
4 changes: 2 additions & 2 deletions contracts/math/clamm.ral
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Contract CLAMM() extends Log(){
enum CLAMMError {
InvalidTickIndex = 100
InvalidTickSpacing = 101
InvalidTickIndex = 600
InvalidTickSpacing = 601
}

pub fn computeSwapStep(
Expand Down
7 changes: 5 additions & 2 deletions contracts/math/log.ral
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Abstract Contract Log() extends Decimal() {
enum LogError {
SqrtPriceOutOfRange = 7001
}

fn getLog2Scale() -> U256 {
return 32
}
Expand Down Expand Up @@ -106,8 +110,7 @@ Abstract Contract Log() extends Decimal() {
}

pub fn getTickAtSqrtPrice(sqrtPrice: U256, tickSpacing: U256) -> I256 {
// TODO add error enum
// assert!(sqrtPrice <= getMaxSqrtPrice() && sqrtPrice >= getMinSqrtPrice(), "sqrt price out of range")
assert!(sqrtPrice <= getMaxSqrtPrice() && sqrtPrice >= getMinSqrtPrice(), LogError.SqrtPriceOutOfRange)
let sqrtPriceX32 = sqrtPriceToX32(sqrtPrice)

let (log2Sign, log2SqrtPrice) = log2IterativeApproximationX32(sqrtPriceX32)
Expand Down
2 changes: 1 addition & 1 deletion contracts/storage/fee_tier.ral
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Contract FeeTier(admin: Address, fee: U256, tickSpacing: U256, mut isActive: Bool) {
enum FeeTierError {
NotAdmin = 200
NotAdmin = 800
}

pub fn getFee() -> U256 {
Expand Down
4 changes: 2 additions & 2 deletions contracts/storage/position.ral
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Contract Position(
mut posIsActive: Bool
) extends Decimal() {
enum PositionError {
InsufficientLiquidity = 700
EmptyPositionPokes = 701
InsufficientLiquidity = 900
EmptyPositionPokes = 901
}

pub fn getPoolKey() -> ByteVec {
Expand Down
2 changes: 1 addition & 1 deletion contracts/storage/tick.ral
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Contract Tick(
tickSecondsOutside: U256
) extends Decimal() {
enum TickError {
InvalidTickLiquidity = 800
InvalidTickLiquidity = 1000
}

pub fn getSign() -> Bool {
Expand Down
2 changes: 0 additions & 2 deletions contracts/swap.ral
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ Contract SwapUtils(
let currentTimestamp = blockTimeStamp!()

assert!(amount != 0, SwapError.ZeroAmount)

// let mut ticks =

let mut currentSqrtPrice = Pools(poolsContractId).getCurrentSqrtPrice(poolKey)

Expand Down
4 changes: 2 additions & 2 deletions contracts/utils.ral
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Abstract Contract Utils() {
assert!(fee <= (10 ** 12), 5001)

for (let mut i = 0; i <= size!(token0); i = i + 1) {
if (u256From1Byte!(byteVecSlice!(token0, i, i + 1)) > u256From1Byte!(byteVecSlice!(token1, i, i + 1))) {
if (u256From1Byte!(byteVecSlice!(token0, i, i + 1)) < u256From1Byte!(byteVecSlice!(token1, i, i + 1))) {
return token0 ++ token1 ++ toByteVec!(fee) ++ toByteVec!(tickSpacing)
}

if (u256From1Byte!(byteVecSlice!(token0, i , i + 1)) < u256From1Byte!(byteVecSlice!(token1, i , i + 1))) {
if (u256From1Byte!(byteVecSlice!(token0, i , i + 1)) > u256From1Byte!(byteVecSlice!(token1, i , i + 1))) {
return token1 ++ token0 ++ toByteVec!(fee) ++ toByteVec!(tickSpacing)
}
}
Expand Down
Loading

0 comments on commit ca35b7e

Please sign in to comment.