Skip to content

Commit

Permalink
Merge pull request #21 from invariant-labs/refactor-positions
Browse files Browse the repository at this point in the history
Refactor positions
  • Loading branch information
zielvna authored Feb 25, 2024
2 parents fec4b12 + 13dbdaf commit de6d2ae
Show file tree
Hide file tree
Showing 20 changed files with 1,054 additions and 279 deletions.
6 changes: 3 additions & 3 deletions contracts/collections/pool_keys.ral
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Contract PoolKeys(poolKeyTemplateContractId: ByteVec, mut poolKeyCount: U256) {
}

@using(preapprovedAssets = true, updateFields = true, checkExternalCaller = false)
pub fn add(originalCaller: Address, tokenX: Address, tokenY: Address, fee: U256, tickSpacing: U256) -> () {
pub fn add(originalCaller: Address, tokenX: ByteVec, tokenY: ByteVec, fee: U256, tickSpacing: U256) -> () {
// assert!(callerAddress!() == admin, PoolKeysError.NotAdmin)

for (let mut i = 1; i <= poolKeyCount; i = i + 1) {
Expand All @@ -24,7 +24,7 @@ Contract PoolKeys(poolKeyTemplateContractId: ByteVec, mut poolKeyCount: U256) {
)
}

pub fn contains(tokenX: Address, tokenY: Address, fee: U256, tickSpacing: U256) -> Bool {
pub fn contains(tokenX: ByteVec, tokenY: ByteVec, fee: U256, tickSpacing: U256) -> Bool {
for (let mut i = 1; i <= poolKeyCount; i = i + 1) {
let contract = PoolKey(subContractId!(toByteVec!(i)))

Expand All @@ -41,7 +41,7 @@ Contract PoolKeys(poolKeyTemplateContractId: ByteVec, mut poolKeyCount: U256) {

for (let mut i = 1; i <= poolKeyCount; i = i + 1) {
let contract = PoolKey(subContractId!(toByteVec!(i)))
poolsBytes = poolsBytes ++ toByteVec!(contract.getTokenX()) ++ b`break` ++ toByteVec!(contract.getTokenY()) ++ b`break` ++ toByteVec!(contract.getFee()) ++ b`break` ++ toByteVec!(contract.getTickSpacing()) ++ b`break`
poolsBytes = poolsBytes ++ contract.getTokenX() ++ b`break` ++ contract.getTokenY() ++ b`break` ++ toByteVec!(contract.getFee()) ++ b`break` ++ toByteVec!(contract.getTickSpacing()) ++ b`break`
}

return poolsBytes
Expand Down
16 changes: 14 additions & 2 deletions contracts/collections/pools.ral
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Contract Pools(poolTemplateContractId: ByteVec, clammContractId: ByteVec) extend
}

@using(preapprovedAssets = true, checkExternalCaller = false)
pub fn add(originalCaller: Address, poolKey: ByteVec, initSqrtPrice: U256, initTick: I256, currentTimestamp: U256, feeReceiver: Address) -> () {
pub fn add(originalCaller: Address, poolKey: ByteVec, tokenX: ByteVec, tokenY: ByteVec, tickSpacing: U256, initSqrtPrice: U256, initTick: I256, currentTimestamp: U256, feeReceiver: Address) -> () {

assert!(!contractExists!(subContractId!(poolKey)), PoolsError.PoolAlreadyExist)

let (encodedImmFields, encodedMutFields) = Pool.encodeFields!(0, initSqrtPrice, initTick, 0, 0, 0, 0, currentTimestamp, currentTimestamp, feeReceiver, clammContractId)
let (encodedImmFields, encodedMutFields) = Pool.encodeFields!(tokenX, tokenY, tickSpacing, 0, initSqrtPrice, initTick, 0, 0, 0, 0, currentTimestamp, currentTimestamp, feeReceiver, clammContractId)
copyCreateSubContract!{originalCaller -> ALPH: 1 alph}(
poolKey,
poolTemplateContractId,
Expand Down Expand Up @@ -38,6 +38,18 @@ Contract Pools(poolTemplateContractId: ByteVec, clammContractId: ByteVec) extend
return Pool(subContractId!(poolKey)).getStartTimestamp()
}

pub fn getTokenX(poolKey: ByteVec) -> ByteVec {
return Pool(subContractId!(poolKey)).getTokenX()
}

pub fn getTokenY(poolKey: ByteVec) -> ByteVec {
return Pool(subContractId!(poolKey)).getTokenY()
}

pub fn getTickSpacing(poolKey: ByteVec) -> U256 {
return Pool(subContractId!(poolKey)).getTickSpacing()
}

@using(checkExternalCaller = false)
pub fn wrappedAddFee(poolKey: ByteVec, feeAmount: U256, xToY: Bool, fee: U256) -> () {
Pool(subContractId!(poolKey)).addFee(feeAmount, xToY, fee)
Expand Down
106 changes: 76 additions & 30 deletions contracts/collections/positions.ral
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
Contract Positions(positionTemplateContractId: ByteVec, positionsCounterTemplateId: ByteVec) {
Contract Positions(positionTemplateContractId: ByteVec, positionsCounterContractId: ByteVec) {
enum PositionsError {
NotOwner = 500
PositionNotExist = 501
}

@using(preapprovedAssets = true, checkExternalCaller = false)
pub fn add(
caller: Address,
poolKey: ByteVec,
liquidity: U256,
lowerTickIndex: I256,
upperTickIndex: I256,
Expand All @@ -11,63 +17,104 @@ Contract Positions(positionTemplateContractId: ByteVec, positionsCounterTemplate
tokensOwedX: U256,
tokensOwedY: U256
) -> () {
let length = getLength(caller)

if (length == 0) {
let (positionsCounterEncodedImmFields, positionsCounterEncodedMutFields) = PositionsCounter.encodeFields!(1)
copyCreateSubContract!{caller -> ALPH: 1 alph}(
toByteVec!(caller),
positionsCounterTemplateId,
positionsCounterEncodedImmFields,
positionsCounterEncodedMutFields
)
} else {
let contract = PositionsCounter(subContractId!(toByteVec!(caller)))
contract.set(contract.get() + 1)
}
let positionsCounter = PositionsCounter(positionsCounterContractId)
let length = positionsCounter.get()
positionsCounter.set(length + 1)

let (encodedImmFields, encodedMutFields) = Position.encodeFields!(
poolKey,
liquidity,
lowerTickIndex,
upperTickIndex,
feeGrowthInsideX,
feeGrowthInsideY,
lastBlockNumber,
tokensOwedX,
tokensOwedY
tokensOwedY,
caller,
true
)
copyCreateSubContract!{caller -> ALPH: 1 alph}(
toByteVec!(caller) ++ toByteVec!(length + 1),
toByteVec!(length + 1),
positionTemplateContractId,
encodedImmFields,
encodedMutFields
)
}

fn getLength(owner: Address) -> U256 {
let subContractId = subContractId!(toByteVec!(owner))
@using(checkExternalCaller = false)
pub fn remove(caller: Address, index: U256) -> () {
let subContractId = subContractId!(toByteVec!(index))
assert!(contractExists!(subContractId), PositionsError.PositionNotExist)
let position = Position(subContractId)
assert!(position.getOwner() == caller, PositionsError.NotOwner)
position.setIsActive(false)
}

if(contractExists!(subContractId)) {
return PositionsCounter(subContractId).get()
} else {
return 0
}
@using(checkExternalCaller = false)
pub fn transfer(caller: Address, index: U256, newOwner: Address) -> () {
let subContractId = subContractId!(toByteVec!(index))
assert!(contractExists!(subContractId), PositionsError.PositionNotExist)
let position = Position(subContractId)
assert!(position.getOwner() == caller, PositionsError.NotOwner)
position.setOwner(newOwner)
}

pub fn getPoolKey(index: U256) -> ByteVec {
let subContractId = subContractId!(toByteVec!(index))
return Position(subContractId).getPoolKey()
}

pub fn getLowerTickIndex(index: U256) -> I256 {
let subContractId = subContractId!(toByteVec!(index))
return Position(subContractId).getLowerTickIndex()
}

pub fn getUpperTickIndex(index: U256) -> I256 {
let subContractId = subContractId!(toByteVec!(index))
return Position(subContractId).getUpperTickIndex()
}

pub fn getLiquidity(index: U256) -> U256 {
let subContractId = subContractId!(toByteVec!(index))
return Position(subContractId).getLiquidity()
}

pub fn getTokensOwedX(index: U256) -> U256 {
let subContractId = subContractId!(toByteVec!(index))
return Position(subContractId).getTokensOwedX()
}

pub fn getTokensOwedY(index: U256) -> U256 {
let subContractId = subContractId!(toByteVec!(index))
return Position(subContractId).getTokensOwedY()
}

@using(checkExternalCaller = false)
pub fn setTokensOwedX(index: U256, value: U256) -> () {
let subContractId = subContractId!(toByteVec!(index))
Position(subContractId).setTokensOwedX(value)
}

@using(checkExternalCaller = false)
pub fn setTokensOwedY(index: U256, value: U256) -> () {
let subContractId = subContractId!(toByteVec!(index))
Position(subContractId).setTokensOwedY(value)
}

@using(checkExternalCaller = false)
pub fn update(
owner: Address,
index: U256,
sign: Bool,
liquidityDelta: U256,
feeGrowthInsideX: U256,
feeGrowthInsideY: U256
) -> () {
Position(subContractId!(toByteVec!(owner) ++ toByteVec!(index))).update(sign, liquidityDelta, feeGrowthInsideX, feeGrowthInsideY)
Position(subContractId!(toByteVec!(index))).update(sign, liquidityDelta, feeGrowthInsideX, feeGrowthInsideY)
}

pub fn get(owner: Address, index: U256) -> (Bool, U256, I256, I256, U256, U256, U256, U256, U256) {
let subContractId = subContractId!(toByteVec!(owner) ++ toByteVec!(index))
pub fn get(index: U256) -> (Bool, U256, I256, I256, U256, U256, U256, U256, U256) {
let subContractId = subContractId!(toByteVec!(index))

if (!contractExists!(subContractId)) {
return false, 0, 0i, 0i, 0, 0, 0, 0, 0
Expand All @@ -78,7 +125,6 @@ Contract Positions(positionTemplateContractId: ByteVec, positionsCounterTemplate

@using(checkExternalCaller = false)
pub fn wrappedModify(
caller: Address,
index: U256,
poolsContractId: ByteVec,
ticksContractId: ByteVec,
Expand All @@ -91,6 +137,6 @@ Contract Positions(positionTemplateContractId: ByteVec, positionsCounterTemplate
currentTimestamp: U256,
tickSpacing: U256
) -> (U256, U256) {
return Position(subContractId!(toByteVec!(caller) ++ toByteVec!(index))).modify(poolsContractId, ticksContractId, clammContractId, poolKey, upperTick, lowerTick, liquidityDelta, add, currentTimestamp, tickSpacing)
return Position(subContractId!(toByteVec!(index))).modify(poolsContractId, ticksContractId, clammContractId, poolKey, upperTick, lowerTick, liquidityDelta, add, currentTimestamp, tickSpacing)
}
}
Loading

0 comments on commit de6d2ae

Please sign in to comment.