Skip to content

Commit

Permalink
Delta for amounts/safe liq mgmt, unique pos_id index, vanity selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
af-afk committed Jul 17, 2024
1 parent bd087a5 commit b36dee7
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 156 deletions.
2 changes: 1 addition & 1 deletion db/migrations/1712766721-events_seawater.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CREATE TABLE events_seawater_mintPosition (
upper BIGINT NOT NULL
);

CREATE UNIQUE INDEX ON events_seawater_mintPosition (pos_id, pool);
CREATE UNIQUE INDEX ON events_seawater_mintPosition (pos_id);
CREATE INDEX ON events_seawater_mintPosition (owner, pool);

CREATE TABLE events_seawater_transferPosition (
Expand Down
28 changes: 26 additions & 2 deletions lib/math/concentrated-liq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestGetPriceAtSqrtRatio(t *testing.T) {
t.Fatal("unimplemented")
}

var getAmountForsLiqTestTable = map[string]struct {
var getAmountsForLiqTestTable = map[string]struct {
sqrtRatioX96, sqrtRatioAX96, sqrtRatioBX96 *big.Int
liq int

Expand All @@ -116,10 +116,34 @@ var getAmountForsLiqTestTable = map[string]struct {
encodePriceSqrt(111, 100), encodePriceSqrt(100, 110), encodePriceSqrt(110, 100), 2097,
new(big.Rat).SetInt64(0), new(big.Rat).SetInt64(200),
},
"contract alex_0f08c379a": {
GetSqrtRatioAtTick(new(big.Int).SetInt64(2970)), // Current tick
GetSqrtRatioAtTick(new(big.Int).SetInt64(2100)), // Lower tick
GetSqrtRatioAtTick(new(big.Int).SetInt64(4080)), // Upper tick
18117952900, // Liquidity (delta)
new(big.Rat).SetInt64(842893567), // Amount0
new(big.Rat).SetInt64(842893567), // Amount1
},
"contract alex_0f08c379a without tick conversion in the go code": {
mustIntFromStr("91911338314972375132734921679"), // Current tick
mustIntFromStr("87999098777895760865233273050"), // Lower tick
mustIntFromStr("97156358459122590463153608088"), // Upper tick
18117952900, // Liquidity (delta)
new(big.Rat).SetInt64(842893567), // Amount0
new(big.Rat).SetInt64(842893567), // Amount1
},
}

func mustIntFromStr(x string) *big.Int {
i, ok := new(big.Int).SetString(x, 10)
if !ok {
panic(x)
}
return i
}

func TestGetAmountsForLiq(t *testing.T) {
for k, test := range getAmountForsLiqTestTable {
for k, test := range getAmountsForLiqTestTable {
test := test
t.Run(k, func(t *testing.T) {
t.Parallel()
Expand Down
1 change: 1 addition & 0 deletions pkg/Cargo.lock

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

3 changes: 2 additions & 1 deletion pkg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ incremental = false
stylus-sdk = "0.5.0"
thiserror = "1.0.48"
tiny-keccak = "2.0.2"
ruint = "1.11.0"
ruint = { version = "1.11.0", features = ["num-traits"] }
num-traits = "0.2.19"
alloy-sol-types = "0.3.1"
ruint-macro = "1.1.0"
keccak-const = "0.2.0"
Expand Down
1 change: 1 addition & 0 deletions pkg/seawater/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tiny-keccak.workspace = true
lol_alloc.workspace = true
keccak-const.workspace = true
const-hex.workspace = true
num-traits.workspace = true

[dev-dependencies]
rand = "0.8.5"
Expand Down
6 changes: 6 additions & 0 deletions pkg/seawater/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ pub enum Error {

#[error("Invalid tick spacing")]
InvalidTickSpacing,

#[error("Amount 0 too low")]
Amount0TooLow,

#[error("Amount 1 too low")]
Amount1TooLow
}

impl From<Error> for Vec<u8> {
Expand Down
48 changes: 16 additions & 32 deletions pkg/seawater/src/host_erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,29 @@ pub fn decimals(_token: Address) -> Result<u8, Error> {
///! Assumes a single token is in use for the life of this test.
pub fn take_transfer_from(_token: Address, _amount: U256) -> Result<(), Error> {
#[cfg(feature = "testing-dbg")]
{
dbg!(("take_transfer_from", current_test!(), _token, _amount));
return host_test_shims::take_caller_bal(_token, _amount).map_err(
|_| Error::Erc20RevertNoData, // follow the trace!
);
}
#[allow(unreachable_code)]
Ok(())
dbg!(("take_transfer_from", current_test!(), _token, _amount));
host_test_shims::take_caller_bal(_token, _amount).map_err(
|_| Error::Erc20RevertNoData, // follow the trace!
)
}

/// Pretends to give users tokens. Only useful for testing.
pub fn give(_token: Address, _amount: U256) -> Result<(), Error> {
#[cfg(feature = "testing-dbg")]
{
dbg!(("give", current_test!(), _token, _amount));
return host_test_shims::take_amm_bal(_token, _amount).map_err(
|_| Error::Erc20RevertNoData, // follow the trace!
);
}
#[allow(unreachable_code)]
Ok(())
dbg!(("give", current_test!(), _token, _amount));
host_test_shims::take_amm_bal(_token, _amount).map_err(
|_| Error::Erc20RevertNoData, // follow the trace!
)
}

/// Pretends to take ERC20 tokens from the user, only happening if the underlying
/// environment is not WASM. Only useful for testing.
pub fn take_permit2(_token: Address, _amount: U256, _details: Permit2Args) -> Result<(), Error> {
#[cfg(feature = "testing-dbg")]
{
dbg!(("take_permit2", current_test!(), _token, _amount, _details));
return host_test_shims::take_caller_bal(_token, _amount).map_err(
|_| Error::Erc20RevertNoData, // follow the trace!
);
}
#[allow(unreachable_code)]
Ok(())
dbg!(("take_permit2", current_test!(), _token, _amount, _details));
host_test_shims::take_caller_bal(_token, _amount).map_err(
|_| Error::Erc20RevertNoData, // follow the trace!
)
}

/// Pretends to take ERC20 tokens from the user, only happening if the underlying
Expand All @@ -67,14 +55,10 @@ pub fn take(
_permit2_details: Option<Permit2Args>,
) -> Result<(), Error> {
#[cfg(feature = "testing-dbg")]
{
dbg!(("take", current_test!(), _token, _amount, _permit2_details));
return host_test_shims::take_caller_bal(_token, _amount).map_err(
|_| Error::Erc20RevertNoData, // follow the trace!
);
}
#[allow(unreachable_code)]
Ok(())
dbg!(("take", current_test!(), _token, _amount, _permit2_details));
host_test_shims::take_caller_bal(_token, _amount).map_err(
|_| Error::Erc20RevertNoData, // follow the trace!
)
}

/// Pretends to construct a revert string from a message, only happening if the underlying
Expand Down
Loading

0 comments on commit b36dee7

Please sign in to comment.