Skip to content

Commit

Permalink
Merge pull request #26 from invariant-labs/admin-checks
Browse files Browse the repository at this point in the history
Added admin checks
  • Loading branch information
Sniezka1927 authored Feb 26, 2024
2 parents ca35b7e + 03226db commit 9742dfc
Show file tree
Hide file tree
Showing 21 changed files with 302 additions and 187 deletions.
17 changes: 15 additions & 2 deletions contracts/collections/fee_tiers.ral
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
Contract FeeTiers(feeTierTemplateContractId: ByteVec, mut feeTierCount: U256) {
Contract FeeTiers(feeTierTemplateContractId: ByteVec, mut feeTierCount: U256, mut invariantId: ByteVec, mut areAdminsSet: Bool) {
enum FeeTiersError {
FeeTierAlreadyExist = 101
FeeTierNotFound = 102
NotAdmin = 103
AdminsAreSet = 104
}

@using(updateFields = true, checkExternalCaller = false)
pub fn setAdmins(invtId: ByteVec) -> () {
assert!(!areAdminsSet, FeeTiersError.AdminsAreSet)
invariantId = invtId
areAdminsSet = true
}

@using(preapprovedAssets = true, updateFields = true, checkExternalCaller = false)
pub fn add(originalCaller: Address, fee: U256, tickSpacing: U256) -> () {
let callerId = toByteVec!(callerAddress!())
assert!(callerId == invariantId && areAdminsSet, FeeTiersError.NotAdmin)

let exists = contains(fee, tickSpacing)

assert!(!exists, FeeTiersError.FeeTierAlreadyExist)
Expand All @@ -22,7 +34,8 @@ Contract FeeTiers(feeTierTemplateContractId: ByteVec, mut feeTierCount: U256) {

@using(checkExternalCaller = false)
pub fn remove(fee: U256, tickSpacing: U256) -> () {
// assert!(callerAddress!() == admin, FeeTiersError.NotAdmin)
let callerId = toByteVec!(callerAddress!())
assert!(callerId == invariantId && areAdminsSet, FeeTiersError.NotAdmin)

for (let mut i = 1; i <= feeTierCount; i = i + 1) {
let contract = FeeTier(subContractId!(toByteVec!(i)))
Expand Down
19 changes: 16 additions & 3 deletions contracts/collections/pool_keys.ral
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
Contract PoolKeys(poolKeyTemplateContractId: ByteVec, mut poolKeyCount: U256) {
Contract PoolKeys(poolKeyTemplateContractId: ByteVec, mut poolKeyCount: U256, mut invariantId: ByteVec, mut areAdminsSet: Bool) {
enum PoolKeysError {
// NotAdmin = 200
NotAdmin = 200
PoolKeyAlreadyExist = 201
AdminsAreSet = 202
}


@using(updateFields = true, checkExternalCaller = false)
pub fn setAdmins(invtId: ByteVec) -> () {
assert!(!areAdminsSet, PoolKeysError.AdminsAreSet)
invariantId = invtId
areAdminsSet = true
}

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

for (let mut i = 1; i <= poolKeyCount; i = i + 1) {
let contract = PoolKey(subContractId!(toByteVec!(i)))
Expand All @@ -25,6 +35,9 @@ Contract PoolKeys(poolKeyTemplateContractId: ByteVec, mut poolKeyCount: U256) {
}

pub fn contains(tokenX: ByteVec, tokenY: ByteVec, fee: U256, tickSpacing: U256) -> Bool {
let callerId = toByteVec!(callerAddress!())
assert!(callerId == invariantId && areAdminsSet, PoolKeysError.NotAdmin)

for (let mut i = 1; i <= poolKeyCount; i = i + 1) {
let contract = PoolKey(subContractId!(toByteVec!(i)))

Expand Down
39 changes: 38 additions & 1 deletion contracts/collections/pools.ral
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
Contract Pools(poolTemplateContractId: ByteVec, clammContractId: ByteVec) extends Utils() {
Contract Pools(
poolTemplateContractId: ByteVec,
clammContractId: ByteVec,
mut invariantId: ByteVec,
mut positionsId: ByteVec,
mut swapUtilsId: ByteVec,
mut areAdminsSet: Bool
) extends Utils() {
enum PoolsError {
PoolAlreadyExist = 301
NotAdmin = 302
AdminsAreSet = 303
}

@using(updateFields = true, checkExternalCaller = false)
pub fn setAdmins(invariant: ByteVec, positions: ByteVec, swapUtils: ByteVec) -> () {
assert!(!areAdminsSet, PoolsError.AdminsAreSet)
invariantId = invariant
positionsId = positions
swapUtilsId = swapUtils
areAdminsSet = true
}


@using(preapprovedAssets = true, checkExternalCaller = false)
pub fn add(originalCaller: Address, poolKey: ByteVec, tokenX: ByteVec, tokenY: ByteVec, tickSpacing: U256, initSqrtPrice: U256, initTick: I256, currentTimestamp: U256, feeReceiver: Address) -> () {
let callerId = toByteVec!(callerAddress!())
assert!((callerId == invariantId || callerId == positionsId || callerId == swapUtilsId) && areAdminsSet, PoolsError.NotAdmin)

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

Expand Down Expand Up @@ -52,6 +73,9 @@ Contract Pools(poolTemplateContractId: ByteVec, clammContractId: ByteVec) extend

@using(checkExternalCaller = false)
pub fn wrappedAddFee(poolKey: ByteVec, feeAmount: U256, xToY: Bool, fee: U256) -> () {
let callerId = toByteVec!(callerAddress!())
assert!((callerId == invariantId || callerId == positionsId || callerId == swapUtilsId) && areAdminsSet, PoolsError.NotAdmin)

Pool(subContractId!(poolKey)).addFee(feeAmount, xToY, fee)
}

Expand All @@ -78,6 +102,9 @@ Contract Pools(poolTemplateContractId: ByteVec, clammContractId: ByteVec) extend
fee: U256,
tickSpacing: U256
) -> (U256, U256, Bool) {
let callerId = toByteVec!(callerAddress!())
assert!((callerId == invariantId || callerId == positionsId || callerId == swapUtilsId) && areAdminsSet, PoolsError.NotAdmin)

return Pool(subContractId!(poolKey)).crossTick(
nextSqrtPrice,
swapLimit,
Expand All @@ -97,16 +124,26 @@ Contract Pools(poolTemplateContractId: ByteVec, clammContractId: ByteVec) extend
add: Bool,
liquidityDelta: U256
) -> () {
let callerId = toByteVec!(callerAddress!())
assert!((callerId == invariantId || callerId == positionsId || callerId == swapUtilsId) && areAdminsSet, PoolsError.NotAdmin)

return Pool(subContractId!(poolKey)).crossLiquidityUpdate(add, liquidityDelta)
}

@using(checkExternalCaller = false)
pub fn setLastTimestamp(poolKey: ByteVec, timestamp: U256) -> () {
let callerId = toByteVec!(callerAddress!())
assert!((callerId == invariantId || callerId == positionsId || callerId == swapUtilsId) && areAdminsSet, PoolsError.NotAdmin)

Pool(subContractId!(poolKey)).setLastTimestamp(timestamp)
}

@using(checkExternalCaller = false)
pub fn updateLiquidity(poolKey: ByteVec, liquidityDelta: U256, liquiditySign: Bool, upperTick: I256, lowerTick: I256) -> (U256, U256) {
let callerId = toByteVec!(callerAddress!())
assert!((callerId == invariantId || callerId == positionsId || callerId == swapUtilsId) && areAdminsSet, PoolsError.NotAdmin)


return Pool(subContractId!(poolKey)).updateLiquidity(liquidityDelta, liquiditySign, upperTick, lowerTick)
}

Expand Down
71 changes: 69 additions & 2 deletions contracts/collections/positions.ral
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
Contract Positions(positionTemplateContractId: ByteVec, positionsCounterContractId: ByteVec) {
Contract Positions(positionTemplateContractId: ByteVec, positionsCounterContractId: ByteVec, mut invariantId: ByteVec, mut areAdminsSet: Bool) {
enum PositionsError {
NotOwner = 500
PositionNotExist = 501
NotAdmin = 502
AdminsAreSet = 503
}

@using(updateFields = true, checkExternalCaller = false)
pub fn setAdmins(invariant: ByteVec) -> () {
assert!(!areAdminsSet, PositionsError.AdminsAreSet)
invariantId = invariant
PositionsCounter(positionsCounterContractId).setAdmins(toByteVec!(selfAddress!()))
areAdminsSet = true
}

@using(checkExternalCaller = false)
pub fn getSelfId() -> ByteVec {
return toByteVec!(selfAddress!())
}

@using(preapprovedAssets = true, checkExternalCaller = false)
Expand All @@ -17,6 +32,10 @@ Contract Positions(positionTemplateContractId: ByteVec, positionsCounterContract
tokensOwedX: U256,
tokensOwedY: U256
) -> () {
let callerId = toByteVec!(callerAddress!())
assert!(callerId == invariantId && areAdminsSet, PositionsError.NotAdmin)


let positionsCounter = PositionsCounter(positionsCounterContractId)
let length = positionsCounter.get()
positionsCounter.set(length + 1)
Expand Down Expand Up @@ -44,6 +63,9 @@ Contract Positions(positionTemplateContractId: ByteVec, positionsCounterContract

@using(checkExternalCaller = false)
pub fn remove(caller: Address, index: U256) -> () {
let callerId = toByteVec!(callerAddress!())
assert!(callerId == invariantId && areAdminsSet, PositionsError.NotAdmin)

let subContractId = subContractId!(toByteVec!(index))
assert!(contractExists!(subContractId), PositionsError.PositionNotExist)
let position = Position(subContractId)
Expand All @@ -53,6 +75,9 @@ Contract Positions(positionTemplateContractId: ByteVec, positionsCounterContract

@using(checkExternalCaller = false)
pub fn transfer(caller: Address, index: U256, newOwner: Address) -> () {
let callerId = toByteVec!(callerAddress!())
assert!(callerId == invariantId && areAdminsSet, PositionsError.NotAdmin)

let subContractId = subContractId!(toByteVec!(index))
assert!(contractExists!(subContractId), PositionsError.PositionNotExist)
let position = Position(subContractId)
Expand Down Expand Up @@ -92,12 +117,18 @@ Contract Positions(positionTemplateContractId: ByteVec, positionsCounterContract

@using(checkExternalCaller = false)
pub fn setTokensOwedX(index: U256, value: U256) -> () {
let callerId = toByteVec!(callerAddress!())
assert!(callerId == invariantId && areAdminsSet, PositionsError.NotAdmin)

let subContractId = subContractId!(toByteVec!(index))
Position(subContractId).setTokensOwedX(value)
}

@using(checkExternalCaller = false)
pub fn setTokensOwedY(index: U256, value: U256) -> () {
let callerId = toByteVec!(callerAddress!())
assert!(callerId == invariantId && areAdminsSet, PositionsError.NotAdmin)

let subContractId = subContractId!(toByteVec!(index))
Position(subContractId).setTokensOwedY(value)
}
Expand All @@ -110,6 +141,9 @@ Contract Positions(positionTemplateContractId: ByteVec, positionsCounterContract
feeGrowthInsideX: U256,
feeGrowthInsideY: U256
) -> () {
let callerId = toByteVec!(callerAddress!())
assert!(callerId == invariantId && areAdminsSet, PositionsError.NotAdmin)

Position(subContractId!(toByteVec!(index))).update(sign, liquidityDelta, feeGrowthInsideX, feeGrowthInsideY)
}

Expand Down Expand Up @@ -137,6 +171,39 @@ Contract Positions(positionTemplateContractId: ByteVec, positionsCounterContract
currentTimestamp: U256,
tickSpacing: U256
) -> (U256, U256) {
return Position(subContractId!(toByteVec!(index))).modify(poolsContractId, ticksContractId, clammContractId, poolKey, upperTick, lowerTick, liquidityDelta, add, currentTimestamp, tickSpacing)
let callerId = toByteVec!(callerAddress!())
assert!(callerId == invariantId && areAdminsSet, PositionsError.NotAdmin)

let pools = Pools(poolsContractId)
pools.setLastTimestamp(poolKey, currentTimestamp)

let maxLiquidityPerTick = CLAMM(clammContractId).calculateMaxLiquidityPerTick(tickSpacing)

let ticks = Ticks(ticksContractId)

ticks.updateTick(poolKey, lowerTick, liquidityDelta, maxLiquidityPerTick, false, add)
ticks.updateTick(poolKey, upperTick, liquidityDelta, maxLiquidityPerTick, true, add)

let (lowerTickFeeGrowthOutsideX, lowerTickFeeGrowthOutsideY) = ticks.getFeeGrowthOutside(poolKey, lowerTick)
let (upperTickFeeGrowthOutsideX, upperTickFeeGrowthOutsideY) = ticks.getFeeGrowthOutside(poolKey, upperTick)
let poolCurrentTickIndex = pools.getCurrentTickIndex(poolKey)
let (poolFeeGrowthGlobalX, poolFeeGrowthGlobalY) = pools.getFeeGrowthGlobal(poolKey)

Position(subContractId!(toByteVec!(index))).modify(
clammContractId,
upperTick,
upperTickFeeGrowthOutsideX,
upperTickFeeGrowthOutsideY,
lowerTick,
lowerTickFeeGrowthOutsideX,
lowerTickFeeGrowthOutsideY,
poolCurrentTickIndex,
poolFeeGrowthGlobalX,
poolFeeGrowthGlobalY,
liquidityDelta,
add
)

return pools.updateLiquidity(poolKey, liquidityDelta, add, upperTick, lowerTick)
}
}
16 changes: 15 additions & 1 deletion contracts/collections/positions_counter.ral
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
Contract PositionsCounter(mut value: U256) {
Contract PositionsCounter(mut value: U256, mut positionsId: ByteVec, mut areAdminsSet: Bool) {
enum PositionCounterError {
NotAdmin = 400
AdminsAreSet = 401
}

pub fn get() -> U256 {
return value
}

@using(updateFields = true, checkExternalCaller = false)
pub fn setAdmins(positions: ByteVec) -> () {
assert!(!areAdminsSet, PositionCounterError.AdminsAreSet)
positionsId = positions
areAdminsSet = true
}

@using(updateFields = true, checkExternalCaller = false)
pub fn set(updatedValue: U256) -> () {
let callerId = toByteVec!(callerAddress!())
assert!(callerId == positionsId && areAdminsSet, PositionCounterError.NotAdmin)
value = updatedValue
}
}
40 changes: 27 additions & 13 deletions contracts/collections/tickmap.ral
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
Contract Tickmap(
admin: Address,
chunkTemplateContractId: ByteVec,
clammContractId: ByteVec
clammContractId: ByteVec,
mut invariantId: ByteVec,
mut swapUtilsId: ByteVec,
mut areAdminsSet: Bool
) {
enum TickmapErrors {
// NotAdmin = 400
InvalidTickIndex = 401
TickAndTickSpacingMismatch = 402
LimitReached = 403
ChunkNotFound = 404
TickInitialized = 405
NotAdmin = 600
InvalidTickIndex = 601
TickAndTickSpacingMismatch = 602
LimitReached = 603
ChunkNotFound = 604
TickInitialized = 605
AdminsAreSet = 606
}

pub fn placeholder() -> () {
assert!(callerAddress!() == admin, 10003)
@using(updateFields = true, checkExternalCaller = false)
pub fn setAdmins(invariant: ByteVec, swapUtils: ByteVec) -> () {
assert!(!areAdminsSet, TickmapErrors.AdminsAreSet)
invariantId = invariant
swapUtilsId = swapUtils
areAdminsSet = true
}

fn getChunkSize() -> U256 {
Expand Down Expand Up @@ -73,7 +80,7 @@ Contract Tickmap(
return limit * toI256!(tickSpacing)
}

pub fn nextInitialized(tick: I256, tickSpacing: U256, poolKey: ByteVec) -> (Bool, I256) {
fn nextInitialized(tick: I256, tickSpacing: U256, poolKey: ByteVec) -> (Bool, I256) {
let limit = getSearchLimit(tick, tickSpacing, true)

if (tick + toI256!(tickSpacing) > CLAMM(clammContractId).getGlobalMaxTick()) {
Expand Down Expand Up @@ -108,7 +115,7 @@ Contract Tickmap(
return false, 0i
}

pub fn prevInitialized(tick: I256, tickSpacing: U256, poolKey: ByteVec) -> (Bool, I256) {
fn prevInitialized(tick: I256, tickSpacing: U256, poolKey: ByteVec) -> (Bool, I256) {
let limit = getSearchLimit(tick, tickSpacing, false)
let (mut chunk, mut bit) = tickToPosition(tick, tickSpacing)
let (limitingChunk, limitingBit) = tickToPosition(limit, tickSpacing)
Expand Down Expand Up @@ -139,6 +146,9 @@ Contract Tickmap(
}

pub fn getCloserLimit(sqrtPriceLimit: U256, xToY: Bool, currentTick: I256, tickSpacing: U256, poolKey: ByteVec) -> (U256, Bool, I256, Bool) {
let callerId = toByteVec!(callerAddress!())
assert!((callerId == invariantId || callerId == swapUtilsId) && areAdminsSet, TickmapErrors.NotAdmin)

let mut closesTickBool = false
let mut closesTickIndex = 0i

Expand Down Expand Up @@ -194,7 +204,8 @@ Contract Tickmap(

@using(preapprovedAssets = false, checkExternalCaller = false)
pub fn flip(value: Bool, tick: I256, tickSpacing: U256, poolKey: ByteVec) -> () {
// assert!(callerAddress!() == admin, TickmapErrors.NotAdmin)
let callerId = toByteVec!(callerAddress!())
assert!((callerId == invariantId || callerId == swapUtilsId) && areAdminsSet, TickmapErrors.NotAdmin)

let (chunk, bit) = tickToPosition(tick, tickSpacing)
let isInitialized = getBitAtPosition(chunk, bit)
Expand All @@ -210,6 +221,9 @@ Contract Tickmap(

@using(preapprovedAssets = true, checkExternalCaller = false)
pub fn initializeChunk(caller: Address, poolKey: ByteVec, chunk: U256) -> () {
let callerId = toByteVec!(callerAddress!())
assert!((callerId == invariantId || callerId == swapUtilsId) && areAdminsSet, TickmapErrors.NotAdmin)

let id = poolKey ++ toByteVec!(chunk)
let exists = contractExists!(subContractId!(id))

Expand Down
Loading

0 comments on commit 9742dfc

Please sign in to comment.