Skip to content

Commit

Permalink
Merge pull request #303 from invariant-labs/add-tickmap-deallocation
Browse files Browse the repository at this point in the history
add tickmap deallocation
  • Loading branch information
none00y authored Oct 1, 2024
2 parents 9bb1a3c + 7375be6 commit a8842f9
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 16 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": "0xd654be3763d7b10c0b51fcf0625e58173d23d1f1f6c0ae1c3d3242ec90c7c93e",
"hash": "0x1cb24d4deb9c26024479701449993e8696236281e7f6d4253678b9a757732846",
"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.
13 changes: 5 additions & 8 deletions sdk/package-lock.json

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

2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@invariant-labs/a0-sdk",
"version": "0.2.29",
"version": "0.2.30",
"collaborators": [
"Invariant Labs"
],
Expand Down
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": "0xd654be3763d7b10c0b51fcf0625e58173d23d1f1f6c0ae1c3d3242ec90c7c93e",
"hash": "0x1cb24d4deb9c26024479701449993e8696236281e7f6d4253678b9a757732846",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
95 changes: 90 additions & 5 deletions src/contracts/storage/tickmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,22 @@ impl Tickmap {
.get((chunk_lookup_index, pool_key))
.unwrap_or(0);

self.chunk_lookups.insert(
(chunk_lookup_index, pool_key),
&(chunk_lookup | (1 << chunk_lookup_bit)),
);
let chunk_lookup = if chunk_value == &0 {
self.bitmap.remove((chunk_index, pool_key));

chunk_lookup & !(1 << chunk_lookup_bit)
} else {
self.bitmap.insert((chunk_index, pool_key), chunk_value);

self.bitmap.insert((chunk_index, pool_key), chunk_value);
chunk_lookup | (1 << chunk_lookup_bit)
};

if chunk_lookup == 0 {
self.chunk_lookups.remove((chunk_lookup_index, pool_key));
} else {
self.chunk_lookups
.insert((chunk_lookup_index, pool_key), &(chunk_lookup));
}
}
}

Expand Down Expand Up @@ -386,53 +396,128 @@ mod tests {
//zero
{
let index = 0;
let (chunk_index, chunk_bit) = tick_to_position(index, fee_tier.tick_spacing);
let lookup_index = get_chunk_lookup_index(chunk_index);
let lookup_bit = get_chunk_lookup_bit(chunk_index);

assert!(!tickmap.get(index, 1, pool_key));
assert_eq!(tickmap.bitmap.get((chunk_index, pool_key)), None);
assert_eq!(tickmap.chunk_lookups.get((lookup_index, pool_key)), None);
tickmap.flip(true, index, 1, pool_key);
assert!(tickmap.get(index, 1, pool_key));
assert_eq!(
tickmap.chunk_lookups.get((lookup_index, pool_key)),
Some(1u64 << lookup_bit)
);
assert_eq!(
tickmap.bitmap.get((chunk_index, pool_key)),
Some(1u64 << chunk_bit)
);
tickmap.flip(false, index, 1, pool_key);
assert!(!tickmap.get(index, 1, pool_key));
assert_eq!(tickmap.bitmap.get((chunk_index, pool_key)), None);
assert_eq!(tickmap.chunk_lookups.get((lookup_index, pool_key)), None);
}
// small
{
let index = 7;
let (chunk_index, chunk_bit) = tick_to_position(index, fee_tier.tick_spacing);
let lookup_index = get_chunk_lookup_index(chunk_index);
let lookup_bit = get_chunk_lookup_bit(chunk_index);

assert!(!tickmap.get(index, 1, pool_key));
assert_eq!(tickmap.bitmap.get((chunk_index, pool_key)), None);
assert_eq!(tickmap.chunk_lookups.get((lookup_index, pool_key)), None);
tickmap.flip(true, index, 1, pool_key);
assert!(tickmap.get(index, 1, pool_key));
assert_eq!(
tickmap.chunk_lookups.get((lookup_index, pool_key)),
Some(1u64 << lookup_bit)
);
assert_eq!(
tickmap.bitmap.get((chunk_index, pool_key)),
Some(1u64 << chunk_bit)
);
tickmap.flip(false, index, 1, pool_key);
assert!(!tickmap.get(index, 1, pool_key));
assert_eq!(tickmap.bitmap.get((chunk_index, pool_key)), None);
assert_eq!(tickmap.chunk_lookups.get((lookup_index, pool_key)), None);
}
// big
{
let index = MAX_TICK - 1;
let (chunk_index, chunk_bit) = tick_to_position(index, fee_tier.tick_spacing);
let lookup_index = get_chunk_lookup_index(chunk_index);
let lookup_bit = get_chunk_lookup_bit(chunk_index);

assert!(!tickmap.get(index, 1, pool_key));
assert_eq!(tickmap.bitmap.get((chunk_index, pool_key)), None);
assert_eq!(tickmap.chunk_lookups.get((lookup_index, pool_key)), None);
tickmap.flip(true, index, 1, pool_key);
assert!(tickmap.get(index, 1, pool_key));
assert_eq!(
tickmap.chunk_lookups.get((lookup_index, pool_key)),
Some(1u64 << lookup_bit)
);
assert_eq!(
tickmap.bitmap.get((chunk_index, pool_key)),
Some(1u64 << chunk_bit)
);
tickmap.flip(false, index, 1, pool_key);
assert!(!tickmap.get(index, 1, pool_key));
assert_eq!(tickmap.bitmap.get((chunk_index, pool_key)), None);
assert_eq!(tickmap.chunk_lookups.get((lookup_index, pool_key)), None);
}
// negative
{
let index = MAX_TICK - 40;
let (chunk_index, chunk_bit) = tick_to_position(index, fee_tier.tick_spacing);
let lookup_index = get_chunk_lookup_index(chunk_index);
let lookup_bit = get_chunk_lookup_bit(chunk_index);

assert!(!tickmap.get(index, 1, pool_key));
assert_eq!(tickmap.bitmap.get((chunk_index, pool_key)), None);
assert_eq!(tickmap.chunk_lookups.get((lookup_index, pool_key)), None);
tickmap.flip(true, index, 1, pool_key);
assert!(tickmap.get(index, 1, pool_key));
assert_eq!(
tickmap.chunk_lookups.get((lookup_index, pool_key)),
Some(1u64 << lookup_bit)
);
assert_eq!(
tickmap.bitmap.get((chunk_index, pool_key)),
Some(1u64 << chunk_bit)
);
tickmap.flip(false, index, 1, pool_key);
assert!(!tickmap.get(index, 1, pool_key));
assert_eq!(tickmap.bitmap.get((chunk_index, pool_key)), None);
assert_eq!(tickmap.chunk_lookups.get((lookup_index, pool_key)), None);
}
// tick spacing
{
let index = 20000;
let tick_spacing = 1000;
let (chunk_index, chunk_bit) = tick_to_position(index, tick_spacing);
let lookup_index = get_chunk_lookup_index(chunk_index);
let lookup_bit = get_chunk_lookup_bit(chunk_index);

assert!(!tickmap.get(index, tick_spacing, pool_key));
assert_eq!(tickmap.bitmap.get((chunk_index, pool_key)), None);
assert_eq!(tickmap.chunk_lookups.get((lookup_index, pool_key)), None);
tickmap.flip(true, index, tick_spacing, pool_key);
assert!(tickmap.get(index, tick_spacing, pool_key));
assert_eq!(
tickmap.chunk_lookups.get((lookup_index, pool_key)),
Some(1u64 << lookup_bit)
);
assert_eq!(
tickmap.bitmap.get((chunk_index, pool_key)),
Some(1u64 << chunk_bit)
);
tickmap.flip(false, index, tick_spacing, pool_key);
assert!(!tickmap.get(index, tick_spacing, pool_key));
assert_eq!(tickmap.bitmap.get((chunk_index, pool_key)), None);
assert_eq!(tickmap.chunk_lookups.get((lookup_index, pool_key)), None);
}
}

Expand Down

0 comments on commit a8842f9

Please sign in to comment.