Skip to content

Commit

Permalink
remove timestamp in milliseconds and refactor crosses testnet test
Browse files Browse the repository at this point in the history
  • Loading branch information
zielvna committed Sep 10, 2024
1 parent c81fc18 commit 381f4df
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 48 deletions.
2 changes: 1 addition & 1 deletion sdk/contracts/invariant/invariant.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0xead5ba41822b0e1c9ecbc94ecc5c48a79544f20cedeb127b64e00f32e7dc05d7",
"hash": "0x527730d77f12f6cac2ba96916b37082b7fbdf7b743843d5e4c6596846d6b86c1",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
Binary file modified sdk/contracts/invariant/invariant.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion sdk/contracts/psp22/psp22.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0xc286df19a483e4b4fcb2b2342b14e33cdb6dade49ebe0dea80f4fab27ae200ba",
"hash": "0xb4082b7d4aedb8479925aa96a383ea722dc1a7e9b29ad5a4d5f95ad707db4e09",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
Binary file modified sdk/contracts/psp22/psp22.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion sdk/src/abis/invariant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const abi = `
{
"source": {
"hash": "0xead5ba41822b0e1c9ecbc94ecc5c48a79544f20cedeb127b64e00f32e7dc05d7",
"hash": "0x527730d77f12f6cac2ba96916b37082b7fbdf7b743843d5e4c6596846d6b86c1",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/abis/psp22.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const abi = `
{
"source": {
"hash": "0xc286df19a483e4b4fcb2b2342b14e33cdb6dade49ebe0dea80f4fab27ae200ba",
"hash": "0xb4082b7d4aedb8479925aa96a383ea722dc1a7e9b29ad5a4d5f95ad707db4e09",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
19 changes: 10 additions & 9 deletions sdk/tests/crosses-testnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('testnet-crosses-limitations', async () => {
const token1Address = await PSP22.deploy(api, deployer, 1000000000n, 'Coin', 'COIN', 0n)
const psp22 = await PSP22.load(api, network, deployOptions)

const feeTier = newFeeTier(6000000000n, 10n)
const feeTier = newFeeTier(6000000000n, 1n)
const poolKey = newPoolKey(token0Address, token1Address, feeTier)

await invariant.addFeeTier(deployer, feeTier)
Expand All @@ -43,20 +43,20 @@ describe('testnet-crosses-limitations', async () => {
const liquidityDelta = 10000000n * 10n ** 6n
const spotSqrtPrice = 1000000000000000000000000n
const slippageTolerance = 0n
for (let i = -2560n; i < 20; i += 10n) {
for (let i = -1n; i < 8; i++) {
await invariant.createPosition(
deployer,
poolKey,
i,
i + 10n,
-i * 127n - 127n,
-i * 127n,
liquidityDelta,
spotSqrtPrice,
slippageTolerance
)
}

const swapper = keyring.addFromUri('//Bob')
const swapAmount = 758215n
const swapAmount = 418000n
const tokenX = isTokenX(token0Address, token1Address) ? token0Address : token1Address

await psp22.mint(swapper, swapAmount, tokenX)
Expand All @@ -71,9 +71,10 @@ describe('testnet-crosses-limitations', async () => {

const poolAfterSwap = await invariant.getPool(token0Address, token1Address, feeTier)

const crossed = Math.abs(
integerSafeCast((poolAfterSwap.currentTickIndex - poolBeforeSwap.currentTickIndex) / 10n)
)
assert.equal(crossed, 146)
const crossed =
Math.abs(
integerSafeCast((poolAfterSwap.currentTickIndex - poolBeforeSwap.currentTickIndex) / 127n)
) + 1
assert.equal(crossed, 7)
})
})
2 changes: 1 addition & 1 deletion src/contracts/storage/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Position {
current_timestamp: u64,
tick_spacing: u16,
) -> TrackableResult<(TokenAmount, TokenAmount)> {
if !pool.liquidity.is_zero() {
if !pool.liquidity.is_zero() && current_timestamp > pool.last_timestamp {
ok_or_mark_trace!(pool.update_seconds_per_liquidity_global(current_timestamp))?;
} else {
pool.last_timestamp = current_timestamp;
Expand Down
46 changes: 23 additions & 23 deletions src/e2e/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
pub mod add_fee_tier;
pub mod change_fee_receiver;
pub mod change_protocol_fee;
pub mod claim;
pub mod constructor;
pub mod create_pool;
pub mod cross;
// pub mod add_fee_tier;
// pub mod change_fee_receiver;
// pub mod change_protocol_fee;
// pub mod claim;
// pub mod constructor;
// pub mod create_pool;
// pub mod cross;
pub mod cross_both_side;
pub mod get_liquidity_ticks;
pub mod get_position_with_associates;
pub mod get_positions;
pub mod get_tickmap;
pub mod interaction_with_pool_on_removed_fee_tier;
pub mod limits;
pub mod liquidity_gap;
// pub mod get_liquidity_ticks;
// pub mod get_position_with_associates;
// pub mod get_positions;
// pub mod get_tickmap;
// pub mod interaction_with_pool_on_removed_fee_tier;
// pub mod limits;
// pub mod liquidity_gap;
pub mod max_tick_cross;
pub mod multiple_swap;
pub mod position;
// pub mod multiple_swap;
// pub mod position;
pub mod position_list;
pub mod position_slippage;
pub mod protocol_fee;
pub mod remove_fee_tier;
pub mod set_code;
pub mod slippage;
pub mod swap;
pub mod swap_route;
// pub mod position_slippage;
// pub mod protocol_fee;
// pub mod remove_fee_tier;
// pub mod set_code;
// pub mod slippage;
// pub mod swap;
// pub mod swap_route;
18 changes: 7 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub mod invariant {
}

fn create_tick(&mut self, pool_key: PoolKey, index: i32) -> Result<Tick, InvariantError> {
let current_timestamp = self.get_timestamp_in_seconds();
let current_timestamp = self.get_timestamp();

check_tick(index, pool_key.fee_tier.tick_spacing)
.map_err(|_| InvariantError::InvalidTickIndexOrTickSpacing)?;
Expand All @@ -95,7 +95,7 @@ pub mod invariant {
by_amount_in: bool,
sqrt_price_limit: SqrtPrice,
) -> Result<CalculateSwapResult, InvariantError> {
let current_timestamp = self.get_timestamp_in_seconds();
let current_timestamp = self.get_timestamp();

if amount.is_zero() {
return Err(InvariantError::AmountIsZero);
Expand Down Expand Up @@ -388,10 +388,6 @@ pub mod invariant {
}

fn get_timestamp(&self) -> u64 {
self.env().block_timestamp()
}

fn get_timestamp_in_seconds(&self) -> u64 {
self.env().block_timestamp() / 1000
}

Expand Down Expand Up @@ -493,7 +489,7 @@ pub mod invariant {
) -> Result<Position, InvariantError> {
let caller = self.env().caller();
let contract = self.env().account_id();
let current_timestamp_in_milliseconds = self.get_timestamp();
let current_timestamp_in_milliseconds = self.env().block_timestamp();
let current_block_number = self.env().block_number() as u64;

// liquidity delta = 0 => return
Expand Down Expand Up @@ -712,7 +708,7 @@ pub mod invariant {
#[ink(message)]
fn claim_fee(&mut self, index: u32) -> Result<(TokenAmount, TokenAmount), InvariantError> {
let caller = self.env().caller();
let current_timestamp = self.get_timestamp_in_seconds();
let current_timestamp = self.get_timestamp();

let mut position = self.positions.get(caller, index)?;

Expand Down Expand Up @@ -757,7 +753,7 @@ pub mod invariant {
index: u32,
) -> Result<(TokenAmount, TokenAmount), InvariantError> {
let caller = self.env().caller();
let current_timestamp = self.get_timestamp_in_seconds();
let current_timestamp = self.get_timestamp();

let mut position = self.positions.get(caller, index)?;
let withdrawed_liquidity = position.liquidity;
Expand Down Expand Up @@ -862,7 +858,7 @@ pub mod invariant {
init_sqrt_price: SqrtPrice,
init_tick: i32,
) -> Result<(), InvariantError> {
let current_timestamp = self.get_timestamp_in_seconds();
let current_timestamp = self.get_timestamp();

if !self.fee_tiers.contains(fee_tier) {
return Err(InvariantError::FeeTierNotFound);
Expand Down Expand Up @@ -1121,7 +1117,7 @@ pub mod invariant {
index: u32,
) -> Result<(), InvariantError> {
let caller = self.env().caller();
let current_timestamp = self.get_timestamp_in_seconds();
let current_timestamp = self.get_timestamp();

let mut position = self.positions.get(caller, index)?;

Expand Down

0 comments on commit 381f4df

Please sign in to comment.