From 2078e983394be4f26092ea48588554b403ad31e9 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Fri, 8 Dec 2023 13:15:25 -0700 Subject: [PATCH 01/39] arb mainnet first deployment --- hardhat.config.ts | 7 + package.json | 2 +- scripts/autogen/contract-deployments-keys.ts | 78 +++++++- scripts/autogen/contract-deployments.json | 188 ++++++++++++++++++- tasks/deploy/deploy-limitpools.ts | 3 + test/utils/setup/initialSetup.ts | 2 +- 6 files changed, 264 insertions(+), 16 deletions(-) diff --git a/hardhat.config.ts b/hardhat.config.ts index 896e2777..374bd1ac 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -39,6 +39,12 @@ const config: HardhatUserConfig = { accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], timeout: 60000, }, + arb_one: { + chainId: 42161, + url: process.env.ARBITRUM_ONE_URL || '', + accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], + timeout: 60000, + }, scrollSepolia: { chainId: 534351, url: "https://sepolia-rpc.scroll.io/" || "", @@ -55,6 +61,7 @@ const config: HardhatUserConfig = { }, etherscan: { apiKey: { + arbitrumOne: process.env.ARBITRUM_ONE_API_KEY, arbitrumGoerli: process.env.ARBITRUM_GOERLI_API_KEY, scrollSepolia: 'D62920783A4311EE9D6600155D570C742E', }, diff --git a/package.json b/package.json index 4cd0ec25..e19f45f2 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "clean": "rm -rf ./artifacts ./cache ./typechain; npx hardhat clean", "compile": "npx hardhat compile", "test": "npx hardhat test", - "deploy": "rm -rf ./artifacts ./cache ./typechain; npx hardhat clean; npx hardhat deploy-limitpools --network arb_goerli;", + "deploy": "rm -rf ./artifacts ./cache ./typechain; npx hardhat clean; npx hardhat compile; npx hardhat deploy-limitpools --network arb_goerli;", "verify": "npx hardhat verify-contracts", "all": "yarn clean; yarn compile; yarn test; yarn deploy;" }, diff --git a/scripts/autogen/contract-deployments-keys.ts b/scripts/autogen/contract-deployments-keys.ts index 16c5e7fc..94669ef4 100644 --- a/scripts/autogen/contract-deployments-keys.ts +++ b/scripts/autogen/contract-deployments-keys.ts @@ -2,23 +2,83 @@ import { ContractDeploymentsKey } from '../util/files/contractDeploymentsJson' export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ { - networkName: 'arb_goerli', - objectName: 'poolRouter' + networkName: 'arb_one', + objectName: 'tickMapLib' }, { - networkName: 'arb_goerli', - objectName: 'rangeStaker' + networkName: 'arb_one', + objectName: 'ticksLib' }, { - networkName: 'arb_goerli', - objectName: 'poolRouter' + networkName: 'arb_one', + objectName: 'limitPositionsLib' }, { - networkName: 'arb_goerli', - objectName: 'poolRouter' + networkName: 'arb_one', + objectName: 'limitPoolManager' + }, + { + networkName: 'arb_one', + objectName: 'limitPoolFactory' + }, + { + networkName: 'arb_one', + objectName: 'swapCall' + }, + { + networkName: 'arb_one', + objectName: 'mintRangeCall' + }, + { + networkName: 'arb_one', + objectName: 'burnRangeCall' + }, + { + networkName: 'arb_one', + objectName: 'mintLimitCall' + }, + { + networkName: 'arb_one', + objectName: 'burnLimitCall' + }, + { + networkName: 'arb_one', + objectName: 'snapshotLimitCall' }, { - networkName: 'arb_goerli', + networkName: 'arb_one', + objectName: 'quoteCall' + }, + { + networkName: 'arb_one', + objectName: 'feesCall' + }, + { + networkName: 'arb_one', + objectName: 'sampleCall' + }, + { + networkName: 'arb_one', + objectName: 'snapshotRangeCall' + }, + { + networkName: 'arb_one', + objectName: 'limitPoolImpl' + }, + { + networkName: 'arb_one', + objectName: 'positionERC1155' + }, + { + networkName: 'arb_one', + objectName: 'limitPool' + }, + { + networkName: 'arb_one', objectName: 'poolRouter' }, + { + networkName: 'arb_one', + objectName: 'rangeStaker' + }, ]; \ No newline at end of file diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index 436d7ea2..4a350aae 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -1,4 +1,181 @@ { + "arb_one": { + "token0": { + "contractName": "Token20", + "contractAddress": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", + "constructorArguments": [ + "Wrapped Ether", + "WETH", + 18 + ], + "created": "2023-08-23T15:56:24.715Z" + }, + "token1": { + "contractName": "Token20", + "contractAddress": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", + "constructorArguments": [ + "USD Coin", + "USDC", + 6 + ], + "created": "2023-08-23T15:56:24.723Z" + }, + "weth9": { + "contractName": "WETH9", + "contractAddress": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", + "constructorArguments": [], + "created": "2023-11-25T12:32:49.206Z" + }, + "coverPoolFactory": { + "contractName": "CoverPoolFactory", + "contractAddress": "0x0000000000000000000000000000000000000000", + "constructorArguments": [ + "0x7877A681D1290d80ed0272A920b9a5CeE8Fd2fAC" + ], + "created": "2023-10-01T13:30:41.488Z" + }, + "tickMapLib": { + "contractName": "TickMap", + "contractAddress": "0x334a70f0cb8b9ceee3d0f4ea232581ff3dfc6e58", + "constructorArguments": [], + "created": "2023-12-08T16:27:08.959Z" + }, + "ticksLib": { + "contractName": "Ticks", + "contractAddress": "0x3fa761492f411ebc64a81fcf3292fdc0b677c00f", + "constructorArguments": [], + "created": "2023-12-08T16:27:13.655Z" + }, + "limitPositionsLib": { + "contractName": "LimitPositions", + "contractAddress": "0x414b73f989e7ca0653b5c98186749a348405e6d5", + "constructorArguments": [], + "created": "2023-12-08T16:27:18.380Z" + }, + "limitPoolManager": { + "contractName": "LimitPoolManager", + "contractAddress": "0xd50b04a5693f2d026d589bf239609bf5b8346adc", + "constructorArguments": [], + "created": "2023-12-08T16:27:23.238Z" + }, + "limitPoolFactory": { + "contractName": "LimitPoolFactory", + "contractAddress": "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", + "constructorArguments": [ + "0xd50B04a5693F2d026D589Bf239609Bf5B8346AdC" + ], + "created": "2023-12-08T16:27:28.092Z" + }, + "swapCall": { + "contractName": "SwapCall", + "contractAddress": "0xd58628f15d4391a036c578638105e67f857b6770", + "constructorArguments": [], + "created": "2023-12-08T16:27:32.840Z" + }, + "mintRangeCall": { + "contractName": "MintRangeCall", + "contractAddress": "0xe7623a3a8d94296f9fe2b0c1fdfbcd36cc9c8d32", + "constructorArguments": [], + "created": "2023-12-08T16:27:37.650Z" + }, + "burnRangeCall": { + "contractName": "BurnRangeCall", + "contractAddress": "0x26323c7e4f000227932d3ed640ea79d6027701de", + "constructorArguments": [], + "created": "2023-12-08T16:27:42.664Z" + }, + "mintLimitCall": { + "contractName": "MintLimitCall", + "contractAddress": "0x912c3e7d140e4ec85eb8e3910447acda8654d523", + "constructorArguments": [], + "created": "2023-12-08T16:27:47.884Z" + }, + "burnLimitCall": { + "contractName": "BurnLimitCall", + "contractAddress": "0x7024879eda80577cbc0cb039ad3c7081f38abb41", + "constructorArguments": [], + "created": "2023-12-08T16:27:52.852Z" + }, + "snapshotLimitCall": { + "contractName": "SnapshotLimitCall", + "contractAddress": "0xa826f06c47597549faa74d8916ef4da8f417f16c", + "constructorArguments": [], + "created": "2023-12-08T16:27:57.638Z" + }, + "quoteCall": { + "contractName": "QuoteCall", + "contractAddress": "0x92192101f6c5138160c093882b0fa502dee889cd", + "constructorArguments": [], + "created": "2023-12-08T16:28:02.421Z" + }, + "feesCall": { + "contractName": "FeesCall", + "contractAddress": "0x4dc4f1a2a3303e40a07c3aac4919a97f33ab01eb", + "constructorArguments": [], + "created": "2023-12-08T16:28:07.294Z" + }, + "sampleCall": { + "contractName": "SampleCall", + "contractAddress": "0xc1e9a857e96aeefe28f8340cf82de7801bd95a77", + "constructorArguments": [], + "created": "2023-12-08T16:28:12.093Z" + }, + "snapshotRangeCall": { + "contractName": "SnapshotRangeCall", + "contractAddress": "0x96f8d308db8f86fccbef8c57df7bbad6ee4c7d89", + "constructorArguments": [], + "created": "2023-12-08T16:28:16.860Z" + }, + "limitPoolImpl": { + "contractName": "LimitPool", + "contractAddress": "0x215afe9a2d7e1f8b82d221213d880f7e28e1dd03", + "constructorArguments": [ + "0x9F479560cd8A531E6C0fe04521Cb246264fE6b71" + ], + "created": "2023-12-08T16:28:21.902Z" + }, + "positionERC1155": { + "contractName": "PositionERC1155", + "contractAddress": "0x3f96856927e6192291717d1f43cc5b264d93caa8", + "constructorArguments": [ + "0x9F479560cd8A531E6C0fe04521Cb246264fE6b71" + ], + "created": "2023-12-08T16:28:26.710Z" + }, + "limitPool": { + "contractName": "LimitPool", + "contractAddress": "0xb3db76a4e1986a433dc3f8fcb7fdc3b2cdcf490f", + "constructorArguments": [ + "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", + "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", + "1000", + 0 + ], + "created": "2023-12-08T16:28:51.180Z" + }, + "poolRouter": { + "contractName": "PoolsharkRouter", + "contractAddress": "0x7183ffeefc0883d499c52c1a1dff1af716467a46", + "constructorArguments": [ + "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", + "0x0000000000000000000000000000000000000000", + "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" + ], + "created": "2023-12-08T17:57:31.406Z" + }, + "rangeStaker": { + "contractName": "RangeStaker", + "contractAddress": "0xde95e92dd151c39eb51cfae80fdff4d6c32c1fad", + "constructorArguments": [ + { + "limitPoolFactory": "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", + "startTime": 0, + "endTime": 2000707154 + } + ], + "created": "2023-12-08T17:57:36.413Z" + } + }, "arb_goerli": { "token0": { "contractName": "Token20", @@ -136,13 +313,13 @@ }, "poolRouter": { "contractName": "PoolsharkRouter", - "contractAddress": "0x24757e9d68bfcc99a9dba0a62737703cb1a32e06", + "contractAddress": "0xd4f769b054252a3bcbabab3f280315ce67346a65", "constructorArguments": [ "0x1b215002e688135549cc0290d6cf1f94e3aa425c", "0xf8f75a39663e97d36da4be2882608513512f4cf0", "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7" ], - "created": "2023-11-29T19:48:26.040Z" + "created": "2023-12-08T18:17:44.533Z" }, "limitPool": { "contractName": "LimitPool", @@ -191,7 +368,7 @@ }, "rangeStaker": { "contractName": "RangeStaker", - "contractAddress": "0xe5e2e95a986ce078606c403593593b18ed98f4d6", + "contractAddress": "0xe09aebe3c2a008fc9a3528893e9ead1f30dd6d5a", "constructorArguments": [ { "limitPoolFactory": "0x1b215002e688135549cc0290d6cf1f94e3aa425c", @@ -199,7 +376,7 @@ "endTime": 2000707154 } ], - "created": "2023-11-28T22:19:18.570Z" + "created": "2023-12-08T18:17:46.387Z" } }, "scrollSepolia": { @@ -365,5 +542,6 @@ ], "created": "2023-10-02T23:25:47.853Z" } - } + }, + "undefined": {} } \ No newline at end of file diff --git a/tasks/deploy/deploy-limitpools.ts b/tasks/deploy/deploy-limitpools.ts index a2277bc7..0564ba6c 100644 --- a/tasks/deploy/deploy-limitpools.ts +++ b/tasks/deploy/deploy-limitpools.ts @@ -27,5 +27,8 @@ task(DEPLOY_LIMITPOOLS) await deployLimitPools.deployLimitPools.postDeployment() + const psharkAsciiArt = '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#%@@@@%@@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@%%%%%%@@%==========+++**#@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@%%##%%%@@%%%@@%%*-=====+++**#**+*#*#*#@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@=+===========+#@@@@@%#@%%%%%%*#%%%%%##%@@@@@@@@#****++#%#**#%=%@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@+-==+++***++++=++@@%%%%%%%%*****#%#*#@%@@@@@@@@@@@@@%#+#%#*##*=#@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@+=+*******+++%%##%%####++++++**@@%%@@@@@@@@@@@@@@@###*#%#+==@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@%=**##**+#%###########+====*@%@@%%@@@@@@@@@@@@@%###%@+%=+=*@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@#%%####*##*##*#**#**+=+=-=*%%%###%@@@@@@@@@@@####@#=====+*@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@#%#***#***#*****=*+=+==--=@@%#**###@@@@@@@@@@@###@@%=+++=++#@@@@*@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@##*********+***##++*+#%@*%@@@@@####@@@@#=%@@@@@@@@@@@+==++=+=%%#+*@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@##*****+**+*********++=%%%@@@@@@@@@*@@@%===@@@@@@@@@*===++=+++**#%#@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@#**++++*+++++===+***+==#%%#@@@@@@@@*##@*===+%@@@@%+==+=+++++++*%@%%@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@**++++++++====----+++====+###@@@@@@@%#%%*==++*+#+===+++++++++++*%%@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@%###*++++++====---=*@@@@+===-%@**%@@@@@@@#%%%===*+****+++*++++*+*+*#@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@*+++*+++++===--=-*@@@@@@@@=--=@@@#%@@@@@@@@#%%*++++**#+++*++**++***#*@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@%===+++++=+++===*@@@@@@@@@@=-=%@@@@@%#@@@@@@@*===*++=++*++++****+**#%+@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@*=+@@#==+==+=-=@@@%%@@@@@@@@@@@@#***%%@@@@%+===+++*+*******+**++*#%*#@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@======-==#***%#@@@@@@@@@@@##%%%%%%@#====****+*****#**###*+++#*@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@%-===*=++**###@@@@@@@@@@@@@+#%%%%@*======+*********###+++++++++%@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@%-=+#%%+*###%@@@@@@@@@@@@@@@#%#@%++=+++===**#*#****#%=====+++++++%@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@-=#****####@@@@@@@@@@@@@@@@#%@*++*#++++=+#*#####*#%%==============*@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@*=#**+*###@@@@@@@@@@@@@@@@@#%+**###**+++######%%#+-==--=-=------======#@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@++#++++*##%@@@@@@@@@@@@@@@%+*#%%######*##%#+=-=#@@@@@*----------------*@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@%+#*+++==+***#@@@@@@@@@@@%#%%####%%#*+#*++%@@@@@@@@@@@@@%+=======*@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@*+**+========------=-=%@#%%%%%%%%%%%%#%#%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@#=====-==-------=#@@@@########%%%%%%##++#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@%%##%%@@@@@@@@@@@@%#######%%#*+=++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#*####++++=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##**+=#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@' + console.log(psharkAsciiArt) + console.log('Limit pool deployment complete.\n') }) diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index 2a214b31..9004069c 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -32,7 +32,7 @@ export class InitialSetup { private deployContracts = false private deployPools = false private deployRouter = true - private deployStaker = false + private deployStaker = true constructor() { this.deployAssist = new DeployAssist() From 10c1b4080b2caac0934f6e6dc969caa34f1a02a0 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Fri, 8 Dec 2023 18:28:41 -0700 Subject: [PATCH 02/39] mainnet subgraph --- subgraph/config/arbitrum-goerli.json | 48 +++++ subgraph/package.json | 11 +- .../{constants.ts => arbitrum-goerli.ts} | 0 subgraph/src/constants/arbitrum.ts | 60 ++++++ subgraph/src/mappings/limit/burn.ts | 2 +- subgraph/src/mappings/limit/mint.ts | 2 +- subgraph/src/mappings/limitpoolfactory.ts | 2 +- subgraph/src/mappings/limitpoolmanager.ts | 2 +- subgraph/src/mappings/pool/swap.ts | 2 +- subgraph/src/mappings/positionerc1155.ts | 2 +- subgraph/src/mappings/range/burn.ts | 2 +- subgraph/src/mappings/range/mint.ts | 2 +- subgraph/src/mappings/staking/rangestaker.ts | 2 +- subgraph/src/mappings/utils/loads.ts | 5 +- subgraph/src/mappings/utils/math.ts | 2 +- subgraph/src/mappings/utils/price.ts | 2 +- subgraph/src/mappings/utils/tvl.ts | 2 +- subgraph/subgraph.yaml | 26 ++- .../arbitrum-goerli.subgraph.template.yaml | 198 +++++++++++++++++ .../templates/arbitrum.subgraph.template.yaml | 200 ++++++++++++++++++ subgraph/templates/subgraph.template.yaml | 200 ++++++++++++++++++ tasks/deploy/utils/mintPosition.ts | 45 ++-- 22 files changed, 763 insertions(+), 54 deletions(-) create mode 100644 subgraph/config/arbitrum-goerli.json rename subgraph/src/constants/{constants.ts => arbitrum-goerli.ts} (100%) create mode 100644 subgraph/src/constants/arbitrum.ts create mode 100644 subgraph/templates/arbitrum-goerli.subgraph.template.yaml create mode 100644 subgraph/templates/arbitrum.subgraph.template.yaml create mode 100644 subgraph/templates/subgraph.template.yaml diff --git a/subgraph/config/arbitrum-goerli.json b/subgraph/config/arbitrum-goerli.json new file mode 100644 index 00000000..a9400f22 --- /dev/null +++ b/subgraph/config/arbitrum-goerli.json @@ -0,0 +1,48 @@ +{ + "Network": "arbitrum-goerli", + "Contracts": { + "Weth": "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7", + "StablePool": { + "Address": "0x4998545e13a668a884272aaebff14ab21c5b4e89", + "StableIsToken0": true + }, + "RangeStaker": "0xe5e2E95A986CE078606C403593593b18Ed98f4d6", + "LimitPoolFactory": "0x1b215002e688135549cc0290d6cf1f94e3aa425c", + "LimitPoolManager": "0x6d3af137e75097b892683832a2c0132f99625d0e", + "Stablecoins": [ + "0x19bee8e887a5db5cf20a841eb4daacbcacf14b1b", + "0xebff7a98149b4774c9743c5d1f382305fe5422c9" + ], + "WhitelistedTokens": [ + "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7", + "0x19bee8e887a5db5cf20a841eb4daacbcacf14b1b", + "0xebff7a98149b4774c9743c5d1f382305fe5422c9" + ], + "WhitelistedPairs": [ + "0xebff7a98149b4774c9743c5d1f382305fe5422c9-0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7" + ] + }, + "StartBlocks": { + "LimitPoolFactory": 56821935, + "LimitPoolManager": 56821935 + }, + "Symbols": { + "Stablecoins": [ + "DAI", + "USDC" + ], + "WhitelistedTokens": [ + "WETH", + "DAI", + "USDC" + ], + + "WhitelistedPairs": [ + "WETH-USDC" + ] + }, + "Season1": { + "startTime": "0", + "endTime": "2000707154" + } + } \ No newline at end of file diff --git a/subgraph/package.json b/subgraph/package.json index 358e261f..119aba82 100644 --- a/subgraph/package.json +++ b/subgraph/package.json @@ -26,7 +26,12 @@ "deploy-op-test": "graph deploy --product hosted-service alphak3y/poolshark-cover-op-goerli", "deploy-hs": "graph deploy --product hosted-service alphak3y/poolshark-limit", "deploy-local": "graph deploy example --ipfs http://localhost:5001 --node http://127.0.0.1:8020", - "test": "graph codegen; graph test -v 0.2.0" + "deploy-arb-one": "graph deploy --version-label v0.1.0 --node https://api.graph-ams.p2pify.com/234b623901aae1c380fb69e4d7534129/deploy --ipfs https://api.graph-ams.p2pify.com/234b623901aae1c380fb69e4d7534129/ipfs limit-arbitrum-test", + "test": "graph codegen; graph test -v 0.2.0", + "prepare:subgraph": "mustache ./config/${NETWORK}.json ./templates/${BLOCKCHAIN}.subgraph.template.yaml > subgraph.yaml && rm -rf generated && npm run generate:schema && npm run subgraph:codegen && npm run subgraph:build", + "subgraph:deploy:chainstack:test": "graph deploy --version-label v0.3.1 --node https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/deploy --ipfs https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/ipfs limit-arbitrumGoerli-test", + "subgraph:deploy:chainstack:ordertest": "graph deploy --version-label v0.1.0 --node https://api.graph-ams.p2pify.com/719c33840eec545dba4ed49d362448f0/deploy --ipfs https://api.graph-ams.p2pify.com/719c33840eec545dba4ed49d362448f0/ipfs limit-arbitrumGoerli-order-testing", + "deploy:chainstack:arb-goerli": "BLOCKCHAIN=arbitrum =arbitrum-goerli env-cmd npm run prepare:subgraph && SLUG=aave/protocol-v2-goerli env-cmd npm run subgraph:deploy:chainstack:ordertest" }, "devDependencies": { "@graphprotocol/graph-cli": "0.25.1", @@ -43,6 +48,8 @@ "assemblyscript-json": "1.1.0", "json-as": "0.2.6", "node-gyp": "9.1.0", - "source-map-support": "0.5.21" + "source-map-support": "0.5.21", + "mustache": "^3.1.0", + "env-cmd": "^10.1.0" } } diff --git a/subgraph/src/constants/constants.ts b/subgraph/src/constants/arbitrum-goerli.ts similarity index 100% rename from subgraph/src/constants/constants.ts rename to subgraph/src/constants/arbitrum-goerli.ts diff --git a/subgraph/src/constants/arbitrum.ts b/subgraph/src/constants/arbitrum.ts new file mode 100644 index 00000000..7d635d1b --- /dev/null +++ b/subgraph/src/constants/arbitrum.ts @@ -0,0 +1,60 @@ +/* eslint-disable */ +import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts' +import { LimitPoolFactory as FactoryContract } from '../../generated/templates/LimitPoolTemplate/LimitPoolFactory' + +export let FACTORY_ADDRESS = '0x9f479560cd8a531e6c0fe04521cb246264fe6b71' +export let RANGE_STAKER_ADDRESS = '0xde95e92dd151c39eb51cfae80fdff4d6c32c1fad' +export let WETH_ADDRESS = '0x82af49447d8a07e3bd95bd0d56f35241523fbab1' + +// tokens where USD value is safe to use for globals +export let WHITELISTED_TOKENS: string[] = [ + '0x82af49447d8a07e3bd95bd0d56f35241523fbab1', // WETH + '0xda10009cbd5d07dd0cecc66161fc93d7c9000da1', // DAI + '0xaf88d065e77c8cc2239327c5edb3a432268e5831', // USDC + '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', // USDC.e + '0x5979d7b546e38e414f7e9822514be443a4800529', // wstETH + '0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9' // USDT +] + +export let WHITELISTED_PAIRS: string[] = [ + '0x82af49447d8a07e3bd95bd0d56f35241523fbab1-0xaf88d065e77c8cc2239327c5edb3a432268e5831', // WETH - USDC + '0xaf88d065e77c8cc2239327c5edb3a432268e5831-0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', // USDC - USDC.e + '0x5979d7b546e38e414f7e9822514be443a4800529-0x82af49447d8a07e3bd95bd0d56f35241523fbab1', // wstETH - WETH + '0x82af49447d8a07e3bd95bd0d56f35241523fbab1-0x912ce59144191c1204e64559fe8253a0e49e6548', // WETH - ARB + '0x912ce59144191c1204e64559fe8253a0e49e6548-0xaf88d065e77c8cc2239327c5edb3a432268e5831', // ARB - USDC + '0xaf88d065e77c8cc2239327c5edb3a432268e5831-0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9' // USDT - USDC +] + +export let SEASON_1_START_TIME = BigInt.fromString('1674345600') // 1-22-2024 0:00 GMT +export let SEASON_1_END_TIME = BigInt.fromString('1684713600') // 5-22-2024 0:00 GMT + +// used for safe eth pricing +export let STABLE_COINS: string[] = [ + '0xda10009cbd5d07dd0cecc66161fc93d7c9000da1', // DAI + '0xaf88d065e77c8cc2239327c5edb3a432268e5831', // USDC + '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', // USDC.e + '0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9' // USDT +] + +// used for safe eth pricing +export const STABLE_POOL_ADDRESS = '0xb3db76a4e1986a433dc3f8fcb7fdc3b2cdcf490f' + +// determines which token to use for eth <-> usd rate, true means stable is token0 in pool above +export const STABLE_IS_TOKEN_0 = false + +// minimum eth required in pool to count usd values towards global prices +export let MINIMUM_ETH_LOCKED = BigDecimal.fromString('0') + +// pool that breaks with subgraph logic +export const ERROR_POOL = '0x0000000000000000000000000000000000000000' + +export let ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' + +export let ZERO_BI = BigInt.fromI32(0) +export let ONE_BI = BigInt.fromI32(1) +export let ZERO_BD = BigDecimal.fromString('0') +export let ONE_BD = BigDecimal.fromString('1') +export let TWO_BD = BigDecimal.fromString('2') +export let BI_18 = BigInt.fromI32(18) + +export let factoryContract = FactoryContract.bind(Address.fromString(FACTORY_ADDRESS)) \ No newline at end of file diff --git a/subgraph/src/mappings/limit/burn.ts b/subgraph/src/mappings/limit/burn.ts index c80c0d66..3090433c 100644 --- a/subgraph/src/mappings/limit/burn.ts +++ b/subgraph/src/mappings/limit/burn.ts @@ -1,6 +1,6 @@ import { store, BigInt } from "@graphprotocol/graph-ts" import { BurnLimit } from "../../../generated/LimitPoolFactory/LimitPool" -import { FACTORY_ADDRESS, ONE_BI, SEASON_1_END_TIME, SEASON_1_START_TIME } from "../../constants/constants" +import { FACTORY_ADDRESS, ONE_BI, SEASON_1_END_TIME, SEASON_1_START_TIME } from "../../constants/arbitrum" import { BIGINT_ONE, BIGINT_ZERO, convertTokenToDecimal } from "../utils/helpers" import { safeLoadBasePrice, safeLoadBurnLog, safeLoadHistoricalOrder, safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadLimitPosition, safeLoadLimitTick, safeLoadToken, safeLoadTotalSeasonReward, safeLoadTvlUpdateLog, safeLoadUserSeasonReward } from "../utils/loads" import { findEthPerToken } from "../utils/price" diff --git a/subgraph/src/mappings/limit/mint.ts b/subgraph/src/mappings/limit/mint.ts index 184b3d90..56f50860 100644 --- a/subgraph/src/mappings/limit/mint.ts +++ b/subgraph/src/mappings/limit/mint.ts @@ -3,7 +3,7 @@ import { Bytes, } from '@graphprotocol/graph-ts' import { MintLimit } from "../../../generated/LimitPoolFactory/LimitPool" -import { ONE_BI } from "../../constants/constants" +import { ONE_BI } from "../../constants/arbitrum" import { BIGDECIMAL_ZERO, BIGINT_ONE, BIGINT_ZERO, convertTokenToDecimal } from "../utils/helpers" import { safeLoadBasePrice, safeLoadHistoricalOrder, safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadLimitPosition, safeLoadLimitTick, safeLoadToken, safeLoadTvlUpdateLog } from "../utils/loads" import { findEthPerToken } from '../utils/price' diff --git a/subgraph/src/mappings/limitpoolfactory.ts b/subgraph/src/mappings/limitpoolfactory.ts index 608f466f..346a76a3 100644 --- a/subgraph/src/mappings/limitpoolfactory.ts +++ b/subgraph/src/mappings/limitpoolfactory.ts @@ -9,7 +9,7 @@ import { } from './utils/helpers' import { safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadLimitPoolToken, safeLoadToken } from './utils/loads' import { BigInt } from '@graphprotocol/graph-ts' -import { FACTORY_ADDRESS, ONE_BI, RANGE_STAKER_ADDRESS, STABLE_COINS, WETH_ADDRESS } from '../constants/constants' +import { FACTORY_ADDRESS, ONE_BI, RANGE_STAKER_ADDRESS, STABLE_COINS, WETH_ADDRESS } from '../constants/arbitrum' export function handlePoolCreated(event: PoolCreated): void { // grab event parameters diff --git a/subgraph/src/mappings/limitpoolmanager.ts b/subgraph/src/mappings/limitpoolmanager.ts index 62aa4a88..25a1d61a 100644 --- a/subgraph/src/mappings/limitpoolmanager.ts +++ b/subgraph/src/mappings/limitpoolmanager.ts @@ -1,6 +1,6 @@ import { safeLoadManager, safeLoadLimitPoolFactory, safeLoadFeeTier } from './utils/loads' import { BigInt, log } from '@graphprotocol/graph-ts' -import { FACTORY_ADDRESS } from '../constants/constants' +import { FACTORY_ADDRESS } from '../constants/arbitrum' import { FactoryChanged, FeeTierEnabled, FeeToTransfer, PoolTypeEnabled, OwnerTransfer, ProtocolSwapFeesModified, ProtocolFeesCollected, ProtocolFillFeesModified } from '../../generated/LimitPoolManager/LimitPoolManager' export function handleFeeTierEnabled(event: FeeTierEnabled): void { diff --git a/subgraph/src/mappings/pool/swap.ts b/subgraph/src/mappings/pool/swap.ts index 5fdf0f4a..522cb7c2 100644 --- a/subgraph/src/mappings/pool/swap.ts +++ b/subgraph/src/mappings/pool/swap.ts @@ -1,7 +1,7 @@ import { BigDecimal, BigInt, log } from "@graphprotocol/graph-ts" import { safeLoadBasePrice, safeLoadHistoricalOrder, safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadSwap, safeLoadToken, safeLoadTotalSeasonReward, safeLoadTransaction, safeLoadTvlUpdateLog, safeLoadUserSeasonReward } from "../utils/loads" import { BIGDECIMAL_ZERO, BIGINT_ONE, BIGINT_ZERO, convertTokenToDecimal } from "../utils/helpers" -import { ZERO_BD, TWO_BD, ONE_BI, FACTORY_ADDRESS, SEASON_1_END_TIME, SEASON_1_START_TIME } from "../../constants/constants" +import { ZERO_BD, TWO_BD, ONE_BI, FACTORY_ADDRESS, SEASON_1_END_TIME, SEASON_1_START_TIME } from "../../constants/arbitrum" import { AmountType, findEthPerToken, getAdjustedAmounts, getEthPriceInUSD, sqrtPriceX96ToTokenPrices } from "../utils/price" import { updateDerivedTVLAmounts } from "../utils/tvl" import { Swap } from "../../../generated/LimitPoolFactory/LimitPool" diff --git a/subgraph/src/mappings/positionerc1155.ts b/subgraph/src/mappings/positionerc1155.ts index c1f121e1..e2de4ef1 100644 --- a/subgraph/src/mappings/positionerc1155.ts +++ b/subgraph/src/mappings/positionerc1155.ts @@ -1,6 +1,6 @@ import { RouterDeployed } from "../../generated/PoolsharkRouter/PoolsharkRouter"; import { TransferBatch, TransferSingle } from "../../generated/templates/PositionERC1155Template/PositionERC1155"; -import { RANGE_STAKER_ADDRESS } from "../constants/constants"; +import { RANGE_STAKER_ADDRESS } from "../constants/arbitrum"; import { safeLoadLimitPoolToken, safeLoadLimitPosition, safeLoadPoolRouter, safeLoadRangePosition } from "./utils/loads"; export function handleTransferSingle(event: TransferSingle): void { diff --git a/subgraph/src/mappings/range/burn.ts b/subgraph/src/mappings/range/burn.ts index 6ff034a2..826b2e7f 100644 --- a/subgraph/src/mappings/range/burn.ts +++ b/subgraph/src/mappings/range/burn.ts @@ -3,7 +3,7 @@ import { safeLoadBasePrice, safeLoadBurnLog, safeLoadRangePositionById, safeLoad import { BigInt, } from '@graphprotocol/graph-ts' -import { ONE_BI } from "../../constants/constants" +import { ONE_BI } from "../../constants/arbitrum" import { updateDerivedTVLAmounts } from "../utils/tvl" import { BIGINT_ZERO, convertTokenToDecimal } from "../utils/helpers" import { findEthPerToken } from "../utils/price" diff --git a/subgraph/src/mappings/range/mint.ts b/subgraph/src/mappings/range/mint.ts index ab3fa56a..d0efde43 100644 --- a/subgraph/src/mappings/range/mint.ts +++ b/subgraph/src/mappings/range/mint.ts @@ -12,7 +12,7 @@ import { safeLoadTvlUpdateLog, } from '../utils/loads' import { BIGINT_ONE, convertTokenToDecimal } from '../utils/helpers' -import { ONE_BI } from '../../constants/constants' +import { ONE_BI } from '../../constants/arbitrum' import { updateDerivedTVLAmounts } from '../utils/tvl' import { findEthPerToken } from '../utils/price' import { MintRange } from '../../../generated/LimitPoolFactory/LimitPool' diff --git a/subgraph/src/mappings/staking/rangestaker.ts b/subgraph/src/mappings/staking/rangestaker.ts index 032f77f1..b6c67205 100644 --- a/subgraph/src/mappings/staking/rangestaker.ts +++ b/subgraph/src/mappings/staking/rangestaker.ts @@ -1,6 +1,6 @@ import { safeLoadLimitPool, safeLoadRangePosition, safeLoadToken, safeLoadTotalSeasonReward, safeLoadUserSeasonReward } from '../utils/loads' import { FeeToTransfer, OwnerTransfer, StakeRange, StakeRangeAccrued, UnstakeRange } from '../../../generated/templates/RangeStakerTemplate/RangeStaker' -import { FACTORY_ADDRESS, SEASON_1_END_TIME, SEASON_1_START_TIME, WHITELISTED_PAIRS, WHITELISTED_TOKENS } from '../../constants/constants' +import { FACTORY_ADDRESS, SEASON_1_END_TIME, SEASON_1_START_TIME, WHITELISTED_PAIRS, WHITELISTED_TOKENS } from '../../constants/arbitrum' import { convertTokenToDecimal } from '../utils/helpers' export function handleFeeToTransfer(event: FeeToTransfer): void { diff --git a/subgraph/src/mappings/utils/loads.ts b/subgraph/src/mappings/utils/loads.ts index eb24229e..5f046b23 100644 --- a/subgraph/src/mappings/utils/loads.ts +++ b/subgraph/src/mappings/utils/loads.ts @@ -1,6 +1,6 @@ import { Address, BigDecimal, BigInt, Bytes, ethereum, log } from '@graphprotocol/graph-ts' import { LimitPool, LimitPoolFactory, LimitPoolManager, LimitPosition, Token, FeeTier, BasePrice, RangePosition, RangeTick, Transaction, LimitTick, Swap, CompoundRangeLog, MintRangeLog, BurnRangeLog, PoolRouter, TvlUpdateLog, HistoricalOrder, TotalSeasonReward, UserSeasonReward, LimitPoolToken } from '../../../generated/schema' -import { ONE_BD } from '../../constants/constants' +import { ONE_BD } from '../../constants/arbitrum' import { fetchTokenSymbol, fetchTokenName, @@ -479,10 +479,7 @@ export function safeLoadHistoricalOrder(tokenInAddress: string, tokenOutAddress: if (!historicalOrderEntity) { historicalOrderEntity = new HistoricalOrder(historicalOrderId) -<<<<<<< HEAD historicalOrderEntity.positionId = BIGINT_ZERO -======= ->>>>>>> master historicalOrderEntity.tokenIn = tokenInAddress historicalOrderEntity.tokenOut = tokenOutAddress historicalOrderEntity.amountIn = BIGDECIMAL_ZERO diff --git a/subgraph/src/mappings/utils/math.ts b/subgraph/src/mappings/utils/math.ts index 7835a29f..64be669b 100644 --- a/subgraph/src/mappings/utils/math.ts +++ b/subgraph/src/mappings/utils/math.ts @@ -1,6 +1,6 @@ /* eslint-disable prefer-const */ import { BigInt, BigDecimal } from '@graphprotocol/graph-ts' -import { ONE_BI, ZERO_BI, ZERO_BD, ONE_BD } from '../../constants/constants' +import { ONE_BI, ZERO_BI, ZERO_BD, ONE_BD } from '../../constants/arbitrum' export function exponentToBigDecimal(decimals: BigInt): BigDecimal { let bd = BigDecimal.fromString('1') diff --git a/subgraph/src/mappings/utils/price.ts b/subgraph/src/mappings/utils/price.ts index 0b953791..7eba0feb 100644 --- a/subgraph/src/mappings/utils/price.ts +++ b/subgraph/src/mappings/utils/price.ts @@ -1,5 +1,5 @@ /* eslint-disable prefer-const */ -import { MINIMUM_ETH_LOCKED, ONE_BD, STABLE_COINS, STABLE_IS_TOKEN_0, STABLE_POOL_ADDRESS, WETH_ADDRESS, WHITELISTED_TOKENS, ZERO_BD, ZERO_BI } from '../../constants/constants' +import { MINIMUM_ETH_LOCKED, ONE_BD, STABLE_COINS, STABLE_IS_TOKEN_0, STABLE_POOL_ADDRESS, WETH_ADDRESS, WHITELISTED_TOKENS, ZERO_BD, ZERO_BI } from '../../constants/arbitrum' import { BasePrice, LimitPool, Token } from '../../../generated/schema' import { BigDecimal, BigInt, log } from '@graphprotocol/graph-ts' import { exponentToBigDecimal, safeDiv } from './math' diff --git a/subgraph/src/mappings/utils/tvl.ts b/subgraph/src/mappings/utils/tvl.ts index 1fc7ce9e..3017983d 100644 --- a/subgraph/src/mappings/utils/tvl.ts +++ b/subgraph/src/mappings/utils/tvl.ts @@ -3,7 +3,7 @@ import { BigDecimal, log } from '@graphprotocol/graph-ts' import { BasePrice, LimitPoolFactory, LimitPool, Token } from '../../../generated/schema' import { AmountType, getAdjustedAmounts } from './price' -import { ZERO_ADDRESS } from '../../constants/constants' +import { ZERO_ADDRESS } from '../../constants/arbitrum' import { safeLoadBasePrice, safeLoadToken } from './loads' diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 88fcc3d2..4dd1efb8 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -6,7 +6,7 @@ schema: templates: - kind: ethereum/contract name: LimitPoolTemplate - network: arbitrum-goerli + network: arbitrum-one source: abi: LimitPool mapping: @@ -63,7 +63,7 @@ templates: # ERC-1155 events - kind: ethereum/contract name: PositionERC1155Template - network: arbitrum-goerli + network: arbitrum-one source: abi: PositionERC1155 mapping: @@ -86,7 +86,7 @@ templates: # RangeStaker events - kind: ethereum/contract name: RangeStakerTemplate - network: arbitrum-goerli + network: arbitrum-one source: # address: '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' abi: RangeStaker @@ -114,11 +114,11 @@ templates: dataSources: - kind: ethereum/contract name: LimitPoolFactory - network: arbitrum-goerli + network: arbitrum-one source: - address: '0x1b215002e688135549cc0290d6cf1f94e3aa425c' + address: '0x9f479560cd8a531e6c0fe04521cb246264fe6b71' abi: LimitPoolFactory - startBlock: 56821935 + startBlock: 158133516 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -143,11 +143,11 @@ dataSources: handler: handlePoolCreated - kind: ethereum/contract name: LimitPoolManager - network: arbitrum-goerli + network: arbitrum-one source: - address: '0x6d3af137e75097b892683832a2c0132f99625d0e' + address: '0xd50b04a5693f2d026d589bf239609bf5b8346adc' abi: LimitPoolManager - startBlock: 56821935 + startBlock: 158133499 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -178,11 +178,11 @@ dataSources: handler: ProtocolSwapFeesModified - kind: ethereum/contract name: PoolsharkRouter - network: arbitrum-goerli + network: arbitrum-one source: - address: '0x4b5a159109bb997135e9b2bb6e486e2101466881' + address: '0x7183ffeefc0883d499c52c1a1dff1af716467a46' abi: PoolsharkRouter - startBlock: 56821935 + startBlock: 158133499 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -196,5 +196,3 @@ dataSources: eventHandlers: - event: RouterDeployed(address,address,address) handler: handleRouterDeployed - - diff --git a/subgraph/templates/arbitrum-goerli.subgraph.template.yaml b/subgraph/templates/arbitrum-goerli.subgraph.template.yaml new file mode 100644 index 00000000..d36cf0bd --- /dev/null +++ b/subgraph/templates/arbitrum-goerli.subgraph.template.yaml @@ -0,0 +1,198 @@ +specVersion: 0.0.4 +description: Poolshark is a Directional Liquidity AMM allowing LPs to capture directional strength. +repository: https://github.com/poolshark-protocol/limit +schema: + file: ./schema.graphql +templates: + - kind: ethereum/contract + name: LimitPoolTemplate + network: arbitrum-goerli + source: + abi: LimitPool + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpool.ts + entities: + - Token + - LimitPool + - Position + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: Initialize(int24,int24,uint160,int24) + handler: handleInitialize + - event: Swap(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + handler: handleSwap + - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) + handler: handleMintLimit + - event: SampleCountIncreased(uint16) + handler: handleSampleCountIncreased + - event: SampleRecorded(int56,uint160) + handler: handleSampleRecorded + - event: MintRange(indexed address,int24,int24,indexed uint32,uint128,int128,int128) + handler: handleMintRange + - event: BurnRange(indexed address,indexed uint256,uint128,int128,int128) + handler: handleBurnRange + - event: CompoundRange(indexed uint32,uint128) + handler: handleCompoundRange + - event: SyncRangeTick(uint200,uint200,int24) + handler: handleSyncRangeTick + - event: BurnLimit(indexed address,uint32,int24,int24,int24,int24,bool,uint128,uint128,uint128) + handler: handleBurnLimit + - event: SyncLimitPool(uint160,uint128,uint32,int24,bool) + handler: handleSyncLimitPool + - event: SyncLimitLiquidity(uint128,int24,bool) + handler: handleSyncLimitLiquidity + - event: SyncLimitTick(uint32,int24,bool) + handler: handleSyncLimitTick + - event: CollectRange0(uint128) + handler: handleCollectRange0 + - event: CollectRange1(uint128) + handler: handleCollectRange1 + # ERC-1155 events + - kind: ethereum/contract + name: PositionERC1155Template + network: arbitrum-goerli + source: + abi: PositionERC1155 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/positionerc1155.ts + entities: + - Token + - RangePool + - RangePosition + abis: + - name: PositionERC1155 + file: ./abis/PositionERC1155.json + eventHandlers: + - event: TransferSingle(indexed address,indexed address,indexed address,uint256,uint256) + handler: handleTransferSingle + - event: TransferBatch(indexed address,indexed address,indexed address,uint256[],uint256[]) + handler: handleTransferBatch + # RangeStaker events + - kind: ethereum/contract + name: RangeStakerTemplate + network: arbitrum-goerli + source: + # address: '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' + abi: RangeStaker + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/staking/rangestaker.ts + entities: + - RangeStake + abis: + - name: RangeStaker + file: ./abis/RangeStaker.json + eventHandlers: + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: StakeRange(address,uint32,address,uint256,uint256,uint128) + handler: handleStakeRange + - event: StakeRangeAccrued(address,uint32,uint256,uint256) + handler: handleStakeRangeAccrued + - event: UnstakeRange(address,uint32,address) + handler: handleUnstakeRange +dataSources: + - kind: ethereum/contract + name: LimitPoolFactory + network: arbitrum-goerli + source: + address: '0x1b215002e688135549cc0290d6cf1f94e3aa425c' + abi: LimitPoolFactory + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolfactory.ts + entities: + - Token + - LimitPool + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: PoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + handler: handlePoolCreated + - kind: ethereum/contract + name: LimitPoolManager + network: arbitrum-goerli + source: + address: '0x6d3af137e75097b892683832a2c0132f99625d0e' + abi: LimitPoolManager + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolmanager.ts + entities: + - LimitPool + - LimitPoolFactory + abis: + - name: LimitPoolManager + file: ./abis/LimitPoolManager.json + eventHandlers: + - event: FeeTierEnabled(uint16,int16) + handler: handleFeeTierEnabled + - event: PoolTypeEnabled(bytes32,address,address,uint16) + handler: handlePoolTypeEnabled + - event: FactoryChanged(indexed address,indexed address) + handler: handleFactoryChanged + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: ProtocolFeesCollected(address[],uint128[],uint128[]) + handler: handleProtocolFeesCollected + - event: ProtocolFillFeesModified(address[],int16[],int16[]) + handler: handleProtocolFillFeesModified + - event: ProtocolSwapFeesModified(address[],int16[],int16[]) + handler: ProtocolSwapFeesModified + - kind: ethereum/contract + name: PoolsharkRouter + network: arbitrum-goerli + source: + address: '0x4b5a159109bb997135e9b2bb6e486e2101466881' + abi: PoolsharkRouter + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/poolsharkrouter.ts + entities: + - PoolsharkRouter + abis: + - name: PoolsharkRouter + file: ./abis/PoolsharkRouter.json + eventHandlers: + - event: RouterDeployed(address,address,address) + handler: handleRouterDeployed diff --git a/subgraph/templates/arbitrum.subgraph.template.yaml b/subgraph/templates/arbitrum.subgraph.template.yaml new file mode 100644 index 00000000..c26e58a9 --- /dev/null +++ b/subgraph/templates/arbitrum.subgraph.template.yaml @@ -0,0 +1,200 @@ +specVersion: 0.0.4 +description: Poolshark is a Directional Liquidity AMM allowing LPs to capture directional strength. +repository: https://github.com/poolshark-protocol/limit +schema: + file: ./schema.graphql +templates: + - kind: ethereum/contract + name: LimitPoolTemplate + network: arbitrum + source: + abi: LimitPool + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpool.ts + entities: + - Token + - LimitPool + - Position + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: Initialize(int24,int24,uint160,int24) + handler: handleInitialize + - event: Swap(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + handler: handleSwap + - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) + handler: handleMintLimit + - event: SampleCountIncreased(uint16) + handler: handleSampleCountIncreased + - event: SampleRecorded(int56,uint160) + handler: handleSampleRecorded + - event: MintRange(indexed address,int24,int24,indexed uint32,uint128,int128,int128) + handler: handleMintRange + - event: BurnRange(indexed address,indexed uint256,uint128,int128,int128) + handler: handleBurnRange + - event: CompoundRange(indexed uint32,uint128) + handler: handleCompoundRange + - event: SyncRangeTick(uint200,uint200,int24) + handler: handleSyncRangeTick + - event: BurnLimit(indexed address,uint32,int24,int24,int24,int24,bool,uint128,uint128,uint128) + handler: handleBurnLimit + - event: SyncLimitPool(uint160,uint128,uint32,int24,bool) + handler: handleSyncLimitPool + - event: SyncLimitLiquidity(uint128,int24,bool) + handler: handleSyncLimitLiquidity + - event: SyncLimitTick(uint32,int24,bool) + handler: handleSyncLimitTick + - event: CollectRange0(uint128) + handler: handleCollectRange0 + - event: CollectRange1(uint128) + handler: handleCollectRange1 + # ERC-1155 events + - kind: ethereum/contract + name: PositionERC1155Template + network: arbitrum-one + source: + abi: PositionERC1155 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/positionerc1155.ts + entities: + - Token + - RangePool + - RangePosition + abis: + - name: PositionERC1155 + file: ./abis/PositionERC1155.json + eventHandlers: + - event: TransferSingle(indexed address,indexed address,indexed address,uint256,uint256) + handler: handleTransferSingle + - event: TransferBatch(indexed address,indexed address,indexed address,uint256[],uint256[]) + handler: handleTransferBatch + # RangeStaker events + - kind: ethereum/contract + name: RangeStakerTemplate + network: arbitrum-one + source: + # address: '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' + abi: RangeStaker + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/staking/rangestaker.ts + entities: + - RangeStake + abis: + - name: RangeStaker + file: ./abis/RangeStaker.json + eventHandlers: + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: StakeRange(address,uint32,address,uint256,uint256,uint128) + handler: handleStakeRange + - event: StakeRangeAccrued(address,uint32,uint256,uint256) + handler: handleStakeRangeAccrued + - event: UnstakeRange(address,uint32,address) + handler: handleUnstakeRange +dataSources: + - kind: ethereum/contract + name: LimitPoolFactory + network: arbitrum-one + source: + address: '0x9f479560cd8a531e6c0fe04521cb246264fe6b71' + abi: LimitPoolFactory + startBlock: 158133516 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolfactory.ts + entities: + - Token + - LimitPool + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: PoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + handler: handlePoolCreated + - kind: ethereum/contract + name: LimitPoolManager + network: arbitrum-one + source: + address: '0xd50b04a5693f2d026d589bf239609bf5b8346adc' + abi: LimitPoolManager + startBlock: 158133499 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolmanager.ts + entities: + - LimitPool + - LimitPoolFactory + abis: + - name: LimitPoolManager + file: ./abis/LimitPoolManager.json + eventHandlers: + - event: FeeTierEnabled(uint16,int16) + handler: handleFeeTierEnabled + - event: PoolTypeEnabled(bytes32,address,address,uint16) + handler: handlePoolTypeEnabled + - event: FactoryChanged(indexed address,indexed address) + handler: handleFactoryChanged + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: ProtocolFeesCollected(address[],uint128[],uint128[]) + handler: handleProtocolFeesCollected + - event: ProtocolFillFeesModified(address[],int16[],int16[]) + handler: handleProtocolFillFeesModified + - event: ProtocolSwapFeesModified(address[],int16[],int16[]) + handler: ProtocolSwapFeesModified + - kind: ethereum/contract + name: PoolsharkRouter + network: arbitrum-one + source: + address: '0x7183ffeefc0883d499c52c1a1dff1af716467a46' + abi: PoolsharkRouter + startBlock: 158133499 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/poolsharkrouter.ts + entities: + - PoolsharkRouter + abis: + - name: PoolsharkRouter + file: ./abis/PoolsharkRouter.json + eventHandlers: + - event: RouterDeployed(address,address,address) + handler: handleRouterDeployed + + diff --git a/subgraph/templates/subgraph.template.yaml b/subgraph/templates/subgraph.template.yaml new file mode 100644 index 00000000..a96e5db0 --- /dev/null +++ b/subgraph/templates/subgraph.template.yaml @@ -0,0 +1,200 @@ +specVersion: 0.0.4 +description: Poolshark is a Directional Liquidity AMM allowing LPs to capture directional strength. +repository: https://github.com/poolshark-protocol/limit +schema: + file: ./schema.graphql +templates: + - kind: ethereum/contract + name: LimitPoolTemplate + network: {{network}} + source: + abi: LimitPool + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpool.ts + entities: + - Token + - LimitPool + - Position + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: Initialize(int24,int24,uint160,int24) + handler: handleInitialize + - event: Swap(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + handler: handleSwap + - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) + handler: handleMintLimit + - event: SampleCountIncreased(uint16) + handler: handleSampleCountIncreased + - event: SampleRecorded(int56,uint160) + handler: handleSampleRecorded + - event: MintRange(indexed address,int24,int24,indexed uint32,uint128,int128,int128) + handler: handleMintRange + - event: BurnRange(indexed address,indexed uint256,uint128,int128,int128) + handler: handleBurnRange + - event: CompoundRange(indexed uint32,uint128) + handler: handleCompoundRange + - event: SyncRangeTick(uint200,uint200,int24) + handler: handleSyncRangeTick + - event: BurnLimit(indexed address,uint32,int24,int24,int24,int24,bool,uint128,uint128,uint128) + handler: handleBurnLimit + - event: SyncLimitPool(uint160,uint128,uint32,int24,bool) + handler: handleSyncLimitPool + - event: SyncLimitLiquidity(uint128,int24,bool) + handler: handleSyncLimitLiquidity + - event: SyncLimitTick(uint32,int24,bool) + handler: handleSyncLimitTick + - event: CollectRange0(uint128) + handler: handleCollectRange0 + - event: CollectRange1(uint128) + handler: handleCollectRange1 + # ERC-1155 events + - kind: ethereum/contract + name: PositionERC1155Template + network: arbitrum-goerli + source: + abi: PositionERC1155 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/positionerc1155.ts + entities: + - Token + - RangePool + - RangePosition + abis: + - name: PositionERC1155 + file: ./abis/PositionERC1155.json + eventHandlers: + - event: TransferSingle(indexed address,indexed address,indexed address,uint256,uint256) + handler: handleTransferSingle + - event: TransferBatch(indexed address,indexed address,indexed address,uint256[],uint256[]) + handler: handleTransferBatch + # RangeStaker events + - kind: ethereum/contract + name: RangeStakerTemplate + network: arbitrum-goerli + source: + # address: '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' + abi: RangeStaker + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/staking/rangestaker.ts + entities: + - RangeStake + abis: + - name: RangeStaker + file: ./abis/RangeStaker.json + eventHandlers: + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: StakeRange(address,uint32,address,uint256,uint256,uint128) + handler: handleStakeRange + - event: StakeRangeAccrued(address,uint32,uint256,uint256) + handler: handleStakeRangeAccrued + - event: UnstakeRange(address,uint32,address) + handler: handleUnstakeRange +dataSources: + - kind: ethereum/contract + name: LimitPoolFactory + network: arbitrum-goerli + source: + address: '{{0x1b215002e688135549cc0290d6cf1f94e3aa425c}}' + abi: LimitPoolFactory + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolfactory.ts + entities: + - Token + - LimitPool + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: PoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + handler: handlePoolCreated + - kind: ethereum/contract + name: LimitPoolManager + network: arbitrum-goerli + source: + address: '0x6d3af137e75097b892683832a2c0132f99625d0e' + abi: LimitPoolManager + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolmanager.ts + entities: + - LimitPool + - LimitPoolFactory + abis: + - name: LimitPoolManager + file: ./abis/LimitPoolManager.json + eventHandlers: + - event: FeeTierEnabled(uint16,int16) + handler: handleFeeTierEnabled + - event: PoolTypeEnabled(bytes32,address,address,uint16) + handler: handlePoolTypeEnabled + - event: FactoryChanged(indexed address,indexed address) + handler: handleFactoryChanged + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: ProtocolFeesCollected(address[],uint128[],uint128[]) + handler: handleProtocolFeesCollected + - event: ProtocolFillFeesModified(address[],int16[],int16[]) + handler: handleProtocolFillFeesModified + - event: ProtocolSwapFeesModified(address[],int16[],int16[]) + handler: ProtocolSwapFeesModified + - kind: ethereum/contract + name: PoolsharkRouter + network: arbitrum-goerli + source: + address: '0x4b5a159109bb997135e9b2bb6e486e2101466881' + abi: PoolsharkRouter + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/poolsharkrouter.ts + entities: + - PoolsharkRouter + abis: + - name: PoolsharkRouter + file: ./abis/PoolsharkRouter.json + eventHandlers: + - event: RouterDeployed(address,address,address) + handler: handleRouterDeployed + + diff --git a/tasks/deploy/utils/mintPosition.ts b/tasks/deploy/utils/mintPosition.ts index dec8ce86..b9381425 100644 --- a/tasks/deploy/utils/mintPosition.ts +++ b/tasks/deploy/utils/mintPosition.ts @@ -34,8 +34,8 @@ export class MintPosition { const token0Amount = ethers.utils.parseUnits('100', await hre.props.token0.decimals()) const token1Amount = ethers.utils.parseUnits('100', await hre.props.token1.decimals()) - await mintSigners20(hre.props.token0, token0Amount.mul(10000), [hre.props.alice]) - await mintSigners20(hre.props.token1, token1Amount.mul(10000), [hre.props.alice]) + // await mintSigners20(hre.props.token0, token0Amount.mul(10000), [hre.props.alice]) + // await mintSigners20(hre.props.token1, token1Amount.mul(10000), [hre.props.alice]) const liquidityAmount = '49802891105937278098768' @@ -98,32 +98,33 @@ export class MintPosition { // revertMessage: '', // }) // return - // const signer = hre.props.alice + const signer = hre.props.alice // let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, token1Amount.mul(1000)) // await approveTxn.wait() // // for (let i=0; i < 20; i++) { - // const zeroForOne = false - // const amountIn = token1Amount.mul(1000) - // const priceLimit = BigNumber.from('3169126500570573503741758013440') - - // let txn = await hre.props.poolRouter - // .connect(signer) - // .multiSwapSplit( - // [hre.props.limitPool.address], - // [ - // { - // to: signer.address, - // priceLimit: priceLimit, - // amount: amountIn, - // zeroForOne: zeroForOne, - // exactIn: true, - // callbackData: ethers.utils.formatBytes32String('') - // }, - // ], {gasLimit: 3000000}) - // await txn.wait() + const zeroForOne = true + const amountIn = token1Amount.mul(1000) + const priceLimit = BigNumber.from('3848890000000000000000000') + let txn = await hre.props.poolRouter + .connect(signer) + .multiSwapSplit( + ['0xc641fbc8f6ab236249c99bf83b9be34b753c7882'], + [ + { + to: signer.address, + priceLimit: priceLimit, + amount: 1, + zeroForOne: zeroForOne, + exactIn: true, + callbackData: ethers.utils.formatBytes32String('') + }, + ]) + await txn.wait() // } + return + // const globalStateAfter = (await hre.props.limitPool.globalState()) // console.log('sample state', globalStateAfter.pool.samples.index, globalStateAfter.pool.samples.count, globalStateAfter.pool.samples.countMax, globalStateAfter.pool.tickAtPrice) From 07c702733b4b0290485dea639a52ac8ce3b4a5d4 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Fri, 8 Dec 2023 20:16:39 -0700 Subject: [PATCH 03/39] fix range router mint --- contracts/utils/PoolsharkRouter.sol | 14 +++- scripts/autogen/contract-deployments-keys.ts | 76 -------------------- scripts/autogen/contract-deployments.json | 7 +- test/utils/setup/initialSetup.ts | 2 +- 4 files changed, 16 insertions(+), 83 deletions(-) diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index 81582926..d59507fd 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -183,12 +183,20 @@ contract PoolsharkRouter is // decode original sender MintRangeCallbackData memory _data = abi.decode(data, (MintRangeCallbackData)); - // transfer from mint caller + // transfer from swap caller if (amount0Delta < 0) { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + if (constants.token0 == wethAddress && _data.wrapped) { + wrapEth(uint256(-amount0Delta)); + } else { + SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + } } if (amount1Delta < 0) { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + if (constants.token1 == wethAddress && _data.wrapped) { + wrapEth(uint256(-amount1Delta)); + } else { + SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + } } } diff --git a/scripts/autogen/contract-deployments-keys.ts b/scripts/autogen/contract-deployments-keys.ts index 94669ef4..952c1462 100644 --- a/scripts/autogen/contract-deployments-keys.ts +++ b/scripts/autogen/contract-deployments-keys.ts @@ -1,84 +1,8 @@ import { ContractDeploymentsKey } from '../util/files/contractDeploymentsJson' export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ - { - networkName: 'arb_one', - objectName: 'tickMapLib' - }, - { - networkName: 'arb_one', - objectName: 'ticksLib' - }, - { - networkName: 'arb_one', - objectName: 'limitPositionsLib' - }, - { - networkName: 'arb_one', - objectName: 'limitPoolManager' - }, - { - networkName: 'arb_one', - objectName: 'limitPoolFactory' - }, - { - networkName: 'arb_one', - objectName: 'swapCall' - }, - { - networkName: 'arb_one', - objectName: 'mintRangeCall' - }, - { - networkName: 'arb_one', - objectName: 'burnRangeCall' - }, - { - networkName: 'arb_one', - objectName: 'mintLimitCall' - }, - { - networkName: 'arb_one', - objectName: 'burnLimitCall' - }, - { - networkName: 'arb_one', - objectName: 'snapshotLimitCall' - }, - { - networkName: 'arb_one', - objectName: 'quoteCall' - }, - { - networkName: 'arb_one', - objectName: 'feesCall' - }, - { - networkName: 'arb_one', - objectName: 'sampleCall' - }, - { - networkName: 'arb_one', - objectName: 'snapshotRangeCall' - }, - { - networkName: 'arb_one', - objectName: 'limitPoolImpl' - }, - { - networkName: 'arb_one', - objectName: 'positionERC1155' - }, - { - networkName: 'arb_one', - objectName: 'limitPool' - }, { networkName: 'arb_one', objectName: 'poolRouter' }, - { - networkName: 'arb_one', - objectName: 'rangeStaker' - }, ]; \ No newline at end of file diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index 4a350aae..d513c78f 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -155,13 +155,13 @@ }, "poolRouter": { "contractName": "PoolsharkRouter", - "contractAddress": "0x7183ffeefc0883d499c52c1a1dff1af716467a46", + "contractAddress": "0x62e0671022af1b2e705f08b282767c57d29c7c4c", "constructorArguments": [ "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", "0x0000000000000000000000000000000000000000", "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" ], - "created": "2023-12-08T17:57:31.406Z" + "created": "2023-12-09T02:36:07.159Z" }, "rangeStaker": { "contractName": "RangeStaker", @@ -543,5 +543,6 @@ "created": "2023-10-02T23:25:47.853Z" } }, - "undefined": {} + "undefined": { + } } \ No newline at end of file diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index 9004069c..2a214b31 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -32,7 +32,7 @@ export class InitialSetup { private deployContracts = false private deployPools = false private deployRouter = true - private deployStaker = true + private deployStaker = false constructor() { this.deployAssist = new DeployAssist() From c5c333084fe2e1bb28bccc32bf6459f2d885ddc5 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sun, 10 Dec 2023 02:50:45 -0700 Subject: [PATCH 04/39] deploy bond protocol test tokens --- contracts/test/Token20.sol | 4 +++- package.json | 3 ++- scripts/autogen/contract-deployments-keys.ts | 6 +++++- scripts/autogen/contract-deployments.json | 21 ++++++++++++++++++++ test/utils/setup/initialSetup.ts | 1 + 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/contracts/test/Token20.sol b/contracts/test/Token20.sol index 23c062b8..debddd5a 100644 --- a/contracts/test/Token20.sol +++ b/contracts/test/Token20.sol @@ -1,10 +1,11 @@ //SPDX-License-Identifier: Unlicense pragma solidity 0.8.13; +import '@openzeppelin/contracts/access/Ownable.sol'; import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; import '@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol'; -contract Token20 is ERC20, ERC20Burnable { +contract Token20 is ERC20, ERC20Burnable, Ownable { uint8 _decimals; constructor( @@ -12,6 +13,7 @@ contract Token20 is ERC20, ERC20Burnable { string memory tokenSymbol, uint8 decimals_ ) ERC20(tokenName, tokenSymbol) { + _transferOwnership(msg.sender); _decimals = decimals_; } diff --git a/package.json b/package.json index e19f45f2..aa6a798e 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "clean": "rm -rf ./artifacts ./cache ./typechain; npx hardhat clean", "compile": "npx hardhat compile", "test": "npx hardhat test", - "deploy": "rm -rf ./artifacts ./cache ./typechain; npx hardhat clean; npx hardhat compile; npx hardhat deploy-limitpools --network arb_goerli;", + "deploy-test": "rm -rf ./artifacts ./cache ./typechain; npx hardhat clean; npx hardhat compile; npx hardhat deploy-limitpools --network arb_goerli;", + "deploy-main": "npx hardhat deploy-limitpools --network arb_one", "verify": "npx hardhat verify-contracts", "all": "yarn clean; yarn compile; yarn test; yarn deploy;" }, diff --git a/scripts/autogen/contract-deployments-keys.ts b/scripts/autogen/contract-deployments-keys.ts index 952c1462..444b7674 100644 --- a/scripts/autogen/contract-deployments-keys.ts +++ b/scripts/autogen/contract-deployments-keys.ts @@ -3,6 +3,10 @@ import { ContractDeploymentsKey } from '../util/files/contractDeploymentsJson' export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ { networkName: 'arb_one', - objectName: 'poolRouter' + objectName: 'tokenPay' + }, + { + networkName: 'arb_one', + objectName: 'tokenQuote' }, ]; \ No newline at end of file diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index d513c78f..2d113c1a 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -20,6 +20,26 @@ ], "created": "2023-08-23T15:56:24.723Z" }, + "tokenPay": { + "contractName": "Token20", + "contractAddress": "0x3788ce0817512e33130b1ea0490d2957b4ffc406", + "constructorArguments": [ + "Payout Token", + "PAY", + 18 + ], + "created": "2023-12-10T00:19:39.570Z" + }, + "tokenQuote": { + "contractName": "Token20", + "contractAddress": "0xeb590779e7f68e12a48f8b146252a0b0200df16f", + "constructorArguments": [ + "Quote Token", + "QUOTE", + 18 + ], + "created": "2023-12-10T00:19:45.000Z" + }, "weth9": { "contractName": "WETH9", "contractAddress": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", @@ -544,5 +564,6 @@ } }, "undefined": { + } } \ No newline at end of file diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index 2a214b31..2040241d 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -1,3 +1,4 @@ +import { keccak256 } from 'ethers/lib/utils' import { SUPPORTED_NETWORKS } from '../../../scripts/constants/supportedNetworks' import { DeployAssist } from '../../../scripts/util/deployAssist' import { ContractDeploymentsKeys } from '../../../scripts/util/files/contractDeploymentKeys' From 77e5b2ff39617e0cff1889cd2bacdfbe30942380 Mon Sep 17 00:00:00 2001 From: alphak3y <84204260+alphak3y@users.noreply.github.com> Date: Sun, 10 Dec 2023 03:41:00 -0700 Subject: [PATCH 05/39] switch to solidity 0.8.18 (#101) --- contracts/LimitPool.sol | 2 +- contracts/LimitPoolFactory.sol | 2 +- contracts/base/events/FinStakerEvents.sol | 2 +- contracts/base/events/LimitPoolEvents.sol | 2 +- contracts/base/events/LimitPoolFactoryEvents.sol | 2 +- contracts/base/events/LimitPoolManagerEvents.sol | 2 +- contracts/base/events/PoolsharkRouterEvents.sol | 2 +- contracts/base/events/PositionERC1155Events.sol | 2 +- contracts/base/events/RangeStakerEvents.sol | 2 +- contracts/base/storage/LimitPoolFactoryStorage.sol | 2 +- contracts/base/storage/LimitPoolImmutables.sol | 2 +- contracts/base/storage/LimitPoolStorage.sol | 2 +- contracts/base/storage/PositionERC1155Immutables.sol | 2 +- .../external/openzeppelin/security/LimitReentrancyGuard.sol | 2 +- contracts/external/openzeppelin/security/ReentrancyGuard.sol | 2 +- contracts/external/solady/Clone.sol | 2 +- contracts/external/solady/LibClone.sol | 2 +- contracts/interfaces/IERC20Minimal.sol | 2 +- contracts/interfaces/IPool.sol | 2 +- contracts/interfaces/IPositionERC1155.sol | 2 +- contracts/interfaces/IWETH9.sol | 2 +- contracts/interfaces/callbacks/ICoverPoolCallback.sol | 2 +- contracts/interfaces/callbacks/ILimitPoolCallback.sol | 2 +- contracts/interfaces/cover/ICoverPool.sol | 2 +- contracts/interfaces/cover/ICoverPoolFactory.sol | 2 +- contracts/interfaces/cover/ITwapSource.sol | 2 +- contracts/interfaces/limit/ILimitPool.sol | 2 +- contracts/interfaces/limit/ILimitPoolFactory.sol | 2 +- contracts/interfaces/limit/ILimitPoolManager.sol | 2 +- contracts/interfaces/limit/ILimitPoolStorageView.sol | 2 +- contracts/interfaces/limit/ILimitPoolView.sol | 2 +- contracts/interfaces/range/IRangePool.sol | 2 +- contracts/interfaces/range/IRangePoolFactory.sol | 2 +- contracts/interfaces/range/IRangePoolManager.sol | 2 +- contracts/interfaces/staking/IRangeStaker.sol | 2 +- contracts/interfaces/structs/LimitPoolStructs.sol | 2 +- contracts/interfaces/structs/PoolsharkStructs.sol | 2 +- contracts/interfaces/structs/RangePoolStructs.sol | 2 +- contracts/libraries/Samples.sol | 2 +- contracts/libraries/TickMap.sol | 2 +- contracts/libraries/Ticks.sol | 2 +- contracts/libraries/limit/Claims.sol | 2 +- contracts/libraries/limit/EpochMap.sol | 2 +- contracts/libraries/limit/LimitPositions.sol | 2 +- contracts/libraries/limit/LimitTicks.sol | 2 +- contracts/libraries/limit/pool/BurnLimitCall.sol | 2 +- contracts/libraries/limit/pool/MintLimitCall.sol | 2 +- contracts/libraries/limit/pool/SnapshotLimitCall.sol | 2 +- contracts/libraries/math/ConstantProduct.sol | 2 +- contracts/libraries/math/OverflowMath.sol | 2 +- contracts/libraries/pool/FeesCall.sol | 2 +- contracts/libraries/pool/QuoteCall.sol | 2 +- contracts/libraries/pool/SampleCall.sol | 2 +- contracts/libraries/pool/SwapCall.sol | 2 +- contracts/libraries/range/RangePositions.sol | 2 +- contracts/libraries/range/RangeTicks.sol | 2 +- contracts/libraries/range/math/FeeMath.sol | 2 +- contracts/libraries/range/pool/BurnRangeCall.sol | 2 +- contracts/libraries/range/pool/MintRangeCall.sol | 2 +- contracts/libraries/range/pool/SnapshotRangeCall.sol | 2 +- contracts/libraries/utils/Bytes.sol | 2 +- contracts/libraries/utils/Collect.sol | 2 +- contracts/libraries/utils/PositionTokens.sol | 2 +- contracts/libraries/utils/SafeCast.sol | 2 +- contracts/libraries/utils/SafeTransfers.sol | 2 +- contracts/libraries/utils/String.sol | 2 +- contracts/staking/RangeStaker.sol | 2 +- contracts/test/Token20.sol | 2 +- contracts/test/WETH9.sol | 2 +- contracts/utils/LimitPoolManager.sol | 2 +- contracts/utils/PoolsharkRouter.sol | 2 +- contracts/utils/PositionERC1155.sol | 2 +- hardhat.config.ts | 2 +- 73 files changed, 73 insertions(+), 73 deletions(-) diff --git a/contracts/LimitPool.sol b/contracts/LimitPool.sol index 2e905937..f6a3d7be 100644 --- a/contracts/LimitPool.sol +++ b/contracts/LimitPool.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './interfaces/IPool.sol'; import './interfaces/range/IRangePool.sol'; diff --git a/contracts/LimitPoolFactory.sol b/contracts/LimitPoolFactory.sol index 15e707b3..a38bcb93 100644 --- a/contracts/LimitPoolFactory.sol +++ b/contracts/LimitPoolFactory.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './interfaces/range/IRangePool.sol'; import './interfaces/limit/ILimitPool.sol'; diff --git a/contracts/base/events/FinStakerEvents.sol b/contracts/base/events/FinStakerEvents.sol index 64878c85..34e4c826 100644 --- a/contracts/base/events/FinStakerEvents.sol +++ b/contracts/base/events/FinStakerEvents.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; abstract contract FinStakerEvents { event StakeFin( diff --git a/contracts/base/events/LimitPoolEvents.sol b/contracts/base/events/LimitPoolEvents.sol index 188d49b9..81bb8c3a 100644 --- a/contracts/base/events/LimitPoolEvents.sol +++ b/contracts/base/events/LimitPoolEvents.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; abstract contract LimitPoolEvents { event Initialize( diff --git a/contracts/base/events/LimitPoolFactoryEvents.sol b/contracts/base/events/LimitPoolFactoryEvents.sol index 08389743..863e3738 100644 --- a/contracts/base/events/LimitPoolFactoryEvents.sol +++ b/contracts/base/events/LimitPoolFactoryEvents.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; abstract contract LimitPoolFactoryEvents { event PoolCreated( diff --git a/contracts/base/events/LimitPoolManagerEvents.sol b/contracts/base/events/LimitPoolManagerEvents.sol index 56571f91..f38c8a80 100644 --- a/contracts/base/events/LimitPoolManagerEvents.sol +++ b/contracts/base/events/LimitPoolManagerEvents.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/base/events/PoolsharkRouterEvents.sol b/contracts/base/events/PoolsharkRouterEvents.sol index b20b90f6..57d3de58 100644 --- a/contracts/base/events/PoolsharkRouterEvents.sol +++ b/contracts/base/events/PoolsharkRouterEvents.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; abstract contract PoolsharkRouterEvents { event RouterDeployed( diff --git a/contracts/base/events/PositionERC1155Events.sol b/contracts/base/events/PositionERC1155Events.sol index e940d4c7..a02d652c 100644 --- a/contracts/base/events/PositionERC1155Events.sol +++ b/contracts/base/events/PositionERC1155Events.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; abstract contract PositionERC1155Events { event TransferSingle( diff --git a/contracts/base/events/RangeStakerEvents.sol b/contracts/base/events/RangeStakerEvents.sol index 263a74da..d68b35d5 100644 --- a/contracts/base/events/RangeStakerEvents.sol +++ b/contracts/base/events/RangeStakerEvents.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; abstract contract RangeStakerEvents { event StakeRange( diff --git a/contracts/base/storage/LimitPoolFactoryStorage.sol b/contracts/base/storage/LimitPoolFactoryStorage.sol index 67529676..e5b62bcb 100644 --- a/contracts/base/storage/LimitPoolFactoryStorage.sol +++ b/contracts/base/storage/LimitPoolFactoryStorage.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; abstract contract LimitPoolFactoryStorage { mapping(bytes32 => address) public pools; diff --git a/contracts/base/storage/LimitPoolImmutables.sol b/contracts/base/storage/LimitPoolImmutables.sol index c03f9df4..0d14703b 100644 --- a/contracts/base/storage/LimitPoolImmutables.sol +++ b/contracts/base/storage/LimitPoolImmutables.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BSD -pragma solidity 0.8.13; +pragma solidity 0.8.18; import { Clone } from "../../external/solady/Clone.sol"; diff --git a/contracts/base/storage/LimitPoolStorage.sol b/contracts/base/storage/LimitPoolStorage.sol index f484d3e6..ec829d3f 100644 --- a/contracts/base/storage/LimitPoolStorage.sol +++ b/contracts/base/storage/LimitPoolStorage.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/RangePoolStructs.sol'; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/base/storage/PositionERC1155Immutables.sol b/contracts/base/storage/PositionERC1155Immutables.sol index 6a96ed5d..fe0e01a4 100644 --- a/contracts/base/storage/PositionERC1155Immutables.sol +++ b/contracts/base/storage/PositionERC1155Immutables.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BSD -pragma solidity 0.8.13; +pragma solidity 0.8.18; import { Clone } from "../../external/solady/Clone.sol"; diff --git a/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol b/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol index 76e0bc8b..e56d1e28 100644 --- a/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol +++ b/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol) -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/external/openzeppelin/security/ReentrancyGuard.sol b/contracts/external/openzeppelin/security/ReentrancyGuard.sol index 6a182c87..0f6db30e 100644 --- a/contracts/external/openzeppelin/security/ReentrancyGuard.sol +++ b/contracts/external/openzeppelin/security/ReentrancyGuard.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/ReentrancyGuard.sol) -pragma solidity 0.8.13; +pragma solidity 0.8.18; /** * @dev Contract module that helps prevent reentrant calls to a function. diff --git a/contracts/external/solady/Clone.sol b/contracts/external/solady/Clone.sol index 13fdd6ef..c1a04b9e 100644 --- a/contracts/external/solady/Clone.sol +++ b/contracts/external/solady/Clone.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; /// @notice Class with helper read functions for clone with immutable args. /// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/Clone.sol) diff --git a/contracts/external/solady/LibClone.sol b/contracts/external/solady/LibClone.sol index d86a6db7..4b7d9410 100644 --- a/contracts/external/solady/LibClone.sol +++ b/contracts/external/solady/LibClone.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; /// @notice Minimal proxy library. /// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol) diff --git a/contracts/interfaces/IERC20Minimal.sol b/contracts/interfaces/IERC20Minimal.sol index d75e9a1b..f1a4e0b5 100644 --- a/contracts/interfaces/IERC20Minimal.sol +++ b/contracts/interfaces/IERC20Minimal.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-2.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; interface IERC20Minimal { /// @notice Returns the balance of a token diff --git a/contracts/interfaces/IPool.sol b/contracts/interfaces/IPool.sol index d5c70c15..0ba7f8bb 100644 --- a/contracts/interfaces/IPool.sol +++ b/contracts/interfaces/IPool.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/IPositionERC1155.sol b/contracts/interfaces/IPositionERC1155.sol index 6651cba2..2bec3482 100644 --- a/contracts/interfaces/IPositionERC1155.sol +++ b/contracts/interfaces/IPositionERC1155.sol @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; import "@openzeppelin/contracts/utils/introspection/IERC165.sol"; diff --git a/contracts/interfaces/IWETH9.sol b/contracts/interfaces/IWETH9.sol index 8a9df11b..0fb60e76 100644 --- a/contracts/interfaces/IWETH9.sol +++ b/contracts/interfaces/IWETH9.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-2.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; interface IWETH9 { /// @notice Deposits ether in return for wrapped ether diff --git a/contracts/interfaces/callbacks/ICoverPoolCallback.sol b/contracts/interfaces/callbacks/ICoverPoolCallback.sol index bd33aa50..f1ab76c5 100644 --- a/contracts/interfaces/callbacks/ICoverPoolCallback.sol +++ b/contracts/interfaces/callbacks/ICoverPoolCallback.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; /// @title Callback for mints /// @notice Any contract that calls the `mint` function must implement this interface. diff --git a/contracts/interfaces/callbacks/ILimitPoolCallback.sol b/contracts/interfaces/callbacks/ILimitPoolCallback.sol index 9b3407d2..9cf73f54 100644 --- a/contracts/interfaces/callbacks/ILimitPoolCallback.sol +++ b/contracts/interfaces/callbacks/ILimitPoolCallback.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; /// @title Callback for range mints /// @notice Any contract that calls the `mintRange` function must implement this interface. diff --git a/contracts/interfaces/cover/ICoverPool.sol b/contracts/interfaces/cover/ICoverPool.sol index 44c6a00d..fdf6131e 100644 --- a/contracts/interfaces/cover/ICoverPool.sol +++ b/contracts/interfaces/cover/ICoverPool.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity ^0.8.13; +pragma solidity ^0.8.18; import '../structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/cover/ICoverPoolFactory.sol b/contracts/interfaces/cover/ICoverPoolFactory.sol index 644830f7..68be72ca 100644 --- a/contracts/interfaces/cover/ICoverPoolFactory.sol +++ b/contracts/interfaces/cover/ICoverPoolFactory.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; abstract contract ICoverPoolFactory { diff --git a/contracts/interfaces/cover/ITwapSource.sol b/contracts/interfaces/cover/ITwapSource.sol index e5c02aa8..f2c97ecf 100644 --- a/contracts/interfaces/cover/ITwapSource.sol +++ b/contracts/interfaces/cover/ITwapSource.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity ^0.8.13; +pragma solidity ^0.8.18; import '../structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPool.sol b/contracts/interfaces/limit/ILimitPool.sol index eef76c6c..fc266d31 100644 --- a/contracts/interfaces/limit/ILimitPool.sol +++ b/contracts/interfaces/limit/ILimitPool.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPoolFactory.sol b/contracts/interfaces/limit/ILimitPoolFactory.sol index 76a2dad4..04b369ff 100644 --- a/contracts/interfaces/limit/ILimitPoolFactory.sol +++ b/contracts/interfaces/limit/ILimitPoolFactory.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../structs/PoolsharkStructs.sol'; import '../../base/storage/LimitPoolFactoryStorage.sol'; diff --git a/contracts/interfaces/limit/ILimitPoolManager.sol b/contracts/interfaces/limit/ILimitPoolManager.sol index 5df59cb7..61d4d811 100644 --- a/contracts/interfaces/limit/ILimitPoolManager.sol +++ b/contracts/interfaces/limit/ILimitPoolManager.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; /// @notice LimitPoolManager interface interface ILimitPoolManager { diff --git a/contracts/interfaces/limit/ILimitPoolStorageView.sol b/contracts/interfaces/limit/ILimitPoolStorageView.sol index bd6767b5..d6ae0159 100644 --- a/contracts/interfaces/limit/ILimitPoolStorageView.sol +++ b/contracts/interfaces/limit/ILimitPoolStorageView.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPoolView.sol b/contracts/interfaces/limit/ILimitPoolView.sol index f7ad341c..fe892c2e 100644 --- a/contracts/interfaces/limit/ILimitPoolView.sol +++ b/contracts/interfaces/limit/ILimitPoolView.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; diff --git a/contracts/interfaces/range/IRangePool.sol b/contracts/interfaces/range/IRangePool.sol index b3caf43d..55f211d3 100644 --- a/contracts/interfaces/range/IRangePool.sol +++ b/contracts/interfaces/range/IRangePool.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../structs/RangePoolStructs.sol'; import './IRangePoolManager.sol'; diff --git a/contracts/interfaces/range/IRangePoolFactory.sol b/contracts/interfaces/range/IRangePoolFactory.sol index 4f7e3396..b6f991dc 100644 --- a/contracts/interfaces/range/IRangePoolFactory.sol +++ b/contracts/interfaces/range/IRangePoolFactory.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; interface IRangePoolFactory { function createRangePool( diff --git a/contracts/interfaces/range/IRangePoolManager.sol b/contracts/interfaces/range/IRangePoolManager.sol index 11361579..b974dd5e 100644 --- a/contracts/interfaces/range/IRangePoolManager.sol +++ b/contracts/interfaces/range/IRangePoolManager.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../structs/RangePoolStructs.sol'; diff --git a/contracts/interfaces/staking/IRangeStaker.sol b/contracts/interfaces/staking/IRangeStaker.sol index bb068639..a2bbdee0 100644 --- a/contracts/interfaces/staking/IRangeStaker.sol +++ b/contracts/interfaces/staking/IRangeStaker.sol @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/structs/LimitPoolStructs.sol b/contracts/interfaces/structs/LimitPoolStructs.sol index af1ba8a6..08b4ca98 100644 --- a/contracts/interfaces/structs/LimitPoolStructs.sol +++ b/contracts/interfaces/structs/LimitPoolStructs.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './PoolsharkStructs.sol'; diff --git a/contracts/interfaces/structs/PoolsharkStructs.sol b/contracts/interfaces/structs/PoolsharkStructs.sol index c5926621..5af0f588 100644 --- a/contracts/interfaces/structs/PoolsharkStructs.sol +++ b/contracts/interfaces/structs/PoolsharkStructs.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../cover/ITwapSource.sol'; diff --git a/contracts/interfaces/structs/RangePoolStructs.sol b/contracts/interfaces/structs/RangePoolStructs.sol index b68287c4..6d229da0 100644 --- a/contracts/interfaces/structs/RangePoolStructs.sol +++ b/contracts/interfaces/structs/RangePoolStructs.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './PoolsharkStructs.sol'; diff --git a/contracts/libraries/Samples.sol b/contracts/libraries/Samples.sol index f2b3474a..4d784b4b 100644 --- a/contracts/libraries/Samples.sol +++ b/contracts/libraries/Samples.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './math/ConstantProduct.sol'; import './utils/SafeCast.sol'; diff --git a/contracts/libraries/TickMap.sol b/contracts/libraries/TickMap.sol index da97a597..b6c63c85 100644 --- a/contracts/libraries/TickMap.sol +++ b/contracts/libraries/TickMap.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './math/ConstantProduct.sol'; import '../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/libraries/Ticks.sol b/contracts/libraries/Ticks.sol index 8c53c8ab..06695142 100644 --- a/contracts/libraries/Ticks.sol +++ b/contracts/libraries/Ticks.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; import './range/math/FeeMath.sol'; diff --git a/contracts/libraries/limit/Claims.sol b/contracts/libraries/limit/Claims.sol index b06dc62e..1621e203 100644 --- a/contracts/libraries/limit/Claims.sol +++ b/contracts/libraries/limit/Claims.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; import './EpochMap.sol'; diff --git a/contracts/libraries/limit/EpochMap.sol b/contracts/libraries/limit/EpochMap.sol index 0fa89d72..2750fe93 100644 --- a/contracts/libraries/limit/EpochMap.sol +++ b/contracts/libraries/limit/EpochMap.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../math/ConstantProduct.sol'; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/limit/LimitPositions.sol b/contracts/libraries/limit/LimitPositions.sol index 35d71993..0005ff01 100644 --- a/contracts/libraries/limit/LimitPositions.sol +++ b/contracts/libraries/limit/LimitPositions.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './LimitTicks.sol'; import '../../interfaces/IPositionERC1155.sol'; diff --git a/contracts/libraries/limit/LimitTicks.sol b/contracts/libraries/limit/LimitTicks.sol index 75373471..c4d41787 100644 --- a/contracts/libraries/limit/LimitTicks.sol +++ b/contracts/libraries/limit/LimitTicks.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; import '../../interfaces/limit/ILimitPoolFactory.sol'; diff --git a/contracts/libraries/limit/pool/BurnLimitCall.sol b/contracts/libraries/limit/pool/BurnLimitCall.sol index 308a1d84..ede29f72 100644 --- a/contracts/libraries/limit/pool/BurnLimitCall.sol +++ b/contracts/libraries/limit/pool/BurnLimitCall.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; import '../../../interfaces/IPositionERC1155.sol'; diff --git a/contracts/libraries/limit/pool/MintLimitCall.sol b/contracts/libraries/limit/pool/MintLimitCall.sol index 1866f7d7..54b30eba 100644 --- a/contracts/libraries/limit/pool/MintLimitCall.sol +++ b/contracts/libraries/limit/pool/MintLimitCall.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; import '../../../interfaces/callbacks/ILimitPoolCallback.sol'; diff --git a/contracts/libraries/limit/pool/SnapshotLimitCall.sol b/contracts/libraries/limit/pool/SnapshotLimitCall.sol index 4c3799cc..c2df7e30 100644 --- a/contracts/libraries/limit/pool/SnapshotLimitCall.sol +++ b/contracts/libraries/limit/pool/SnapshotLimitCall.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; import '../LimitPositions.sol'; diff --git a/contracts/libraries/math/ConstantProduct.sol b/contracts/libraries/math/ConstantProduct.sol index c40362c3..9ec27abd 100644 --- a/contracts/libraries/math/ConstantProduct.sol +++ b/contracts/libraries/math/ConstantProduct.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './OverflowMath.sol'; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/math/OverflowMath.sol b/contracts/libraries/math/OverflowMath.sol index 1e66376f..bfdc9dc8 100644 --- a/contracts/libraries/math/OverflowMath.sol +++ b/contracts/libraries/math/OverflowMath.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; /// @notice Math library that facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision. library OverflowMath { diff --git a/contracts/libraries/pool/FeesCall.sol b/contracts/libraries/pool/FeesCall.sol index 74158876..fd5349ca 100644 --- a/contracts/libraries/pool/FeesCall.sol +++ b/contracts/libraries/pool/FeesCall.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/IPositionERC1155.sol'; import '../../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/libraries/pool/QuoteCall.sol b/contracts/libraries/pool/QuoteCall.sol index 9f29b46c..09782920 100644 --- a/contracts/libraries/pool/QuoteCall.sol +++ b/contracts/libraries/pool/QuoteCall.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; import '../Ticks.sol'; diff --git a/contracts/libraries/pool/SampleCall.sol b/contracts/libraries/pool/SampleCall.sol index 9ade269e..626ac183 100644 --- a/contracts/libraries/pool/SampleCall.sol +++ b/contracts/libraries/pool/SampleCall.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/RangePoolStructs.sol'; import '../Samples.sol'; diff --git a/contracts/libraries/pool/SwapCall.sol b/contracts/libraries/pool/SwapCall.sol index 88c5c4dd..fd1a7e61 100644 --- a/contracts/libraries/pool/SwapCall.sol +++ b/contracts/libraries/pool/SwapCall.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; import '../../interfaces/callbacks/ILimitPoolCallback.sol'; diff --git a/contracts/libraries/range/RangePositions.sol b/contracts/libraries/range/RangePositions.sol index 812f48e8..92b0b705 100644 --- a/contracts/libraries/range/RangePositions.sol +++ b/contracts/libraries/range/RangePositions.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/IPool.sol'; import '../../interfaces/IPositionERC1155.sol'; diff --git a/contracts/libraries/range/RangeTicks.sol b/contracts/libraries/range/RangeTicks.sol index fb22027d..ed9f7dd1 100644 --- a/contracts/libraries/range/RangeTicks.sol +++ b/contracts/libraries/range/RangeTicks.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/PoolsharkStructs.sol'; import '../../interfaces/structs/RangePoolStructs.sol'; diff --git a/contracts/libraries/range/math/FeeMath.sol b/contracts/libraries/range/math/FeeMath.sol index fc537489..c244b6bb 100644 --- a/contracts/libraries/range/math/FeeMath.sol +++ b/contracts/libraries/range/math/FeeMath.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../Samples.sol'; import '../../utils/SafeCast.sol'; diff --git a/contracts/libraries/range/pool/BurnRangeCall.sol b/contracts/libraries/range/pool/BurnRangeCall.sol index 499deb31..5121d496 100644 --- a/contracts/libraries/range/pool/BurnRangeCall.sol +++ b/contracts/libraries/range/pool/BurnRangeCall.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../../interfaces/structs/RangePoolStructs.sol'; import '../../utils/Collect.sol'; diff --git a/contracts/libraries/range/pool/MintRangeCall.sol b/contracts/libraries/range/pool/MintRangeCall.sol index 4ff9af2f..d13249f8 100644 --- a/contracts/libraries/range/pool/MintRangeCall.sol +++ b/contracts/libraries/range/pool/MintRangeCall.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../../interfaces/structs/RangePoolStructs.sol'; import '../../../interfaces/callbacks/ILimitPoolCallback.sol'; diff --git a/contracts/libraries/range/pool/SnapshotRangeCall.sol b/contracts/libraries/range/pool/SnapshotRangeCall.sol index 1639348e..0b9c2fe2 100644 --- a/contracts/libraries/range/pool/SnapshotRangeCall.sol +++ b/contracts/libraries/range/pool/SnapshotRangeCall.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; import '../RangePositions.sol'; diff --git a/contracts/libraries/utils/Bytes.sol b/contracts/libraries/utils/Bytes.sol index e221f5bd..6906498a 100644 --- a/contracts/libraries/utils/Bytes.sol +++ b/contracts/libraries/utils/Bytes.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; library Bytes { bytes16 private constant alphabet = "0123456789abcdef"; diff --git a/contracts/libraries/utils/Collect.sol b/contracts/libraries/utils/Collect.sol index 4b345377..0aca1b69 100644 --- a/contracts/libraries/utils/Collect.sol +++ b/contracts/libraries/utils/Collect.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; import '../limit/LimitPositions.sol'; diff --git a/contracts/libraries/utils/PositionTokens.sol b/contracts/libraries/utils/PositionTokens.sol index ebfe777d..2d0fae32 100644 --- a/contracts/libraries/utils/PositionTokens.sol +++ b/contracts/libraries/utils/PositionTokens.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './Bytes.sol'; import './String.sol'; diff --git a/contracts/libraries/utils/SafeCast.sol b/contracts/libraries/utils/SafeCast.sol index 7f9a9086..3e8d23bc 100644 --- a/contracts/libraries/utils/SafeCast.sol +++ b/contracts/libraries/utils/SafeCast.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-2.0-or-later -pragma solidity 0.8.13; +pragma solidity 0.8.18; /// @title Safe casting methods /// @notice Contains methods for safely casting between types diff --git a/contracts/libraries/utils/SafeTransfers.sol b/contracts/libraries/utils/SafeTransfers.sol index bc1e2696..5d91471e 100644 --- a/contracts/libraries/utils/SafeTransfers.sol +++ b/contracts/libraries/utils/SafeTransfers.sol @@ -1,5 +1,5 @@ //SPDX-License-Identifier: Unlicense -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; diff --git a/contracts/libraries/utils/String.sol b/contracts/libraries/utils/String.sol index 0f8136e8..273a712a 100644 --- a/contracts/libraries/utils/String.sol +++ b/contracts/libraries/utils/String.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; library String { bytes16 private constant alphabet = "0123456789abcdef"; diff --git a/contracts/staking/RangeStaker.sol b/contracts/staking/RangeStaker.sol index 1c53d8a8..f186cace 100644 --- a/contracts/staking/RangeStaker.sol +++ b/contracts/staking/RangeStaker.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../interfaces/IPool.sol'; import '../interfaces/IPositionERC1155.sol'; diff --git a/contracts/test/Token20.sol b/contracts/test/Token20.sol index debddd5a..90d8e337 100644 --- a/contracts/test/Token20.sol +++ b/contracts/test/Token20.sol @@ -1,5 +1,5 @@ //SPDX-License-Identifier: Unlicense -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '@openzeppelin/contracts/access/Ownable.sol'; import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; diff --git a/contracts/test/WETH9.sol b/contracts/test/WETH9.sol index 9f64e454..95de2323 100644 --- a/contracts/test/WETH9.sol +++ b/contracts/test/WETH9.sol @@ -1,5 +1,5 @@ //SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; contract WETH9 { string public name = "Wrapped Ether"; diff --git a/contracts/utils/LimitPoolManager.sol b/contracts/utils/LimitPoolManager.sol index f555534f..14fb452f 100644 --- a/contracts/utils/LimitPoolManager.sol +++ b/contracts/utils/LimitPoolManager.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../interfaces/IPool.sol'; import '../interfaces/limit/ILimitPool.sol'; diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index d59507fd..cff64209 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../interfaces/IPool.sol'; import '../interfaces/staking/IRangeStaker.sol'; diff --git a/contracts/utils/PositionERC1155.sol b/contracts/utils/PositionERC1155.sol index bfc5ce6b..4d0c3eef 100644 --- a/contracts/utils/PositionERC1155.sol +++ b/contracts/utils/PositionERC1155.sol @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../interfaces/IPool.sol'; import '../base/storage/PositionERC1155Immutables.sol'; diff --git a/hardhat.config.ts b/hardhat.config.ts index 374bd1ac..d5dd2efc 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -14,7 +14,7 @@ const config: HardhatUserConfig = { solidity: { compilers: [ { - version: '0.8.13', + version: '0.8.18', settings: { optimizer: { enabled: true, From e8d080b722baa6b2d6c6f5e50e06a1b20b8df2d4 Mon Sep 17 00:00:00 2001 From: alphak3y <84204260+alphak3y@users.noreply.github.com> Date: Sun, 10 Dec 2023 03:55:50 -0700 Subject: [PATCH 06/39] Dynamic Fee Config (#102) * switch to solidity 0.8.18 * dynamic fee config on manager --- contracts/base/events/LimitPoolManagerEvents.sol | 1 + contracts/interfaces/limit/ILimitPoolManager.sol | 2 +- contracts/libraries/Ticks.sol | 2 +- contracts/libraries/range/math/FeeMath.sol | 11 +++++------ contracts/utils/LimitPoolManager.sol | 15 +++++++++++++++ test/contracts/limitpool.ts | 4 ++-- test/contracts/rangepool.ts | 1 - 7 files changed, 25 insertions(+), 11 deletions(-) diff --git a/contracts/base/events/LimitPoolManagerEvents.sol b/contracts/base/events/LimitPoolManagerEvents.sol index f38c8a80..8d5f8bed 100644 --- a/contracts/base/events/LimitPoolManagerEvents.sol +++ b/contracts/base/events/LimitPoolManagerEvents.sol @@ -5,6 +5,7 @@ import '../../interfaces/structs/PoolsharkStructs.sol'; abstract contract LimitPoolManagerEvents is PoolsharkStructs { event FactoryChanged(address indexed previousFactory, address indexed newFactory); + event FeeDeltaConstChanged(uint16 oldFeeDeltaConst, uint16 newFeeDeltaConst); event PoolTypeEnabled( bytes32 poolTypeName, address poolImpl, diff --git a/contracts/interfaces/limit/ILimitPoolManager.sol b/contracts/interfaces/limit/ILimitPoolManager.sol index 61d4d811..f0a69fc5 100644 --- a/contracts/interfaces/limit/ILimitPoolManager.sol +++ b/contracts/interfaces/limit/ILimitPoolManager.sol @@ -3,9 +3,9 @@ pragma solidity 0.8.18; /// @notice LimitPoolManager interface interface ILimitPoolManager { - function owner() external view returns (address); function feeTo() external view returns (address); + function feeDeltaConst() external view returns (uint16); function poolTypes( uint16 poolType ) external view returns ( diff --git a/contracts/libraries/Ticks.sol b/contracts/libraries/Ticks.sol index 06695142..520a17c9 100644 --- a/contracts/libraries/Ticks.sol +++ b/contracts/libraries/Ticks.sol @@ -276,7 +276,7 @@ library Ticks { PoolsharkStructs.SwapCache memory cache, uint160 priceLimit, bool zeroForOne - ) internal pure returns ( + ) internal view returns ( PoolsharkStructs.SwapCache memory ) { if ((zeroForOne ? priceLimit >= cache.price diff --git a/contracts/libraries/range/math/FeeMath.sol b/contracts/libraries/range/math/FeeMath.sol index c244b6bb..e099f1e1 100644 --- a/contracts/libraries/range/math/FeeMath.sol +++ b/contracts/libraries/range/math/FeeMath.sol @@ -4,6 +4,7 @@ pragma solidity 0.8.18; import '../../Samples.sol'; import '../../utils/SafeCast.sol'; import "../../math/OverflowMath.sol"; +import '../../../interfaces/limit/ILimitPoolManager.sol'; import '../../../interfaces/structs/PoolsharkStructs.sol'; import "../../../interfaces/structs/RangePoolStructs.sol"; @@ -11,9 +12,6 @@ import "../../../interfaces/structs/RangePoolStructs.sol"; library FeeMath { using SafeCast for uint256; - uint256 internal constant FEE_DELTA_CONST = 0; - //TODO: change FEE_DELTA_CONST before launch - // uint256 internal constant FEE_DELTA_CONST = 5000; uint256 internal constant Q128 = 0x100000000000000000000000000000000; struct CalculateLocals { @@ -33,7 +31,7 @@ library FeeMath { uint256 amountIn, uint256 amountOut, bool zeroForOne - ) internal pure returns ( + ) internal view returns ( PoolsharkStructs.SwapCache memory ) { @@ -51,8 +49,9 @@ library FeeMath { locals.lastPrice = locals.minPrice; // delta is % modifier on the swapFee uint256 delta = OverflowMath.mulDiv( - FEE_DELTA_CONST / uint16(cache.constants.tickSpacing), // higher FEE_DELTA_CONST means - ( // more aggressive dynamic fee + ILimitPoolManager(cache.constants.owner).feeDeltaConst() // higher feeDeltaConst means + / uint16(cache.constants.tickSpacing), // more aggressive dynamic fee + ( locals.price > locals.lastPrice ? locals.price - locals.lastPrice : locals.lastPrice - locals.price diff --git a/contracts/utils/LimitPoolManager.sol b/contracts/utils/LimitPoolManager.sol index 14fb452f..bbcb2abf 100644 --- a/contracts/utils/LimitPoolManager.sol +++ b/contracts/utils/LimitPoolManager.sol @@ -15,6 +15,9 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { address public owner; address public feeTo; address public factory; + // fee delta const for dynamic fees + uint16 public feeDeltaConst; + // max protocol fees uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; /// @dev - max protocol swap fee of 100% uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; /// @dev - max protocol fill fee of 1% // impl name => impl address @@ -48,6 +51,10 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { emit FeeTierEnabled(1000, 10); emit FeeTierEnabled(3000, 30); emit FeeTierEnabled(10000, 100); + + // set initial fee delta const + feeDeltaConst = 0; + emit FeeDeltaConstChanged(0, 0); } /** @@ -135,6 +142,14 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { factory = factory_; } + function setFeeDeltaConst( + uint16 feeDeltaConst_ + ) external onlyOwner { + if (feeDeltaConst_ > 10000) require (false, 'FeeDeltaConstCeilingExceeded()'); + emit FeeDeltaConstChanged(feeDeltaConst, feeDeltaConst_); + feeDeltaConst = feeDeltaConst_; + } + function collectProtocolFees( address[] calldata pools ) external onlyOwnerOrFeeTo { diff --git a/test/contracts/limitpool.ts b/test/contracts/limitpool.ts index 3db63cc6..7ad94a83 100644 --- a/test/contracts/limitpool.ts +++ b/test/contracts/limitpool.ts @@ -6948,7 +6948,7 @@ describe('LimitPool Tests', function () { zeroForOne: false, balanceInIncrease: "364", balanceOutIncrease: "0", - lowerTickCleared: true, //TODO: double check this + lowerTickCleared: true, upperTickCleared: true, revertMessage: "", }); @@ -7225,7 +7225,7 @@ describe('LimitPool Tests', function () { balanceInDecrease: tokenAmount, positionLiquidityChange: '24296516120648317604804', liquidityIncrease: "46581025846542819175371", - balanceOutIncrease: "51167329561763357462", //TODO: why is this number different on this side + balanceOutIncrease: "51167329561763357462", upperTickCleared: false, lowerTickCleared: true, revertMessage: "", diff --git a/test/contracts/rangepool.ts b/test/contracts/rangepool.ts index 169fc728..e69bacb8 100644 --- a/test/contracts/rangepool.ts +++ b/test/contracts/rangepool.ts @@ -3221,5 +3221,4 @@ describe('RangePool Exact Out Tests', function () { console.log('balance after token1:', (await hre.props.token1.balanceOf(hre.props.limitPool.address)).toString()) } }) - //TODO: swapping to boundary price w/ exactOut }) \ No newline at end of file From 8a8c4deef998a2b867f2f120bf03ba9f8571a363 Mon Sep 17 00:00:00 2001 From: alphak3y <84204260+alphak3y@users.noreply.github.com> Date: Sun, 10 Dec 2023 03:59:05 -0700 Subject: [PATCH 07/39] Deploy Single Pool; Set Pool Price for Arb Mainnet (#103) * switch to solidity 0.8.18 * dynamic fee config on manager * deploy single pool; use correct start price --- test/utils/setup/initialSetup.ts | 42 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index 2040241d..d9061c2f 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -385,7 +385,7 @@ export class InitialSetup { tokenIn: hre.props.token0.address, tokenOut: hre.props.token1.address, swapFee: '1000', - startPrice: '1738267302024796147492397123192298' + startPrice: '3825621589765822368824140' }); await createPoolTxn.wait(); @@ -399,28 +399,28 @@ export class InitialSetup { ) hre.nonce += 1; + // @dev - skip 0.3% and 1% fee tiers for now + // createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ + // poolTypeId: 0, + // tokenIn: hre.props.token0.address, + // tokenOut: hre.props.token1.address, + // swapFee: '3000', + // startPrice: '1738267302024796147492397123192298' + // }); + // await createPoolTxn.wait(); - createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ - poolTypeId: 0, - tokenIn: hre.props.token0.address, - tokenOut: hre.props.token1.address, - swapFee: '3000', - startPrice: '1738267302024796147492397123192298' - }); - await createPoolTxn.wait(); - - hre.nonce += 1; + // hre.nonce += 1; - createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ - poolTypeId: 0, - tokenIn: hre.props.token0.address, - tokenOut: hre.props.token1.address, - swapFee: '10000', - startPrice: '1738267302024796147492397123192298' - }); - await createPoolTxn.wait(); - - hre.nonce += 1; + // createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ + // poolTypeId: 0, + // tokenIn: hre.props.token0.address, + // tokenOut: hre.props.token1.address, + // swapFee: '10000', + // startPrice: '1738267302024796147492397123192298' + // }); + // await createPoolTxn.wait(); + + // hre.nonce += 1; } hre.props.limitPool = await hre.ethers.getContractAt('LimitPool', limitPoolAddress) From 14aaad16a17e14cb4b88afe21d6e9679107e5ac9 Mon Sep 17 00:00:00 2001 From: alphak3y <84204260+alphak3y@users.noreply.github.com> Date: Sun, 10 Dec 2023 06:09:42 -0700 Subject: [PATCH 08/39] `transferInto` Balance Check Removed (#104) * safe transfers balance check removed * test transferInto address(0) * remove it only * get correct expected position id * add burn for testing --- contracts/libraries/utils/SafeTransfers.sol | 11 +--- contracts/test/Token20.sol | 5 ++ test/contracts/wethpool.ts | 64 +++++++++++++++++++++ test/utils/contracts/limitpool.ts | 3 +- test/utils/contracts/rangepool.ts | 6 +- 5 files changed, 77 insertions(+), 12 deletions(-) diff --git a/contracts/libraries/utils/SafeTransfers.sol b/contracts/libraries/utils/SafeTransfers.sol index 5d91471e..81a2822f 100644 --- a/contracts/libraries/utils/SafeTransfers.sol +++ b/contracts/libraries/utils/SafeTransfers.sol @@ -62,13 +62,11 @@ library SafeTransfers { * See here: https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca */ // slither-disable-next-line assembly - function transferInto(address token, address sender, uint256 amount) internal returns (uint256) { + function transferInto(address token, address sender, uint256 amount) internal { if (token == address(0)) { - if (msg.value < amount) require(false, 'SafeTransfers::LowEthAmountSent()'); - return amount; + require(false, 'SafeTransfers::CannotTransferInEth()'); } IERC20 erc20Token = IERC20(token); - uint256 balanceBefore = IERC20(token).balanceOf(address(this)); /// @dev - msg.sender here is the pool erc20Token.transferFrom(sender, msg.sender, amount); @@ -91,10 +89,5 @@ library SafeTransfers { } } if (!success) require(false, 'TransferFailed(msg.sender, address(this)'); - - // Calculate the amount that was *actually* transferred - uint256 balanceAfter = IERC20(token).balanceOf(address(this)); - - return balanceAfter - balanceBefore; // underflow already checked above, just subtract } } diff --git a/contracts/test/Token20.sol b/contracts/test/Token20.sol index 90d8e337..f387b8dd 100644 --- a/contracts/test/Token20.sol +++ b/contracts/test/Token20.sol @@ -4,6 +4,7 @@ pragma solidity 0.8.18; import '@openzeppelin/contracts/access/Ownable.sol'; import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; import '@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol'; +import '../libraries/utils/SafeTransfers.sol'; contract Token20 is ERC20, ERC20Burnable, Ownable { uint8 _decimals; @@ -24,4 +25,8 @@ contract Token20 is ERC20, ERC20Burnable, Ownable { function decimals() public view override returns (uint8) { return _decimals; } + + function transferIntoTest() external { + SafeTransfers.transferInto(address(0), address(0), 0); + } } diff --git a/test/contracts/wethpool.ts b/test/contracts/wethpool.ts index b8bc9f3c..da8c7774 100644 --- a/test/contracts/wethpool.ts +++ b/test/contracts/wethpool.ts @@ -5,6 +5,7 @@ import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' import { BigNumber } from 'ethers' import { mintSigners20 } from '../utils/token' import { + BN_ONE, BN_ZERO, LimitPoolState, getLiquidity, @@ -207,4 +208,67 @@ describe('WethPool Tests', function () { if (debugMode) console.log('pool eth balance after collect:', (await ethers.provider.getBalance(hre.props.limitPool.address)).toString()) if (debugMode) console.log('pool token eth balance after:', (await ethers.provider.getBalance(hre.props.limitPoolToken.address)).toString()) }) + + it('pool - send too low of ETH amount in msg.value', async function () { + + const wethToken0 = hre.props.weth9.address == (await hre.props.wethPool.token0()) + + if (wethToken0) { + let globalState = await hre.props.wethPool.globalState() + if (debugMode) console.log('weth pool start:', globalState.pool.price.toString(), globalState.pool.liquidity.toString()) + const aliceLiquidity = '10405966812730338361' + // mint should revert + const aliceId = await validateMintRange({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '69080', + upper: '80070', + amount0: BigNumber.from(tokenAmount), + amount1: BigNumber.from(tokenAmount), + balance0Decrease: BigNumber.from('7356461269128718'), + balance1Decrease: BigNumber.from('99999999999999999991'), + liquidityIncrease: BigNumber.from("5202983406365169180"), + revertMessage: '', + poolContract: hre.props.wethPool, + poolTokenContract: hre.props.wethPoolToken + }) + // globalState = await hre.props.wethPool.globalState() + // console.log('weth pool swap1:', globalState.pool.price.toString(), globalState.pool.liquidity.toString()) + + // no-op swap + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: true, + amountIn: tokenAmountBn.mul(2), + priceLimit: minPrice, + balanceInDecrease: '396087570498016', // only gas is used; all other ETH is returned + balanceOutIncrease: '0', + revertMessage: 'WrapEth::LowEthBalance()', + nativeIn: true, + poolContract: hre.props.wethPool, + gasUsed: '396087570498016', + customMsgValue: BigNumber.from(2) + }) + + await expect( + hre.props.token0 + .connect(hre.props.alice) + .transferIntoTest() + ).to.be.revertedWith('SafeTransfers::CannotTransferInEth()'); + + await validateBurnRange({ + signer: hre.props.alice, + positionId: aliceId, + lower: '69080', + upper: '80070', + liquidityAmount: BigNumber.from("5202983406365169180"), + balance0Increase: BigNumber.from('7356461269128717'), + balance1Increase: BigNumber.from('99999999999999999990'), + revertMessage: '', + poolContract: hre.props.wethPool, + poolTokenContract: hre.props.wethPoolToken, + }) + } + }) }) \ No newline at end of file diff --git a/test/utils/contracts/limitpool.ts b/test/utils/contracts/limitpool.ts index 1ccf22e7..4a3c4e90 100644 --- a/test/utils/contracts/limitpool.ts +++ b/test/utils/contracts/limitpool.ts @@ -130,6 +130,7 @@ export interface ValidateSwapParams { nativeOut?: boolean poolContract?: LimitPool gasUsed?: string + customMsgValue?: BigNumber } export interface ValidateBurnParams { @@ -374,7 +375,7 @@ export async function validateSwap(params: ValidateSwapParams) { priceLimit: priceLimit, exactIn: true, callbackData: ethers.utils.formatBytes32String('') - }], {gasLimit: 3000000, value: getSwapMsgValue(nativeIn, nativeOut, amountIn)}) + }], {gasLimit: 3000000, value: params.customMsgValue ?? getSwapMsgValue(nativeIn, nativeOut, amountIn)}) ).to.be.revertedWith(revertMessage) return } diff --git a/test/utils/contracts/rangepool.ts b/test/utils/contracts/rangepool.ts index 34155515..99581c74 100644 --- a/test/utils/contracts/rangepool.ts +++ b/test/utils/contracts/rangepool.ts @@ -53,6 +53,7 @@ export interface ValidateMintParams { poolContract?: LimitPool poolTokenContract?: PositionERC1155 stake?: boolean + customMsgValue?: BigNumber } export interface ValidateSampleParams { @@ -325,10 +326,11 @@ export async function validateMint(params: ValidateMintParams): Promise const revertMessage = params.revertMessage const collectRevertMessage = params.collectRevertMessage const positionId = params.positionId ? params.positionId : 0 - const expectedPositionId = params.positionId ? params.positionId - : (await hre.props.limitPool.globalState()).positionIdNext const poolContract = params.poolContract ?? hre.props.limitPool const poolTokenContract = params.poolTokenContract ?? hre.props.limitPoolToken + const expectedPositionId = params.positionId ? params.positionId + : (await poolContract.globalState()).positionIdNext + const stake = params.stake ?? false let balance0Before From c2961e6cc1e92776fc58ce11f470013cf606721c Mon Sep 17 00:00:00 2001 From: alphak3y <84204260+alphak3y@users.noreply.github.com> Date: Sun, 10 Dec 2023 07:02:26 -0700 Subject: [PATCH 09/39] Return Amount Deltas (#105) * return amount deltas * range amount deltas --- contracts/LimitPool.sol | 28 +++++++++++++------ contracts/interfaces/limit/ILimitPool.sol | 10 +++++-- contracts/interfaces/range/IRangePool.sol | 10 +++++-- .../libraries/limit/pool/BurnLimitCall.sol | 22 +++++++++++++-- .../libraries/limit/pool/MintLimitCall.sol | 10 ++++++- .../libraries/range/pool/BurnRangeCall.sol | 12 +++++++- .../libraries/range/pool/MintRangeCall.sol | 11 +++++++- contracts/libraries/utils/Collect.sol | 8 ++++-- 8 files changed, 92 insertions(+), 19 deletions(-) diff --git a/contracts/LimitPool.sol b/contracts/LimitPool.sol index f6a3d7be..6ed46041 100644 --- a/contracts/LimitPool.sol +++ b/contracts/LimitPool.sol @@ -80,10 +80,13 @@ contract LimitPool is ) external nonReentrant(globalState) canonicalOnly - { + returns ( + int256, + int256 + ) { MintRangeCache memory cache; cache.constants = immutables(); - MintRangeCall.perform( + return MintRangeCall.perform( positions, ticks, rangeTickMap, @@ -99,10 +102,13 @@ contract LimitPool is ) external nonReentrant(globalState) canonicalOnly - { + returns ( + int256, + int256 + ) { BurnRangeCache memory cache; cache.constants = immutables(); - BurnRangeCall.perform( + return BurnRangeCall.perform( positions, ticks, rangeTickMap, @@ -119,10 +125,13 @@ contract LimitPool is ) external nonReentrant(globalState) canonicalOnly - { + returns ( + int256, + int256 + ) { MintLimitCache memory cache; cache.constants = immutables(); - MintLimitCall.perform( + return MintLimitCall.perform( params.zeroForOne ? positions0 : positions1, ticks, samples, @@ -139,10 +148,13 @@ contract LimitPool is ) external nonReentrant(globalState) canonicalOnly - { + returns ( + int256, + int256 + ) { BurnLimitCache memory cache; cache.constants = immutables(); - BurnLimitCall.perform( + return BurnLimitCall.perform( params.zeroForOne ? positions0 : positions1, ticks, limitTickMap, diff --git a/contracts/interfaces/limit/ILimitPool.sol b/contracts/interfaces/limit/ILimitPool.sol index fc266d31..a6dc24ef 100644 --- a/contracts/interfaces/limit/ILimitPool.sol +++ b/contracts/interfaces/limit/ILimitPool.sol @@ -10,11 +10,17 @@ interface ILimitPool is LimitPoolStructs { function mintLimit( MintLimitParams memory params - ) external; + ) external returns ( + int256, + int256 + ); function burnLimit( BurnLimitParams memory params - ) external; + ) external returns ( + int256, + int256 + ); function fees( FeesParams memory params diff --git a/contracts/interfaces/range/IRangePool.sol b/contracts/interfaces/range/IRangePool.sol index 55f211d3..ea7048bd 100644 --- a/contracts/interfaces/range/IRangePool.sol +++ b/contracts/interfaces/range/IRangePool.sol @@ -7,11 +7,17 @@ import './IRangePoolManager.sol'; interface IRangePool is RangePoolStructs { function mintRange( MintRangeParams memory mintParams - ) external; + ) external returns ( + int256, + int256 + ); function burnRange( BurnRangeParams memory burnParams - ) external; + ) external returns ( + int256, + int256 + ); function swap( SwapParams memory params diff --git a/contracts/libraries/limit/pool/BurnLimitCall.sol b/contracts/libraries/limit/pool/BurnLimitCall.sol index ede29f72..40e1c199 100644 --- a/contracts/libraries/limit/pool/BurnLimitCall.sol +++ b/contracts/libraries/limit/pool/BurnLimitCall.sol @@ -8,6 +8,8 @@ import '../../utils/Collect.sol'; import '../../utils/PositionTokens.sol'; library BurnLimitCall { + using SafeCast for uint128; + event BurnLimit( address indexed to, uint32 positionId, @@ -21,6 +23,11 @@ library BurnLimitCall { uint128 tokenOutBurned ); + struct BurnLimitLocals { + int128 amount0Delta; + int128 amount1Delta; + } + function perform( mapping(uint256 => LimitPoolStructs.LimitPosition) storage positions, @@ -29,7 +36,11 @@ library BurnLimitCall { PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache - ) external { + ) external returns ( + int256, // amount0Delta + int256 // amount1Delta + ) + { // check for invalid receiver if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); @@ -76,10 +87,17 @@ library BurnLimitCall { // save state before transfer call save(cache, globalState, params.zeroForOne); - cache = Collect.burnLimit( + BurnLimitLocals memory locals; + ( + cache, + locals.amount0Delta, + locals.amount1Delta + ) = Collect.burnLimit( cache, params ); + + return (locals.amount0Delta, locals.amount1Delta); } function save( diff --git a/contracts/libraries/limit/pool/MintLimitCall.sol b/contracts/libraries/limit/pool/MintLimitCall.sol index 54b30eba..38b7870f 100644 --- a/contracts/libraries/limit/pool/MintLimitCall.sol +++ b/contracts/libraries/limit/pool/MintLimitCall.sol @@ -38,7 +38,10 @@ library MintLimitCall { PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.MintLimitParams memory params, LimitPoolStructs.MintLimitCache memory cache - ) external { + ) external returns ( + int256, // amount0Delta + int256 // amount1Delta + ){ // check for invalid receiver if (params.to == address(0)) require(false, "CollectToZeroAddress()"); @@ -168,6 +171,11 @@ library MintLimitCall { // check balance requirements after callback if (balance(params, cache) < balanceStart + params.amount + cache.swapCache.input) require(false, 'MintInputAmountTooLow()'); + + return ( + params.zeroForOne ? -int256(params.amount + cache.swapCache.input) : int256(cache.swapCache.output), + params.zeroForOne ? int256(cache.swapCache.output) : -int256(params.amount + cache.swapCache.input) + ); } function save( diff --git a/contracts/libraries/range/pool/BurnRangeCall.sol b/contracts/libraries/range/pool/BurnRangeCall.sol index 5121d496..732ae629 100644 --- a/contracts/libraries/range/pool/BurnRangeCall.sol +++ b/contracts/libraries/range/pool/BurnRangeCall.sol @@ -28,7 +28,11 @@ library BurnRangeCall { PoolsharkStructs.GlobalState storage globalState, RangePoolStructs.BurnRangeCache memory cache, RangePoolStructs.BurnRangeParams memory params - ) external { + ) external returns ( + int256, + int256 + ) + { // check for invalid receiver if (params.to == address(0)) require(false, "CollectToZeroAddress()"); @@ -100,6 +104,12 @@ library BurnRangeCall { cache.amount0, cache.amount1 ); + + // return amount deltas + return ( + cache.amount0, + cache.amount1 + ); } function save( diff --git a/contracts/libraries/range/pool/MintRangeCall.sol b/contracts/libraries/range/pool/MintRangeCall.sol index d13249f8..7aa99b2c 100644 --- a/contracts/libraries/range/pool/MintRangeCall.sol +++ b/contracts/libraries/range/pool/MintRangeCall.sol @@ -38,7 +38,11 @@ library MintRangeCall { PoolsharkStructs.GlobalState storage globalState, RangePoolStructs.MintRangeCache memory cache, RangePoolStructs.MintRangeParams memory params - ) external { + ) external returns ( + int256, // amount0Delta + int256 // amount1Delta + ) + { // check for invalid receiver if (params.to == address(0)) require(false, "CollectToZeroAddress()"); @@ -150,6 +154,11 @@ library MintRangeCall { if (cache.amount1 < 0) if (balance1(cache) < startBalance.amount1 + (-cache.amount1).toUint128()) require(false, 'MintInputAmount1TooLow()'); + + return ( + cache.amount0 + cache.feesAccrued0, + cache.amount1 + cache.feesAccrued1 + ); } function save( diff --git a/contracts/libraries/utils/Collect.sol b/contracts/libraries/utils/Collect.sol index 0aca1b69..5c8bb3b6 100644 --- a/contracts/libraries/utils/Collect.sol +++ b/contracts/libraries/utils/Collect.sol @@ -7,6 +7,7 @@ import '../utils/SafeTransfers.sol'; library Collect { using SafeCast for int128; + using SafeCast for uint128; event CollectRange0( uint128 amount0 @@ -33,13 +34,16 @@ library Collect { SafeTransfers.transferOut(recipient, constants.token1, amount1.toUint128()); emit CollectRange1(amount1.toUint128()); } + } function burnLimit( LimitPoolStructs.BurnLimitCache memory cache, PoolsharkStructs.BurnLimitParams memory params ) internal returns ( - LimitPoolStructs.BurnLimitCache memory + LimitPoolStructs.BurnLimitCache memory, + int128 amount0Delta, + int128 amount1Delta ) { uint128 amount0 = params.zeroForOne ? cache.amountOut : cache.amountIn; @@ -54,6 +58,6 @@ library Collect { cache.amountOut = 0; SafeTransfers.transferOut(params.to, cache.constants.token1, amount1); } - return cache; + return (cache, amount0.toInt128(), amount1.toInt128()); } } From 4b9069c0c8ce2fe9ce03048e144201dfef745b2b Mon Sep 17 00:00:00 2001 From: alphak3y <84204260+alphak3y@users.noreply.github.com> Date: Sun, 10 Dec 2023 08:03:45 -0700 Subject: [PATCH 10/39] original only for createlimitpool (#107) --- contracts/LimitPoolFactory.sol | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/contracts/LimitPoolFactory.sol b/contracts/LimitPoolFactory.sol index a38bcb93..a8aacfb6 100644 --- a/contracts/LimitPoolFactory.sol +++ b/contracts/LimitPoolFactory.sol @@ -33,9 +33,16 @@ contract LimitPoolFactory is original = address(this); } + modifier originalOnly() { + _onlyOriginal(); + _; + } + function createLimitPool( LimitPoolParams memory params - ) public override returns ( + ) public override + originalOnly + returns ( address pool, address poolToken ) { @@ -172,4 +179,8 @@ contract LimitPoolFactory is return (pool, poolToken); } + + function _onlyOriginal() private view { + if (address(this) != original) require(false, 'OriginalOnly()'); + } } From b308ca6933bc026df9a2affaeec7fda4823f4ff1 Mon Sep 17 00:00:00 2001 From: alphak3y <84204260+alphak3y@users.noreply.github.com> Date: Sun, 10 Dec 2023 08:12:51 -0700 Subject: [PATCH 11/39] Router: Block `receive()` for non-WETH addresses (#106) * router msg value check * return if eth amount 0; block receives from non-weth address --- contracts/libraries/utils/SafeTransfers.sol | 2 +- contracts/utils/PoolsharkRouter.sol | 46 ++++++++++----------- test/contracts/wethpool.ts | 7 ++++ 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/contracts/libraries/utils/SafeTransfers.sol b/contracts/libraries/utils/SafeTransfers.sol index 81a2822f..5dae1fbf 100644 --- a/contracts/libraries/utils/SafeTransfers.sol +++ b/contracts/libraries/utils/SafeTransfers.sol @@ -20,12 +20,12 @@ library SafeTransfers { uint256 amount ) internal { bool success; + if (amount == 0) return; if (token == address(0)) { (success, ) = to.call{value: amount}(""); if (!success) require(false, "SafeTransfers::EthTransferFailed()"); return; } - if (amount == 0) return; IERC20 erc20Token = IERC20(token); // ? We are checking the transfer, but since we are doing so in an assembly block // ? Slither does not pick up on that and results in a hit diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index cff64209..646b398b 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -80,7 +80,11 @@ contract PoolsharkRouter is ); } - receive() external payable {} + receive() external payable { + if (msg.sender != wethAddress) { + require(false, "PoolsharkRouter::ReceiveInvalid()"); + } + } /// @inheritdoc ILimitPoolSwapCallback function limitPoolSwapCallback( @@ -277,10 +281,7 @@ contract PoolsharkRouter is ++i; } } - if (address(this).balance > 0) { - // return eth balance to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } function multiMintRange( @@ -316,10 +317,7 @@ contract PoolsharkRouter is ++i; } } - if (address(this).balance > 0) { - // return eth balance to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } function multiMintCover( @@ -338,10 +336,7 @@ contract PoolsharkRouter is ++i; } } - if (address(this).balance > 0) { - // return eth balance to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } function multiQuote( @@ -439,10 +434,7 @@ contract PoolsharkRouter is ++i; } } - if (address(this).balance > 0) { - // return eth balance to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } function multiSnapshotLimit( @@ -531,10 +523,7 @@ contract PoolsharkRouter is ++i; } } - if (address(this).balance > 0) { - // send remaining eth to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } function createCoverPoolAndMint( @@ -573,10 +562,7 @@ contract PoolsharkRouter is ++i; } } - if (address(this).balance > 0) { - // send remaining eth to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } struct SortQuoteResultsLocals { @@ -787,4 +773,14 @@ contract PoolsharkRouter is // send balance to recipient SafeTransfers.transferOut(recipient, ethAddress, amount); } + + function refundEth() private { + if (address(this).balance > 0) { + if (address(this).balance >= msg.value) { + SafeTransfers.transferOut(msg.sender, ethAddress, msg.value); + } else { + SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); + } + } + } } \ No newline at end of file diff --git a/test/contracts/wethpool.ts b/test/contracts/wethpool.ts index da8c7774..b931711a 100644 --- a/test/contracts/wethpool.ts +++ b/test/contracts/wethpool.ts @@ -257,6 +257,13 @@ describe('WethPool Tests', function () { .transferIntoTest() ).to.be.revertedWith('SafeTransfers::CannotTransferInEth()'); + await expect( + hre.props.alice.sendTransaction({ + to: hre.props.poolRouter.address, + value: ethers.utils.parseEther("50.0") + }) + ).to.be.revertedWith('PoolsharkRouter::ReceiveInvalid()'); + await validateBurnRange({ signer: hre.props.alice, positionId: aliceId, From 1fcd0e59ac4848bf4176fe01e531e89783f665b2 Mon Sep 17 00:00:00 2001 From: alphak3y <84204260+alphak3y@users.noreply.github.com> Date: Sun, 10 Dec 2023 15:07:45 -0700 Subject: [PATCH 12/39] Documentation; Code Formatting (#108) * cleanup before prettier * format all contracts * document interfaces * add comments to major contracts; protocol fee testing * prettier contracts * exclude coverage --- .prettierrc | 20 +- .solcover.js | 2 +- contracts/LimitPool.sol | 407 +++++++------ contracts/LimitPoolFactory.sol | 103 ++-- contracts/base/events/FinStakerEvents.sol | 29 - contracts/base/events/LimitPoolEvents.sol | 49 +- .../base/events/LimitPoolFactoryEvents.sol | 2 + .../base/events/LimitPoolManagerEvents.sol | 45 +- .../base/events/PoolsharkRouterEvents.sol | 2 + .../base/events/PositionERC1155Events.sol | 4 + contracts/base/events/RangeStakerEvents.sol | 12 +- .../base/storage/LimitPoolFactoryStorage.sol | 6 +- .../base/storage/LimitPoolImmutables.sol | 4 +- contracts/base/storage/LimitPoolStorage.sol | 2 +- .../storage/PositionERC1155Immutables.sol | 4 +- .../security/LimitReentrancyGuard.sol | 8 +- .../openzeppelin/security/ReentrancyGuard.sol | 2 +- contracts/external/solady/Clone.sol | 205 +++++-- contracts/external/solady/LibClone.sol | 54 +- contracts/interfaces/IERC20Minimal.sol | 2 +- contracts/interfaces/IPool.sol | 95 ++- contracts/interfaces/IPositionERC1155.sol | 24 +- contracts/interfaces/IWETH9.sol | 6 +- .../callbacks/ICoverPoolCallback.sol | 3 +- .../callbacks/ILimitPoolCallback.sol | 2 +- contracts/interfaces/cover/ICoverPool.sol | 41 +- .../interfaces/cover/ICoverPoolFactory.sol | 24 +- contracts/interfaces/cover/ITwapSource.sol | 29 +- contracts/interfaces/limit/ILimitPool.sol | 31 +- .../interfaces/limit/ILimitPoolFactory.sol | 24 +- .../interfaces/limit/ILimitPoolManager.sol | 20 +- .../limit/ILimitPoolStorageView.sol | 21 +- contracts/interfaces/limit/ILimitPoolView.sol | 45 +- contracts/interfaces/range/IRangePool.sol | 120 ++-- .../interfaces/range/IRangePoolFactory.sol | 2 +- .../interfaces/range/IRangePoolManager.sol | 5 +- contracts/interfaces/staking/IRangeStaker.sol | 2 +- .../interfaces/structs/LimitPoolStructs.sol | 9 +- .../interfaces/structs/PoolsharkStructs.sol | 572 ++++++++++++------ .../interfaces/structs/RangePoolStructs.sol | 15 +- contracts/libraries/Samples.sol | 360 ++++++----- contracts/libraries/TickMap.sol | 343 ++++++----- contracts/libraries/Ticks.sol | 526 +++++++++++----- contracts/libraries/limit/Claims.sol | 353 ++++++++--- contracts/libraries/limit/EpochMap.sol | 77 +-- contracts/libraries/limit/LimitPositions.sol | 343 +++++++---- contracts/libraries/limit/LimitTicks.sol | 314 +++++++--- .../libraries/limit/pool/BurnLimitCall.sol | 69 ++- .../libraries/limit/pool/MintLimitCall.sol | 150 +++-- .../limit/pool/SnapshotLimitCall.sol | 31 +- contracts/libraries/math/ConstantProduct.sol | 257 ++++---- contracts/libraries/math/OverflowMath.sol | 10 +- contracts/libraries/pool/FeesCall.sol | 10 +- contracts/libraries/pool/QuoteCall.sol | 22 +- contracts/libraries/pool/SampleCall.sol | 49 +- contracts/libraries/pool/SwapCall.sol | 56 +- contracts/libraries/range/RangePositions.sol | 284 +++++---- contracts/libraries/range/RangeTicks.sol | 60 +- contracts/libraries/range/math/FeeMath.sol | 114 ++-- .../libraries/range/pool/BurnRangeCall.sol | 68 +-- .../libraries/range/pool/MintRangeCall.sol | 132 ++-- .../range/pool/SnapshotRangeCall.sol | 31 +- contracts/libraries/utils/Bytes.sol | 12 +- contracts/libraries/utils/Collect.sol | 45 +- contracts/libraries/utils/PositionTokens.sol | 34 +- contracts/libraries/utils/SafeCast.sol | 22 +- contracts/libraries/utils/SafeTransfers.sol | 16 +- contracts/libraries/utils/String.sol | 50 +- contracts/staking/RangeStaker.sol | 233 ++++--- contracts/test/WETH9.sol | 49 +- contracts/utils/LimitPoolManager.sol | 103 ++-- contracts/utils/PoolsharkRouter.sol | 509 ++++++++++------ contracts/utils/PositionERC1155.sol | 204 +++++-- package.json | 7 +- test/contracts/limitpoolmanager.ts | 163 +++-- 75 files changed, 4410 insertions(+), 2748 deletions(-) delete mode 100644 contracts/base/events/FinStakerEvents.sol diff --git a/.prettierrc b/.prettierrc index 4ed64425..cb6ea45f 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,6 +1,16 @@ { - "semi": false, - "singleQuote": true, - "printWidth": 110, - "tabWidth": 4 -} + "plugins": ["prettier-plugin-solidity"], + "overrides": [ + { + "files": "*.sol", + "options": { + "parser": "solidity-parse", + "printWidth": 80, + "tabWidth": 4, + "useTabs": false, + "singleQuote": true, + "bracketSpacing": false + } + } + ] +} \ No newline at end of file diff --git a/.solcover.js b/.solcover.js index 2b7d939b..69fa9111 100644 --- a/.solcover.js +++ b/.solcover.js @@ -4,7 +4,7 @@ module.exports = { 'utils', 'libraries/TickMap.sol', 'libraries/EpochMap.sol', - 'libraries/utils/String.sol', + 'libraries/utils', 'external', 'staking/FinStaker.sol', ], diff --git a/contracts/LimitPool.sol b/contracts/LimitPool.sol index 6ed46041..49432d9f 100644 --- a/contracts/LimitPool.sol +++ b/contracts/LimitPool.sol @@ -22,8 +22,11 @@ import './libraries/math/ConstantProduct.sol'; import './external/solady/LibClone.sol'; import './external/openzeppelin/security/LimitReentrancyGuard.sol'; - -/// @notice Poolshark Limit Pool Implementation +/** + * @title Limit Pool - Constant Product + * @author Poolshark + * @author @alphak3y + */ contract LimitPool is ILimitPool, ILimitPoolView, @@ -31,35 +34,38 @@ contract LimitPool is LimitPoolImmutables, LimitReentrancyGuard { - + /// @notice This modifier only allows `owner` to call a function modifier ownerOnly() { _onlyOwner(); _; } + /// @notice This modifier only allows `factory` to call a function modifier factoryOnly() { _onlyFactory(); _; } + /// @notice This modifier checks for canoncial limit pools modifier canonicalOnly() { _onlyCanoncialClones(); _; } + /// @notice The original address of the deployed contract address public immutable original; + /// @notice The factory address for the `initialize()` call address public immutable factory; - constructor( - address factory_ - ) { + constructor(address factory_) { original = address(this); factory = factory_; } - function initialize( - uint160 startPrice - ) external + /// @notice Initializes the LimitPool contract storage + /// @param startPrice the Q64.96 sqrt price to start the pool from + function initialize(uint160 startPrice) + external nonReentrant(globalState) factoryOnly canonicalOnly @@ -75,239 +81,251 @@ contract LimitPool is ); } - function mintRange( - MintRangeParams memory params - ) external + /// @notice Adds bidirectional liquidity (RangePosition) + /// @param params the params for minting the position + /// @dev See PoolsharkStructs.sol for struct data + function mintRange(MintRangeParams memory params) + external nonReentrant(globalState) canonicalOnly - returns ( - int256, - int256 - ) { + returns (int256, int256) + { MintRangeCache memory cache; cache.constants = immutables(); - return MintRangeCall.perform( - positions, - ticks, - rangeTickMap, - samples, - globalState, - cache, - params - ); + return + MintRangeCall.perform( + positions, + ticks, + rangeTickMap, + samples, + globalState, + cache, + params + ); } - function burnRange( - BurnRangeParams memory params - ) external + /// @notice Removes bidirectional liquidity (RangePosition) + /// @param params the params for burning the position + /// @dev See PoolsharkStructs.sol for struct data + function burnRange(BurnRangeParams memory params) + external nonReentrant(globalState) canonicalOnly - returns ( - int256, - int256 - ) { + returns (int256, int256) + { BurnRangeCache memory cache; cache.constants = immutables(); - return BurnRangeCall.perform( - positions, - ticks, - rangeTickMap, - samples, - globalState, - cache, - params - ); + return + BurnRangeCall.perform( + positions, + ticks, + rangeTickMap, + samples, + globalState, + cache, + params + ); } - //limitSwap - function mintLimit( - MintLimitParams memory params - ) external + /// @notice Adds directional liquidity (LimitPosition) + /// @param params the params for minting the position + /// @dev See PoolsharkStructs.sol for struct data + function mintLimit(MintLimitParams memory params) + external nonReentrant(globalState) canonicalOnly - returns ( - int256, - int256 - ) { + returns (int256, int256) + { MintLimitCache memory cache; cache.constants = immutables(); - return MintLimitCall.perform( - params.zeroForOne ? positions0 : positions1, - ticks, - samples, - rangeTickMap, - limitTickMap, - globalState, - params, - cache - ); + return + MintLimitCall.perform( + params.zeroForOne ? positions0 : positions1, + ticks, + samples, + rangeTickMap, + limitTickMap, + globalState, + params, + cache + ); } - function burnLimit( - BurnLimitParams memory params - ) external + /// @notice Removes directional liquidity (LimitPosition) + /// @param params the params for burning the position + /// @dev See PoolsharkStructs.sol for struct data + function burnLimit(BurnLimitParams memory params) + external nonReentrant(globalState) canonicalOnly - returns ( - int256, - int256 - ) { + returns (int256, int256) + { BurnLimitCache memory cache; cache.constants = immutables(); - return BurnLimitCall.perform( - params.zeroForOne ? positions0 : positions1, - ticks, - limitTickMap, - globalState, - params, - cache - ); + return + BurnLimitCall.perform( + params.zeroForOne ? positions0 : positions1, + ticks, + limitTickMap, + globalState, + params, + cache + ); } - function swap( - SwapParams memory params - ) external + /// @notice Swaps tokens with the liquidity pool + /// @param params the params for executing the swap + /// @dev See PoolsharkStructs.sol for struct data + function swap(SwapParams memory params) + external nonReentrant(globalState) canonicalOnly - returns ( - int256, - int256 - ) + returns (int256, int256) { SwapCache memory cache; cache.constants = immutables(); - return SwapCall.perform( - ticks, - samples, - rangeTickMap, - limitTickMap, - globalState, - params, - cache - ); + return + SwapCall.perform( + ticks, + samples, + rangeTickMap, + limitTickMap, + globalState, + params, + cache + ); } - function increaseSampleCount( - uint16 newSampleCountMax - ) external + /// @notice Increase the max sample count for oracle data + /// @param newSampleCountMax the new max sample count + function increaseSampleCount(uint16 newSampleCountMax) + external nonReentrant(globalState) - canonicalOnly + canonicalOnly { - Samples.expand( - samples, - globalState.pool, - newSampleCountMax - ); + Samples.expand(samples, globalState.pool, newSampleCountMax); } - function fees( - FeesParams memory params - ) external + /// @notice Modify or collect protocol fees + /// @param params the params for modifying fees + /// @dev See PoolsharkStructs.sol for struct data + function fees(FeesParams memory params) + external ownerOnly nonReentrant(globalState) - canonicalOnly - returns ( - uint128 token0Fees, - uint128 token1Fees - ) { - return FeesCall.perform( - globalState, - params, - immutables() - ); + canonicalOnly + returns (uint128 token0Fees, uint128 token1Fees) + { + return FeesCall.perform(globalState, params, immutables()); } - function quote( - QuoteParams memory params - ) external view - returns ( - uint256, - uint256, - uint160 - ) { + /// @notice Receive a swap quote for tokenIn and tokenOut amounts + /// @param params the params for executing the quote + /// @dev See PoolsharkStructs.sol for struct data + function quote(QuoteParams memory params) + external + view + returns ( + uint256, + uint256, + uint160 + ) + { SwapCache memory cache; cache.constants = immutables(); - return QuoteCall.perform( - ticks, - rangeTickMap, - limitTickMap, - globalState, - params, - cache - ); + return + QuoteCall.perform( + ticks, + rangeTickMap, + limitTickMap, + globalState, + params, + cache + ); } - function sample( - uint32[] memory secondsAgo - ) external view override - returns( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ) + /// @notice Receive oracle samples + /// @param secondsAgo an array of seconds in the past to receive samples for + function sample(uint32[] memory secondsAgo) + external + view + override + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ) { - return SampleCall.perform( - globalState, - immutables(), - secondsAgo - ); + return SampleCall.perform(globalState, immutables(), secondsAgo); } - function snapshotRange( - uint32 positionId - ) external view returns ( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum, - uint128 feesOwed0, - uint128 feesOwed1 - ) { - return SnapshotRangeCall.perform( - positions, - ticks, - globalState, - immutables(), - positionId - ); + /// @notice Snapshot token amounts for a RangePosition + /// @param positionId the position id to snapshot values for + function snapshotRange(uint32 positionId) + external + view + returns ( + int56 tickSecondsAccum, + uint160 secondsPerLiquidityAccum, + uint128 feesOwed0, + uint128 feesOwed1 + ) + { + return + SnapshotRangeCall.perform( + positions, + ticks, + globalState, + immutables(), + positionId + ); } - function snapshotLimit( - SnapshotLimitParams memory params - ) external view returns( - uint128, - uint128 - ) { - return SnapshotLimitCall.perform( - params.zeroForOne ? positions0 : positions1, - ticks, - limitTickMap, - globalState, - immutables(), - params - ); + /// @notice Snapshot token amounts for a LimitPosition + /// @param params the params to snapshot values for + /// @dev See PoolsharkStructs.sol for struct data + function snapshotLimit(SnapshotLimitParams memory params) + external + view + returns (uint128, uint128) + { + return + SnapshotLimitCall.perform( + params.zeroForOne ? positions0 : positions1, + ticks, + limitTickMap, + globalState, + immutables(), + params + ); } - function immutables() public view returns ( - LimitImmutables memory - ) { - return LimitImmutables( - owner(), - original, - factory, - PriceBounds(minPrice(), maxPrice()), - token0(), - token1(), - poolToken(), - genesisTime(), - tickSpacing(), - swapFee() - ); + /// @notice Immutable values embedded directly in the contract bytecode + /// @dev See PoolsharkStructs.sol for struct data + function immutables() public view returns (LimitImmutables memory) { + return + LimitImmutables( + owner(), + original, + factory, + PriceBounds(minPrice(), maxPrice()), + token0(), + token1(), + poolToken(), + genesisTime(), + tickSpacing(), + swapFee() + ); } - function priceBounds( - int16 tickSpacing - ) external pure returns (uint160, uint160) { + /// @notice The price bounds for the given curve math + function priceBounds(int16 tickSpacing) + external + pure + returns (uint160, uint160) + { return ConstantProduct.priceBounds(tickSpacing); } @@ -317,8 +335,10 @@ contract LimitPool is function _onlyCanoncialClones() private view { // compute pool key - bytes32 key = keccak256(abi.encode(original, token0(), token1(), swapFee())); - + bytes32 key = keccak256( + abi.encode(original, token0(), token1(), swapFee()) + ); + // compute canonical pool address address predictedAddress = LibClone.predictDeterministicAddress( original, @@ -337,7 +357,8 @@ contract LimitPool is factory ); // only allow delegateCall from canonical clones - if (address(this) != predictedAddress) require(false, 'NoDelegateCall()'); + if (address(this) != predictedAddress) + require(false, 'NoDelegateCall()'); } function _onlyFactory() private view { diff --git a/contracts/LimitPoolFactory.sol b/contracts/LimitPoolFactory.sol index a8aacfb6..87b5ec4a 100644 --- a/contracts/LimitPoolFactory.sol +++ b/contracts/LimitPoolFactory.sol @@ -14,7 +14,12 @@ import './libraries/utils/SafeCast.sol'; import './libraries/utils/PositionTokens.sol'; import './libraries/math/ConstantProduct.sol'; -contract LimitPoolFactory is +/** + * @notice Limit Pool Factory + * @author Poolshark + * @author @alphak3y + */ +contract LimitPoolFactory is ILimitPoolFactory, LimitPoolStructs, RangePoolStructs, @@ -23,58 +28,68 @@ contract LimitPoolFactory is using LibClone for address; using SafeCast for uint256; - address immutable public owner; - address immutable public original; + /// @notice The original address of the deployed contract + address public immutable original; + /// @notice The owner address for all LimitPool contracts + address public immutable owner; - constructor( - address owner_ - ) { + constructor(address owner_) { owner = owner_; original = address(this); } + /// @notice This modifier ensures against delegate calls modifier originalOnly() { _onlyOriginal(); _; } - function createLimitPool( - LimitPoolParams memory params - ) public override + /// @notice Creates a new LimitPool for the selected poolTypeId + /// @param params the params for which to create the pool with + /// @dev See PoolsharkStructs.sol for struct data + function createLimitPool(LimitPoolParams memory params) + public + override originalOnly - returns ( - address pool, - address poolToken - ) { + returns (address pool, address poolToken) + { // validate token pair - if (params.tokenIn == params.tokenOut || params.tokenIn == address(0) || params.tokenOut == address(0)) { + if ( + params.tokenIn == params.tokenOut || + params.tokenIn == address(0) || + params.tokenOut == address(0) + ) { require(false, 'InvalidTokenAddress()'); } // sort tokens by address LimitImmutables memory constants; - (constants.token0, constants.token1) = params.tokenIn < params.tokenOut ? (params.tokenIn, params.tokenOut) - : (params.tokenOut, params.tokenIn); + (constants.token0, constants.token1) = params.tokenIn < params.tokenOut + ? (params.tokenIn, params.tokenOut) + : (params.tokenOut, params.tokenIn); // check if tick spacing supported constants.swapFee = params.swapFee; - constants.tickSpacing = ILimitPoolManager(owner).feeTiers(params.swapFee); + constants.tickSpacing = ILimitPoolManager(owner).feeTiers( + params.swapFee + ); if (constants.tickSpacing == 0) require(false, 'FeeTierNotSupported()'); // check if pool type supported - ( - address poolImpl, - address tokenImpl - ) = ILimitPoolManager(owner).poolTypes(params.poolTypeId); - if (poolImpl == address(0) || tokenImpl == address(0)) require(false, 'PoolTypeNotSupported()'); + (address poolImpl, address tokenImpl) = ILimitPoolManager(owner) + .poolTypes(params.poolTypeId); + if (poolImpl == address(0) || tokenImpl == address(0)) + require(false, 'PoolTypeNotSupported()'); // generate key for pool - bytes32 key = keccak256(abi.encode( - poolImpl, - constants.token0, - constants.token1, - constants.swapFee - )); + bytes32 key = keccak256( + abi.encode( + poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); // check if pool already exists if (pools[key] != address(0)) require(false, 'PoolAlreadyExists()'); @@ -83,10 +98,8 @@ contract LimitPoolFactory is constants.owner = owner; constants.factory = original; constants.genesisTime = block.timestamp.toUint32(); - ( - constants.bounds.min, - constants.bounds.max - ) = ILimitPoolView(poolImpl).priceBounds(constants.tickSpacing); + (constants.bounds.min, constants.bounds.max) = ILimitPoolView(poolImpl) + .priceBounds(constants.tickSpacing); // take that ERC1155 contract address and pass that into pool // launch pool token @@ -133,15 +146,17 @@ contract LimitPoolFactory is return (pool, constants.poolToken); } + /// @notice Gets a new LimitPool for the selected poolTypeId + /// @param tokenIn the first token in the pair + /// @param tokenOut the second token in the pair + /// @param swapFee the swap fee for the pool + /// @param poolTypeId the poolTypeId for the pool function getLimitPool( address tokenIn, address tokenOut, uint16 swapFee, uint16 poolTypeId - ) public view override returns ( - address pool, - address poolToken - ) { + ) public view override returns (address pool, address poolToken) { // set lexographical token address ordering address token0 = tokenIn < tokenOut ? tokenIn : tokenOut; address token1 = tokenIn < tokenOut ? tokenOut : tokenIn; @@ -151,19 +166,13 @@ contract LimitPoolFactory is if (tickSpacing == 0) require(false, 'FeeTierNotSupported()'); // check if pool type supported - ( - address poolImpl, - address tokenImpl - ) = ILimitPoolManager(owner).poolTypes(poolTypeId); - if (poolImpl == address(0) || tokenImpl == address(0)) require(false, 'PoolTypeNotSupported()'); + (address poolImpl, address tokenImpl) = ILimitPoolManager(owner) + .poolTypes(poolTypeId); + if (poolImpl == address(0) || tokenImpl == address(0)) + require(false, 'PoolTypeNotSupported()'); // generate key for pool - bytes32 key = keccak256(abi.encode( - poolImpl, - token0, - token1, - swapFee - )); + bytes32 key = keccak256(abi.encode(poolImpl, token0, token1, swapFee)); pool = pools[key]; diff --git a/contracts/base/events/FinStakerEvents.sol b/contracts/base/events/FinStakerEvents.sol deleted file mode 100644 index 34e4c826..00000000 --- a/contracts/base/events/FinStakerEvents.sol +++ /dev/null @@ -1,29 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.18; - -abstract contract FinStakerEvents { - event StakeFin( - address owner, - uint256 amount - ); - - event UnstakeFin( - address pool, - uint256 amount - ); - - event StakeFinAccrued( - address owner, - uint128 stakingPointsAccrued - ); - - event FeeToTransfer( - address indexed previousFeeTo, - address indexed newFeeTo - ); - - event OwnerTransfer( - address indexed previousOwner, - address indexed newOwner - ); -} diff --git a/contracts/base/events/LimitPoolEvents.sol b/contracts/base/events/LimitPoolEvents.sol index 81bb8c3a..fad0e986 100644 --- a/contracts/base/events/LimitPoolEvents.sol +++ b/contracts/base/events/LimitPoolEvents.sol @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; +/// @notice Events emitted by the LimitPool contract(s) abstract contract LimitPoolEvents { + /// @notice Event emitted when pool is initialized by the factory event Initialize( int24 minTick, int24 maxTick, @@ -9,6 +11,7 @@ abstract contract LimitPoolEvents { int24 startTick ); + /// @notice Event emitted when a swap is successful event Swap( address indexed recipient, uint256 amountIn, @@ -23,15 +26,7 @@ abstract contract LimitPoolEvents { bool indexed exactIn ); - event SampleRecorded( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum - ); - - event SampleCountIncreased( - uint16 newSampleCountMax - ); - + /// @notice Event emitted when liquidity added to RangePosition event MintRange( address indexed recipient, int24 lower, @@ -42,6 +37,7 @@ abstract contract LimitPoolEvents { int128 amount1Delta ); + /// @notice Event emitted when liquidity removed from RangePosition event BurnRange( address indexed recipient, uint256 indexed positionId, @@ -50,25 +46,23 @@ abstract contract LimitPoolEvents { int128 amount1 ); - event CompoundRange( - uint32 indexed positionId, - uint128 liquidityCompounded - ); + /// @notice Event emitted when liquidity is added as a result of calling `burnRange` + event CompoundRange(uint32 indexed positionId, uint128 liquidityCompounded); - event CollectRange0( - uint128 amount0 - ); + /// @notice Event emitted when token0 is collected from the pool + event CollectRange0(uint128 amount0); - event CollectRange1( - uint128 amount1 - ); + /// @notice Event emitted when token1 is collected from the pool + event CollectRange1(uint128 amount1); + /// @notice Event emitted when a RangeTick is updated event SyncRangeTick( uint200 feeGrowthOutside0, uint200 feeGrowthOutside1, int24 tick ); + /// @notice Event emitted when liquidity is added to a LimitPosition event MintLimit( address indexed to, int24 lower, @@ -80,6 +74,7 @@ abstract contract LimitPoolEvents { uint128 liquidityMinted ); + /// @notice Event emitted when liquidity is removed from a LimitPosition event BurnLimit( address indexed to, uint32 positionId, @@ -93,6 +88,7 @@ abstract contract LimitPoolEvents { uint128 tokenOutBurned ); + /// @notice Event emitted when a LimitPosition undercuts pool0 or pool1 event SyncLimitPool( uint160 price, uint128 liquidity, @@ -101,15 +97,22 @@ abstract contract LimitPoolEvents { bool isPool0 ); + /// @notice Event emitted when a LimitTick is created via undercutting event SyncLimitLiquidity( uint128 liquidityAdded, int24 tick, bool zeroForOne ); - event SyncLimitTick( - uint32 epoch, - int24 tick, - bool zeroForOne + /// @notice Event emitted when a LimitTick is crossed or initialized + event SyncLimitTick(uint32 epoch, int24 tick, bool zeroForOne); + + /// @notice Event emitted when an oracle sample is recorded + event SampleRecorded( + int56 tickSecondsAccum, + uint160 secondsPerLiquidityAccum ); + + /// @notice Event emitted when max sample count is increased + event SampleCountIncreased(uint16 newSampleCountMax); } diff --git a/contracts/base/events/LimitPoolFactoryEvents.sol b/contracts/base/events/LimitPoolFactoryEvents.sol index 863e3738..2e9c883c 100644 --- a/contracts/base/events/LimitPoolFactoryEvents.sol +++ b/contracts/base/events/LimitPoolFactoryEvents.sol @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; +/// @notice Events emitted by the LimitPoolFactory contract abstract contract LimitPoolFactoryEvents { + /// @notice Event emitted when a LimitPool is created event PoolCreated( address pool, address token, diff --git a/contracts/base/events/LimitPoolManagerEvents.sol b/contracts/base/events/LimitPoolManagerEvents.sol index 8d5f8bed..aecc840e 100644 --- a/contracts/base/events/LimitPoolManagerEvents.sol +++ b/contracts/base/events/LimitPoolManagerEvents.sol @@ -3,34 +3,61 @@ pragma solidity 0.8.18; import '../../interfaces/structs/PoolsharkStructs.sol'; +/// @notice Events emitted by the LimitPoolManager contract abstract contract LimitPoolManagerEvents is PoolsharkStructs { - event FactoryChanged(address indexed previousFactory, address indexed newFactory); - event FeeDeltaConstChanged(uint16 oldFeeDeltaConst, uint16 newFeeDeltaConst); + /// @notice Event emitted when pool is initialized by the factory + event FactoryChanged( + address indexed previousFactory, + address indexed newFactory + ); + + /// @notice Event emitted the fee delta constant is modified + event FeeDeltaConstChanged( + uint16 oldFeeDeltaConst, + uint16 newFeeDeltaConst + ); + + /// @notice Event emitted when a new pool type is enabled event PoolTypeEnabled( bytes32 poolTypeName, address poolImpl, address tokenImpl, - uint16 poolTypeId + uint16 poolTypeId ); - event FeeTierEnabled( - uint16 swapFee, - int16 tickSpacing + + /// @notice Event emitted when a new fee tier is enabled + event FeeTierEnabled(uint16 swapFee, int16 tickSpacing); + + /// @notice Event emitted when the feeTo address is modified + event FeeToTransfer( + address indexed previousFeeTo, + address indexed newFeeTo ); - event FeeToTransfer(address indexed previousFeeTo, address indexed newFeeTo); - event OwnerTransfer(address indexed previousOwner, address indexed newOwner); + + /// @notice Event emitted when the owner address is modified + event OwnerTransfer( + address indexed previousOwner, + address indexed newOwner + ); + + /// @notice Event emitted when protocolSwapFees0 or protocolSwapFees1 is modified event ProtocolSwapFeesModified( address[] pools, int16[] protocolSwapFees0, int16[] protocolSwapFees1 ); + + /// @notice Event emitted when protocolSwapFees0 or protocolSwapFees1 is modified event ProtocolFillFeesModified( address[] pools, int16[] protocolFillFees0, int16[] protocolFillFees1 ); + + /// @notice Event emitted when protocol fees are collected event ProtocolFeesCollected( address[] pools, uint128[] token0FeesCollected, uint128[] token1FeesCollected ); -} \ No newline at end of file +} diff --git a/contracts/base/events/PoolsharkRouterEvents.sol b/contracts/base/events/PoolsharkRouterEvents.sol index 57d3de58..0e427db1 100644 --- a/contracts/base/events/PoolsharkRouterEvents.sol +++ b/contracts/base/events/PoolsharkRouterEvents.sol @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; +/// @notice Events emitted by the PoolsharkRouter contract abstract contract PoolsharkRouterEvents { + /// @notice Event emitted when the router is initially deployed event RouterDeployed( address router, address limitPoolFactory, diff --git a/contracts/base/events/PositionERC1155Events.sol b/contracts/base/events/PositionERC1155Events.sol index a02d652c..49a0c8ba 100644 --- a/contracts/base/events/PositionERC1155Events.sol +++ b/contracts/base/events/PositionERC1155Events.sol @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPLv3 pragma solidity 0.8.18; +/// @notice Events emitted by the PositionERC1155 contract(s) abstract contract PositionERC1155Events { + /// @notice Event emitted when single token is transferred event TransferSingle( address indexed sender, address indexed from, @@ -10,6 +12,7 @@ abstract contract PositionERC1155Events { uint256 amount ); + /// @notice Event emitted when multiple tokens are transferred event TransferBatch( address indexed sender, address indexed from, @@ -18,6 +21,7 @@ abstract contract PositionERC1155Events { uint256[] amounts ); + /// @notice Event emitted when spender is approved for all token ids event ApprovalForAll( address indexed account, address indexed sender, diff --git a/contracts/base/events/RangeStakerEvents.sol b/contracts/base/events/RangeStakerEvents.sol index d68b35d5..2fefde07 100644 --- a/contracts/base/events/RangeStakerEvents.sol +++ b/contracts/base/events/RangeStakerEvents.sol @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; +/// @notice Events emitted by the RangeStaker contract abstract contract RangeStakerEvents { + /// @notice Event emitted when a RangePosition is staked event StakeRange( address pool, uint32 positionId, @@ -11,12 +13,10 @@ abstract contract RangeStakerEvents { uint128 liquidity ); - event UnstakeRange( - address pool, - uint32 positionId, - address recipient - ); + /// @notice Event emitted when a Range Position is unstaked + event UnstakeRange(address pool, uint32 positionId, address recipient); + /// @notice Event emitted when a staked RangePosition accrues fees event StakeRangeAccrued( address pool, uint32 positionId, @@ -24,11 +24,13 @@ abstract contract RangeStakerEvents { uint256 feeGrowth1Accrued ); + /// @notice Event emitted when the feeTo address is modified event FeeToTransfer( address indexed previousFeeTo, address indexed newFeeTo ); + /// @notice Event emitted when the owner address is modified event OwnerTransfer( address indexed previousOwner, address indexed newOwner diff --git a/contracts/base/storage/LimitPoolFactoryStorage.sol b/contracts/base/storage/LimitPoolFactoryStorage.sol index e5b62bcb..67a27179 100644 --- a/contracts/base/storage/LimitPoolFactoryStorage.sol +++ b/contracts/base/storage/LimitPoolFactoryStorage.sol @@ -2,9 +2,5 @@ pragma solidity 0.8.18; abstract contract LimitPoolFactoryStorage { - mapping(bytes32 => address) public pools; + mapping(bytes32 => address) public pools; ///@dev - map for limit pool lookup by key } - - - - diff --git a/contracts/base/storage/LimitPoolImmutables.sol b/contracts/base/storage/LimitPoolImmutables.sol index 0d14703b..15edf156 100644 --- a/contracts/base/storage/LimitPoolImmutables.sol +++ b/contracts/base/storage/LimitPoolImmutables.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: BSD pragma solidity 0.8.18; -import { Clone } from "../../external/solady/Clone.sol"; +import {Clone} from '../../external/solady/Clone.sol'; contract LimitPoolImmutables is Clone { function owner() public pure returns (address) { @@ -39,4 +39,4 @@ contract LimitPoolImmutables is Clone { function swapFee() public pure returns (uint16) { return _getArgUint16(126); } -} \ No newline at end of file +} diff --git a/contracts/base/storage/LimitPoolStorage.sol b/contracts/base/storage/LimitPoolStorage.sol index ec829d3f..29490120 100644 --- a/contracts/base/storage/LimitPoolStorage.sol +++ b/contracts/base/storage/LimitPoolStorage.sol @@ -12,7 +12,7 @@ abstract contract LimitPoolStorage is ILimitPoolStorageView, RangePoolStructs { TickMap public limitTickMap; ///@dev - tick bitmap for limit ticks Sample[65535] public samples; ///@dev - oracle TWAP samples mapping(int24 => Tick) public ticks; ///@dev - range and limit tick data - mapping(uint256 => RangePosition) public positions; ///@dev - range positions token0 <> token1 + mapping(uint256 => RangePosition) public positions; ///@dev - range positions token0 <> token1 mapping(uint256 => LimitPosition) public positions0; ///@dev - limit positions token0 -> token1 mapping(uint256 => LimitPosition) public positions1; ///@dev - limit positions token0 <- token1 } diff --git a/contracts/base/storage/PositionERC1155Immutables.sol b/contracts/base/storage/PositionERC1155Immutables.sol index fe0e01a4..ede4b10a 100644 --- a/contracts/base/storage/PositionERC1155Immutables.sol +++ b/contracts/base/storage/PositionERC1155Immutables.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: BSD pragma solidity 0.8.18; -import { Clone } from "../../external/solady/Clone.sol"; +import {Clone} from '../../external/solady/Clone.sol'; contract PositionERC1155Immutables is Clone { function tokenName() public pure returns (bytes32) { @@ -11,4 +11,4 @@ contract PositionERC1155Immutables is Clone { function tokenSymbol() public pure returns (bytes32) { return _getArgBytes32(32); } -} \ No newline at end of file +} diff --git a/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol b/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol index e56d1e28..1b43efff 100644 --- a/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol +++ b/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol @@ -85,7 +85,11 @@ abstract contract LimitReentrancyGuard is PoolsharkStructs { * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a * `nonReentrant` function in the call stack. */ - function _reentrancyGuardEntered(GlobalState storage state) internal view returns (bool) { + function _reentrancyGuardEntered(GlobalState storage state) + internal + view + returns (bool) + { return state.unlocked == _ENTERED; } -} \ No newline at end of file +} diff --git a/contracts/external/openzeppelin/security/ReentrancyGuard.sol b/contracts/external/openzeppelin/security/ReentrancyGuard.sol index 0f6db30e..43eb14dd 100644 --- a/contracts/external/openzeppelin/security/ReentrancyGuard.sol +++ b/contracts/external/openzeppelin/security/ReentrancyGuard.sol @@ -81,4 +81,4 @@ abstract contract ReentrancyGuard { function _reentrancyGuardEntered() internal view returns (bool) { return _status == ENTERED; } -} \ No newline at end of file +} diff --git a/contracts/external/solady/Clone.sol b/contracts/external/solady/Clone.sol index c1a04b9e..715afba5 100644 --- a/contracts/external/solady/Clone.sol +++ b/contracts/external/solady/Clone.sol @@ -25,7 +25,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type address. - function _getArgAddress(uint256 argOffset) internal pure returns (address arg) { + function _getArgAddress(uint256 argOffset) + internal + pure + returns (address arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -66,7 +70,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type bytes32. - function _getArgBytes32(uint256 argOffset) internal pure returns (bytes32 arg) { + function _getArgBytes32(uint256 argOffset) + internal + pure + returns (bytes32 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -75,7 +83,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint256. - function _getArgUint256(uint256 argOffset) internal pure returns (uint256 arg) { + function _getArgUint256(uint256 argOffset) + internal + pure + returns (uint256 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -84,7 +96,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint248. - function _getArgUint248(uint256 argOffset) internal pure returns (uint248 arg) { + function _getArgUint248(uint256 argOffset) + internal + pure + returns (uint248 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -93,7 +109,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint240. - function _getArgUint240(uint256 argOffset) internal pure returns (uint240 arg) { + function _getArgUint240(uint256 argOffset) + internal + pure + returns (uint240 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -102,7 +122,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint232. - function _getArgUint232(uint256 argOffset) internal pure returns (uint232 arg) { + function _getArgUint232(uint256 argOffset) + internal + pure + returns (uint232 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -111,7 +135,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint224. - function _getArgUint224(uint256 argOffset) internal pure returns (uint224 arg) { + function _getArgUint224(uint256 argOffset) + internal + pure + returns (uint224 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -120,7 +148,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint216. - function _getArgUint216(uint256 argOffset) internal pure returns (uint216 arg) { + function _getArgUint216(uint256 argOffset) + internal + pure + returns (uint216 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -129,7 +161,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint208. - function _getArgUint208(uint256 argOffset) internal pure returns (uint208 arg) { + function _getArgUint208(uint256 argOffset) + internal + pure + returns (uint208 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -138,7 +174,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint200. - function _getArgUint200(uint256 argOffset) internal pure returns (uint200 arg) { + function _getArgUint200(uint256 argOffset) + internal + pure + returns (uint200 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -147,7 +187,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint192. - function _getArgUint192(uint256 argOffset) internal pure returns (uint192 arg) { + function _getArgUint192(uint256 argOffset) + internal + pure + returns (uint192 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -156,7 +200,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint184. - function _getArgUint184(uint256 argOffset) internal pure returns (uint184 arg) { + function _getArgUint184(uint256 argOffset) + internal + pure + returns (uint184 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -165,7 +213,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint176. - function _getArgUint176(uint256 argOffset) internal pure returns (uint176 arg) { + function _getArgUint176(uint256 argOffset) + internal + pure + returns (uint176 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -174,7 +226,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint168. - function _getArgUint168(uint256 argOffset) internal pure returns (uint168 arg) { + function _getArgUint168(uint256 argOffset) + internal + pure + returns (uint168 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -183,7 +239,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint160. - function _getArgUint160(uint256 argOffset) internal pure returns (uint160 arg) { + function _getArgUint160(uint256 argOffset) + internal + pure + returns (uint160 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -192,7 +252,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint152. - function _getArgUint152(uint256 argOffset) internal pure returns (uint152 arg) { + function _getArgUint152(uint256 argOffset) + internal + pure + returns (uint152 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -201,7 +265,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint144. - function _getArgUint144(uint256 argOffset) internal pure returns (uint144 arg) { + function _getArgUint144(uint256 argOffset) + internal + pure + returns (uint144 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -210,7 +278,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint136. - function _getArgUint136(uint256 argOffset) internal pure returns (uint136 arg) { + function _getArgUint136(uint256 argOffset) + internal + pure + returns (uint136 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -219,7 +291,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint128. - function _getArgUint128(uint256 argOffset) internal pure returns (uint128 arg) { + function _getArgUint128(uint256 argOffset) + internal + pure + returns (uint128 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -228,7 +304,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint120. - function _getArgUint120(uint256 argOffset) internal pure returns (uint120 arg) { + function _getArgUint120(uint256 argOffset) + internal + pure + returns (uint120 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -237,7 +317,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint112. - function _getArgUint112(uint256 argOffset) internal pure returns (uint112 arg) { + function _getArgUint112(uint256 argOffset) + internal + pure + returns (uint112 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -246,7 +330,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint104. - function _getArgUint104(uint256 argOffset) internal pure returns (uint104 arg) { + function _getArgUint104(uint256 argOffset) + internal + pure + returns (uint104 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -255,7 +343,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint96. - function _getArgUint96(uint256 argOffset) internal pure returns (uint96 arg) { + function _getArgUint96(uint256 argOffset) + internal + pure + returns (uint96 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -264,7 +356,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint88. - function _getArgUint88(uint256 argOffset) internal pure returns (uint88 arg) { + function _getArgUint88(uint256 argOffset) + internal + pure + returns (uint88 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -273,7 +369,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint80. - function _getArgUint80(uint256 argOffset) internal pure returns (uint80 arg) { + function _getArgUint80(uint256 argOffset) + internal + pure + returns (uint80 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -282,7 +382,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint72. - function _getArgUint72(uint256 argOffset) internal pure returns (uint72 arg) { + function _getArgUint72(uint256 argOffset) + internal + pure + returns (uint72 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -291,7 +395,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint64. - function _getArgUint64(uint256 argOffset) internal pure returns (uint64 arg) { + function _getArgUint64(uint256 argOffset) + internal + pure + returns (uint64 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -300,7 +408,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint56. - function _getArgUint56(uint256 argOffset) internal pure returns (uint56 arg) { + function _getArgUint56(uint256 argOffset) + internal + pure + returns (uint56 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -309,7 +421,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint48. - function _getArgUint48(uint256 argOffset) internal pure returns (uint48 arg) { + function _getArgUint48(uint256 argOffset) + internal + pure + returns (uint48 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -318,7 +434,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint40. - function _getArgUint40(uint256 argOffset) internal pure returns (uint40 arg) { + function _getArgUint40(uint256 argOffset) + internal + pure + returns (uint40 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -327,7 +447,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint32. - function _getArgUint32(uint256 argOffset) internal pure returns (uint32 arg) { + function _getArgUint32(uint256 argOffset) + internal + pure + returns (uint32 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -336,7 +460,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint24. - function _getArgUint24(uint256 argOffset) internal pure returns (uint24 arg) { + function _getArgUint24(uint256 argOffset) + internal + pure + returns (uint24 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -345,7 +473,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint16. - function _getArgUint16(uint256 argOffset) internal pure returns (uint16 arg) { + function _getArgUint16(uint256 argOffset) + internal + pure + returns (uint16 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -366,7 +498,10 @@ abstract contract Clone { function _getImmutableArgsOffset() internal pure returns (uint256 offset) { /// @solidity memory-safe-assembly assembly { - offset := sub(calldatasize(), shr(240, calldataload(sub(calldatasize(), 2)))) + offset := sub( + calldatasize(), + shr(240, calldataload(sub(calldatasize(), 2))) + ) } } -} \ No newline at end of file +} diff --git a/contracts/external/solady/LibClone.sol b/contracts/external/solady/LibClone.sol index 4b7d9410..cc94bdc5 100644 --- a/contracts/external/solady/LibClone.sol +++ b/contracts/external/solady/LibClone.sol @@ -34,10 +34,11 @@ library LibClone { /// @dev Deploys a deterministic clone of `implementation`, /// using immutable arguments encoded in `data`, with `salt`. - function cloneDeterministic(address implementation, bytes memory data, bytes32 salt) - internal - returns (address instance) - { + function cloneDeterministic( + address implementation, + bytes memory data, + bytes32 salt + ) internal returns (address instance) { assembly { // Compute the boundaries of the data and cache the memory slots around it. let mBefore3 := mload(sub(data, 0x60)) @@ -57,20 +58,32 @@ library LibClone { // Write the rest of the bytecode. mstore( sub(data, 0x21), - or(shl(0x48, extraLength), 0x593da1005b363d3d373d3d3d3d610000806062363936013d73) + or( + shl(0x48, extraLength), + 0x593da1005b363d3d373d3d3d3d610000806062363936013d73 + ) ) // `keccak256("ReceiveETH(uint256)")` mstore( - sub(data, 0x3a), 0x9e4ac34f21c619cefc926c8bd93b54bf5a39c7ab2127a895af1cc0691d7e3dff + sub(data, 0x3a), + 0x9e4ac34f21c619cefc926c8bd93b54bf5a39c7ab2127a895af1cc0691d7e3dff ) mstore( sub(data, 0x5a), - or(shl(0x78, add(extraLength, 0x62)), 0x6100003d81600a3d39f336602c57343d527f) + or( + shl(0x78, add(extraLength, 0x62)), + 0x6100003d81600a3d39f336602c57343d527f + ) ) mstore(dataEnd, shl(0xf0, extraLength)) // Create the instance. - instance := create2(0, sub(data, 0x4c), add(extraLength, 0x6c), salt) + instance := create2( + 0, + sub(data, 0x4c), + add(extraLength, 0x6c), + salt + ) // If `instance` is zero, revert. if iszero(instance) { @@ -116,15 +129,22 @@ library LibClone { // Write the rest of the bytecode. mstore( sub(data, 0x21), - or(shl(0x48, extraLength), 0x593da1005b363d3d373d3d3d3d610000806062363936013d73) + or( + shl(0x48, extraLength), + 0x593da1005b363d3d373d3d3d3d610000806062363936013d73 + ) ) // `keccak256("ReceiveETH(uint256)")` mstore( - sub(data, 0x3a), 0x9e4ac34f21c619cefc926c8bd93b54bf5a39c7ab2127a895af1cc0691d7e3dff + sub(data, 0x3a), + 0x9e4ac34f21c619cefc926c8bd93b54bf5a39c7ab2127a895af1cc0691d7e3dff ) mstore( sub(data, 0x5a), - or(shl(0x78, add(extraLength, 0x62)), 0x6100003d81600a3d39f336602c57343d527f) + or( + shl(0x78, add(extraLength, 0x62)), + 0x6100003d81600a3d39f336602c57343d527f + ) ) mstore(dataEnd, shl(0xf0, extraLength)) @@ -158,11 +178,11 @@ library LibClone { /// @dev Returns the address when a contract with initialization code hash, /// `hash`, is deployed with `salt`, by `deployer`. - function predictDeterministicAddress(bytes32 hash, bytes32 salt, address deployer) - internal - pure - returns (address predicted) - { + function predictDeterministicAddress( + bytes32 hash, + bytes32 salt, + address deployer + ) internal pure returns (address predicted) { /// @solidity memory-safe-assembly assembly { // Compute and store the bytecode hash. @@ -189,4 +209,4 @@ library LibClone { } } } -} \ No newline at end of file +} diff --git a/contracts/interfaces/IERC20Minimal.sol b/contracts/interfaces/IERC20Minimal.sol index f1a4e0b5..377f86dd 100644 --- a/contracts/interfaces/IERC20Minimal.sol +++ b/contracts/interfaces/IERC20Minimal.sol @@ -6,4 +6,4 @@ interface IERC20Minimal { /// @param account The address for which to look up the balance for /// @return amount of tokens held by the account function balanceOf(address account) external view returns (uint256); -} \ No newline at end of file +} diff --git a/contracts/interfaces/IPool.sol b/contracts/interfaces/IPool.sol index 0ba7f8bb..adbfd72a 100644 --- a/contracts/interfaces/IPool.sol +++ b/contracts/interfaces/IPool.sol @@ -5,58 +5,51 @@ import '../interfaces/structs/PoolsharkStructs.sol'; interface IPool is PoolsharkStructs { function immutables() external view returns (LimitImmutables memory); - - function swap( - SwapParams memory params - ) external returns ( - int256 amount0, - int256 amount1 - ); - function quote( - QuoteParams memory params - ) external view returns ( - int256 inAmount, - int256 outAmount, - uint160 priceAfter - ); - - function fees( - FeesParams memory params - ) external returns ( - uint128 token0Fees, - uint128 token1Fees - ); - - function sample( - uint32[] memory secondsAgo - ) external view returns ( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ); - - function snapshotRange( - uint32 positionId - ) external view returns( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum, - uint128 feesOwed0, - uint128 feesOwed1 - ); - - function snapshotLimit( - SnapshotLimitParams memory params - ) external view returns( - uint128 amountIn, - uint128 amountOut - ); - - function poolToken() external view returns( - address poolToken - ); + function swap(SwapParams memory params) + external + returns (int256 amount0, int256 amount1); + + function quote(QuoteParams memory params) + external + view + returns ( + int256 inAmount, + int256 outAmount, + uint160 priceAfter + ); + + function fees(FeesParams memory params) + external + returns (uint128 token0Fees, uint128 token1Fees); + + function sample(uint32[] memory secondsAgo) + external + view + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ); + + function snapshotRange(uint32 positionId) + external + view + returns ( + int56 tickSecondsAccum, + uint160 secondsPerLiquidityAccum, + uint128 feesOwed0, + uint128 feesOwed1 + ); + + function snapshotLimit(SnapshotLimitParams memory params) + external + view + returns (uint128 amountIn, uint128 amountOut); + + function poolToken() external view returns (address poolToken); function token0() external view returns (address token0); diff --git a/contracts/interfaces/IPositionERC1155.sol b/contracts/interfaces/IPositionERC1155.sol index 2bec3482..a80ca6a9 100644 --- a/contracts/interfaces/IPositionERC1155.sol +++ b/contracts/interfaces/IPositionERC1155.sol @@ -3,7 +3,7 @@ pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; -import "@openzeppelin/contracts/utils/introspection/IERC165.sol"; +import '@openzeppelin/contracts/utils/introspection/IERC165.sol'; interface IPositionERC1155 is IERC165, PoolsharkStructs { event TransferSingle( @@ -32,18 +32,22 @@ interface IPositionERC1155 is IERC165, PoolsharkStructs { function symbol() external view returns (string memory); - function balanceOf(address account, uint256 id) external view returns (uint256); + function balanceOf(address account, uint256 id) + external + view + returns (uint256); - function balanceOfBatch( - address[] calldata accounts, - uint256[] calldata ids - ) external view returns ( - uint256[] memory batchBalances - ); + function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids) + external + view + returns (uint256[] memory batchBalances); function totalSupply(uint256 id) external view returns (uint256); - function isApprovedForAll(address owner, address spender) external view returns (bool); + function isApprovedForAll(address owner, address spender) + external + view + returns (bool); function setApprovalForAll(address sender, bool approved) external; @@ -79,4 +83,4 @@ interface IPositionERC1155 is IERC165, PoolsharkStructs { address recipient, PoolsharkStructs.LimitImmutables memory constants ) external; -} \ No newline at end of file +} diff --git a/contracts/interfaces/IWETH9.sol b/contracts/interfaces/IWETH9.sol index 0fb60e76..37ff5f09 100644 --- a/contracts/interfaces/IWETH9.sol +++ b/contracts/interfaces/IWETH9.sol @@ -6,11 +6,11 @@ interface IWETH9 { function deposit() external payable; /// @notice Withdraws ether from wrapped ether balance - function withdraw(uint wad) external; + function withdraw(uint256 wad) external; /// @notice Withdraws ether from wrapped ether balance - function transfer(address dst, uint wad) external returns (bool); + function transfer(address dst, uint256 wad) external returns (bool); /// @notice Returns balance for address function balanceOf(address account) external returns (uint256); -} \ No newline at end of file +} diff --git a/contracts/interfaces/callbacks/ICoverPoolCallback.sol b/contracts/interfaces/callbacks/ICoverPoolCallback.sol index f1ab76c5..621ca2f2 100644 --- a/contracts/interfaces/callbacks/ICoverPoolCallback.sol +++ b/contracts/interfaces/callbacks/ICoverPoolCallback.sol @@ -14,7 +14,6 @@ interface ICoverPoolMintCallback { ) external; } - /// @title Callback for swaps /// @notice Any contract that calls the `swap` function must implement this interface. interface ICoverPoolSwapCallback { @@ -28,4 +27,4 @@ interface ICoverPoolSwapCallback { int256 amount1Delta, bytes calldata data ) external; -} \ No newline at end of file +} diff --git a/contracts/interfaces/callbacks/ILimitPoolCallback.sol b/contracts/interfaces/callbacks/ILimitPoolCallback.sol index 9cf73f54..56422faf 100644 --- a/contracts/interfaces/callbacks/ILimitPoolCallback.sol +++ b/contracts/interfaces/callbacks/ILimitPoolCallback.sol @@ -40,4 +40,4 @@ interface ILimitPoolSwapCallback { int256 amount1Delta, bytes calldata data ) external; -} \ No newline at end of file +} diff --git a/contracts/interfaces/cover/ICoverPool.sol b/contracts/interfaces/cover/ICoverPool.sol index fdf6131e..05d26a30 100644 --- a/contracts/interfaces/cover/ICoverPool.sol +++ b/contracts/interfaces/cover/ICoverPool.sol @@ -9,11 +9,10 @@ import '../structs/PoolsharkStructs.sol'; * @notice Defines the basic interface for a Cover Pool. */ interface ICoverPool is PoolsharkStructs { - - function immutables( - ) external view returns ( - CoverImmutables memory constants - ); + function immutables() + external + view + returns (CoverImmutables memory constants); /** * @notice Deposits `amountIn` of asset to be auctioned off each time price range is crossed further into. @@ -24,9 +23,7 @@ interface ICoverPool is PoolsharkStructs { * @dev The position will be minted with the `to` address as the owner. * @param params The parameters for the function. See MintCoverParams. */ - function mint( - MintCoverParams memory params - ) external; + function mint(MintCoverParams memory params) external; /** * @notice Withdraws the input token and returns any filled and/or unfilled amounts to the 'to' address specified. @@ -38,9 +35,7 @@ interface ICoverPool is PoolsharkStructs { * @dev The `sync` flag can be set to false so users can exit safely without syncing latestTick. * @param params The parameters for the function. See BurnCoverParams. */ - function burn( - BurnCoverParams memory params - ) external; + function burn(BurnCoverParams memory params) external; /** * @notice Swaps `tokenIn` for `tokenOut`. @@ -53,12 +48,9 @@ interface ICoverPool is PoolsharkStructs { * @return amount0Delta The amount of token0 spent (negative) or received (positive) by the user * @return amount1Delta The amount of token1 spent (negative) or received (positive) by the user */ - function swap( - SwapParams memory params - ) external returns ( - int256 amount0Delta, - int256 amount1Delta - ); + function swap(SwapParams memory params) + external + returns (int256 amount0Delta, int256 amount1Delta); /** * @notice Quotes the amount of `tokenIn` for `tokenOut`. @@ -72,11 +64,12 @@ interface ICoverPool is PoolsharkStructs { * @return outAmount The amount of tokenOut to be received * @return priceAfter The Q64.96 square root price after the swap */ - function quote( - QuoteParams memory params - ) external view returns ( - int256 inAmount, - int256 outAmount, - uint256 priceAfter - ); + function quote(QuoteParams memory params) + external + view + returns ( + int256 inAmount, + int256 outAmount, + uint256 priceAfter + ); } diff --git a/contracts/interfaces/cover/ICoverPoolFactory.sol b/contracts/interfaces/cover/ICoverPoolFactory.sol index 68be72ca..8b091dc7 100644 --- a/contracts/interfaces/cover/ICoverPoolFactory.sol +++ b/contracts/interfaces/cover/ICoverPoolFactory.sol @@ -2,13 +2,12 @@ pragma solidity 0.8.18; abstract contract ICoverPoolFactory { - struct CoverPoolParams { bytes32 poolType; address tokenIn; address tokenOut; uint16 feeTier; - int16 tickSpread; + int16 tickSpread; uint16 twapLength; } @@ -16,21 +15,18 @@ abstract contract ICoverPoolFactory { * @notice Creates a new CoverPool. * @param params The CoverPoolParams struct referenced above. */ - function createCoverPool( - CoverPoolParams memory params - ) external virtual returns ( - address pool, - address poolToken - ); + function createCoverPool(CoverPoolParams memory params) + external + virtual + returns (address pool, address poolToken); /** * @notice Fetches an existing CoverPool. * @param params The CoverPoolParams struct referenced above. */ - function getCoverPool( - CoverPoolParams memory params - ) external view virtual returns ( - address pool, - address poolToken - ); + function getCoverPool(CoverPoolParams memory params) + external + view + virtual + returns (address pool, address poolToken); } diff --git a/contracts/interfaces/cover/ITwapSource.sol b/contracts/interfaces/cover/ITwapSource.sol index f2c97ecf..4c3e7097 100644 --- a/contracts/interfaces/cover/ITwapSource.sol +++ b/contracts/interfaces/cover/ITwapSource.sol @@ -4,34 +4,25 @@ pragma solidity ^0.8.18; import '../structs/PoolsharkStructs.sol'; interface ITwapSource { - function initialize( - PoolsharkStructs.CoverImmutables memory constants - ) external returns ( - uint8 initializable, - int24 startingTick - ); + function initialize(PoolsharkStructs.CoverImmutables memory constants) + external + returns (uint8 initializable, int24 startingTick); function calculateAverageTick( PoolsharkStructs.CoverImmutables memory constants, int24 latestTick - ) external view returns ( - int24 averageTick - ); + ) external view returns (int24 averageTick); function getPool( address tokenA, address tokenB, uint16 feeTier - ) external view returns ( - address pool - ); + ) external view returns (address pool); - function feeTierTickSpacing( - uint16 feeTier - ) external view returns ( - int24 tickSpacing - ); + function feeTierTickSpacing(uint16 feeTier) + external + view + returns (int24 tickSpacing); - function factory() - external view returns (address); + function factory() external view returns (address); } diff --git a/contracts/interfaces/limit/ILimitPool.sol b/contracts/interfaces/limit/ILimitPool.sol index a6dc24ef..8b16147c 100644 --- a/contracts/interfaces/limit/ILimitPool.sol +++ b/contracts/interfaces/limit/ILimitPool.sol @@ -4,28 +4,17 @@ pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; interface ILimitPool is LimitPoolStructs { - function initialize( - uint160 startPrice - ) external; + function initialize(uint160 startPrice) external; - function mintLimit( - MintLimitParams memory params - ) external returns ( - int256, - int256 - ); + function mintLimit(MintLimitParams memory params) + external + returns (int256, int256); - function burnLimit( - BurnLimitParams memory params - ) external returns ( - int256, - int256 - ); + function burnLimit(BurnLimitParams memory params) + external + returns (int256, int256); - function fees( - FeesParams memory params - ) external returns ( - uint128 token0Fees, - uint128 token1Fees - ); + function fees(FeesParams memory params) + external + returns (uint128 token0Fees, uint128 token1Fees); } diff --git a/contracts/interfaces/limit/ILimitPoolFactory.sol b/contracts/interfaces/limit/ILimitPoolFactory.sol index 04b369ff..8c3e4411 100644 --- a/contracts/interfaces/limit/ILimitPoolFactory.sol +++ b/contracts/interfaces/limit/ILimitPoolFactory.sol @@ -4,21 +4,19 @@ pragma solidity 0.8.18; import '../structs/PoolsharkStructs.sol'; import '../../base/storage/LimitPoolFactoryStorage.sol'; -abstract contract ILimitPoolFactory is LimitPoolFactoryStorage, PoolsharkStructs { - function createLimitPool( - LimitPoolParams memory params - ) external virtual returns ( - address pool, - address poolToken - ); +abstract contract ILimitPoolFactory is + LimitPoolFactoryStorage, + PoolsharkStructs +{ + function createLimitPool(LimitPoolParams memory params) + external + virtual + returns (address pool, address poolToken); function getLimitPool( address tokenIn, address tokenOut, - uint16 swapFee, - uint16 poolTypeId - ) external view virtual returns ( - address pool, - address poolToken - ); + uint16 swapFee, + uint16 poolTypeId + ) external view virtual returns (address pool, address poolToken); } diff --git a/contracts/interfaces/limit/ILimitPoolManager.sol b/contracts/interfaces/limit/ILimitPoolManager.sol index f0a69fc5..ecc159f4 100644 --- a/contracts/interfaces/limit/ILimitPoolManager.sol +++ b/contracts/interfaces/limit/ILimitPoolManager.sol @@ -4,17 +4,15 @@ pragma solidity 0.8.18; /// @notice LimitPoolManager interface interface ILimitPoolManager { function owner() external view returns (address); + function feeTo() external view returns (address); + function feeDeltaConst() external view returns (uint16); - function poolTypes( - uint16 poolType - ) external view returns ( - address poolImpl, - address tokenImpl - ); - function feeTiers( - uint16 swapFee - ) external view returns ( - int16 tickSpacing - ); + + function poolTypes(uint16 poolType) + external + view + returns (address poolImpl, address tokenImpl); + + function feeTiers(uint16 swapFee) external view returns (int16 tickSpacing); } diff --git a/contracts/interfaces/limit/ILimitPoolStorageView.sol b/contracts/interfaces/limit/ILimitPoolStorageView.sol index d6ae0159..fd6f5250 100644 --- a/contracts/interfaces/limit/ILimitPoolStorageView.sol +++ b/contracts/interfaces/limit/ILimitPoolStorageView.sol @@ -4,13 +4,16 @@ pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; interface ILimitPoolStorageView is LimitPoolStructs { - function globalState() external view returns ( - RangePoolState memory pool, - LimitPoolState memory pool0, - LimitPoolState memory pool1, - uint128 liquidityGlobal, - uint32 positionIdNext, - uint32 epoch, - uint8 unlocked - ); + function globalState() + external + view + returns ( + RangePoolState memory pool, + LimitPoolState memory pool0, + LimitPoolState memory pool1, + uint128 liquidityGlobal, + uint32 positionIdNext, + uint32 epoch, + uint8 unlocked + ); } diff --git a/contracts/interfaces/limit/ILimitPoolView.sol b/contracts/interfaces/limit/ILimitPoolView.sol index fe892c2e..262730ba 100644 --- a/contracts/interfaces/limit/ILimitPoolView.sol +++ b/contracts/interfaces/limit/ILimitPoolView.sol @@ -4,33 +4,26 @@ pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; interface ILimitPoolView is LimitPoolStructs { + function snapshotLimit(SnapshotLimitParams memory params) + external + view + returns (uint128, uint128); - function snapshotLimit( - SnapshotLimitParams memory params - ) external view returns( - uint128, - uint128 - ); + function immutables() external view returns (LimitImmutables memory); - function immutables( - ) external view returns( - LimitImmutables memory - ); + function priceBounds(int16 tickSpacing) + external + pure + returns (uint160 minPrice, uint160 maxPrice); - function priceBounds( - int16 tickSpacing - ) external pure returns ( - uint160 minPrice, - uint160 maxPrice - ); - - function sample( - uint32[] memory secondsAgo - ) external view returns( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ); + function sample(uint32[] memory secondsAgo) + external + view + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ); } diff --git a/contracts/interfaces/range/IRangePool.sol b/contracts/interfaces/range/IRangePool.sol index ea7048bd..931b8ee7 100644 --- a/contracts/interfaces/range/IRangePool.sol +++ b/contracts/interfaces/range/IRangePool.sol @@ -5,74 +5,72 @@ import '../structs/RangePoolStructs.sol'; import './IRangePoolManager.sol'; interface IRangePool is RangePoolStructs { - function mintRange( - MintRangeParams memory mintParams - ) external returns ( - int256, - int256 - ); + function mintRange(MintRangeParams memory mintParams) + external + returns (int256, int256); - function burnRange( - BurnRangeParams memory burnParams - ) external returns ( - int256, - int256 - ); + function burnRange(BurnRangeParams memory burnParams) + external + returns (int256, int256); - function swap( - SwapParams memory params - ) external returns ( - int256 amount0, - int256 amount1 - ); + function swap(SwapParams memory params) + external + returns (int256 amount0, int256 amount1); - function quote( - QuoteParams memory params - ) external view returns ( - uint256 inAmount, - uint256 outAmount, - uint160 priceAfter - ); + function quote(QuoteParams memory params) + external + view + returns ( + uint256 inAmount, + uint256 outAmount, + uint160 priceAfter + ); - function snapshotRange( - uint32 positionId - ) external view returns( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum, - uint128 feesOwed0, - uint128 feesOwed1 - ); + function snapshotRange(uint32 positionId) + external + view + returns ( + int56 tickSecondsAccum, + uint160 secondsPerLiquidityAccum, + uint128 feesOwed0, + uint128 feesOwed1 + ); - function sample( - uint32[] memory secondsAgo - ) external view returns( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ); + function sample(uint32[] memory secondsAgo) + external + view + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ); - function positions(uint256 positionId) external view returns ( - uint256 feeGrowthInside0Last, - uint256 feeGrowthInside1Last, - uint128 liquidity, - int24 lower, - int24 upper - ); + function positions(uint256 positionId) + external + view + returns ( + uint256 feeGrowthInside0Last, + uint256 feeGrowthInside1Last, + uint128 liquidity, + int24 lower, + int24 upper + ); - function increaseSampleCount( - uint16 newSampleCountMax - ) external; + function increaseSampleCount(uint16 newSampleCountMax) external; - function ticks(int24) external view returns ( - RangeTick memory, - LimitTick memory - ); + function ticks(int24) + external + view + returns (RangeTick memory, LimitTick memory); - function samples(uint256) external view returns ( - uint32, - int56, - uint160 - ); + function samples(uint256) + external + view + returns ( + uint32, + int56, + uint160 + ); } diff --git a/contracts/interfaces/range/IRangePoolFactory.sol b/contracts/interfaces/range/IRangePoolFactory.sol index b6f991dc..ea566475 100644 --- a/contracts/interfaces/range/IRangePoolFactory.sol +++ b/contracts/interfaces/range/IRangePoolFactory.sol @@ -15,5 +15,5 @@ interface IRangePoolFactory { uint256 fee ) external view returns (address); - function owner() external view returns(address); + function owner() external view returns (address); } diff --git a/contracts/interfaces/range/IRangePoolManager.sol b/contracts/interfaces/range/IRangePoolManager.sol index b974dd5e..b5542828 100644 --- a/contracts/interfaces/range/IRangePoolManager.sol +++ b/contracts/interfaces/range/IRangePoolManager.sol @@ -5,7 +5,10 @@ import '../structs/RangePoolStructs.sol'; interface IRangePoolManager { function owner() external view returns (address); + function feeTo() external view returns (address); + function protocolFees(address pool) external view returns (uint16); + function feeTiers(uint16 swapFee) external view returns (int24); -} \ No newline at end of file +} diff --git a/contracts/interfaces/staking/IRangeStaker.sol b/contracts/interfaces/staking/IRangeStaker.sol index a2bbdee0..2bbc13dd 100644 --- a/contracts/interfaces/staking/IRangeStaker.sol +++ b/contracts/interfaces/staking/IRangeStaker.sol @@ -6,4 +6,4 @@ import '../structs/PoolsharkStructs.sol'; interface IRangeStaker is PoolsharkStructs { function stakeRange(StakeRangeParams memory) external; -} \ No newline at end of file +} diff --git a/contracts/interfaces/structs/LimitPoolStructs.sol b/contracts/interfaces/structs/LimitPoolStructs.sol index 08b4ca98..83558ae1 100644 --- a/contracts/interfaces/structs/LimitPoolStructs.sol +++ b/contracts/interfaces/structs/LimitPoolStructs.sol @@ -4,13 +4,12 @@ pragma solidity 0.8.18; import './PoolsharkStructs.sol'; interface LimitPoolStructs is PoolsharkStructs { - struct LimitPosition { uint128 liquidity; // expected amount to be used not actual - uint32 epochLast; // epoch when this position was created at - int24 lower; // lower price tick of position range - int24 upper; // upper price tick of position range - bool crossedInto; // whether the position was crossed into already + uint32 epochLast; // epoch when this position was created at + int24 lower; // lower price tick of position range + int24 upper; // upper price tick of position range + bool crossedInto; // whether the position was crossed into already } struct MintLimitCache { diff --git a/contracts/interfaces/structs/PoolsharkStructs.sol b/contracts/interfaces/structs/PoolsharkStructs.sol index 5af0f588..176a5457 100644 --- a/contracts/interfaces/structs/PoolsharkStructs.sol +++ b/contracts/interfaces/structs/PoolsharkStructs.sol @@ -4,12 +4,316 @@ pragma solidity 0.8.18; import '../cover/ITwapSource.sol'; interface PoolsharkStructs { + /** + * @custom:struct LimitPoolParams + */ + struct LimitPoolParams { + /** + * @custom:field tokenIn + * @notice Address for the first token in the pair + */ + address tokenIn; + /** + * @custom:field tokenOut + * @notice Address for the second token in the pair + */ + address tokenOut; + /** + * @custom:field startPrice + * @notice Q64.96 formatted sqrt price to start the pool at + */ + uint160 startPrice; + /** + * @custom:field swapFee + * @notice The base swap fee for the pool; 1000 = 0.1% fee + */ + uint16 swapFee; + /** + * @custom:field poolTypeId + * @notice The pool type id for which to clone the implementation for + */ + uint16 poolTypeId; + } + + /** + * @custom:struct MintRangeParams + */ + struct MintRangeParams { + /** + * @custom:field to + * @notice Address for the receiver of the minted position + */ + address to; + /** + * @custom:field lower + * @notice The lower price tick for the position range + */ + int24 lower; + /** + * @custom:field upper + * @notice The upper price tick for the position range + */ + int24 upper; + /** + * @custom:field positionId + * @notice 0 if creating a new position; id of previous if adding liquidity + */ + uint32 positionId; + /** + * @custom:field amount0 + * @notice token0 amount to be deposited into the minted position + */ + uint128 amount0; + /** + * @custom:field amount1 + * @notice token1 amount to be deposited into the minted position + */ + uint128 amount1; + /** + * @custom:field callbackData + * @notice callback data which gets passed back to msg.sender at the end of a `mint` call + */ + bytes callbackData; + } + + struct BurnRangeParams { + /** + * @custom:field to + * @notice Address for the receiver of the burned liquidity + */ + address to; + /** + * @custom:field positionId + * @notice id of previous position minted + */ + uint32 positionId; + uint128 burnPercent; + } + + /** + * @custom:struct MintLimitParams + */ + struct MintLimitParams { + /** + * @custom:field to + * @notice Address for the receiver of the minted position + */ + address to; + /** + * @custom:field amount + * @notice Token amount to be deposited into the minted position + */ + uint128 amount; + /** + * @custom:field mintPercent + * @notice The percent of `amount` below which a LimitPosition will not be minted + * @notice 1e26 = 1% + * @notice 5e25 = 0.5% + */ + uint96 mintPercent; + /** + * @custom:field positionId + * @notice 0 if creating a new position; id of previous if adding liquidity + */ + uint32 positionId; + /** + * @custom:field lower + * @notice The lower price tick for the position range + */ + int24 lower; + /** + * @custom:field upper + * @notice The upper price tick for the position range + */ + int24 upper; + /** + * @custom:field zeroForOne + * @notice True if depositing token0, the first token address in lexographical order + * @notice False if depositing token1, the second token address in lexographical order + */ + bool zeroForOne; + /** + * @custom:field callbackData + * @notice callback data which gets passed back to msg.sender at the end of a `mint` call + */ + bytes callbackData; + } + + /** + * @custom:struct BurnLimitParams + */ + struct BurnLimitParams { + /** + * @custom:field to + * @notice Address for the receiver of the collected position amounts + */ + address to; + /** + * @custom:field burnPercent + * @notice Percent of the remaining liquidity to be removed + * @notice 1e38 represents 100% + * @notice 5e37 represents 50% + * @notice 1e37 represents 10% + */ + uint128 burnPercent; + /** + * @custom:field positionId + * @notice 0 if creating a new position; id of previous if adding liquidity + */ + uint32 positionId; + /** + * @custom:field claim + * @notice The most recent tick crossed in this range + * @notice if `zeroForOne` is true, claim tick progresses from lower => upper + * @notice if `zeroForOne` is false, claim tick progresses from upper => lower + */ + int24 claim; + /** + * @custom:field zeroForOne + * @notice True if deposited token0, the first token address in lexographical order + * @notice False if deposited token1, the second token address in lexographical order + */ + bool zeroForOne; + } + + struct SwapParams { + /** + * @custom:field to + * @notice Address for the receiver of the swap token output + */ + address to; + /** + * @custom:field priceLimit + * @notice The Q64.96 formatted sqrt price to stop swapping at + * @notice zeroForOne (i.e. token0 => token1 swap) moves price lower + * @notice !zeroForOne (i.e. token1 => token0 swap) moves price higher + */ + uint160 priceLimit; + /** + * @custom:field amount + * @notice The maximum tokenIn to be spent (exactIn) + * @notice OR tokenOut amount to be received (!exactIn) + */ + uint128 amount; + /** + * @custom:field exactIn + * @notice True if `amount` is in tokenIn; False if `amount` is in tokenOut + */ + bool exactIn; + /** + * @custom:field zeroForOne + * @notice True if swapping token0 => token1 + * @notice False if swapping token1 => token0 + */ + bool zeroForOne; + /** + * @custom:field callbackData + * @notice callback data which gets passed back to msg.sender at the end of a `mint` call + */ + bytes callbackData; + } + + struct QuoteParams { + /** + * @custom:field priceLimit + * @notice The Q64.96 formatted sqrt price to stop swapping at + * @notice zeroForOne (i.e. token0 => token1 swap) moves price lower + * @notice !zeroForOne (i.e. token1 => token0 swap) moves price higher + */ + uint160 priceLimit; + /** + * @custom:field amount + * @notice The maximum tokenIn to be spent (exactIn) + * @notice OR tokenOut amount to be received (!exactIn) + */ + uint128 amount; + /** + * @custom:field exactIn + * @notice True if `amount` is in tokenIn; False if `amount` is in tokenOut + */ + bool exactIn; + /** + * @custom:field zeroForOne + * @notice True if swapping token0 => token1 + * @notice False if swapping token1 => token0 + */ + bool zeroForOne; + } + + struct SnapshotLimitParams { + /** + * @custom:field owner + * @notice The owner address of the Limit Position + */ + address owner; + /** + * @custom:field burnPercent + * @notice The % of liquidity to burn + * @notice 1e38 = 100% + */ + uint128 burnPercent; + /** + * @custom:field positionId + * @notice The position id for the LimitPosition + */ + uint32 positionId; + /** + * @custom:field claim + * @notice The most recent tick crossed in this range + * @notice if `zeroForOne` is true, claim tick progresses from lower => upper + * @notice if `zeroForOne` is false, claim tick progresses from upper => lower + */ + int24 claim; + /** + * @custom:field zeroForOne + * @notice True if swapping token0 => token1 + * @notice False if swapping token1 => token0 + */ + bool zeroForOne; + } + + struct FeesParams { + /** + * @custom:field protocolSwapFee0 + * @notice The protocol fee taken on all token0 fees + * @notice 1e4 = 100% + */ + uint16 protocolSwapFee0; + /** + * @custom:field protocolSwapFee1 + * @notice The protocol fee taken on all token1 fees + * @notice 1e4 = 100% + */ + uint16 protocolSwapFee1; + /** + * @custom:field protocolFillFee0 + * @notice The protocol fee taken on all token0 LimitPosition fills + * @notice 1e2 = 1% + */ + uint16 protocolFillFee0; + /** + * @custom:field protocolFillFee1 + * @notice The protocol fee taken on all token1 LimitPosition fills + * @notice 1e2 = 1% + */ + uint16 protocolFillFee1; + /** + * @custom:field setFeesFlags + * @notice The flags for which protocol fees will be set + * @notice - PROTOCOL_SWAP_FEE_0 = 2**0; + * @notice - PROTOCOL_SWAP_FEE_1 = 2**1; + * @notice - PROTOCOL_FILL_FEE_0 = 2**2; + * @notice - PROTOCOL_FILL_FEE_1 = 2**3; + */ + uint8 setFeesFlags; + } + struct GlobalState { RangePoolState pool; LimitPoolState pool0; LimitPoolState pool1; uint128 liquidityGlobal; - uint32 positionIdNext; + uint32 positionIdNext; uint32 epoch; uint8 unlocked; } @@ -23,14 +327,14 @@ interface PoolsharkStructs { } struct RangePoolState { - SampleState samples; + SampleState samples; uint200 feeGrowthGlobal0; uint200 feeGrowthGlobal1; uint160 secondsPerLiquidityAccum; - uint160 price; /// @dev Starting price current - uint128 liquidity; /// @dev Liquidity currently active - int56 tickSecondsAccum; - int24 tickAtPrice; + uint160 price; /// @dev Starting price current + uint128 liquidity; /// @dev Liquidity currently active + int56 tickSecondsAccum; + int24 tickAtPrice; uint16 protocolSwapFee0; uint16 protocolSwapFee1; } @@ -56,107 +360,113 @@ interface PoolsharkStructs { } struct Sample { - uint32 blockTimestamp; - int56 tickSecondsAccum; + uint32 blockTimestamp; + int56 tickSecondsAccum; uint160 secondsPerLiquidityAccum; } struct SampleState { - uint16 index; - uint16 count; - uint16 countMax; - } - - struct LimitPoolParams { - address tokenIn; - address tokenOut; - uint160 startPrice; - uint16 swapFee; - uint16 poolTypeId; - } - - struct SwapParams { - address to; - uint160 priceLimit; - uint128 amount; - bool exactIn; - bool zeroForOne; - bytes callbackData; + uint16 index; + uint16 count; + uint16 countMax; } - struct MintLimitParams { + struct StakeRangeParams { address to; - uint128 amount; - uint96 mintPercent; + address pool; uint32 positionId; - int24 lower; - int24 upper; - bool zeroForOne; - bytes callbackData; } - struct BurnLimitParams { + struct UnstakeRangeParams { address to; - uint128 burnPercent; + address pool; uint32 positionId; - int24 claim; - bool zeroForOne; } - struct MintRangeParams { + struct StakeFinParams { address to; - int24 lower; - int24 upper; - uint32 positionId; - uint128 amount0; - uint128 amount1; - bytes callbackData; + uint128 amount; } - struct BurnRangeParams { - address to; - uint32 positionId; - uint128 burnPercent; + struct QuoteResults { + address pool; + int256 amountIn; + int256 amountOut; + uint160 priceAfter; } - struct QuoteParams { - uint160 priceLimit; - uint128 amount; - bool exactIn; - bool zeroForOne; + struct LimitImmutables { + address owner; + address poolImpl; + address factory; + PriceBounds bounds; + address token0; + address token1; + address poolToken; + uint32 genesisTime; + int16 tickSpacing; + uint16 swapFee; } - struct FeesParams { - uint16 protocolSwapFee0; - uint16 protocolSwapFee1; - uint16 protocolFillFee0; - uint16 protocolFillFee1; - uint8 setFeesFlags; + struct CoverImmutables { + ITwapSource source; + PriceBounds bounds; + address owner; + address token0; + address token1; + address poolImpl; + address poolToken; + address inputPool; + uint128 minAmountPerAuction; + uint32 genesisTime; + int16 minPositionWidth; + int16 tickSpread; + uint16 twapLength; + uint16 auctionLength; + uint16 sampleInterval; + uint8 token0Decimals; + uint8 token1Decimals; + bool minAmountLowerPriced; } - struct SnapshotLimitParams { - address owner; - uint128 burnPercent; - uint32 positionId; - int24 claim; - bool zeroForOne; + struct PriceBounds { + uint160 min; + uint160 max; } - struct StakeRangeParams { - address to; - address pool; - uint32 positionId; + struct TickMap { + uint256 blocks; /// @dev - sets of words + mapping(uint256 => uint256) words; /// @dev - sets to words + mapping(uint256 => uint256) ticks; /// @dev - words to ticks + mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) epochs0; /// @dev - ticks to epochs + mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) epochs1; /// @dev - ticks to epochs } - struct UnstakeRangeParams { - address to; - address pool; - uint32 positionId; + struct SwapCache { + GlobalState state; + LimitImmutables constants; + uint256 price; + uint256 liquidity; + uint256 amountLeft; + uint256 input; + uint256 output; + uint160 crossPrice; + uint160 averagePrice; + uint160 secondsPerLiquidityAccum; + uint128 feeAmount; + int56 tickSecondsAccum; + int56 tickSecondsAccumBase; + int24 crossTick; + uint8 crossStatus; + bool limitActive; + bool exactIn; + bool cross; } - struct StakeFinParams { - address to; - uint128 amount; + enum CrossStatus { + RANGE, + LIMIT, + BOTH } /** @@ -168,38 +478,32 @@ interface PoolsharkStructs { * @notice Address for the receiver of the minted position */ address to; - /** * @custom:field amount * @notice Token amount to be deposited into the minted position */ uint128 amount; - /** * @custom:field positionId * @notice 0 if creating a new position; id of previous if adding liquidity */ uint32 positionId; - /** * @custom:field lower * @notice The lower price tick for the position range */ int24 lower; - /** * @custom:field upper * @notice The upper price tick for the position range */ int24 upper; - /** * @custom:field zeroForOne * @notice True if depositing token0, the first token address in lexographical order - * @notice False if depositing token1, the second token address in lexographical order + * @notice False if depositing token1, the second token address in lexographical order */ bool zeroForOne; - /** * @custom:field callbackData * @notice callback data which gets passed back to msg.sender at the end of a `mint` call @@ -216,7 +520,6 @@ interface PoolsharkStructs { * @notice Address for the receiver of the collected position amounts */ address to; - /** * @custom:field burnPercent * @notice Percent of the remaining liquidity to be removed @@ -225,13 +528,11 @@ interface PoolsharkStructs { * @notice 1e37 represents 10% */ uint128 burnPercent; - /** * @custom:field positionId * @notice 0 if creating a new position; id of previous if adding liquidity */ uint32 positionId; - /** * @custom:field claim * @notice The most recent tick crossed in this range @@ -239,18 +540,16 @@ interface PoolsharkStructs { * @notice if `zeroForOne` is false, claim tick progresses from lower => upper */ int24 claim; - /** * @custom:field zeroForOne * @notice True if deposited token0, the first token address in lexographical order - * @notice False if deposited token1, the second token address in lexographical order + * @notice False if deposited token1, the second token address in lexographical order */ bool zeroForOne; - /** * @custom:field sync * @notice True will sync the pool latestTick - * @notice False will skip syncing latestTick + * @notice False will skip syncing latestTick */ bool sync; } @@ -264,13 +563,11 @@ interface PoolsharkStructs { * @notice Address of the position owner */ address owner; - /** * @custom:field positionId * @notice id of position */ uint32 positionId; - /** * @custom:field burnPercent * @notice Percent of the remaining liquidity to be removed @@ -279,7 +576,6 @@ interface PoolsharkStructs { * @notice 1e37 represents 10% */ uint128 burnPercent; - /** * @custom:field claim * @notice The most recent tick crossed in this range @@ -287,93 +583,11 @@ interface PoolsharkStructs { * @notice if `zeroForOne` is false, claim tick progresses from lower => upper */ int24 claim; - /** * @custom:field zeroForOne * @notice True if deposited token0, the first token address in lexographical order - * @notice False if deposited token1, the second token address in lexographical order + * @notice False if deposited token1, the second token address in lexographical order */ bool zeroForOne; } - - struct QuoteResults { - address pool; - int256 amountIn; - int256 amountOut; - uint160 priceAfter; - } - - struct LimitImmutables { - address owner; - address poolImpl; - address factory; - PriceBounds bounds; - address token0; - address token1; - address poolToken; - uint32 genesisTime; - int16 tickSpacing; - uint16 swapFee; - } - - struct CoverImmutables { - ITwapSource source; - PriceBounds bounds; - address owner; - address token0; - address token1; - address poolImpl; - address poolToken; - address inputPool; - uint128 minAmountPerAuction; - uint32 genesisTime; - int16 minPositionWidth; - int16 tickSpread; - uint16 twapLength; - uint16 auctionLength; - uint16 sampleInterval; - uint8 token0Decimals; - uint8 token1Decimals; - bool minAmountLowerPriced; - } - - struct PriceBounds { - uint160 min; - uint160 max; - } - - struct TickMap { - uint256 blocks; /// @dev - sets of words - mapping(uint256 => uint256) words; /// @dev - sets to words - mapping(uint256 => uint256) ticks; /// @dev - words to ticks - mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) epochs0; /// @dev - ticks to epochs - mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) epochs1; /// @dev - ticks to epochs - } - - struct SwapCache { - GlobalState state; - LimitImmutables constants; - uint256 price; - uint256 liquidity; - uint256 amountLeft; - uint256 input; - uint256 output; - uint160 crossPrice; - uint160 averagePrice; - uint160 secondsPerLiquidityAccum; - uint128 feeAmount; - int56 tickSecondsAccum; - int56 tickSecondsAccumBase; - int24 crossTick; - uint8 crossStatus; - bool limitActive; - bool exactIn; - bool cross; - } - - enum CrossStatus { - RANGE, - LIMIT, - BOTH - } -} \ No newline at end of file +} diff --git a/contracts/interfaces/structs/RangePoolStructs.sol b/contracts/interfaces/structs/RangePoolStructs.sol index 6d229da0..cdea9143 100644 --- a/contracts/interfaces/structs/RangePoolStructs.sol +++ b/contracts/interfaces/structs/RangePoolStructs.sol @@ -4,7 +4,6 @@ pragma solidity 0.8.18; import './PoolsharkStructs.sol'; interface RangePoolStructs is PoolsharkStructs { - struct RangePosition { uint256 feeGrowthInside0Last; uint256 feeGrowthInside1Last; @@ -84,12 +83,12 @@ interface RangePoolStructs is PoolsharkStructs { uint128 liquidity; uint128 amount0; uint128 amount1; - int56 tickSecondsAccum; - int56 tickSecondsAccumLower; - int56 tickSecondsAccumUpper; - uint32 secondsOutsideLower; - uint32 secondsOutsideUpper; - uint32 blockTimestamp; - int24 tick; + int56 tickSecondsAccum; + int56 tickSecondsAccumLower; + int56 tickSecondsAccumUpper; + uint32 secondsOutsideLower; + uint32 secondsOutsideUpper; + uint32 blockTimestamp; + int24 tick; } } diff --git a/contracts/libraries/Samples.sol b/contracts/libraries/Samples.sol index 4d784b4b..609cb9ab 100644 --- a/contracts/libraries/Samples.sol +++ b/contracts/libraries/Samples.sol @@ -16,17 +16,12 @@ library Samples { uint160 secondsPerLiquidityAccum ); - event SampleCountIncreased( - uint16 newSampleCountMax - ); + event SampleCountIncreased(uint16 newSampleCountMax); function initialize( RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.RangePoolState memory state - ) internal returns ( - PoolsharkStructs.RangePoolState memory - ) - { + ) internal returns (PoolsharkStructs.RangePoolState memory) { samples[0] = PoolsharkStructs.Sample({ blockTimestamp: uint32(block.timestamp), tickSecondsAccum: 0, @@ -44,11 +39,8 @@ library Samples { RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.SampleState memory sampleState, uint128 startLiquidity, /// @dev - liquidity from start of block - int24 tick - ) internal returns ( - uint16 sampleIndexNew, - uint16 sampleLengthNew - ) { + int24 tick + ) internal returns (uint16 sampleIndexNew, uint16 sampleLengthNew) { // grab the latest sample RangePoolStructs.Sample memory newSample = samples[sampleState.index]; @@ -56,15 +48,22 @@ library Samples { if (newSample.blockTimestamp + 2 > uint32(block.timestamp)) return (sampleState.index, sampleState.count); - if (sampleState.countMax > sampleState.count - && sampleState.index == (sampleState.count - 1)) { + if ( + sampleState.countMax > sampleState.count && + sampleState.index == (sampleState.count - 1) + ) { // increase sampleLengthNew if old size exceeded sampleLengthNew = sampleState.count + 1; } else { sampleLengthNew = sampleState.count; } sampleIndexNew = (sampleState.index + 1) % sampleLengthNew; - samples[sampleIndexNew] = _build(newSample, uint32(block.timestamp), tick, startLiquidity); + samples[sampleIndexNew] = _build( + newSample, + uint32(block.timestamp), + tick, + startLiquidity + ); emit SampleRecorded( samples[sampleIndexNew].tickSecondsAccum, @@ -77,7 +76,7 @@ library Samples { PoolsharkStructs.RangePoolState storage pool, uint16 newSampleCountMax ) internal { - if (newSampleCountMax <= pool.samples.countMax) return ; + if (newSampleCountMax <= pool.samples.countMax) return; for (uint16 i = pool.samples.countMax; i < newSampleCountMax; i++) { samples[i].blockTimestamp = 1; } @@ -85,85 +84,108 @@ library Samples { emit SampleCountIncreased(newSampleCountMax); } - function get( - address pool, - RangePoolStructs.SampleParams memory params - ) internal view returns ( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ) { + function get(address pool, RangePoolStructs.SampleParams memory params) + internal + view + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ) + { if (params.sampleLength == 0) require(false, 'InvalidSampleLength()'); - if (params.secondsAgo.length == 0) require(false, 'SecondsAgoArrayEmpty()'); - uint256 size = params.secondsAgo.length > 1 ? params.secondsAgo.length : 2; + if (params.secondsAgo.length == 0) + require(false, 'SecondsAgoArrayEmpty()'); + uint256 size = params.secondsAgo.length > 1 + ? params.secondsAgo.length + : 2; uint32[] memory secondsAgo = new uint32[](size); if (params.secondsAgo.length == 1) { secondsAgo = new uint32[](2); secondsAgo[0] = params.secondsAgo[0]; secondsAgo[1] = params.secondsAgo[0] + 2; - } - else secondsAgo = params.secondsAgo; + } else secondsAgo = params.secondsAgo; - if (secondsAgo[0] == secondsAgo[secondsAgo.length - 1]) require(false, 'SecondsAgoArrayValuesEqual()'); + if (secondsAgo[0] == secondsAgo[secondsAgo.length - 1]) + require(false, 'SecondsAgoArrayValuesEqual()'); tickSecondsAccum = new int56[](secondsAgo.length); secondsPerLiquidityAccum = new uint160[](secondsAgo.length); for (uint256 i = 0; i < secondsAgo.length; i++) { - ( - tickSecondsAccum[i], - secondsPerLiquidityAccum[i] - ) = getSingle( + (tickSecondsAccum[i], secondsPerLiquidityAccum[i]) = getSingle( IRangePool(pool), params, secondsAgo[i] ); } if (secondsAgo[secondsAgo.length - 1] > secondsAgo[0]) { - averageTick = int24((tickSecondsAccum[0] - tickSecondsAccum[secondsAgo.length - 1]) - / int32(secondsAgo[secondsAgo.length - 1] - secondsAgo[0])); - averagePrice = ConstantProduct.getPriceAtTick(averageTick, params.constants); - averageLiquidity = uint128((secondsPerLiquidityAccum[0] - secondsPerLiquidityAccum[secondsAgo.length - 1]) - * (secondsAgo[secondsAgo.length - 1] - secondsAgo[0])); + averageTick = int24( + (tickSecondsAccum[0] - + tickSecondsAccum[secondsAgo.length - 1]) / + int32(secondsAgo[secondsAgo.length - 1] - secondsAgo[0]) + ); + averagePrice = ConstantProduct.getPriceAtTick( + averageTick, + params.constants + ); + averageLiquidity = uint128( + (secondsPerLiquidityAccum[0] - + secondsPerLiquidityAccum[secondsAgo.length - 1]) * + (secondsAgo[secondsAgo.length - 1] - secondsAgo[0]) + ); } else { - averageTick = int24((tickSecondsAccum[secondsAgo.length - 1] - tickSecondsAccum[0]) - / int32(secondsAgo[0] - secondsAgo[secondsAgo.length - 1])); - averagePrice = ConstantProduct.getPriceAtTick(averageTick, params.constants); - averageLiquidity = uint128((secondsPerLiquidityAccum[secondsAgo.length - 1] - secondsPerLiquidityAccum[0]) - * (secondsAgo[0] - secondsAgo[secondsAgo.length - 1])); + averageTick = int24( + (tickSecondsAccum[secondsAgo.length - 1] - + tickSecondsAccum[0]) / + int32(secondsAgo[0] - secondsAgo[secondsAgo.length - 1]) + ); + averagePrice = ConstantProduct.getPriceAtTick( + averageTick, + params.constants + ); + averageLiquidity = uint128( + (secondsPerLiquidityAccum[secondsAgo.length - 1] - + secondsPerLiquidityAccum[0]) * + (secondsAgo[0] - secondsAgo[secondsAgo.length - 1]) + ); } } - function _poolSample( - IRangePool pool, - uint256 sampleIndex - ) internal view returns ( - RangePoolStructs.Sample memory - ) { + function _poolSample(IRangePool pool, uint256 sampleIndex) + internal + view + returns (RangePoolStructs.Sample memory) + { ( uint32 blockTimestamp, int56 tickSecondsAccum, uint160 liquidityPerSecondsAccum ) = IRangePool(pool).samples(sampleIndex); - return PoolsharkStructs.Sample( - blockTimestamp, - tickSecondsAccum, - liquidityPerSecondsAccum - ); + return + PoolsharkStructs.Sample( + blockTimestamp, + tickSecondsAccum, + liquidityPerSecondsAccum + ); } function getSingle( IRangePool pool, RangePoolStructs.SampleParams memory params, uint32 secondsAgo - ) internal view returns ( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum - ) { - RangePoolStructs.Sample memory latest = _poolSample(pool, params.sampleIndex); + ) + internal + view + returns (int56 tickSecondsAccum, uint160 secondsPerLiquidityAccum) + { + RangePoolStructs.Sample memory latest = _poolSample( + pool, + params.sampleIndex + ); if (secondsAgo == 0) { // if 2 seconds have elapsed build new sample @@ -174,11 +196,8 @@ library Samples { params.tick, params.liquidity ); - } - return ( - latest.tickSecondsAccum, - latest.secondsPerLiquidityAccum - ); + } + return (latest.tickSecondsAccum, latest.secondsPerLiquidityAccum); } uint32 targetTime = uint32(block.timestamp) - secondsAgo; @@ -187,12 +206,7 @@ library Samples { ( RangePoolStructs.Sample memory firstSample, RangePoolStructs.Sample memory secondSample - ) = _getAdjacentSamples( - pool, - latest, - params, - targetTime - ); + ) = _getAdjacentSamples(pool, latest, params, targetTime); if (targetTime == firstSample.blockTimestamp) { // first sample @@ -208,20 +222,24 @@ library Samples { ); } else { // average two samples - int32 sampleTimeDelta = int32(secondSample.blockTimestamp - firstSample.blockTimestamp); - int56 targetDelta = int56(int32(targetTime - firstSample.blockTimestamp)); + int32 sampleTimeDelta = int32( + secondSample.blockTimestamp - firstSample.blockTimestamp + ); + int56 targetDelta = int56( + int32(targetTime - firstSample.blockTimestamp) + ); return ( firstSample.tickSecondsAccum + - ((secondSample.tickSecondsAccum - firstSample.tickSecondsAccum) - / sampleTimeDelta) - * targetDelta, + ((secondSample.tickSecondsAccum - + firstSample.tickSecondsAccum) / sampleTimeDelta) * + targetDelta, firstSample.secondsPerLiquidityAccum + uint160( (uint256( - secondSample.secondsPerLiquidityAccum - firstSample.secondsPerLiquidityAccum - ) - * uint256(uint56(targetDelta))) - / uint32(sampleTimeDelta) + secondSample.secondsPerLiquidityAccum - + firstSample.secondsPerLiquidityAccum + ) * uint256(uint56(targetDelta))) / + uint32(sampleTimeDelta) ) ); } @@ -231,43 +249,42 @@ library Samples { PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, uint256 liquidity - ) internal view returns ( - uint160 latestPrice, - uint160 secondsPerLiquidityAccum, - int56 tickSecondsAccum - ) { + ) + internal + view + returns ( + uint160 latestPrice, + uint160 secondsPerLiquidityAccum, + int56 tickSecondsAccum + ) + { uint32 timeDelta = timeElapsed(constants); - ( - tickSecondsAccum, - secondsPerLiquidityAccum - ) = getSingle( - IRangePool(address(this)), - RangePoolStructs.SampleParams( - state.pool.samples.index, - state.pool.samples.count, - uint32(block.timestamp), - new uint32[](2), - state.pool.tickAtPrice, - liquidity.toUint128(), - constants - ), - 0 + (tickSecondsAccum, secondsPerLiquidityAccum) = getSingle( + IRangePool(address(this)), + RangePoolStructs.SampleParams( + state.pool.samples.index, + state.pool.samples.count, + uint32(block.timestamp), + new uint32[](2), + state.pool.tickAtPrice, + liquidity.toUint128(), + constants + ), + 0 ); // grab older sample for dynamic fee calculation - ( - int56 tickSecondsAccumBase, - ) = Samples.getSingle( - IRangePool(address(this)), - RangePoolStructs.SampleParams( - state.pool.samples.index, - state.pool.samples.count, - uint32(block.timestamp), - new uint32[](2), - state.pool.tickAtPrice, - liquidity.toUint128(), - constants - ), - timeDelta + (int56 tickSecondsAccumBase, ) = Samples.getSingle( + IRangePool(address(this)), + RangePoolStructs.SampleParams( + state.pool.samples.index, + state.pool.samples.count, + uint32(block.timestamp), + new uint32[](2), + state.pool.tickAtPrice, + liquidity.toUint128(), + constants + ), + timeDelta ); latestPrice = calculateLatestPrice( @@ -285,9 +302,7 @@ library Samples { uint32 timeDelta, uint32 timeDeltaMax, PoolsharkStructs.LimitImmutables memory constants - ) private pure returns ( - uint160 averagePrice - ) { + ) private pure returns (uint160 averagePrice) { int56 tickSecondsAccumDiff = tickSecondsAccum - tickSecondsAccumBase; int24 averageTick; if (timeDelta == timeDeltaMax) { @@ -298,22 +313,18 @@ library Samples { averagePrice = ConstantProduct.getPriceAtTick(averageTick, constants); } - - function timeElapsed( - PoolsharkStructs.LimitImmutables memory constants - ) private view returns ( - uint32 - ) + function timeElapsed(PoolsharkStructs.LimitImmutables memory constants) + private + view + returns (uint32) { - return uint32(block.timestamp) - constants.genesisTime >= TIME_DELTA_MAX - ? TIME_DELTA_MAX - : uint32(block.timestamp - constants.genesisTime); + return + uint32(block.timestamp) - constants.genesisTime >= TIME_DELTA_MAX + ? TIME_DELTA_MAX + : uint32(block.timestamp - constants.genesisTime); } - function _lte( - uint32 timeA, - uint32 timeB - ) private view returns (bool) { + function _lte(uint32 timeA, uint32 timeB) private view returns (bool) { uint32 currentTime = uint32(block.timestamp); if (timeA <= currentTime && timeB <= currentTime) return timeA <= timeB; @@ -332,20 +343,23 @@ library Samples { function _build( RangePoolStructs.Sample memory newSample, - uint32 blockTimestamp, - int24 tick, + uint32 blockTimestamp, + int24 tick, uint128 liquidity - ) internal pure returns ( - RangePoolStructs.Sample memory - ) { - int56 timeDelta = int56(uint56(blockTimestamp - newSample.blockTimestamp)); + ) internal pure returns (RangePoolStructs.Sample memory) { + int56 timeDelta = int56( + uint56(blockTimestamp - newSample.blockTimestamp) + ); return PoolsharkStructs.Sample({ blockTimestamp: blockTimestamp, - tickSecondsAccum: newSample.tickSecondsAccum + int56(tick) * int32(timeDelta), + tickSecondsAccum: newSample.tickSecondsAccum + + int56(tick) * + int32(timeDelta), secondsPerLiquidityAccum: newSample.secondsPerLiquidityAccum + - ((uint160(uint56(timeDelta)) << 128) / (liquidity > 0 ? liquidity : 1)) + ((uint160(uint56(timeDelta)) << 128) / + (liquidity > 0 ? liquidity : 1)) }); } @@ -354,12 +368,16 @@ library Samples { uint32 targetTime, uint16 sampleIndex, uint16 sampleLength - ) private view returns ( - RangePoolStructs.Sample memory firstSample, - RangePoolStructs.Sample memory secondSample - ) { + ) + private + view + returns ( + RangePoolStructs.Sample memory firstSample, + RangePoolStructs.Sample memory secondSample + ) + { uint256 oldIndex = (sampleIndex + 1) % sampleLength; - uint256 newIndex = oldIndex + sampleLength - 1; + uint256 newIndex = oldIndex + sampleLength - 1; uint256 index; while (true) { // start in the middle @@ -378,8 +396,14 @@ library Samples { secondSample = _poolSample(pool, (index + 1) % sampleLength); // check if target time within first and second sample - bool targetAfterFirst = _lte(firstSample.blockTimestamp, targetTime); - bool targetBeforeSecond = _lte(targetTime, secondSample.blockTimestamp); + bool targetAfterFirst = _lte( + firstSample.blockTimestamp, + targetTime + ); + bool targetBeforeSecond = _lte( + targetTime, + secondSample.blockTimestamp + ); if (targetAfterFirst && targetBeforeSecond) break; if (!targetAfterFirst) newIndex = index - 1; else oldIndex = index + 1; @@ -391,28 +415,42 @@ library Samples { RangePoolStructs.Sample memory firstSample, RangePoolStructs.SampleParams memory params, uint32 targetTime - ) private view returns ( - RangePoolStructs.Sample memory, - RangePoolStructs.Sample memory - ) { + ) + private + view + returns (RangePoolStructs.Sample memory, RangePoolStructs.Sample memory) + { if (_lte(firstSample.blockTimestamp, targetTime)) { if (firstSample.blockTimestamp == targetTime) { - return (firstSample, PoolsharkStructs.Sample(0,0,0)); + return (firstSample, PoolsharkStructs.Sample(0, 0, 0)); } else { - return (firstSample, _build(firstSample, targetTime, params.tick, params.liquidity)); + return ( + firstSample, + _build( + firstSample, + targetTime, + params.tick, + params.liquidity + ) + ); } } - firstSample = _poolSample(pool, (params.sampleIndex + 1) % params.sampleLength); + firstSample = _poolSample( + pool, + (params.sampleIndex + 1) % params.sampleLength + ); if (firstSample.blockTimestamp == 0) { firstSample = _poolSample(pool, 0); } - if(!_lte(firstSample.blockTimestamp, targetTime)) require(false, 'SampleLengthNotAvailable()'); + if (!_lte(firstSample.blockTimestamp, targetTime)) + require(false, 'SampleLengthNotAvailable()'); - return _binarySearch( - pool, - targetTime, - params.sampleIndex, - params.sampleLength - ); + return + _binarySearch( + pool, + targetTime, + params.sampleIndex, + params.sampleLength + ); } -} \ No newline at end of file +} diff --git a/contracts/libraries/TickMap.sol b/contracts/libraries/TickMap.sol index b6c63c85..adb936f2 100644 --- a/contracts/libraries/TickMap.sol +++ b/contracts/libraries/TickMap.sol @@ -5,21 +5,18 @@ import './math/ConstantProduct.sol'; import '../interfaces/structs/PoolsharkStructs.sol'; library TickMap { - function get( PoolsharkStructs.TickMap storage tickMap, int24 tick, int24 tickSpacing - ) internal view returns ( - bool exists - ) { - ( - uint256 tickIndex, - uint256 wordIndex, - ) = getIndices(tick, tickSpacing); + ) internal view returns (bool exists) { + (uint256 tickIndex, uint256 wordIndex, ) = getIndices( + tick, + tickSpacing + ); // check if bit is already set - uint256 word = tickMap.ticks[wordIndex] | 1 << (tickIndex & 0xFF); + uint256 word = tickMap.ticks[wordIndex] | (1 << (tickIndex & 0xFF)); if (word == tickMap.ticks[wordIndex]) { return true; } @@ -30,24 +27,21 @@ library TickMap { PoolsharkStructs.TickMap storage tickMap, int24 tick, int24 tickSpacing - ) internal returns ( - bool exists - ) { - ( - uint256 tickIndex, - uint256 wordIndex, - uint256 blockIndex - ) = getIndices(tick, tickSpacing); + ) internal returns (bool exists) { + (uint256 tickIndex, uint256 wordIndex, uint256 blockIndex) = getIndices( + tick, + tickSpacing + ); // check if bit is already set - uint256 word = tickMap.ticks[wordIndex] | 1 << (tickIndex & 0xFF); + uint256 word = tickMap.ticks[wordIndex] | (1 << (tickIndex & 0xFF)); if (word == tickMap.ticks[wordIndex]) { return true; } - tickMap.ticks[wordIndex] = word; - tickMap.words[blockIndex] |= 1 << (wordIndex & 0xFF); // same as modulus 255 - tickMap.blocks |= 1 << blockIndex; + tickMap.ticks[wordIndex] = word; + tickMap.words[blockIndex] |= 1 << (wordIndex & 0xFF); // same as modulus 255 + tickMap.blocks |= 1 << blockIndex; return false; } @@ -56,11 +50,10 @@ library TickMap { int24 tick, int16 tickSpacing ) internal { - ( - uint256 tickIndex, - uint256 wordIndex, - uint256 blockIndex - ) = getIndices(tick, tickSpacing); + (uint256 tickIndex, uint256 wordIndex, uint256 blockIndex) = getIndices( + tick, + tickSpacing + ); tickMap.ticks[wordIndex] &= ~(1 << (tickIndex & 0xFF)); if (tickMap.ticks[wordIndex] == 0) { @@ -76,31 +69,34 @@ library TickMap { int24 tick, int16 tickSpacing, bool inclusive - ) internal view returns ( - int24 previousTick - ) { + ) internal view returns (int24 previousTick) { unchecked { // rounds up to ensure relative position if (tick % (tickSpacing / 2) != 0 || inclusive) { - if (tick < (ConstantProduct.maxTick(tickSpacing) - tickSpacing / 2)) { + if ( + tick < + (ConstantProduct.maxTick(tickSpacing) - tickSpacing / 2) + ) { /// @dev - ensures we cross when tick >= 0 if (tick >= 0) { tick += tickSpacing / 2; } else if (inclusive && tick % (tickSpacing / 2) == 0) { - /// @dev - ensures we cross when tick == tickAtPrice + /// @dev - ensures we cross when tick == tickAtPrice tick += tickSpacing / 2; } } } ( - uint256 tickIndex, - uint256 wordIndex, - uint256 blockIndex + uint256 tickIndex, + uint256 wordIndex, + uint256 blockIndex ) = getIndices(tick, tickSpacing); - uint256 word = tickMap.ticks[wordIndex] & ((1 << (tickIndex & 0xFF)) - 1); + uint256 word = tickMap.ticks[wordIndex] & + ((1 << (tickIndex & 0xFF)) - 1); if (word == 0) { - uint256 block_ = tickMap.words[blockIndex] & ((1 << (wordIndex & 0xFF)) - 1); + uint256 block_ = tickMap.words[blockIndex] & + ((1 << (wordIndex & 0xFF)) - 1); if (block_ == 0) { uint256 blockMap = tickMap.blocks & ((1 << blockIndex) - 1); if (blockMap == 0) return tick; @@ -120,45 +116,49 @@ library TickMap { int24 tick, int16 tickSpacing, bool inclusive - ) internal view returns ( - int24 nextTick - ) { + ) internal view returns (int24 nextTick) { unchecked { /// @dev - handles tickAtPrice being past tickSpacing / 2 if (inclusive && tick % tickSpacing != 0) { // e.g. tick is 5 we subtract 1 to look ahead at 5 if (tick > 0 && (tick % tickSpacing <= (tickSpacing / 2))) tick -= 1; - // e.g. tick is -5 we subtract 1 to look ahead at -5 + // e.g. tick is -5 we subtract 1 to look ahead at -5 else if (tick < 0 && (tick % tickSpacing <= -(tickSpacing / 2))) tick -= 1; - // e.g. tick = 7 and tickSpacing = 10 we sub 5 to look ahead at 5 - // e.g. tick = -2 and tickSpacing = 10 we sub 5 to look ahead at -5 - else - tick -= tickSpacing / 2; + // e.g. tick = 7 and tickSpacing = 10 we sub 5 to look ahead at 5 + // e.g. tick = -2 and tickSpacing = 10 we sub 5 to look ahead at -5 + else tick -= tickSpacing / 2; } /// @dev - handles negative ticks rounding up if (tick % (tickSpacing / 2) != 0) { if (tick < 0) - if (tick > (ConstantProduct.minTick(tickSpacing) + tickSpacing / 2)) - tick -= tickSpacing / 2; + if ( + tick > + (ConstantProduct.minTick(tickSpacing) + tickSpacing / 2) + ) tick -= tickSpacing / 2; } ( - uint256 tickIndex, - uint256 wordIndex, - uint256 blockIndex + uint256 tickIndex, + uint256 wordIndex, + uint256 blockIndex ) = getIndices(tick, tickSpacing); uint256 word; if ((tickIndex & 0xFF) != 255) { - word = tickMap.ticks[wordIndex] & ~((1 << ((tickIndex & 0xFF) + 1)) - 1); + word = + tickMap.ticks[wordIndex] & + ~((1 << ((tickIndex & 0xFF) + 1)) - 1); } if (word == 0) { uint256 block_; if ((blockIndex & 0xFF) != 255) { - block_ = tickMap.words[blockIndex] & ~((1 << ((wordIndex & 0xFF) + 1)) - 1); + block_ = + tickMap.words[blockIndex] & + ~((1 << ((wordIndex & 0xFF) + 1)) - 1); } if (block_ == 0) { - uint256 blockMap = tickMap.blocks & ~((1 << blockIndex + 1) - 1); + uint256 blockMap = tickMap.blocks & + ~((1 << (blockIndex + 1)) - 1); if (blockMap == 0) return tick; blockIndex = _lsb(blockMap); block_ = tickMap.words[blockIndex]; @@ -177,43 +177,59 @@ library TickMap { int24 stopTick, int24[] memory previousTicks, uint16 ticksIncluded - ) internal view returns ( - int24[] memory, - uint16, - int24 - ) { + ) + internal + view + returns ( + int24[] memory, + uint16, + int24 + ) + { // rounds up to ensure relative position if (tick % (tickSpacing / 2) != 0) { - if (tick < (ConstantProduct.maxTick(tickSpacing) - tickSpacing / 2)) { + if ( + tick < (ConstantProduct.maxTick(tickSpacing) - tickSpacing / 2) + ) { /// @dev - ensures we cross when tick >= 0 if (tick >= 0) { tick += tickSpacing / 2; } } } - LimitPoolStructs.TickMapLocals memory locals; - ( - locals.tickIndex, - locals.wordIndex, - locals.blockIndex - ) = getIndices(tick, tickSpacing); - locals.word = tickMap.ticks[locals.wordIndex] & ((1 << (locals.tickIndex & 0xFF)) - 1); + LimitPoolStructs.TickMapLocals memory locals; + (locals.tickIndex, locals.wordIndex, locals.blockIndex) = getIndices( + tick, + tickSpacing + ); + locals.word = + tickMap.ticks[locals.wordIndex] & + ((1 << (locals.tickIndex & 0xFF)) - 1); while (locals.word != 0 && tick > stopTick) { // ticks left within word - tick = _tick((locals.wordIndex << 8) | _msb(locals.word), tickSpacing); + tick = _tick( + (locals.wordIndex << 8) | _msb(locals.word), + tickSpacing + ); previousTicks[ticksIncluded] = tick; unchecked { ++ticksIncluded; } - ( - locals.tickIndex,, - ) = getIndices(tick, tickSpacing); + (locals.tickIndex, , ) = getIndices(tick, tickSpacing); locals.word = locals.word & ((1 << (locals.tickIndex & 0xFF)) - 1); } // no ticks left within word - // int24 firstTickNextWord = - return (previousTicks, ticksIncluded, locals.wordIndex > 0 ? _tick((locals.wordIndex - 1) << 8 | _msb(1 << 255), tickSpacing) - : ConstantProduct.minTick(tickSpacing)); + // int24 firstTickNextWord = + return ( + previousTicks, + ticksIncluded, + locals.wordIndex > 0 + ? _tick( + ((locals.wordIndex - 1) << 8) | _msb(1 << 255), + tickSpacing + ) + : ConstantProduct.minTick(tickSpacing) + ); } function nextTicksWithinWord( @@ -223,86 +239,106 @@ library TickMap { int24 stopTick, int24[] memory nextTicks, uint16 ticksIncluded - ) internal view returns ( - int24[] memory, - uint16, - int24 - ) { + ) + internal + view + returns ( + int24[] memory, + uint16, + int24 + ) + { /// @dev - handles negative ticks rounding up if (tick % (tickSpacing / 2) != 0) { if (tick < 0) - if (tick > (ConstantProduct.minTick(tickSpacing) + tickSpacing / 2)) - tick -= tickSpacing / 2; + if ( + tick > + (ConstantProduct.minTick(tickSpacing) + tickSpacing / 2) + ) tick -= tickSpacing / 2; } - LimitPoolStructs.TickMapLocals memory locals; - ( - locals.tickIndex, - locals.wordIndex, - locals.blockIndex - ) = getIndices(tick, tickSpacing); + LimitPoolStructs.TickMapLocals memory locals; + (locals.tickIndex, locals.wordIndex, locals.blockIndex) = getIndices( + tick, + tickSpacing + ); if ((locals.tickIndex & 0xFF) != 255) { - locals.word = tickMap.ticks[locals.wordIndex] & ~((1 << ((locals.tickIndex & 0xFF) + 1)) - 1); + locals.word = + tickMap.ticks[locals.wordIndex] & + ~((1 << ((locals.tickIndex & 0xFF) + 1)) - 1); } while (locals.word != 0 && tick < stopTick) { // ticks left within word - tick = _tick((locals.wordIndex << 8) | _lsb(locals.word), tickSpacing); + tick = _tick( + (locals.wordIndex << 8) | _lsb(locals.word), + tickSpacing + ); nextTicks[ticksIncluded] = tick; unchecked { ++ticksIncluded; } - ( - locals.tickIndex,, - ) = getIndices(tick, tickSpacing); + (locals.tickIndex, , ) = getIndices(tick, tickSpacing); if ((locals.tickIndex & 0xFF) != 255) { - locals.word = locals.word & ~((1 << ((locals.tickIndex & 0xFF) + 1)) - 1); + locals.word = + locals.word & + ~((1 << ((locals.tickIndex & 0xFF) + 1)) - 1); } } // no ticks left within word - return (nextTicks, ticksIncluded, _tick((locals.wordIndex + 1) << 8 | _lsb(1), tickSpacing)); + return ( + nextTicks, + ticksIncluded, + _tick(((locals.wordIndex + 1) << 8) | _lsb(1), tickSpacing) + ); } - function getIndices( - int24 tick, - int24 tickSpacing - ) public pure returns ( + function getIndices(int24 tick, int24 tickSpacing) + public + pure + returns ( uint256 tickIndex, uint256 wordIndex, uint256 blockIndex ) { unchecked { - if (tick > ConstantProduct.MAX_TICK) require(false, 'TickIndexOverflow()'); - if (tick < ConstantProduct.MIN_TICK) require(false, 'TickIndexUnderflow()'); - if (tick % (tickSpacing / 2) != 0) tick = round(tick, tickSpacing / 2); - tickIndex = uint256(int256((round(tick, tickSpacing / 2) - - round(ConstantProduct.MIN_TICK, tickSpacing / 2)) - / (tickSpacing / 2))); - wordIndex = tickIndex >> 8; // 2^8 ticks per word + if (tick > ConstantProduct.MAX_TICK) + require(false, 'TickIndexOverflow()'); + if (tick < ConstantProduct.MIN_TICK) + require(false, 'TickIndexUnderflow()'); + if (tick % (tickSpacing / 2) != 0) + tick = round(tick, tickSpacing / 2); + tickIndex = uint256( + int256( + (round(tick, tickSpacing / 2) - + round(ConstantProduct.MIN_TICK, tickSpacing / 2)) / + (tickSpacing / 2) + ) + ); + wordIndex = tickIndex >> 8; // 2^8 ticks per word blockIndex = tickIndex >> 16; // 2^8 words per block if (blockIndex > 255) require(false, 'BlockIndexOverflow()'); } } - - - function _tick ( - uint256 tickIndex, - int24 tickSpacing - ) internal pure returns ( - int24 tick - ) { + function _tick(uint256 tickIndex, int24 tickSpacing) + internal + pure + returns (int24 tick) + { unchecked { - if (tickIndex > uint24(round(ConstantProduct.MAX_TICK, tickSpacing) * 2) * 2) - require(false, 'TickIndexOverflow()'); - tick = int24(int256(tickIndex) * (tickSpacing / 2) + round(ConstantProduct.MIN_TICK, tickSpacing / 2)); + if ( + tickIndex > + uint24(round(ConstantProduct.MAX_TICK, tickSpacing) * 2) * 2 + ) require(false, 'TickIndexOverflow()'); + tick = int24( + int256(tickIndex) * + (tickSpacing / 2) + + round(ConstantProduct.MIN_TICK, tickSpacing / 2) + ); } } - function _msb( - uint256 x - ) internal pure returns ( - uint8 r - ) { + function _msb(uint256 x) internal pure returns (uint8 r) { unchecked { assert(x > 0); if (x >= 0x100000000000000000000000000000000) { @@ -337,11 +373,7 @@ library TickMap { } } - function _lsb( - uint256 x - ) internal pure returns ( - uint8 r - ) { + function _lsb(uint256 x) internal pure returns (uint8 r) { unchecked { assert(x > 0); // if x is 0 return 0 r = 255; @@ -384,38 +416,35 @@ library TickMap { } } - function round( - int24 tick, - int24 tickSpacing - ) internal pure returns ( - int24 roundedTick - ) { - return tick / tickSpacing * tickSpacing; + function round(int24 tick, int24 tickSpacing) + internal + pure + returns (int24 roundedTick) + { + return (tick / tickSpacing) * tickSpacing; } function roundHalf( int24 tick, PoolsharkStructs.LimitImmutables memory constants, uint256 price - ) internal pure returns ( - int24 roundedTick, - uint160 roundedTickPrice - ) { + ) internal pure returns (int24 roundedTick, uint160 roundedTickPrice) { //pool.tickAtPrice -99.5 //pool.tickAtPrice -100 //-105 //-95 - roundedTick = tick / constants.tickSpacing * constants.tickSpacing; - roundedTickPrice = ConstantProduct.getPriceAtTick(roundedTick, constants); - if (price == roundedTickPrice) - return (roundedTick, roundedTickPrice); + roundedTick = (tick / constants.tickSpacing) * constants.tickSpacing; + roundedTickPrice = ConstantProduct.getPriceAtTick( + roundedTick, + constants + ); + if (price == roundedTickPrice) return (roundedTick, roundedTickPrice); if (roundedTick > 0) { roundedTick += constants.tickSpacing / 2; } else if (roundedTick < 0) { if (roundedTickPrice < price) roundedTick += constants.tickSpacing / 2; - else - roundedTick -= constants.tickSpacing / 2; + else roundedTick -= constants.tickSpacing / 2; } else { if (price > roundedTickPrice) { roundedTick += constants.tickSpacing / 2; @@ -430,13 +459,13 @@ library TickMap { PoolsharkStructs.LimitImmutables memory constants, bool zeroForOne, uint256 price - ) internal pure returns ( - int24 roundedTick - ) { - roundedTick = tick / constants.tickSpacing * constants.tickSpacing; - uint160 roundedTickPrice = ConstantProduct.getPriceAtTick(roundedTick, constants); - if (price == roundedTickPrice) - return roundedTick; + ) internal pure returns (int24 roundedTick) { + roundedTick = (tick / constants.tickSpacing) * constants.tickSpacing; + uint160 roundedTickPrice = ConstantProduct.getPriceAtTick( + roundedTick, + constants + ); + if (price == roundedTickPrice) return roundedTick; if (zeroForOne) { // round up if positive if (roundedTick > 0 || (roundedTick == 0 && tick >= 0)) @@ -450,7 +479,7 @@ library TickMap { } else { // round down if negative if (roundedTick < 0 || (roundedTick == 0 && tick < 0)) - /// @dev - strictly less due to TickMath always rounding to lesser values + /// @dev - strictly less due to TickMath always rounding to lesser values roundedTick -= constants.tickSpacing; } } @@ -460,13 +489,13 @@ library TickMap { PoolsharkStructs.LimitImmutables memory constants, bool zeroForOne, uint256 price - ) internal pure returns ( - int24 roundedTick - ) { - roundedTick = tick / constants.tickSpacing * constants.tickSpacing; - uint160 roundedTickPrice = ConstantProduct.getPriceAtTick(roundedTick, constants); - if (price == roundedTickPrice) - return roundedTick; + ) internal pure returns (int24 roundedTick) { + roundedTick = (tick / constants.tickSpacing) * constants.tickSpacing; + uint160 roundedTickPrice = ConstantProduct.getPriceAtTick( + roundedTick, + constants + ); + if (price == roundedTickPrice) return roundedTick; if (zeroForOne) { // round down if negative if (roundedTick < 0 || (roundedTick == 0 && tick < 0)) @@ -483,4 +512,4 @@ library TickMap { } } } -} \ No newline at end of file +} diff --git a/contracts/libraries/Ticks.sol b/contracts/libraries/Ticks.sol index 520a17c9..ad3c8100 100644 --- a/contracts/libraries/Ticks.sol +++ b/contracts/libraries/Ticks.sol @@ -13,7 +13,6 @@ import './limit/EpochMap.sol'; import './limit/LimitTicks.sol'; library Ticks { - using SafeCast for uint256; // cross flags @@ -58,27 +57,43 @@ library Ticks { PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, uint160 startPrice - ) external returns ( - PoolsharkStructs.GlobalState memory - ) - { + ) external returns (PoolsharkStructs.GlobalState memory) { // state should only be initialized once - if (state.pool0.price > 0) require (false, 'PoolAlreadyInitialized()'); + if (state.pool0.price > 0) require(false, 'PoolAlreadyInitialized()'); // initialize state state.epoch = 1; state.positionIdNext = 1; // check price bounds - if (startPrice < constants.bounds.min || startPrice >= constants.bounds.max) require(false, 'StartPriceInvalid()'); + if ( + startPrice < constants.bounds.min || + startPrice >= constants.bounds.max + ) require(false, 'StartPriceInvalid()'); // initialize range ticks - TickMap.set(rangeTickMap, ConstantProduct.minTick(constants.tickSpacing), constants.tickSpacing); - TickMap.set(rangeTickMap, ConstantProduct.maxTick(constants.tickSpacing), constants.tickSpacing); - + TickMap.set( + rangeTickMap, + ConstantProduct.minTick(constants.tickSpacing), + constants.tickSpacing + ); + TickMap.set( + rangeTickMap, + ConstantProduct.maxTick(constants.tickSpacing), + constants.tickSpacing + ); + // initialize limit ticks - TickMap.set(limitTickMap, ConstantProduct.minTick(constants.tickSpacing), constants.tickSpacing); - TickMap.set(limitTickMap, ConstantProduct.maxTick(constants.tickSpacing), constants.tickSpacing); + TickMap.set( + limitTickMap, + ConstantProduct.minTick(constants.tickSpacing), + constants.tickSpacing + ); + TickMap.set( + limitTickMap, + ConstantProduct.maxTick(constants.tickSpacing), + constants.tickSpacing + ); // initialize price state.pool.price = startPrice; @@ -103,7 +118,7 @@ library Ticks { return state; } - + function swap( mapping(int24 => PoolsharkStructs.Tick) storage ticks, RangePoolStructs.Sample[65535] storage samples, @@ -111,15 +126,19 @@ library Ticks { PoolsharkStructs.TickMap storage limitTickMap, PoolsharkStructs.SwapParams memory params, PoolsharkStructs.SwapCache memory cache - ) external returns ( - PoolsharkStructs.SwapCache memory - ) - { + ) external returns (PoolsharkStructs.SwapCache memory) { cache.price = cache.state.pool.price; cache.crossTick = cache.state.pool.tickAtPrice; // set initial cross state - cache = _iterate(ticks, rangeTickMap, limitTickMap, cache, params.zeroForOne, true); + cache = _iterate( + ticks, + rangeTickMap, + limitTickMap, + cache, + params.zeroForOne, + true + ); uint128 startLiquidity = cache.state.pool.liquidity; @@ -130,7 +149,7 @@ library Ticks { price: cache.price, liquidity: cache.liquidity, amountLeft: params.amount, - input: 0, + input: 0, output: 0, crossPrice: cache.crossPrice, secondsPerLiquidityAccum: 0, @@ -149,7 +168,11 @@ library Ticks { cache.averagePrice, cache.secondsPerLiquidityAccum, cache.tickSecondsAccum - ) = Samples.getLatest(cache.state, cache.constants, cache.state.pool.liquidity); + ) = Samples.getLatest( + cache.state, + cache.constants, + cache.state.pool.liquidity + ); // grab latest sample and store in cache for _cross while (cache.cross) { @@ -179,7 +202,10 @@ library Ticks { cache.state.pool.price = cache.price.toUint160(); if (cache.price != cache.crossPrice) { - cache.state.pool.tickAtPrice = ConstantProduct.getTickAtPrice(cache.price.toUint160(), cache.constants); + cache.state.pool.tickAtPrice = ConstantProduct.getTickAtPrice( + cache.price.toUint160(), + cache.constants + ); } else { cache.state.pool.tickAtPrice = cache.crossTick; } @@ -214,17 +240,28 @@ library Ticks { PoolsharkStructs.TickMap storage limitTickMap, PoolsharkStructs.QuoteParams memory params, PoolsharkStructs.SwapCache memory cache - ) internal view returns ( - uint256, - uint256, - uint160 - ) { + ) + internal + view + returns ( + uint256, + uint256, + uint160 + ) + { // start with range price cache.price = cache.state.pool.price; cache.crossTick = cache.state.pool.tickAtPrice; - cache = _iterate(ticks, rangeTickMap, limitTickMap, cache, params.zeroForOne, true); - + cache = _iterate( + ticks, + rangeTickMap, + limitTickMap, + cache, + params.zeroForOne, + true + ); + // set crossTick/crossPrice based on the best between limit and range // grab sample for accumulators cache = PoolsharkStructs.SwapCache({ @@ -233,7 +270,7 @@ library Ticks { price: cache.price, liquidity: cache.liquidity, amountLeft: params.amount, - input: 0, + input: 0, output: 0, crossPrice: cache.crossPrice, secondsPerLiquidityAccum: 0, @@ -252,52 +289,60 @@ library Ticks { cache.averagePrice, cache.secondsPerLiquidityAccum, cache.tickSecondsAccum - ) = Samples.getLatest(cache.state, cache.constants, cache.state.pool.liquidity); + ) = Samples.getLatest( + cache.state, + cache.constants, + cache.state.pool.liquidity + ); while (cache.cross) { cache = _quoteSingle(cache, params.priceLimit, params.zeroForOne); if (cache.cross) { - cache = _pass( - ticks, - rangeTickMap, - limitTickMap, - cache, - params - ); + cache = _pass(ticks, rangeTickMap, limitTickMap, cache, params); } } - return ( - cache.input, - cache.output, - cache.price.toUint160() - ); + return (cache.input, cache.output, cache.price.toUint160()); } function _quoteSingle( PoolsharkStructs.SwapCache memory cache, uint160 priceLimit, bool zeroForOne - ) internal view returns ( - PoolsharkStructs.SwapCache memory - ) { - if ((zeroForOne ? priceLimit >= cache.price - : priceLimit <= cache.price) || + ) internal view returns (PoolsharkStructs.SwapCache memory) { + if ( + ( + zeroForOne + ? priceLimit >= cache.price + : priceLimit <= cache.price + ) || (zeroForOne && cache.price == cache.constants.bounds.min) || (!zeroForOne && cache.price == cache.constants.bounds.max) || - (cache.amountLeft == 0 && cache.liquidity > 0)) - { + (cache.amountLeft == 0 && cache.liquidity > 0) + ) { cache.cross = false; return cache; } uint256 nextPrice = cache.crossPrice; - uint256 amountIn; uint256 amountOut; + uint256 amountIn; + uint256 amountOut; if (zeroForOne) { // Trading token 0 (x) for token 1 (y). // price is decreasing. if (nextPrice < priceLimit) { nextPrice = priceLimit; } - uint256 amountMax = cache.exactIn ? ConstantProduct.getDx(cache.liquidity, nextPrice, cache.price, true) - : ConstantProduct.getDy(cache.liquidity, nextPrice, cache.price, false); + uint256 amountMax = cache.exactIn + ? ConstantProduct.getDx( + cache.liquidity, + nextPrice, + cache.price, + true + ) + : ConstantProduct.getDy( + cache.liquidity, + nextPrice, + cache.price, + false + ); if (cache.amountLeft < amountMax) { // calculate price after swap uint256 newPrice = ConstantProduct.getNewPrice( @@ -309,9 +354,19 @@ library Ticks { ); if (cache.exactIn) { amountIn = cache.amountLeft; - amountOut = ConstantProduct.getDy(cache.liquidity, newPrice, cache.price, false); + amountOut = ConstantProduct.getDy( + cache.liquidity, + newPrice, + cache.price, + false + ); } else { - amountIn = ConstantProduct.getDx(cache.liquidity, newPrice, cache.price, true); + amountIn = ConstantProduct.getDx( + cache.liquidity, + newPrice, + cache.price, + true + ); amountOut = cache.amountLeft; } cache.amountLeft = 0; @@ -320,10 +375,19 @@ library Ticks { } else { if (cache.exactIn) { amountIn = amountMax; - amountOut = ConstantProduct.getDy(cache.liquidity, nextPrice, cache.price, false); - + amountOut = ConstantProduct.getDy( + cache.liquidity, + nextPrice, + cache.price, + false + ); } else { - amountIn = ConstantProduct.getDx(cache.liquidity, nextPrice, cache.price, true); + amountIn = ConstantProduct.getDx( + cache.liquidity, + nextPrice, + cache.price, + true + ); amountOut = amountMax; } cache.amountLeft -= amountMax; @@ -336,8 +400,19 @@ library Ticks { if (nextPrice > priceLimit) { nextPrice = priceLimit; } - uint256 amountMax = cache.exactIn ? ConstantProduct.getDy(cache.liquidity, cache.price, nextPrice, true) - : ConstantProduct.getDx(cache.liquidity, cache.price, nextPrice, false); + uint256 amountMax = cache.exactIn + ? ConstantProduct.getDy( + cache.liquidity, + cache.price, + nextPrice, + true + ) + : ConstantProduct.getDx( + cache.liquidity, + cache.price, + nextPrice, + false + ); if (cache.amountLeft < amountMax) { uint256 newPrice = ConstantProduct.getNewPrice( cache.price, @@ -348,9 +423,19 @@ library Ticks { ); if (cache.exactIn) { amountIn = cache.amountLeft; - amountOut = ConstantProduct.getDx(cache.liquidity, cache.price, newPrice, false); + amountOut = ConstantProduct.getDx( + cache.liquidity, + cache.price, + newPrice, + false + ); } else { - amountIn = ConstantProduct.getDy(cache.liquidity, cache.price, newPrice, true); + amountIn = ConstantProduct.getDy( + cache.liquidity, + cache.price, + newPrice, + true + ); amountOut = cache.amountLeft; } cache.amountLeft = 0; @@ -359,9 +444,19 @@ library Ticks { } else { if (cache.exactIn) { amountIn = amountMax; - amountOut = ConstantProduct.getDx(cache.liquidity, cache.price, nextPrice, false); + amountOut = ConstantProduct.getDx( + cache.liquidity, + cache.price, + nextPrice, + false + ); } else { - amountIn = ConstantProduct.getDy(cache.liquidity, cache.price, nextPrice, true); + amountIn = ConstantProduct.getDy( + cache.liquidity, + cache.price, + nextPrice, + true + ); amountOut = amountMax; } cache.amountLeft -= amountMax; @@ -380,18 +475,29 @@ library Ticks { PoolsharkStructs.TickMap storage limitTickMap, PoolsharkStructs.SwapCache memory cache, PoolsharkStructs.SwapParams memory params - ) internal returns ( - PoolsharkStructs.SwapCache memory - ) { + ) internal returns (PoolsharkStructs.SwapCache memory) { // crossing range ticks if ((cache.crossStatus & RANGE_TICK) > 0) { // skip if crossing down and stopping at crossPrice - if (!params.zeroForOne || (cache.amountLeft > 0 && params.priceLimit < cache.crossPrice)) { - PoolsharkStructs.RangeTick memory crossTick = ticks[cache.crossTick].range; - crossTick.feeGrowthOutside0 = cache.state.pool.feeGrowthGlobal0 - crossTick.feeGrowthOutside0; - crossTick.feeGrowthOutside1 = cache.state.pool.feeGrowthGlobal1 - crossTick.feeGrowthOutside1; - crossTick.tickSecondsAccumOutside = cache.tickSecondsAccum - crossTick.tickSecondsAccumOutside; - crossTick.secondsPerLiquidityAccumOutside = cache.secondsPerLiquidityAccum - crossTick.secondsPerLiquidityAccumOutside; + if ( + !params.zeroForOne || + (cache.amountLeft > 0 && params.priceLimit < cache.crossPrice) + ) { + PoolsharkStructs.RangeTick memory crossTick = ticks[ + cache.crossTick + ].range; + crossTick.feeGrowthOutside0 = + cache.state.pool.feeGrowthGlobal0 - + crossTick.feeGrowthOutside0; + crossTick.feeGrowthOutside1 = + cache.state.pool.feeGrowthGlobal1 - + crossTick.feeGrowthOutside1; + crossTick.tickSecondsAccumOutside = + cache.tickSecondsAccum - + crossTick.tickSecondsAccumOutside; + crossTick.secondsPerLiquidityAccumOutside = + cache.secondsPerLiquidityAccum - + crossTick.secondsPerLiquidityAccumOutside; ticks[cache.crossTick].range = crossTick; int128 liquidityDelta = crossTick.liquidityDelta; emit SyncRangeTick( @@ -401,22 +507,30 @@ library Ticks { ); if (params.zeroForOne) { unchecked { - if (liquidityDelta >= 0){ + if (liquidityDelta >= 0) { cache.liquidity -= uint128(liquidityDelta); - cache.state.pool.liquidity -= uint128(liquidityDelta); + cache.state.pool.liquidity -= uint128( + liquidityDelta + ); } else { cache.liquidity += uint128(-liquidityDelta); - cache.state.pool.liquidity += uint128(-liquidityDelta); + cache.state.pool.liquidity += uint128( + -liquidityDelta + ); } } } else { unchecked { if (liquidityDelta >= 0) { cache.liquidity += uint128(liquidityDelta); - cache.state.pool.liquidity += uint128(liquidityDelta); + cache.state.pool.liquidity += uint128( + liquidityDelta + ); } else { cache.liquidity -= uint128(-liquidityDelta); - cache.state.pool.liquidity -= uint128(-liquidityDelta); + cache.state.pool.liquidity -= uint128( + -liquidityDelta + ); } } } @@ -428,32 +542,52 @@ library Ticks { // crossing limit tick if ((cache.crossStatus & LIMIT_TICK) > 0) { // cross limit tick - EpochMap.set(cache.crossTick, !params.zeroForOne, cache.state.epoch, limitTickMap, cache.constants); + EpochMap.set( + cache.crossTick, + !params.zeroForOne, + cache.state.epoch, + limitTickMap, + cache.constants + ); int128 liquidityDelta = ticks[cache.crossTick].limit.liquidityDelta; if (liquidityDelta >= 0) { cache.liquidity += uint128(liquidityDelta); - if (params.zeroForOne) cache.state.pool1.liquidity += uint128(liquidityDelta); + if (params.zeroForOne) + cache.state.pool1.liquidity += uint128(liquidityDelta); else cache.state.pool0.liquidity += uint128(liquidityDelta); - } - else { + } else { cache.liquidity -= uint128(-liquidityDelta); - if (params.zeroForOne) cache.state.pool1.liquidity -= uint128(-liquidityDelta); + if (params.zeroForOne) + cache.state.pool1.liquidity -= uint128(-liquidityDelta); else cache.state.pool0.liquidity -= uint128(-liquidityDelta); } // zero out liquidityDelta and priceAt - ticks[cache.crossTick].limit = PoolsharkStructs.LimitTick(0,0,0); - LimitTicks.clear(ticks, cache.constants, limitTickMap, cache.crossTick); + ticks[cache.crossTick].limit = PoolsharkStructs.LimitTick(0, 0, 0); + LimitTicks.clear( + ticks, + cache.constants, + limitTickMap, + cache.crossTick + ); /// @dev - price and tickAtPrice updated at end of loop } if ((cache.crossStatus & LIMIT_POOL) > 0) { // add one-way liquidity - uint128 liquidityDelta = params.zeroForOne ? cache.state.pool1.liquidity - : cache.state.pool0.liquidity; + uint128 liquidityDelta = params.zeroForOne + ? cache.state.pool1.liquidity + : cache.state.pool0.liquidity; if (liquidityDelta > 0) cache.liquidity += liquidityDelta; } if (cache.cross) - cache = _iterate(ticks, rangeTickMap, limitTickMap, cache, params.zeroForOne, false); + cache = _iterate( + ticks, + rangeTickMap, + limitTickMap, + cache, + params.zeroForOne, + false + ); return cache; } @@ -464,26 +598,37 @@ library Ticks { PoolsharkStructs.TickMap storage limitTickMap, PoolsharkStructs.SwapCache memory cache, PoolsharkStructs.QuoteParams memory params - ) internal view returns ( - PoolsharkStructs.SwapCache memory - ) { + ) internal view returns (PoolsharkStructs.SwapCache memory) { if ((cache.crossStatus & RANGE_TICK) > 0) { - if (!params.zeroForOne || (cache.amountLeft > 0 && params.priceLimit < cache.crossPrice)) { - int128 liquidityDelta = ticks[cache.crossTick].range.liquidityDelta; + if ( + !params.zeroForOne || + (cache.amountLeft > 0 && params.priceLimit < cache.crossPrice) + ) { + int128 liquidityDelta = ticks[cache.crossTick] + .range + .liquidityDelta; if (params.zeroForOne) { unchecked { - if (liquidityDelta >= 0){ - cache.state.pool.liquidity -= uint128(liquidityDelta); + if (liquidityDelta >= 0) { + cache.state.pool.liquidity -= uint128( + liquidityDelta + ); } else { - cache.state.pool.liquidity += uint128(-liquidityDelta); + cache.state.pool.liquidity += uint128( + -liquidityDelta + ); } } } else { unchecked { if (liquidityDelta >= 0) { - cache.state.pool.liquidity += uint128(liquidityDelta); + cache.state.pool.liquidity += uint128( + liquidityDelta + ); } else { - cache.state.pool.liquidity -= uint128(-liquidityDelta); + cache.state.pool.liquidity -= uint128( + -liquidityDelta + ); } } } @@ -497,10 +642,10 @@ library Ticks { if (liquidityDelta > 0) { cache.liquidity += uint128(liquidityDelta); - if (params.zeroForOne) cache.state.pool1.liquidity += uint128(liquidityDelta); + if (params.zeroForOne) + cache.state.pool1.liquidity += uint128(liquidityDelta); else cache.state.pool0.liquidity += uint128(liquidityDelta); - } - else { + } else { cache.liquidity -= uint128(-liquidityDelta); if (params.zeroForOne) { cache.state.pool1.liquidity -= uint128(-liquidityDelta); @@ -511,8 +656,9 @@ library Ticks { } if ((cache.crossStatus & LIMIT_POOL) > 0) { // add limit pool - uint128 liquidityDelta = params.zeroForOne ? cache.state.pool1.liquidity - : cache.state.pool0.liquidity; + uint128 liquidityDelta = params.zeroForOne + ? cache.state.pool1.liquidity + : cache.state.pool0.liquidity; if (liquidityDelta > 0) { cache.liquidity += liquidityDelta; @@ -520,7 +666,14 @@ library Ticks { } if (cache.cross) - cache = _iterate(ticks, rangeTickMap, limitTickMap, cache, params.zeroForOne, false); + cache = _iterate( + ticks, + rangeTickMap, + limitTickMap, + cache, + params.zeroForOne, + false + ); return cache; } @@ -532,19 +685,28 @@ library Ticks { PoolsharkStructs.SwapCache memory cache, bool zeroForOne, bool inclusive - ) internal view returns ( - PoolsharkStructs.SwapCache memory - ) - { + ) internal view returns (PoolsharkStructs.SwapCache memory) { if (zeroForOne) { if (cache.price > cache.state.pool1.price) { // load range pool cache.limitActive = false; cache.liquidity = cache.state.pool.liquidity; - (cache.crossTick,) = TickMap.roundHalf(cache.crossTick, cache.constants, cache.price); + (cache.crossTick, ) = TickMap.roundHalf( + cache.crossTick, + cache.constants, + cache.price + ); // next range tick vs. limit pool price - cache.crossTick = TickMap.previous(rangeTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); - cache.crossPrice = ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants); + cache.crossTick = TickMap.previous( + rangeTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); + cache.crossPrice = ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ); if (cache.state.pool1.price >= cache.crossPrice) { // cross into limit pool cache.crossStatus = LIMIT_POOL; @@ -555,29 +717,53 @@ library Ticks { cache.crossTick = cache.state.pool1.tickAtPrice; cache.crossPrice = cache.state.pool1.price; } - } - else { + } else { // cross only range tick cache.crossStatus = RANGE_TICK; } } else { // load range and limit pools cache.limitActive = true; - cache.liquidity = cache.state.pool.liquidity + cache.state.pool1.liquidity; - (cache.crossTick,) = TickMap.roundHalf(cache.crossTick, cache.constants, cache.price); - int24 rangeTickAhead; int24 limitTickAhead; - if (cache.crossStatus == LIMIT_POOL && - cache.crossTick % cache.constants.tickSpacing != 0 && - TickMap.get(limitTickMap, cache.crossTick, cache.constants.tickSpacing)) - { + cache.liquidity = + cache.state.pool.liquidity + + cache.state.pool1.liquidity; + (cache.crossTick, ) = TickMap.roundHalf( + cache.crossTick, + cache.constants, + cache.price + ); + int24 rangeTickAhead; + int24 limitTickAhead; + if ( + cache.crossStatus == LIMIT_POOL && + cache.crossTick % cache.constants.tickSpacing != 0 && + TickMap.get( + limitTickMap, + cache.crossTick, + cache.constants.tickSpacing + ) + ) { limitTickAhead = cache.crossTick; - rangeTickAhead = cache.crossTick - cache.constants.tickSpacing / 2; + rangeTickAhead = + cache.crossTick - + cache.constants.tickSpacing / + 2; } else { - rangeTickAhead = TickMap.previous(rangeTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); - limitTickAhead = TickMap.previous(limitTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); + rangeTickAhead = TickMap.previous( + rangeTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); + limitTickAhead = TickMap.previous( + limitTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); } // next range tick vs. next limit tick - + if (rangeTickAhead >= limitTickAhead) { cache.crossTick = rangeTickAhead; // cross range tick @@ -585,13 +771,20 @@ library Ticks { if (rangeTickAhead == limitTickAhead) // also cross limit tick cache.crossStatus |= LIMIT_TICK; - cache.crossPrice = ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants); + cache.crossPrice = ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ); } else { // only cross limit tick cache.crossTick = limitTickAhead; cache.crossStatus = LIMIT_TICK; - cache.crossPrice = ticks[cache.crossTick].limit.priceAt == 0 ? ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants) - : ticks[cache.crossTick].limit.priceAt; + cache.crossPrice = ticks[cache.crossTick].limit.priceAt == 0 + ? ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ) + : ticks[cache.crossTick].limit.priceAt; } } } else { @@ -599,10 +792,22 @@ library Ticks { // load range pool cache.limitActive = false; cache.liquidity = cache.state.pool.liquidity; - (cache.crossTick,) = TickMap.roundHalf(cache.crossTick, cache.constants, cache.price); + (cache.crossTick, ) = TickMap.roundHalf( + cache.crossTick, + cache.constants, + cache.price + ); // next range tick vs. limit pool price - cache.crossTick = TickMap.next(rangeTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); - cache.crossPrice = ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants); + cache.crossTick = TickMap.next( + rangeTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); + cache.crossPrice = ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ); if (cache.state.pool0.price <= cache.crossPrice) { // cross into limit pool cache.crossStatus = LIMIT_POOL; @@ -613,27 +818,51 @@ library Ticks { cache.crossTick = cache.state.pool0.tickAtPrice; cache.crossPrice = cache.state.pool0.price; } - } - else { + } else { // cross only range tick cache.crossStatus = RANGE_TICK; } } else { // load range and limit pools cache.limitActive = true; - cache.liquidity = cache.state.pool.liquidity + cache.state.pool0.liquidity; - (cache.crossTick,) = TickMap.roundHalf(cache.crossTick, cache.constants, cache.price); + cache.liquidity = + cache.state.pool.liquidity + + cache.state.pool0.liquidity; + (cache.crossTick, ) = TickMap.roundHalf( + cache.crossTick, + cache.constants, + cache.price + ); // next range tick vs. next limit tick - int24 rangeTickAhead; int24 limitTickAhead; - if (cache.crossStatus == LIMIT_POOL && - cache.crossTick % cache.constants.tickSpacing != 0 && - TickMap.get(limitTickMap, cache.crossTick, cache.constants.tickSpacing)) - { + int24 rangeTickAhead; + int24 limitTickAhead; + if ( + cache.crossStatus == LIMIT_POOL && + cache.crossTick % cache.constants.tickSpacing != 0 && + TickMap.get( + limitTickMap, + cache.crossTick, + cache.constants.tickSpacing + ) + ) { limitTickAhead = cache.crossTick; - rangeTickAhead = cache.crossTick + cache.constants.tickSpacing / 2; + rangeTickAhead = + cache.crossTick + + cache.constants.tickSpacing / + 2; } else { - rangeTickAhead = TickMap.next(rangeTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); - limitTickAhead = TickMap.next(limitTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); + rangeTickAhead = TickMap.next( + rangeTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); + limitTickAhead = TickMap.next( + limitTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); } if (rangeTickAhead <= limitTickAhead) { cache.crossTick = rangeTickAhead; @@ -642,16 +871,23 @@ library Ticks { if (rangeTickAhead == limitTickAhead) // also cross limit tick cache.crossStatus |= LIMIT_TICK; - cache.crossPrice = ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants); + cache.crossPrice = ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ); } else { // only cross limit tick cache.crossTick = limitTickAhead; cache.crossStatus |= LIMIT_TICK; - cache.crossPrice = ticks[cache.crossTick].limit.priceAt == 0 ? ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants) - : ticks[cache.crossTick].limit.priceAt; + cache.crossPrice = ticks[cache.crossTick].limit.priceAt == 0 + ? ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ) + : ticks[cache.crossTick].limit.priceAt; } } } return cache; } -} \ No newline at end of file +} diff --git a/contracts/libraries/limit/Claims.sol b/contracts/libraries/limit/Claims.sol index 1621e203..461376ae 100644 --- a/contracts/libraries/limit/Claims.sol +++ b/contracts/libraries/limit/Claims.sol @@ -8,7 +8,6 @@ import '../utils/String.sol'; import '../utils/SafeCast.sol'; library Claims { - using SafeCast for uint256; // if claim tick searched, look max 512 spacings ahead @@ -19,24 +18,39 @@ library Claims { PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache - ) internal view returns ( - PoolsharkStructs.BurnLimitParams memory, - LimitPoolStructs.BurnLimitCache memory - ) { - if (params.claim < cache.position.lower || - params.claim > cache.position.upper) - require (false, 'ClaimTick::OutsidePositionBounds()'); - + ) + internal + view + returns ( + PoolsharkStructs.BurnLimitParams memory, + LimitPoolStructs.BurnLimitCache memory + ) + { + if ( + params.claim < cache.position.lower || + params.claim > cache.position.upper + ) require(false, 'ClaimTick::OutsidePositionBounds()'); + if (params.claim % (cache.constants.tickSpacing / 2) != 0) - require (false, 'ClaimTick::NotHalfTickOrFullTick()'); + require(false, 'ClaimTick::NotHalfTickOrFullTick()'); - uint32 claimTickEpoch = EpochMap.get(params.claim, params.zeroForOne, tickMap, cache.constants); + uint32 claimTickEpoch = EpochMap.get( + params.claim, + params.zeroForOne, + tickMap, + cache.constants + ); - if (params.zeroForOne){ + if (params.zeroForOne) { if (cache.pool.price >= cache.priceClaim) { if (cache.pool.price <= cache.priceUpper) { cache.priceClaim = cache.pool.price; - params.claim = TickMap.roundBack(cache.pool.tickAtPrice, cache.constants, params.zeroForOne, cache.priceClaim); + params.claim = TickMap.roundBack( + cache.pool.tickAtPrice, + cache.constants, + params.zeroForOne, + cache.priceClaim + ); } else { cache.priceClaim = cache.priceUpper; params.claim = cache.position.upper; @@ -47,10 +61,9 @@ library Claims { if (cache.claimTick.priceAt == 0) { // if tick untouched since position creation revert if (claimTickEpoch <= cache.position.epochLast) - require (false, 'ClaimTick::HalfTickClaimInvalid()'); - else + require(false, 'ClaimTick::HalfTickClaimInvalid()'); // search ahead for the correct claim tick - cache.search = true; + else cache.search = true; } cache.priceClaim = cache.claimTick.priceAt; } @@ -58,7 +71,12 @@ library Claims { if (cache.pool.price <= cache.priceClaim) { if (cache.pool.price >= cache.priceLower) { cache.priceClaim = cache.pool.price; - params.claim = TickMap.roundBack(cache.pool.tickAtPrice, cache.constants, params.zeroForOne, cache.priceClaim); + params.claim = TickMap.roundBack( + cache.pool.tickAtPrice, + cache.constants, + params.zeroForOne, + cache.priceClaim + ); } else { cache.priceClaim = cache.priceLower; params.claim = cache.position.lower; @@ -68,26 +86,33 @@ library Claims { } else if (params.claim % cache.constants.tickSpacing != 0) { if (cache.claimTick.priceAt == 0) { if (claimTickEpoch <= cache.position.epochLast) - require (false, 'ClaimTick::HalfTickClaimInvalid()'); - else + require(false, 'ClaimTick::HalfTickClaimInvalid()'); // search ahead for the correct claim tick - cache.search = true; + else cache.search = true; } cache.priceClaim = cache.claimTick.priceAt; } } - if (params.claim == (params.zeroForOne ? cache.position.upper : cache.position.lower)) { + if ( + params.claim == + (params.zeroForOne ? cache.position.upper : cache.position.lower) + ) { // check if final tick crossed cache.liquidityBurned = 0; if (claimTickEpoch <= cache.position.epochLast) // nothing to search - require (false, 'ClaimTick::FinalTickNotCrossedYet()'); + require(false, 'ClaimTick::FinalTickNotCrossedYet()'); } else if (cache.liquidityBurned > 0) { /// @dev - partway claim is valid as long as liquidity is not being removed if (params.zeroForOne) { // check final tick first - uint32 endTickEpoch = EpochMap.get(cache.position.upper, params.zeroForOne, tickMap, cache.constants); + uint32 endTickEpoch = EpochMap.get( + cache.position.upper, + params.zeroForOne, + tickMap, + cache.constants + ); if (endTickEpoch > cache.position.epochLast) { // final tick crossed params.claim = cache.position.upper; @@ -96,8 +121,18 @@ library Claims { cache.liquidityBurned = 0; } else { // check claim tick passed is valid - int24 claimTickNext = TickMap.next(tickMap, params.claim, cache.constants.tickSpacing, false); - uint32 claimTickNextEpoch = EpochMap.get(claimTickNext, params.zeroForOne, tickMap, cache.constants); + int24 claimTickNext = TickMap.next( + tickMap, + params.claim, + cache.constants.tickSpacing, + false + ); + uint32 claimTickNextEpoch = EpochMap.get( + claimTickNext, + params.zeroForOne, + tickMap, + cache.constants + ); if (claimTickNextEpoch > cache.position.epochLast) { ///@dev - next tick in range should not have been crossed // require (false, 'ClaimTick::NextTickAlreadyCrossed()'); @@ -106,7 +141,12 @@ library Claims { } } else { // check final tick first - uint32 endTickEpoch = EpochMap.get(cache.position.lower, params.zeroForOne, tickMap, cache.constants); + uint32 endTickEpoch = EpochMap.get( + cache.position.lower, + params.zeroForOne, + tickMap, + cache.constants + ); if (endTickEpoch > cache.position.epochLast) { // final tick crossed params.claim = cache.position.lower; @@ -115,8 +155,18 @@ library Claims { cache.liquidityBurned = 0; } else { // check claim tick passed is valid - int24 claimTickNext = TickMap.previous(tickMap, params.claim, cache.constants.tickSpacing, false); - uint32 claimTickNextEpoch = EpochMap.get(claimTickNext, params.zeroForOne, tickMap, cache.constants); + int24 claimTickNext = TickMap.previous( + tickMap, + params.claim, + cache.constants.tickSpacing, + false + ); + uint32 claimTickNextEpoch = EpochMap.get( + claimTickNext, + params.zeroForOne, + tickMap, + cache.constants + ); if (claimTickNextEpoch > cache.position.epochLast) { ///@dev - next tick in range should not have been crossed // require (false, 'ClaimTick::NextTickAlreadyCrossed()'); @@ -127,14 +177,22 @@ library Claims { } if (cache.search) { - (params, cache, claimTickEpoch) = search(ticks, tickMap, params, cache); + (params, cache, claimTickEpoch) = search( + ticks, + tickMap, + params, + cache + ); } /// @dev - start tick does not overwrite position and final tick clears position - if (params.claim != cache.position.upper && params.claim != cache.position.lower) { + if ( + params.claim != cache.position.upper && + params.claim != cache.position.lower + ) { // check epochLast on claim tick if (claimTickEpoch <= cache.position.epochLast) - require (false, 'ClaimTick::TickNotCrossed()'); + require(false, 'ClaimTick::TickNotCrossed()'); } return (params, cache); @@ -144,9 +202,7 @@ library Claims { PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache, PoolsharkStructs.LimitImmutables memory constants - ) internal pure returns ( - LimitPoolStructs.BurnLimitCache memory - ) { + ) internal pure returns (LimitPoolStructs.BurnLimitCache memory) { // if half tick priceAt > 0 add amountOut to amountOutClaimed // set claimPriceLast if zero if (!cache.position.crossedInto) { @@ -156,35 +212,95 @@ library Claims { if (params.claim % constants.tickSpacing != 0) // this should pass price at the claim tick - locals.previousFullTick = TickMap.roundBack(params.claim, constants, params.zeroForOne, ConstantProduct.getPriceAtTick(params.claim, constants)); - else - locals.previousFullTick = params.claim; - locals.pricePrevious = ConstantProduct.getPriceAtTick(locals.previousFullTick, constants); - if (params.zeroForOne ? locals.previousFullTick > cache.position.lower - : locals.previousFullTick < cache.position.upper) { - + locals.previousFullTick = TickMap.roundBack( + params.claim, + constants, + params.zeroForOne, + ConstantProduct.getPriceAtTick(params.claim, constants) + ); + else locals.previousFullTick = params.claim; + locals.pricePrevious = ConstantProduct.getPriceAtTick( + locals.previousFullTick, + constants + ); + if ( + params.zeroForOne + ? locals.previousFullTick > cache.position.lower + : locals.previousFullTick < cache.position.upper + ) { // claim amounts up to latest full tick crossed - cache.amountIn += uint128(params.zeroForOne ? ConstantProduct.getDy(cache.position.liquidity, cache.priceLower, locals.pricePrevious, false) - : ConstantProduct.getDx(cache.position.liquidity, locals.pricePrevious, cache.priceUpper, false)); + cache.amountIn += uint128( + params.zeroForOne + ? ConstantProduct.getDy( + cache.position.liquidity, + cache.priceLower, + locals.pricePrevious, + false + ) + : ConstantProduct.getDx( + cache.position.liquidity, + locals.pricePrevious, + cache.priceUpper, + false + ) + ); } if (cache.liquidityBurned > 0) { - // if tick hasn't been set back calculate amountIn - if (params.zeroForOne ? cache.priceClaim > locals.pricePrevious - : cache.priceClaim < locals.pricePrevious) { + // if tick hasn't been set back calculate amountIn + if ( + params.zeroForOne + ? cache.priceClaim > locals.pricePrevious + : cache.priceClaim < locals.pricePrevious + ) { // allow partial tick claim if removing liquidity - cache.amountIn += uint128(params.zeroForOne ? ConstantProduct.getDy(cache.liquidityBurned, locals.pricePrevious, cache.priceClaim, false) - : ConstantProduct.getDx(cache.liquidityBurned, cache.priceClaim, locals.pricePrevious, false)); + cache.amountIn += uint128( + params.zeroForOne + ? ConstantProduct.getDy( + cache.liquidityBurned, + locals.pricePrevious, + cache.priceClaim, + false + ) + : ConstantProduct.getDx( + cache.liquidityBurned, + cache.priceClaim, + locals.pricePrevious, + false + ) + ); } // use priceClaim if tick hasn't been set back // else use claimPriceLast to calculate amountOut - if (params.claim != (params.zeroForOne ? cache.position.upper : cache.position.lower)) { - cache.amountOut += uint128(params.zeroForOne ? ConstantProduct.getDx(cache.liquidityBurned, cache.priceClaim, cache.priceUpper, false) - : ConstantProduct.getDy(cache.liquidityBurned, cache.priceLower, cache.priceClaim, false)); + if ( + params.claim != + ( + params.zeroForOne + ? cache.position.upper + : cache.position.lower + ) + ) { + cache.amountOut += uint128( + params.zeroForOne + ? ConstantProduct.getDx( + cache.liquidityBurned, + cache.priceClaim, + cache.priceUpper, + false + ) + : ConstantProduct.getDy( + cache.liquidityBurned, + cache.priceLower, + cache.priceClaim, + false + ) + ); } } // take protocol fee if needed if (cache.pool.protocolFillFee > 0 && cache.amountIn > 0) { - uint128 protocolFeeAmount = OverflowMath.mulDiv(cache.amountIn, cache.pool.protocolFillFee, 1e4).toUint128(); + uint128 protocolFeeAmount = OverflowMath + .mulDiv(cache.amountIn, cache.pool.protocolFillFee, 1e4) + .toUint128(); cache.amountIn -= protocolFeeAmount; cache.pool.protocolFees += protocolFeeAmount; } @@ -196,18 +312,26 @@ library Claims { PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache - ) internal view returns ( - PoolsharkStructs.BurnLimitParams memory, - LimitPoolStructs.BurnLimitCache memory, - uint32 claimTickEpoch - ) { + ) + internal + view + returns ( + PoolsharkStructs.BurnLimitParams memory, + LimitPoolStructs.BurnLimitCache memory, + uint32 claimTickEpoch + ) + { LimitPoolStructs.SearchLocals memory locals; locals.ticksFound = new int24[](256); locals.searchTick = params.claim; if (params.zeroForOne) { - for (uint i=0; i < maxWordsSearched;) { - (locals.ticksFound, locals.ticksIncluded, locals.searchTick) = TickMap.nextTicksWithinWord( + for (uint256 i = 0; i < maxWordsSearched; ) { + ( + locals.ticksFound, + locals.ticksIncluded, + locals.searchTick + ) = TickMap.nextTicksWithinWord( tickMap, locals.searchTick, cache.constants.tickSpacing, @@ -216,15 +340,24 @@ library Claims { locals.ticksIncluded ); // add start of next word if tick exists and is within range - if (locals.searchTick < cache.position.upper && TickMap.get(tickMap, locals.searchTick, cache.constants.tickSpacing)) { + if ( + locals.searchTick < cache.position.upper && + TickMap.get( + tickMap, + locals.searchTick, + cache.constants.tickSpacing + ) + ) { locals.ticksFound[locals.ticksIncluded] = locals.searchTick; unchecked { ++locals.ticksIncluded; } } // if we reached the final tick break the loop - if (locals.ticksIncluded > 0 && - locals.searchTick >= cache.position.upper) { + if ( + locals.ticksIncluded > 0 && + locals.searchTick >= cache.position.upper + ) { break; } unchecked { @@ -232,8 +365,12 @@ library Claims { } } } else { - for (int i=0; i<2;) { - (locals.ticksFound, locals.ticksIncluded, locals.searchTick) = TickMap.previousTicksWithinWord( + for (int256 i = 0; i < 2; ) { + ( + locals.ticksFound, + locals.ticksIncluded, + locals.searchTick + ) = TickMap.previousTicksWithinWord( tickMap, locals.searchTick, cache.constants.tickSpacing, @@ -242,49 +379,84 @@ library Claims { locals.ticksIncluded ); // add start of next word if tick exists and is within range - if (locals.searchTick > cache.position.lower && TickMap.get(tickMap, locals.searchTick, cache.constants.tickSpacing)) { + if ( + locals.searchTick > cache.position.lower && + TickMap.get( + tickMap, + locals.searchTick, + cache.constants.tickSpacing + ) + ) { locals.ticksFound[locals.ticksIncluded] = locals.searchTick; unchecked { ++locals.ticksIncluded; } } // if we reached the final tick break the loop - if (locals.ticksIncluded > 0 && - locals.searchTick <= cache.position.lower) { + if ( + locals.ticksIncluded > 0 && + locals.searchTick <= cache.position.lower + ) { break; } unchecked { ++i; } - } + } } // set initial endIdx if (locals.ticksIncluded > 0) { locals.endIdx = locals.ticksIncluded - 1; } else { - require(false, "ClaimTick::NoTicksFoundViaSearch()"); + require(false, 'ClaimTick::NoTicksFoundViaSearch()'); } while (locals.startIdx <= locals.endIdx) { // set idx at middle of start & end - locals.searchIdx = (locals.endIdx - locals.startIdx) / 2 + locals.startIdx; - + locals.searchIdx = + (locals.endIdx - locals.startIdx) / + 2 + + locals.startIdx; + // set ticks locals.searchTick = locals.ticksFound[locals.searchIdx]; if (locals.searchIdx + 1 < locals.ticksIncluded) { // tick ahead in array - locals.searchTickAhead = locals.ticksFound[locals.searchIdx + 1]; + locals.searchTickAhead = locals.ticksFound[ + locals.searchIdx + 1 + ]; } else { // tick ahead in storage - locals.searchTickAhead = params.zeroForOne ? TickMap.next(tickMap, locals.searchTick, cache.constants.tickSpacing, false) - : TickMap.previous(tickMap, locals.searchTick, cache.constants.tickSpacing, false); + locals.searchTickAhead = params.zeroForOne + ? TickMap.next( + tickMap, + locals.searchTick, + cache.constants.tickSpacing, + false + ) + : TickMap.previous( + tickMap, + locals.searchTick, + cache.constants.tickSpacing, + false + ); } // set epochs - locals.claimTickEpoch = EpochMap.get(locals.searchTick, params.zeroForOne, tickMap, cache.constants); - locals.claimTickAheadEpoch = EpochMap.get(locals.searchTickAhead, params.zeroForOne, tickMap, cache.constants); - + locals.claimTickEpoch = EpochMap.get( + locals.searchTick, + params.zeroForOne, + tickMap, + cache.constants + ); + locals.claimTickAheadEpoch = EpochMap.get( + locals.searchTickAhead, + params.zeroForOne, + tickMap, + cache.constants + ); + // check epochs if (locals.claimTickEpoch > cache.position.epochLast) { if (locals.claimTickAheadEpoch <= cache.position.epochLast) { @@ -304,22 +476,31 @@ library Claims { } // final check on valid claim tick - if (locals.claimTickEpoch <= cache.position.epochLast || - locals.claimTickAheadEpoch > cache.position.epochLast) { - require(false, "ClaimTick::NotFoundViaSearch()"); + if ( + locals.claimTickEpoch <= cache.position.epochLast || + locals.claimTickAheadEpoch > cache.position.epochLast + ) { + require(false, 'ClaimTick::NotFoundViaSearch()'); } - + cache.claimTick = ticks[locals.searchTick].limit; if ((locals.searchTick % cache.constants.tickSpacing) == 0) - cache.priceClaim = ConstantProduct.getPriceAtTick(locals.searchTick, cache.constants); + cache.priceClaim = ConstantProduct.getPriceAtTick( + locals.searchTick, + cache.constants + ); else { cache.priceClaim = cache.claimTick.priceAt; } if (cache.liquidityBurned == 0) - params.claim = TickMap.roundBack(locals.searchTick, cache.constants, params.zeroForOne, cache.priceClaim); - else - params.claim = locals.searchTick; + params.claim = TickMap.roundBack( + locals.searchTick, + cache.constants, + params.zeroForOne, + cache.priceClaim + ); + else params.claim = locals.searchTick; return (params, cache, locals.claimTickEpoch); } -} \ No newline at end of file +} diff --git a/contracts/libraries/limit/EpochMap.sol b/contracts/libraries/limit/EpochMap.sol index 2750fe93..5c31c69b 100644 --- a/contracts/libraries/limit/EpochMap.sol +++ b/contracts/libraries/limit/EpochMap.sol @@ -5,14 +5,10 @@ import '../math/ConstantProduct.sol'; import '../../interfaces/structs/LimitPoolStructs.sol'; library EpochMap { - event SyncLimitTick( - uint32 epoch, - int24 tick, - bool zeroForOne - ); + event SyncLimitTick(uint32 epoch, int24 tick, bool zeroForOne); function set( - int24 tick, + int24 tick, bool zeroForOne, uint256 epoch, PoolsharkStructs.TickMap storage tickMap, @@ -25,15 +21,17 @@ library EpochMap { uint256 volumeIndex ) = getIndices(tick, constants); // assert epoch isn't bigger than max uint32 - uint256 epochValue = zeroForOne ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] - : tickMap.epochs1[volumeIndex][blockIndex][wordIndex]; + uint256 epochValue = zeroForOne + ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] + : tickMap.epochs1[volumeIndex][blockIndex][wordIndex]; // clear previous value - epochValue &= ~(((1 << 9) - 1) << ((tickIndex & 0x7) * 32)); + epochValue &= ~(((1 << 9) - 1) << ((tickIndex & 0x7) * 32)); // add new value to word epochValue |= epoch << ((tickIndex & 0x7) * 32); // store word in map - zeroForOne ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] = epochValue - : tickMap.epochs1[volumeIndex][blockIndex][wordIndex] = epochValue; + zeroForOne + ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] = epochValue + : tickMap.epochs1[volumeIndex][blockIndex][wordIndex] = epochValue; emit SyncLimitTick(uint32(epoch), tick, zeroForOne); } @@ -43,9 +41,7 @@ library EpochMap { bool zeroForOne, PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.LimitImmutables memory constants - ) internal view returns ( - uint32 epoch - ) { + ) internal view returns (uint32 epoch) { ( uint256 tickIndex, uint256 wordIndex, @@ -53,8 +49,9 @@ library EpochMap { uint256 volumeIndex ) = getIndices(tick, constants); - uint256 epochValue = zeroForOne ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] - : tickMap.epochs1[volumeIndex][blockIndex][wordIndex]; + uint256 epochValue = zeroForOne + ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] + : tickMap.epochs1[volumeIndex][blockIndex][wordIndex]; // right shift so first 8 bits are epoch value epochValue >>= ((tickIndex & 0x7) * 32); // clear other bits @@ -65,7 +62,10 @@ library EpochMap { function getIndices( int24 tick, PoolsharkStructs.LimitImmutables memory constants - ) internal pure returns ( + ) + internal + pure + returns ( uint256 tickIndex, uint256 wordIndex, uint256 blockIndex, @@ -73,27 +73,34 @@ library EpochMap { ) { unchecked { - if (tick > ConstantProduct.maxTick(constants.tickSpacing)) require (false, 'TickIndexOverflow()'); - if (tick < ConstantProduct.minTick(constants.tickSpacing)) require (false, 'TickIndexUnderflow()'); + if (tick > ConstantProduct.maxTick(constants.tickSpacing)) + require(false, 'TickIndexOverflow()'); + if (tick < ConstantProduct.minTick(constants.tickSpacing)) + require(false, 'TickIndexUnderflow()'); if (tick % (constants.tickSpacing / 2) != 0) { - require (false, 'TickIndexInvalid()'); - } - tickIndex = uint256(int256((_round(tick, constants.tickSpacing / 2) - - _round(ConstantProduct.MIN_TICK, constants.tickSpacing / 2)) - / (constants.tickSpacing / 2))); - wordIndex = tickIndex >> 3; // 2^3 epochs per word - blockIndex = tickIndex >> 11; // 2^8 words per block - volumeIndex = tickIndex >> 19; // 2^8 blocks per volume - if (blockIndex > 2046) require (false, 'BlockIndexOverflow()'); + require(false, 'TickIndexInvalid()'); + } + tickIndex = uint256( + int256( + (_round(tick, constants.tickSpacing / 2) - + _round( + ConstantProduct.MIN_TICK, + constants.tickSpacing / 2 + )) / (constants.tickSpacing / 2) + ) + ); + wordIndex = tickIndex >> 3; // 2^3 epochs per word + blockIndex = tickIndex >> 11; // 2^8 words per block + volumeIndex = tickIndex >> 19; // 2^8 blocks per volume + if (blockIndex > 2046) require(false, 'BlockIndexOverflow()'); } } - function _round( - int24 tick, - int24 tickSpacing - ) internal pure returns ( - int24 roundedTick - ) { - return tick / tickSpacing * tickSpacing; + function _round(int24 tick, int24 tickSpacing) + internal + pure + returns (int24 roundedTick) + { + return (tick / tickSpacing) * tickSpacing; } } diff --git a/contracts/libraries/limit/LimitPositions.sol b/contracts/libraries/limit/LimitPositions.sol index 0005ff01..84f31f31 100644 --- a/contracts/libraries/limit/LimitPositions.sol +++ b/contracts/libraries/limit/LimitPositions.sol @@ -35,14 +35,24 @@ library LimitPositions { PoolsharkStructs.TickMap storage limitTickMap, PoolsharkStructs.MintLimitParams memory params, LimitPoolStructs.MintLimitCache memory cache - ) external returns ( - PoolsharkStructs.MintLimitParams memory, - LimitPoolStructs.MintLimitCache memory ) + external + returns ( + PoolsharkStructs.MintLimitParams memory, + LimitPoolStructs.MintLimitCache memory + ) { - cache.priceLower = ConstantProduct.getPriceAtTick(params.lower, cache.constants); - cache.priceUpper = ConstantProduct.getPriceAtTick(params.upper, cache.constants); - cache.mintSize = uint256(params.mintPercent) * uint256(params.amount) / 1e28; + cache.priceLower = ConstantProduct.getPriceAtTick( + params.lower, + cache.constants + ); + cache.priceUpper = ConstantProduct.getPriceAtTick( + params.upper, + cache.constants + ); + cache.mintSize = + (uint256(params.mintPercent) * uint256(params.amount)) / + 1e28; // calculate L constant cache.liquidityMinted = ConstantProduct.getLiquidityForAmounts( @@ -53,16 +63,36 @@ library LimitPositions { params.zeroForOne ? uint256(params.amount) : 0 ); - if (cache.liquidityMinted == 0) require (false, 'NoLiquidityBeingAdded()'); + if (cache.liquidityMinted == 0) + require(false, 'NoLiquidityBeingAdded()'); // calculate price limit by using half of input { - cache.priceLimit = params.zeroForOne ? ConstantProduct.getNewPrice(cache.priceUpper, cache.liquidityMinted, params.amount / 2, true, true) - : ConstantProduct.getNewPrice(cache.priceLower, cache.liquidityMinted, params.amount / 2, false, true); - if (cache.priceLimit == 0) require (false, 'PriceLimitZero()'); + cache.priceLimit = params.zeroForOne + ? ConstantProduct.getNewPrice( + cache.priceUpper, + cache.liquidityMinted, + params.amount / 2, + true, + true + ) + : ConstantProduct.getNewPrice( + cache.priceLower, + cache.liquidityMinted, + params.amount / 2, + false, + true + ); + if (cache.priceLimit == 0) require(false, 'PriceLimitZero()'); // get tick at price - cache.tickLimit = ConstantProduct.getTickAtPrice(cache.priceLimit.toUint160(), cache.constants); + cache.tickLimit = ConstantProduct.getTickAtPrice( + cache.priceLimit.toUint160(), + cache.constants + ); // round to nearest tick spacing - cache.priceLimit = ConstantProduct.getPriceAtTick(cache.tickLimit, cache.constants); + cache.priceLimit = ConstantProduct.getPriceAtTick( + cache.tickLimit, + cache.constants + ); } PoolsharkStructs.SwapCache memory swapCache; @@ -71,9 +101,14 @@ library LimitPositions { swapCache.price = cache.state.pool.price; // swap zero if no liquidity near market price - if (cache.state.pool.liquidity == 0 && - (params.zeroForOne ? swapCache.price > cache.priceLower - : swapCache.price < cache.priceUpper)) { + if ( + cache.state.pool.liquidity == 0 && + ( + params.zeroForOne + ? swapCache.price > cache.priceLower + : swapCache.price < cache.priceUpper + ) + ) { swapCache = Ticks.swap( ticks, samples, @@ -81,8 +116,9 @@ library LimitPositions { limitTickMap, PoolsharkStructs.SwapParams({ to: params.to, - priceLimit: (params.zeroForOne ? cache.priceLower - : cache.priceUpper).toUint160(), + priceLimit: ( + params.zeroForOne ? cache.priceLower : cache.priceUpper + ).toUint160(), amount: 0, exactIn: true, zeroForOne: params.zeroForOne, @@ -93,8 +129,11 @@ library LimitPositions { } // only swap if priceLimit is beyond current pool price - if (params.zeroForOne ? cache.priceLimit < swapCache.price - : cache.priceLimit > swapCache.price) { + if ( + params.zeroForOne + ? cache.priceLimit < swapCache.price + : cache.priceLimit > swapCache.price + ) { // swap and save the pool state swapCache = Ticks.swap( ticks, @@ -113,7 +152,6 @@ library LimitPositions { ); // subtract from remaining input amount params.amount -= uint128(swapCache.input); - } // save to cache cache.swapCache = swapCache; @@ -121,42 +159,72 @@ library LimitPositions { if (params.amount < cache.mintSize) params.amount = 0; // move start tick based on amount filled in swap - if ((params.amount > 0 && swapCache.input > 0) || - (params.zeroForOne ? cache.priceLower < swapCache.price - : cache.priceUpper > swapCache.price) + if ( + (params.amount > 0 && swapCache.input > 0) || + ( + params.zeroForOne + ? cache.priceLower < swapCache.price + : cache.priceUpper > swapCache.price + ) ) { // move the tick limit based on pool.tickAtPrice - if (params.zeroForOne ? cache.priceLower < swapCache.price - : cache.priceUpper > swapCache.price) { + if ( + params.zeroForOne + ? cache.priceLower < swapCache.price + : cache.priceUpper > swapCache.price + ) { cache.tickLimit = swapCache.state.pool.tickAtPrice; } // round ahead tickLimit to avoid crossing epochs - cache.tickLimit = TickMap.roundAhead(cache.tickLimit, cache.constants, params.zeroForOne, swapCache.price); + cache.tickLimit = TickMap.roundAhead( + cache.tickLimit, + cache.constants, + params.zeroForOne, + swapCache.price + ); if (params.zeroForOne) { if (cache.priceLower < swapCache.price) { // if rounding goes past limit trim position /// @dev - if swap didn't go to limit user would be 100% filled params.lower = cache.tickLimit; - cache.priceLower = ConstantProduct.getPriceAtTick(params.lower, cache.constants); + cache.priceLower = ConstantProduct.getPriceAtTick( + params.lower, + cache.constants + ); } - if (params.lower >= params.upper && - params.lower < ConstantProduct.maxTick(cache.constants.tickSpacing) - cache.constants.tickSpacing + if ( + params.lower >= params.upper && + params.lower < + ConstantProduct.maxTick(cache.constants.tickSpacing) - + cache.constants.tickSpacing ) { params.upper = params.lower + cache.constants.tickSpacing; } - cache.priceUpper = ConstantProduct.getPriceAtTick(params.upper, cache.constants); + cache.priceUpper = ConstantProduct.getPriceAtTick( + params.upper, + cache.constants + ); } else { if (cache.priceUpper > swapCache.price) { // if rounding goes past limit trim position params.upper = cache.tickLimit; - cache.priceUpper = ConstantProduct.getPriceAtTick(params.upper, cache.constants); + cache.priceUpper = ConstantProduct.getPriceAtTick( + params.upper, + cache.constants + ); } - if (params.upper <= params.lower && - params.lower > ConstantProduct.minTick(cache.constants.tickSpacing) + cache.constants.tickSpacing + if ( + params.upper <= params.lower && + params.lower > + ConstantProduct.minTick(cache.constants.tickSpacing) + + cache.constants.tickSpacing ) { params.lower = params.upper - cache.constants.tickSpacing; } - cache.priceLower = ConstantProduct.getPriceAtTick(params.lower, cache.constants); + cache.priceLower = ConstantProduct.getPriceAtTick( + params.lower, + cache.constants + ); } if (params.amount > 0 && params.lower < params.upper) { cache.liquidityMinted = ConstantProduct.getLiquidityForAmounts( @@ -166,10 +234,10 @@ library LimitPositions { params.zeroForOne ? 0 : uint256(params.amount), params.zeroForOne ? uint256(params.amount) : 0 ); - if (cache.liquidityMinted == 0) { + if (cache.liquidityMinted == 0) { // skip minting params.amount = 0; - } + } } else { // skip minting params.amount = 0; @@ -184,13 +252,12 @@ library LimitPositions { } // liquidity overflow check - if (cache.state.liquidityGlobal + cache.liquidityMinted > uint128(type(int128).max)) - require(false, 'LiquidityOverflow()'); + if ( + cache.state.liquidityGlobal + cache.liquidityMinted > + uint128(type(int128).max) + ) require(false, 'LiquidityOverflow()'); - return ( - params, - cache - ); + return (params, cache); } function add( @@ -198,10 +265,13 @@ library LimitPositions { mapping(int24 => LimitPoolStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.MintLimitParams memory params - ) internal returns ( - PoolsharkStructs.LimitPoolState memory, - LimitPoolStructs.LimitPosition memory - ) { + ) + internal + returns ( + PoolsharkStructs.LimitPoolState memory, + LimitPoolStructs.LimitPosition memory + ) + { if (cache.liquidityMinted == 0) return (cache.pool, cache.position); if (cache.position.liquidity == 0) { @@ -217,23 +287,26 @@ library LimitPositions { // safety check in case we somehow get here if ( params.zeroForOne - ? EpochMap.get(params.lower, params.zeroForOne, tickMap, cache.constants) - > cache.position.epochLast - : EpochMap.get(params.upper, params.zeroForOne, tickMap, cache.constants) - > cache.position.epochLast + ? EpochMap.get( + params.lower, + params.zeroForOne, + tickMap, + cache.constants + ) > cache.position.epochLast + : EpochMap.get( + params.upper, + params.zeroForOne, + tickMap, + cache.constants + ) > cache.position.epochLast ) { - require (false, 'PositionAlreadyEntered()'); + require(false, 'PositionAlreadyEntered()'); } /// @auditor maybe this shouldn't be a revert but rather just not mint the position? } - + // add liquidity to ticks - LimitTicks.insert( - ticks, - tickMap, - cache, - params - ); + LimitTicks.insert(ticks, tickMap, cache, params); // update liquidity global cache.state.liquidityGlobal += uint128(cache.liquidityMinted); @@ -248,68 +321,71 @@ library LimitPositions { PoolsharkStructs.TickMap storage tickMap, LimitPoolStructs.BurnLimitCache memory cache, PoolsharkStructs.BurnLimitParams memory params - ) internal returns ( - PoolsharkStructs.BurnLimitParams memory, - LimitPoolStructs.BurnLimitCache memory ) + internal + returns ( + PoolsharkStructs.BurnLimitParams memory, + LimitPoolStructs.BurnLimitCache memory + ) { - ( - params, - cache - ) = _deltas( - ticks, - tickMap, - params, - cache - ); + (params, cache) = _deltas(ticks, tickMap, params, cache); // update pool liquidity if (cache.priceClaim == cache.pool.price && cache.liquidityBurned > 0) { // handle pool.price at edge of range - if (params.zeroForOne ? cache.priceClaim < cache.priceUpper - : cache.priceClaim > cache.priceLower) - cache.pool.liquidity -= cache.liquidityBurned; + if ( + params.zeroForOne + ? cache.priceClaim < cache.priceUpper + : cache.priceClaim > cache.priceLower + ) cache.pool.liquidity -= cache.liquidityBurned; } if (cache.liquidityBurned > 0) { - if (params.claim == (params.zeroForOne ? cache.position.upper : cache.position.lower)) { + if ( + params.claim == + ( + params.zeroForOne + ? cache.position.upper + : cache.position.lower + ) + ) { // if claim is final tick no liquidity to remove cache.removeLower = false; cache.removeUpper = false; } else { // else remove liquidity from final tick - params.zeroForOne ? cache.removeUpper = true - : cache.removeLower = true; + params.zeroForOne + ? cache.removeUpper = true + : cache.removeLower = true; if (params.zeroForOne) { - - if (params.claim == cache.position.lower && + if ( + params.claim == cache.position.lower && cache.pool.price < cache.priceLower ) { // full tick price was touched cache.removeLower = true; - } else if (params.claim % cache.constants.tickSpacing != 0 && - cache.pool.price < cache.priceClaim) + } else if ( + params.claim % cache.constants.tickSpacing != 0 && + cache.pool.price < cache.priceClaim + ) // half tick was created cache.removeLower = true; } else { - if (params.claim == cache.position.upper && + if ( + params.claim == cache.position.upper && cache.pool.price > cache.priceUpper ) // full tick price was touched cache.removeUpper = true; - else if (params.claim % cache.constants.tickSpacing != 0 && - cache.pool.price > cache.priceClaim) + else if ( + params.claim % cache.constants.tickSpacing != 0 && + cache.pool.price > cache.priceClaim + ) // half tick was created cache.removeUpper = true; } } - LimitTicks.remove( - ticks, - tickMap, - params, - cache, - cache.constants - ); + LimitTicks.remove(ticks, tickMap, params, cache, cache.constants); // update position liquidity cache.position.liquidity -= uint128(cache.liquidityBurned); // update global liquidity @@ -319,12 +395,20 @@ library LimitPositions { // round back claim tick for storage if (params.claim % cache.constants.tickSpacing != 0) { cache.claim = params.claim; - params.claim = TickMap.roundBack(params.claim, cache.constants, params.zeroForOne, cache.priceClaim); + params.claim = TickMap.roundBack( + params.claim, + cache.constants, + params.zeroForOne, + cache.priceClaim + ); } // clear filled position - if (params.zeroForOne ? params.claim == cache.position.upper - : params.claim == cache.position.lower) { + if ( + params.zeroForOne + ? params.claim == cache.position.upper + : params.claim == cache.position.lower + ) { cache.state.liquidityGlobal -= cache.position.liquidity; cache.position.liquidity = 0; } @@ -334,7 +418,7 @@ library LimitPositions { cache.position.epochLast = 0; cache.position.crossedInto = false; } - + emit BurnLimit( params.to, params.positionId, @@ -360,19 +444,8 @@ library LimitPositions { PoolsharkStructs.TickMap storage tickMap, LimitPoolStructs.BurnLimitCache memory cache, PoolsharkStructs.BurnLimitParams memory params - ) internal view returns ( - uint128 amountIn, - uint128 amountOut - ) { - ( - params, - cache - ) = _deltas( - ticks, - tickMap, - params, - cache - ); + ) internal view returns (uint128 amountIn, uint128 amountOut) { + (params, cache) = _deltas(ticks, tickMap, params, cache); return (cache.amountIn, cache.amountOut); } @@ -382,21 +455,35 @@ library LimitPositions { PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache - ) internal view returns ( - PoolsharkStructs.BurnLimitParams memory, - LimitPoolStructs.BurnLimitCache memory - ) { + ) + internal + view + returns ( + PoolsharkStructs.BurnLimitParams memory, + LimitPoolStructs.BurnLimitCache memory + ) + { cache = LimitPoolStructs.BurnLimitCache({ state: cache.state, pool: params.zeroForOne ? cache.state.pool0 : cache.state.pool1, claimTick: ticks[params.claim].limit, position: cache.position, constants: cache.constants, - priceLower: ConstantProduct.getPriceAtTick(cache.position.lower, cache.constants), - priceClaim: ticks[params.claim].limit.priceAt == 0 ? ConstantProduct.getPriceAtTick(params.claim, cache.constants) - : ticks[params.claim].limit.priceAt, - priceUpper: ConstantProduct.getPriceAtTick(cache.position.upper, cache.constants), - liquidityBurned: _convert(cache.position.liquidity, params.burnPercent), + priceLower: ConstantProduct.getPriceAtTick( + cache.position.lower, + cache.constants + ), + priceClaim: ticks[params.claim].limit.priceAt == 0 + ? ConstantProduct.getPriceAtTick(params.claim, cache.constants) + : ticks[params.claim].limit.priceAt, + priceUpper: ConstantProduct.getPriceAtTick( + cache.position.upper, + cache.constants + ), + liquidityBurned: _convert( + cache.position.liquidity, + params.burnPercent + ), amountIn: 0, amountOut: 0, claim: params.claim, @@ -406,12 +493,7 @@ library LimitPositions { }); // check claim is valid - (params, cache) = Claims.validate( - ticks, - tickMap, - params, - cache - ); + (params, cache) = Claims.validate(ticks, tickMap, params, cache); // calculate position deltas cache = Claims.getDeltas(params, cache, cache.constants); @@ -419,15 +501,14 @@ library LimitPositions { return (params, cache); } - function _convert( - uint128 liquidity, - uint128 percent - ) internal pure returns ( - uint128 - ) { + function _convert(uint128 liquidity, uint128 percent) + internal + pure + returns (uint128) + { // convert percentage to liquidity amount if (percent > 1e38) percent = 1e38; - if (liquidity == 0 && percent > 0) require (false, 'PositionNotFound()'); - return uint128(uint256(liquidity) * uint256(percent) / 1e38); + if (liquidity == 0 && percent > 0) require(false, 'PositionNotFound()'); + return uint128((uint256(liquidity) * uint256(percent)) / 1e38); } -} \ No newline at end of file +} diff --git a/contracts/libraries/limit/LimitTicks.sol b/contracts/libraries/limit/LimitTicks.sol index c4d41787..96ded03a 100644 --- a/contracts/libraries/limit/LimitTicks.sol +++ b/contracts/libraries/limit/LimitTicks.sol @@ -14,7 +14,6 @@ import '../utils/SafeCast.sol'; /// @notice Tick management library for limit pools library LimitTicks { - using SafeCast for uint256; uint256 internal constant Q96 = 0x1000000000000000000000000; @@ -31,9 +30,11 @@ library LimitTicks { int24 tickSpacing ) internal pure { if (lower % tickSpacing != 0) require(false, 'InvalidLowerTick()'); - if (lower <= ConstantProduct.MIN_TICK) require(false, 'InvalidLowerTick()'); + if (lower <= ConstantProduct.MIN_TICK) + require(false, 'InvalidLowerTick()'); if (upper % tickSpacing != 0) require(false, 'InvalidUpperTick()'); - if (upper >= ConstantProduct.MAX_TICK) require(false, 'InvalidUpperTick()'); + if (upper >= ConstantProduct.MAX_TICK) + require(false, 'InvalidUpperTick()'); if (lower >= upper) require(false, 'InvalidPositionBounds()'); } @@ -48,18 +49,42 @@ library LimitTicks { // check if adding liquidity necessary if (!params.zeroForOne || cache.priceLower > cache.pool.price) { // sets bit in map - if(!TickMap.set(tickMap, params.lower, cache.constants.tickSpacing)){ - // inherit epoch + if ( + !TickMap.set(tickMap, params.lower, cache.constants.tickSpacing) + ) { + // inherit epoch int24 tickAhead; if (params.zeroForOne) { - tickAhead = TickMap.next(tickMap, params.lower, cache.constants.tickSpacing, false); + tickAhead = TickMap.next( + tickMap, + params.lower, + cache.constants.tickSpacing, + false + ); } else { - tickAhead = TickMap.previous(tickMap, params.lower, cache.constants.tickSpacing, false); + tickAhead = TickMap.previous( + tickMap, + params.lower, + cache.constants.tickSpacing, + false + ); } - uint32 epochAhead = EpochMap.get(tickAhead, params.zeroForOne, tickMap, cache.constants); - EpochMap.set(params.lower, params.zeroForOne, epochAhead, tickMap, cache.constants); + uint32 epochAhead = EpochMap.get( + tickAhead, + params.zeroForOne, + tickMap, + cache.constants + ); + EpochMap.set( + params.lower, + params.zeroForOne, + epochAhead, + tickMap, + cache.constants + ); } - PoolsharkStructs.LimitTick memory tickLower = ticks[params.lower].limit; + PoolsharkStructs.LimitTick memory tickLower = ticks[params.lower] + .limit; if (params.zeroForOne) { tickLower.liquidityDelta += int128(liquidityMinted); } else { @@ -71,21 +96,51 @@ library LimitTicks { /// @dev - i.e. if zeroForOne && cache.priceLower <= cache.pool.price cache.state.epoch += 1; // mark epoch on undercut tick - EpochMap.set(params.lower, params.zeroForOne, cache.state.epoch, tickMap, cache.constants); + EpochMap.set( + params.lower, + params.zeroForOne, + cache.state.epoch, + tickMap, + cache.constants + ); } if (params.zeroForOne || cache.priceUpper < cache.pool.price) { - if(!TickMap.set(tickMap, params.upper, cache.constants.tickSpacing)) { + if ( + !TickMap.set(tickMap, params.upper, cache.constants.tickSpacing) + ) { int24 tickAhead; if (params.zeroForOne) { - tickAhead = TickMap.next(tickMap, params.upper, cache.constants.tickSpacing, false); + tickAhead = TickMap.next( + tickMap, + params.upper, + cache.constants.tickSpacing, + false + ); } else { - tickAhead = TickMap.previous(tickMap, params.upper, cache.constants.tickSpacing, false); + tickAhead = TickMap.previous( + tickMap, + params.upper, + cache.constants.tickSpacing, + false + ); } - uint32 epochAhead = EpochMap.get(tickAhead, params.zeroForOne, tickMap, cache.constants); - EpochMap.set(params.upper, params.zeroForOne, epochAhead, tickMap, cache.constants); + uint32 epochAhead = EpochMap.get( + tickAhead, + params.zeroForOne, + tickMap, + cache.constants + ); + EpochMap.set( + params.upper, + params.zeroForOne, + epochAhead, + tickMap, + cache.constants + ); } - PoolsharkStructs.LimitTick memory tickUpper = ticks[params.upper].limit; + PoolsharkStructs.LimitTick memory tickUpper = ticks[params.upper] + .limit; if (params.zeroForOne) { tickUpper.liquidityDelta -= int128(liquidityMinted); } else { @@ -97,7 +152,13 @@ library LimitTicks { /// @dev - i.e. if !zeroForOne && cache.priceUpper >= cache.pool.price cache.state.epoch += 1; // mark epoch on undercut tick - EpochMap.set(params.upper, params.zeroForOne, cache.state.epoch, tickMap, cache.constants); + EpochMap.set( + params.upper, + params.zeroForOne, + cache.state.epoch, + tickMap, + cache.constants + ); } } @@ -108,58 +169,127 @@ library LimitTicks { LimitPoolStructs.MintLimitCache memory cache, PoolsharkStructs.LimitPoolState memory pool, PoolsharkStructs.LimitImmutables memory constants - ) internal returns ( - PoolsharkStructs.LimitPoolState memory - ){ + ) internal returns (PoolsharkStructs.LimitPoolState memory) { /// @auditor - would be smart to protect against the case of epochs crossing - ( - int24 tickToSave, - uint160 roundedPrice - ) = TickMap.roundHalf(pool.tickAtPrice, constants, pool.price); + (int24 tickToSave, uint160 roundedPrice) = TickMap.roundHalf( + pool.tickAtPrice, + constants, + pool.price + ); // update tick to save LimitPoolStructs.LimitTick memory tick = ticks[tickToSave].limit; /// @auditor - tick.priceAt will be zero for tick % tickSpacing == 0 if (tick.priceAt == 0) { - if (pool.price != (params.zeroForOne ? cache.priceLower : cache.priceUpper)) { + if ( + pool.price != + (params.zeroForOne ? cache.priceLower : cache.priceUpper) + ) { TickMap.set(tickMap, tickToSave, constants.tickSpacing); } - EpochMap.set(tickToSave, params.zeroForOne, cache.state.epoch, tickMap, constants); + EpochMap.set( + tickToSave, + params.zeroForOne, + cache.state.epoch, + tickMap, + constants + ); } // skip if we are at the nearest full tick - if(pool.price != roundedPrice) { + if (pool.price != roundedPrice) { // if empty just save the pool price if (tick.priceAt == 0) { tick.priceAt = pool.price; - } - else { + } else { // we need to blend the two partial fills into a single tick LimitPoolStructs.InsertSingleLocals memory locals; if (params.zeroForOne) { // price moves up so previousFullTick is lesser - locals.previousFullTick = tickToSave - constants.tickSpacing / 2; - locals.pricePrevious = ConstantProduct.getPriceAtTick(locals.previousFullTick, constants); + locals.previousFullTick = + tickToSave - + constants.tickSpacing / + 2; + locals.pricePrevious = ConstantProduct.getPriceAtTick( + locals.previousFullTick, + constants + ); // calculate amountOut filled across both partial fills - locals.amountOutExact = ConstantProduct.getDy(pool.liquidity, locals.pricePrevious, pool.price, false); - locals.amountOutExact += ConstantProduct.getDy(uint128(tick.liquidityDelta), locals.pricePrevious, tick.priceAt, false); + locals.amountOutExact = ConstantProduct.getDy( + pool.liquidity, + locals.pricePrevious, + pool.price, + false + ); + locals.amountOutExact += ConstantProduct.getDy( + uint128(tick.liquidityDelta), + locals.pricePrevious, + tick.priceAt, + false + ); // add current pool liquidity to partial tick - uint128 combinedLiquidity = pool.liquidity + uint128(tick.liquidityDelta); + uint128 combinedLiquidity = pool.liquidity + + uint128(tick.liquidityDelta); // advance price based on combined fill - tick.priceAt = ConstantProduct.getNewPrice(uint256(locals.pricePrevious), combinedLiquidity, locals.amountOutExact, false, true).toUint160(); + tick.priceAt = ConstantProduct + .getNewPrice( + uint256(locals.pricePrevious), + combinedLiquidity, + locals.amountOutExact, + false, + true + ) + .toUint160(); // dx to the next tick is less than before the tick blend - EpochMap.set(tickToSave, params.zeroForOne, cache.state.epoch, tickMap, constants); + EpochMap.set( + tickToSave, + params.zeroForOne, + cache.state.epoch, + tickMap, + constants + ); } else { // price moves down so previousFullTick is greater - locals.previousFullTick = tickToSave + constants.tickSpacing / 2; - locals.pricePrevious = ConstantProduct.getPriceAtTick(locals.previousFullTick, constants); + locals.previousFullTick = + tickToSave + + constants.tickSpacing / + 2; + locals.pricePrevious = ConstantProduct.getPriceAtTick( + locals.previousFullTick, + constants + ); // calculate amountOut filled across both partial fills - locals.amountOutExact = ConstantProduct.getDx(pool.liquidity, pool.price, locals.pricePrevious, false); - locals.amountOutExact += ConstantProduct.getDx(uint128(tick.liquidityDelta), tick.priceAt, locals.pricePrevious, false); + locals.amountOutExact = ConstantProduct.getDx( + pool.liquidity, + pool.price, + locals.pricePrevious, + false + ); + locals.amountOutExact += ConstantProduct.getDx( + uint128(tick.liquidityDelta), + tick.priceAt, + locals.pricePrevious, + false + ); // add current pool liquidity to partial tick - uint128 combinedLiquidity = pool.liquidity + uint128(tick.liquidityDelta); + uint128 combinedLiquidity = pool.liquidity + + uint128(tick.liquidityDelta); // advance price based on combined fill - tick.priceAt = ConstantProduct.getNewPrice(uint256(locals.pricePrevious), combinedLiquidity, locals.amountOutExact, true, true).toUint160(); + tick.priceAt = ConstantProduct + .getNewPrice( + uint256(locals.pricePrevious), + combinedLiquidity, + locals.amountOutExact, + true, + true + ) + .toUint160(); // mark epoch for second partial fill positions - EpochMap.set(tickToSave, params.zeroForOne, cache.state.epoch, tickMap, constants); + EpochMap.set( + tickToSave, + params.zeroForOne, + cache.state.epoch, + tickMap, + constants + ); } } } @@ -167,7 +297,11 @@ library LimitTicks { if ((tickToSave != (params.zeroForOne ? params.lower : params.upper))) { tick.liquidityDelta += int128(pool.liquidity); tick.liquidityAbsolute += pool.liquidity; - emit SyncLimitLiquidity(pool.liquidity, tickToSave, params.zeroForOne); + emit SyncLimitLiquidity( + pool.liquidity, + tickToSave, + params.zeroForOne + ); pool.liquidity = 0; } ticks[tickToSave].limit = tick; @@ -184,9 +318,9 @@ library LimitTicks { // set ticks based on claim and zeroForOne int24 lower = params.zeroForOne ? params.claim : cache.position.lower; int24 upper = params.zeroForOne ? cache.position.upper : params.claim; - { + { PoolsharkStructs.LimitTick memory tickLower = ticks[lower].limit; - + if (cache.removeLower) { if (params.zeroForOne) { tickLower.liquidityDelta -= int128(cache.liquidityBurned); @@ -213,31 +347,65 @@ library LimitTicks { } } - function unlock( + function unlock( LimitPoolStructs.MintLimitCache memory cache, PoolsharkStructs.LimitPoolState memory pool, mapping(int24 => LimitPoolStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, bool zeroForOne - ) internal returns ( - LimitPoolStructs.MintLimitCache memory, - PoolsharkStructs.LimitPoolState memory ) + internal + returns ( + LimitPoolStructs.MintLimitCache memory, + PoolsharkStructs.LimitPoolState memory + ) { if (pool.liquidity > 0) return (cache, pool); - (int24 startTick,) = TickMap.roundHalf(pool.tickAtPrice, cache.constants, pool.price); + (int24 startTick, ) = TickMap.roundHalf( + pool.tickAtPrice, + cache.constants, + pool.price + ); if (zeroForOne) { - pool.tickAtPrice = TickMap.next(tickMap, startTick, cache.constants.tickSpacing, true); - if (pool.tickAtPrice < ConstantProduct.maxTick(cache.constants.tickSpacing)) { - EpochMap.set(pool.tickAtPrice, zeroForOne, cache.state.epoch, tickMap, cache.constants); + pool.tickAtPrice = TickMap.next( + tickMap, + startTick, + cache.constants.tickSpacing, + true + ); + if ( + pool.tickAtPrice < + ConstantProduct.maxTick(cache.constants.tickSpacing) + ) { + EpochMap.set( + pool.tickAtPrice, + zeroForOne, + cache.state.epoch, + tickMap, + cache.constants + ); } } else { /// @dev - roundedUp true since liquidity could be equal to the current pool tickAtPrice - pool.tickAtPrice = TickMap.previous(tickMap, startTick, cache.constants.tickSpacing, true); - if (pool.tickAtPrice > ConstantProduct.minTick(cache.constants.tickSpacing)) { - EpochMap.set(pool.tickAtPrice, zeroForOne, cache.state.epoch, tickMap, cache.constants); + pool.tickAtPrice = TickMap.previous( + tickMap, + startTick, + cache.constants.tickSpacing, + true + ); + if ( + pool.tickAtPrice > + ConstantProduct.minTick(cache.constants.tickSpacing) + ) { + EpochMap.set( + pool.tickAtPrice, + zeroForOne, + cache.state.epoch, + tickMap, + cache.constants + ); } } @@ -248,15 +416,21 @@ library LimitTicks { if (tickPriceAt == 0) { // if full tick crossed - pool.price = ConstantProduct.getPriceAtTick(pool.tickAtPrice, cache.constants); + pool.price = ConstantProduct.getPriceAtTick( + pool.tickAtPrice, + cache.constants + ); } else { // if half tick crossed pool.price = tickPriceAt; - pool.tickAtPrice = ConstantProduct.getTickAtPrice(tickPriceAt, cache.constants); + pool.tickAtPrice = ConstantProduct.getTickAtPrice( + tickPriceAt, + cache.constants + ); } // zero out tick - ticks[tickToClear].limit = PoolsharkStructs.LimitTick(0,0,0); + ticks[tickToClear].limit = PoolsharkStructs.LimitTick(0, 0, 0); clear(ticks, cache.constants, tickMap, tickToClear); return (cache, pool); @@ -269,19 +443,21 @@ library LimitTicks { int24 tickToClear ) internal { if (_empty(ticks[tickToClear])) { - if (tickToClear != ConstantProduct.maxTick(constants.tickSpacing) && - tickToClear != ConstantProduct.minTick(constants.tickSpacing)) { - ticks[tickToClear].limit = PoolsharkStructs.LimitTick(0,0,0); + if ( + tickToClear != ConstantProduct.maxTick(constants.tickSpacing) && + tickToClear != ConstantProduct.minTick(constants.tickSpacing) + ) { + ticks[tickToClear].limit = PoolsharkStructs.LimitTick(0, 0, 0); TickMap.unset(tickMap, tickToClear, constants.tickSpacing); } } } - function _empty( - LimitPoolStructs.Tick memory tick - ) internal pure returns ( - bool - ) { + function _empty(LimitPoolStructs.Tick memory tick) + internal + pure + returns (bool) + { return tick.limit.liquidityAbsolute == 0; } } diff --git a/contracts/libraries/limit/pool/BurnLimitCall.sol b/contracts/libraries/limit/pool/BurnLimitCall.sol index 40e1c199..4086b24c 100644 --- a/contracts/libraries/limit/pool/BurnLimitCall.sol +++ b/contracts/libraries/limit/pool/BurnLimitCall.sol @@ -29,45 +29,46 @@ library BurnLimitCall { } function perform( - mapping(uint256 => LimitPoolStructs.LimitPosition) - storage positions, + mapping(uint256 => LimitPoolStructs.LimitPosition) storage positions, mapping(int24 => LimitPoolStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache - ) external returns ( - int256, // amount0Delta - int256 // amount1Delta ) + external + returns ( + int256, // amount0Delta + int256 // amount1Delta + ) { // check for invalid receiver - if (params.to == address(0)) - require(false, 'CollectToZeroAddress()'); + if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); // initialize cache cache.state = globalState; cache.position = positions[params.positionId]; - if (cache.position.liquidity == 0) - require(false, 'PositionNotFound()'); - if (PositionTokens.balanceOf(cache.constants, msg.sender, params.positionId) == 0) - require(false, 'PositionOwnerMismatch()'); - + if (cache.position.liquidity == 0) require(false, 'PositionNotFound()'); + if ( + PositionTokens.balanceOf( + cache.constants, + msg.sender, + params.positionId + ) == 0 + ) require(false, 'PositionOwnerMismatch()'); + // update position - ( - params, - cache - ) = LimitPositions.update( - ticks, - tickMap, - cache, - params - ); + (params, cache) = LimitPositions.update(ticks, tickMap, cache, params); // save position before transfer - if ((params.zeroForOne ? params.claim != cache.position.upper - : params.claim != cache.position.lower)) { + if ( + ( + params.zeroForOne + ? params.claim != cache.position.upper + : params.claim != cache.position.lower + ) + ) { if (cache.position.liquidity > 0) { if (params.zeroForOne) { cache.position.lower = params.claim; @@ -76,23 +77,29 @@ library BurnLimitCall { } positions[params.positionId] = cache.position; } else { - IPositionERC1155(cache.constants.poolToken).burn(msg.sender, params.positionId, 1, cache.constants); + IPositionERC1155(cache.constants.poolToken).burn( + msg.sender, + params.positionId, + 1, + cache.constants + ); delete positions[params.positionId]; } } else { - IPositionERC1155(cache.constants.poolToken).burn(msg.sender, params.positionId, 1, cache.constants); + IPositionERC1155(cache.constants.poolToken).burn( + msg.sender, + params.positionId, + 1, + cache.constants + ); delete positions[params.positionId]; } // save state before transfer call save(cache, globalState, params.zeroForOne); - + BurnLimitLocals memory locals; - ( - cache, - locals.amount0Delta, - locals.amount1Delta - ) = Collect.burnLimit( + (cache, locals.amount0Delta, locals.amount1Delta) = Collect.burnLimit( cache, params ); diff --git a/contracts/libraries/limit/pool/MintLimitCall.sol b/contracts/libraries/limit/pool/MintLimitCall.sol index 38b7870f..25ebe8b6 100644 --- a/contracts/libraries/limit/pool/MintLimitCall.sol +++ b/contracts/libraries/limit/pool/MintLimitCall.sol @@ -29,8 +29,7 @@ library MintLimitCall { ); function perform( - mapping(uint256 => LimitPoolStructs.LimitPosition) - storage positions, + mapping(uint256 => LimitPoolStructs.LimitPosition) storage positions, mapping(int24 => LimitPoolStructs.Tick) storage ticks, RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.TickMap storage rangeTickMap, @@ -38,18 +37,24 @@ library MintLimitCall { PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.MintLimitParams memory params, LimitPoolStructs.MintLimitCache memory cache - ) external returns ( - int256, // amount0Delta - int256 // amount1Delta - ){ + ) + external + returns ( + int256, // amount0Delta + int256 // amount1Delta + ) + { // check for invalid receiver - if (params.to == address(0)) - require(false, "CollectToZeroAddress()"); + if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); cache.state = globalState; // validate position ticks - ConstantProduct.checkTicks(params.lower, params.upper, cache.constants.tickSpacing); + ConstantProduct.checkTicks( + params.lower, + params.upper, + cache.constants.tickSpacing + ); if (params.positionId > 0) { cache.position = positions[params.positionId]; @@ -57,8 +62,13 @@ library MintLimitCall { // position doesn't exist require(false, 'PositionNotFound()'); } - if (PositionTokens.balanceOf(cache.constants, params.to, params.positionId) == 0) - require(false, 'PositionOwnerMismatch()'); + if ( + PositionTokens.balanceOf( + cache.constants, + params.to, + params.positionId + ) == 0 + ) require(false, 'PositionOwnerMismatch()'); } // resize position if necessary @@ -74,20 +84,24 @@ library MintLimitCall { // save state for reentrancy safety save(cache, globalState, !params.zeroForOne); - // transfer out if swap output + // transfer out if swap output if (cache.swapCache.output > 0) SafeTransfers.transferOut( params.to, - params.zeroForOne ? cache.constants.token1 - : cache.constants.token0, + params.zeroForOne + ? cache.constants.token1 + : cache.constants.token0, cache.swapCache.output ); // mint position if amount is left if (params.amount > 0 && params.lower < params.upper) { // check if new position created - if (params.positionId == 0 || // new position - params.lower != cache.position.lower || // lower mismatch - params.upper != cache.position.upper) { // upper mismatch + if ( + params.positionId == 0 || // new position + params.lower != cache.position.lower || // lower mismatch + params.upper != cache.position.upper + ) { + // upper mismatch LimitPoolStructs.LimitPosition memory newPosition; newPosition.lower = params.lower; newPosition.upper = params.upper; @@ -96,19 +110,37 @@ library MintLimitCall { params.positionId = cache.state.positionIdNext; cache.state.positionIdNext += 1; } - cache.pool = params.zeroForOne ? cache.state.pool0 : cache.state.pool1; + cache.pool = params.zeroForOne + ? cache.state.pool0 + : cache.state.pool1; // bump to the next tick if there is no liquidity if (cache.pool.liquidity == 0) { /// @dev - this makes sure to have liquidity unlocked if undercutting - (cache, cache.pool) = LimitTicks.unlock(cache, cache.pool, ticks, limitTickMap, params.zeroForOne); + (cache, cache.pool) = LimitTicks.unlock( + cache, + cache.pool, + ticks, + limitTickMap, + params.zeroForOne + ); } if (params.zeroForOne) { - uint160 priceLower = ConstantProduct.getPriceAtTick(params.lower, cache.constants); + uint160 priceLower = ConstantProduct.getPriceAtTick( + params.lower, + cache.constants + ); if (priceLower <= cache.pool.price) { // save liquidity if active if (cache.pool.liquidity > 0) { - cache.pool = LimitTicks.insertSingle(params, ticks, limitTickMap, cache, cache.pool, cache.constants); + cache.pool = LimitTicks.insertSingle( + params, + ticks, + limitTickMap, + cache, + cache.pool, + cache.constants + ); } cache.pool.price = priceLower; cache.pool.tickAtPrice = params.lower; @@ -117,13 +149,29 @@ library MintLimitCall { cache.position.crossedInto = true; // set epoch on start tick to signify position being crossed into /// @auditor - this is safe assuming we have swapped at least this far on the other side - emit SyncLimitPool(cache.pool.price, cache.pool.liquidity, cache.state.epoch, cache.pool.tickAtPrice, params.zeroForOne); + emit SyncLimitPool( + cache.pool.price, + cache.pool.liquidity, + cache.state.epoch, + cache.pool.tickAtPrice, + params.zeroForOne + ); } } else { - uint160 priceUpper = ConstantProduct.getPriceAtTick(params.upper, cache.constants); + uint160 priceUpper = ConstantProduct.getPriceAtTick( + params.upper, + cache.constants + ); if (priceUpper >= cache.pool.price) { if (cache.pool.liquidity > 0) { - cache.pool = LimitTicks.insertSingle(params, ticks, limitTickMap, cache, cache.pool, cache.constants); + cache.pool = LimitTicks.insertSingle( + params, + ticks, + limitTickMap, + cache, + cache.pool, + cache.constants + ); } cache.pool.price = priceUpper; cache.pool.tickAtPrice = params.upper; @@ -131,7 +179,13 @@ library MintLimitCall { cache.position.crossedInto = true; // set epoch on start tick to signify position being crossed into /// @auditor - this is safe assuming we have swapped at least this far on the other side - emit SyncLimitPool(cache.pool.price, cache.pool.liquidity, cache.state.epoch, cache.pool.tickAtPrice, params.zeroForOne); + emit SyncLimitPool( + cache.pool.price, + cache.pool.liquidity, + cache.state.epoch, + cache.pool.tickAtPrice, + params.zeroForOne + ); } } (cache.pool, cache.position) = LimitPositions.add( @@ -144,7 +198,9 @@ library MintLimitCall { // save position to storage positions[params.positionId] = cache.position; - params.zeroForOne ? cache.state.pool0 = cache.pool : cache.state.pool1 = cache.pool; + params.zeroForOne + ? cache.state.pool0 = cache.pool + : cache.state.pool1 = cache.pool; emit MintLimit( params.to, @@ -163,18 +219,28 @@ library MintLimitCall { // check balance and execute callback uint256 balanceStart = balance(params, cache); ILimitPoolMintLimitCallback(msg.sender).limitPoolMintLimitCallback( - params.zeroForOne ? -int256(params.amount + cache.swapCache.input) : int256(cache.swapCache.output), - params.zeroForOne ? int256(cache.swapCache.output) : -int256(params.amount + cache.swapCache.input), + params.zeroForOne + ? -int256(params.amount + cache.swapCache.input) + : int256(cache.swapCache.output), + params.zeroForOne + ? int256(cache.swapCache.output) + : -int256(params.amount + cache.swapCache.input), params.callbackData ); // check balance requirements after callback - if (balance(params, cache) < balanceStart + params.amount + cache.swapCache.input) - require(false, 'MintInputAmountTooLow()'); + if ( + balance(params, cache) < + balanceStart + params.amount + cache.swapCache.input + ) require(false, 'MintInputAmountTooLow()'); return ( - params.zeroForOne ? -int256(params.amount + cache.swapCache.input) : int256(cache.swapCache.output), - params.zeroForOne ? int256(cache.swapCache.output) : -int256(params.amount + cache.swapCache.input) + params.zeroForOne + ? -int256(params.amount + cache.swapCache.input) + : int256(cache.swapCache.output), + params.zeroForOne + ? int256(cache.swapCache.output) + : -int256(params.amount + cache.swapCache.input) ); } @@ -195,22 +261,18 @@ library MintLimitCall { } } - function balance( PoolsharkStructs.MintLimitParams memory params, LimitPoolStructs.MintLimitCache memory cache ) private view returns (uint256) { - ( - bool success, - bytes memory data - ) = (params.zeroForOne ? cache.constants.token0 - : cache.constants.token1) - .staticcall( - abi.encodeWithSelector( - IERC20Minimal.balanceOf.selector, - address(this) - ) - ); + (bool success, bytes memory data) = ( + params.zeroForOne ? cache.constants.token0 : cache.constants.token1 + ).staticcall( + abi.encodeWithSelector( + IERC20Minimal.balanceOf.selector, + address(this) + ) + ); require(success && data.length >= 32); return abi.decode(data, (uint256)); } diff --git a/contracts/libraries/limit/pool/SnapshotLimitCall.sol b/contracts/libraries/limit/pool/SnapshotLimitCall.sol index c2df7e30..74dc48e7 100644 --- a/contracts/libraries/limit/pool/SnapshotLimitCall.sol +++ b/contracts/libraries/limit/pool/SnapshotLimitCall.sol @@ -22,37 +22,28 @@ library SnapshotLimitCall { ); function perform( - mapping(uint256 => LimitPoolStructs.LimitPosition) - storage positions, + mapping(uint256 => LimitPoolStructs.LimitPosition) storage positions, mapping(int24 => LimitPoolStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, LimitPoolStructs.SnapshotLimitParams memory params - ) external view returns ( - uint128, - uint128 - ) - { + ) external view returns (uint128, uint128) { if (state.unlocked == _ENTERED) require(false, 'ReentrancyGuardReadOnlyReentrantCall()'); LimitPoolStructs.BurnLimitCache memory cache; cache.state = state; cache.constants = constants; cache.position = positions[params.positionId]; - PoolsharkStructs.BurnLimitParams memory burnParams = PoolsharkStructs.BurnLimitParams ({ - to: params.owner, - burnPercent: params.burnPercent, - positionId: params.positionId, - claim: params.claim, - zeroForOne: params.zeroForOne - }); + PoolsharkStructs.BurnLimitParams memory burnParams = PoolsharkStructs + .BurnLimitParams({ + to: params.owner, + burnPercent: params.burnPercent, + positionId: params.positionId, + claim: params.claim, + zeroForOne: params.zeroForOne + }); if (cache.position.epochLast == 0) require(false, 'PositionNotFound()'); - return LimitPositions.snapshot( - ticks, - tickMap, - cache, - burnParams - ); + return LimitPositions.snapshot(ticks, tickMap, cache, burnParams); } } diff --git a/contracts/libraries/math/ConstantProduct.sol b/contracts/libraries/math/ConstantProduct.sol index 9ec27abd..fd386073 100644 --- a/contracts/libraries/math/ConstantProduct.sol +++ b/contracts/libraries/math/ConstantProduct.sol @@ -27,9 +27,17 @@ library ConstantProduct { unchecked { if (liquidity == 0) return 0; if (roundUp) { - dy = OverflowMath.mulDivRoundingUp(liquidity, priceUpper - priceLower, Q96); + dy = OverflowMath.mulDivRoundingUp( + liquidity, + priceUpper - priceLower, + Q96 + ); } else { - dy = OverflowMath.mulDiv(liquidity, priceUpper - priceLower, Q96); + dy = OverflowMath.mulDiv( + liquidity, + priceUpper - priceLower, + Q96 + ); } } } @@ -44,19 +52,21 @@ library ConstantProduct { if (liquidity == 0) return 0; if (roundUp) { dx = OverflowMath.divRoundingUp( - OverflowMath.mulDivRoundingUp( - liquidity << 96, - priceUpper - priceLower, - priceUpper - ), - priceLower + OverflowMath.mulDivRoundingUp( + liquidity << 96, + priceUpper - priceLower, + priceUpper + ), + priceLower ); } else { - dx = OverflowMath.mulDiv( + dx = + OverflowMath.mulDiv( liquidity << 96, priceUpper - priceLower, priceUpper - ) / priceLower; + ) / + priceLower; } } } @@ -70,7 +80,11 @@ library ConstantProduct { ) internal pure returns (uint256 liquidity) { unchecked { if (priceUpper <= currentPrice) { - liquidity = OverflowMath.mulDiv(dy, Q96, priceUpper - priceLower); + liquidity = OverflowMath.mulDiv( + dy, + Q96, + priceUpper - priceLower + ); } else if (currentPrice <= priceLower) { liquidity = OverflowMath.mulDiv( dx, @@ -83,7 +97,11 @@ library ConstantProduct { OverflowMath.mulDiv(priceUpper, currentPrice, Q96), priceUpper - currentPrice ); - uint256 liquidity1 = OverflowMath.mulDiv(dy, Q96, currentPrice - priceLower); + uint256 liquidity1 = OverflowMath.mulDiv( + dy, + Q96, + currentPrice - priceLower + ); liquidity = liquidity0 < liquidity1 ? liquidity0 : liquidity1; } } @@ -97,15 +115,25 @@ library ConstantProduct { bool roundUp ) internal pure returns (uint128 token0amount, uint128 token1amount) { if (priceUpper <= currentPrice) { - token1amount = uint128(getDy(liquidityAmount, priceLower, priceUpper, roundUp)); + token1amount = uint128( + getDy(liquidityAmount, priceLower, priceUpper, roundUp) + ); } else if (currentPrice <= priceLower) { - token0amount = uint128(getDx(liquidityAmount, priceLower, priceUpper, roundUp)); + token0amount = uint128( + getDx(liquidityAmount, priceLower, priceUpper, roundUp) + ); } else { - token0amount = uint128(getDx(liquidityAmount, currentPrice, priceUpper, roundUp)); - token1amount = uint128(getDy(liquidityAmount, priceLower, currentPrice, roundUp)); + token0amount = uint128( + getDx(liquidityAmount, currentPrice, priceUpper, roundUp) + ); + token1amount = uint128( + getDy(liquidityAmount, priceLower, currentPrice, roundUp) + ); } - if (token0amount > uint128(type(int128).max)) require(false, 'AmountsOutOfBounds()'); - if (token1amount > uint128(type(int128).max)) require(false, 'AmountsOutOfBounds()'); + if (token0amount > uint128(type(int128).max)) + require(false, 'AmountsOutOfBounds()'); + if (token1amount > uint128(type(int128).max)) + require(false, 'AmountsOutOfBounds()'); } function getNewPrice( @@ -114,92 +142,71 @@ library ConstantProduct { uint256 amount, bool zeroForOne, bool exactIn - ) internal pure returns ( - uint256 newPrice - ) { + ) internal pure returns (uint256 newPrice) { if (exactIn) { if (zeroForOne) { uint256 liquidityPadded = liquidity << 96; newPrice = OverflowMath.mulDivRoundingUp( - liquidityPadded, - price, - liquidityPadded + price * amount - ); + liquidityPadded, + price, + liquidityPadded + price * amount + ); } else { newPrice = price + (amount << 96) / liquidity; } } else { if (zeroForOne) { - newPrice = price - - OverflowMath.divRoundingUp(amount << 96, liquidity); + newPrice = + price - + OverflowMath.divRoundingUp(amount << 96, liquidity); } else { uint256 liquidityPadded = uint256(liquidity) << 96; newPrice = OverflowMath.mulDivRoundingUp( - liquidityPadded, - price, - liquidityPadded - uint256(price) * amount + liquidityPadded, + price, + liquidityPadded - uint256(price) * amount ); } } } - function getPrice( - uint256 sqrtPrice - ) internal pure returns (uint256 price) { - if (sqrtPrice >= 2 ** 48) - price = OverflowMath.mulDiv(sqrtPrice, sqrtPrice, 2 ** 96); - else - price = sqrtPrice; + function getPrice(uint256 sqrtPrice) internal pure returns (uint256 price) { + if (sqrtPrice >= 2**48) + price = OverflowMath.mulDiv(sqrtPrice, sqrtPrice, 2**96); + else price = sqrtPrice; } ///////////////////////////////////////////////////////////// ///////////////////////// TICK MATH ///////////////////////// ///////////////////////////////////////////////////////////// - int24 internal constant MIN_TICK = -887272; /// @dev - tick for price of 2^-128 + int24 internal constant MIN_TICK = -887272; /// @dev - tick for price of 2^-128 int24 internal constant MAX_TICK = -MIN_TICK; /// @dev - tick for price of 2^128 - function minTick( - int16 tickSpacing - ) internal pure returns ( - int24 tick - ) { - return MIN_TICK / tickSpacing * tickSpacing; + function minTick(int16 tickSpacing) internal pure returns (int24 tick) { + return (MIN_TICK / tickSpacing) * tickSpacing; } - function maxTick( - int16 tickSpacing - ) internal pure returns ( - int24 tick - ) { - return MAX_TICK / tickSpacing * tickSpacing; + function maxTick(int16 tickSpacing) internal pure returns (int24 tick) { + return (MAX_TICK / tickSpacing) * tickSpacing; } - function priceBounds( - int16 tickSpacing - ) internal pure returns ( - uint160, - uint160 - ) { + function priceBounds(int16 tickSpacing) + internal + pure + returns (uint160, uint160) + { return (minPrice(tickSpacing), maxPrice(tickSpacing)); } - function minPrice( - int16 tickSpacing - ) internal pure returns ( - uint160 price - ) { - PoolsharkStructs.LimitImmutables memory constants; + function minPrice(int16 tickSpacing) internal pure returns (uint160 price) { + PoolsharkStructs.LimitImmutables memory constants; constants.tickSpacing = tickSpacing; return getPriceAtTick(minTick(tickSpacing), constants); } - function maxPrice( - int16 tickSpacing - ) internal pure returns ( - uint160 price - ) { - PoolsharkStructs.LimitImmutables memory constants; + function maxPrice(int16 tickSpacing) internal pure returns (uint160 price) { + PoolsharkStructs.LimitImmutables memory constants; constants.tickSpacing = tickSpacing; return getPriceAtTick(maxTick(tickSpacing), constants); } @@ -208,20 +215,24 @@ library ConstantProduct { int24 lower, int24 upper, int16 tickSpacing - ) internal pure - { - if (lower < minTick(tickSpacing)) require (false, 'LowerTickOutOfBounds()'); - if (upper > maxTick(tickSpacing)) require (false, 'UpperTickOutOfBounds()'); - if (lower % tickSpacing != 0) require (false, 'LowerTickOutsideTickSpacing()'); - if (upper % tickSpacing != 0) require (false, 'UpperTickOutsideTickSpacing()'); - if (lower >= upper) require (false, 'LowerUpperTickOrderInvalid()'); + ) internal pure { + if (lower < minTick(tickSpacing)) + require(false, 'LowerTickOutOfBounds()'); + if (upper > maxTick(tickSpacing)) + require(false, 'UpperTickOutOfBounds()'); + if (lower % tickSpacing != 0) + require(false, 'LowerTickOutsideTickSpacing()'); + if (upper % tickSpacing != 0) + require(false, 'UpperTickOutsideTickSpacing()'); + if (lower >= upper) require(false, 'LowerUpperTickOrderInvalid()'); } - function checkPrice( - uint160 price, - PriceBounds memory bounds - ) internal pure { - if (price < bounds.min || price >= bounds.max) require (false, 'PriceOutOfBounds()'); + function checkPrice(uint160 price, PriceBounds memory bounds) + internal + pure + { + if (price < bounds.min || price >= bounds.max) + require(false, 'PriceOutOfBounds()'); } /// @notice Calculates sqrt(1.0001^tick) * 2^96. @@ -232,34 +243,54 @@ library ConstantProduct { function getPriceAtTick( int24 tick, PoolsharkStructs.LimitImmutables memory constants - ) internal pure returns ( - uint160 price - ) { - uint256 absTick = tick < 0 ? uint256(-int256(tick)) : uint256(int256(tick)); - if (absTick > uint256(uint24(maxTick(constants.tickSpacing)))) require (false, 'TickOutOfBounds()'); + ) internal pure returns (uint160 price) { + uint256 absTick = tick < 0 + ? uint256(-int256(tick)) + : uint256(int256(tick)); + if (absTick > uint256(uint24(maxTick(constants.tickSpacing)))) + require(false, 'TickOutOfBounds()'); unchecked { uint256 ratio = absTick & 0x1 != 0 ? 0xfffcb933bd6fad37aa2d162d1a594001 : 0x100000000000000000000000000000000; - if (absTick & 0x2 != 0) ratio = (ratio * 0xfff97272373d413259a46990580e213a) >> 128; - if (absTick & 0x4 != 0) ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdcc) >> 128; - if (absTick & 0x8 != 0) ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0) >> 128; - if (absTick & 0x10 != 0) ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644) >> 128; - if (absTick & 0x20 != 0) ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0) >> 128; - if (absTick & 0x40 != 0) ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861) >> 128; - if (absTick & 0x80 != 0) ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053) >> 128; - if (absTick & 0x100 != 0) ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4) >> 128; - if (absTick & 0x200 != 0) ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54) >> 128; - if (absTick & 0x400 != 0) ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3) >> 128; - if (absTick & 0x800 != 0) ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9) >> 128; - if (absTick & 0x1000 != 0) ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825) >> 128; - if (absTick & 0x2000 != 0) ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5) >> 128; - if (absTick & 0x4000 != 0) ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7) >> 128; - if (absTick & 0x8000 != 0) ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6) >> 128; - if (absTick & 0x10000 != 0) ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9) >> 128; - if (absTick & 0x20000 != 0) ratio = (ratio * 0x5d6af8dedb81196699c329225ee604) >> 128; - if (absTick & 0x40000 != 0) ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98) >> 128; - if (absTick & 0x80000 != 0) ratio = (ratio * 0x48a170391f7dc42444e8fa2) >> 128; + if (absTick & 0x2 != 0) + ratio = (ratio * 0xfff97272373d413259a46990580e213a) >> 128; + if (absTick & 0x4 != 0) + ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdcc) >> 128; + if (absTick & 0x8 != 0) + ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0) >> 128; + if (absTick & 0x10 != 0) + ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644) >> 128; + if (absTick & 0x20 != 0) + ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0) >> 128; + if (absTick & 0x40 != 0) + ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861) >> 128; + if (absTick & 0x80 != 0) + ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053) >> 128; + if (absTick & 0x100 != 0) + ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4) >> 128; + if (absTick & 0x200 != 0) + ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54) >> 128; + if (absTick & 0x400 != 0) + ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3) >> 128; + if (absTick & 0x800 != 0) + ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9) >> 128; + if (absTick & 0x1000 != 0) + ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825) >> 128; + if (absTick & 0x2000 != 0) + ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5) >> 128; + if (absTick & 0x4000 != 0) + ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7) >> 128; + if (absTick & 0x8000 != 0) + ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6) >> 128; + if (absTick & 0x10000 != 0) + ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9) >> 128; + if (absTick & 0x20000 != 0) + ratio = (ratio * 0x5d6af8dedb81196699c329225ee604) >> 128; + if (absTick & 0x40000 != 0) + ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98) >> 128; + if (absTick & 0x80000 != 0) + ratio = (ratio * 0x48a170391f7dc42444e8fa2) >> 128; if (tick > 0) ratio = type(uint256).max / ratio; // This divides by 1<<32 rounding up to go from a Q128.128 to a Q128.96. @@ -274,11 +305,11 @@ library ConstantProduct { /// @return tick The greatest tick for which the ratio is less than or equal to the input ratio. function getTickAtPrice( uint160 price, - PoolsharkStructs.LimitImmutables memory constants + PoolsharkStructs.LimitImmutables memory constants ) internal pure returns (int24 tick) { // Second inequality must be < because the price can never reach the price at the max tick. if (price < constants.bounds.min || price > constants.bounds.max) - require (false, 'PriceOutOfBounds()'); + require(false, 'PriceOutOfBounds()'); uint256 ratio = uint256(price) << 32; uint256 r = ratio; @@ -415,11 +446,17 @@ library ConstantProduct { int256 log_sqrt10001 = log_2 * 255738958999603826347141; // 128.128 number - int24 tickLow = int24((log_sqrt10001 - 3402992956809132418596140100660247210) >> 128); - int24 tickHi = int24((log_sqrt10001 + 291339464771989622907027621153398088495) >> 128); + int24 tickLow = int24( + (log_sqrt10001 - 3402992956809132418596140100660247210) >> 128 + ); + int24 tickHi = int24( + (log_sqrt10001 + 291339464771989622907027621153398088495) >> 128 + ); - tick = tickLow == tickHi ? tickLow : getPriceAtTick(tickHi, constants) <= price + tick = tickLow == tickHi + ? tickLow + : getPriceAtTick(tickHi, constants) <= price ? tickHi : tickLow; } -} \ No newline at end of file +} diff --git a/contracts/libraries/math/OverflowMath.sol b/contracts/libraries/math/OverflowMath.sol index bfdc9dc8..567a6457 100644 --- a/contracts/libraries/math/OverflowMath.sol +++ b/contracts/libraries/math/OverflowMath.sol @@ -3,9 +3,12 @@ pragma solidity 0.8.18; /// @notice Math library that facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision. library OverflowMath { - // @dev no underflow or overflow checks - function divRoundingUp(uint256 x, uint256 y) internal pure returns (uint256 z) { + function divRoundingUp(uint256 x, uint256 y) + internal + pure + returns (uint256 z) + { assembly { z := add(div(x, y), gt(mod(x, y), 0)) } @@ -118,7 +121,8 @@ library OverflowMath { result = mulDiv(a, b, denominator); unchecked { if (mulmod(a, b, denominator) != 0) { - if (result >= type(uint256).max) require (false, 'MaxUintExceeded()'); + if (result >= type(uint256).max) + require(false, 'MaxUintExceeded()'); result++; } } diff --git a/contracts/libraries/pool/FeesCall.sol b/contracts/libraries/pool/FeesCall.sol index fd5349ca..e477f318 100644 --- a/contracts/libraries/pool/FeesCall.sol +++ b/contracts/libraries/pool/FeesCall.sol @@ -7,10 +7,9 @@ import '../../interfaces/limit/ILimitPoolManager.sol'; import '../utils/SafeTransfers.sol'; library FeesCall { - // protocol fee ceilings - uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; // max protocol swap fee of 100% - uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; // max protocol fill fee of 1% + uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; // max protocol swap fee of 100% + uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; // max protocol fill fee of 1% // protocol fee flags uint8 internal constant PROTOCOL_SWAP_FEE_0 = 2**0; @@ -27,10 +26,7 @@ library FeesCall { PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.FeesParams memory params, PoolsharkStructs.LimitImmutables memory constants - ) external returns ( - uint128 token0Fees, - uint128 token1Fees - ) { + ) external returns (uint128 token0Fees, uint128 token1Fees) { // swap fee token0 if ((params.setFeesFlags & PROTOCOL_SWAP_FEE_0) > 0) { if (params.protocolSwapFee0 > MAX_PROTOCOL_SWAP_FEE) diff --git a/contracts/libraries/pool/QuoteCall.sol b/contracts/libraries/pool/QuoteCall.sol index 09782920..f4cd81f7 100644 --- a/contracts/libraries/pool/QuoteCall.sol +++ b/contracts/libraries/pool/QuoteCall.sol @@ -24,20 +24,18 @@ library QuoteCall { PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.QuoteParams memory params, PoolsharkStructs.SwapCache memory cache - ) external view returns ( - uint256, - uint256, - uint160 - ) { + ) + external + view + returns ( + uint256, + uint256, + uint160 + ) + { if (cache.state.unlocked == _ENTERED) require(false, 'ReentrancyGuardReadOnlyReentrantCall()'); cache.state = globalState; - return Ticks.quote( - ticks, - rangeTickMap, - limitTickMap, - params, - cache - ); + return Ticks.quote(ticks, rangeTickMap, limitTickMap, params, cache); } } diff --git a/contracts/libraries/pool/SampleCall.sol b/contracts/libraries/pool/SampleCall.sol index 626ac183..cf939214 100644 --- a/contracts/libraries/pool/SampleCall.sol +++ b/contracts/libraries/pool/SampleCall.sol @@ -6,40 +6,43 @@ import '../Samples.sol'; library SampleCall { uint8 private constant _ENTERED = 2; - + event SampleRecorded( int56 tickSecondsAccum, uint160 secondsPerLiquidityAccum ); - event SampleLengthIncreased( - uint16 sampleLengthNext - ); + event SampleLengthIncreased(uint16 sampleLengthNext); function perform( PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, uint32[] memory secondsAgo - ) external view returns ( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ) { + ) + external + view + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ) + { if (state.unlocked == _ENTERED) require(false, 'ReentrancyGuardReadOnlyReentrantCall()'); - return Samples.get( - address(this), - RangePoolStructs.SampleParams( - state.pool.samples.index, - state.pool.samples.count, - uint32(block.timestamp), - secondsAgo, - state.pool.tickAtPrice, - state.pool.liquidity, - constants - ) - ); + return + Samples.get( + address(this), + RangePoolStructs.SampleParams( + state.pool.samples.index, + state.pool.samples.count, + uint32(block.timestamp), + secondsAgo, + state.pool.tickAtPrice, + state.pool.liquidity, + constants + ) + ); } } diff --git a/contracts/libraries/pool/SwapCall.sol b/contracts/libraries/pool/SwapCall.sol index fd1a7e61..f3005e81 100644 --- a/contracts/libraries/pool/SwapCall.sol +++ b/contracts/libraries/pool/SwapCall.sol @@ -28,17 +28,13 @@ library SwapCall { PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.SwapParams memory params, PoolsharkStructs.SwapCache memory cache - ) external returns ( - int256, - int256 - ) { + ) external returns (int256, int256) { // check for invalid receiver - if (params.to == address(0)) - require(false, "CollectToZeroAddress()"); - + if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); + // initialize state cache.state = globalState; - + // execute swap cache = Ticks.swap( ticks, @@ -54,9 +50,8 @@ library SwapCall { // transfer output amount SafeTransfers.transferOut( - params.to, - params.zeroForOne ? cache.constants.token1 - : cache.constants.token0, + params.to, + params.zeroForOne ? cache.constants.token1 : cache.constants.token0, cache.output ); @@ -70,19 +65,13 @@ library SwapCall { // check balance requirements after callback if (balance(params, cache) < balanceStart + cache.input) { - require(false, 'SwapInputAmountTooLow()'); + require(false, 'SwapInputAmountTooLow()'); } return ( - params.zeroForOne ? - ( - -int256(cache.input), - int256(cache.output) - ) - : ( - int256(cache.output), - -int256(cache.input) - ) + params.zeroForOne + ? (-int256(cache.input), int256(cache.output)) + : (int256(cache.output), -int256(cache.input)) ); } @@ -93,27 +82,22 @@ library SwapCall { ) internal { globalState.epoch = cache.state.epoch; globalState.pool = cache.state.pool; - if (zeroForOne) - globalState.pool1 = cache.state.pool1; - else - globalState.pool0 = cache.state.pool0; + if (zeroForOne) globalState.pool1 = cache.state.pool1; + else globalState.pool0 = cache.state.pool0; } function balance( PoolsharkStructs.SwapParams memory params, PoolsharkStructs.SwapCache memory cache ) private view returns (uint256) { - ( - bool success, - bytes memory data - ) = (params.zeroForOne ? cache.constants.token0 - : cache.constants.token1) - .staticcall( - abi.encodeWithSelector( - IERC20Minimal.balanceOf.selector, - address(this) - ) - ); + (bool success, bytes memory data) = ( + params.zeroForOne ? cache.constants.token0 : cache.constants.token1 + ).staticcall( + abi.encodeWithSelector( + IERC20Minimal.balanceOf.selector, + address(this) + ) + ); require(success && data.length >= 32); return abi.decode(data, (uint256)); } diff --git a/contracts/libraries/range/RangePositions.sol b/contracts/libraries/range/RangePositions.sol index 92b0b705..096afd0a 100644 --- a/contracts/libraries/range/RangePositions.sol +++ b/contracts/libraries/range/RangePositions.sol @@ -29,18 +29,19 @@ library RangePositions { int128 amount1 ); - event CompoundRange( - uint32 indexed positionId, - uint128 liquidityCompounded - ); + event CompoundRange(uint32 indexed positionId, uint128 liquidityCompounded); function validate( RangePoolStructs.MintRangeParams memory params, RangePoolStructs.MintRangeCache memory cache - ) internal pure returns ( - RangePoolStructs.MintRangeParams memory, - RangePoolStructs.MintRangeCache memory - ) { + ) + internal + pure + returns ( + RangePoolStructs.MintRangeParams memory, + RangePoolStructs.MintRangeCache memory + ) + { cache.liquidityMinted = ConstantProduct.getLiquidityForAmounts( cache.priceLower, cache.priceUpper, @@ -48,16 +49,20 @@ library RangePositions { params.amount1, params.amount0 ); - if (cache.liquidityMinted == 0) require(false, 'NoLiquidityBeingAdded()'); - (params.amount0, params.amount1) = ConstantProduct.getAmountsForLiquidity( - cache.priceLower, - cache.priceUpper, - cache.state.pool.price, - cache.liquidityMinted, - true - ); - if (cache.state.liquidityGlobal + cache.liquidityMinted > uint128(type(int128).max)) - require(false, 'LiquidityOverflow()'); + if (cache.liquidityMinted == 0) + require(false, 'NoLiquidityBeingAdded()'); + (params.amount0, params.amount1) = ConstantProduct + .getAmountsForLiquidity( + cache.priceLower, + cache.priceUpper, + cache.state.pool.price, + cache.liquidityMinted, + true + ); + if ( + cache.state.liquidityGlobal + cache.liquidityMinted > + uint128(type(int128).max) + ) require(false, 'LiquidityOverflow()'); return (params, cache); } @@ -68,9 +73,7 @@ library RangePositions { PoolsharkStructs.TickMap storage tickMap, RangePoolStructs.MintRangeCache memory cache, RangePoolStructs.MintRangeParams memory params - ) internal returns ( - RangePoolStructs.MintRangeCache memory - ) { + ) internal returns (RangePoolStructs.MintRangeCache memory) { if (params.amount0 == 0 && params.amount1 == 0) return cache; cache.state = RangeTicks.insert( @@ -111,25 +114,35 @@ library RangePositions { PoolsharkStructs.TickMap storage tickMap, RangePoolStructs.BurnRangeParams memory params, RangePoolStructs.BurnRangeCache memory cache - ) internal returns ( - RangePoolStructs.BurnRangeCache memory - ) { - cache.priceLower = ConstantProduct.getPriceAtTick(cache.position.lower, cache.constants); - cache.priceUpper = ConstantProduct.getPriceAtTick(cache.position.upper, cache.constants); - cache.liquidityBurned = _convert(cache.position.liquidity, params.burnPercent); - if (cache.liquidityBurned == 0) { + ) internal returns (RangePoolStructs.BurnRangeCache memory) { + cache.priceLower = ConstantProduct.getPriceAtTick( + cache.position.lower, + cache.constants + ); + cache.priceUpper = ConstantProduct.getPriceAtTick( + cache.position.upper, + cache.constants + ); + cache.liquidityBurned = _convert( + cache.position.liquidity, + params.burnPercent + ); + if (cache.liquidityBurned == 0) { return cache; } - if (cache.liquidityBurned > cache.position.liquidity) require(false, 'NotEnoughPositionLiquidity()'); + if (cache.liquidityBurned > cache.position.liquidity) + require(false, 'NotEnoughPositionLiquidity()'); { - uint128 amount0Removed; uint128 amount1Removed; - (amount0Removed, amount1Removed) = ConstantProduct.getAmountsForLiquidity( - cache.priceLower, - cache.priceUpper, - cache.state.pool.price, - cache.liquidityBurned , - false - ); + uint128 amount0Removed; + uint128 amount1Removed; + (amount0Removed, amount1Removed) = ConstantProduct + .getAmountsForLiquidity( + cache.priceLower, + cache.priceUpper, + cache.state.pool.price, + cache.liquidityBurned, + false + ); cache.amount0 += amount0Removed.toInt128(); cache.amount1 += amount1Removed.toInt128(); cache.position.liquidity -= cache.liquidityBurned.toUint128(); @@ -166,12 +179,15 @@ library RangePositions { PoolsharkStructs.LimitImmutables memory constants, RangePoolStructs.RangePosition memory position, RangePoolStructs.CompoundRangeParams memory params - ) internal returns ( - RangePoolStructs.RangePosition memory, - PoolsharkStructs.GlobalState memory, - int128, - int128 - ) { + ) + internal + returns ( + RangePoolStructs.RangePosition memory, + PoolsharkStructs.GlobalState memory, + int128, + int128 + ) + { // price tells you the ratio so you need to swap into the correct ratio and add liquidity uint256 liquidityAmount = ConstantProduct.getLiquidityForAmounts( params.priceLower, @@ -191,7 +207,8 @@ library RangePositions { position.upper, uint128(liquidityAmount) ); - uint256 amount0; uint256 amount1; + uint256 amount0; + uint256 amount1; (amount0, amount1) = ConstantProduct.getAmountsForLiquidity( params.priceLower, params.priceUpper, @@ -199,15 +216,21 @@ library RangePositions { liquidityAmount, true ); - params.amount0 -= (amount0 <= params.amount0) ? uint128(amount0) : params.amount0; - params.amount1 -= (amount1 <= params.amount1) ? uint128(amount1) : params.amount1; + params.amount0 -= (amount0 <= params.amount0) + ? uint128(amount0) + : params.amount0; + params.amount1 -= (amount1 <= params.amount1) + ? uint128(amount1) + : params.amount1; position.liquidity += uint128(liquidityAmount); } - emit CompoundRange( - params.positionId, - uint128(liquidityAmount) + emit CompoundRange(params.positionId, uint128(liquidityAmount)); + return ( + position, + state, + params.amount0.toInt128(), + params.amount1.toInt128() ); - return (position, state, params.amount0.toInt128(), params.amount1.toInt128()); } function update( @@ -216,17 +239,28 @@ library RangePositions { PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, RangePoolStructs.UpdateParams memory params - ) internal returns ( - RangePoolStructs.RangePosition memory, - int128, - int128 - ) { + ) + internal + returns ( + RangePoolStructs.RangePosition memory, + int128, + int128 + ) + { RangePoolStructs.RangePositionCache memory cache; /// @dev - only true if burn call if (params.burnPercent > 0) { - cache.liquidityAmount = _convert(position.liquidity, params.burnPercent); + cache.liquidityAmount = _convert( + position.liquidity, + params.burnPercent + ); if (position.liquidity == cache.liquidityAmount) - IPositionERC1155(constants.poolToken).burn(msg.sender, params.positionId, 1, constants); + IPositionERC1155(constants.poolToken).burn( + msg.sender, + params.positionId, + 1, + constants + ); } (uint256 rangeFeeGrowth0, uint256 rangeFeeGrowth1) = rangeFeeGrowth( @@ -237,17 +271,23 @@ library RangePositions { position.upper ); - int128 amount0Fees = OverflowMath.mulDiv( - rangeFeeGrowth0 - position.feeGrowthInside0Last, - uint256(position.liquidity), - Q128 - ).toInt256().toInt128(); + int128 amount0Fees = OverflowMath + .mulDiv( + rangeFeeGrowth0 - position.feeGrowthInside0Last, + uint256(position.liquidity), + Q128 + ) + .toInt256() + .toInt128(); - int128 amount1Fees = OverflowMath.mulDiv( - rangeFeeGrowth1 - position.feeGrowthInside1Last, - position.liquidity, - Q128 - ).toInt256().toInt128(); + int128 amount1Fees = OverflowMath + .mulDiv( + rangeFeeGrowth1 - position.feeGrowthInside1Last, + position.liquidity, + Q128 + ) + .toInt256() + .toInt128(); position.feeGrowthInside0Last = rangeFeeGrowth0; position.feeGrowthInside1Last = rangeFeeGrowth1; @@ -261,8 +301,11 @@ library RangePositions { PoolsharkStructs.GlobalState memory state, int24 lower, int24 upper - ) internal pure returns (uint256 feeGrowthInside0, uint256 feeGrowthInside1) { - + ) + internal + pure + returns (uint256 feeGrowthInside0, uint256 feeGrowthInside1) + { uint256 feeGrowthGlobal0 = state.pool.feeGrowthGlobal0; uint256 feeGrowthGlobal1 = state.pool.feeGrowthGlobal1; @@ -290,41 +333,49 @@ library RangePositions { } function snapshot( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, mapping(int24 => PoolsharkStructs.Tick) storage ticks, PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, uint32 positionId - ) internal view returns ( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum, - uint128 feesOwed0, - uint128 feesOwed1 - ) { + ) + internal + view + returns ( + int56 tickSecondsAccum, + uint160 secondsPerLiquidityAccum, + uint128 feesOwed0, + uint128 feesOwed1 + ) + { RangePoolStructs.SnapshotRangeCache memory cache; cache.position = positions[positionId]; // early return if position empty - if (cache.position.liquidity == 0) - return (0,0,0,0); + if (cache.position.liquidity == 0) return (0, 0, 0, 0); cache.price = state.pool.price; cache.liquidity = state.pool.liquidity; cache.samples = state.pool.samples; // grab lower tick - PoolsharkStructs.RangeTick memory tickLower = ticks[cache.position.lower].range; - + PoolsharkStructs.RangeTick memory tickLower = ticks[ + cache.position.lower + ].range; + // grab upper tick - PoolsharkStructs.RangeTick memory tickUpper = ticks[cache.position.upper].range; + PoolsharkStructs.RangeTick memory tickUpper = ticks[ + cache.position.upper + ].range; - cache.tickSecondsAccumLower = tickLower.tickSecondsAccumOutside; - cache.secondsPerLiquidityAccumLower = tickLower.secondsPerLiquidityAccumOutside; + cache.tickSecondsAccumLower = tickLower.tickSecondsAccumOutside; + cache.secondsPerLiquidityAccumLower = tickLower + .secondsPerLiquidityAccumOutside; // if both have never been crossed into return 0 cache.tickSecondsAccumUpper = tickUpper.tickSecondsAccumOutside; - cache.secondsPerLiquidityAccumUpper = tickUpper.secondsPerLiquidityAccumOutside; + cache.secondsPerLiquidityAccumUpper = tickUpper + .secondsPerLiquidityAccumOutside; cache.constants = constants; (uint256 rangeFeeGrowth0, uint256 rangeFeeGrowth1) = rangeFeeGrowth( @@ -357,36 +408,35 @@ library RangePositions { // lower accum values are greater return ( cache.tickSecondsAccumLower - cache.tickSecondsAccumUpper, - cache.secondsPerLiquidityAccumLower - cache.secondsPerLiquidityAccumUpper, + cache.secondsPerLiquidityAccumLower - + cache.secondsPerLiquidityAccumUpper, cache.amount0, cache.amount1 ); } else if (cache.position.upper >= cache.tick) { // grab current sample cache.blockTimestamp = uint32(block.timestamp); - ( - cache.tickSecondsAccum, - cache.secondsPerLiquidityAccum - ) = Samples.getSingle( - IRangePool(address(this)), - RangePoolStructs.SampleParams( - cache.samples.index, - cache.samples.count, - uint32(block.timestamp), - new uint32[](2), - cache.tick, - cache.liquidity, - cache.constants - ), - 0 - ); + (cache.tickSecondsAccum, cache.secondsPerLiquidityAccum) = Samples + .getSingle( + IRangePool(address(this)), + RangePoolStructs.SampleParams( + cache.samples.index, + cache.samples.count, + uint32(block.timestamp), + new uint32[](2), + cache.tick, + cache.liquidity, + cache.constants + ), + 0 + ); return ( - cache.tickSecondsAccum - - cache.tickSecondsAccumLower - - cache.tickSecondsAccumUpper, - cache.secondsPerLiquidityAccum - - cache.secondsPerLiquidityAccumLower - - cache.secondsPerLiquidityAccumUpper, + cache.tickSecondsAccum - + cache.tickSecondsAccumLower - + cache.tickSecondsAccumUpper, + cache.secondsPerLiquidityAccum - + cache.secondsPerLiquidityAccumLower - + cache.secondsPerLiquidityAccumUpper, cache.amount0, cache.amount1 ); @@ -394,22 +444,22 @@ library RangePositions { // upper accum values are greater return ( cache.tickSecondsAccumUpper - cache.tickSecondsAccumLower, - cache.secondsPerLiquidityAccumUpper - cache.secondsPerLiquidityAccumLower, + cache.secondsPerLiquidityAccumUpper - + cache.secondsPerLiquidityAccumLower, cache.amount0, cache.amount1 ); } } - function _convert( - uint128 liquidity, - uint128 percent - ) internal pure returns ( - uint128 - ) { + function _convert(uint128 liquidity, uint128 percent) + internal + pure + returns (uint128) + { // convert percentage to liquidity amount if (percent > 1e38) percent = 1e38; - if (liquidity == 0 && percent > 0) require (false, 'PositionNotFound()'); - return uint128(uint256(liquidity) * uint256(percent) / 1e38); + if (liquidity == 0 && percent > 0) require(false, 'PositionNotFound()'); + return uint128((uint256(liquidity) * uint256(percent)) / 1e38); } } diff --git a/contracts/libraries/range/RangeTicks.sol b/contracts/libraries/range/RangeTicks.sol index ed9f7dd1..b3644e83 100644 --- a/contracts/libraries/range/RangeTicks.sol +++ b/contracts/libraries/range/RangeTicks.sol @@ -14,7 +14,6 @@ import '../Samples.sol'; /// @notice Tick management library for range pools library RangeTicks { - event SyncRangeTick( uint200 feeGrowthOutside0, uint200 feeGrowthOutside1, @@ -30,9 +29,11 @@ library RangeTicks { int16 tickSpacing ) internal pure { if (lower % tickSpacing != 0) require(false, 'InvalidLowerTick()'); - if (lower < ConstantProduct.minTick(tickSpacing)) require(false, 'InvalidLowerTick()'); + if (lower < ConstantProduct.minTick(tickSpacing)) + require(false, 'InvalidLowerTick()'); if (upper % tickSpacing != 0) require(false, 'InvalidUpperTick()'); - if (upper > ConstantProduct.maxTick(tickSpacing)) require(false, 'InvalidUpperTick()'); + if (upper > ConstantProduct.maxTick(tickSpacing)) + require(false, 'InvalidUpperTick()'); if (lower >= upper) require(false, 'InvalidPositionBounds()'); } @@ -46,11 +47,10 @@ library RangeTicks { int24 upper, uint128 amount ) internal returns (PoolsharkStructs.GlobalState memory) { - // get tick at price int24 tickAtPrice = state.pool.tickAtPrice; - if(TickMap.set(tickMap, lower, constants.tickSpacing)) { + if (TickMap.set(tickMap, lower, constants.tickSpacing)) { ticks[lower].range.liquidityDelta += int128(amount); ticks[lower].range.liquidityAbsolute += amount; } else { @@ -59,7 +59,7 @@ library RangeTicks { int56 tickSecondsAccum, uint160 secondsPerLiquidityAccum ) = Samples.getSingle( - IRangePool(address(this)), + IRangePool(address(this)), RangePoolStructs.SampleParams( state.pool.samples.index, state.pool.samples.count, @@ -70,14 +70,14 @@ library RangeTicks { constants ), 0 - ); + ); ticks[lower].range = PoolsharkStructs.RangeTick( state.pool.feeGrowthGlobal0, state.pool.feeGrowthGlobal1, secondsPerLiquidityAccum, tickSecondsAccum, - int128(amount), // liquidityDelta - amount // liquidityAbsolute + int128(amount), // liquidityDelta + amount // liquidityAbsolute ); emit SyncRangeTick( state.pool.feeGrowthGlobal0, @@ -89,17 +89,16 @@ library RangeTicks { ticks[lower].range.liquidityAbsolute += amount; } } - if(TickMap.set(tickMap, upper, constants.tickSpacing)) { + if (TickMap.set(tickMap, upper, constants.tickSpacing)) { ticks[upper].range.liquidityDelta -= int128(amount); ticks[upper].range.liquidityAbsolute += amount; } else { if (upper <= tickAtPrice) { - ( int56 tickSecondsAccum, uint160 secondsPerLiquidityAccum ) = Samples.getSingle( - IRangePool(address(this)), + IRangePool(address(this)), RangePoolStructs.SampleParams( state.pool.samples.index, state.pool.samples.count, @@ -110,7 +109,7 @@ library RangeTicks { constants ), 0 - ); + ); ticks[upper].range = PoolsharkStructs.RangeTick( state.pool.feeGrowthGlobal0, state.pool.feeGrowthGlobal1, @@ -151,7 +150,7 @@ library RangeTicks { RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.GlobalState memory state, - PoolsharkStructs.LimitImmutables memory constants, + PoolsharkStructs.LimitImmutables memory constants, int24 lower, int24 upper, uint128 amount @@ -159,8 +158,10 @@ library RangeTicks { validate(lower, upper, constants.tickSpacing); //check for amount to overflow liquidity delta & global if (amount == 0) return state; - if (amount > uint128(type(int128).max)) require(false, 'LiquidityUnderflow()'); - if (amount > state.liquidityGlobal) require(false, 'LiquidityUnderflow()'); + if (amount > uint128(type(int128).max)) + require(false, 'LiquidityUnderflow()'); + if (amount > state.liquidityGlobal) + require(false, 'LiquidityUnderflow()'); // get pool tick at price int24 tickAtPrice = state.pool.tickAtPrice; @@ -193,7 +194,7 @@ library RangeTicks { state.pool.liquidity, tickAtPrice ); - state.pool.liquidity -= amount; + state.pool.liquidity -= amount; } state.liquidityGlobal -= amount; @@ -207,19 +208,28 @@ library RangeTicks { int24 tickToClear ) internal { if (_empty(ticks[tickToClear])) { - if (tickToClear != ConstantProduct.maxTick(constants.tickSpacing) && - tickToClear != ConstantProduct.minTick(constants.tickSpacing)) { - ticks[tickToClear].range = PoolsharkStructs.RangeTick(0,0,0,0,0,0); + if ( + tickToClear != ConstantProduct.maxTick(constants.tickSpacing) && + tickToClear != ConstantProduct.minTick(constants.tickSpacing) + ) { + ticks[tickToClear].range = PoolsharkStructs.RangeTick( + 0, + 0, + 0, + 0, + 0, + 0 + ); TickMap.unset(tickMap, tickToClear, constants.tickSpacing); } } } - function _empty( - LimitPoolStructs.Tick memory tick - ) internal pure returns ( - bool - ) { + function _empty(LimitPoolStructs.Tick memory tick) + internal + pure + returns (bool) + { return tick.range.liquidityAbsolute == 0; } } diff --git a/contracts/libraries/range/math/FeeMath.sol b/contracts/libraries/range/math/FeeMath.sol index e099f1e1..0c08a2fa 100644 --- a/contracts/libraries/range/math/FeeMath.sol +++ b/contracts/libraries/range/math/FeeMath.sol @@ -3,10 +3,10 @@ pragma solidity 0.8.18; import '../../Samples.sol'; import '../../utils/SafeCast.sol'; -import "../../math/OverflowMath.sol"; +import '../../math/OverflowMath.sol'; import '../../../interfaces/limit/ILimitPoolManager.sol'; import '../../../interfaces/structs/PoolsharkStructs.sol'; -import "../../../interfaces/structs/RangePoolStructs.sol"; +import '../../../interfaces/structs/RangePoolStructs.sol'; /// @notice Math library that facilitates fee handling. library FeeMath { @@ -31,15 +31,14 @@ library FeeMath { uint256 amountIn, uint256 amountOut, bool zeroForOne - ) internal view returns ( - PoolsharkStructs.SwapCache memory - ) - { + ) internal view returns (PoolsharkStructs.SwapCache memory) { CalculateLocals memory locals; if (cache.state.pool.liquidity != 0) { // calculate dynamic fee { - locals.minPrice = ConstantProduct.getPrice(cache.constants.bounds.min); + locals.minPrice = ConstantProduct.getPrice( + cache.constants.bounds.min + ); // square prices to take delta locals.price = ConstantProduct.getPrice(cache.price); locals.lastPrice = ConstantProduct.getPrice(cache.averagePrice); @@ -49,14 +48,14 @@ library FeeMath { locals.lastPrice = locals.minPrice; // delta is % modifier on the swapFee uint256 delta = OverflowMath.mulDiv( - ILimitPoolManager(cache.constants.owner).feeDeltaConst() // higher feeDeltaConst means - / uint16(cache.constants.tickSpacing), // more aggressive dynamic fee - ( - locals.price > locals.lastPrice - ? locals.price - locals.lastPrice - : locals.lastPrice - locals.price - ) * 1_000_000, - locals.lastPrice + ILimitPoolManager(cache.constants.owner).feeDeltaConst() / // higher feeDeltaConst means + uint16(cache.constants.tickSpacing), // more aggressive dynamic fee + ( + locals.price > locals.lastPrice + ? locals.price - locals.lastPrice + : locals.lastPrice - locals.price + ) * 1_000_000, + locals.lastPrice ); // max fee increase at 5x if (delta > 4_000_000) delta = 4_000_000; @@ -65,55 +64,100 @@ library FeeMath { // adjust fee based on direction if (zeroForOne == locals.feeDirection) { // if swapping away from twap price, increase fee - locals.swapFee = cache.constants.swapFee + OverflowMath.mulDiv(delta,cache.constants.swapFee, 1e6); + locals.swapFee = + cache.constants.swapFee + + OverflowMath.mulDiv( + delta, + cache.constants.swapFee, + 1e6 + ); } else if (delta < 1e6) { // if swapping towards twap price, decrease fee - locals.swapFee = cache.constants.swapFee - OverflowMath.mulDiv(delta,cache.constants.swapFee, 1e6); + locals.swapFee = + cache.constants.swapFee - + OverflowMath.mulDiv( + delta, + cache.constants.swapFee, + 1e6 + ); } else { // if swapping towards twap price and delta > 100%, set fee to zero locals.swapFee = 0; } - // console.log('price movement', locals.lastPrice, locals.price); - // console.log('swap fee adjustment',cache.constants.swapFee + delta * cache.constants.swapFee / 1e6); } if (cache.exactIn) { // calculate output from range liquidity - locals.amountRange = OverflowMath.mulDiv(amountOut, cache.state.pool.liquidity, cache.liquidity); + locals.amountRange = OverflowMath.mulDiv( + amountOut, + cache.state.pool.liquidity, + cache.liquidity + ); // take enough fees to cover fee growth - locals.feeAmount = OverflowMath.mulDivRoundingUp(locals.amountRange, locals.swapFee, 1e6); + locals.feeAmount = OverflowMath.mulDivRoundingUp( + locals.amountRange, + locals.swapFee, + 1e6 + ); amountOut -= locals.feeAmount; } else { // calculate input from range liquidity - locals.amountRange = OverflowMath.mulDiv(amountIn, cache.state.pool.liquidity, cache.liquidity); + locals.amountRange = OverflowMath.mulDiv( + amountIn, + cache.state.pool.liquidity, + cache.liquidity + ); // take enough fees to cover fee growth - locals.feeAmount = OverflowMath.mulDivRoundingUp(locals.amountRange, locals.swapFee, 1e6); + locals.feeAmount = OverflowMath.mulDivRoundingUp( + locals.amountRange, + locals.swapFee, + 1e6 + ); amountIn += locals.feeAmount; } // add to total fees paid for swap cache.feeAmount += locals.feeAmount.toUint128(); // load protocol fee from cache - // zeroForOne && exactIn = fee on token1 // zeroForOne && !exactIn = fee on token0 - // !zeroForOne && !exactIn = fee on token1 - // !zeroForOne && exactIn = fee on token0 - locals.protocolFee = (zeroForOne == cache.exactIn) ? cache.state.pool.protocolSwapFee1 - : cache.state.pool.protocolSwapFee0; + // zeroForOne && exactIn = fee on token1 + locals.protocolFee = (zeroForOne == cache.exactIn) + ? cache.state.pool.protocolSwapFee1 + : cache.state.pool.protocolSwapFee0; // calculate fee - locals.protocolFeesAccrued = OverflowMath.mulDiv(locals.feeAmount, locals.protocolFee, 1e4); + locals.protocolFeesAccrued = OverflowMath.mulDiv( + locals.feeAmount, + locals.protocolFee, + 1e4 + ); // fees for this swap step locals.feeAmount -= locals.protocolFeesAccrued; // save fee growth and protocol fees if (zeroForOne == cache.exactIn) { - cache.state.pool0.protocolFees += uint128(locals.protocolFeesAccrued); - cache.state.pool.feeGrowthGlobal1 += uint200(OverflowMath.mulDiv(locals.feeAmount, Q128, cache.state.pool.liquidity)); + cache.state.pool0.protocolFees += uint128( + locals.protocolFeesAccrued + ); + cache.state.pool.feeGrowthGlobal1 += uint200( + OverflowMath.mulDiv( + locals.feeAmount, + Q128, + cache.state.pool.liquidity + ) + ); } else { - cache.state.pool1.protocolFees += uint128(locals.protocolFeesAccrued); - cache.state.pool.feeGrowthGlobal0 += uint200(OverflowMath.mulDiv(locals.feeAmount, Q128, cache.state.pool.liquidity)); + cache.state.pool1.protocolFees += uint128( + locals.protocolFeesAccrued + ); + cache.state.pool.feeGrowthGlobal0 += uint200( + OverflowMath.mulDiv( + locals.feeAmount, + Q128, + cache.state.pool.liquidity + ) + ); } } - cache.input += amountIn; + cache.input += amountIn; cache.output += amountOut; return cache; } -} \ No newline at end of file +} diff --git a/contracts/libraries/range/pool/BurnRangeCall.sol b/contracts/libraries/range/pool/BurnRangeCall.sol index 732ae629..ecea2a48 100644 --- a/contracts/libraries/range/pool/BurnRangeCall.sol +++ b/contracts/libraries/range/pool/BurnRangeCall.sol @@ -20,55 +20,49 @@ library BurnRangeCall { ); function perform( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, mapping(int24 => PoolsharkStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.GlobalState storage globalState, RangePoolStructs.BurnRangeCache memory cache, RangePoolStructs.BurnRangeParams memory params - ) external returns ( - int256, - int256 ) + external + returns ( + int256, // amount0Delta + int256 // amount1Delta + ) { // check for invalid receiver - if (params.to == address(0)) - require(false, "CollectToZeroAddress()"); - + if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); + // initialize cache cache.state = globalState; cache.position = positions[params.positionId]; - if (cache.position.liquidity == 0) - require(false, 'PositionNotFound()'); - if (PositionTokens.balanceOf(cache.constants, msg.sender, params.positionId) == 0) - require(false, 'PositionOwnerMismatch()'); - - ( - cache.position, - cache.amount0, - cache.amount1 - ) = RangePositions.update( - ticks, - cache.position, - cache.state, + if (cache.position.liquidity == 0) require(false, 'PositionNotFound()'); + if ( + PositionTokens.balanceOf( cache.constants, - RangePoolStructs.UpdateParams( - cache.position.lower, - cache.position.upper, - params.positionId, - params.burnPercent - ) - ); - cache = RangePositions.remove( + msg.sender, + params.positionId + ) == 0 + ) require(false, 'PositionOwnerMismatch()'); + + (cache.position, cache.amount0, cache.amount1) = RangePositions.update( ticks, - samples, - tickMap, - params, - cache + cache.position, + cache.state, + cache.constants, + RangePoolStructs.UpdateParams( + cache.position.lower, + cache.position.upper, + params.positionId, + params.burnPercent + ) ); + cache = RangePositions.remove(ticks, samples, tickMap, params, cache); // only compound if burnPercent is zero if (params.burnPercent == 0) if (cache.amount0 > 0 || cache.amount1 > 0) { @@ -106,15 +100,11 @@ library BurnRangeCall { ); // return amount deltas - return ( - cache.amount0, - cache.amount1 - ); + return (cache.amount0, cache.amount1); } function save( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, PoolsharkStructs.GlobalState storage globalState, RangePoolStructs.BurnRangeCache memory cache, uint32 positionId diff --git a/contracts/libraries/range/pool/MintRangeCall.sol b/contracts/libraries/range/pool/MintRangeCall.sol index 7aa99b2c..9654f489 100644 --- a/contracts/libraries/range/pool/MintRangeCall.sol +++ b/contracts/libraries/range/pool/MintRangeCall.sol @@ -30,25 +30,29 @@ library MintRangeCall { } function perform( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, mapping(int24 => PoolsharkStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.GlobalState storage globalState, RangePoolStructs.MintRangeCache memory cache, RangePoolStructs.MintRangeParams memory params - ) external returns ( - int256, // amount0Delta - int256 // amount1Delta ) + external + returns ( + int256, // amount0Delta + int256 // amount1Delta + ) { // check for invalid receiver - if (params.to == address(0)) - require(false, "CollectToZeroAddress()"); + if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); // validate position ticks - ConstantProduct.checkTicks(params.lower, params.upper, cache.constants.tickSpacing); + ConstantProduct.checkTicks( + params.lower, + params.upper, + cache.constants.tickSpacing + ); cache.state = globalState; @@ -58,8 +62,13 @@ library MintRangeCall { cache.position = positions[params.positionId]; if (cache.position.liquidity == 0) require(false, 'PositionNotFound()'); - if (PositionTokens.balanceOf(cache.constants, params.to, params.positionId) == 0) - require(false, 'PositionOwnerMismatch()'); + if ( + PositionTokens.balanceOf( + cache.constants, + params.to, + params.positionId + ) == 0 + ) require(false, 'PositionOwnerMismatch()'); // existing position cache.owner = params.to; // set bounds as defined by position @@ -71,16 +80,16 @@ library MintRangeCall { cache.feesAccrued0, cache.feesAccrued1 ) = RangePositions.update( - ticks, - cache.position, - cache.state, - cache.constants, - RangePoolStructs.UpdateParams( - params.lower, - params.upper, - params.positionId, - 0 - ) + ticks, + cache.position, + cache.state, + cache.constants, + RangePoolStructs.UpdateParams( + params.lower, + params.upper, + params.positionId, + 0 + ) ); } else { // create a new position @@ -93,8 +102,14 @@ library MintRangeCall { cache.owner = params.to; } // set cache based on bounds - cache.priceLower = ConstantProduct.getPriceAtTick(cache.position.lower, cache.constants); - cache.priceUpper = ConstantProduct.getPriceAtTick(cache.position.upper, cache.constants); + cache.priceLower = ConstantProduct.getPriceAtTick( + cache.position.lower, + cache.constants + ); + cache.priceUpper = ConstantProduct.getPriceAtTick( + cache.position.upper, + cache.constants + ); // validate input amounts (params, cache) = RangePositions.validate(params, cache); @@ -111,17 +126,11 @@ library MintRangeCall { params.positionId, cache.liquidityMinted.toUint128(), -(cache.amount0 + cache.feesAccrued0), /// @dev - emit token0 balance delta - -(cache.amount1 + cache.feesAccrued1) /// @dev - emit token1 balance delta + -(cache.amount1 + cache.feesAccrued1) /// @dev - emit token1 balance delta ); // update position with latest fees accrued - cache = RangePositions.add( - ticks, - samples, - tickMap, - cache, - params - ); + cache = RangePositions.add(ticks, samples, tickMap, cache, params); // save changes to storage before transfer out save(positions, globalState, cache, params.positionId); @@ -149,11 +158,15 @@ library MintRangeCall { // check balance after callback if (cache.amount0 < 0) - if (balance0(cache) < startBalance.amount0 + (-cache.amount0).toUint128()) - require(false, 'MintInputAmount0TooLow()'); + if ( + balance0(cache) < + startBalance.amount0 + (-cache.amount0).toUint128() + ) require(false, 'MintInputAmount0TooLow()'); if (cache.amount1 < 0) - if (balance1(cache) < startBalance.amount1 + (-cache.amount1).toUint128()) - require(false, 'MintInputAmount1TooLow()'); + if ( + balance1(cache) < + startBalance.amount1 + (-cache.amount1).toUint128() + ) require(false, 'MintInputAmount1TooLow()'); return ( cache.amount0 + cache.feesAccrued0, @@ -162,8 +175,7 @@ library MintRangeCall { } function save( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, PoolsharkStructs.GlobalState storage globalState, RangePoolStructs.MintRangeCache memory cache, uint32 positionId @@ -174,36 +186,32 @@ library MintRangeCall { globalState.positionIdNext = cache.state.positionIdNext; } - function balance0( - RangePoolStructs.MintRangeCache memory cache - ) private view returns (uint256) { - ( - bool success, - bytes memory data - ) = (cache.constants.token0) - .staticcall( - abi.encodeWithSelector( - IERC20Minimal.balanceOf.selector, - address(this) - ) - ); + function balance0(RangePoolStructs.MintRangeCache memory cache) + private + view + returns (uint256) + { + (bool success, bytes memory data) = (cache.constants.token0).staticcall( + abi.encodeWithSelector( + IERC20Minimal.balanceOf.selector, + address(this) + ) + ); require(success && data.length >= 32); return abi.decode(data, (uint256)); } - function balance1( - RangePoolStructs.MintRangeCache memory cache - ) private view returns (uint256) { - ( - bool success, - bytes memory data - ) = (cache.constants.token1) - .staticcall( - abi.encodeWithSelector( - IERC20Minimal.balanceOf.selector, - address(this) - ) - ); + function balance1(RangePoolStructs.MintRangeCache memory cache) + private + view + returns (uint256) + { + (bool success, bytes memory data) = (cache.constants.token1).staticcall( + abi.encodeWithSelector( + IERC20Minimal.balanceOf.selector, + address(this) + ) + ); require(success && data.length >= 32); return abi.decode(data, (uint256)); } diff --git a/contracts/libraries/range/pool/SnapshotRangeCall.sol b/contracts/libraries/range/pool/SnapshotRangeCall.sol index 0b9c2fe2..701cd74c 100644 --- a/contracts/libraries/range/pool/SnapshotRangeCall.sol +++ b/contracts/libraries/range/pool/SnapshotRangeCall.sol @@ -19,27 +19,30 @@ library SnapshotRangeCall { ); function perform( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, mapping(int24 => PoolsharkStructs.Tick) storage ticks, PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, uint32 positionId - ) external view returns ( - int56, - uint160, - uint128, - uint128 ) + external + view + returns ( + int56, + uint160, + uint128, + uint128 + ) { if (state.unlocked == _ENTERED) require(false, 'ReentrancyGuardReadOnlyReentrantCall()'); - return RangePositions.snapshot( - positions, - ticks, - state, - constants, - positionId - ); + return + RangePositions.snapshot( + positions, + ticks, + state, + constants, + positionId + ); } } diff --git a/contracts/libraries/utils/Bytes.sol b/contracts/libraries/utils/Bytes.sol index 6906498a..84bc6e5d 100644 --- a/contracts/libraries/utils/Bytes.sol +++ b/contracts/libraries/utils/Bytes.sol @@ -2,7 +2,7 @@ pragma solidity 0.8.18; library Bytes { - bytes16 private constant alphabet = "0123456789abcdef"; + bytes16 private constant alphabet = '0123456789abcdef'; function from(string memory source) internal pure returns (bytes32 result) { bytes memory tempEmptyStringTest = bytes(source); @@ -15,9 +15,13 @@ library Bytes { } } - function bytes32ToString(bytes32 _bytes32) internal pure returns (string memory) { + function bytes32ToString(bytes32 _bytes32) + internal + pure + returns (string memory) + { uint8 i = 0; - while(i < 32 && _bytes32[i] != 0) { + while (i < 32 && _bytes32[i] != 0) { i++; } bytes memory bytesArray = new bytes(i); @@ -26,4 +30,4 @@ library Bytes { } return string(bytesArray); } -} \ No newline at end of file +} diff --git a/contracts/libraries/utils/Collect.sol b/contracts/libraries/utils/Collect.sol index 5c8bb3b6..9cd22f45 100644 --- a/contracts/libraries/utils/Collect.sol +++ b/contracts/libraries/utils/Collect.sol @@ -9,13 +9,9 @@ library Collect { using SafeCast for int128; using SafeCast for uint128; - event CollectRange0( - uint128 amount0 - ); + event CollectRange0(uint128 amount0); - event CollectRange1( - uint128 amount1 - ); + event CollectRange1(uint128 amount1); function range( PoolsharkStructs.LimitImmutables memory constants, @@ -26,25 +22,34 @@ library Collect { /// @dev - negative balances will revert if (amount0 > 0) { /// @dev - cast to ensure user doesn't owe the pool balance - SafeTransfers.transferOut(recipient, constants.token0, amount0.toUint128()); + SafeTransfers.transferOut( + recipient, + constants.token0, + amount0.toUint128() + ); emit CollectRange0(amount0.toUint128()); } if (amount1 > 0) { /// @dev - cast to ensure user doesn't owe the pool balance - SafeTransfers.transferOut(recipient, constants.token1, amount1.toUint128()); + SafeTransfers.transferOut( + recipient, + constants.token1, + amount1.toUint128() + ); emit CollectRange1(amount1.toUint128()); } - } function burnLimit( LimitPoolStructs.BurnLimitCache memory cache, PoolsharkStructs.BurnLimitParams memory params - ) internal returns ( - LimitPoolStructs.BurnLimitCache memory, - int128 amount0Delta, - int128 amount1Delta - ) + ) + internal + returns ( + LimitPoolStructs.BurnLimitCache memory, + int128 amount0Delta, + int128 amount1Delta + ) { uint128 amount0 = params.zeroForOne ? cache.amountOut : cache.amountIn; uint128 amount1 = params.zeroForOne ? cache.amountIn : cache.amountOut; @@ -52,11 +57,19 @@ library Collect { /// zero out balances and transfer out if (amount0 > 0) { cache.amountIn = 0; - SafeTransfers.transferOut(params.to, cache.constants.token0, amount0); + SafeTransfers.transferOut( + params.to, + cache.constants.token0, + amount0 + ); } if (amount1 > 0) { cache.amountOut = 0; - SafeTransfers.transferOut(params.to, cache.constants.token1, amount1); + SafeTransfers.transferOut( + params.to, + cache.constants.token1, + amount1 + ); } return (cache, amount0.toInt128(), amount1.toInt128()); } diff --git a/contracts/libraries/utils/PositionTokens.sol b/contracts/libraries/utils/PositionTokens.sol index 2d0fae32..d006917f 100644 --- a/contracts/libraries/utils/PositionTokens.sol +++ b/contracts/libraries/utils/PositionTokens.sol @@ -3,10 +3,10 @@ pragma solidity 0.8.18; import './Bytes.sol'; import './String.sol'; -import "../math/OverflowMath.sol"; +import '../math/OverflowMath.sol'; import '../../interfaces/IPositionERC1155.sol'; -import "../../interfaces/range/IRangePoolFactory.sol"; -import "../../interfaces/structs/RangePoolStructs.sol"; +import '../../interfaces/range/IRangePoolFactory.sol'; +import '../../interfaces/structs/RangePoolStructs.sol'; import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; /// @notice Token library for ERC-1155 calls. @@ -17,30 +17,38 @@ library PositionTokens { PoolsharkStructs.LimitImmutables memory constants, address owner, uint32 positionId - ) internal view returns ( - uint256 - ) - { - return IPositionERC1155(constants.poolToken).balanceOf(owner, positionId); + ) internal view returns (uint256) { + return + IPositionERC1155(constants.poolToken).balanceOf(owner, positionId); } - function name(address token0, address token1) internal view returns (bytes32 result) { + function name(address token0, address token1) + internal + view + returns (bytes32 result) + { string memory nameString = string.concat( 'Poolshark ', - ERC20(token0).symbol(), '-', + ERC20(token0).symbol(), + '-', ERC20(token1).symbol() ); result = Bytes.from(nameString); } - function symbol(address token0, address token1) internal view returns (bytes32 result) { + function symbol(address token0, address token1) + internal + view + returns (bytes32 result) + { string memory symbolString = string.concat( 'PSHARK-', - ERC20(token0).symbol(), '-', + ERC20(token0).symbol(), + '-', ERC20(token1).symbol() ); result = Bytes.from(symbolString); } -} \ No newline at end of file +} diff --git a/contracts/libraries/utils/SafeCast.sol b/contracts/libraries/utils/SafeCast.sol index 3e8d23bc..d7655b2d 100644 --- a/contracts/libraries/utils/SafeCast.sol +++ b/contracts/libraries/utils/SafeCast.sol @@ -8,14 +8,15 @@ library SafeCast { /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint128 function toUint128(uint256 y) internal pure returns (uint128 z) { - if((z = uint128(y)) != y) require(false, 'Uint256ToUint128:Overflow()'); + if ((z = uint128(y)) != y) + require(false, 'Uint256ToUint128:Overflow()'); } /// @notice Cast a uint256 to a uint128, revert on overflow /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint128 function toUint128(int128 y) internal pure returns (uint128 z) { - if(y < 0) require(false, 'Int128ToUint128:Underflow()'); + if (y < 0) require(false, 'Int128ToUint128:Underflow()'); z = uint128(y); } @@ -23,14 +24,15 @@ library SafeCast { /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint160 function toUint160(uint256 y) internal pure returns (uint160 z) { - if((z = uint160(y)) != y) require(false, 'Uint256ToUint160:Overflow()'); + if ((z = uint160(y)) != y) + require(false, 'Uint256ToUint160:Overflow()'); } /// @notice Cast a uint256 to a uint160, revert on overflow /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint160 function toUint32(uint256 y) internal pure returns (uint32 z) { - if((z = uint32(y)) != y) require(false, 'Uint256ToUint32:Overflow()'); + if ((z = uint32(y)) != y) require(false, 'Uint256ToUint32:Overflow()'); } /// @notice Cast a int256 to a int128, revert on overflow or underflow @@ -44,7 +46,8 @@ library SafeCast { /// @param y The int256 to be downcasted /// @return z The downcasted integer, now type int128 function toInt128(uint128 y) internal pure returns (int128 z) { - if(y > uint128(type(int128).max)) require(false, 'Uint128ToInt128:Overflow()'); + if (y > uint128(type(int128).max)) + require(false, 'Uint128ToInt128:Overflow()'); z = int128(y); } @@ -52,7 +55,8 @@ library SafeCast { /// @param y The uint256 to be casted /// @return z The casted integer, now type int256 function toInt256(uint256 y) internal pure returns (int256 z) { - if(y > uint256(type(int256).max)) require(false, 'Uint256ToInt256:Overflow()'); + if (y > uint256(type(int256).max)) + require(false, 'Uint256ToInt256:Overflow()'); z = int256(y); } @@ -60,7 +64,7 @@ library SafeCast { /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint128 function toUint256(int256 y) internal pure returns (uint256 z) { - if(y < 0) require(false, 'Int256ToUint256:Underflow()'); + if (y < 0) require(false, 'Int256ToUint256:Underflow()'); z = uint256(y); } @@ -68,6 +72,6 @@ library SafeCast { /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint128 function toUint16(uint256 y) internal pure returns (uint16 z) { - if((z = uint16(y)) != y) require(false, 'Uint256ToUint16:Overflow()'); + if ((z = uint16(y)) != y) require(false, 'Uint256ToUint16:Overflow()'); } -} \ No newline at end of file +} diff --git a/contracts/libraries/utils/SafeTransfers.sol b/contracts/libraries/utils/SafeTransfers.sol index 5dae1fbf..a8fef9e5 100644 --- a/contracts/libraries/utils/SafeTransfers.sol +++ b/contracts/libraries/utils/SafeTransfers.sol @@ -22,8 +22,8 @@ library SafeTransfers { bool success; if (amount == 0) return; if (token == address(0)) { - (success, ) = to.call{value: amount}(""); - if (!success) require(false, "SafeTransfers::EthTransferFailed()"); + (success, ) = to.call{value: amount}(''); + if (!success) require(false, 'SafeTransfers::EthTransferFailed()'); return; } IERC20 erc20Token = IERC20(token); @@ -49,7 +49,8 @@ library SafeTransfers { success := 0 } } - if (!success) require(false, 'TransferFailed(address(this), msg.sender'); + if (!success) + require(false, 'TransferFailed(address(this), msg.sender'); } /** @@ -62,7 +63,11 @@ library SafeTransfers { * See here: https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca */ // slither-disable-next-line assembly - function transferInto(address token, address sender, uint256 amount) internal { + function transferInto( + address token, + address sender, + uint256 amount + ) internal { if (token == address(0)) { require(false, 'SafeTransfers::CannotTransferInEth()'); } @@ -88,6 +93,7 @@ library SafeTransfers { success := 0 } } - if (!success) require(false, 'TransferFailed(msg.sender, address(this)'); + if (!success) + require(false, 'TransferFailed(msg.sender, address(this)'); } } diff --git a/contracts/libraries/utils/String.sol b/contracts/libraries/utils/String.sol index 273a712a..0cc44aea 100644 --- a/contracts/libraries/utils/String.sol +++ b/contracts/libraries/utils/String.sol @@ -2,17 +2,17 @@ pragma solidity 0.8.18; library String { - bytes16 private constant alphabet = "0123456789abcdef"; + bytes16 private constant alphabet = '0123456789abcdef'; - function from(bytes32 value) internal pure returns(string memory) { + function from(bytes32 value) internal pure returns (string memory) { return toString(abi.encodePacked(value)); } - function from(address account) internal pure returns(string memory) { + function from(address account) internal pure returns (string memory) { return toString(abi.encodePacked(account)); } - function from(uint256 value) internal pure returns(string memory) { + function from(uint256 value) internal pure returns (string memory) { unchecked { uint256 length = log10(value) + 1; string memory buffer = new string(length); @@ -35,7 +35,7 @@ library String { } function from(int256 value) internal pure returns (string memory) { - return string(abi.encodePacked(value < 0 ? "-" : "", from(abs(value)))); + return string(abi.encodePacked(value < 0 ? '-' : '', from(abs(value)))); } function abs(int256 n) internal pure returns (uint256) { @@ -48,48 +48,48 @@ library String { function log10(uint256 value) internal pure returns (uint256) { uint256 result = 0; unchecked { - if (value >= 10 ** 64) { - value /= 10 ** 64; + if (value >= 10**64) { + value /= 10**64; result += 64; } - if (value >= 10 ** 32) { - value /= 10 ** 32; + if (value >= 10**32) { + value /= 10**32; result += 32; } - if (value >= 10 ** 16) { - value /= 10 ** 16; + if (value >= 10**16) { + value /= 10**16; result += 16; } - if (value >= 10 ** 8) { - value /= 10 ** 8; + if (value >= 10**8) { + value /= 10**8; result += 8; } - if (value >= 10 ** 4) { - value /= 10 ** 4; + if (value >= 10**4) { + value /= 10**4; result += 4; } - if (value >= 10 ** 2) { - value /= 10 ** 2; + if (value >= 10**2) { + value /= 10**2; result += 2; } - if (value >= 10 ** 1) { + if (value >= 10**1) { result += 1; } } return result; } - function toString(bytes memory data) internal pure returns(string memory) { + function toString(bytes memory data) internal pure returns (string memory) { bytes memory str = new bytes(2 + data.length * 2); - str[0] = "0"; - str[1] = "x"; - for (uint i = 0; i < data.length;) { - str[2+i*2] = alphabet[uint(uint8(data[i] >> 4))]; - str[3+i*2] = alphabet[uint(uint8(data[i] & 0x0f))]; + str[0] = '0'; + str[1] = 'x'; + for (uint256 i = 0; i < data.length; ) { + str[2 + i * 2] = alphabet[uint256(uint8(data[i] >> 4))]; + str[3 + i * 2] = alphabet[uint256(uint8(data[i] & 0x0f))]; unchecked { ++i; } } return string(str); } -} \ No newline at end of file +} diff --git a/contracts/staking/RangeStaker.sol b/contracts/staking/RangeStaker.sol index f186cace..b965fdee 100644 --- a/contracts/staking/RangeStaker.sol +++ b/contracts/staking/RangeStaker.sol @@ -18,11 +18,7 @@ import '../external/openzeppelin/security/ReentrancyGuard.sol'; /** * @dev Defines the actions which can be executed by the factory admin. */ -contract RangeStaker is - RangeStakerEvents, - PoolsharkStructs, - ReentrancyGuard -{ +contract RangeStaker is RangeStakerEvents, PoolsharkStructs, ReentrancyGuard { address public immutable limitPoolFactory; uint32 public immutable startTimestamp; uint32 public immutable endTimestamp; @@ -50,9 +46,7 @@ contract RangeStaker is uint32 endTime; } - constructor( - RangeStakerParams memory params - ) { + constructor(RangeStakerParams memory params) { owner = msg.sender; feeTo = msg.sender; limitPoolFactory = params.limitPoolFactory; @@ -76,14 +70,11 @@ contract RangeStaker is uint32 positionIdNext; } - function stakeRange( - StakeRangeParams memory params - ) external nonReentrant() { - + function stakeRange(StakeRangeParams memory params) external nonReentrant { // load pool constants StakeRangeLocals memory locals; locals.constants = ILimitPoolView(params.pool).immutables(); - + // Checks: validate deterministic address canonicalLimitPoolsOnly(params.pool, locals.constants); @@ -92,41 +83,43 @@ contract RangeStaker is locals.stake.positionId = params.positionId; } else { // grab positionIdNext from pool - (,,,,locals.positionIdNext,,) = ILimitPoolStorageView(params.pool).globalState(); + (, , , , locals.positionIdNext, , ) = ILimitPoolStorageView( + params.pool + ).globalState(); locals.stake.positionId = locals.positionIdNext - 1; } // stake info locals.stake.pool = params.pool; locals.poolToken = locals.constants.poolToken; - locals.stakeKey = keccak256(abi.encode( - locals.stake.pool, - locals.stake.positionId - )); + locals.stakeKey = keccak256( + abi.encode(locals.stake.pool, locals.stake.positionId) + ); // load previous fee growth and staked flag locals.stake.isStaked = rangeStakes[locals.stakeKey].isStaked; // check position exists if (!locals.stake.isStaked) { - ( - ,, - locals.stake.liquidity,, - ) = IRangePool(params.pool).positions(locals.stake.positionId); + (, , locals.stake.liquidity, , ) = IRangePool(params.pool) + .positions(locals.stake.positionId); } else { locals.stake.owner = rangeStakes[locals.stakeKey].owner; locals.stake.liquidity = rangeStakes[locals.stakeKey].liquidity; if (locals.stake.owner != params.to) { - require(false, "RangeStake::PositionOwnerMismatch()"); + require(false, 'RangeStake::PositionOwnerMismatch()'); } } if (locals.stake.liquidity == 0) { - require(false, "RangeStake::PositionNotFound()"); + require(false, 'RangeStake::PositionNotFound()'); } // check if transfer needed - locals.positionBalance = IPositionERC1155(locals.poolToken).balanceOf(address(this), locals.stake.positionId); + locals.positionBalance = IPositionERC1155(locals.poolToken).balanceOf( + address(this), + locals.stake.positionId + ); if (locals.positionBalance == 0) { // position not staked and balance not held @@ -141,15 +134,19 @@ contract RangeStaker is // start tracking fee growth from after compound if (!locals.stake.isStaked) { // compound position to avoid including old fees accrued - IRangePool(params.pool).burnRange(BurnRangeParams({ - to: params.to, - positionId: locals.stake.positionId, - burnPercent: 0 - })); + IRangePool(params.pool).burnRange( + BurnRangeParams({ + to: params.to, + positionId: locals.stake.positionId, + burnPercent: 0 + }) + ); ( locals.stake.feeGrowthInside0Last, locals.stake.feeGrowthInside1Last, - ,, + , + , + ) = IRangePool(params.pool).positions(locals.stake.positionId); // mark position as staked @@ -157,10 +154,7 @@ contract RangeStaker is locals.stake.owner = params.to; } else { // load previous fee growth - ( - locals.feeGrowthInside0Start, - locals.feeGrowthInside1Start - ) = ( + (locals.feeGrowthInside0Start, locals.feeGrowthInside1Start) = ( rangeStakes[locals.stakeKey].feeGrowthInside0Last, rangeStakes[locals.stakeKey].feeGrowthInside1Last ); @@ -168,23 +162,32 @@ contract RangeStaker is ( locals.stake.feeGrowthInside0Last, locals.stake.feeGrowthInside1Last, - locals.newPositionLiquidity,, + locals.newPositionLiquidity, + , + ) = IRangePool(params.pool).positions(params.positionId); // increment fee growth accrued if inside reward period locals.feeGrowth0Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside0Last - locals.feeGrowthInside0Start, + locals.stake.feeGrowthInside0Last - + locals.feeGrowthInside0Start, locals.stake.liquidity, Q128 ); locals.feeGrowth1Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside1Last - locals.feeGrowthInside1Start, + locals.stake.feeGrowthInside1Last - + locals.feeGrowthInside1Start, locals.stake.liquidity, Q128 ); - if (block.timestamp > startTimestamp && block.timestamp <= endTimestamp) { - if (locals.feeGrowth0Accrued > 0 || locals.feeGrowth1Accrued > 0) + if ( + block.timestamp > startTimestamp && + block.timestamp <= endTimestamp + ) { + if ( + locals.feeGrowth0Accrued > 0 || locals.feeGrowth1Accrued > 0 + ) emit StakeRangeAccrued( locals.stake.pool, locals.stake.positionId, @@ -211,63 +214,78 @@ contract RangeStaker is // Interactions: transfer out fees accrued if (locals.feeGrowth0Accrued > 0) - SafeTransfers.transferOut(locals.stake.owner, locals.constants.token0, locals.feeGrowth0Accrued); + SafeTransfers.transferOut( + locals.stake.owner, + locals.constants.token0, + locals.feeGrowth0Accrued + ); if (locals.feeGrowth1Accrued > 0) - SafeTransfers.transferOut(locals.stake.owner, locals.constants.token1, locals.feeGrowth1Accrued); + SafeTransfers.transferOut( + locals.stake.owner, + locals.constants.token1, + locals.feeGrowth1Accrued + ); } - function unstakeRange( - UnstakeRangeParams memory params - ) external nonReentrant() { - + function unstakeRange(UnstakeRangeParams memory params) + external + nonReentrant + { StakeRangeLocals memory locals; locals.poolToken = IPool(params.pool).poolToken(); - locals.stakeKey = keccak256(abi.encode( - params.pool, - params.positionId - )); + locals.stakeKey = keccak256(abi.encode(params.pool, params.positionId)); // load previous stake locals.stake = rangeStakes[locals.stakeKey]; if (locals.stake.pool == address(0)) { - require(false, "RangeUnstake::StakeNotFound()"); + require(false, 'RangeUnstake::StakeNotFound()'); } else if (locals.stake.owner != msg.sender) { - require(false, "RangeUnstake::PositionOwnerMisMatch()"); + require(false, 'RangeUnstake::PositionOwnerMisMatch()'); } else if (!locals.stake.isStaked) { - require(false, "RangeUnstake::PositionAlreadyUnstaked()"); + require(false, 'RangeUnstake::PositionAlreadyUnstaked()'); } ( locals.feeGrowthInside0Start, locals.feeGrowthInside1Start, - ,, + , + , + ) = IRangePool(params.pool).positions(params.positionId); // compound position to reward user for staked period - IRangePool(params.pool).burnRange(BurnRangeParams({ - to: params.to, - positionId: params.positionId, - burnPercent: 0 - })); + IRangePool(params.pool).burnRange( + BurnRangeParams({ + to: params.to, + positionId: params.positionId, + burnPercent: 0 + }) + ); // start tracking fee growth from after compound ( locals.stake.feeGrowthInside0Last, locals.stake.feeGrowthInside1Last, - ,, + , + , + ) = IRangePool(params.pool).positions(params.positionId); - if (block.timestamp > startTimestamp && block.timestamp <= endTimestamp) { + if ( + block.timestamp > startTimestamp && block.timestamp <= endTimestamp + ) { // increment fee growth accrued if inside reward period locals.feeGrowth0Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside0Last - locals.feeGrowthInside0Start, + locals.stake.feeGrowthInside0Last - + locals.feeGrowthInside0Start, locals.stake.liquidity, Q128 ); locals.feeGrowth1Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside1Last - locals.feeGrowthInside1Start, + locals.stake.feeGrowthInside1Last - + locals.feeGrowthInside1Start, locals.stake.liquidity, Q128 ); @@ -286,7 +304,7 @@ contract RangeStaker is params.positionId, 1 ); - + // mark position unstaked locals.stake.isStaked = false; @@ -300,32 +318,31 @@ contract RangeStaker is rangeStakes[locals.stakeKey] = locals.stake; } - function burnRangeStake( - address pool, - BurnRangeParams memory params - ) external nonReentrant() { + function burnRangeStake(address pool, BurnRangeParams memory params) + external + nonReentrant + { StakeRangeLocals memory locals; - locals.stakeKey = keccak256(abi.encode( - pool, - params.positionId - )); + locals.stakeKey = keccak256(abi.encode(pool, params.positionId)); // load previous stake locals.stake = rangeStakes[locals.stakeKey]; if (locals.stake.pool == address(0)) { - require(false, "BurnRangeStake::StakeNotFound()"); + require(false, 'BurnRangeStake::StakeNotFound()'); } else if (locals.stake.owner != msg.sender) { - require(false, "BurnRangeStake::PositionOwnerMismatch()"); + require(false, 'BurnRangeStake::PositionOwnerMismatch()'); } else if (!locals.stake.isStaked) { - require(false, "BurnRangeStake::PositionAlreadyUnstaked()"); + require(false, 'BurnRangeStake::PositionAlreadyUnstaked()'); } ( locals.feeGrowthInside0Start, locals.feeGrowthInside1Start, - ,, + , + , + ) = IRangePool(pool).positions(params.positionId); // compound position to reward user for staked period @@ -335,18 +352,24 @@ contract RangeStaker is ( locals.stake.feeGrowthInside0Last, locals.stake.feeGrowthInside1Last, - locals.stake.liquidity,, + locals.stake.liquidity, + , + ) = IRangePool(pool).positions(params.positionId); - if (block.timestamp > startTimestamp && block.timestamp <= endTimestamp) { + if ( + block.timestamp > startTimestamp && block.timestamp <= endTimestamp + ) { // increment fee growth accrued if inside reward period locals.feeGrowth0Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside0Last - locals.feeGrowthInside0Start, + locals.stake.feeGrowthInside0Last - + locals.feeGrowthInside0Start, locals.stake.liquidity, Q128 ); locals.feeGrowth1Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside1Last - locals.feeGrowthInside1Start, + locals.stake.feeGrowthInside1Last - + locals.feeGrowthInside1Start, locals.stake.liquidity, Q128 ); @@ -384,12 +407,14 @@ contract RangeStaker is * Can only be called by the current owner. */ function transferOwner(address newOwner) public virtual onlyOwner { - if(newOwner == address(0)) require (false, 'TransferredToZeroAddress()'); + if (newOwner == address(0)) + require(false, 'TransferredToZeroAddress()'); _transferOwner(newOwner); } function transferFeeTo(address newFeeTo) public virtual onlyFeeTo { - if(newFeeTo == address(0)) require (false, 'TransferredToZeroAddress()'); + if (newFeeTo == address(0)) + require(false, 'TransferredToZeroAddress()'); _transferFeeTo(newFeeTo); } @@ -417,19 +442,24 @@ contract RangeStaker is * @dev Throws if the sender is not the owner. */ function _checkOwner() internal view { - if (owner != msg.sender) require (false, 'OwnerOnly()'); + if (owner != msg.sender) require(false, 'OwnerOnly()'); } /** * @dev Throws if the sender is not the feeTo. */ function _checkFeeTo() internal view { - if (feeTo != msg.sender) require (false, 'FeeToOnly()'); + if (feeTo != msg.sender) require(false, 'FeeToOnly()'); } - function supportsInterface(bytes4 interfaceId) external pure returns (bool) { - return interfaceId == 0x01ffc9a7 || // ERC-165 support - interfaceId == 0xd9b67a26; // ERC-1155 support + function supportsInterface(bytes4 interfaceId) + external + pure + returns (bool) + { + return + interfaceId == 0x01ffc9a7 || // ERC-165 support + interfaceId == 0xd9b67a26; // ERC-1155 support } function canonicalLimitPoolsOnly( @@ -437,12 +467,14 @@ contract RangeStaker is PoolsharkStructs.LimitImmutables memory constants ) private view { // generate key for pool - bytes32 key = keccak256(abi.encode( - constants.poolImpl, - constants.token0, - constants.token1, - constants.swapFee - )); + bytes32 key = keccak256( + abi.encode( + constants.poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); // compute address address predictedAddress = LibClone.predictDeterministicAddress( @@ -456,10 +488,13 @@ contract RangeStaker is if (pool != predictedAddress) require(false, 'InvalidCallerAddress()'); } - function encodeLimit( - LimitImmutables memory constants - ) private pure returns (bytes memory) { - return abi.encodePacked( + function encodeLimit(LimitImmutables memory constants) + private + pure + returns (bytes memory) + { + return + abi.encodePacked( constants.owner, constants.token0, constants.token1, @@ -469,6 +504,6 @@ contract RangeStaker is constants.genesisTime, constants.tickSpacing, constants.swapFee - ); + ); } -} \ No newline at end of file +} diff --git a/contracts/test/WETH9.sol b/contracts/test/WETH9.sol index 95de2323..cc6f2eed 100644 --- a/contracts/test/WETH9.sol +++ b/contracts/test/WETH9.sol @@ -2,18 +2,18 @@ pragma solidity 0.8.18; contract WETH9 { - string public name = "Wrapped Ether"; - string public symbol = "WETH"; - uint8 public decimals = 18; + string public name = 'Wrapped Ether'; + string public symbol = 'WETH'; + uint8 public decimals = 18; address public immutable owner; - event Approval(address indexed src, address indexed guy, uint wad); - event Transfer(address indexed src, address indexed dst, uint wad); - event Deposit(address indexed dst, uint wad); - event Withdrawal(address indexed src, uint wad); + event Approval(address indexed src, address indexed guy, uint256 wad); + event Transfer(address indexed src, address indexed dst, uint256 wad); + event Deposit(address indexed dst, uint256 wad); + event Withdrawal(address indexed src, uint256 wad); - mapping (address => uint) public balanceOf; - mapping (address => mapping (address => uint)) public allowance; + mapping(address => uint256) public balanceOf; + mapping(address => mapping(address => uint256)) public allowance; modifier ownerOnly() { _onlyOwner(); @@ -37,34 +37,38 @@ contract WETH9 { emit Deposit(msg.sender, msg.value); } - function withdraw(uint wad) public { + function withdraw(uint256 wad) public { require(balanceOf[msg.sender] >= wad); balanceOf[msg.sender] -= wad; payable(msg.sender).transfer(wad); emit Withdrawal(msg.sender, wad); } - function totalSupply() public view returns (uint) { + function totalSupply() public view returns (uint256) { return address(this).balance; } - function approve(address guy, uint wad) public returns (bool) { + function approve(address guy, uint256 wad) public returns (bool) { allowance[msg.sender][guy] = wad; emit Approval(msg.sender, guy, wad); return true; } - function transfer(address dst, uint wad) public returns (bool) { + function transfer(address dst, uint256 wad) public returns (bool) { return transferFrom(msg.sender, dst, wad); } - function transferFrom(address src, address dst, uint wad) - public - returns (bool) - { + function transferFrom( + address src, + address dst, + uint256 wad + ) public returns (bool) { require(balanceOf[src] >= wad); - if (src != msg.sender && allowance[src][msg.sender] != uint(int(-1))) { + if ( + src != msg.sender && + allowance[src][msg.sender] != uint256(int256(-1)) + ) { require(allowance[src][msg.sender] >= wad); allowance[src][msg.sender] -= wad; } @@ -78,9 +82,9 @@ contract WETH9 { } function _mint(address account, uint256 amount) internal { - balanceOf[account] += amount; + balanceOf[account] += amount; - emit Transfer(address(0), account, amount); + emit Transfer(address(0), account, amount); } function _onlyOwner() private view { @@ -88,9 +92,6 @@ contract WETH9 { } } - - - /* GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 @@ -767,4 +768,4 @@ the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . -*/ \ No newline at end of file +*/ diff --git a/contracts/utils/LimitPoolManager.sol b/contracts/utils/LimitPoolManager.sol index bbcb2abf..adb19b7e 100644 --- a/contracts/utils/LimitPoolManager.sol +++ b/contracts/utils/LimitPoolManager.sol @@ -9,7 +9,10 @@ import '../base/events/LimitPoolManagerEvents.sol'; import '../libraries/utils/SafeCast.sol'; /** - * @dev Defines the actions which can be executed by the factory admin. + * @title LimitPoolManager + * @notice The manager for all limit pools + * @author Poolshark + * @author @alphak3y */ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { address public owner; @@ -18,8 +21,8 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { // fee delta const for dynamic fees uint16 public feeDeltaConst; // max protocol fees - uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; /// @dev - max protocol swap fee of 100% - uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; /// @dev - max protocol fill fee of 1% + uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; /// @dev - max protocol swap fee of 100% + uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; /// @dev - max protocol fill fee of 1% // impl name => impl address bytes32[] _poolTypeNames; mapping(uint256 => address) internal _poolImpls; @@ -75,12 +78,14 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { * Can only be called by the current owner. */ function transferOwner(address newOwner) public virtual onlyOwner { - if(newOwner == address(0)) require (false, 'TransferredToZeroAddress()'); + if (newOwner == address(0)) + require(false, 'TransferredToZeroAddress()'); _transferOwner(newOwner); } function transferFeeTo(address newFeeTo) public virtual onlyOwner { - if(newFeeTo == address(0)) require (false, 'TransferredToZeroAddress()'); + if (newFeeTo == address(0)) + require(false, 'TransferredToZeroAddress()'); _transferFeeTo(newFeeTo); } @@ -104,10 +109,10 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { emit FeeToTransfer(oldFeeTo, newFeeTo); } - function enableFeeTier( - uint16 swapFee, - int16 tickSpacing - ) external onlyOwner { + function enableFeeTier(uint16 swapFee, int16 tickSpacing) + external + onlyOwner + { if (_feeTiers[swapFee] != 0) revert FeeTierAlreadyEnabled(); if (tickSpacing <= 0) revert InvalidTickSpacing(); if (tickSpacing % 2 != 0) revert InvalidTickSpacing(); @@ -134,40 +139,40 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { emit PoolTypeEnabled(poolTypeName_, poolImpl_, tokenImpl_, poolTypeId_); } - function setFactory( - address factory_ - ) external onlyOwner { - if (factory != address(0)) require (false, 'FactoryAlreadySet()'); + function setFactory(address factory_) external onlyOwner { + if (factory != address(0)) require(false, 'FactoryAlreadySet()'); emit FactoryChanged(factory, factory_); factory = factory_; } - function setFeeDeltaConst( - uint16 feeDeltaConst_ - ) external onlyOwner { - if (feeDeltaConst_ > 10000) require (false, 'FeeDeltaConstCeilingExceeded()'); + function setFeeDeltaConst(uint16 feeDeltaConst_) external onlyOwner { + if (feeDeltaConst_ > 10000) + require(false, 'FeeDeltaConstCeilingExceeded()'); emit FeeDeltaConstChanged(feeDeltaConst, feeDeltaConst_); feeDeltaConst = feeDeltaConst_; } - function collectProtocolFees( - address[] calldata pools - ) external onlyOwnerOrFeeTo { - if (pools.length == 0) require (false, 'EmptyPoolsArray()'); + function collectProtocolFees(address[] calldata pools) + external + onlyOwnerOrFeeTo + { + if (pools.length == 0) require(false, 'EmptyPoolsArray()'); uint128[] memory token0FeesCollected = new uint128[](pools.length); uint128[] memory token1FeesCollected = new uint128[](pools.length); // pass empty fees params FeesParams memory feesParams; - for (uint i; i < pools.length;) { - ( - token0FeesCollected[i], - token1FeesCollected[i] - ) = IPool(pools[i]).fees(feesParams); + for (uint256 i; i < pools.length; ) { + (token0FeesCollected[i], token1FeesCollected[i]) = IPool(pools[i]) + .fees(feesParams); unchecked { ++i; } } - emit ProtocolFeesCollected(pools, token0FeesCollected, token1FeesCollected); + emit ProtocolFeesCollected( + pools, + token0FeesCollected, + token1FeesCollected + ); } // protocol fee flags @@ -180,9 +185,9 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { address[] calldata pools, FeesParams[] calldata feesParams ) external onlyOwner { - if (pools.length == 0) require (false, 'EmptyPoolsArray()'); + if (pools.length == 0) require(false, 'EmptyPoolsArray()'); if (pools.length != feesParams.length) { - require (false, 'MismatchedArrayLengths()'); + require(false, 'MismatchedArrayLengths()'); } uint128[] memory token0FeesCollected = new uint128[](pools.length); uint128[] memory token1FeesCollected = new uint128[](pools.length); @@ -190,13 +195,9 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { int16[] memory protocolSwapFees1 = new int16[](pools.length); int16[] memory protocolFillFees0 = new int16[](pools.length); int16[] memory protocolFillFees1 = new int16[](pools.length); - for (uint i; i < pools.length;) { - ( - token0FeesCollected[i], - token1FeesCollected[i] - ) = IPool(pools[i]).fees( - feesParams[i] - ); + for (uint256 i; i < pools.length; ) { + (token0FeesCollected[i], token1FeesCollected[i]) = IPool(pools[i]) + .fees(feesParams[i]); if ((feesParams[i].setFeesFlags & PROTOCOL_SWAP_FEE_0) > 0) { protocolSwapFees0[i] = int16(feesParams[i].protocolSwapFee0); } else { @@ -243,29 +244,27 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { ); } - function poolTypes( - uint16 poolTypeId - ) external view returns ( - address, - address - ) { + function poolTypes(uint16 poolTypeId) + external + view + returns (address, address) + { return (_poolImpls[poolTypeId], _tokenImpls[poolTypeId]); } - function feeTiers( - uint16 swapFee - ) external view returns ( - int16 tickSpacing - ) { + function feeTiers(uint16 swapFee) + external + view + returns (int16 tickSpacing) + { return _feeTiers[swapFee]; } - + /** * @dev Throws if the sender is not the owner. */ function _checkOwner() internal view { - if (owner != msg.sender) - require (false, 'OwnerOnly()'); + if (owner != msg.sender) require(false, 'OwnerOnly()'); } /** @@ -273,6 +272,6 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { */ function _checkFeeToAndOwner() internal view { if (feeTo != msg.sender && owner != msg.sender) - require (false, 'OwnerOrFeeToOnly()'); + require(false, 'OwnerOrFeeToOnly()'); } -} \ No newline at end of file +} diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index 646b398b..13498a9c 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -73,16 +73,12 @@ contract PoolsharkRouter is limitPoolFactory = limitPoolFactory_; coverPoolFactory = coverPoolFactory_; wethAddress = wethAddress_; - emit RouterDeployed( - address(this), - limitPoolFactory, - coverPoolFactory - ); + emit RouterDeployed(address(this), limitPoolFactory, coverPoolFactory); } receive() external payable { if (msg.sender != wethAddress) { - require(false, "PoolsharkRouter::ReceiveInvalid()"); + require(false, 'PoolsharkRouter::ReceiveInvalid()'); } } @@ -92,7 +88,9 @@ contract PoolsharkRouter is int256 amount1Delta, bytes calldata data ) external override { - PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView(msg.sender).immutables(); + PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView( + msg.sender + ).immutables(); // validate sender is a canonical limit pool canonicalLimitPoolsOnly(constants); @@ -105,14 +103,22 @@ contract PoolsharkRouter is if (constants.token0 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount0Delta)); } else { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + SafeTransfers.transferInto( + constants.token0, + _data.sender, + uint256(-amount0Delta) + ); } } if (amount1Delta < 0) { if (constants.token1 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount1Delta)); } else { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + SafeTransfers.transferInto( + constants.token1, + _data.sender, + uint256(-amount1Delta) + ); } } // transfer to swap caller @@ -136,27 +142,37 @@ contract PoolsharkRouter is int256 amount1Delta, bytes calldata data ) external override { - PoolsharkStructs.CoverImmutables memory constants = ICoverPool(msg.sender).immutables(); + PoolsharkStructs.CoverImmutables memory constants = ICoverPool( + msg.sender + ).immutables(); // validate sender is a canonical cover pool canonicalCoverPoolsOnly(constants); // decode original sender SwapCallbackData memory _data = abi.decode(data, (SwapCallbackData)); - + // transfer from swap caller if (amount0Delta < 0) { if (constants.token0 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount0Delta)); } else { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + SafeTransfers.transferInto( + constants.token0, + _data.sender, + uint256(-amount0Delta) + ); } } if (amount1Delta < 0) { if (constants.token1 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount1Delta)); } else { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + SafeTransfers.transferInto( + constants.token1, + _data.sender, + uint256(-amount1Delta) + ); } } if (amount0Delta > 0) { @@ -179,27 +195,40 @@ contract PoolsharkRouter is int256 amount1Delta, bytes calldata data ) external override { - PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView(msg.sender).immutables(); + PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView( + msg.sender + ).immutables(); // validate sender is a canonical limit pool canonicalLimitPoolsOnly(constants); // decode original sender - MintRangeCallbackData memory _data = abi.decode(data, (MintRangeCallbackData)); + MintRangeCallbackData memory _data = abi.decode( + data, + (MintRangeCallbackData) + ); // transfer from swap caller if (amount0Delta < 0) { if (constants.token0 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount0Delta)); } else { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + SafeTransfers.transferInto( + constants.token0, + _data.sender, + uint256(-amount0Delta) + ); } } if (amount1Delta < 0) { if (constants.token1 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount1Delta)); } else { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + SafeTransfers.transferInto( + constants.token1, + _data.sender, + uint256(-amount1Delta) + ); } } } @@ -210,27 +239,40 @@ contract PoolsharkRouter is int256 amount1Delta, bytes calldata data ) external override { - PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView(msg.sender).immutables(); + PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView( + msg.sender + ).immutables(); // validate sender is a canonical limit pool canonicalLimitPoolsOnly(constants); // decode original sender - MintLimitCallbackData memory _data = abi.decode(data, (MintLimitCallbackData)); - + MintLimitCallbackData memory _data = abi.decode( + data, + (MintLimitCallbackData) + ); + // transfer from swap caller if (amount0Delta < 0) { if (constants.token0 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount0Delta)); } else { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + SafeTransfers.transferInto( + constants.token0, + _data.sender, + uint256(-amount0Delta) + ); } } if (amount1Delta < 0) { if (constants.token1 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount1Delta)); } else { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + SafeTransfers.transferInto( + constants.token1, + _data.sender, + uint256(-amount1Delta) + ); } } } @@ -241,27 +283,40 @@ contract PoolsharkRouter is int256 amount1Delta, bytes calldata data ) external override { - PoolsharkStructs.CoverImmutables memory constants = ICoverPool(msg.sender).immutables(); + PoolsharkStructs.CoverImmutables memory constants = ICoverPool( + msg.sender + ).immutables(); // validate sender is a canonical cover pool canonicalCoverPoolsOnly(constants); // decode original sender - MintCoverCallbackData memory _data = abi.decode(data, (MintCoverCallbackData)); + MintCoverCallbackData memory _data = abi.decode( + data, + (MintCoverCallbackData) + ); // transfer from swap caller if (amount0Delta < 0) { if (constants.token0 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount0Delta)); } else { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + SafeTransfers.transferInto( + constants.token0, + _data.sender, + uint256(-amount0Delta) + ); } } if (amount1Delta < 0) { if (constants.token1 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount1Delta)); } else { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + SafeTransfers.transferInto( + constants.token1, + _data.sender, + uint256(-amount1Delta) + ); } } } @@ -270,12 +325,15 @@ contract PoolsharkRouter is address[] memory pools, MintLimitParams[] memory params ) external payable { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); - for (uint i = 0; i < pools.length;) { - params[i].callbackData = abi.encode(MintLimitCallbackData({ - sender: msg.sender, - wrapped: msg.value > 0 - })); + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { + params[i].callbackData = abi.encode( + MintLimitCallbackData({ + sender: msg.sender, + wrapped: msg.value > 0 + }) + ); ILimitPool(pools[i]).mintLimit(params[i]); unchecked { ++i; @@ -288,16 +346,20 @@ contract PoolsharkRouter is address[] memory pools, MintRangeParams[] memory params ) external payable { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); - for (uint i = 0; i < pools.length;) { + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { address staker; { - MintRangeCallbackData memory callbackData = MintRangeCallbackData({ - sender: msg.sender, - recipient: params[i].to, - wrapped: msg.value > 0 - }); - staker = abi.decode(params[i].callbackData, (MintRangeInputData)).staker; + MintRangeCallbackData + memory callbackData = MintRangeCallbackData({ + sender: msg.sender, + recipient: params[i].to, + wrapped: msg.value > 0 + }); + staker = abi + .decode(params[i].callbackData, (MintRangeInputData)) + .staker; if (staker != address(0)) { params[i].to = staker; } @@ -305,11 +367,18 @@ contract PoolsharkRouter is } IRangePool(pools[i]).mintRange(params[i]); if (staker != address(0)) { - IRangeStaker(staker).stakeRange(StakeRangeParams({ - to: abi.decode(params[i].callbackData, (MintRangeCallbackData)).recipient, - pool: pools[i], - positionId: params[i].positionId - })); + IRangeStaker(staker).stakeRange( + StakeRangeParams({ + to: abi + .decode( + params[i].callbackData, + (MintRangeCallbackData) + ) + .recipient, + pool: pools[i], + positionId: params[i].positionId + }) + ); } // call to staking contract using positionId returned from mintRange // fees and staked position will go to params.to @@ -324,14 +393,16 @@ contract PoolsharkRouter is address[] memory pools, PoolsharkStructs.MintCoverParams[] memory params ) external payable { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); - for (uint i = 0; i < pools.length;) { - params[i].callbackData = abi.encode(MintCoverCallbackData({ - sender: msg.sender, - wrapped: msg.value > 0 - })); - try ICoverPool(pools[i]).mint(params[i]) { - } catch {} + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { + params[i].callbackData = abi.encode( + MintCoverCallbackData({ + sender: msg.sender, + wrapped: msg.value > 0 + }) + ); + try ICoverPool(pools[i]).mint(params[i]) {} catch {} unchecked { ++i; } @@ -342,18 +413,18 @@ contract PoolsharkRouter is function multiQuote( address[] memory pools, QuoteParams[] memory params, - bool sortResults - ) external view returns ( - QuoteResults[] memory results - ) - { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); + bool sortResults + ) external view returns (QuoteResults[] memory results) { + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); if (sortResults) { // if sorting results check for matching params - for (uint i = 0; i < pools.length;) { + for (uint256 i = 0; i < pools.length; ) { if (i > 0) { - if (params[i].zeroForOne != params[0].zeroForOne) require (false, 'ZeroForOneParamMismatch()'); - if (params[i].exactIn != params[0].exactIn) require(false, 'ExactInParamMismatch()'); + if (params[i].zeroForOne != params[0].zeroForOne) + require(false, 'ZeroForOneParamMismatch()'); + if (params[i].exactIn != params[0].exactIn) + require(false, 'ExactInParamMismatch()'); /// @dev - amount and priceLimit values are allowed to be different } unchecked { @@ -362,7 +433,7 @@ contract PoolsharkRouter is } } results = new QuoteResults[](pools.length); - for (uint i = 0; i < pools.length;) { + for (uint256 i = 0; i < pools.length; ) { results[i].pool = pools[i]; ( results[i].amountIn, @@ -379,43 +450,52 @@ contract PoolsharkRouter is } } - function multiSwapSplit( - address[] memory pools, - SwapParams[] memory params - ) external payable { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); - for (uint i = 0; i < pools.length;) { + function multiSwapSplit(address[] memory pools, SwapParams[] memory params) + external + payable + { + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { if (i > 0) { - if (params[i].zeroForOne != params[0].zeroForOne) require (false, 'ZeroForOneParamMismatch()'); - if (params[i].exactIn != params[0].exactIn) require(false, 'ExactInParamMismatch()'); - if (params[i].amount != params[0].amount) require(false, 'AmountParamMisMatch()'); + if (params[i].zeroForOne != params[0].zeroForOne) + require(false, 'ZeroForOneParamMismatch()'); + if (params[i].exactIn != params[0].exactIn) + require(false, 'ExactInParamMismatch()'); + if (params[i].amount != params[0].amount) + require(false, 'AmountParamMisMatch()'); } unchecked { ++i; } } - for (uint i = 0; i < pools.length && params[0].amount > 0;) { + for (uint256 i = 0; i < pools.length && params[0].amount > 0; ) { // if msg.value > 0 we either need to wrap or unwrap the native gas token - params[i].callbackData = abi.encode(SwapCallbackData({ - sender: msg.sender, - recipient: params[i].to, - wrapped: msg.value > 0 - })); + params[i].callbackData = abi.encode( + SwapCallbackData({ + sender: msg.sender, + recipient: params[i].to, + wrapped: msg.value > 0 + }) + ); if (msg.value > 0) { IPool pool = IPool(pools[i]); - address tokenIn = params[i].zeroForOne ? pool.token0() : pool.token1(); - address tokenOut = params[i].zeroForOne ? pool.token1() : pool.token0(); + address tokenIn = params[i].zeroForOne + ? pool.token0() + : pool.token1(); + address tokenOut = params[i].zeroForOne + ? pool.token1() + : pool.token0(); if (tokenOut == wethAddress) { // send weth to router for unwrapping params[i].to = address(this); } else if (tokenIn != wethAddress) { - require (false, "NonNativeTokenPair()"); + require(false, 'NonNativeTokenPair()'); } } - ( - int256 amount0Delta, - int256 amount1Delta - ) = IPool(pools[i]).swap(params[i]); + (int256 amount0Delta, int256 amount1Delta) = IPool(pools[i]).swap( + params[i] + ); // if there is another pool to swap against if ((i + 1) < pools.length) { // calculate amount left and set for next call @@ -428,7 +508,7 @@ contract PoolsharkRouter is } else if (!params[0].zeroForOne && params[0].exactIn) { params[0].amount -= (-amount1Delta).toUint256().toUint128(); } - params[i+1].amount = params[0].amount; + params[i + 1].amount = params[0].amount; } unchecked { ++i; @@ -439,16 +519,18 @@ contract PoolsharkRouter is function multiSnapshotLimit( address[] memory pools, - SnapshotLimitParams[] memory params - ) external view returns( - uint128[] memory amountIns, - uint128[] memory amountOuts - ) { + SnapshotLimitParams[] memory params + ) + external + view + returns (uint128[] memory amountIns, uint128[] memory amountOuts) + { amountIns = new uint128[](pools.length); amountOuts = new uint128[](pools.length); - for (uint i = 0; i < pools.length;) { - if (pools[i] == address(0)) require(false, "InvalidPoolAddress()"); - (amountIns[i], amountOuts[i]) = ILimitPoolView(pools[i]).snapshotLimit(params[i]); + for (uint256 i = 0; i < pools.length; ) { + if (pools[i] == address(0)) require(false, 'InvalidPoolAddress()'); + (amountIns[i], amountOuts[i]) = ILimitPoolView(pools[i]) + .snapshotLimit(params[i]); unchecked { ++i; } @@ -459,15 +541,9 @@ contract PoolsharkRouter is ILimitPoolFactory.LimitPoolParams memory params, MintRangeParams[] memory mintRangeParams, MintLimitParams[] memory mintLimitParams - ) external payable returns ( - address pool, - address poolToken - ) { + ) external payable returns (address pool, address poolToken) { // check if pool exists - ( - pool, - poolToken - ) = ILimitPoolFactory(limitPoolFactory).getLimitPool( + (pool, poolToken) = ILimitPoolFactory(limitPoolFactory).getLimitPool( params.tokenIn, params.tokenOut, params.swapFee, @@ -475,49 +551,59 @@ contract PoolsharkRouter is ); // create if pool doesn't exist if (pool == address(0)) { - ( - pool, - poolToken - ) = ILimitPoolFactory(limitPoolFactory).createLimitPool( - params - ); + (pool, poolToken) = ILimitPoolFactory(limitPoolFactory) + .createLimitPool(params); } // mint initial range positions - for (uint i = 0; i < mintRangeParams.length;) { + for (uint256 i = 0; i < mintRangeParams.length; ) { address staker; { mintRangeParams[i].positionId = 0; - MintRangeCallbackData memory callbackData = MintRangeCallbackData({ - sender: msg.sender, - recipient: mintRangeParams[i].to, - wrapped: msg.value > 0 - }); - staker = abi.decode(mintRangeParams[i].callbackData, (MintRangeInputData)).staker; + MintRangeCallbackData + memory callbackData = MintRangeCallbackData({ + sender: msg.sender, + recipient: mintRangeParams[i].to, + wrapped: msg.value > 0 + }); + staker = abi + .decode( + mintRangeParams[i].callbackData, + (MintRangeInputData) + ) + .staker; if (staker != address(0)) { mintRangeParams[i].to = staker; } mintRangeParams[i].callbackData = abi.encode(callbackData); } - try IRangePool(pool).mintRange(mintRangeParams[i]) { - } catch {} + try IRangePool(pool).mintRange(mintRangeParams[i]) {} catch {} if (staker != address(0)) { - IRangeStaker(staker).stakeRange(StakeRangeParams({ - to: abi.decode(mintRangeParams[i].callbackData, (MintRangeCallbackData)).recipient, - pool: pool, - positionId: 0 - })); + IRangeStaker(staker).stakeRange( + StakeRangeParams({ + to: abi + .decode( + mintRangeParams[i].callbackData, + (MintRangeCallbackData) + ) + .recipient, + pool: pool, + positionId: 0 + }) + ); } unchecked { ++i; } } // mint initial limit positions - for (uint i = 0; i < mintLimitParams.length;) { + for (uint256 i = 0; i < mintLimitParams.length; ) { mintLimitParams[i].positionId = 0; - mintLimitParams[i].callbackData = abi.encode(MintLimitCallbackData({ - sender: msg.sender, - wrapped: msg.value > 0 - })); + mintLimitParams[i].callbackData = abi.encode( + MintLimitCallbackData({ + sender: msg.sender, + wrapped: msg.value > 0 + }) + ); ILimitPool(pool).mintLimit(mintLimitParams[i]); unchecked { ++i; @@ -529,35 +615,26 @@ contract PoolsharkRouter is function createCoverPoolAndMint( ICoverPoolFactory.CoverPoolParams memory params, MintCoverParams[] memory mintCoverParams - ) external payable returns ( - address pool, - address poolToken - ) { + ) external payable returns (address pool, address poolToken) { // check if pool exists - ( - pool, - poolToken - ) = ICoverPoolFactory(coverPoolFactory).getCoverPool( + (pool, poolToken) = ICoverPoolFactory(coverPoolFactory).getCoverPool( params ); // create if pool doesn't exist if (pool == address(0)) { - ( - pool, - poolToken - ) = ICoverPoolFactory(coverPoolFactory).createCoverPool( - params - ); + (pool, poolToken) = ICoverPoolFactory(coverPoolFactory) + .createCoverPool(params); } // mint initial cover positions - for (uint i = 0; i < mintCoverParams.length;) { + for (uint256 i = 0; i < mintCoverParams.length; ) { mintCoverParams[i].positionId = 0; - mintCoverParams[i].callbackData = abi.encode(MintCoverCallbackData({ - sender: msg.sender, - wrapped: msg.value > 0 - })); - try ICoverPool(pool).mint(mintCoverParams[i]) { - } catch {} + mintCoverParams[i].callbackData = abi.encode( + MintCoverCallbackData({ + sender: msg.sender, + wrapped: msg.value > 0 + }) + ); + try ICoverPool(pool).mint(mintCoverParams[i]) {} catch {} unchecked { ++i; } @@ -578,28 +655,34 @@ contract PoolsharkRouter is function sortQuoteResults( QuoteParams[] memory params, QuoteResults[] memory results - ) internal pure returns ( - QuoteResults[] memory - ) { + ) internal pure returns (QuoteResults[] memory) { SortQuoteResultsLocals memory locals; locals.sortedResults = new QuoteResults[](results.length); locals.sortedFlags = new bool[](results.length); locals.emptyResults = 0; - for (uint sorted = 0; sorted < results.length;) { + for (uint256 sorted = 0; sorted < results.length; ) { // if exactIn, sort by most output // if exactOut, sort by most output then least input - locals.sortAmount = params[0].exactIn ? int256(0) : type(int256).max; + locals.sortAmount = params[0].exactIn + ? int256(0) + : type(int256).max; locals.sortIndex = type(uint256).max; - for (uint index = 0; index < results.length;) { + for (uint256 index = 0; index < results.length; ) { // check if result already sorted if (!locals.sortedFlags[index]) { if (params[0].exactIn) { - if (results[index].amountOut > 0 && results[index].amountOut >= locals.sortAmount) { + if ( + results[index].amountOut > 0 && + results[index].amountOut >= locals.sortAmount + ) { locals.sortIndex = index; locals.sortAmount = results[index].amountOut; } } else { - if (results[index].amountIn > 0 && results[index].amountIn <= locals.sortAmount) { + if ( + results[index].amountIn > 0 && + results[index].amountIn <= locals.sortAmount + ) { locals.sortIndex = index; locals.sortAmount = results[index].amountIn; } @@ -612,10 +695,17 @@ contract PoolsharkRouter is } if (locals.sortIndex != type(uint256).max) { // add the sorted result - locals.sortedResults[sorted].pool = results[locals.sortIndex].pool; - locals.sortedResults[sorted].amountIn = results[locals.sortIndex].amountIn; - locals.sortedResults[sorted].amountOut = results[locals.sortIndex].amountOut; - locals.sortedResults[sorted].priceAfter = results[locals.sortIndex].priceAfter; + locals.sortedResults[sorted].pool = results[locals.sortIndex] + .pool; + locals.sortedResults[sorted].amountIn = results[ + locals.sortIndex + ].amountIn; + locals.sortedResults[sorted].amountOut = results[ + locals.sortIndex + ].amountOut; + locals.sortedResults[sorted].priceAfter = results[ + locals.sortIndex + ].priceAfter; // indicate this result was already sorted locals.sortedFlags[locals.sortIndex] = true; @@ -629,12 +719,15 @@ contract PoolsharkRouter is } // if any results were empty, prune them if (locals.emptyResults > 0) { - locals.prunedResults = new QuoteResults[](results.length - locals.emptyResults); + locals.prunedResults = new QuoteResults[]( + results.length - locals.emptyResults + ); locals.prunedIndex = 0; - for (uint sorted = 0; sorted < results.length;) { + for (uint256 sorted = 0; sorted < results.length; ) { // empty results are omitted if (locals.sortedResults[sorted].pool != address(0)) { - locals.prunedResults[locals.prunedIndex] = locals.sortedResults[sorted]; + locals.prunedResults[locals.prunedIndex] = locals + .sortedResults[sorted]; unchecked { ++locals.prunedIndex; } @@ -649,13 +742,19 @@ contract PoolsharkRouter is return locals.prunedResults; } - function multiCall( - address[] memory pools, - SwapParams[] memory params - ) external { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); - for (uint i = 0; i < pools.length;) { - params[i].callbackData = abi.encode(SwapCallbackData({sender: msg.sender, recipient: params[i].to, wrapped: true})); + function multiCall(address[] memory pools, SwapParams[] memory params) + external + { + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { + params[i].callbackData = abi.encode( + SwapCallbackData({ + sender: msg.sender, + recipient: params[i].to, + wrapped: true + }) + ); ICoverPool(pools[i]).swap(params[i]); unchecked { ++i; @@ -667,12 +766,14 @@ contract PoolsharkRouter is PoolsharkStructs.LimitImmutables memory constants ) private view { // generate key for pool - bytes32 key = keccak256(abi.encode( - constants.poolImpl, - constants.token0, - constants.token1, - constants.swapFee - )); + bytes32 key = keccak256( + abi.encode( + constants.poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); // compute address address predictedAddress = LibClone.predictDeterministicAddress( @@ -683,21 +784,24 @@ contract PoolsharkRouter is ); // revert on sender mismatch - if (msg.sender != predictedAddress) require(false, 'InvalidCallerAddress()'); + if (msg.sender != predictedAddress) + require(false, 'InvalidCallerAddress()'); } function canonicalCoverPoolsOnly( PoolsharkStructs.CoverImmutables memory constants ) private view { // generate key for pool - bytes32 key = keccak256(abi.encode( - constants.token0, - constants.token1, - constants.source, - constants.inputPool, - constants.tickSpread, - constants.twapLength - )); + bytes32 key = keccak256( + abi.encode( + constants.token0, + constants.token1, + constants.source, + constants.inputPool, + constants.tickSpread, + constants.twapLength + ) + ); // compute address address predictedAddress = LibClone.predictDeterministicAddress( @@ -708,13 +812,17 @@ contract PoolsharkRouter is ); // revert on sender mismatch - if (msg.sender != predictedAddress) require(false, 'InvalidCallerAddress()'); + if (msg.sender != predictedAddress) + require(false, 'InvalidCallerAddress()'); } - function encodeLimit( - LimitImmutables memory constants - ) private pure returns (bytes memory) { - return abi.encodePacked( + function encodeLimit(LimitImmutables memory constants) + private + pure + returns (bytes memory) + { + return + abi.encodePacked( constants.owner, constants.token0, constants.token1, @@ -724,12 +832,14 @@ contract PoolsharkRouter is constants.genesisTime, constants.tickSpacing, constants.swapFee - ); + ); } - function encodeCover( - CoverImmutables memory constants - ) private pure returns (bytes memory) { + function encodeCover(CoverImmutables memory constants) + private + pure + returns (bytes memory) + { bytes memory value1 = abi.encodePacked( constants.owner, constants.token0, @@ -760,7 +870,8 @@ contract PoolsharkRouter is function wrapEth(uint256 amount) private { // wrap necessary amount of WETH IWETH9 weth = IWETH9(wethAddress); - if (amount > address(this).balance) require(false, 'WrapEth::LowEthBalance()'); + if (amount > address(this).balance) + require(false, 'WrapEth::LowEthBalance()'); weth.deposit{value: amount}(); // transfer weth into pool SafeTransfers.transferOut(msg.sender, wethAddress, amount); @@ -779,8 +890,12 @@ contract PoolsharkRouter is if (address(this).balance >= msg.value) { SafeTransfers.transferOut(msg.sender, ethAddress, msg.value); } else { - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); + SafeTransfers.transferOut( + msg.sender, + ethAddress, + address(this).balance + ); } } } -} \ No newline at end of file +} diff --git a/contracts/utils/PositionERC1155.sol b/contracts/utils/PositionERC1155.sol index 4d0c3eef..5531cd26 100644 --- a/contracts/utils/PositionERC1155.sol +++ b/contracts/utils/PositionERC1155.sol @@ -4,22 +4,17 @@ pragma solidity 0.8.18; import '../interfaces/IPool.sol'; import '../base/storage/PositionERC1155Immutables.sol'; -import "../interfaces/IPositionERC1155.sol"; +import '../interfaces/IPositionERC1155.sol'; import '../external/solady/LibClone.sol'; import '../libraries/utils/String.sol'; import '../libraries/utils/SafeTransfers.sol'; import '../libraries/utils/PositionTokens.sol'; -contract PositionERC1155 is - IPositionERC1155, - PositionERC1155Immutables -{ +contract PositionERC1155 is IPositionERC1155, PositionERC1155Immutables { address public immutable factory; address public immutable original; - constructor( - address factory_ - ) { + constructor(address factory_) { factory = factory_; original = address(this); } @@ -39,31 +34,54 @@ contract PositionERC1155 is modifier onlyCanonicalClones( PoolsharkStructs.LimitImmutables memory constants ) { - if(!_onlyCanonicalPools(constants)) require (false, 'CanoncialPoolsOnly()'); - if(!_onlyCanonicalPoolTokens(constants)) require (false, 'CanoncialPoolTokensOnly()'); + if (!_onlyCanonicalPools(constants)) + require(false, 'CanoncialPoolsOnly()'); + if (!_onlyCanonicalPoolTokens(constants)) + require(false, 'CanoncialPoolTokensOnly()'); _; } modifier checkApproval(address _from, address _spender) { if (_from != _spender) - if(!_isApprovedForAll(_from, _spender)) - require(false, string.concat('SpenderNotApproved(', String.from(_from), ', ', String.from(_spender), ')')); + if (!_isApprovedForAll(_from, _spender)) + require( + false, + string.concat( + 'SpenderNotApproved(', + String.from(_from), + ', ', + String.from(_spender), + ')' + ) + ); _; } modifier checkAddresses(address _from, address _to) { - if (_from == address(0) || _to == address(0)) require(false, 'TransferFromOrToAddress0()'); + if (_from == address(0) || _to == address(0)) + require(false, 'TransferFromOrToAddress0()'); if (_from == _to) require(false, 'TransferToSelf()'); _; } modifier checkLength(uint256 _lengthA, uint256 _lengthB) { - if (_lengthA != _lengthB) require(false, string.concat('LengthMismatch(', String.from(_lengthA), ', ', String.from(_lengthB), ')')); + if (_lengthA != _lengthB) + require( + false, + string.concat( + 'LengthMismatch(', + String.from(_lengthA), + ', ', + String.from(_lengthB), + ')' + ) + ); _; } modifier checkERC1155Support(address recipient) { - if (!_verifyERC1155Support(recipient)) require(false, 'ERC1155NotSupported()'); + if (!_verifyERC1155Support(recipient)) + require(false, 'ERC1155NotSupported()'); _; } @@ -72,10 +90,7 @@ contract PositionERC1155 is uint256 _id, uint256 _amount, PoolsharkStructs.LimitImmutables memory constants - ) external - onlyCanonicalClones(constants) - checkERC1155Support(_account) - { + ) external onlyCanonicalClones(constants) checkERC1155Support(_account) { _mint(_account, _id, _amount); } @@ -84,13 +99,15 @@ contract PositionERC1155 is uint256 _id, uint256 _amount, PoolsharkStructs.LimitImmutables memory constants - ) external - onlyCanonicalClones(constants) - { + ) external onlyCanonicalClones(constants) { _burn(_account, _id, _amount); } - function setApprovalForAll(address _spender, bool _approved) public virtual override { + function setApprovalForAll(address _spender, bool _approved) + public + virtual + override + { _setApprovalForAll(msg.sender, _spender, _approved); } @@ -99,7 +116,10 @@ contract PositionERC1155 is address _to, uint256 _id, uint256 _amount - ) public virtual override + ) + public + virtual + override checkAddresses(_from, _to) checkApproval(_from, msg.sender) checkERC1155Support(_to) @@ -114,7 +134,10 @@ contract PositionERC1155 is address _to, uint256[] calldata _ids, uint256[] calldata _amounts - ) public virtual override + ) + public + virtual + override checkLength(_ids.length, _amounts.length) checkAddresses(_from, _to) checkApproval(_from, msg.sender) @@ -131,19 +154,28 @@ contract PositionERC1155 is function withdrawEth( address recipient, PoolsharkStructs.LimitImmutables memory constants - ) external - onlyCanonicalClones(constants) - { + ) external onlyCanonicalClones(constants) { SafeTransfers.transferOut(recipient, ethAddress, address(this).balance); } - function isApprovedForAll(address _owner, address _spender) public view virtual override returns (bool) { + function isApprovedForAll(address _owner, address _spender) + public + view + virtual + override + returns (bool) + { return _isApprovedForAll(_owner, _spender); } - function supportsInterface(bytes4 interfaceID) external pure returns (bool) { - return interfaceID == 0x01ffc9a7 || // ERC-165 support - interfaceID == 0xd9b67a26; // ERC-1155 support + function supportsInterface(bytes4 interfaceID) + external + pure + returns (bool) + { + return + interfaceID == 0x01ffc9a7 || // ERC-165 support + interfaceID == 0xd9b67a26; // ERC-1155 support } function name() public pure virtual override returns (string memory) { @@ -154,18 +186,34 @@ contract PositionERC1155 is return Bytes.bytes32ToString(tokenSymbol()); } - function totalSupply(uint256 _id) public view virtual override returns (uint256) { + function totalSupply(uint256 _id) + public + view + virtual + override + returns (uint256) + { return _totalSupplyById[_id]; } - function balanceOf(address _account, uint256 _id) public view virtual override returns (uint256) { + function balanceOf(address _account, uint256 _id) + public + view + virtual + override + returns (uint256) + { return _tokenBalances[_id][_account]; } function balanceOfBatch( address[] calldata _accounts, uint256[] calldata _ids - ) public view virtual override + ) + public + view + virtual + override checkLength(_accounts.length, _ids.length) returns (uint256[] memory batchBalances) { @@ -199,7 +247,19 @@ contract PositionERC1155 is ) internal virtual { if (_account == address(0)) require(false, 'BurnFromAddress0()'); uint256 _accountBalance = _tokenBalances[_id][_account]; - if (_accountBalance < _amount) require(false, string.concat('BurnExceedsBalance(', String.from(_account), ', ', String.from(_id), ', ', String.from(_amount), ')')); + if (_accountBalance < _amount) + require( + false, + string.concat( + 'BurnExceedsBalance(', + String.from(_account), + ', ', + String.from(_id), + ', ', + String.from(_amount), + ')' + ) + ); _beforeTokenTransfer(_account, address(0), _id, _amount); unchecked { _tokenBalances[_id][_account] = _accountBalance - _amount; @@ -215,7 +275,19 @@ contract PositionERC1155 is uint256 _amount ) internal virtual { uint256 _fromBalance = _tokenBalances[_id][_from]; - if (_fromBalance < _amount) require(false, string.concat('TransferExceedsBalance(', String.from(_from), ', ', String.from(_id), ', ', String.from(_amount), ')')); + if (_fromBalance < _amount) + require( + false, + string.concat( + 'TransferExceedsBalance(', + String.from(_from), + ', ', + String.from(_id), + ', ', + String.from(_amount), + ')' + ) + ); _beforeTokenTransfer(_from, _to, _id, _amount); unchecked { _tokenBalances[_id][_from] = _fromBalance - _amount; @@ -231,12 +303,21 @@ contract PositionERC1155 is address _spender, bool _approved ) internal virtual { - if (_owner == _spender) require(false, string.concat('SelfApproval(', String.from(_owner), ')')); + if (_owner == _spender) + require( + false, + string.concat('SelfApproval(', String.from(_owner), ')') + ); _spenderApprovals[_owner][_spender] = _approved; emit ApprovalForAll(_owner, _spender, _approved); } - function _isApprovedForAll(address _owner, address _spender) internal view virtual returns (bool) { + function _isApprovedForAll(address _owner, address _spender) + internal + view + virtual + returns (bool) + { return _owner == _spender || _spenderApprovals[_owner][_spender]; } @@ -252,20 +333,19 @@ contract PositionERC1155 is PoolsharkStructs.LimitImmutables memory constants ) private view returns (bool) { // generate key for pool - bytes32 key = keccak256(abi.encode( - constants.poolImpl, - constants.token0, - constants.token1, - constants.swapFee - )); + bytes32 key = keccak256( + abi.encode( + constants.poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); // compute address address predictedAddress = LibClone.predictDeterministicAddress( original, - abi.encodePacked( - tokenName(), - tokenSymbol() - ), + abi.encodePacked(tokenName(), tokenSymbol()), key, factory ); @@ -279,12 +359,14 @@ contract PositionERC1155 is PoolsharkStructs.LimitImmutables memory constants ) private view returns (bool) { // generate key for pool - bytes32 key = keccak256(abi.encode( - constants.poolImpl, - constants.token0, - constants.token1, - constants.swapFee - )); + bytes32 key = keccak256( + abi.encode( + constants.poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); // compute address address predictedAddress = LibClone.predictDeterministicAddress( @@ -312,14 +394,20 @@ contract PositionERC1155 is /// @notice Return if the `_target` contract supports ERC-1155 interface /// @param _target The address of the contract /// @return supported Whether the contract is supported (true) or not (false) - function _verifyERC1155Support(address _target) private view returns (bool supported) { + function _verifyERC1155Support(address _target) + private + view + returns (bool supported) + { if (_target.code.length == 0) return true; bytes memory encodedParams = abi.encodeWithSelector( IERC165.supportsInterface.selector, bytes4(0xd9b67a26) // ERC-1155 support ); - (bool success, bytes memory result) = _target.staticcall{gas: 30_000}(encodedParams); + (bool success, bytes memory result) = _target.staticcall{gas: 30_000}( + encodedParams + ); if (result.length < 32) return false; return success && abi.decode(result, (bool)); } -} \ No newline at end of file +} diff --git a/package.json b/package.json index aa6a798e..9eb024a0 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "deploy-test": "rm -rf ./artifacts ./cache ./typechain; npx hardhat clean; npx hardhat compile; npx hardhat deploy-limitpools --network arb_goerli;", "deploy-main": "npx hardhat deploy-limitpools --network arb_one", "verify": "npx hardhat verify-contracts", - "all": "yarn clean; yarn compile; yarn test; yarn deploy;" + "all": "yarn clean; yarn compile; yarn test; yarn deploy;", + "prettier": "prettier --write --plugin=prettier-plugin-solidity contracts/**/*.sol" }, "devDependencies": { "@nomicfoundation/hardhat-network-helpers": "1.0.8", @@ -41,8 +42,8 @@ "hardhat-contract-sizer": "2.6.1", "hardhat-gas-reporter": "1.0.9", "mythxjs": "1.3.13", - "prettier": "2.7.1", - "prettier-plugin-solidity": "1.0.0-beta.24", + "prettier": "^2.7.1", + "prettier-plugin-solidity": "^1.0.0-beta.24", "solhint": "3.3.7", "solidity-coverage": "0.7.22", "ts-node": "10.9.1", diff --git a/test/contracts/limitpoolmanager.ts b/test/contracts/limitpoolmanager.ts index cb07502c..37917dcd 100644 --- a/test/contracts/limitpoolmanager.ts +++ b/test/contracts/limitpoolmanager.ts @@ -207,19 +207,6 @@ describe('LimitPoolManager Tests', function () { ).to.be.revertedWith('InvalidSwapFee()') }) - /// @dev - pool type id is incremented each time - // it('Should not enable pool type a second time', async function () { - // await expect( - // hre.props.limitPoolManager - // .connect(hre.props.admin) - // .enablePoolType( - // hre.props.limitPoolImpl.address, - // hre.props.positionERC1155.address, - // constantProductString - // ) - // ).to.be.revertedWith('PoolTypeAlreadyExists()') - // }) - it('Should not enable pool type w/ invalid impl addresses', async function () { await expect( hre.props.limitPoolManager @@ -250,34 +237,6 @@ describe('LimitPoolManager Tests', function () { ).to.be.revertedWith('InvalidImplAddresses()') }) - // it('Should enable new twap source', async function () { - // await hre.props.limitPoolManager - // .connect(hre.props.admin) - // .enableTwapSource(psharkString, hre.props.uniswapV3Source.address, hre.props.uniswapV3Source.address) - - // const twapSource = await hre.props.limitPoolManager - // .twapSources(psharkString) - // expect(twapSource[0]).to.be.equal(hre.props.uniswapV3Source.address) - // expect(twapSource[1]).to.be.equal(hre.props.uniswapV3Source.address) - // }) - - - // it('Should not enable twap source with OwnerOnly()', async function () { - // await expect( - // hre.props.limitPoolManager - // .connect(hre.props.bob) - // .enableTwapSource(psharkString, hre.props.uniswapV3Source.address, hre.props.uniswapV3Source.address) - // ).to.be.revertedWith('OwnerOnly()') - // }) - - // it('Should not enable twap source with invalid string', async function () { - // await expect( - // hre.props.limitPoolManager - // .connect(hre.props.admin) - // .enableTwapSource(ethers.utils.formatBytes32String(''), hre.props.uniswapV3Source.address, hre.props.uniswapV3Source.address) - // ).to.be.revertedWith('TwapSourceNameInvalid()') - // }) - it('Should not update protocol fees on pool', async function () { let globalStateBefore = await hre.props.limitPool.globalState(); await expect( @@ -341,11 +300,129 @@ describe('LimitPoolManager Tests', function () { let globalStateAfter = await hre.props.limitPool.globalState(); expect(globalStateBefore.pool.protocolSwapFee0).to.be.equal(globalStateAfter.pool.protocolSwapFee0) expect(globalStateBefore.pool.protocolSwapFee1).to.be.equal(globalStateAfter.pool.protocolSwapFee1) - expect(globalStateBefore.pool0.protocolFillFee).to.be.equal(globalStateBefore.pool0.protocolFillFee) - expect(globalStateBefore.pool1.protocolFillFee).to.be.equal(globalStateBefore.pool1.protocolFillFee) + expect(globalStateBefore.pool0.protocolFillFee).to.be.equal(globalStateAfter.pool0.protocolFillFee) + expect(globalStateBefore.pool1.protocolFillFee).to.be.equal(globalStateAfter.pool1.protocolFillFee) // erc-20 balance should not change }) + it('Should update protocol fees on pool', async function () { + // protocol swap fee 0 + let globalStateBefore = await hre.props.limitPool.globalState(); + let txn = await hre.props.limitPoolManager + .connect(hre.props.admin) + .modifyProtocolFees( + [hre.props.limitPool.address], + [ + { + protocolSwapFee0: 500, + protocolSwapFee1: 500, + protocolFillFee0: 500, + protocolFillFee1: 500, + setFeesFlags: 1 + } + ] + ) + let globalStateAfter = await hre.props.limitPool.globalState(); + expect(globalStateBefore.pool.protocolSwapFee0).to.not.be.equal(globalStateAfter.pool.protocolSwapFee0) + expect(globalStateAfter.pool.protocolSwapFee0).to.be.equal(500) + expect(globalStateBefore.pool.protocolSwapFee1).to.be.equal(globalStateAfter.pool.protocolSwapFee1) + expect(globalStateBefore.pool0.protocolFillFee).to.be.equal(globalStateAfter.pool0.protocolFillFee) + expect(globalStateBefore.pool1.protocolFillFee).to.be.equal(globalStateAfter.pool1.protocolFillFee) + + // protocol swap fee 1 + globalStateBefore = globalStateAfter + txn = await hre.props.limitPoolManager + .connect(hre.props.admin) + .modifyProtocolFees( + [hre.props.limitPool.address], + [ + { + protocolSwapFee0: 500, + protocolSwapFee1: 500, + protocolFillFee0: 500, + protocolFillFee1: 500, + setFeesFlags: 2 + } + ] + ) + globalStateAfter = await hre.props.limitPool.globalState(); + expect(globalStateBefore.pool.protocolSwapFee0).to.be.equal(globalStateAfter.pool.protocolSwapFee0) + expect(globalStateBefore.pool.protocolSwapFee1).to.not.be.equal(globalStateAfter.pool.protocolSwapFee1) + expect(globalStateAfter.pool.protocolSwapFee1).to.be.equal(500) + expect(globalStateBefore.pool0.protocolFillFee).to.be.equal(globalStateAfter.pool0.protocolFillFee) + expect(globalStateBefore.pool1.protocolFillFee).to.be.equal(globalStateAfter.pool1.protocolFillFee) + + // protocol fill fee 0 + globalStateBefore = globalStateAfter + txn = await hre.props.limitPoolManager + .connect(hre.props.admin) + .modifyProtocolFees( + [hre.props.limitPool.address], + [ + { + protocolSwapFee0: 500, + protocolSwapFee1: 500, + protocolFillFee0: 50, + protocolFillFee1: 50, + setFeesFlags: 4 + } + ] + ) + globalStateAfter = await hre.props.limitPool.globalState(); + expect(globalStateBefore.pool.protocolSwapFee0).to.be.equal(globalStateAfter.pool.protocolSwapFee0) + expect(globalStateBefore.pool.protocolSwapFee1).to.be.equal(globalStateAfter.pool.protocolSwapFee1) + + expect(globalStateBefore.pool0.protocolFillFee).to.be.equal(globalStateAfter.pool0.protocolFillFee) + expect(globalStateBefore.pool1.protocolFillFee).to.not.be.equal(globalStateAfter.pool1.protocolFillFee) + expect(globalStateAfter.pool1.protocolFillFee).to.be.equal(50) + + // protocol fill fee 1 + globalStateBefore = globalStateAfter + txn = await hre.props.limitPoolManager + .connect(hre.props.admin) + .modifyProtocolFees( + [hre.props.limitPool.address], + [ + { + protocolSwapFee0: 500, + protocolSwapFee1: 500, + protocolFillFee0: 50, + protocolFillFee1: 50, + setFeesFlags: 8 + } + ] + ) + globalStateAfter = await hre.props.limitPool.globalState(); + expect(globalStateBefore.pool.protocolSwapFee0).to.be.equal(globalStateAfter.pool.protocolSwapFee0) + expect(globalStateBefore.pool.protocolSwapFee1).to.be.equal(globalStateAfter.pool.protocolSwapFee1) + + expect(globalStateBefore.pool0.protocolFillFee).to.not.be.equal(globalStateAfter.pool0.protocolFillFee) + expect(globalStateBefore.pool1.protocolFillFee).to.be.equal(globalStateAfter.pool1.protocolFillFee) + expect(globalStateAfter.pool0.protocolFillFee).to.be.equal(50) + + // reset all protocol fees + globalStateBefore = globalStateAfter + txn = await hre.props.limitPoolManager + .connect(hre.props.admin) + .modifyProtocolFees( + [hre.props.limitPool.address], + [ + { + protocolSwapFee0: 0, + protocolSwapFee1: 0, + protocolFillFee0: 0, + protocolFillFee1: 0, + setFeesFlags: 15 // 8 + 4 + 2 + 1 = 15 + } + ] + ) + globalStateAfter = await hre.props.limitPool.globalState(); + expect(globalStateAfter.pool.protocolSwapFee0).to.be.equal(0) + expect(globalStateAfter.pool.protocolSwapFee1).to.be.equal(0) + expect(globalStateAfter.pool0.protocolFillFee).to.be.equal(0) + expect(globalStateAfter.pool1.protocolFillFee).to.be.equal(0) + }) + // modify each protocol fee one by one and verify others do not change // mint position and complete a swap with protocol fee turned on From 731fd76b66b9e36c5a949013b58b368894da426c Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sun, 10 Dec 2023 15:57:16 -0700 Subject: [PATCH 13/39] final arbitrum mainnet deployment --- scripts/autogen/contract-deployments-keys.ts | 80 +++++++++++++++++ scripts/autogen/contract-deployments.json | 94 ++++++++++---------- scripts/config/networkConfigs.ts | 7 ++ scripts/constants/supportedNetworks.ts | 1 + test/utils/setup/initialSetup.ts | 4 +- 5 files changed, 136 insertions(+), 50 deletions(-) diff --git a/scripts/autogen/contract-deployments-keys.ts b/scripts/autogen/contract-deployments-keys.ts index 444b7674..538678f8 100644 --- a/scripts/autogen/contract-deployments-keys.ts +++ b/scripts/autogen/contract-deployments-keys.ts @@ -9,4 +9,84 @@ export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ networkName: 'arb_one', objectName: 'tokenQuote' }, + { + networkName: 'arb_one', + objectName: 'tickMapLib' + }, + { + networkName: 'arb_one', + objectName: 'ticksLib' + }, + { + networkName: 'arb_one', + objectName: 'limitPositionsLib' + }, + { + networkName: 'arb_one', + objectName: 'limitPoolManager' + }, + { + networkName: 'arb_one', + objectName: 'limitPoolFactory' + }, + { + networkName: 'arb_one', + objectName: 'swapCall' + }, + { + networkName: 'arb_one', + objectName: 'mintRangeCall' + }, + { + networkName: 'arb_one', + objectName: 'burnRangeCall' + }, + { + networkName: 'arb_one', + objectName: 'mintLimitCall' + }, + { + networkName: 'arb_one', + objectName: 'burnLimitCall' + }, + { + networkName: 'arb_one', + objectName: 'snapshotLimitCall' + }, + { + networkName: 'arb_one', + objectName: 'quoteCall' + }, + { + networkName: 'arb_one', + objectName: 'feesCall' + }, + { + networkName: 'arb_one', + objectName: 'sampleCall' + }, + { + networkName: 'arb_one', + objectName: 'snapshotRangeCall' + }, + { + networkName: 'arb_one', + objectName: 'limitPoolImpl' + }, + { + networkName: 'arb_one', + objectName: 'positionERC1155' + }, + { + networkName: 'arb_one', + objectName: 'limitPool' + }, + { + networkName: 'arb_one', + objectName: 'poolRouter' + }, + { + networkName: 'arb_one', + objectName: 'rangeStaker' + }, ]; \ No newline at end of file diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index 2d113c1a..6dac502b 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -56,144 +56,144 @@ }, "tickMapLib": { "contractName": "TickMap", - "contractAddress": "0x334a70f0cb8b9ceee3d0f4ea232581ff3dfc6e58", + "contractAddress": "0x51c65a3d7fb6304206b64888daf95285374f3a96", "constructorArguments": [], - "created": "2023-12-08T16:27:08.959Z" + "created": "2023-12-10T22:48:47.380Z" }, "ticksLib": { "contractName": "Ticks", - "contractAddress": "0x3fa761492f411ebc64a81fcf3292fdc0b677c00f", + "contractAddress": "0xac424407a8bbaa42a781eb94f83ae37231594a4a", "constructorArguments": [], - "created": "2023-12-08T16:27:13.655Z" + "created": "2023-12-10T22:48:53.420Z" }, "limitPositionsLib": { "contractName": "LimitPositions", - "contractAddress": "0x414b73f989e7ca0653b5c98186749a348405e6d5", + "contractAddress": "0xf04bf4e3e8157ba5b91bfda16e21be770e7ac790", "constructorArguments": [], - "created": "2023-12-08T16:27:18.380Z" + "created": "2023-12-10T22:48:54.635Z" }, "limitPoolManager": { "contractName": "LimitPoolManager", - "contractAddress": "0xd50b04a5693f2d026d589bf239609bf5b8346adc", + "contractAddress": "0x441bb7b76c9932d5c65d7a78b3413978d7370761", "constructorArguments": [], - "created": "2023-12-08T16:27:23.238Z" + "created": "2023-12-10T22:49:00.148Z" }, "limitPoolFactory": { "contractName": "LimitPoolFactory", - "contractAddress": "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", + "contractAddress": "0xd28d620853af6837d76f1360dc65229d57ba5435", "constructorArguments": [ - "0xd50B04a5693F2d026D589Bf239609Bf5B8346AdC" + "0x441Bb7B76c9932d5c65D7A78B3413978d7370761" ], - "created": "2023-12-08T16:27:28.092Z" + "created": "2023-12-10T22:49:01.378Z" }, "swapCall": { "contractName": "SwapCall", - "contractAddress": "0xd58628f15d4391a036c578638105e67f857b6770", + "contractAddress": "0x14ce728bf96d5ec0976f332605fe80a42ec0b244", "constructorArguments": [], - "created": "2023-12-08T16:27:32.840Z" + "created": "2023-12-10T22:49:06.947Z" }, "mintRangeCall": { "contractName": "MintRangeCall", - "contractAddress": "0xe7623a3a8d94296f9fe2b0c1fdfbcd36cc9c8d32", + "contractAddress": "0x83eea57c3536a7ec64ead2c67d5b6bfeb6593720", "constructorArguments": [], - "created": "2023-12-08T16:27:37.650Z" + "created": "2023-12-10T22:49:12.892Z" }, "burnRangeCall": { "contractName": "BurnRangeCall", - "contractAddress": "0x26323c7e4f000227932d3ed640ea79d6027701de", + "contractAddress": "0xc66eebf930bdcbe450fbdd56deff8caea9e9943f", "constructorArguments": [], - "created": "2023-12-08T16:27:42.664Z" + "created": "2023-12-10T22:49:18.718Z" }, "mintLimitCall": { "contractName": "MintLimitCall", - "contractAddress": "0x912c3e7d140e4ec85eb8e3910447acda8654d523", + "contractAddress": "0x4255e7a88bae87d2a5ff3ba317c6d512562f7078", "constructorArguments": [], - "created": "2023-12-08T16:27:47.884Z" + "created": "2023-12-10T22:49:24.731Z" }, "burnLimitCall": { "contractName": "BurnLimitCall", - "contractAddress": "0x7024879eda80577cbc0cb039ad3c7081f38abb41", + "contractAddress": "0xe373e46f8900a17bb8e3880d00791ac06d975c0a", "constructorArguments": [], - "created": "2023-12-08T16:27:52.852Z" + "created": "2023-12-10T22:49:30.609Z" }, "snapshotLimitCall": { "contractName": "SnapshotLimitCall", - "contractAddress": "0xa826f06c47597549faa74d8916ef4da8f417f16c", + "contractAddress": "0x898b8e21901cfda9e3524f39339cd7700f711b6f", "constructorArguments": [], - "created": "2023-12-08T16:27:57.638Z" + "created": "2023-12-10T22:49:36.596Z" }, "quoteCall": { "contractName": "QuoteCall", - "contractAddress": "0x92192101f6c5138160c093882b0fa502dee889cd", + "contractAddress": "0xfaf9ea467dcb1581c66939a2781bbbbae0b2e615", "constructorArguments": [], - "created": "2023-12-08T16:28:02.421Z" + "created": "2023-12-10T22:49:42.393Z" }, "feesCall": { "contractName": "FeesCall", - "contractAddress": "0x4dc4f1a2a3303e40a07c3aac4919a97f33ab01eb", + "contractAddress": "0x1a16415867fde435d3f8a6631ef308ae7bf96b3b", "constructorArguments": [], - "created": "2023-12-08T16:28:07.294Z" + "created": "2023-12-10T22:49:43.270Z" }, "sampleCall": { "contractName": "SampleCall", - "contractAddress": "0xc1e9a857e96aeefe28f8340cf82de7801bd95a77", + "contractAddress": "0xfa20280428016b781a294ac88eee3a8ead580039", "constructorArguments": [], - "created": "2023-12-08T16:28:12.093Z" + "created": "2023-12-10T22:49:44.564Z" }, "snapshotRangeCall": { "contractName": "SnapshotRangeCall", - "contractAddress": "0x96f8d308db8f86fccbef8c57df7bbad6ee4c7d89", + "contractAddress": "0xa801ca07b20ef56251739d9b3197bfd02cfa20e8", "constructorArguments": [], - "created": "2023-12-08T16:28:16.860Z" + "created": "2023-12-10T22:49:50.041Z" }, "limitPoolImpl": { "contractName": "LimitPool", - "contractAddress": "0x215afe9a2d7e1f8b82d221213d880f7e28e1dd03", + "contractAddress": "0x8212ce17d271ed2378cc59bd234b670db7ad301e", "constructorArguments": [ - "0x9F479560cd8A531E6C0fe04521Cb246264fE6b71" + "0xD28d620853aF6837D76F1360Dc65229D57bA5435" ], - "created": "2023-12-08T16:28:21.902Z" + "created": "2023-12-10T22:49:51.346Z" }, "positionERC1155": { "contractName": "PositionERC1155", - "contractAddress": "0x3f96856927e6192291717d1f43cc5b264d93caa8", + "contractAddress": "0xd987de01302ff6a7acc20f1ac2d2239317169df5", "constructorArguments": [ - "0x9F479560cd8A531E6C0fe04521Cb246264fE6b71" + "0xD28d620853aF6837D76F1360Dc65229D57bA5435" ], - "created": "2023-12-08T16:28:26.710Z" + "created": "2023-12-10T22:49:56.886Z" }, "limitPool": { "contractName": "LimitPool", - "contractAddress": "0xb3db76a4e1986a433dc3f8fcb7fdc3b2cdcf490f", + "contractAddress": "0x5330588b4efd339b8ea441afedf09aab851f2d48", "constructorArguments": [ "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", "1000", 0 ], - "created": "2023-12-08T16:28:51.180Z" + "created": "2023-12-10T22:50:00.921Z" }, "poolRouter": { "contractName": "PoolsharkRouter", - "contractAddress": "0x62e0671022af1b2e705f08b282767c57d29c7c4c", + "contractAddress": "0x5357308a5a4db6c65fa6c4b122dfc3d749f2dd6a", "constructorArguments": [ - "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", + "0xd28d620853af6837d76f1360dc65229d57ba5435", "0x0000000000000000000000000000000000000000", "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" ], - "created": "2023-12-09T02:36:07.159Z" + "created": "2023-12-10T22:51:39.430Z" }, "rangeStaker": { "contractName": "RangeStaker", - "contractAddress": "0xde95e92dd151c39eb51cfae80fdff4d6c32c1fad", + "contractAddress": "0x373bf43249bed0518119d29b80c7d9c68d98cfc2", "constructorArguments": [ { - "limitPoolFactory": "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", + "limitPoolFactory": "0xd28d620853af6837d76f1360dc65229d57ba5435", "startTime": 0, "endTime": 2000707154 } ], - "created": "2023-12-08T17:57:36.413Z" + "created": "2023-12-10T22:51:45.136Z" } }, "arb_goerli": { @@ -563,7 +563,5 @@ "created": "2023-10-02T23:25:47.853Z" } }, - "undefined": { - - } + "undefined": {} } \ No newline at end of file diff --git a/scripts/config/networkConfigs.ts b/scripts/config/networkConfigs.ts index fc490818..5f97f68b 100644 --- a/scripts/config/networkConfigs.ts +++ b/scripts/config/networkConfigs.ts @@ -40,6 +40,13 @@ export const NETWORK_CONFIGS: NetworkConfigs = { url: process.env.ARBITRUM_GOERLI_URL || '', accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], }, + arb_one: { + chainId: 42161, + gas: 9000000, + gasPrice: 1_000_000_000, + url: process.env.ARBITRUM_ONE_URL || '', + accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], + }, scrollSepolia : { chainId: 534353, gas: 9000000, diff --git a/scripts/constants/supportedNetworks.ts b/scripts/constants/supportedNetworks.ts index 86a43fdf..4ed8b12d 100644 --- a/scripts/constants/supportedNetworks.ts +++ b/scripts/constants/supportedNetworks.ts @@ -5,6 +5,7 @@ export enum SUPPORTED_NETWORKS { /* Testnet Supported Networks */ GOERLI = 'goerli', ARB_GOERLI = 'arb_goerli', + ARB_ONE = 'arb_one', SCROLL_SEPOLIA = 'scrollSepolia' } diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index d9061c2f..e93bdd51 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -33,7 +33,7 @@ export class InitialSetup { private deployContracts = false private deployPools = false private deployRouter = true - private deployStaker = false + private deployStaker = true constructor() { this.deployAssist = new DeployAssist() @@ -385,7 +385,7 @@ export class InitialSetup { tokenIn: hre.props.token0.address, tokenOut: hre.props.token1.address, swapFee: '1000', - startPrice: '3825621589765822368824140' + startPrice: '3854594635971501655978346' }); await createPoolTxn.wait(); From b5c360abadb09c5fd221d45c0e2fac743a8700cd Mon Sep 17 00:00:00 2001 From: alphak3y Date: Mon, 11 Dec 2023 05:33:16 -0700 Subject: [PATCH 14/39] arb mainnet final deployment --- scripts/autogen/contract-deployments-keys.ts | 8 -------- subgraph/package.json | 2 +- subgraph/src/constants/arbitrum.ts | 19 +++++++++++-------- subgraph/subgraph.yaml | 15 +++++++-------- .../templates/arbitrum.subgraph.template.yaml | 13 ++++++------- 5 files changed, 25 insertions(+), 32 deletions(-) diff --git a/scripts/autogen/contract-deployments-keys.ts b/scripts/autogen/contract-deployments-keys.ts index 538678f8..94669ef4 100644 --- a/scripts/autogen/contract-deployments-keys.ts +++ b/scripts/autogen/contract-deployments-keys.ts @@ -1,14 +1,6 @@ import { ContractDeploymentsKey } from '../util/files/contractDeploymentsJson' export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ - { - networkName: 'arb_one', - objectName: 'tokenPay' - }, - { - networkName: 'arb_one', - objectName: 'tokenQuote' - }, { networkName: 'arb_one', objectName: 'tickMapLib' diff --git a/subgraph/package.json b/subgraph/package.json index 119aba82..3e60b798 100644 --- a/subgraph/package.json +++ b/subgraph/package.json @@ -26,7 +26,7 @@ "deploy-op-test": "graph deploy --product hosted-service alphak3y/poolshark-cover-op-goerli", "deploy-hs": "graph deploy --product hosted-service alphak3y/poolshark-limit", "deploy-local": "graph deploy example --ipfs http://localhost:5001 --node http://127.0.0.1:8020", - "deploy-arb-one": "graph deploy --version-label v0.1.0 --node https://api.graph-ams.p2pify.com/234b623901aae1c380fb69e4d7534129/deploy --ipfs https://api.graph-ams.p2pify.com/234b623901aae1c380fb69e4d7534129/ipfs limit-arbitrum-test", + "deploy-arb-one": "graph deploy --version-label v0.1.1 --node https://api.graph-ams.p2pify.com/e57bc8ecd57c8bac9bfc6222cb8257e7/deploy --ipfs https://api.graph-ams.p2pify.com/e57bc8ecd57c8bac9bfc6222cb8257e7/ipfs limit-arbitrum", "test": "graph codegen; graph test -v 0.2.0", "prepare:subgraph": "mustache ./config/${NETWORK}.json ./templates/${BLOCKCHAIN}.subgraph.template.yaml > subgraph.yaml && rm -rf generated && npm run generate:schema && npm run subgraph:codegen && npm run subgraph:build", "subgraph:deploy:chainstack:test": "graph deploy --version-label v0.3.1 --node https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/deploy --ipfs https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/ipfs limit-arbitrumGoerli-test", diff --git a/subgraph/src/constants/arbitrum.ts b/subgraph/src/constants/arbitrum.ts index 7d635d1b..6349d3bd 100644 --- a/subgraph/src/constants/arbitrum.ts +++ b/subgraph/src/constants/arbitrum.ts @@ -2,8 +2,17 @@ import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts' import { LimitPoolFactory as FactoryContract } from '../../generated/templates/LimitPoolTemplate/LimitPoolFactory' -export let FACTORY_ADDRESS = '0x9f479560cd8a531e6c0fe04521cb246264fe6b71' -export let RANGE_STAKER_ADDRESS = '0xde95e92dd151c39eb51cfae80fdff4d6c32c1fad' +// -------------- START REQUIRED CONFIG PER DEPLOYMENT -------------- +export let FACTORY_ADDRESS = '0xd28d620853af6837d76f1360dc65229d57ba5435' +export let RANGE_STAKER_ADDRESS = '0x373bf43249bed0518119d29b80c7d9c68d98cfc2' +// used for safe eth pricing +export const STABLE_POOL_ADDRESS = '0x5330588b4efd339b8ea441afedf09aab851f2d48' +// -------------- END REQUIRED CONFIG PER DEPLOYMENT -------------- + +// determines which token to use for eth <-> usd rate, true means stable is token0 in pool above +export const STABLE_IS_TOKEN_0 = false + +// chain WETH address export let WETH_ADDRESS = '0x82af49447d8a07e3bd95bd0d56f35241523fbab1' // tokens where USD value is safe to use for globals @@ -36,12 +45,6 @@ export let STABLE_COINS: string[] = [ '0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9' // USDT ] -// used for safe eth pricing -export const STABLE_POOL_ADDRESS = '0xb3db76a4e1986a433dc3f8fcb7fdc3b2cdcf490f' - -// determines which token to use for eth <-> usd rate, true means stable is token0 in pool above -export const STABLE_IS_TOKEN_0 = false - // minimum eth required in pool to count usd values towards global prices export let MINIMUM_ETH_LOCKED = BigDecimal.fromString('0') diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 4dd1efb8..337c3f1d 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -88,7 +88,6 @@ templates: name: RangeStakerTemplate network: arbitrum-one source: - # address: '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' abi: RangeStaker mapping: kind: ethereum/events @@ -116,9 +115,9 @@ dataSources: name: LimitPoolFactory network: arbitrum-one source: - address: '0x9f479560cd8a531e6c0fe04521cb246264fe6b71' + address: '0xd28d620853af6837d76f1360dc65229d57ba5435' abi: LimitPoolFactory - startBlock: 158133516 + startBlock: 158864748 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -145,9 +144,9 @@ dataSources: name: LimitPoolManager network: arbitrum-one source: - address: '0xd50b04a5693f2d026d589bf239609bf5b8346adc' + address: '0x441bb7b76c9932d5c65d7a78b3413978d7370761' abi: LimitPoolManager - startBlock: 158133499 + startBlock: 158864726 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -180,9 +179,9 @@ dataSources: name: PoolsharkRouter network: arbitrum-one source: - address: '0x7183ffeefc0883d499c52c1a1dff1af716467a46' + address: '0x5357308a5a4db6c65fa6c4b122dfc3d749f2dd6a' abi: PoolsharkRouter - startBlock: 158133499 + startBlock: 158865312 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -195,4 +194,4 @@ dataSources: file: ./abis/PoolsharkRouter.json eventHandlers: - event: RouterDeployed(address,address,address) - handler: handleRouterDeployed + handler: handleRouterDeployed \ No newline at end of file diff --git a/subgraph/templates/arbitrum.subgraph.template.yaml b/subgraph/templates/arbitrum.subgraph.template.yaml index c26e58a9..3007067f 100644 --- a/subgraph/templates/arbitrum.subgraph.template.yaml +++ b/subgraph/templates/arbitrum.subgraph.template.yaml @@ -88,7 +88,6 @@ templates: name: RangeStakerTemplate network: arbitrum-one source: - # address: '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' abi: RangeStaker mapping: kind: ethereum/events @@ -116,9 +115,9 @@ dataSources: name: LimitPoolFactory network: arbitrum-one source: - address: '0x9f479560cd8a531e6c0fe04521cb246264fe6b71' + address: '0xd28d620853af6837d76f1360dc65229d57ba5435' abi: LimitPoolFactory - startBlock: 158133516 + startBlock: 158864748 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -145,9 +144,9 @@ dataSources: name: LimitPoolManager network: arbitrum-one source: - address: '0xd50b04a5693f2d026d589bf239609bf5b8346adc' + address: '0x441bb7b76c9932d5c65d7a78b3413978d7370761' abi: LimitPoolManager - startBlock: 158133499 + startBlock: 158864726 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -180,9 +179,9 @@ dataSources: name: PoolsharkRouter network: arbitrum-one source: - address: '0x7183ffeefc0883d499c52c1a1dff1af716467a46' + address: '0x5357308a5a4db6c65fa6c4b122dfc3d749f2dd6a' abi: PoolsharkRouter - startBlock: 158133499 + startBlock: 158865312 mapping: kind: ethereum/events apiVersion: 0.0.6 From bd4ac8221b1a24b76065e48fb6b636d0e7495983 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Mon, 18 Dec 2023 09:51:21 -0700 Subject: [PATCH 15/39] vfin deployment --- subgraph/schema.graphql | 9 +++++++++ subgraph/src/mappings/utils/loads.ts | 24 +++++++++++++++++++++++- subgraph/subgraph.yaml | 22 +++++++++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/subgraph/schema.graphql b/subgraph/schema.graphql index ee1a45f7..196d233f 100644 --- a/subgraph/schema.graphql +++ b/subgraph/schema.graphql @@ -371,3 +371,12 @@ type UserSeasonReward @entity { stakingPoints: BigInt! } +type VFinPosition @entity { + # vFinAddress + positionId + id: ID! + + owner: Bytes! + positionId: BigInt! + vFinAddress: Bytes! +} + diff --git a/subgraph/src/mappings/utils/loads.ts b/subgraph/src/mappings/utils/loads.ts index 5f046b23..bc4650a1 100644 --- a/subgraph/src/mappings/utils/loads.ts +++ b/subgraph/src/mappings/utils/loads.ts @@ -1,5 +1,5 @@ import { Address, BigDecimal, BigInt, Bytes, ethereum, log } from '@graphprotocol/graph-ts' -import { LimitPool, LimitPoolFactory, LimitPoolManager, LimitPosition, Token, FeeTier, BasePrice, RangePosition, RangeTick, Transaction, LimitTick, Swap, CompoundRangeLog, MintRangeLog, BurnRangeLog, PoolRouter, TvlUpdateLog, HistoricalOrder, TotalSeasonReward, UserSeasonReward, LimitPoolToken } from '../../../generated/schema' +import { LimitPool, LimitPoolFactory, LimitPoolManager, LimitPosition, Token, FeeTier, BasePrice, RangePosition, RangeTick, Transaction, LimitTick, Swap, CompoundRangeLog, MintRangeLog, BurnRangeLog, PoolRouter, TvlUpdateLog, HistoricalOrder, TotalSeasonReward, UserSeasonReward, LimitPoolToken, VFinPosition } from '../../../generated/schema' import { ONE_BD } from '../../constants/arbitrum' import { fetchTokenSymbol, @@ -260,6 +260,28 @@ export function safeLoadPoolRouter(routerAddress: string): LoadPoolRouterRet { } } +class LoadVFinPositionRet { + entity: VFinPosition + exists: boolean +} +export function safeLoadVFinPosition(vFinAddress: string, positionId: BigInt): LoadVFinPositionRet { + let exists = true + + let vFinPositionId = vFinAddress.concat(positionId.toString()) + + let vFinPositionEntity = VFinPosition.load(vFinPositionId) + + if (!vFinPositionEntity) { + vFinPositionEntity = new VFinPosition(vFinPositionId) + exists = false + } + + return { + entity: vFinPositionEntity, + exists: exists, + } +} + class LoadLimitPositionRet { entity: LimitPosition exists: boolean diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 337c3f1d..1af329f1 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -194,4 +194,24 @@ dataSources: file: ./abis/PoolsharkRouter.json eventHandlers: - event: RouterDeployed(address,address,address) - handler: handleRouterDeployed \ No newline at end of file + handler: handleRouterDeployed + - kind: ethereum/contract + name: vFIN + network: arbitrum-one + source: + address: '0xFA3e62Aae5DE014c4CD20377Ec90Eb8e59d31169' + abi: vFIN + startBlock: 161266335 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/vfin.ts + entities: + - VestedFin + abis: + - name: vFIN + file: ./abis/vFIN.json + eventHandlers: + - event: Transfer(indexed address,indexed address,indexed uint256) + handler: handleTransfer \ No newline at end of file From b338c13c901b44b99b192326f1cbd21f2f68dccf Mon Sep 17 00:00:00 2001 From: alphak3y Date: Tue, 19 Dec 2023 15:26:37 -0700 Subject: [PATCH 16/39] deploy tge function --- contracts/interfaces/IPool.sol | 10 +++++ contracts/utils/PoolsharkRouter.sol | 59 +++++++++++++++++++++++++++++ subgraph/src/mappings/vfin.ts | 22 +++++++++++ test/contracts/rangepool.ts | 31 +++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 subgraph/src/mappings/vfin.ts diff --git a/contracts/interfaces/IPool.sol b/contracts/interfaces/IPool.sol index adbfd72a..fa52cde0 100644 --- a/contracts/interfaces/IPool.sol +++ b/contracts/interfaces/IPool.sol @@ -54,4 +54,14 @@ interface IPool is PoolsharkStructs { function token0() external view returns (address token0); function token1() external view returns (address token1); + + function globalState() external view returns ( + RangePoolState memory pool, + LimitPoolState memory pool0, + LimitPoolState memory pool1, + uint128 liquidityGlobal, + uint32 positionIdNext, + uint32 epoch, + uint8 unlocked + ); } diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index 13498a9c..de8f024d 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -742,6 +742,65 @@ contract PoolsharkRouter is return locals.prunedResults; } + function deployTge() external { + address tgePool = 0x53C314F6BCdA3C253ECA949B0D66b9679c2bB219; + // read pool price + RangePoolState memory tgePoolState; + ( + tgePoolState, + ,,,,, + ) = IPool(tgePool).globalState(); + if (tgePoolState.price < 2358285847295149069702956253974) { + // move pool price up if below + SwapParams memory swapParams = SwapParams({ + to: msg.sender, + priceLimit: 2358285847295149069702956253974, + amount: 10e18, + exactIn: true, + zeroForOne: false, + callbackData: abi.encode( + SwapCallbackData({ + sender: msg.sender, + recipient: msg.sender, + wrapped: false + }) + ) + }); + IPool(tgePool).swap(swapParams); + } else if (tgePoolState.price < 2358285847295149069702956253974) { + // move pool price down if above + SwapParams memory swapParams = SwapParams({ + to: msg.sender, + priceLimit: 2358285847295149069702956253974, + amount: 1e18, + exactIn: true, + zeroForOne: true, + callbackData: abi.encode( + SwapCallbackData({ + sender: msg.sender, + recipient: msg.sender, + wrapped: false + }) + ) + }); + IPool(tgePool).swap(swapParams); + } + MintRangeParams memory mintRangeParams = MintRangeParams({ + to: msg.sender, + lower: 44850, + upper: 77040, + positionId: 0, + amount0: 31568903742987611804, + amount1: 51999999999999999999996, + callbackData: abi.encode(MintRangeCallbackData({ + sender: msg.sender, + recipient: msg.sender, + wrapped: false + })) + }); + IRangePool(tgePool).mintRange(mintRangeParams); + } + function multiCall(address[] memory pools, SwapParams[] memory params) external { diff --git a/subgraph/src/mappings/vfin.ts b/subgraph/src/mappings/vfin.ts new file mode 100644 index 00000000..ce864816 --- /dev/null +++ b/subgraph/src/mappings/vfin.ts @@ -0,0 +1,22 @@ +import { RouterDeployed } from "../../generated/PoolsharkRouter/PoolsharkRouter"; +import { Transfer } from "../../generated/vFIN/vFIN"; +import { safeLoadPoolRouter, safeLoadVFinPosition } from "./utils/loads"; + +export function handleTransfer(event: Transfer): void { + let fromParam = event.params.from + let toParam = event.params.to + let idParam = event.params.id + let vFinAddress = event.address + + let loadVFinPosition = safeLoadVFinPosition(vFinAddress.toHex(), idParam) + + let vFinPosition = loadVFinPosition.entity + + if (!loadVFinPosition.exists) { + vFinPosition.vFinAddress = vFinAddress + vFinPosition.positionId = idParam + } + vFinPosition.owner = toParam + + vFinPosition.save() +} \ No newline at end of file diff --git a/test/contracts/rangepool.ts b/test/contracts/rangepool.ts index e69bacb8..72f1c744 100644 --- a/test/contracts/rangepool.ts +++ b/test/contracts/rangepool.ts @@ -18,6 +18,7 @@ import { getTickLiquidity, } from '../utils/contracts/rangepool' import { RangePoolState, ZERO_ADDRESS } from '../utils/contracts/limitpool' +import { parseUnits } from 'ethers/lib/utils' alice: SignerWithAddress describe('RangePool Exact In Tests', function () { @@ -275,6 +276,36 @@ describe('RangePool Exact In Tests', function () { }) }); + // it.only('token1 - Should calculate liquidity for TGE position', async function () { + + // await validateSwap({ + // signer: hre.props.alice, + // recipient: hre.props.alice.address, + // zeroForOne: false, + // amount: tokenAmount.div(10), + // sqrtPriceLimitX96: BigNumber.from('2358285847295149069702956253974'), // 2.50 USD per FIN + // balanceInDecrease: BigNumber.from('0'), + // balanceOutIncrease: BigNumber.from('0'), + // revertMessage: '', + // }) + + // await mintSigners20(hre.props.token0, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) + // await mintSigners20(hre.props.token1, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) + + // const aliceId = await validateMint({ + // signer: hre.props.alice, + // recipient: hre.props.alice.address, + // lower: '44850', + // upper: '77040', + // amount0: parseUnits('52000', 18), + // amount1: parseUnits('52000', 18), + // balance0Decrease: BigNumber.from('31568903742987611804'), // 49.84 = ~110495.28 USD + // balance1Decrease: BigNumber.from('51999999999999999999996'), // 40000 = 100000 USD + // liquidityIncrease: BigNumber.from('2555287091759866264142'), + // revertMessage: '', + // }) + // }) + it('token1 - Should mint, swap, and burn 14', async function () { const aliceId = await validateMint({ From 7be7522fd8f8d298815ddead6356c51a705dd25f Mon Sep 17 00:00:00 2001 From: alphak3y Date: Wed, 20 Dec 2023 18:47:54 -0700 Subject: [PATCH 17/39] tge deployment --- contracts/utils/PoolsharkRouter.sol | 70 +- scripts/autogen/contract-deployments-keys.ts | 72 +- scripts/autogen/contract-deployments.json | 4 +- subgraph/abis/vFIN.json | 684 +++++++++++++++++++ tasks/deploy/utils/mintPosition.ts | 40 +- test/contracts/rangepool.ts | 54 +- test/contracts/tgedeployer.ts | 136 ++++ test/utils/contracts/poolsharkrouter.ts | 128 +++- test/utils/setup/initialSetup.ts | 2 +- 9 files changed, 1044 insertions(+), 146 deletions(-) create mode 100644 subgraph/abis/vFIN.json create mode 100644 test/contracts/tgedeployer.ts diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index de8f024d..30d2f3b0 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -742,8 +742,29 @@ contract PoolsharkRouter is return locals.prunedResults; } - function deployTge() external { - address tgePool = 0x53C314F6BCdA3C253ECA949B0D66b9679c2bB219; + + function multiCall(address[] memory pools, SwapParams[] memory params) + external + { + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { + params[i].callbackData = abi.encode( + SwapCallbackData({ + sender: msg.sender, + recipient: params[i].to, + wrapped: true + }) + ); + ICoverPool(pools[i]).swap(params[i]); + unchecked { + ++i; + } + } + } + + function deployTge(address tgePool, address staker) external { + // address staker = 0x373bF43249BeD0518119d29B80c7D9C68d98cFC2; // read pool price RangePoolState memory tgePoolState; ( @@ -755,7 +776,7 @@ contract PoolsharkRouter is SwapParams memory swapParams = SwapParams({ to: msg.sender, priceLimit: 2358285847295149069702956253974, - amount: 10e18, + amount: 1000e18, exactIn: true, zeroForOne: false, callbackData: abi.encode( @@ -785,39 +806,36 @@ contract PoolsharkRouter is }); IPool(tgePool).swap(swapParams); } + // read pool price + ( + tgePoolState, + ,,,,, + ) = IPool(tgePool).globalState(); + if(tgePoolState.price != 2358285847295149069702956253974) + require(false, 'PoolPriceMismatch()'); + MintRangeCallbackData memory callbackData = MintRangeCallbackData({ + sender: msg.sender, + recipient: staker != address(0) ? staker : msg.sender, + wrapped: false + }); MintRangeParams memory mintRangeParams = MintRangeParams({ - to: msg.sender, + to: staker, lower: 44850, upper: 77040, positionId: 0, amount0: 31568903742987611804, amount1: 51999999999999999999996, - callbackData: abi.encode(MintRangeCallbackData({ - sender: msg.sender, - recipient: msg.sender, - wrapped: false - })) + callbackData: abi.encode(callbackData) }); IRangePool(tgePool).mintRange(mintRangeParams); - } - - function multiCall(address[] memory pools, SwapParams[] memory params) - external - { - if (pools.length != params.length) - require(false, 'InputArrayLengthsMismatch()'); - for (uint256 i = 0; i < pools.length; ) { - params[i].callbackData = abi.encode( - SwapCallbackData({ - sender: msg.sender, - recipient: params[i].to, - wrapped: true + if (staker != address(0)) { + IRangeStaker(staker).stakeRange( + StakeRangeParams({ + to: msg.sender, + pool: tgePool, + positionId: 0 }) ); - ICoverPool(pools[i]).swap(params[i]); - unchecked { - ++i; - } } } diff --git a/scripts/autogen/contract-deployments-keys.ts b/scripts/autogen/contract-deployments-keys.ts index 94669ef4..d1ee60e6 100644 --- a/scripts/autogen/contract-deployments-keys.ts +++ b/scripts/autogen/contract-deployments-keys.ts @@ -3,75 +3,7 @@ import { ContractDeploymentsKey } from '../util/files/contractDeploymentsJson' export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ { networkName: 'arb_one', - objectName: 'tickMapLib' - }, - { - networkName: 'arb_one', - objectName: 'ticksLib' - }, - { - networkName: 'arb_one', - objectName: 'limitPositionsLib' - }, - { - networkName: 'arb_one', - objectName: 'limitPoolManager' - }, - { - networkName: 'arb_one', - objectName: 'limitPoolFactory' - }, - { - networkName: 'arb_one', - objectName: 'swapCall' - }, - { - networkName: 'arb_one', - objectName: 'mintRangeCall' - }, - { - networkName: 'arb_one', - objectName: 'burnRangeCall' - }, - { - networkName: 'arb_one', - objectName: 'mintLimitCall' - }, - { - networkName: 'arb_one', - objectName: 'burnLimitCall' - }, - { - networkName: 'arb_one', - objectName: 'snapshotLimitCall' - }, - { - networkName: 'arb_one', - objectName: 'quoteCall' - }, - { - networkName: 'arb_one', - objectName: 'feesCall' - }, - { - networkName: 'arb_one', - objectName: 'sampleCall' - }, - { - networkName: 'arb_one', - objectName: 'snapshotRangeCall' - }, - { - networkName: 'arb_one', - objectName: 'limitPoolImpl' - }, - { - networkName: 'arb_one', - objectName: 'positionERC1155' - }, - { - networkName: 'arb_one', - objectName: 'limitPool' + objectName: 'poolRouter' }, { networkName: 'arb_one', @@ -79,6 +11,6 @@ export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ }, { networkName: 'arb_one', - objectName: 'rangeStaker' + objectName: 'poolRouter' }, ]; \ No newline at end of file diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index 6dac502b..fd98a3c4 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -175,13 +175,13 @@ }, "poolRouter": { "contractName": "PoolsharkRouter", - "contractAddress": "0x5357308a5a4db6c65fa6c4b122dfc3d749f2dd6a", + "contractAddress": "0x122372809577c99513f17a4555b3856fd2677dfa", "constructorArguments": [ "0xd28d620853af6837d76f1360dc65229d57ba5435", "0x0000000000000000000000000000000000000000", "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" ], - "created": "2023-12-10T22:51:39.430Z" + "created": "2023-12-20T17:05:56.282Z" }, "rangeStaker": { "contractName": "RangeStaker", diff --git a/subgraph/abis/vFIN.json b/subgraph/abis/vFIN.json new file mode 100644 index 00000000..efd37f34 --- /dev/null +++ b/subgraph/abis/vFIN.json @@ -0,0 +1,684 @@ +[ + { + "inputs": [ + { + "internalType": "address", + "name": "_owner", + "type": "address" + }, + { + "internalType": "address", + "name": "_finAddress", + "type": "address" + }, + { + "internalType": "address", + "name": "_tellerAddress", + "type": "address" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "inputs": [], + "name": "AccountBalanceOverflow", + "type": "error" + }, + { + "inputs": [], + "name": "BalanceQueryForZeroAddress", + "type": "error" + }, + { + "inputs": [], + "name": "NotOwnerNorApproved", + "type": "error" + }, + { + "inputs": [], + "name": "TokenAlreadyExists", + "type": "error" + }, + { + "inputs": [], + "name": "TokenDoesNotExist", + "type": "error" + }, + { + "inputs": [], + "name": "TransferFromIncorrectOwner", + "type": "error" + }, + { + "inputs": [], + "name": "TransferToNonERC721ReceiverImplementer", + "type": "error" + }, + { + "inputs": [], + "name": "TransferToZeroAddress", + "type": "error" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "account", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "operator", + "type": "address" + }, + { + "indexed": false, + "internalType": "bool", + "name": "isApproved", + "type": "bool" + } + ], + "name": "ApprovalForAll", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "previousOwner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "OwnershipTransferred", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "inputs": [], + "name": "BOND_TOKEN_ID", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "BOND_TOTAL_SUPPLY", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "account", + "type": "address" + }, + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "internalType": "uint256", + "name": "result", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint32", + "name": "positionId", + "type": "uint32" + } + ], + "name": "claim", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "internalType": "uint32", + "name": "positionId", + "type": "uint32" + } + ], + "name": "exchangeBond", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "finAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "getApproved", + "outputs": [ + { + "internalType": "address", + "name": "result", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "internalType": "address", + "name": "operator", + "type": "address" + } + ], + "name": "isApprovedForAll", + "outputs": [ + { + "internalType": "bool", + "name": "result", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "name", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "operator", + "type": "address" + }, + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "value", + "type": "uint256" + }, + { + "internalType": "bytes", + "name": "data", + "type": "bytes" + } + ], + "name": "onERC1155Received", + "outputs": [ + { + "internalType": "bytes4", + "name": "", + "type": "bytes4" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "owner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "ownerOf", + "outputs": [ + { + "internalType": "address", + "name": "result", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "renounceOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "safeTransferFrom", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + }, + { + "internalType": "bytes", + "name": "data", + "type": "bytes" + } + ], + "name": "safeTransferFrom", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "operator", + "type": "address" + }, + { + "internalType": "bool", + "name": "isApproved", + "type": "bool" + } + ], + "name": "setApprovalForAll", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "startLinearVest", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes4", + "name": "interfaceId", + "type": "bytes4" + } + ], + "name": "supportsInterface", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [], + "name": "symbol", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [], + "name": "tellerAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "tokenURI", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "transferOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "vestEndTime", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "vestPositions", + "outputs": [ + { + "internalType": "uint128", + "name": "amount", + "type": "uint128" + }, + { + "internalType": "uint32", + "name": "lastClaimTimestamp", + "type": "uint32" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "vestStartTime", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "vestState", + "outputs": [ + { + "internalType": "uint32", + "name": "idNext", + "type": "uint32" + }, + { + "internalType": "bool", + "name": "started", + "type": "bool" + }, + { + "internalType": "bool", + "name": "ended", + "type": "bool" + }, + { + "internalType": "bool", + "name": "withdrawn", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint32", + "name": "positionId", + "type": "uint32" + } + ], + "name": "viewClaim", + "outputs": [ + { + "internalType": "uint256", + "name": "vestedAmount", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bool", + "name": "redeem", + "type": "bool" + } + ], + "name": "withdraw", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } +] \ No newline at end of file diff --git a/tasks/deploy/utils/mintPosition.ts b/tasks/deploy/utils/mintPosition.ts index b9381425..e7f3d3a5 100644 --- a/tasks/deploy/utils/mintPosition.ts +++ b/tasks/deploy/utils/mintPosition.ts @@ -31,8 +31,8 @@ export class MintPosition { console.log(this.nonce) await this.initialSetup.readLimitPoolSetup(this.nonce) console.log('read positions') - const token0Amount = ethers.utils.parseUnits('100', await hre.props.token0.decimals()) - const token1Amount = ethers.utils.parseUnits('100', await hre.props.token1.decimals()) + + // const token1Amount = ethers.utils.parseUnits('100', await hre.props.token1.decimals()) // await mintSigners20(hre.props.token0, token0Amount.mul(10000), [hre.props.alice]) // await mintSigners20(hre.props.token1, token1Amount.mul(10000), [hre.props.alice]) @@ -98,25 +98,27 @@ export class MintPosition { // revertMessage: '', // }) // return + const token0Amount = ethers.utils.parseUnits('1', 16) const signer = hre.props.alice - // let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, token1Amount.mul(1000)) - // await approveTxn.wait() + const amountIn = token0Amount + let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, amountIn) + await approveTxn.wait() // // for (let i=0; i < 20; i++) { const zeroForOne = true - const amountIn = token1Amount.mul(1000) - const priceLimit = BigNumber.from('3848890000000000000000000') + + const priceLimit = BigNumber.from('2358285847295149069702956253974') let txn = await hre.props.poolRouter .connect(signer) .multiSwapSplit( - ['0xc641fbc8f6ab236249c99bf83b9be34b753c7882'], + ['0x53C314F6BCdA3C253ECA949B0D66b9679c2bB219'], [ { to: signer.address, priceLimit: priceLimit, - amount: 1, + amount: amountIn, zeroForOne: zeroForOne, - exactIn: true, + exactIn: false, callbackData: ethers.utils.formatBytes32String('') }, ]) @@ -128,16 +130,16 @@ export class MintPosition { // const globalStateAfter = (await hre.props.limitPool.globalState()) // console.log('sample state', globalStateAfter.pool.samples.index, globalStateAfter.pool.samples.count, globalStateAfter.pool.samples.countMax, globalStateAfter.pool.tickAtPrice) - await validateSwap({ - signer: hre.props.alice, - recipient: hre.props.alice.address, - zeroForOne: false, - amountIn: token1Amount.mul(100), - priceLimit: BigNumber.from('3256300000000000000000000'), - balanceInDecrease: token1Amount.mul(10000), - balanceOutIncrease: '15641085361593105857', - revertMessage:'', - }) + // await validateSwap({ + // signer: hre.props.alice, + // recipient: hre.props.alice.address, + // zeroForOne: false, + // amountIn: token1Amount.mul(100), + // priceLimit: BigNumber.from('3256300000000000000000000'), + // balanceInDecrease: token1Amount.mul(10000), + // balanceOutIncrease: '15641085361593105857', + // revertMessage:'', + // }) // await validateBurn({ // signer: hre.props.alice, diff --git a/test/contracts/rangepool.ts b/test/contracts/rangepool.ts index 72f1c744..bb23dd2e 100644 --- a/test/contracts/rangepool.ts +++ b/test/contracts/rangepool.ts @@ -276,35 +276,35 @@ describe('RangePool Exact In Tests', function () { }) }); - // it.only('token1 - Should calculate liquidity for TGE position', async function () { + it('token1 - Should calculate liquidity for TGE position', async function () { - // await validateSwap({ - // signer: hre.props.alice, - // recipient: hre.props.alice.address, - // zeroForOne: false, - // amount: tokenAmount.div(10), - // sqrtPriceLimitX96: BigNumber.from('2358285847295149069702956253974'), // 2.50 USD per FIN - // balanceInDecrease: BigNumber.from('0'), - // balanceOutIncrease: BigNumber.from('0'), - // revertMessage: '', - // }) + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: false, + amount: tokenAmount.div(10), + sqrtPriceLimitX96: BigNumber.from('2358285847295149069702956253974'), // 2.50 USD per FIN + balanceInDecrease: BigNumber.from('0'), + balanceOutIncrease: BigNumber.from('0'), + revertMessage: '', + }) - // await mintSigners20(hre.props.token0, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) - // await mintSigners20(hre.props.token1, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) - - // const aliceId = await validateMint({ - // signer: hre.props.alice, - // recipient: hre.props.alice.address, - // lower: '44850', - // upper: '77040', - // amount0: parseUnits('52000', 18), - // amount1: parseUnits('52000', 18), - // balance0Decrease: BigNumber.from('31568903742987611804'), // 49.84 = ~110495.28 USD - // balance1Decrease: BigNumber.from('51999999999999999999996'), // 40000 = 100000 USD - // liquidityIncrease: BigNumber.from('2555287091759866264142'), - // revertMessage: '', - // }) - // }) + await mintSigners20(hre.props.token0, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) + await mintSigners20(hre.props.token1, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) + + const aliceId = await validateMint({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '44850', + upper: '77040', + amount0: parseUnits('52000', 18), + amount1: parseUnits('52000', 18), + balance0Decrease: BigNumber.from('31568903742987611804'), // 49.84 = ~110495.28 USD + balance1Decrease: BigNumber.from('51999999999999999999996'), // 40000 = 100000 USD + liquidityIncrease: BigNumber.from('2555287091759866264142'), + revertMessage: '', + }) + }) it('token1 - Should mint, swap, and burn 14', async function () { diff --git a/test/contracts/tgedeployer.ts b/test/contracts/tgedeployer.ts new file mode 100644 index 00000000..aab19bdd --- /dev/null +++ b/test/contracts/tgedeployer.ts @@ -0,0 +1,136 @@ +/* global describe it before ethers */ +const hardhat = require('hardhat') +const { expect } = require('chai') +import { gBefore } from '../utils/hooks.test' +import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' +import { BigNumber } from 'ethers' +import { mintSigners20 } from '../utils/token' +import { + validateMint, + BN_ZERO, + validateSwap, + validateBurn, + getTickAtPrice, + getRangeBalanceOf, + getSnapshot, + getPrice, + getRangeLiquidity, + getTickLiquidity, +} from '../utils/contracts/rangepool' +import { RangePoolState, ZERO_ADDRESS } from '../utils/contracts/limitpool' +import { parseUnits } from 'ethers/lib/utils' +import { validateDeployTge } from '../utils/contracts/poolsharkrouter' + +alice: SignerWithAddress +describe('TGE Deployment Tests', function () { + let tokenAmount: BigNumber + let token0Decimals: number + let token1Decimals: number + let minPrice: BigNumber + let maxPrice: BigNumber + + let alice: SignerWithAddress + let bob: SignerWithAddress + let carol: SignerWithAddress + + ////////// DEBUG FLAGS ////////// + let debugMode = false + let balanceCheck = false + + const liquidityAmount = BigNumber.from('49902591570441687020675') + const liquidityAmount2 = BigNumber.from('50102591670431696268925') + const liquidityAmount3 = BigNumber.from('3852877204305891777654') + const minTickIdx = BigNumber.from('-887272') + const maxTickIdx = BigNumber.from('887272') + + before(async function () { + await gBefore() + let currentBlock = await ethers.provider.getBlockNumber() + const pool: RangePoolState = (await hre.props.limitPool.globalState()).pool + const liquidity = pool.liquidity + const feeGrowthGlobal0 = pool.feeGrowthGlobal0 + const feeGrowthGlobal1 = pool.feeGrowthGlobal1 + const price = pool.price + const nearestTick = pool.tickAtPrice + + expect(liquidity).to.be.equal(BN_ZERO) + + minPrice = BigNumber.from('4295128739') + maxPrice = BigNumber.from('1461446703485210103287273052203988822378723970341') + token0Decimals = await hre.props.token0.decimals() + token1Decimals = await hre.props.token1.decimals() + tokenAmount = ethers.utils.parseUnits('100', token0Decimals) + tokenAmount = ethers.utils.parseUnits('100', token1Decimals) + alice = hre.props.alice + bob = hre.props.bob + carol = hre.props.carol + }) + + this.beforeEach(async function () { + await mintSigners20(hre.props.token0, tokenAmount.mul(10), [hre.props.alice, hre.props.bob]) + await mintSigners20(hre.props.token1, tokenAmount.mul(10), [hre.props.alice, hre.props.bob]) + }) + + it.skip('token1 - Should calculate liquidity for TGE position', async function () { + + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: false, + amount: tokenAmount.div(10), + sqrtPriceLimitX96: BigNumber.from('2358285847295149069702956253974'), // 2.50 USD per FIN + balanceInDecrease: BigNumber.from('0'), + balanceOutIncrease: BigNumber.from('0'), + revertMessage: '', + }) + + await mintSigners20(hre.props.token0, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) + await mintSigners20(hre.props.token1, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) + + const aliceId = await validateMint({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '44850', + upper: '77040', + amount0: parseUnits('52000', 18), + amount1: parseUnits('52000', 18), + balance0Decrease: BigNumber.from('31568903742987611804'), // 49.84 = ~110495.28 USD + balance1Decrease: BigNumber.from('51999999999999999999996'), // 40000 = 100000 USD + liquidityIncrease: BigNumber.from('2555287091759866264142'), + revertMessage: '', + }) + }) + + it.only('token1 - Should deploy TGE position', async function () { + + await mintSigners20(hre.props.token0, tokenAmount.mul(55000), [hre.props.alice, hre.props.bob]) + await mintSigners20(hre.props.token1, tokenAmount.mul(55000), [hre.props.alice, hre.props.bob]) + + const aliceId = await validateDeployTge({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '44850', // $25 per FIN + upper: '77040', // $1 per FIN + amount0: parseUnits('52000', 18), + amount1: parseUnits('52000', 18), + balance0Decrease: BigNumber.from('31568903742987611804'), // 31.568 ETH + balance1Decrease: BigNumber.from('51999999999999999999996'), // 52k FIN + liquidityIncrease: BigNumber.from('2555287091759866264142'), + revertMessage: '', + stake: true + }) + + await validateBurn({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '44850', // $25 per FIN + upper: '77040', // $1 per FIN, + positionId: aliceId, + liquidityAmount: BigNumber.from('2555287091759866264142'), + balance0Increase: BigNumber.from('31568903742987611803'), // 31.568 ETH + balance1Increase: BigNumber.from('51999999999999999999995'), // 52k FIN + revertMessage: '', + staked: true + }) + }) +}); \ No newline at end of file diff --git a/test/utils/contracts/poolsharkrouter.ts b/test/utils/contracts/poolsharkrouter.ts index c856b11a..406d385e 100644 --- a/test/utils/contracts/poolsharkrouter.ts +++ b/test/utils/contracts/poolsharkrouter.ts @@ -1,3 +1,8 @@ +import { expect } from "chai" +import { BigNumber, Contract } from "ethers" +import { BN_ZERO, Position, ValidateMintParams } from "./rangepool" +import { RangeTick, RangeStake, GlobalState } from "./limitpool" + export function getMintRangeInputData(stake: boolean): any { if (stake) return ethers.utils.defaultAbiCoder.encode( @@ -22,4 +27,125 @@ export function getMintRangeInputData(stake: boolean): any { ) else return ethers.utils.formatBytes32String('') -} \ No newline at end of file +} + +export async function validateDeployTge(params: ValidateMintParams): Promise { + const signer = params.signer + const recipient = params.recipient + const lower = BigNumber.from(params.lower) + const upper = BigNumber.from(params.upper) + const amount0 = params.amount0 + const amount1 = params.amount1 + const balance0Decrease = params.balance0Decrease + const balance1Decrease = params.balance1Decrease + const liquidityIncrease = params.liquidityIncrease + const revertMessage = params.revertMessage + const collectRevertMessage = params.collectRevertMessage + const positionId = params.positionId ? params.positionId : 0 + const poolContract = params.poolContract ?? hre.props.limitPool + const poolTokenContract = params.poolTokenContract ?? hre.props.limitPoolToken + const expectedPositionId = params.positionId ? params.positionId + : (await poolContract.globalState()).positionIdNext + + const stake = params.stake ?? false + + let balance0Before + let balance1Before + const token0 = await hre.ethers.getContractAt('Token20', await poolContract.token0()) + const token1 = await hre.ethers.getContractAt('Token20', await poolContract.token1()) + balance0Before = await token0.balanceOf(params.signer.address) + balance1Before = await token1.balanceOf(params.signer.address) + const approve0Txn = await token0 + .connect(params.signer) + .approve(hre.props.poolRouter.address, amount0) + await approve0Txn.wait() + const approve1Txn = await token1 + .connect(params.signer) + .approve(hre.props.poolRouter.address, amount1) + await approve1Txn.wait() + + let lowerTickBefore: RangeTick + let upperTickBefore: RangeTick + let positionBefore: Position + let positionTokens: Contract + let positionTokenBalanceBefore: BigNumber + lowerTickBefore = (await poolContract.ticks(lower)).range + upperTickBefore = (await poolContract.ticks(upper)).range + + positionBefore = await poolContract.positions(positionId) + positionTokens = await hre.ethers.getContractAt('PositionERC1155', poolTokenContract.address); + positionTokenBalanceBefore = await positionTokens.balanceOf(stake ? hre.props.rangeStaker.address : signer.address, expectedPositionId); + // if (params.positionId && !stake) + // expect(positionTokenBalanceBefore).to.be.equal(1) + if (revertMessage == '') { + const txn = await hre.props.poolRouter + .connect(params.signer) + .deployTge(poolContract.address, hre.props.rangeStaker.address) + await txn.wait() + } else { + await expect( + hre.props.poolRouter + .connect(params.signer) + .multiMintRange( + [poolContract.address], + [ + { + to: recipient, + lower: lower, + upper: upper, + positionId: positionId, + amount0: amount0, + amount1: amount1, + callbackData: getMintRangeInputData(stake), + } + ]) + ).to.be.revertedWith(revertMessage) + return + } + let balance0After + let balance1After + balance0After = await token0.balanceOf(params.signer.address) + balance1After = await token1.balanceOf(params.signer.address) + + expect(balance0Before.sub(balance0After)).to.be.equal(balance0Decrease) + expect(balance1Before.sub(balance1After)).to.be.equal(balance1Decrease) + + let lowerTickAfter: RangeTick + let upperTickAfter: RangeTick + let positionAfter: Position + let positionTokenBalanceAfter: BigNumber + lowerTickAfter = (await poolContract.ticks(lower)).range + upperTickAfter = (await poolContract.ticks(upper)).range + + let globalStateAfter: GlobalState = await poolContract.globalState(); + expect(globalStateAfter.pool.price).to.be.equal(BigNumber.from('2358285847295149069702956253974')) + + positionAfter = await poolContract.positions(expectedPositionId) + positionTokens = await hre.ethers.getContractAt('PositionERC1155', poolTokenContract.address); + positionTokenBalanceAfter = await positionTokens.balanceOf(stake ? hre.props.rangeStaker.address : signer.address, expectedPositionId); + if (!params.positionId) + expect(positionTokenBalanceAfter.sub(positionTokenBalanceBefore)).to.be.equal(BigNumber.from(1)) + expect(lowerTickAfter.liquidityDelta.sub(lowerTickBefore.liquidityDelta)).to.be.equal( + liquidityIncrease + ) + expect(upperTickAfter.liquidityDelta.sub(upperTickBefore.liquidityDelta)).to.be.equal( + BN_ZERO.sub(liquidityIncrease) + ) + expect(positionAfter.liquidity.sub(positionBefore.liquidity)).to.be.equal(liquidityIncrease) + if (stake) { + // check fg0/1 and liquidity match + const stakeKey = ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode( + ["address", "uint32"], // encode as address array + [ poolContract.address, expectedPositionId ] + )) + const rangeStake: RangeStake = await hre.props.rangeStaker.rangeStakes(stakeKey) + expect(positionAfter.feeGrowthInside0Last).to.be.equal(rangeStake.feeGrowthInside0Last) + expect(positionAfter.feeGrowthInside1Last).to.be.equal(rangeStake.feeGrowthInside1Last) + expect(positionAfter.liquidity).to.be.equal(rangeStake.liquidity) + expect(rangeStake.positionId).to.be.equal(expectedPositionId) + expect(rangeStake.pool).to.be.equal(poolContract.address) + expect(rangeStake.isStaked).to.be.equal(true) + expect(rangeStake.owner).to.be.equal(params.recipient) + } + return expectedPositionId + } \ No newline at end of file diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index e93bdd51..261d68a8 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -33,7 +33,7 @@ export class InitialSetup { private deployContracts = false private deployPools = false private deployRouter = true - private deployStaker = true + private deployStaker = false constructor() { this.deployAssist = new DeployAssist() From 84c1550df098950c0efc918ff135ea48215c7ac6 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Wed, 27 Dec 2023 15:02:05 -0700 Subject: [PATCH 18/39] test for kyber exploit --- test/contracts/rangepool.ts | 75 ++++++++++++++++++++++++++++- test/utils/contracts/rangepool.ts | 31 ++++++------ test/utils/setup/beforeEachProps.ts | 6 +++ test/utils/setup/initialSetup.ts | 42 +++++++++++----- 4 files changed, 125 insertions(+), 29 deletions(-) diff --git a/test/contracts/rangepool.ts b/test/contracts/rangepool.ts index bb23dd2e..012d9640 100644 --- a/test/contracts/rangepool.ts +++ b/test/contracts/rangepool.ts @@ -17,7 +17,7 @@ import { getRangeLiquidity, getTickLiquidity, } from '../utils/contracts/rangepool' -import { RangePoolState, ZERO_ADDRESS } from '../utils/contracts/limitpool' +import { RangePoolState, ZERO_ADDRESS, getLiquidity } from '../utils/contracts/limitpool' import { parseUnits } from 'ethers/lib/utils' alice: SignerWithAddress @@ -153,7 +153,7 @@ describe('RangePool Exact In Tests', function () { // } // }); - it('pool - Should not overflow global liquidity', async function () { + it.only('pool - Should not overflow global liquidity', async function () { const aliceId = await validateMint({ signer: hre.props.alice, recipient: hre.props.alice.address, @@ -173,6 +173,77 @@ describe('RangePool Exact In Tests', function () { } }) + it.only('pool - Should not skip crossing tick - kyber exploit', async function () { + console.log('kyber pool:', hre.props.kyberPool.address, hre.props.kyberPoolToken.address) + + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: true, + amount: tokenAmount.div(10), + sqrtPriceLimitX96: BigNumber.from('309485009821345068724781056'), // 2.50 USD per FIN + balanceInDecrease: BigNumber.from('0'), + balanceOutIncrease: BigNumber.from('0'), + poolContract: hre.props.kyberPool, + revertMessage: '', + }) + const aliceId = await validateMint({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '-111310', + upper: '-110908', + amount0: BigNumber.from('3408506672908522891'), + amount1: BigNumber.from('18870618627071590'), + balance0Decrease: BigNumber.from('3408506672908522891'), + balance1Decrease: BigNumber.from('18870618627071590'), + liquidityIncrease: BigNumber.from('243433600698313192894'), + poolContract: hre.props.kyberPool, + poolTokenContract: hre.props.kyberPoolToken, + revertMessage: '', + }) + await validateBurn({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '-111310', + upper: '-110908', + positionId: aliceId ?? 0, + liquidityAmount: BigNumber.from('40572266783052180174'), + balance0Increase: BigNumber.from('568084445484753554'), + balance1Increase: BigNumber.from('3145103104511930'), + poolContract: hre.props.kyberPool, + poolTokenContract: hre.props.kyberPoolToken, + revertMessage: '', + }) + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: true, + amount: BigNumber.from('3056056735638220799999'), + sqrtPriceLimitX96: BigNumber.from('303343357908702216014253965'), + balanceInDecrease: BigNumber.from('1051453208852302636974'), + balanceOutIncrease: BigNumber.from('15712935110141611'), + poolContract: hre.props.kyberPool, + revertMessage: '', + }) + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: false, + amount: BigNumber.from('15768859296842439'), + sqrtPriceLimitX96: BigNumber.from('320556756984076944269385500'), + balanceInDecrease: BigNumber.from('15768859296842439'), + balanceOutIncrease: BigNumber.from('1053450196174862625184'), + poolContract: hre.props.kyberPool, + revertMessage: '', + }) + await getPrice(hre.props.kyberPool) + await getRangeLiquidity() + if (balanceCheck) { + console.log('balance after token0:', (await hre.props.token0.balanceOf(hre.props.limitPool.address)).toString()) + console.log('balance after token1:', (await hre.props.token1.balanceOf(hre.props.limitPool.address)).toString()) + } + }) + it("pool0 - Should revert mint, burn, and swap when recipient is zero address", async function () { const aliceId = await validateMint({ signer: hre.props.alice, diff --git a/test/utils/contracts/rangepool.ts b/test/utils/contracts/rangepool.ts index 99581c74..8e73ad0f 100644 --- a/test/utils/contracts/rangepool.ts +++ b/test/utils/contracts/rangepool.ts @@ -74,6 +74,7 @@ export interface ValidateSwapParams { balanceOutIncrease: BigNumber revertMessage: string exactIn?: boolean + poolContract?: LimitPool } export interface ValidateBurnParams { @@ -119,8 +120,8 @@ export async function getTickAtPrice() { console.log('tick at price:', tickAtPrice) } -export async function getPrice() { - const poolPrice = (await hre.props.limitPool.globalState()).pool.price +export async function getPrice(poolContract?: LimitPool) { + const poolPrice = (await (poolContract ?? hre.props.limitPool).globalState()).pool.price console.log('pool price:', poolPrice.toString()) } @@ -214,14 +215,14 @@ export async function validateSwap(params: ValidateSwapParams) { const balanceInDecrease = params.balanceInDecrease const balanceOutIncrease = params.balanceOutIncrease const revertMessage = params.revertMessage - - const poolBefore: RangePoolState = (await hre.props.limitPool.globalState()).pool + const poolContract = params.poolContract ?? hre.props.limitPool + const poolBefore: RangePoolState = (await poolContract.globalState()).pool const liquidityBefore = poolBefore.liquidity const priceBefore = poolBefore.price const nearestTickBefore = poolBefore.tickAtPrice // quote pre-swap and validate balance changes match post-swap - const quote = await hre.props.limitPool.quote({ + const quote = await poolContract.quote({ zeroForOne: zeroForOne, amount: amount, exactIn: params.exactIn ?? true, @@ -259,7 +260,7 @@ export async function validateSwap(params: ValidateSwapParams) { let txn = await hre.props.poolRouter .connect(signer) .multiSwapSplit( - [hre.props.limitPool.address], + [poolContract.address], [{ to: signer.address, zeroForOne: zeroForOne, @@ -274,7 +275,7 @@ export async function validateSwap(params: ValidateSwapParams) { hre.props.poolRouter .connect(signer) .multiSwapSplit( - [hre.props.limitPool.address], + [poolContract.address], [{ to: signer.address, zeroForOne: zeroForOne, @@ -302,7 +303,7 @@ export async function validateSwap(params: ValidateSwapParams) { expect(balanceInBefore.sub(balanceInAfter)).to.be.equal(inAmount) expect(balanceOutAfter.sub(balanceOutBefore)).to.be.equal(outAmount) - const poolAfter: RangePoolState = (await hre.props.limitPool.globalState()).pool + const poolAfter: RangePoolState = (await poolContract.globalState()).pool const liquidityAfter = poolAfter.liquidity const priceAfter = poolAfter.price @@ -542,13 +543,13 @@ export async function validateBurn(params: ValidateBurnParams) { positionAfter = await poolContract.positions(params.positionId) if (burnPercent.eq(ethers.utils.parseUnits('1', 38))) expect(positionTokenBalanceAfter.sub(positionTokenBalanceBefore)).to.be.equal(-1) - expect(lowerTickAfter.liquidityDelta.sub(lowerTickBefore.liquidityDelta)).to.be.equal( - BN_ZERO.sub(params.liquidityAmount) - ) - expect(upperTickAfter.liquidityDelta.sub(upperTickBefore.liquidityDelta)).to.be.equal( - liquidityAmount - ) - expect(positionAfter.liquidity.sub(positionBefore.liquidity)).to.be.equal(BN_ZERO.sub(liquidityAmount)) + // expect(lowerTickAfter.liquidityDelta.sub(lowerTickBefore.liquidityDelta)).to.be.equal( + // BN_ZERO.sub(params.liquidityAmount) + // ) + // expect(upperTickAfter.liquidityDelta.sub(upperTickBefore.liquidityDelta)).to.be.equal( + // liquidityAmount + // ) + // expect(positionAfter.liquidity.sub(positionBefore.liquidity)).to.be.equal(BN_ZERO.sub(liquidityAmount)) } export async function validateStake(params: ValidateStakeParams) { diff --git a/test/utils/setup/beforeEachProps.ts b/test/utils/setup/beforeEachProps.ts index 31a7a036..c25cbce1 100644 --- a/test/utils/setup/beforeEachProps.ts +++ b/test/utils/setup/beforeEachProps.ts @@ -33,6 +33,8 @@ export interface BeforeEachProps { wethPoolToken: PositionERC1155 limitPoolImpl: LimitPool limitPoolToken: PositionERC1155 + kyberPool: LimitPool + kyberPoolToken: PositionERC1155 limitPoolManager: LimitPoolManager limitPoolFactory: LimitPoolFactory poolRouter: PoolsharkRouter @@ -94,6 +96,8 @@ export class GetBeforeEach { let wethPoolToken: PositionERC1155 let limitPoolImpl: LimitPool let limitPoolToken: PositionERC1155 + let kyberPool: LimitPool + let kyberPoolToken: PositionERC1155 let limitPoolManager: LimitPoolManager let limitPoolFactory: LimitPoolFactory let poolRouter: PoolsharkRouter @@ -133,6 +137,8 @@ export class GetBeforeEach { wethPoolToken, limitPoolImpl, limitPoolToken, + kyberPool, + kyberPoolToken, limitPoolManager, limitPoolFactory, poolRouter, diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index 261d68a8..84dedab5 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -324,6 +324,15 @@ export class InitialSetup { hre.nonce += 1; + // add 500 fee tier + enableFeeTierTxn = await hre.props.limitPoolManager.enableFeeTier( + 800, + 2 + ); + await enableFeeTierTxn.wait(); + + hre.nonce += 1; + // create first limit pool let createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ poolTypeId: 0, @@ -346,6 +355,18 @@ export class InitialSetup { }); await wethPoollTxn.wait(); + hre.nonce += 1; + + // create kyber test limit pool + let kyperPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ + poolTypeId: 0, + tokenIn: hre.props.token0.address, + tokenOut: hre.props.token1.address, + swapFee: '800', + startPrice: '3266660825699135434887405499641' + }); + await kyperPoolTxn.wait(); + hre.nonce += 1; [limitPoolAddress, limitPoolTokenAddress] = await hre.props.limitPoolFactory.getLimitPool( @@ -365,19 +386,16 @@ export class InitialSetup { hre.props.wethPool = await hre.ethers.getContractAt('LimitPool', wethPoolAddress) hre.props.wethPoolToken = await hre.ethers.getContractAt('PositionERC1155', wethPoolTokenAddress) - await this.deployAssist.saveContractDeployment( - network, - 'LimitPool', - 'wethPool', - hre.props.wethPool, - [ - hre.props.weth9.address, - hre.props.token1.address, - '500', - '3266660825699135434887405499641', - 0 - ] + let [kyberPoolAddress, kyberPoolTokenAddress] = await hre.props.limitPoolFactory.getLimitPool( + hre.props.token0.address, + hre.props.token1.address, + '800', + 0 ) + + hre.props.kyberPool = await hre.ethers.getContractAt('LimitPool', kyberPoolAddress) + hre.props.kyberPoolToken = await hre.ethers.getContractAt('PositionERC1155', kyberPoolTokenAddress) + } else if (this.deployPools) { // create first limit pool let createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ From ea2ddc7ca2267635c2c21b6e87d18cda97ac9cc4 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Wed, 27 Dec 2023 15:10:44 -0700 Subject: [PATCH 19/39] cleanup testing --- test/contracts/rangepool.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/contracts/rangepool.ts b/test/contracts/rangepool.ts index 012d9640..06accf54 100644 --- a/test/contracts/rangepool.ts +++ b/test/contracts/rangepool.ts @@ -153,7 +153,7 @@ describe('RangePool Exact In Tests', function () { // } // }); - it.only('pool - Should not overflow global liquidity', async function () { + it('pool - Should not overflow global liquidity', async function () { const aliceId = await validateMint({ signer: hre.props.alice, recipient: hre.props.alice.address, @@ -173,7 +173,7 @@ describe('RangePool Exact In Tests', function () { } }) - it.only('pool - Should not skip crossing tick - kyber exploit', async function () { + it('pool - Should not skip crossing tick - kyber exploit', async function () { console.log('kyber pool:', hre.props.kyberPool.address, hre.props.kyberPoolToken.address) await validateSwap({ From c4fe9ccbdc53b3b004dfcb6c42c0a4ae99da8066 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Wed, 27 Dec 2023 15:24:44 -0700 Subject: [PATCH 20/39] skip tge test --- test/contracts/rangepool.ts | 2 +- test/utils/contracts/rangepool.ts | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/test/contracts/rangepool.ts b/test/contracts/rangepool.ts index 06accf54..8129ff24 100644 --- a/test/contracts/rangepool.ts +++ b/test/contracts/rangepool.ts @@ -347,7 +347,7 @@ describe('RangePool Exact In Tests', function () { }) }); - it('token1 - Should calculate liquidity for TGE position', async function () { + it.skip('token1 - Should calculate liquidity for TGE position', async function () { await validateSwap({ signer: hre.props.alice, diff --git a/test/utils/contracts/rangepool.ts b/test/utils/contracts/rangepool.ts index 8e73ad0f..4bc8e278 100644 --- a/test/utils/contracts/rangepool.ts +++ b/test/utils/contracts/rangepool.ts @@ -482,6 +482,9 @@ export async function validateBurn(params: ValidateBurnParams) { burnPercent = liquidityAmount .mul(ethers.utils.parseUnits('1', 38)) .div(positionBefore.liquidity) + liquidityAmount = burnPercent + .mul(positionBefore.liquidity) + .div(ethers.utils.parseUnits('1', 38)) } if (revertMessage == '') { positionSnapshot = await poolContract.snapshotRange(params.positionId) @@ -543,13 +546,13 @@ export async function validateBurn(params: ValidateBurnParams) { positionAfter = await poolContract.positions(params.positionId) if (burnPercent.eq(ethers.utils.parseUnits('1', 38))) expect(positionTokenBalanceAfter.sub(positionTokenBalanceBefore)).to.be.equal(-1) - // expect(lowerTickAfter.liquidityDelta.sub(lowerTickBefore.liquidityDelta)).to.be.equal( - // BN_ZERO.sub(params.liquidityAmount) - // ) - // expect(upperTickAfter.liquidityDelta.sub(upperTickBefore.liquidityDelta)).to.be.equal( - // liquidityAmount - // ) - // expect(positionAfter.liquidity.sub(positionBefore.liquidity)).to.be.equal(BN_ZERO.sub(liquidityAmount)) + expect(lowerTickAfter.liquidityDelta.sub(lowerTickBefore.liquidityDelta)).to.be.equal( + BN_ZERO.sub(liquidityAmount) + ) + expect(upperTickAfter.liquidityDelta.sub(upperTickBefore.liquidityDelta)).to.be.equal( + liquidityAmount + ) + expect(positionAfter.liquidity.sub(positionBefore.liquidity)).to.be.equal(BN_ZERO.sub(liquidityAmount)) } export async function validateStake(params: ValidateStakeParams) { From b8612724c7839c3e3e6f4cb973381c3e4f1f8c1f Mon Sep 17 00:00:00 2001 From: alphak3y Date: Thu, 28 Dec 2023 20:28:45 -0700 Subject: [PATCH 21/39] add standard events for dexscreener --- contracts/LimitPoolFactory.sol | 10 +++ contracts/base/events/LimitPoolEvents.sol | 70 ++++++++++++++++--- .../base/events/LimitPoolFactoryEvents.sol | 20 +++++- .../base/events/LimitPoolManagerEvents.sol | 5 ++ contracts/libraries/Ticks.sol | 40 ++++++++++- contracts/libraries/limit/LimitPositions.sol | 22 ++++++ .../libraries/limit/pool/BurnLimitCall.sol | 2 +- .../libraries/limit/pool/MintLimitCall.sol | 21 ++++++ contracts/libraries/pool/FeesCall.sol | 2 - contracts/libraries/pool/QuoteCall.sol | 10 +-- contracts/libraries/pool/SampleCall.sol | 7 +- contracts/libraries/pool/SwapCall.sol | 22 +++--- contracts/libraries/range/RangePositions.sol | 43 ++++++++++++ .../libraries/range/pool/BurnRangeCall.sol | 13 +++- .../libraries/range/pool/MintRangeCall.sol | 24 ++++++- contracts/libraries/utils/Collect.sol | 25 +++++-- test/contracts/libraries/samples.ts | 24 +++---- test/contracts/tgedeployer.ts | 2 +- 18 files changed, 298 insertions(+), 64 deletions(-) diff --git a/contracts/LimitPoolFactory.sol b/contracts/LimitPoolFactory.sol index 87b5ec4a..8fa2de63 100644 --- a/contracts/LimitPoolFactory.sol +++ b/contracts/LimitPoolFactory.sol @@ -133,7 +133,17 @@ contract LimitPoolFactory is // save pool in mapping pools[key] = pool; + // emit standard event emit PoolCreated( + constants.token0, + constants.token1, + constants.swapFee, + constants.tickSpacing, + pool + ); + + // emit custom event + emit LimitPoolCreated( pool, constants.poolToken, constants.token0, diff --git a/contracts/base/events/LimitPoolEvents.sol b/contracts/base/events/LimitPoolEvents.sol index fad0e986..bc06bee3 100644 --- a/contracts/base/events/LimitPoolEvents.sol +++ b/contracts/base/events/LimitPoolEvents.sol @@ -2,17 +2,22 @@ pragma solidity 0.8.18; /// @notice Events emitted by the LimitPool contract(s) -abstract contract LimitPoolEvents { - /// @notice Event emitted when pool is initialized by the factory - event Initialize( +interface LimitPoolEvents { + + ///////////////////////////////////////////////////////////// + /////////////////////// Custom Events /////////////////////// + ///////////////////////////////////////////////////////////// + + /// @notice Custom event emitted when pool is initialized by the factory + event InitializeLimit( int24 minTick, int24 maxTick, uint160 startPrice, int24 startTick ); - /// @notice Event emitted when a swap is successful - event Swap( + /// @notice Custom event emitted when a swap is successful + event SwapLimit( address indexed recipient, uint256 amountIn, uint256 amountOut, @@ -49,12 +54,6 @@ abstract contract LimitPoolEvents { /// @notice Event emitted when liquidity is added as a result of calling `burnRange` event CompoundRange(uint32 indexed positionId, uint128 liquidityCompounded); - /// @notice Event emitted when token0 is collected from the pool - event CollectRange0(uint128 amount0); - - /// @notice Event emitted when token1 is collected from the pool - event CollectRange1(uint128 amount1); - /// @notice Event emitted when a RangeTick is updated event SyncRangeTick( uint200 feeGrowthOutside0, @@ -115,4 +114,53 @@ abstract contract LimitPoolEvents { /// @notice Event emitted when max sample count is increased event SampleCountIncreased(uint16 newSampleCountMax); + + ///////////////////////////////////////////////////////////// + ////////////////////// Standard Events ////////////////////// + ///////////////////////////////////////////////////////////// + + /// @notice Emitted when pool is initialized by the factory + event Initialize(uint160 price, int24 tick); + + /// @notice Emitted when liquidity is added + event Mint( + address sender, + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + + /// @notice Emitted when liquidity is removed + event Burn( + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + + /// @notice Emitted when liquidity is swapped + event Swap( + address indexed sender, + address indexed recipient, + int256 amount0, + int256 amount1, + uint160 price, + uint128 liquidity, + int24 tickAtPrice + ); + + /// @notice Emitted when fees are collected by the owner of a position + event Collect( + address indexed owner, + address recipient, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount0, + uint128 amount1 + ); } diff --git a/contracts/base/events/LimitPoolFactoryEvents.sol b/contracts/base/events/LimitPoolFactoryEvents.sol index 2e9c883c..1ea74041 100644 --- a/contracts/base/events/LimitPoolFactoryEvents.sol +++ b/contracts/base/events/LimitPoolFactoryEvents.sol @@ -3,8 +3,12 @@ pragma solidity 0.8.18; /// @notice Events emitted by the LimitPoolFactory contract abstract contract LimitPoolFactoryEvents { - /// @notice Event emitted when a LimitPool is created - event PoolCreated( + + ///////////////////////////////////////////////////////////// + /////////////////////// Custom Events /////////////////////// + ///////////////////////////////////////////////////////////// + + event LimitPoolCreated( address pool, address token, address indexed token0, @@ -13,4 +17,16 @@ abstract contract LimitPoolFactoryEvents { int16 tickSpacing, uint16 poolTypeId ); + + ///////////////////////////////////////////////////////////// + ////////////////////// Standard Events ////////////////////// + ///////////////////////////////////////////////////////////// + + event PoolCreated( + address indexed token0, + address indexed token1, + uint24 indexed fee, + int24 tickSpacing, + address pool + ); } diff --git a/contracts/base/events/LimitPoolManagerEvents.sol b/contracts/base/events/LimitPoolManagerEvents.sol index aecc840e..b51e29d5 100644 --- a/contracts/base/events/LimitPoolManagerEvents.sol +++ b/contracts/base/events/LimitPoolManagerEvents.sol @@ -5,6 +5,11 @@ import '../../interfaces/structs/PoolsharkStructs.sol'; /// @notice Events emitted by the LimitPoolManager contract abstract contract LimitPoolManagerEvents is PoolsharkStructs { + + ///////////////////////////////////////////////////////////// + /////////////////////// Custom Events /////////////////////// + ///////////////////////////////////////////////////////////// + /// @notice Event emitted when pool is initialized by the factory event FactoryChanged( address indexed previousFactory, diff --git a/contracts/libraries/Ticks.sol b/contracts/libraries/Ticks.sol index ad3c8100..c8f3ecfd 100644 --- a/contracts/libraries/Ticks.sol +++ b/contracts/libraries/Ticks.sol @@ -2,6 +2,7 @@ pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; +import '../base/events/LimitPoolEvents.sol'; import './range/math/FeeMath.sol'; import './math/OverflowMath.sol'; import './math/ConstantProduct.sol'; @@ -23,7 +24,8 @@ library Ticks { // for Q64.96 numbers uint256 internal constant Q96 = 0x1000000000000000000000000; - event Initialize( + event Initialize(uint160 price, int24 tick); + event InitializeLimit( int24 minTick, int24 maxTick, uint160 startPrice, @@ -31,6 +33,15 @@ library Ticks { ); event Swap( + address indexed sender, + address indexed recipient, + int256 amount0, + int256 amount1, + uint160 price, + uint128 liquidity, + int24 tickAtPrice + ); + event SwapLimit( address indexed recipient, uint256 amountIn, uint256 amountOut, @@ -108,8 +119,14 @@ library Ticks { // intialize samples state.pool = Samples.initialize(samples, state.pool); - // emit event + // emit standard event emit Initialize( + state.pool0.price, + state.pool0.tickAtPrice + ); + + // emit custom event + emit InitializeLimit( ConstantProduct.minTick(constants.tickSpacing), ConstantProduct.maxTick(constants.tickSpacing), state.pool0.price, @@ -188,6 +205,7 @@ library Ticks { ); } } + /// @dev - write oracle entry after start of block ( cache.state.pool.samples.index, @@ -198,6 +216,7 @@ library Ticks { startLiquidity, cache.state.pool.tickAtPrice ); + // pool liquidity should be updated along the way cache.state.pool.price = cache.price.toUint160(); @@ -218,7 +237,20 @@ library Ticks { cache.state.pool0.tickAtPrice = cache.state.pool.tickAtPrice; } } + + // emit standard event emit Swap( + msg.sender, + params.to, + params.zeroForOne ? int256(cache.input) : -int256(cache.output), + params.zeroForOne ? -int256(cache.output) : int256(cache.input), + cache.state.pool.price, + cache.liquidity.toUint128(), + cache.state.pool.tickAtPrice + ); + + // emit custom event + emit SwapLimit( params.to, cache.input, cache.output, @@ -284,6 +316,7 @@ library Ticks { cross: true, averagePrice: 0 }); + // grab latest price and sample ( cache.averagePrice, @@ -294,12 +327,14 @@ library Ticks { cache.constants, cache.state.pool.liquidity ); + while (cache.cross) { cache = _quoteSingle(cache, params.priceLimit, params.zeroForOne); if (cache.cross) { cache = _pass(ticks, rangeTickMap, limitTickMap, cache, params); } } + return (cache.input, cache.output, cache.price.toUint160()); } @@ -500,6 +535,7 @@ library Ticks { crossTick.secondsPerLiquidityAccumOutside; ticks[cache.crossTick].range = crossTick; int128 liquidityDelta = crossTick.liquidityDelta; + // emit custom event emit SyncRangeTick( crossTick.feeGrowthOutside0, crossTick.feeGrowthOutside1, diff --git a/contracts/libraries/limit/LimitPositions.sol b/contracts/libraries/limit/LimitPositions.sol index 84f31f31..47c32624 100644 --- a/contracts/libraries/limit/LimitPositions.sol +++ b/contracts/libraries/limit/LimitPositions.sol @@ -15,6 +15,15 @@ import '../Ticks.sol'; library LimitPositions { using SafeCast for uint256; + event Burn( + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + event BurnLimit( address indexed to, uint32 positionId, @@ -419,6 +428,19 @@ library LimitPositions { cache.position.crossedInto = false; } + // emit standard event + emit Burn( + msg.sender, + cache.position.lower, + cache.position.upper, + uint128(cache.liquidityBurned), + params.zeroForOne ? cache.amountOut + : cache.amountIn, + params.zeroForOne ? cache.amountIn + : cache.amountOut + ); + + // emit custom event emit BurnLimit( params.to, params.positionId, diff --git a/contracts/libraries/limit/pool/BurnLimitCall.sol b/contracts/libraries/limit/pool/BurnLimitCall.sol index 4086b24c..021a0f95 100644 --- a/contracts/libraries/limit/pool/BurnLimitCall.sol +++ b/contracts/libraries/limit/pool/BurnLimitCall.sol @@ -99,7 +99,7 @@ library BurnLimitCall { save(cache, globalState, params.zeroForOne); BurnLimitLocals memory locals; - (cache, locals.amount0Delta, locals.amount1Delta) = Collect.burnLimit( + (cache, locals.amount0Delta, locals.amount1Delta) = Collects.burnLimit( cache, params ); diff --git a/contracts/libraries/limit/pool/MintLimitCall.sol b/contracts/libraries/limit/pool/MintLimitCall.sol index 25ebe8b6..1bf2e762 100644 --- a/contracts/libraries/limit/pool/MintLimitCall.sol +++ b/contracts/libraries/limit/pool/MintLimitCall.sol @@ -9,6 +9,17 @@ import '../../utils/Collect.sol'; import '../../utils/PositionTokens.sol'; library MintLimitCall { + + event Mint( + address sender, + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + event MintLimit( address indexed to, int24 lower, @@ -202,6 +213,16 @@ library MintLimitCall { ? cache.state.pool0 = cache.pool : cache.state.pool1 = cache.pool; + emit Mint( + msg.sender, + params.to, + cache.position.lower, + cache.position.upper, + uint128(cache.liquidityMinted), + uint128(params.zeroForOne ? params.amount : 0), + uint128(params.zeroForOne ? 0 : params.amount) + ); + emit MintLimit( params.to, params.lower, diff --git a/contracts/libraries/pool/FeesCall.sol b/contracts/libraries/pool/FeesCall.sol index e477f318..3eb312b1 100644 --- a/contracts/libraries/pool/FeesCall.sol +++ b/contracts/libraries/pool/FeesCall.sol @@ -20,8 +20,6 @@ library FeesCall { // eth address for safe withdrawal address public constant ethAddress = address(0); - /// @dev - LimitPoolManager (i.e. constants.owner) emits events in aggregate - function perform( PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.FeesParams memory params, diff --git a/contracts/libraries/pool/QuoteCall.sol b/contracts/libraries/pool/QuoteCall.sol index f4cd81f7..3c54fad5 100644 --- a/contracts/libraries/pool/QuoteCall.sol +++ b/contracts/libraries/pool/QuoteCall.sol @@ -7,15 +7,7 @@ import '../Ticks.sol'; library QuoteCall { uint8 private constant _ENTERED = 2; - event Swap( - address indexed recipient, - bool zeroForOne, - uint256 amountIn, - uint256 amountOut, - uint160 price, - uint128 liquidity, - int24 tickAtPrice - ); + event Event(); function perform( mapping(int24 => LimitPoolStructs.Tick) storage ticks, diff --git a/contracts/libraries/pool/SampleCall.sol b/contracts/libraries/pool/SampleCall.sol index cf939214..ea4b4d1a 100644 --- a/contracts/libraries/pool/SampleCall.sol +++ b/contracts/libraries/pool/SampleCall.sol @@ -7,12 +7,7 @@ import '../Samples.sol'; library SampleCall { uint8 private constant _ENTERED = 2; - event SampleRecorded( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum - ); - - event SampleLengthIncreased(uint16 sampleLengthNext); + event Event(); function perform( PoolsharkStructs.GlobalState memory state, diff --git a/contracts/libraries/pool/SwapCall.sol b/contracts/libraries/pool/SwapCall.sol index f3005e81..616e84d8 100644 --- a/contracts/libraries/pool/SwapCall.sol +++ b/contracts/libraries/pool/SwapCall.sol @@ -9,16 +9,18 @@ import '../utils/Collect.sol'; import '../utils/SafeTransfers.sol'; library SwapCall { - event Swap( - address indexed recipient, - bool zeroForOne, - uint256 amountIn, - uint256 amountOut, - uint160 price, - uint128 liquidity, - uint128 feeAmount, - int24 tickAtPrice - ); + // event Swap( + // address indexed recipient, + // bool zeroForOne, + // uint256 amountIn, + // uint256 amountOut, + // uint160 price, + // uint128 liquidity, + // uint128 feeAmount, + // int24 tickAtPrice + // ); + + event Event(); function perform( mapping(int24 => LimitPoolStructs.Tick) storage ticks, diff --git a/contracts/libraries/range/RangePositions.sol b/contracts/libraries/range/RangePositions.sol index 096afd0a..c3babf23 100644 --- a/contracts/libraries/range/RangePositions.sol +++ b/contracts/libraries/range/RangePositions.sol @@ -21,6 +21,25 @@ library RangePositions { uint256 internal constant Q96 = 0x1000000000000000000000000; uint256 internal constant Q128 = 0x100000000000000000000000000000000; + event Mint( + address sender, + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + + event Burn( + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + event BurnRange( address indexed recipient, uint256 indexed positionId, @@ -157,6 +176,18 @@ library RangePositions { cache.position.upper, uint128(cache.liquidityBurned) ); + + // emit standard event + emit Burn( + msg.sender, + cache.position.lower, + cache.position.upper, + uint128(cache.liquidityBurned), + cache.amount0.toUint128(), + cache.amount1.toUint128() + ); + + // emit custom event emit BurnRange( params.to, params.positionId, @@ -164,10 +195,13 @@ library RangePositions { cache.amount0, cache.amount1 ); + + // clear position bounds if (cache.position.liquidity == 0) { cache.position.lower = 0; cache.position.upper = 0; } + return cache; } @@ -224,6 +258,15 @@ library RangePositions { : params.amount1; position.liquidity += uint128(liquidityAmount); } + emit Mint( + msg.sender, + msg.sender, + position.lower, + position.upper, + liquidityAmount.toUint128(), + 0, + 0 + ); emit CompoundRange(params.positionId, uint128(liquidityAmount)); return ( position, diff --git a/contracts/libraries/range/pool/BurnRangeCall.sol b/contracts/libraries/range/pool/BurnRangeCall.sol index ecea2a48..178a76b2 100644 --- a/contracts/libraries/range/pool/BurnRangeCall.sol +++ b/contracts/libraries/range/pool/BurnRangeCall.sol @@ -9,6 +9,15 @@ import '../RangePositions.sol'; library BurnRangeCall { using SafeCast for int128; + event Burn( + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + event BurnRange( address indexed recipient, int24 lower, @@ -92,8 +101,10 @@ library BurnRangeCall { // transfer amounts to user if (cache.amount0 > 0 || cache.amount1 > 0) - Collect.range( + Collects.range( + cache.position, cache.constants, + msg.sender, params.to, cache.amount0, cache.amount1 diff --git a/contracts/libraries/range/pool/MintRangeCall.sol b/contracts/libraries/range/pool/MintRangeCall.sol index 9654f489..9c02de91 100644 --- a/contracts/libraries/range/pool/MintRangeCall.sol +++ b/contracts/libraries/range/pool/MintRangeCall.sol @@ -14,6 +14,16 @@ library MintRangeCall { using SafeCast for int128; using SafeCast for uint128; + event Mint( + address sender, + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + event MintRange( address indexed recipient, int24 lower, @@ -119,6 +129,16 @@ library MintRangeCall { cache.amount0 -= params.amount0.toInt128(); cache.amount1 -= params.amount1.toInt128(); + emit Mint( + msg.sender, + cache.owner, + cache.position.lower, + cache.position.upper, + cache.liquidityMinted.toUint128(), + uint128(-cache.amount0), + uint128(-cache.amount1) + ); + emit MintRange( cache.owner, cache.position.lower, @@ -137,9 +157,11 @@ library MintRangeCall { // transfer positive amounts back to user if (cache.feesAccrued0 > 0 || cache.feesAccrued1 > 0) - Collect.range( + Collects.range( + cache.position, cache.constants, cache.owner, + cache.owner, cache.feesAccrued0, cache.feesAccrued1 ); diff --git a/contracts/libraries/utils/Collect.sol b/contracts/libraries/utils/Collect.sol index 9cd22f45..0b6240ec 100644 --- a/contracts/libraries/utils/Collect.sol +++ b/contracts/libraries/utils/Collect.sol @@ -5,16 +5,23 @@ import '../../interfaces/structs/LimitPoolStructs.sol'; import '../limit/LimitPositions.sol'; import '../utils/SafeTransfers.sol'; -library Collect { +library Collects { using SafeCast for int128; using SafeCast for uint128; - event CollectRange0(uint128 amount0); - - event CollectRange1(uint128 amount1); + event Collect( + address indexed owner, + address recipient, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount0, + uint128 amount1 + ); function range( + RangePoolStructs.RangePosition memory position, PoolsharkStructs.LimitImmutables memory constants, + address owner, address recipient, int128 amount0, int128 amount1 @@ -27,7 +34,6 @@ library Collect { constants.token0, amount0.toUint128() ); - emit CollectRange0(amount0.toUint128()); } if (amount1 > 0) { /// @dev - cast to ensure user doesn't owe the pool balance @@ -36,8 +42,15 @@ library Collect { constants.token1, amount1.toUint128() ); - emit CollectRange1(amount1.toUint128()); } + emit Collect( + owner, + recipient, + position.lower, + position.upper, + amount0 > 0 ? amount0.toUint128() : 0, + amount1 > 0 ? amount1.toUint128() : 0 + ); } function burnLimit( diff --git a/test/contracts/libraries/samples.ts b/test/contracts/libraries/samples.ts index f5894765..5d579789 100644 --- a/test/contracts/libraries/samples.ts +++ b/test/contracts/libraries/samples.ts @@ -84,8 +84,8 @@ describe('Samples Tests', function () { if (debugMode) await getSample(debugMode) await validateSample({ - secondsPerLiquidityAccum: '2722258935367507707706996859454145691648', - tickSecondsAccum: '128760', + secondsPerLiquidityAccum: '3062541302288446171170371466885913903104', + tickSecondsAccum: '144855', averagePrice: '177157928842132501967358423881', averageLiquidity: '0', averageTick: 16095 @@ -107,8 +107,8 @@ describe('Samples Tests', function () { if (debugMode) await getSample(debugMode) await validateSample({ - secondsPerLiquidityAccum: '3743106036130323098097120681749450326016', - tickSecondsAccum: '177045', + secondsPerLiquidityAccum: '4083388403051261561560495289181218537472', + tickSecondsAccum: '193140', averagePrice: '177157928842132501967358423881', averageLiquidity: '0', averageTick: 16095 @@ -126,8 +126,8 @@ describe('Samples Tests', function () { }) await validateSample({ - secondsPerLiquidityAccum: '3743106036130323098112338571829045210211', - tickSecondsAccum: '209235', + secondsPerLiquidityAccum: '4083388403051261561575713179260813421667', + tickSecondsAccum: '225330', averagePrice: '177157928842132501967358423881', averageLiquidity: '30435780159189768390', averageTick: 16095 @@ -147,8 +147,8 @@ describe('Samples Tests', function () { }) await validateSample({ - secondsPerLiquidityAccum: '3743106036130323098127556461908640094406', - tickSecondsAccum: '209313', + secondsPerLiquidityAccum: '4083388403051261561590931069340408305862', + tickSecondsAccum: '225408', averagePrice: '79382800422362568253159300200', averageLiquidity: '30435780159189768390', averageTick: 39 @@ -168,8 +168,8 @@ describe('Samples Tests', function () { }) await validateSample({ - secondsPerLiquidityAccum: '3743106036130323098142774351988234978601', - tickSecondsAccum: '209471', + secondsPerLiquidityAccum: '4083388403051261561606148959420003190057', + tickSecondsAccum: '225566', averagePrice: '79541716941062961637430132579', averageLiquidity: '30435780159189768390', averageTick: 79 @@ -189,8 +189,8 @@ describe('Samples Tests', function () { }) await validateSample({ - secondsPerLiquidityAccum: '3743106036130323098142774351988234978601', - tickSecondsAccum: '209471', + secondsPerLiquidityAccum: '4083388403051261561606148959420003190057', + tickSecondsAccum: '225566', averagePrice: '79382800422362568253159300200', averageLiquidity: '15217890079594884196', averageTick: 39 diff --git a/test/contracts/tgedeployer.ts b/test/contracts/tgedeployer.ts index aab19bdd..71b3ea83 100644 --- a/test/contracts/tgedeployer.ts +++ b/test/contracts/tgedeployer.ts @@ -101,7 +101,7 @@ describe('TGE Deployment Tests', function () { }) }) - it.only('token1 - Should deploy TGE position', async function () { + it('token1 - Should deploy TGE position', async function () { await mintSigners20(hre.props.token0, tokenAmount.mul(55000), [hre.props.alice, hre.props.bob]) await mintSigners20(hre.props.token1, tokenAmount.mul(55000), [hre.props.alice, hre.props.bob]) From f2cee06805eae6435165091415c01e7a7212906a Mon Sep 17 00:00:00 2001 From: alphak3y Date: Fri, 29 Dec 2023 14:03:49 -0700 Subject: [PATCH 22/39] switch to GPL license --- contracts/LimitPool.sol | 2 +- contracts/LimitPoolFactory.sol | 2 +- .../base/storage/LimitPoolFactoryStorage.sol | 2 +- contracts/base/storage/LimitPoolStorage.sol | 2 +- .../interfaces/cover/ICoverPoolFactory.sol | 2 +- .../interfaces/limit/ILimitPoolFactory.sol | 2 +- .../interfaces/structs/LimitPoolStructs.sol | 2 +- contracts/libraries/Ticks.sol | 13 +- contracts/libraries/limit/Claims.sol | 2 +- contracts/libraries/limit/EpochMap.sol | 2 +- contracts/libraries/limit/LimitPositions.sol | 31 +-- .../libraries/limit/pool/BurnLimitCall.sol | 2 +- .../libraries/range/pool/BurnRangeCall.sol | 2 +- .../libraries/range/pool/MintRangeCall.sol | 6 +- contracts/libraries/utils/Collect.sol | 2 +- subgraph/abis/LimitPool.json | 199 +++++++++++++++++- subgraph/abis/LimitPoolFactory.json | 37 ++++ subgraph/src/mappings/limitpool.ts | 16 +- subgraph/src/mappings/limitpoolfactory.ts | 4 +- subgraph/src/mappings/pool/initialize.ts | 4 +- subgraph/src/mappings/pool/swap.ts | 4 +- subgraph/src/mappings/range/collect.ts | 170 +++++---------- subgraph/subgraph.yaml | 10 +- test/contracts/rangepool.ts | 7 +- 24 files changed, 334 insertions(+), 191 deletions(-) diff --git a/contracts/LimitPool.sol b/contracts/LimitPool.sol index 49432d9f..88b6d8e9 100644 --- a/contracts/LimitPool.sol +++ b/contracts/LimitPool.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BUSL-1.1 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import './interfaces/IPool.sol'; diff --git a/contracts/LimitPoolFactory.sol b/contracts/LimitPoolFactory.sol index 8fa2de63..c38b1c76 100644 --- a/contracts/LimitPoolFactory.sol +++ b/contracts/LimitPoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BUSL-1.1 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import './interfaces/range/IRangePool.sol'; diff --git a/contracts/base/storage/LimitPoolFactoryStorage.sol b/contracts/base/storage/LimitPoolFactoryStorage.sol index 67a27179..7b1be853 100644 --- a/contracts/base/storage/LimitPoolFactoryStorage.sol +++ b/contracts/base/storage/LimitPoolFactoryStorage.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BUSL-1.1 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; abstract contract LimitPoolFactoryStorage { diff --git a/contracts/base/storage/LimitPoolStorage.sol b/contracts/base/storage/LimitPoolStorage.sol index 29490120..68de1ccf 100644 --- a/contracts/base/storage/LimitPoolStorage.sol +++ b/contracts/base/storage/LimitPoolStorage.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BUSL-1.1 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../../interfaces/structs/RangePoolStructs.sol'; diff --git a/contracts/interfaces/cover/ICoverPoolFactory.sol b/contracts/interfaces/cover/ICoverPoolFactory.sol index 8b091dc7..87cf2186 100644 --- a/contracts/interfaces/cover/ICoverPoolFactory.sol +++ b/contracts/interfaces/cover/ICoverPoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BUSL-1.1 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; abstract contract ICoverPoolFactory { diff --git a/contracts/interfaces/limit/ILimitPoolFactory.sol b/contracts/interfaces/limit/ILimitPoolFactory.sol index 8c3e4411..93f4c991 100644 --- a/contracts/interfaces/limit/ILimitPoolFactory.sol +++ b/contracts/interfaces/limit/ILimitPoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BUSL-1.1 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/structs/LimitPoolStructs.sol b/contracts/interfaces/structs/LimitPoolStructs.sol index 83558ae1..48a720bb 100644 --- a/contracts/interfaces/structs/LimitPoolStructs.sol +++ b/contracts/interfaces/structs/LimitPoolStructs.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BUSL-1.1 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import './PoolsharkStructs.sol'; diff --git a/contracts/libraries/Ticks.sol b/contracts/libraries/Ticks.sol index c8f3ecfd..6fa409eb 100644 --- a/contracts/libraries/Ticks.sol +++ b/contracts/libraries/Ticks.sol @@ -121,16 +121,16 @@ library Ticks { // emit standard event emit Initialize( - state.pool0.price, - state.pool0.tickAtPrice + startPrice, + startTick ); // emit custom event emit InitializeLimit( ConstantProduct.minTick(constants.tickSpacing), ConstantProduct.maxTick(constants.tickSpacing), - state.pool0.price, - state.pool0.tickAtPrice + startPrice, + startTick ); return state; @@ -263,6 +263,7 @@ library Ticks { params.zeroForOne, params.exactIn ); + return cache; } @@ -387,6 +388,8 @@ library Ticks { zeroForOne, cache.exactIn ); + if (newPrice < nextPrice) + require(false, 'NextPriceExceeded()'); if (cache.exactIn) { amountIn = cache.amountLeft; amountOut = ConstantProduct.getDy( @@ -456,6 +459,8 @@ library Ticks { zeroForOne, cache.exactIn ); + if (newPrice > nextPrice) + require(false, 'NextPriceExceeded()'); if (cache.exactIn) { amountIn = cache.amountLeft; amountOut = ConstantProduct.getDx( diff --git a/contracts/libraries/limit/Claims.sol b/contracts/libraries/limit/Claims.sol index 461376ae..02f88dc5 100644 --- a/contracts/libraries/limit/Claims.sol +++ b/contracts/libraries/limit/Claims.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BUSL-1.1 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/limit/EpochMap.sol b/contracts/libraries/limit/EpochMap.sol index 5c31c69b..0fae9697 100644 --- a/contracts/libraries/limit/EpochMap.sol +++ b/contracts/libraries/limit/EpochMap.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BUSL-1.1 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../math/ConstantProduct.sol'; diff --git a/contracts/libraries/limit/LimitPositions.sol b/contracts/libraries/limit/LimitPositions.sol index 47c32624..b2dd175d 100644 --- a/contracts/libraries/limit/LimitPositions.sol +++ b/contracts/libraries/limit/LimitPositions.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BUSL-1.1 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import './LimitTicks.sol'; @@ -412,12 +412,27 @@ library LimitPositions { ); } + // emit custom event + emit BurnLimit( + params.to, + params.positionId, + cache.position.lower, + cache.position.upper, + cache.claim, + params.claim, + params.zeroForOne, + cache.liquidityBurned, + cache.amountIn, + cache.amountOut + ); + // clear filled position if ( params.zeroForOne ? params.claim == cache.position.upper : params.claim == cache.position.lower ) { + cache.liquidityBurned = cache.position.liquidity; cache.state.liquidityGlobal -= cache.position.liquidity; cache.position.liquidity = 0; } @@ -440,20 +455,6 @@ library LimitPositions { : cache.amountOut ); - // emit custom event - emit BurnLimit( - params.to, - params.positionId, - cache.position.lower, - cache.position.upper, - cache.claim, - params.claim, - params.zeroForOne, - cache.liquidityBurned, - cache.amountIn, - cache.amountOut - ); - // save pool to state in memory if (params.zeroForOne) cache.state.pool0 = cache.pool; else cache.state.pool1 = cache.pool; diff --git a/contracts/libraries/limit/pool/BurnLimitCall.sol b/contracts/libraries/limit/pool/BurnLimitCall.sol index 021a0f95..70617e92 100644 --- a/contracts/libraries/limit/pool/BurnLimitCall.sol +++ b/contracts/libraries/limit/pool/BurnLimitCall.sol @@ -99,7 +99,7 @@ library BurnLimitCall { save(cache, globalState, params.zeroForOne); BurnLimitLocals memory locals; - (cache, locals.amount0Delta, locals.amount1Delta) = Collects.burnLimit( + (cache, locals.amount0Delta, locals.amount1Delta) = CollectLib.burnLimit( cache, params ); diff --git a/contracts/libraries/range/pool/BurnRangeCall.sol b/contracts/libraries/range/pool/BurnRangeCall.sol index 178a76b2..1e2b2656 100644 --- a/contracts/libraries/range/pool/BurnRangeCall.sol +++ b/contracts/libraries/range/pool/BurnRangeCall.sol @@ -101,7 +101,7 @@ library BurnRangeCall { // transfer amounts to user if (cache.amount0 > 0 || cache.amount1 > 0) - Collects.range( + CollectLib.range( cache.position, cache.constants, msg.sender, diff --git a/contracts/libraries/range/pool/MintRangeCall.sol b/contracts/libraries/range/pool/MintRangeCall.sol index 9c02de91..d10f85c4 100644 --- a/contracts/libraries/range/pool/MintRangeCall.sol +++ b/contracts/libraries/range/pool/MintRangeCall.sol @@ -135,8 +135,8 @@ library MintRangeCall { cache.position.lower, cache.position.upper, cache.liquidityMinted.toUint128(), - uint128(-cache.amount0), - uint128(-cache.amount1) + params.amount0, + params.amount1 ); emit MintRange( @@ -157,7 +157,7 @@ library MintRangeCall { // transfer positive amounts back to user if (cache.feesAccrued0 > 0 || cache.feesAccrued1 > 0) - Collects.range( + CollectLib.range( cache.position, cache.constants, cache.owner, diff --git a/contracts/libraries/utils/Collect.sol b/contracts/libraries/utils/Collect.sol index 0b6240ec..225f00b8 100644 --- a/contracts/libraries/utils/Collect.sol +++ b/contracts/libraries/utils/Collect.sol @@ -5,7 +5,7 @@ import '../../interfaces/structs/LimitPoolStructs.sol'; import '../limit/LimitPositions.sol'; import '../utils/SafeTransfers.sol'; -library Collects { +library CollectLib { using SafeCast for int128; using SafeCast for uint128; diff --git a/subgraph/abis/LimitPool.json b/subgraph/abis/LimitPool.json index cf430cb0..c95053f3 100644 --- a/subgraph/abis/LimitPool.json +++ b/subgraph/abis/LimitPool.json @@ -1,4 +1,47 @@ [ + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickLower", + "type": "int24" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickUpper", + "type": "int24" + }, + { + "indexed": false, + "internalType": "uint128", + "name": "amount", + "type": "uint128" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount0", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount1", + "type": "uint256" + } + ], + "name": "Burn", + "type": "event" + }, { "anonymous": false, "inputs": [ @@ -106,19 +149,36 @@ { "anonymous": false, "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "recipient", + "type": "address" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickLower", + "type": "int24" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickUpper", + "type": "int24" + }, { "indexed": false, "internalType": "uint128", "name": "amount0", "type": "uint128" - } - ], - "name": "CollectRange0", - "type": "event" - }, - { - "anonymous": false, - "inputs": [ + }, { "indexed": false, "internalType": "uint128", @@ -126,7 +186,7 @@ "type": "uint128" } ], - "name": "CollectRange1", + "name": "Collect", "type": "event" }, { @@ -148,6 +208,25 @@ "name": "CompoundRange", "type": "event" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint160", + "name": "price", + "type": "uint160" + }, + { + "indexed": false, + "internalType": "int24", + "name": "tick", + "type": "int24" + } + ], + "name": "Initialize", + "type": "event" + }, { "anonymous": false, "inputs": [ @@ -176,7 +255,56 @@ "type": "int24" } ], - "name": "Initialize", + "name": "InitializeLimit", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "sender", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickLower", + "type": "int24" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickUpper", + "type": "int24" + }, + { + "indexed": false, + "internalType": "uint128", + "name": "amount", + "type": "uint128" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount0", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount1", + "type": "uint256" + } + ], + "name": "Mint", "type": "event" }, { @@ -315,6 +443,55 @@ "name": "SampleRecorded", "type": "event" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "sender", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "recipient", + "type": "address" + }, + { + "indexed": false, + "internalType": "int256", + "name": "amount0", + "type": "int256" + }, + { + "indexed": false, + "internalType": "int256", + "name": "amount1", + "type": "int256" + }, + { + "indexed": false, + "internalType": "uint160", + "name": "price", + "type": "uint160" + }, + { + "indexed": false, + "internalType": "uint128", + "name": "liquidity", + "type": "uint128" + }, + { + "indexed": false, + "internalType": "int24", + "name": "tickAtPrice", + "type": "int24" + } + ], + "name": "Swap", + "type": "event" + }, { "anonymous": false, "inputs": [ @@ -385,7 +562,7 @@ "type": "bool" } ], - "name": "Swap", + "name": "SwapLimit", "type": "event" }, { diff --git a/subgraph/abis/LimitPoolFactory.json b/subgraph/abis/LimitPoolFactory.json index 17e9a4ac..a4bdcc3b 100644 --- a/subgraph/abis/LimitPoolFactory.json +++ b/subgraph/abis/LimitPoolFactory.json @@ -45,6 +45,43 @@ "type": "uint16" } ], + "name": "LimitPoolCreated", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "token0", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "token1", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint24", + "name": "fee", + "type": "uint24" + }, + { + "indexed": false, + "internalType": "int24", + "name": "tickSpacing", + "type": "int24" + }, + { + "indexed": false, + "internalType": "address", + "name": "pool", + "type": "address" + } + ], "name": "PoolCreated", "type": "event" } diff --git a/subgraph/src/mappings/limitpool.ts b/subgraph/src/mappings/limitpool.ts index e982600c..27ddf7ee 100644 --- a/subgraph/src/mappings/limitpool.ts +++ b/subgraph/src/mappings/limitpool.ts @@ -11,15 +11,15 @@ import { handleMintRange as handleMintRangeHelper } from './range/mint' import { handleBurnRange as handleBurnRangeHelper } from './range/burn' import { handleCompoundRange as handleCompoundRangeHelper } from './range/compound' import { handleSyncRangeTick as handleSyncRangeTickHelper } from './range/synctick' -import { BurnLimit, BurnRange, CollectRange0, CollectRange1, CompoundRange, Initialize, MintLimit, MintRange, SampleCountIncreased, SampleRecorded, Swap, SyncLimitLiquidity, SyncLimitPool, SyncLimitTick, SyncRangeTick } from '../../generated/LimitPoolFactory/LimitPool' -import { handleCollectRange0 as handleCollectRange0Helper, handleCollectRange1 as handleCollectRange1Helper } from './range/collect' +import { BurnLimit, BurnRange, Collect, CompoundRange, InitializeLimit, MintLimit, MintRange, SampleCountIncreased, SampleRecorded, Swap, SwapLimit, SyncLimitLiquidity, SyncLimitPool, SyncLimitTick, SyncRangeTick } from '../../generated/LimitPoolFactory/LimitPool' +import { handleCollect as handleCollectHelper } from './range/collect' // pool -export function handleInitialize(event: Initialize): void { +export function handleInitialize(event: InitializeLimit): void { handleInitializeHelper(event) } -export function handleSwap(event: Swap): void { +export function handleSwap(event: SwapLimit): void { handleSwapHelper(event) } @@ -49,12 +49,8 @@ export function handleSyncRangeTick(event: SyncRangeTick): void { handleSyncRangeTickHelper(event) } -export function handleCollectRange0(event: CollectRange0): void { - handleCollectRange0Helper(event) -} - -export function handleCollectRange1(event: CollectRange1): void { - handleCollectRange1Helper(event) +export function handleCollect(event: Collect): void { + handleCollectHelper(event) } // limit positions diff --git a/subgraph/src/mappings/limitpoolfactory.ts b/subgraph/src/mappings/limitpoolfactory.ts index 346a76a3..8a626f64 100644 --- a/subgraph/src/mappings/limitpoolfactory.ts +++ b/subgraph/src/mappings/limitpoolfactory.ts @@ -1,5 +1,5 @@ import { Address, log } from '@graphprotocol/graph-ts' -import { PoolCreated } from '../../generated/LimitPoolFactory/LimitPoolFactory' +import { LimitPoolCreated, PoolCreated } from '../../generated/LimitPoolFactory/LimitPoolFactory' import { LimitPoolTemplate, PositionERC1155Template, RangeStakerTemplate } from '../../generated/templates' import { fetchTokenSymbol, @@ -11,7 +11,7 @@ import { safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadLimitPoolToken, sa import { BigInt } from '@graphprotocol/graph-ts' import { FACTORY_ADDRESS, ONE_BI, RANGE_STAKER_ADDRESS, STABLE_COINS, WETH_ADDRESS } from '../constants/arbitrum' -export function handlePoolCreated(event: PoolCreated): void { +export function handlePoolCreated(event: LimitPoolCreated): void { // grab event parameters let poolAddressParam = event.params.pool let poolTokenParam = event.params.token diff --git a/subgraph/src/mappings/pool/initialize.ts b/subgraph/src/mappings/pool/initialize.ts index f3a012cb..4adb0cb3 100644 --- a/subgraph/src/mappings/pool/initialize.ts +++ b/subgraph/src/mappings/pool/initialize.ts @@ -1,9 +1,9 @@ import { BigInt, log } from "@graphprotocol/graph-ts" -import { Initialize } from "../../../generated/LimitPoolFactory/LimitPool" +import { Initialize, InitializeLimit } from "../../../generated/LimitPoolFactory/LimitPool" import { safeLoadBasePrice, safeLoadLimitPool, safeLoadLimitTick, safeLoadRangeTick, safeLoadToken } from "../utils/loads" import { sqrtPriceX96ToTokenPrices, findEthPerToken } from "../utils/price" -export function handleInitialize(event: Initialize): void { +export function handleInitialize(event: InitializeLimit): void { let minTickParam = event.params.minTick let maxTickParam = event.params.maxTick let startPriceParam = event.params.startPrice diff --git a/subgraph/src/mappings/pool/swap.ts b/subgraph/src/mappings/pool/swap.ts index 522cb7c2..b3732e43 100644 --- a/subgraph/src/mappings/pool/swap.ts +++ b/subgraph/src/mappings/pool/swap.ts @@ -4,10 +4,10 @@ import { BIGDECIMAL_ZERO, BIGINT_ONE, BIGINT_ZERO, convertTokenToDecimal } from import { ZERO_BD, TWO_BD, ONE_BI, FACTORY_ADDRESS, SEASON_1_END_TIME, SEASON_1_START_TIME } from "../../constants/arbitrum" import { AmountType, findEthPerToken, getAdjustedAmounts, getEthPriceInUSD, sqrtPriceX96ToTokenPrices } from "../utils/price" import { updateDerivedTVLAmounts } from "../utils/tvl" -import { Swap } from "../../../generated/LimitPoolFactory/LimitPool" +import { Swap, SwapLimit } from "../../../generated/LimitPoolFactory/LimitPool" import { safeDiv } from "../utils/math" -export function handleSwap(event: Swap): void { +export function handleSwap(event: SwapLimit): void { let recipientParam = event.params.recipient let amountInParam = event.params.amountIn let amountOutParam = event.params.amountOut diff --git a/subgraph/src/mappings/range/collect.ts b/subgraph/src/mappings/range/collect.ts index e845200d..73b5a996 100644 --- a/subgraph/src/mappings/range/collect.ts +++ b/subgraph/src/mappings/range/collect.ts @@ -1,121 +1,51 @@ -import { safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadToken, safeLoadBasePrice, safeLoadTvlUpdateLog } from "../utils/loads" +import { safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadToken, safeLoadBasePrice } from "../utils/loads" import { BIGINT_ZERO, convertTokenToDecimal } from "../utils/helpers" -import { CollectRange0, CollectRange1 } from "../../../generated/LimitPoolFactory/LimitPool" import { findEthPerToken } from "../utils/price" import { updateDerivedTVLAmounts } from "../utils/tvl" - -export function handleCollectRange0(event: CollectRange0): void { - // let amount0Param = event.params.amount0 - // let amount1Param = BIGINT_ZERO - // let poolAddress = event.address.toHex() - // let senderParam = event.transaction.from - - // let loadLimitPool = safeLoadLimitPool(poolAddress) - // let pool = loadLimitPool.entity // 1 - - // let loadLimitPoolFactory = safeLoadLimitPoolFactory(pool.factory) - // let loadToken0 = safeLoadToken(pool.token0) - // let loadToken1 = safeLoadToken(pool.token1) - // let loadBasePrice = safeLoadBasePrice('eth') - - // let factory = loadLimitPoolFactory.entity // 2 - // let token0 = loadToken0.entity // 3 - // let token1 = loadToken1.entity // 4 - // let basePrice = loadBasePrice.entity // 5 - - // let amount0 = convertTokenToDecimal(amount0Param, token0.decimals) - // let amount1 = convertTokenToDecimal(amount1Param, token1.decimals) - // let amountUsd = amount0 - // .times(token0.ethPrice.times(basePrice.USD)) - // .plus(amount1.times(token1.ethPrice.times(basePrice.USD))) - - // // eth price updates - // token0.ethPrice = findEthPerToken(token0, token1, basePrice) - // token1.ethPrice = findEthPerToken(token1, token0, basePrice) - // token0.usdPrice = token0.ethPrice.times(basePrice.USD) - // token1.usdPrice = token1.ethPrice.times(basePrice.USD) - - // // tvl updates - // let oldPoolTotalValueLockedEth = pool.totalValueLockedEth - // token0.totalValueLocked = token0.totalValueLocked.minus(amount0) - // token1.totalValueLocked = token1.totalValueLocked.minus(amount1) - // pool.totalValueLocked0 = pool.totalValueLocked0.minus(amount0) - // pool.totalValueLocked1 = pool.totalValueLocked1.minus(amount1) - // let updateTvlRet = updateDerivedTVLAmounts(token0, token1, pool, factory, basePrice, oldPoolTotalValueLockedEth) - // token0 = updateTvlRet.token0 - // token1 = updateTvlRet.token1 - // pool = updateTvlRet.pool - // factory = updateTvlRet.factory - - // let loadTvlUpdateLog = safeLoadTvlUpdateLog(event.transaction.hash, poolAddress) - // let tvlUpdateLog = loadTvlUpdateLog.entity - - // tvlUpdateLog.pool = poolAddress - // tvlUpdateLog.eventName = "CollectRange" - // tvlUpdateLog.txnHash = event.transaction.hash - // tvlUpdateLog.txnBlockNumber = event.block.number - // tvlUpdateLog.amount0Change = amount0.neg() - // tvlUpdateLog.amount1Change = amount1.neg() - // tvlUpdateLog.amount0Total = pool.totalValueLocked0 - // tvlUpdateLog.amount1Total = pool.totalValueLocked1 - // tvlUpdateLog.token0UsdPrice = token0.usdPrice - // tvlUpdateLog.token1UsdPrice = token1.usdPrice - // tvlUpdateLog.amountUsdChange = amount0 - // .times(token0.ethPrice.times(basePrice.USD)) - // .plus(amount1.times(token1.ethPrice.times(basePrice.USD))).neg() - // tvlUpdateLog.amountUsdTotal = pool.totalValueLockedUsd - - // tvlUpdateLog.save() - - // pool.save() - // token0.save() - // token1.save() - // factory.save() - // basePrice.save() -} - -export function handleCollectRange1(event: CollectRange1): void { - // let amount0Param = BIGINT_ZERO - // let amount1Param = event.params.amount1 - // let poolAddress = event.address.toHex() - // let senderParam = event.transaction.from - - // let loadLimitPool = safeLoadLimitPool(poolAddress) - // let pool = loadLimitPool.entity // 1 - - // let loadLimitPoolFactory = safeLoadLimitPoolFactory(pool.factory) - // let loadToken0 = safeLoadToken(pool.token0) - // let loadToken1 = safeLoadToken(pool.token1) - // let loadBasePrice = safeLoadBasePrice('eth') - - // let factory = loadLimitPoolFactory.entity // 2 - // let token0 = loadToken0.entity // 3 - // let token1 = loadToken1.entity // 4 - // let basePrice = loadBasePrice.entity // 5 - - // let amount0 = convertTokenToDecimal(amount0Param, token0.decimals) - // let amount1 = convertTokenToDecimal(amount1Param, token1.decimals) - // let amountUsd = amount0 - // .times(token0.ethPrice.times(basePrice.USD)) - // .plus(amount1.times(token1.ethPrice.times(basePrice.USD))) - - // // eth price updates - // token0.ethPrice = findEthPerToken(token0, token1, basePrice) - // token1.ethPrice = findEthPerToken(token1, token0, basePrice) - // token0.usdPrice = token0.ethPrice.times(basePrice.USD) - // token1.usdPrice = token1.ethPrice.times(basePrice.USD) - - // // tvl updates - // let oldPoolTotalValueLockedEth = pool.totalValueLockedEth - // token0.totalValueLocked = token0.totalValueLocked.minus(amount0) - // token1.totalValueLocked = token1.totalValueLocked.minus(amount1) - // pool.totalValueLocked0 = pool.totalValueLocked0.minus(amount0) - // pool.totalValueLocked1 = pool.totalValueLocked1.minus(amount1) - // let updateTvlRet = updateDerivedTVLAmounts(token0, token1, pool, factory, basePrice, oldPoolTotalValueLockedEth) - // token0 = updateTvlRet.token0 - // token1 = updateTvlRet.token1 - // pool = updateTvlRet.pool - // factory = updateTvlRet.factory +import { Collect } from "../../../generated/LimitPoolFactory/LimitPool" + +export function handleCollect(event: Collect): void { + let amount0Param = event.params.amount0 + let amount1Param = BIGINT_ZERO + let poolAddress = event.address.toHex() + let senderParam = event.transaction.from + + let loadLimitPool = safeLoadLimitPool(poolAddress) + let pool = loadLimitPool.entity // 1 + + let loadLimitPoolFactory = safeLoadLimitPoolFactory(pool.factory) + let loadToken0 = safeLoadToken(pool.token0) + let loadToken1 = safeLoadToken(pool.token1) + let loadBasePrice = safeLoadBasePrice('eth') + + let factory = loadLimitPoolFactory.entity // 2 + let token0 = loadToken0.entity // 3 + let token1 = loadToken1.entity // 4 + let basePrice = loadBasePrice.entity // 5 + + let amount0 = convertTokenToDecimal(amount0Param, token0.decimals) + let amount1 = convertTokenToDecimal(amount1Param, token1.decimals) + let amountUsd = amount0 + .times(token0.ethPrice.times(basePrice.USD)) + .plus(amount1.times(token1.ethPrice.times(basePrice.USD))) + + // eth price updates + token0.ethPrice = findEthPerToken(token0, token1, pool, basePrice) + token1.ethPrice = findEthPerToken(token1, token0, pool, basePrice) + token0.usdPrice = token0.ethPrice.times(basePrice.USD) + token1.usdPrice = token1.ethPrice.times(basePrice.USD) + + // tvl updates + let oldPoolTotalValueLockedEth = pool.totalValueLockedEth + token0.totalValueLocked = token0.totalValueLocked.minus(amount0) + token1.totalValueLocked = token1.totalValueLocked.minus(amount1) + pool.totalValueLocked0 = pool.totalValueLocked0.minus(amount0) + pool.totalValueLocked1 = pool.totalValueLocked1.minus(amount1) + let updateTvlRet = updateDerivedTVLAmounts(token0, token1, pool, factory, basePrice, oldPoolTotalValueLockedEth) + token0 = updateTvlRet.token0 + token1 = updateTvlRet.token1 + pool = updateTvlRet.pool + factory = updateTvlRet.factory // let loadTvlUpdateLog = safeLoadTvlUpdateLog(event.transaction.hash, poolAddress) // let tvlUpdateLog = loadTvlUpdateLog.entity @@ -137,9 +67,9 @@ export function handleCollectRange1(event: CollectRange1): void { // tvlUpdateLog.save() - // pool.save() - // token0.save() - // token1.save() - // factory.save() - // basePrice.save() + pool.save() // 1 + token0.save() // 2 + token1.save() // 3 + factory.save() // 4 + basePrice.save() // 5 } \ No newline at end of file diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 1af329f1..e248cdb3 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -30,9 +30,9 @@ templates: - name: ERC20NameBytes file: ./abis/ERC20NameBytes.json eventHandlers: - - event: Initialize(int24,int24,uint160,int24) + - event: InitializeLimit(int24,int24,uint160,int24) handler: handleInitialize - - event: Swap(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + - event: SwapLimit(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) handler: handleSwap - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) handler: handleMintLimit @@ -56,10 +56,8 @@ templates: handler: handleSyncLimitLiquidity - event: SyncLimitTick(uint32,int24,bool) handler: handleSyncLimitTick - - event: CollectRange0(uint128) + - event: Collect(indexed address,address,indexed int24,indexed int24,uint128,uint128) handler: handleCollectRange0 - - event: CollectRange1(uint128) - handler: handleCollectRange1 # ERC-1155 events - kind: ethereum/contract name: PositionERC1155Template @@ -138,7 +136,7 @@ dataSources: - name: ERC20NameBytes file: ./abis/ERC20NameBytes.json eventHandlers: - - event: PoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + - event: LimitPoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) handler: handlePoolCreated - kind: ethereum/contract name: LimitPoolManager diff --git a/test/contracts/rangepool.ts b/test/contracts/rangepool.ts index 8129ff24..27c92b0c 100644 --- a/test/contracts/rangepool.ts +++ b/test/contracts/rangepool.ts @@ -174,8 +174,7 @@ describe('RangePool Exact In Tests', function () { }) it('pool - Should not skip crossing tick - kyber exploit', async function () { - console.log('kyber pool:', hre.props.kyberPool.address, hre.props.kyberPoolToken.address) - + if (debugMode) console.log('kyber pool:', hre.props.kyberPool.address, hre.props.kyberPoolToken.address) await validateSwap({ signer: hre.props.alice, recipient: hre.props.alice.address, @@ -236,8 +235,8 @@ describe('RangePool Exact In Tests', function () { poolContract: hre.props.kyberPool, revertMessage: '', }) - await getPrice(hre.props.kyberPool) - await getRangeLiquidity() + if (debugMode) await getPrice(hre.props.kyberPool) + if (debugMode) await getRangeLiquidity() if (balanceCheck) { console.log('balance after token0:', (await hre.props.token0.balanceOf(hre.props.limitPool.address)).toString()) console.log('balance after token1:', (await hre.props.token1.balanceOf(hre.props.limitPool.address)).toString()) From 088bd4145f5c5c5424ddf1ebddfee3d1b2064cce Mon Sep 17 00:00:00 2001 From: alphak3y Date: Fri, 29 Dec 2023 14:08:07 -0700 Subject: [PATCH 23/39] update licenses --- contracts/base/events/PositionERC1155Events.sol | 2 +- contracts/interfaces/range/IRangePoolFactory.sol | 2 +- contracts/interfaces/structs/PoolsharkStructs.sol | 2 +- contracts/interfaces/structs/RangePoolStructs.sol | 2 +- contracts/libraries/Samples.sol | 2 +- contracts/libraries/TickMap.sol | 2 +- contracts/libraries/Ticks.sol | 2 +- contracts/libraries/limit/LimitTicks.sol | 2 +- contracts/libraries/range/RangePositions.sol | 2 +- contracts/libraries/range/RangeTicks.sol | 2 +- contracts/libraries/range/math/FeeMath.sol | 2 +- contracts/libraries/utils/PositionTokens.sol | 2 +- contracts/test/WETH9.sol | 2 +- contracts/utils/PoolsharkRouter.sol | 2 +- 14 files changed, 14 insertions(+), 14 deletions(-) diff --git a/contracts/base/events/PositionERC1155Events.sol b/contracts/base/events/PositionERC1155Events.sol index 49a0c8ba..131516da 100644 --- a/contracts/base/events/PositionERC1155Events.sol +++ b/contracts/base/events/PositionERC1155Events.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; /// @notice Events emitted by the PositionERC1155 contract(s) diff --git a/contracts/interfaces/range/IRangePoolFactory.sol b/contracts/interfaces/range/IRangePoolFactory.sol index ea566475..8e26a4ba 100644 --- a/contracts/interfaces/range/IRangePoolFactory.sol +++ b/contracts/interfaces/range/IRangePoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; interface IRangePoolFactory { diff --git a/contracts/interfaces/structs/PoolsharkStructs.sol b/contracts/interfaces/structs/PoolsharkStructs.sol index 176a5457..7332f984 100644 --- a/contracts/interfaces/structs/PoolsharkStructs.sol +++ b/contracts/interfaces/structs/PoolsharkStructs.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../cover/ITwapSource.sol'; diff --git a/contracts/interfaces/structs/RangePoolStructs.sol b/contracts/interfaces/structs/RangePoolStructs.sol index cdea9143..02610636 100644 --- a/contracts/interfaces/structs/RangePoolStructs.sol +++ b/contracts/interfaces/structs/RangePoolStructs.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import './PoolsharkStructs.sol'; diff --git a/contracts/libraries/Samples.sol b/contracts/libraries/Samples.sol index 609cb9ab..d5754b03 100644 --- a/contracts/libraries/Samples.sol +++ b/contracts/libraries/Samples.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import './math/ConstantProduct.sol'; diff --git a/contracts/libraries/TickMap.sol b/contracts/libraries/TickMap.sol index adb936f2..8a7b86a5 100644 --- a/contracts/libraries/TickMap.sol +++ b/contracts/libraries/TickMap.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import './math/ConstantProduct.sol'; diff --git a/contracts/libraries/Ticks.sol b/contracts/libraries/Ticks.sol index 6fa409eb..c48c0347 100644 --- a/contracts/libraries/Ticks.sol +++ b/contracts/libraries/Ticks.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/libraries/limit/LimitTicks.sol b/contracts/libraries/limit/LimitTicks.sol index 96ded03a..4948f1bf 100644 --- a/contracts/libraries/limit/LimitTicks.sol +++ b/contracts/libraries/limit/LimitTicks.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/range/RangePositions.sol b/contracts/libraries/range/RangePositions.sol index c3babf23..4a324bfb 100644 --- a/contracts/libraries/range/RangePositions.sol +++ b/contracts/libraries/range/RangePositions.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../../interfaces/IPool.sol'; diff --git a/contracts/libraries/range/RangeTicks.sol b/contracts/libraries/range/RangeTicks.sol index b3644e83..8f97fcf6 100644 --- a/contracts/libraries/range/RangeTicks.sol +++ b/contracts/libraries/range/RangeTicks.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/libraries/range/math/FeeMath.sol b/contracts/libraries/range/math/FeeMath.sol index 0c08a2fa..a6f46024 100644 --- a/contracts/libraries/range/math/FeeMath.sol +++ b/contracts/libraries/range/math/FeeMath.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../../Samples.sol'; diff --git a/contracts/libraries/utils/PositionTokens.sol b/contracts/libraries/utils/PositionTokens.sol index d006917f..ba9d0660 100644 --- a/contracts/libraries/utils/PositionTokens.sol +++ b/contracts/libraries/utils/PositionTokens.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import './Bytes.sol'; diff --git a/contracts/test/WETH9.sol b/contracts/test/WETH9.sol index cc6f2eed..6be3cdb0 100644 --- a/contracts/test/WETH9.sol +++ b/contracts/test/WETH9.sol @@ -1,4 +1,4 @@ -//SPDX-License-Identifier: GPLv3 +//SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; contract WETH9 { diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index 30d2f3b0..80b740ab 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPLv3 +// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.18; import '../interfaces/IPool.sol'; From c43e4ad68041ecf5f65b25e4d03aaa4ef9c5dd9c Mon Sep 17 00:00:00 2001 From: alphak3y Date: Fri, 29 Dec 2023 14:24:45 -0700 Subject: [PATCH 24/39] cleanup swap event --- contracts/libraries/pool/SwapCall.sol | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/contracts/libraries/pool/SwapCall.sol b/contracts/libraries/pool/SwapCall.sol index 616e84d8..dd63674f 100644 --- a/contracts/libraries/pool/SwapCall.sol +++ b/contracts/libraries/pool/SwapCall.sol @@ -9,16 +9,6 @@ import '../utils/Collect.sol'; import '../utils/SafeTransfers.sol'; library SwapCall { - // event Swap( - // address indexed recipient, - // bool zeroForOne, - // uint256 amountIn, - // uint256 amountOut, - // uint160 price, - // uint128 liquidity, - // uint128 feeAmount, - // int24 tickAtPrice - // ); event Event(); From 0b128cb79d2ba96156f483b5102929acedf73e3d Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sat, 6 Jan 2024 16:11:56 -0700 Subject: [PATCH 25/39] split BUSL and APGL licenses; v3 pool for eth price --- contracts/LimitPool.sol | 40 +- contracts/LimitPoolFactory.sol | 42 +- contracts/base/events/LimitPoolEvents.sol | 2 +- .../base/events/LimitPoolFactoryEvents.sol | 2 +- .../base/events/LimitPoolManagerEvents.sol | 9 +- .../base/events/PoolsharkRouterEvents.sol | 2 +- .../base/events/PositionERC1155Events.sol | 2 +- contracts/base/events/RangeStakerEvents.sol | 2 +- .../base/storage/LimitPoolFactoryStorage.sol | 2 +- .../base/storage/LimitPoolImmutables.sol | 2 +- contracts/base/storage/LimitPoolStorage.sol | 2 +- .../storage/PositionERC1155Immutables.sol | 2 +- contracts/interfaces/IERC20Minimal.sol | 2 +- contracts/interfaces/IPool.sol | 2 +- contracts/interfaces/IPositionERC1155.sol | 2 +- contracts/interfaces/IWETH9.sol | 2 +- .../callbacks/ICoverPoolCallback.sol | 2 +- .../callbacks/ILimitPoolCallback.sol | 2 +- contracts/interfaces/cover/ICoverPool.sol | 2 +- .../interfaces/cover/ICoverPoolFactory.sol | 2 +- contracts/interfaces/cover/ITwapSource.sol | 2 +- contracts/interfaces/limit/ILimitPool.sol | 2 +- .../interfaces/limit/ILimitPoolFactory.sol | 2 +- .../interfaces/limit/ILimitPoolManager.sol | 4 +- .../limit/ILimitPoolStorageView.sol | 2 +- contracts/interfaces/limit/ILimitPoolView.sol | 2 +- contracts/interfaces/range/IRangePool.sol | 2 +- .../interfaces/range/IRangePoolFactory.sol | 2 +- .../interfaces/range/IRangePoolManager.sol | 2 +- contracts/interfaces/staking/IRangeStaker.sol | 2 +- .../interfaces/structs/LimitPoolStructs.sol | 2 +- .../interfaces/structs/PoolsharkStructs.sol | 9 +- .../interfaces/structs/RangePoolStructs.sol | 2 +- contracts/libraries/Samples.sol | 2 +- contracts/libraries/TickMap.sol | 2 +- contracts/libraries/Ticks.sol | 2 +- contracts/libraries/limit/Claims.sol | 2 +- contracts/libraries/limit/EpochMap.sol | 2 +- contracts/libraries/limit/LimitPositions.sol | 2 +- contracts/libraries/limit/LimitTicks.sol | 2 +- .../libraries/limit/pool/BurnLimitCall.sol | 2 +- .../libraries/limit/pool/MintLimitCall.sol | 2 +- .../limit/pool/SnapshotLimitCall.sol | 2 +- contracts/libraries/math/ConstantProduct.sol | 2 +- contracts/libraries/math/OverflowMath.sol | 2 +- contracts/libraries/pool/FeesCall.sol | 2 +- contracts/libraries/pool/QuoteCall.sol | 2 +- contracts/libraries/pool/SampleCall.sol | 2 +- contracts/libraries/pool/SwapCall.sol | 2 +- contracts/libraries/range/RangePositions.sol | 2 +- contracts/libraries/range/RangeTicks.sol | 2 +- contracts/libraries/range/math/FeeMath.sol | 4 +- .../libraries/range/pool/BurnRangeCall.sol | 2 +- .../libraries/range/pool/MintRangeCall.sol | 2 +- .../range/pool/SnapshotRangeCall.sol | 2 +- contracts/libraries/utils/Bytes.sol | 2 +- contracts/libraries/utils/Collect.sol | 2 +- contracts/libraries/utils/PositionTokens.sol | 2 +- contracts/libraries/utils/SafeCast.sol | 2 +- contracts/libraries/utils/SafeTransfers.sol | 2 +- contracts/libraries/utils/String.sol | 2 +- contracts/staking/RangeStaker.sol | 2 +- contracts/test/WETH9.sol | 2 +- contracts/utils/LimitPoolManager.sol | 62 +- contracts/utils/PoolsharkRouter.sol | 25 +- contracts/utils/PositionERC1155.sol | 2 +- scripts/autogen/contract-deployments-keys.ts | 8 - scripts/autogen/contract-deployments.json | 90 +- subgraph/LICENSE | 72 +- subgraph/abis/v3Pool.json | 1020 +++++++++++++++++ subgraph/package.json | 1 + subgraph/src/constants/arbitrum.ts | 2 +- subgraph/src/mappings/utils/loads.ts | 8 +- subgraph/src/mappings/utils/price.ts | 3 +- subgraph/src/mappings/v3pool.ts | 36 + subgraph/subgraph.yaml | 34 +- tasks/deploy/utils/mintPosition.ts | 45 +- test/utils/setup/initialSetup.ts | 4 +- 78 files changed, 1447 insertions(+), 187 deletions(-) create mode 100644 subgraph/abis/v3Pool.json create mode 100644 subgraph/src/mappings/v3pool.ts diff --git a/contracts/LimitPool.sol b/contracts/LimitPool.sol index 88b6d8e9..8fa7e631 100644 --- a/contracts/LimitPool.sol +++ b/contracts/LimitPool.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import './interfaces/IPool.sol'; @@ -22,6 +22,44 @@ import './libraries/math/ConstantProduct.sol'; import './external/solady/LibClone.sol'; import './external/openzeppelin/security/LimitReentrancyGuard.sol'; +/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#%@@@@%@@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@%%%%%%@@%==========+++**#@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@%%##%%%@@%%%@@%%*-=====+++**#**+*#*#*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@=+===========+#@@@@@%#@%%%%%%*#%%%%%##%@@@@@@@@#****++#%#**#%=%@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@+-==+++***++++=++@@%%%%%%%%*****#%#*#@%@@@@@@@@@@@@@%#+#%#*##*=#@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@+=+*******+++%%##%%####++++++**@@%%@@@@@@@@@@@@@@@###*#%#+==@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%=**##**+#%###########+====*@%@@%%@@@@@@@@@@@@@%###%@+%=+=*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@#%%####*##*##*#**#**+=+=-=*%%%###%@@@@@@@@@@@####@#=====+*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@#%#***#***#*****=*+=+==--=@@%#**###@@@@@@@@@@@###@@%=+++=++#@@@@*@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@##*********+***##++*+#%@*%@@@@@####@@@@#=%@@@@@@@@@@@+==++=+=%%#+*@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@##*****+**+*********++=%%%@@@@@@@@@*@@@%===@@@@@@@@@*===++=+++**#%#@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@#**++++*+++++===+***+==#%%#@@@@@@@@*##@*===+%@@@@%+==+=+++++++*%@%%@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@**++++++++====----+++====+###@@@@@@@%#%%*==++*+#+===+++++++++++*%%@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%###*++++++====---=*@@@@+===-%@**%@@@@@@@#%%%===*+****+++*++++*+*+*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@*+++*+++++===--=-*@@@@@@@@=--=@@@#%@@@@@@@@#%%*++++**#+++*++**++***#*@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@%===+++++=+++===*@@@@@@@@@@=-=%@@@@@%#@@@@@@@*===*++=++*++++****+**#%+@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@*=+@@#==+==+=-=@@@%%@@@@@@@@@@@@#***%%@@@@%+===+++*+*******+**++*#%*#@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@======-==#***%#@@@@@@@@@@@##%%%%%%@#====****+*****#**###*+++#*@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-===*=++**###@@@@@@@@@@@@@+#%%%%@*======+*********###+++++++++%@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-=+#%%+*###%@@@@@@@@@@@@@@@#%#@%++=+++===**#*#****#%=====+++++++%@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@-=#****####@@@@@@@@@@@@@@@@#%@*++*#++++=+#*#####*#%%==============*@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@*=#**+*###@@@@@@@@@@@@@@@@@#%+**###**+++######%%#+-==--=-=------======#@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@++#++++*##%@@@@@@@@@@@@@@@%+*#%%######*##%#+=-=#@@@@@*----------------*@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@%+#*+++==+***#@@@@@@@@@@@%#%%####%%#*+#*++%@@@@@@@@@@@@@%+=======*@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@*+**+========------=-=%@#%%%%%%%%%%%%#%#%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@#=====-==-------=#@@@@########%%%%%%##++#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%##%%@@@@@@@@@@@@%#######%%#*+=++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#*####++++=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##**+=#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ + /** * @title Limit Pool - Constant Product * @author Poolshark diff --git a/contracts/LimitPoolFactory.sol b/contracts/LimitPoolFactory.sol index c38b1c76..dfee96fa 100644 --- a/contracts/LimitPoolFactory.sol +++ b/contracts/LimitPoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import './interfaces/range/IRangePool.sol'; @@ -14,7 +14,45 @@ import './libraries/utils/SafeCast.sol'; import './libraries/utils/PositionTokens.sol'; import './libraries/math/ConstantProduct.sol'; -/** +/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#%@@@@%@@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@%%%%%%@@%==========+++**#@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@%%##%%%@@%%%@@%%*-=====+++**#**+*#*#*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@=+===========+#@@@@@%#@%%%%%%*#%%%%%##%@@@@@@@@#****++#%#**#%=%@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@+-==+++***++++=++@@%%%%%%%%*****#%#*#@%@@@@@@@@@@@@@%#+#%#*##*=#@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@+=+*******+++%%##%%####++++++**@@%%@@@@@@@@@@@@@@@###*#%#+==@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%=**##**+#%###########+====*@%@@%%@@@@@@@@@@@@@%###%@+%=+=*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@#%%####*##*##*#**#**+=+=-=*%%%###%@@@@@@@@@@@####@#=====+*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@#%#***#***#*****=*+=+==--=@@%#**###@@@@@@@@@@@###@@%=+++=++#@@@@*@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@##*********+***##++*+#%@*%@@@@@####@@@@#=%@@@@@@@@@@@+==++=+=%%#+*@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@##*****+**+*********++=%%%@@@@@@@@@*@@@%===@@@@@@@@@*===++=+++**#%#@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@#**++++*+++++===+***+==#%%#@@@@@@@@*##@*===+%@@@@%+==+=+++++++*%@%%@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@**++++++++====----+++====+###@@@@@@@%#%%*==++*+#+===+++++++++++*%%@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%###*++++++====---=*@@@@+===-%@**%@@@@@@@#%%%===*+****+++*++++*+*+*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@*+++*+++++===--=-*@@@@@@@@=--=@@@#%@@@@@@@@#%%*++++**#+++*++**++***#*@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@%===+++++=+++===*@@@@@@@@@@=-=%@@@@@%#@@@@@@@*===*++=++*++++****+**#%+@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@*=+@@#==+==+=-=@@@%%@@@@@@@@@@@@#***%%@@@@%+===+++*+*******+**++*#%*#@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@======-==#***%#@@@@@@@@@@@##%%%%%%@#====****+*****#**###*+++#*@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-===*=++**###@@@@@@@@@@@@@+#%%%%@*======+*********###+++++++++%@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-=+#%%+*###%@@@@@@@@@@@@@@@#%#@%++=+++===**#*#****#%=====+++++++%@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@-=#****####@@@@@@@@@@@@@@@@#%@*++*#++++=+#*#####*#%%==============*@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@*=#**+*###@@@@@@@@@@@@@@@@@#%+**###**+++######%%#+-==--=-=------======#@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@++#++++*##%@@@@@@@@@@@@@@@%+*#%%######*##%#+=-=#@@@@@*----------------*@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@%+#*+++==+***#@@@@@@@@@@@%#%%####%%#*+#*++%@@@@@@@@@@@@@%+=======*@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@*+**+========------=-=%@#%%%%%%%%%%%%#%#%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@#=====-==-------=#@@@@########%%%%%%##++#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%##%%@@@@@@@@@@@@%#######%%#*+=++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#*####++++=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##**+=#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ + +/** * @notice Limit Pool Factory * @author Poolshark * @author @alphak3y diff --git a/contracts/base/events/LimitPoolEvents.sol b/contracts/base/events/LimitPoolEvents.sol index bc06bee3..35c9b72e 100644 --- a/contracts/base/events/LimitPoolEvents.sol +++ b/contracts/base/events/LimitPoolEvents.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; /// @notice Events emitted by the LimitPool contract(s) diff --git a/contracts/base/events/LimitPoolFactoryEvents.sol b/contracts/base/events/LimitPoolFactoryEvents.sol index 1ea74041..0a3981f2 100644 --- a/contracts/base/events/LimitPoolFactoryEvents.sol +++ b/contracts/base/events/LimitPoolFactoryEvents.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; /// @notice Events emitted by the LimitPoolFactory contract diff --git a/contracts/base/events/LimitPoolManagerEvents.sol b/contracts/base/events/LimitPoolManagerEvents.sol index b51e29d5..499bb0cb 100644 --- a/contracts/base/events/LimitPoolManagerEvents.sol +++ b/contracts/base/events/LimitPoolManagerEvents.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../../interfaces/structs/PoolsharkStructs.sol'; @@ -22,6 +22,13 @@ abstract contract LimitPoolManagerEvents is PoolsharkStructs { uint16 newFeeDeltaConst ); + /// @notice Event emitted the fee delta constant is modified + event PoolFeeDeltaConstChanged( + address pool, + uint16 oldFeeDeltaConst, + uint16 newFeeDeltaConst + ); + /// @notice Event emitted when a new pool type is enabled event PoolTypeEnabled( bytes32 poolTypeName, diff --git a/contracts/base/events/PoolsharkRouterEvents.sol b/contracts/base/events/PoolsharkRouterEvents.sol index 0e427db1..f2797a50 100644 --- a/contracts/base/events/PoolsharkRouterEvents.sol +++ b/contracts/base/events/PoolsharkRouterEvents.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; /// @notice Events emitted by the PoolsharkRouter contract diff --git a/contracts/base/events/PositionERC1155Events.sol b/contracts/base/events/PositionERC1155Events.sol index 131516da..eea3bdc7 100644 --- a/contracts/base/events/PositionERC1155Events.sol +++ b/contracts/base/events/PositionERC1155Events.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; /// @notice Events emitted by the PositionERC1155 contract(s) diff --git a/contracts/base/events/RangeStakerEvents.sol b/contracts/base/events/RangeStakerEvents.sol index 2fefde07..afa5dc16 100644 --- a/contracts/base/events/RangeStakerEvents.sol +++ b/contracts/base/events/RangeStakerEvents.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; /// @notice Events emitted by the RangeStaker contract diff --git a/contracts/base/storage/LimitPoolFactoryStorage.sol b/contracts/base/storage/LimitPoolFactoryStorage.sol index 7b1be853..21c4ed9d 100644 --- a/contracts/base/storage/LimitPoolFactoryStorage.sol +++ b/contracts/base/storage/LimitPoolFactoryStorage.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; abstract contract LimitPoolFactoryStorage { diff --git a/contracts/base/storage/LimitPoolImmutables.sol b/contracts/base/storage/LimitPoolImmutables.sol index 15edf156..0faacec9 100644 --- a/contracts/base/storage/LimitPoolImmutables.sol +++ b/contracts/base/storage/LimitPoolImmutables.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BSD +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import {Clone} from '../../external/solady/Clone.sol'; diff --git a/contracts/base/storage/LimitPoolStorage.sol b/contracts/base/storage/LimitPoolStorage.sol index 68de1ccf..29490120 100644 --- a/contracts/base/storage/LimitPoolStorage.sol +++ b/contracts/base/storage/LimitPoolStorage.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../interfaces/structs/RangePoolStructs.sol'; diff --git a/contracts/base/storage/PositionERC1155Immutables.sol b/contracts/base/storage/PositionERC1155Immutables.sol index ede4b10a..48bec754 100644 --- a/contracts/base/storage/PositionERC1155Immutables.sol +++ b/contracts/base/storage/PositionERC1155Immutables.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: BSD +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import {Clone} from '../../external/solady/Clone.sol'; diff --git a/contracts/interfaces/IERC20Minimal.sol b/contracts/interfaces/IERC20Minimal.sol index 377f86dd..e83f0a35 100644 --- a/contracts/interfaces/IERC20Minimal.sol +++ b/contracts/interfaces/IERC20Minimal.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; interface IERC20Minimal { diff --git a/contracts/interfaces/IPool.sol b/contracts/interfaces/IPool.sol index fa52cde0..8549b94c 100644 --- a/contracts/interfaces/IPool.sol +++ b/contracts/interfaces/IPool.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/IPositionERC1155.sol b/contracts/interfaces/IPositionERC1155.sol index a80ca6a9..8d3f216c 100644 --- a/contracts/interfaces/IPositionERC1155.sol +++ b/contracts/interfaces/IPositionERC1155.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; diff --git a/contracts/interfaces/IWETH9.sol b/contracts/interfaces/IWETH9.sol index 37ff5f09..accc740e 100644 --- a/contracts/interfaces/IWETH9.sol +++ b/contracts/interfaces/IWETH9.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; interface IWETH9 { diff --git a/contracts/interfaces/callbacks/ICoverPoolCallback.sol b/contracts/interfaces/callbacks/ICoverPoolCallback.sol index 621ca2f2..e2cb33a5 100644 --- a/contracts/interfaces/callbacks/ICoverPoolCallback.sol +++ b/contracts/interfaces/callbacks/ICoverPoolCallback.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; /// @title Callback for mints diff --git a/contracts/interfaces/callbacks/ILimitPoolCallback.sol b/contracts/interfaces/callbacks/ILimitPoolCallback.sol index 56422faf..5e5452a2 100644 --- a/contracts/interfaces/callbacks/ILimitPoolCallback.sol +++ b/contracts/interfaces/callbacks/ILimitPoolCallback.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; /// @title Callback for range mints diff --git a/contracts/interfaces/cover/ICoverPool.sol b/contracts/interfaces/cover/ICoverPool.sol index 05d26a30..0aaecd74 100644 --- a/contracts/interfaces/cover/ICoverPool.sol +++ b/contracts/interfaces/cover/ICoverPool.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity ^0.8.18; import '../structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/cover/ICoverPoolFactory.sol b/contracts/interfaces/cover/ICoverPoolFactory.sol index 87cf2186..9e7e613b 100644 --- a/contracts/interfaces/cover/ICoverPoolFactory.sol +++ b/contracts/interfaces/cover/ICoverPoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; abstract contract ICoverPoolFactory { diff --git a/contracts/interfaces/cover/ITwapSource.sol b/contracts/interfaces/cover/ITwapSource.sol index 4c3e7097..c76ac188 100644 --- a/contracts/interfaces/cover/ITwapSource.sol +++ b/contracts/interfaces/cover/ITwapSource.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity ^0.8.18; import '../structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPool.sol b/contracts/interfaces/limit/ILimitPool.sol index 8b16147c..8a22f554 100644 --- a/contracts/interfaces/limit/ILimitPool.sol +++ b/contracts/interfaces/limit/ILimitPool.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPoolFactory.sol b/contracts/interfaces/limit/ILimitPoolFactory.sol index 93f4c991..fd07669e 100644 --- a/contracts/interfaces/limit/ILimitPoolFactory.sol +++ b/contracts/interfaces/limit/ILimitPoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPoolManager.sol b/contracts/interfaces/limit/ILimitPoolManager.sol index ecc159f4..a35f7f7d 100644 --- a/contracts/interfaces/limit/ILimitPoolManager.sol +++ b/contracts/interfaces/limit/ILimitPoolManager.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; /// @notice LimitPoolManager interface @@ -7,7 +7,7 @@ interface ILimitPoolManager { function feeTo() external view returns (address); - function feeDeltaConst() external view returns (uint16); + function feeDeltaConsts(address pool) external view returns (uint16); function poolTypes(uint16 poolType) external diff --git a/contracts/interfaces/limit/ILimitPoolStorageView.sol b/contracts/interfaces/limit/ILimitPoolStorageView.sol index fd6f5250..ee38f56a 100644 --- a/contracts/interfaces/limit/ILimitPoolStorageView.sol +++ b/contracts/interfaces/limit/ILimitPoolStorageView.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPoolView.sol b/contracts/interfaces/limit/ILimitPoolView.sol index 262730ba..985d049c 100644 --- a/contracts/interfaces/limit/ILimitPoolView.sol +++ b/contracts/interfaces/limit/ILimitPoolView.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; diff --git a/contracts/interfaces/range/IRangePool.sol b/contracts/interfaces/range/IRangePool.sol index 931b8ee7..63abe328 100644 --- a/contracts/interfaces/range/IRangePool.sol +++ b/contracts/interfaces/range/IRangePool.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../structs/RangePoolStructs.sol'; diff --git a/contracts/interfaces/range/IRangePoolFactory.sol b/contracts/interfaces/range/IRangePoolFactory.sol index 8e26a4ba..e3e9f669 100644 --- a/contracts/interfaces/range/IRangePoolFactory.sol +++ b/contracts/interfaces/range/IRangePoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; interface IRangePoolFactory { diff --git a/contracts/interfaces/range/IRangePoolManager.sol b/contracts/interfaces/range/IRangePoolManager.sol index b5542828..c914972f 100644 --- a/contracts/interfaces/range/IRangePoolManager.sol +++ b/contracts/interfaces/range/IRangePoolManager.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../structs/RangePoolStructs.sol'; diff --git a/contracts/interfaces/staking/IRangeStaker.sol b/contracts/interfaces/staking/IRangeStaker.sol index 2bbc13dd..c310dff6 100644 --- a/contracts/interfaces/staking/IRangeStaker.sol +++ b/contracts/interfaces/staking/IRangeStaker.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; diff --git a/contracts/interfaces/structs/LimitPoolStructs.sol b/contracts/interfaces/structs/LimitPoolStructs.sol index 48a720bb..f48099f2 100644 --- a/contracts/interfaces/structs/LimitPoolStructs.sol +++ b/contracts/interfaces/structs/LimitPoolStructs.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import './PoolsharkStructs.sol'; diff --git a/contracts/interfaces/structs/PoolsharkStructs.sol b/contracts/interfaces/structs/PoolsharkStructs.sol index 7332f984..1ff35ef9 100644 --- a/contracts/interfaces/structs/PoolsharkStructs.sol +++ b/contracts/interfaces/structs/PoolsharkStructs.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../cover/ITwapSource.sol'; @@ -87,6 +87,13 @@ interface PoolsharkStructs { * @notice id of previous position minted */ uint32 positionId; + /** + * @custom:field burnPercent + * @notice Percent of the remaining liquidity to be removed + * @notice 1e38 represents 100% + * @notice 5e37 represents 50% + * @notice 1e37 represents 10% + */ uint128 burnPercent; } diff --git a/contracts/interfaces/structs/RangePoolStructs.sol b/contracts/interfaces/structs/RangePoolStructs.sol index 02610636..d6dc90ac 100644 --- a/contracts/interfaces/structs/RangePoolStructs.sol +++ b/contracts/interfaces/structs/RangePoolStructs.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import './PoolsharkStructs.sol'; diff --git a/contracts/libraries/Samples.sol b/contracts/libraries/Samples.sol index d5754b03..14d02d10 100644 --- a/contracts/libraries/Samples.sol +++ b/contracts/libraries/Samples.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import './math/ConstantProduct.sol'; diff --git a/contracts/libraries/TickMap.sol b/contracts/libraries/TickMap.sol index 8a7b86a5..7ff890bd 100644 --- a/contracts/libraries/TickMap.sol +++ b/contracts/libraries/TickMap.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import './math/ConstantProduct.sol'; diff --git a/contracts/libraries/Ticks.sol b/contracts/libraries/Ticks.sol index c48c0347..3d770059 100644 --- a/contracts/libraries/Ticks.sol +++ b/contracts/libraries/Ticks.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/libraries/limit/Claims.sol b/contracts/libraries/limit/Claims.sol index 02f88dc5..461376ae 100644 --- a/contracts/libraries/limit/Claims.sol +++ b/contracts/libraries/limit/Claims.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/limit/EpochMap.sol b/contracts/libraries/limit/EpochMap.sol index 0fae9697..5c31c69b 100644 --- a/contracts/libraries/limit/EpochMap.sol +++ b/contracts/libraries/limit/EpochMap.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../math/ConstantProduct.sol'; diff --git a/contracts/libraries/limit/LimitPositions.sol b/contracts/libraries/limit/LimitPositions.sol index b2dd175d..18a47a69 100644 --- a/contracts/libraries/limit/LimitPositions.sol +++ b/contracts/libraries/limit/LimitPositions.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import './LimitTicks.sol'; diff --git a/contracts/libraries/limit/LimitTicks.sol b/contracts/libraries/limit/LimitTicks.sol index 4948f1bf..1b135450 100644 --- a/contracts/libraries/limit/LimitTicks.sol +++ b/contracts/libraries/limit/LimitTicks.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/limit/pool/BurnLimitCall.sol b/contracts/libraries/limit/pool/BurnLimitCall.sol index 70617e92..03fda008 100644 --- a/contracts/libraries/limit/pool/BurnLimitCall.sol +++ b/contracts/libraries/limit/pool/BurnLimitCall.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/limit/pool/MintLimitCall.sol b/contracts/libraries/limit/pool/MintLimitCall.sol index 1bf2e762..fe7138b5 100644 --- a/contracts/libraries/limit/pool/MintLimitCall.sol +++ b/contracts/libraries/limit/pool/MintLimitCall.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/limit/pool/SnapshotLimitCall.sol b/contracts/libraries/limit/pool/SnapshotLimitCall.sol index 74dc48e7..a012640c 100644 --- a/contracts/libraries/limit/pool/SnapshotLimitCall.sol +++ b/contracts/libraries/limit/pool/SnapshotLimitCall.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/math/ConstantProduct.sol b/contracts/libraries/math/ConstantProduct.sol index fd386073..1fe52d52 100644 --- a/contracts/libraries/math/ConstantProduct.sol +++ b/contracts/libraries/math/ConstantProduct.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import './OverflowMath.sol'; diff --git a/contracts/libraries/math/OverflowMath.sol b/contracts/libraries/math/OverflowMath.sol index 567a6457..36872721 100644 --- a/contracts/libraries/math/OverflowMath.sol +++ b/contracts/libraries/math/OverflowMath.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; /// @notice Math library that facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision. diff --git a/contracts/libraries/pool/FeesCall.sol b/contracts/libraries/pool/FeesCall.sol index 3eb312b1..1e0039c9 100644 --- a/contracts/libraries/pool/FeesCall.sol +++ b/contracts/libraries/pool/FeesCall.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../interfaces/IPositionERC1155.sol'; diff --git a/contracts/libraries/pool/QuoteCall.sol b/contracts/libraries/pool/QuoteCall.sol index 3c54fad5..b3137fb4 100644 --- a/contracts/libraries/pool/QuoteCall.sol +++ b/contracts/libraries/pool/QuoteCall.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/pool/SampleCall.sol b/contracts/libraries/pool/SampleCall.sol index ea4b4d1a..f09144f5 100644 --- a/contracts/libraries/pool/SampleCall.sol +++ b/contracts/libraries/pool/SampleCall.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../interfaces/structs/RangePoolStructs.sol'; diff --git a/contracts/libraries/pool/SwapCall.sol b/contracts/libraries/pool/SwapCall.sol index dd63674f..1ce01381 100644 --- a/contracts/libraries/pool/SwapCall.sol +++ b/contracts/libraries/pool/SwapCall.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/range/RangePositions.sol b/contracts/libraries/range/RangePositions.sol index 4a324bfb..db67abec 100644 --- a/contracts/libraries/range/RangePositions.sol +++ b/contracts/libraries/range/RangePositions.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../interfaces/IPool.sol'; diff --git a/contracts/libraries/range/RangeTicks.sol b/contracts/libraries/range/RangeTicks.sol index 8f97fcf6..d2b676b2 100644 --- a/contracts/libraries/range/RangeTicks.sol +++ b/contracts/libraries/range/RangeTicks.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/libraries/range/math/FeeMath.sol b/contracts/libraries/range/math/FeeMath.sol index a6f46024..138ec96c 100644 --- a/contracts/libraries/range/math/FeeMath.sol +++ b/contracts/libraries/range/math/FeeMath.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../Samples.sol'; @@ -48,7 +48,7 @@ library FeeMath { locals.lastPrice = locals.minPrice; // delta is % modifier on the swapFee uint256 delta = OverflowMath.mulDiv( - ILimitPoolManager(cache.constants.owner).feeDeltaConst() / // higher feeDeltaConst means + ILimitPoolManager(cache.constants.owner).feeDeltaConsts(address(this)) / // higher feeDeltaConst means uint16(cache.constants.tickSpacing), // more aggressive dynamic fee ( locals.price > locals.lastPrice diff --git a/contracts/libraries/range/pool/BurnRangeCall.sol b/contracts/libraries/range/pool/BurnRangeCall.sol index 1e2b2656..f2e4f5e4 100644 --- a/contracts/libraries/range/pool/BurnRangeCall.sol +++ b/contracts/libraries/range/pool/BurnRangeCall.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../../interfaces/structs/RangePoolStructs.sol'; diff --git a/contracts/libraries/range/pool/MintRangeCall.sol b/contracts/libraries/range/pool/MintRangeCall.sol index d10f85c4..50e33983 100644 --- a/contracts/libraries/range/pool/MintRangeCall.sol +++ b/contracts/libraries/range/pool/MintRangeCall.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../../interfaces/structs/RangePoolStructs.sol'; diff --git a/contracts/libraries/range/pool/SnapshotRangeCall.sol b/contracts/libraries/range/pool/SnapshotRangeCall.sol index 701cd74c..1a050455 100644 --- a/contracts/libraries/range/pool/SnapshotRangeCall.sol +++ b/contracts/libraries/range/pool/SnapshotRangeCall.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/utils/Bytes.sol b/contracts/libraries/utils/Bytes.sol index 84bc6e5d..833a8288 100644 --- a/contracts/libraries/utils/Bytes.sol +++ b/contracts/libraries/utils/Bytes.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; library Bytes { diff --git a/contracts/libraries/utils/Collect.sol b/contracts/libraries/utils/Collect.sol index 225f00b8..ba40406a 100644 --- a/contracts/libraries/utils/Collect.sol +++ b/contracts/libraries/utils/Collect.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/utils/PositionTokens.sol b/contracts/libraries/utils/PositionTokens.sol index ba9d0660..827539d3 100644 --- a/contracts/libraries/utils/PositionTokens.sol +++ b/contracts/libraries/utils/PositionTokens.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import './Bytes.sol'; diff --git a/contracts/libraries/utils/SafeCast.sol b/contracts/libraries/utils/SafeCast.sol index d7655b2d..80924390 100644 --- a/contracts/libraries/utils/SafeCast.sol +++ b/contracts/libraries/utils/SafeCast.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; /// @title Safe casting methods diff --git a/contracts/libraries/utils/SafeTransfers.sol b/contracts/libraries/utils/SafeTransfers.sol index a8fef9e5..d638b6f9 100644 --- a/contracts/libraries/utils/SafeTransfers.sol +++ b/contracts/libraries/utils/SafeTransfers.sol @@ -1,4 +1,4 @@ -//SPDX-License-Identifier: Unlicense +//SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; diff --git a/contracts/libraries/utils/String.sol b/contracts/libraries/utils/String.sol index 0cc44aea..342f4f00 100644 --- a/contracts/libraries/utils/String.sol +++ b/contracts/libraries/utils/String.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; library String { diff --git a/contracts/staking/RangeStaker.sol b/contracts/staking/RangeStaker.sol index b965fdee..bbb66e07 100644 --- a/contracts/staking/RangeStaker.sol +++ b/contracts/staking/RangeStaker.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../interfaces/IPool.sol'; diff --git a/contracts/test/WETH9.sol b/contracts/test/WETH9.sol index 6be3cdb0..44867708 100644 --- a/contracts/test/WETH9.sol +++ b/contracts/test/WETH9.sol @@ -1,4 +1,4 @@ -//SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; contract WETH9 { diff --git a/contracts/utils/LimitPoolManager.sol b/contracts/utils/LimitPoolManager.sol index adb19b7e..3fc2c897 100644 --- a/contracts/utils/LimitPoolManager.sol +++ b/contracts/utils/LimitPoolManager.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../interfaces/IPool.sol'; @@ -8,6 +8,44 @@ import '../interfaces/limit/ILimitPoolManager.sol'; import '../base/events/LimitPoolManagerEvents.sol'; import '../libraries/utils/SafeCast.sol'; +/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#%@@@@%@@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@%%%%%%@@%==========+++**#@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@%%##%%%@@%%%@@%%*-=====+++**#**+*#*#*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@=+===========+#@@@@@%#@%%%%%%*#%%%%%##%@@@@@@@@#****++#%#**#%=%@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@+-==+++***++++=++@@%%%%%%%%*****#%#*#@%@@@@@@@@@@@@@%#+#%#*##*=#@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@+=+*******+++%%##%%####++++++**@@%%@@@@@@@@@@@@@@@###*#%#+==@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%=**##**+#%###########+====*@%@@%%@@@@@@@@@@@@@%###%@+%=+=*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@#%%####*##*##*#**#**+=+=-=*%%%###%@@@@@@@@@@@####@#=====+*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@#%#***#***#*****=*+=+==--=@@%#**###@@@@@@@@@@@###@@%=+++=++#@@@@*@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@##*********+***##++*+#%@*%@@@@@####@@@@#=%@@@@@@@@@@@+==++=+=%%#+*@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@##*****+**+*********++=%%%@@@@@@@@@*@@@%===@@@@@@@@@*===++=+++**#%#@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@#**++++*+++++===+***+==#%%#@@@@@@@@*##@*===+%@@@@%+==+=+++++++*%@%%@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@**++++++++====----+++====+###@@@@@@@%#%%*==++*+#+===+++++++++++*%%@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%###*++++++====---=*@@@@+===-%@**%@@@@@@@#%%%===*+****+++*++++*+*+*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@*+++*+++++===--=-*@@@@@@@@=--=@@@#%@@@@@@@@#%%*++++**#+++*++**++***#*@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@%===+++++=+++===*@@@@@@@@@@=-=%@@@@@%#@@@@@@@*===*++=++*++++****+**#%+@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@*=+@@#==+==+=-=@@@%%@@@@@@@@@@@@#***%%@@@@%+===+++*+*******+**++*#%*#@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@======-==#***%#@@@@@@@@@@@##%%%%%%@#====****+*****#**###*+++#*@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-===*=++**###@@@@@@@@@@@@@+#%%%%@*======+*********###+++++++++%@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-=+#%%+*###%@@@@@@@@@@@@@@@#%#@%++=+++===**#*#****#%=====+++++++%@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@-=#****####@@@@@@@@@@@@@@@@#%@*++*#++++=+#*#####*#%%==============*@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@*=#**+*###@@@@@@@@@@@@@@@@@#%+**###**+++######%%#+-==--=-=------======#@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@++#++++*##%@@@@@@@@@@@@@@@%+*#%%######*##%#+=-=#@@@@@*----------------*@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@%+#*+++==+***#@@@@@@@@@@@%#%%####%%#*+#*++%@@@@@@@@@@@@@%+=======*@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@*+**+========------=-=%@#%%%%%%%%%%%%#%#%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@#=====-==-------=#@@@@########%%%%%%##++#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%##%%@@@@@@@@@@@@%#######%%#*+=++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#*####++++=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##**+=#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ + /** * @title LimitPoolManager * @notice The manager for all limit pools @@ -20,6 +58,7 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { address public factory; // fee delta const for dynamic fees uint16 public feeDeltaConst; + mapping (address => uint16) poolFeeDeltaConsts; // max protocol fees uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; /// @dev - max protocol swap fee of 100% uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; /// @dev - max protocol fill fee of 1% @@ -145,11 +184,16 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { factory = factory_; } - function setFeeDeltaConst(uint16 feeDeltaConst_) external onlyOwner { + function setFeeDeltaConst(address pool, uint16 feeDeltaConst_) external onlyOwner { if (feeDeltaConst_ > 10000) require(false, 'FeeDeltaConstCeilingExceeded()'); - emit FeeDeltaConstChanged(feeDeltaConst, feeDeltaConst_); - feeDeltaConst = feeDeltaConst_; + if (pool == address(0)) { + emit FeeDeltaConstChanged(feeDeltaConst, feeDeltaConst_); + feeDeltaConst = feeDeltaConst_; + } else { + emit PoolFeeDeltaConstChanged(pool, poolFeeDeltaConsts[pool], feeDeltaConst_); + poolFeeDeltaConsts[pool] = feeDeltaConst_; + } } function collectProtocolFees(address[] calldata pools) @@ -244,6 +288,16 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { ); } + function feeDeltaConsts(address pool) external view returns (uint16) { + uint16 poolFeeDeltaConst = poolFeeDeltaConsts[pool]; + if (poolFeeDeltaConst != 0) { + // use custom value if set + return poolFeeDeltaConst; + } + // else use default value + return feeDeltaConst; + } + function poolTypes(uint16 poolTypeId) external view diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index 80b740ab..4b4244a1 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; import '../interfaces/IPool.sol'; @@ -17,6 +17,12 @@ import '../libraries/utils/SafeCast.sol'; import '../interfaces/structs/PoolsharkStructs.sol'; import '../external/solady/LibClone.sol'; +/** + * @title LimitPoolManager + * @notice The router for all limit and cover pools + * @author Poolshark + * @author @alphak3y + */ contract PoolsharkRouter is PoolsharkStructs, ILimitPoolMintRangeCallback, @@ -771,11 +777,12 @@ contract PoolsharkRouter is tgePoolState, ,,,,, ) = IPool(tgePool).globalState(); - if (tgePoolState.price < 2358285847295149069702956253974) { + uint160 expectedPoolPrice = 2062122576071194503151227620392; + if (tgePoolState.price < expectedPoolPrice) { // move pool price up if below SwapParams memory swapParams = SwapParams({ to: msg.sender, - priceLimit: 2358285847295149069702956253974, + priceLimit: expectedPoolPrice, amount: 1000e18, exactIn: true, zeroForOne: false, @@ -788,11 +795,11 @@ contract PoolsharkRouter is ) }); IPool(tgePool).swap(swapParams); - } else if (tgePoolState.price < 2358285847295149069702956253974) { + } else if (tgePoolState.price < expectedPoolPrice) { // move pool price down if above SwapParams memory swapParams = SwapParams({ to: msg.sender, - priceLimit: 2358285847295149069702956253974, + priceLimit: expectedPoolPrice, amount: 1e18, exactIn: true, zeroForOne: true, @@ -811,7 +818,7 @@ contract PoolsharkRouter is tgePoolState, ,,,,, ) = IPool(tgePool).globalState(); - if(tgePoolState.price != 2358285847295149069702956253974) + if(tgePoolState.price != expectedPoolPrice) require(false, 'PoolPriceMismatch()'); MintRangeCallbackData memory callbackData = MintRangeCallbackData({ sender: msg.sender, @@ -820,11 +827,11 @@ contract PoolsharkRouter is }); MintRangeParams memory mintRangeParams = MintRangeParams({ to: staker, - lower: 44850, + lower: 44850, //TODO: change to $10 per FIN = 221.5 = ~54000 upper: 77040, positionId: 0, - amount0: 31568903742987611804, - amount1: 51999999999999999999996, + amount0: 44146000000000000000, //TODO: calculate new amounts + amount1: 42586954683848930000000, //TODO: calculate new amounts callbackData: abi.encode(callbackData) }); IRangePool(tgePool).mintRange(mintRangeParams); diff --git a/contracts/utils/PositionERC1155.sol b/contracts/utils/PositionERC1155.sol index 5531cd26..89e968e0 100644 --- a/contracts/utils/PositionERC1155.sol +++ b/contracts/utils/PositionERC1155.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: AGPL-3.0-only pragma solidity 0.8.18; diff --git a/scripts/autogen/contract-deployments-keys.ts b/scripts/autogen/contract-deployments-keys.ts index d1ee60e6..952c1462 100644 --- a/scripts/autogen/contract-deployments-keys.ts +++ b/scripts/autogen/contract-deployments-keys.ts @@ -5,12 +5,4 @@ export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ networkName: 'arb_one', objectName: 'poolRouter' }, - { - networkName: 'arb_one', - objectName: 'poolRouter' - }, - { - networkName: 'arb_one', - objectName: 'poolRouter' - }, ]; \ No newline at end of file diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index fd98a3c4..c100a057 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -56,144 +56,144 @@ }, "tickMapLib": { "contractName": "TickMap", - "contractAddress": "0x51c65a3d7fb6304206b64888daf95285374f3a96", + "contractAddress": "0x585006e3f0c9f7630d93c32d723c14f42464c5c2", "constructorArguments": [], - "created": "2023-12-10T22:48:47.380Z" + "created": "2023-12-29T21:39:34.704Z" }, "ticksLib": { "contractName": "Ticks", - "contractAddress": "0xac424407a8bbaa42a781eb94f83ae37231594a4a", + "contractAddress": "0xa294993ec6f2e76891ea25610edfd39555dfbaf9", "constructorArguments": [], - "created": "2023-12-10T22:48:53.420Z" + "created": "2023-12-29T21:39:36.032Z" }, "limitPositionsLib": { "contractName": "LimitPositions", - "contractAddress": "0xf04bf4e3e8157ba5b91bfda16e21be770e7ac790", + "contractAddress": "0x5288b2873578d01244228f5219f068c7a8438cf6", "constructorArguments": [], - "created": "2023-12-10T22:48:54.635Z" + "created": "2023-12-29T21:39:37.228Z" }, "limitPoolManager": { "contractName": "LimitPoolManager", - "contractAddress": "0x441bb7b76c9932d5c65d7a78b3413978d7370761", + "contractAddress": "0x6b1ffc44ec8ed486843640b66180d5ee366fee9f", "constructorArguments": [], - "created": "2023-12-10T22:49:00.148Z" + "created": "2023-12-29T21:39:38.177Z" }, "limitPoolFactory": { "contractName": "LimitPoolFactory", - "contractAddress": "0xd28d620853af6837d76f1360dc65229d57ba5435", + "contractAddress": "0x40e21ce13f5089bc421c063506bbb0df02bca07e", "constructorArguments": [ - "0x441Bb7B76c9932d5c65D7A78B3413978d7370761" + "0x6b1ffC44ec8eD486843640B66180d5eE366Fee9f" ], - "created": "2023-12-10T22:49:01.378Z" + "created": "2023-12-29T21:39:39.294Z" }, "swapCall": { "contractName": "SwapCall", - "contractAddress": "0x14ce728bf96d5ec0976f332605fe80a42ec0b244", + "contractAddress": "0x010c25d8aac4f529c65182959f38897a57e784a8", "constructorArguments": [], - "created": "2023-12-10T22:49:06.947Z" + "created": "2023-12-29T21:39:40.293Z" }, "mintRangeCall": { "contractName": "MintRangeCall", - "contractAddress": "0x83eea57c3536a7ec64ead2c67d5b6bfeb6593720", + "contractAddress": "0xc2c8a6c8f95f6a587f2aeabfb702297ef910227d", "constructorArguments": [], - "created": "2023-12-10T22:49:12.892Z" + "created": "2023-12-29T21:39:41.426Z" }, "burnRangeCall": { "contractName": "BurnRangeCall", - "contractAddress": "0xc66eebf930bdcbe450fbdd56deff8caea9e9943f", + "contractAddress": "0xf4bbf76323b4ce1d2aa6f495ad688b4f57439e25", "constructorArguments": [], - "created": "2023-12-10T22:49:18.718Z" + "created": "2023-12-29T21:39:42.507Z" }, "mintLimitCall": { "contractName": "MintLimitCall", - "contractAddress": "0x4255e7a88bae87d2a5ff3ba317c6d512562f7078", + "contractAddress": "0xa5b68a50e74abbe7da5e6e2660c480f26caf2d74", "constructorArguments": [], - "created": "2023-12-10T22:49:24.731Z" + "created": "2023-12-29T21:39:43.549Z" }, "burnLimitCall": { "contractName": "BurnLimitCall", - "contractAddress": "0xe373e46f8900a17bb8e3880d00791ac06d975c0a", + "contractAddress": "0xc12d5c3a00a4d1d7d294be93485251de990f7e23", "constructorArguments": [], - "created": "2023-12-10T22:49:30.609Z" + "created": "2023-12-29T21:39:44.549Z" }, "snapshotLimitCall": { "contractName": "SnapshotLimitCall", - "contractAddress": "0x898b8e21901cfda9e3524f39339cd7700f711b6f", + "contractAddress": "0x8c0b0083edd1f7a4026bd55d3dbd674c08eedf39", "constructorArguments": [], - "created": "2023-12-10T22:49:36.596Z" + "created": "2023-12-29T21:39:45.575Z" }, "quoteCall": { "contractName": "QuoteCall", - "contractAddress": "0xfaf9ea467dcb1581c66939a2781bbbbae0b2e615", + "contractAddress": "0xbcb404012d1deeda64ada405421638d2a6caa329", "constructorArguments": [], - "created": "2023-12-10T22:49:42.393Z" + "created": "2023-12-29T21:39:46.571Z" }, "feesCall": { "contractName": "FeesCall", - "contractAddress": "0x1a16415867fde435d3f8a6631ef308ae7bf96b3b", + "contractAddress": "0x88366b13c8d6b9a5dc20e179861753551a894462", "constructorArguments": [], - "created": "2023-12-10T22:49:43.270Z" + "created": "2023-12-29T21:39:47.550Z" }, "sampleCall": { "contractName": "SampleCall", - "contractAddress": "0xfa20280428016b781a294ac88eee3a8ead580039", + "contractAddress": "0xc84c636c328224ecbc77fb76c37bc0cab2603f49", "constructorArguments": [], - "created": "2023-12-10T22:49:44.564Z" + "created": "2023-12-29T21:39:48.553Z" }, "snapshotRangeCall": { "contractName": "SnapshotRangeCall", - "contractAddress": "0xa801ca07b20ef56251739d9b3197bfd02cfa20e8", + "contractAddress": "0x4b64c1b178f77834a25375a9d908c0d8e2279f1f", "constructorArguments": [], - "created": "2023-12-10T22:49:50.041Z" + "created": "2023-12-29T21:39:49.578Z" }, "limitPoolImpl": { "contractName": "LimitPool", - "contractAddress": "0x8212ce17d271ed2378cc59bd234b670db7ad301e", + "contractAddress": "0x23105bead9cc26a01946f4b8b3477a3876476640", "constructorArguments": [ - "0xD28d620853aF6837D76F1360Dc65229D57bA5435" + "0x40E21Ce13f5089Bc421c063506bbB0dF02BcA07E" ], - "created": "2023-12-10T22:49:51.346Z" + "created": "2023-12-29T21:39:50.897Z" }, "positionERC1155": { "contractName": "PositionERC1155", - "contractAddress": "0xd987de01302ff6a7acc20f1ac2d2239317169df5", + "contractAddress": "0x6c4e29e567c7fbc3aa6710fa58b8c5a60309e632", "constructorArguments": [ - "0xD28d620853aF6837D76F1360Dc65229D57bA5435" + "0x40E21Ce13f5089Bc421c063506bbB0dF02BcA07E" ], - "created": "2023-12-10T22:49:56.886Z" + "created": "2023-12-29T21:39:52.094Z" }, "limitPool": { "contractName": "LimitPool", - "contractAddress": "0x5330588b4efd339b8ea441afedf09aab851f2d48", + "contractAddress": "0xe80dc9a69853483c745f8e32162f0bd5813cb291", "constructorArguments": [ "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", "1000", 0 ], - "created": "2023-12-10T22:50:00.921Z" + "created": "2023-12-29T21:39:56.337Z" }, "poolRouter": { "contractName": "PoolsharkRouter", - "contractAddress": "0x122372809577c99513f17a4555b3856fd2677dfa", + "contractAddress": "0x5357308a5a4db6c65fa6c4b122dfc3d749f2dd6a", "constructorArguments": [ - "0xd28d620853af6837d76f1360dc65229d57ba5435", + "0x40e21ce13f5089bc421c063506bbb0df02bca07e", "0x0000000000000000000000000000000000000000", "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" ], - "created": "2023-12-20T17:05:56.282Z" + "created": "2024-01-05T14:07:58.154Z" }, "rangeStaker": { "contractName": "RangeStaker", - "contractAddress": "0x373bf43249bed0518119d29b80c7d9c68d98cfc2", + "contractAddress": "0x7c6a8fffaedc60cb6709bd911c577619d32664e0", "constructorArguments": [ { - "limitPoolFactory": "0xd28d620853af6837d76f1360dc65229d57ba5435", + "limitPoolFactory": "0x40e21ce13f5089bc421c063506bbb0df02bca07e", "startTime": 0, "endTime": 2000707154 } ], - "created": "2023-12-10T22:51:45.136Z" + "created": "2023-12-29T21:41:33.692Z" } }, "arb_goerli": { diff --git a/subgraph/LICENSE b/subgraph/LICENSE index 8aeaeeee..46c3fc0e 100644 --- a/subgraph/LICENSE +++ b/subgraph/LICENSE @@ -1,21 +1,51 @@ -MIT License - -Copyright (c) 2022 Poolshark - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +Business Source License 1.1 + +License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. "Business Source License" is a trademark of MariaDB Corporation Ab. + +Parameters + +Licensor: Poolshark Labs Ltd + +Licensed Work: Poolshark Limit The Licensed Work is (C) 2023 Poolshark Labs Ltd + +Additional Use Grant: Any uses listed and defined at limit-license-grants.pshark.eth + +Change Date: The earlier of 2025-12-11 or a date specified at limit-license-date.pshark.eth + +Change License: GNU Affero General Public License v3.0 only + +Terms + +The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use. + +Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the terms of the Change License, and the rights granted in the paragraph above terminate. + +If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work. + +All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor. + +You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from a third party, the terms and conditions set forth in this License apply to your use of that work. + +Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work. + +This License does not grant you any right in any trademark or logo of Licensor or its affiliates (provided that you may use a trademark or logo of Licensor as expressly required by this License). + +TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE. + +MariaDB hereby grants you permission to use this License’s text to license your works, and to refer to it using the trademark "Business Source License", as long as you comply with the Covenants of Licensor below. + +Covenants of Licensor + +In consideration of the right to use this License’s text and the "Business Source License" name and trademark, Licensor covenants to MariaDB, and to all other recipients of the licensed work to be provided by Licensor: + +To specify as the Change License the GPL Version 3.0 or any later version, or a license that is compatible with GPL Version 3.0 or a later version, where "compatible" means that software provided under the Change License can be included in a program with software provided under GPL Version 3.0 or a later version. Licensor may specify additional Change Licenses without limitation. + +To either: (a) specify an additional grant of rights to use that does not impose any additional restriction on the right granted in this License, as the Additional Use Grant; or (b) insert the text "None". + +To specify a Change Date. + +Not to modify this License in any other way. + +Notice + +The Business Source License (this document, or the "License") is not an Open Source license. However, the Licensed Work will eventually be made available under an Open Source License, as stated in this License. \ No newline at end of file diff --git a/subgraph/abis/v3Pool.json b/subgraph/abis/v3Pool.json new file mode 100644 index 00000000..f245f073 --- /dev/null +++ b/subgraph/abis/v3Pool.json @@ -0,0 +1,1020 @@ +[ + { + "inputs":[ + + ], + "stateMutability":"nonpayable", + "type":"constructor" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":true, + "internalType":"address", + "name":"owner", + "type":"address" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount", + "type":"uint128" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + } + ], + "name":"Burn", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":true, + "internalType":"address", + "name":"owner", + "type":"address" + }, + { + "indexed":false, + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount0", + "type":"uint128" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount1", + "type":"uint128" + } + ], + "name":"Collect", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":true, + "internalType":"address", + "name":"sender", + "type":"address" + }, + { + "indexed":true, + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount0", + "type":"uint128" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount1", + "type":"uint128" + } + ], + "name":"CollectProtocol", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":true, + "internalType":"address", + "name":"sender", + "type":"address" + }, + { + "indexed":true, + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"paid0", + "type":"uint256" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"paid1", + "type":"uint256" + } + ], + "name":"Flash", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":false, + "internalType":"uint16", + "name":"observationCardinalityNextOld", + "type":"uint16" + }, + { + "indexed":false, + "internalType":"uint16", + "name":"observationCardinalityNextNew", + "type":"uint16" + } + ], + "name":"IncreaseObservationCardinalityNext", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":false, + "internalType":"uint160", + "name":"sqrtPriceX96", + "type":"uint160" + }, + { + "indexed":false, + "internalType":"int24", + "name":"tick", + "type":"int24" + } + ], + "name":"Initialize", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":false, + "internalType":"address", + "name":"sender", + "type":"address" + }, + { + "indexed":true, + "internalType":"address", + "name":"owner", + "type":"address" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount", + "type":"uint128" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + } + ], + "name":"Mint", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":false, + "internalType":"uint8", + "name":"feeProtocol0Old", + "type":"uint8" + }, + { + "indexed":false, + "internalType":"uint8", + "name":"feeProtocol1Old", + "type":"uint8" + }, + { + "indexed":false, + "internalType":"uint8", + "name":"feeProtocol0New", + "type":"uint8" + }, + { + "indexed":false, + "internalType":"uint8", + "name":"feeProtocol1New", + "type":"uint8" + } + ], + "name":"SetFeeProtocol", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":true, + "internalType":"address", + "name":"sender", + "type":"address" + }, + { + "indexed":true, + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "indexed":false, + "internalType":"int256", + "name":"amount0", + "type":"int256" + }, + { + "indexed":false, + "internalType":"int256", + "name":"amount1", + "type":"int256" + }, + { + "indexed":false, + "internalType":"uint160", + "name":"sqrtPriceX96", + "type":"uint160" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"liquidity", + "type":"uint128" + }, + { + "indexed":false, + "internalType":"int24", + "name":"tick", + "type":"int24" + } + ], + "name":"Swap", + "type":"event" + }, + { + "inputs":[ + { + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "internalType":"uint128", + "name":"amount", + "type":"uint128" + } + ], + "name":"burn", + "outputs":[ + { + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + } + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "internalType":"uint128", + "name":"amount0Requested", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"amount1Requested", + "type":"uint128" + } + ], + "name":"collect", + "outputs":[ + { + "internalType":"uint128", + "name":"amount0", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"amount1", + "type":"uint128" + } + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "internalType":"uint128", + "name":"amount0Requested", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"amount1Requested", + "type":"uint128" + } + ], + "name":"collectProtocol", + "outputs":[ + { + "internalType":"uint128", + "name":"amount0", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"amount1", + "type":"uint128" + } + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"factory", + "outputs":[ + { + "internalType":"address", + "name":"", + "type":"address" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"fee", + "outputs":[ + { + "internalType":"uint24", + "name":"", + "type":"uint24" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"feeGrowthGlobal0X128", + "outputs":[ + { + "internalType":"uint256", + "name":"", + "type":"uint256" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"feeGrowthGlobal1X128", + "outputs":[ + { + "internalType":"uint256", + "name":"", + "type":"uint256" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + }, + { + "internalType":"bytes", + "name":"data", + "type":"bytes" + } + ], + "name":"flash", + "outputs":[ + + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"uint16", + "name":"observationCardinalityNext", + "type":"uint16" + } + ], + "name":"increaseObservationCardinalityNext", + "outputs":[ + + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"uint160", + "name":"sqrtPriceX96", + "type":"uint160" + } + ], + "name":"initialize", + "outputs":[ + + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"liquidity", + "outputs":[ + { + "internalType":"uint128", + "name":"", + "type":"uint128" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"maxLiquidityPerTick", + "outputs":[ + { + "internalType":"uint128", + "name":"", + "type":"uint128" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "internalType":"uint128", + "name":"amount", + "type":"uint128" + }, + { + "internalType":"bytes", + "name":"data", + "type":"bytes" + } + ], + "name":"mint", + "outputs":[ + { + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + } + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"uint256", + "name":"", + "type":"uint256" + } + ], + "name":"observations", + "outputs":[ + { + "internalType":"uint32", + "name":"blockTimestamp", + "type":"uint32" + }, + { + "internalType":"int56", + "name":"tickCumulative", + "type":"int56" + }, + { + "internalType":"uint160", + "name":"secondsPerLiquidityCumulativeX128", + "type":"uint160" + }, + { + "internalType":"bool", + "name":"initialized", + "type":"bool" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"uint32[]", + "name":"secondsAgos", + "type":"uint32[]" + } + ], + "name":"observe", + "outputs":[ + { + "internalType":"int56[]", + "name":"tickCumulatives", + "type":"int56[]" + }, + { + "internalType":"uint160[]", + "name":"secondsPerLiquidityCumulativeX128s", + "type":"uint160[]" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"bytes32", + "name":"", + "type":"bytes32" + } + ], + "name":"positions", + "outputs":[ + { + "internalType":"uint128", + "name":"liquidity", + "type":"uint128" + }, + { + "internalType":"uint256", + "name":"feeGrowthInside0LastX128", + "type":"uint256" + }, + { + "internalType":"uint256", + "name":"feeGrowthInside1LastX128", + "type":"uint256" + }, + { + "internalType":"uint128", + "name":"tokensOwed0", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"tokensOwed1", + "type":"uint128" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"protocolFees", + "outputs":[ + { + "internalType":"uint128", + "name":"token0", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"token1", + "type":"uint128" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"uint8", + "name":"feeProtocol0", + "type":"uint8" + }, + { + "internalType":"uint8", + "name":"feeProtocol1", + "type":"uint8" + } + ], + "name":"setFeeProtocol", + "outputs":[ + + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"slot0", + "outputs":[ + { + "internalType":"uint160", + "name":"sqrtPriceX96", + "type":"uint160" + }, + { + "internalType":"int24", + "name":"tick", + "type":"int24" + }, + { + "internalType":"uint16", + "name":"observationIndex", + "type":"uint16" + }, + { + "internalType":"uint16", + "name":"observationCardinality", + "type":"uint16" + }, + { + "internalType":"uint16", + "name":"observationCardinalityNext", + "type":"uint16" + }, + { + "internalType":"uint8", + "name":"feeProtocol", + "type":"uint8" + }, + { + "internalType":"bool", + "name":"unlocked", + "type":"bool" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + } + ], + "name":"snapshotCumulativesInside", + "outputs":[ + { + "internalType":"int56", + "name":"tickCumulativeInside", + "type":"int56" + }, + { + "internalType":"uint160", + "name":"secondsPerLiquidityInsideX128", + "type":"uint160" + }, + { + "internalType":"uint32", + "name":"secondsInside", + "type":"uint32" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "internalType":"bool", + "name":"zeroForOne", + "type":"bool" + }, + { + "internalType":"int256", + "name":"amountSpecified", + "type":"int256" + }, + { + "internalType":"uint160", + "name":"sqrtPriceLimitX96", + "type":"uint160" + }, + { + "internalType":"bytes", + "name":"data", + "type":"bytes" + } + ], + "name":"swap", + "outputs":[ + { + "internalType":"int256", + "name":"amount0", + "type":"int256" + }, + { + "internalType":"int256", + "name":"amount1", + "type":"int256" + } + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"int16", + "name":"", + "type":"int16" + } + ], + "name":"tickBitmap", + "outputs":[ + { + "internalType":"uint256", + "name":"", + "type":"uint256" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"tickSpacing", + "outputs":[ + { + "internalType":"int24", + "name":"", + "type":"int24" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"int24", + "name":"", + "type":"int24" + } + ], + "name":"ticks", + "outputs":[ + { + "internalType":"uint128", + "name":"liquidityGross", + "type":"uint128" + }, + { + "internalType":"int128", + "name":"liquidityNet", + "type":"int128" + }, + { + "internalType":"uint256", + "name":"feeGrowthOutside0X128", + "type":"uint256" + }, + { + "internalType":"uint256", + "name":"feeGrowthOutside1X128", + "type":"uint256" + }, + { + "internalType":"int56", + "name":"tickCumulativeOutside", + "type":"int56" + }, + { + "internalType":"uint160", + "name":"secondsPerLiquidityOutsideX128", + "type":"uint160" + }, + { + "internalType":"uint32", + "name":"secondsOutside", + "type":"uint32" + }, + { + "internalType":"bool", + "name":"initialized", + "type":"bool" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"token0", + "outputs":[ + { + "internalType":"address", + "name":"", + "type":"address" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"token1", + "outputs":[ + { + "internalType":"address", + "name":"", + "type":"address" + } + ], + "stateMutability":"view", + "type":"function" + } + ] \ No newline at end of file diff --git a/subgraph/package.json b/subgraph/package.json index 3e60b798..5106bc7b 100644 --- a/subgraph/package.json +++ b/subgraph/package.json @@ -27,6 +27,7 @@ "deploy-hs": "graph deploy --product hosted-service alphak3y/poolshark-limit", "deploy-local": "graph deploy example --ipfs http://localhost:5001 --node http://127.0.0.1:8020", "deploy-arb-one": "graph deploy --version-label v0.1.1 --node https://api.graph-ams.p2pify.com/e57bc8ecd57c8bac9bfc6222cb8257e7/deploy --ipfs https://api.graph-ams.p2pify.com/e57bc8ecd57c8bac9bfc6222cb8257e7/ipfs limit-arbitrum", + "deploy-arb-one-2": "graph deploy --version-label v0.1.1 --node https://api.graph-ams.p2pify.com/1600f38da0b118b394f0184f131232a1/deploy --ipfs https://api.graph-ams.p2pify.com/1600f38da0b118b394f0184f131232a1/ipfs limit-arbitrum-redeploy", "test": "graph codegen; graph test -v 0.2.0", "prepare:subgraph": "mustache ./config/${NETWORK}.json ./templates/${BLOCKCHAIN}.subgraph.template.yaml > subgraph.yaml && rm -rf generated && npm run generate:schema && npm run subgraph:codegen && npm run subgraph:build", "subgraph:deploy:chainstack:test": "graph deploy --version-label v0.3.1 --node https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/deploy --ipfs https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/ipfs limit-arbitrumGoerli-test", diff --git a/subgraph/src/constants/arbitrum.ts b/subgraph/src/constants/arbitrum.ts index 6349d3bd..c7fc1cfc 100644 --- a/subgraph/src/constants/arbitrum.ts +++ b/subgraph/src/constants/arbitrum.ts @@ -6,7 +6,7 @@ import { LimitPoolFactory as FactoryContract } from '../../generated/templates/L export let FACTORY_ADDRESS = '0xd28d620853af6837d76f1360dc65229d57ba5435' export let RANGE_STAKER_ADDRESS = '0x373bf43249bed0518119d29b80c7d9c68d98cfc2' // used for safe eth pricing -export const STABLE_POOL_ADDRESS = '0x5330588b4efd339b8ea441afedf09aab851f2d48' +export const STABLE_POOL_ADDRESS = '0xc31e54c7a869b9fcbecc14363cf510d1c41fa443' // -------------- END REQUIRED CONFIG PER DEPLOYMENT -------------- // determines which token to use for eth <-> usd rate, true means stable is token0 in pool above diff --git a/subgraph/src/mappings/utils/loads.ts b/subgraph/src/mappings/utils/loads.ts index bc4650a1..ad59ad49 100644 --- a/subgraph/src/mappings/utils/loads.ts +++ b/subgraph/src/mappings/utils/loads.ts @@ -15,7 +15,7 @@ class LoadBasePriceRet { entity: BasePrice exists: boolean } -export function safeLoadBasePrice(name: string): LoadBasePriceRet { +export function safeLoadBasePrice(name: string, stablePool: LimitPool | null = null): LoadBasePriceRet { let exists = true let basePriceEntity = BasePrice.load(name) @@ -25,7 +25,11 @@ export function safeLoadBasePrice(name: string): LoadBasePriceRet { exists = false } - basePriceEntity.USD = getEthPriceInUSD() + if (stablePool !== null) { + // only non-null when updating v3 pool price + basePriceEntity.USD = getEthPriceInUSD(stablePool) + } + return { entity: basePriceEntity, diff --git a/subgraph/src/mappings/utils/price.ts b/subgraph/src/mappings/utils/price.ts index 7eba0feb..a0a243b3 100644 --- a/subgraph/src/mappings/utils/price.ts +++ b/subgraph/src/mappings/utils/price.ts @@ -26,9 +26,8 @@ export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, t return [price0, price1] } -export function getEthPriceInUSD(): BigDecimal { +export function getEthPriceInUSD(stablePool: LimitPool): BigDecimal { // fetch eth prices for each stablecoin - let stablePool = LimitPool.load(STABLE_POOL_ADDRESS) // stable is token0 if (stablePool !== null) { if (STABLE_IS_TOKEN_0) { log.info('stable pool token0: {}', [stablePool.price0.toString()]) diff --git a/subgraph/src/mappings/v3pool.ts b/subgraph/src/mappings/v3pool.ts new file mode 100644 index 00000000..7b504c89 --- /dev/null +++ b/subgraph/src/mappings/v3pool.ts @@ -0,0 +1,36 @@ +import { BigInt } from "@graphprotocol/graph-ts" +import { Swap, Token } from "../../generated/schema" +import { STABLE_POOL_ADDRESS } from "../constants/arbitrum" +import { STABLE_IS_TOKEN_0 } from "../constants/arbitrum-goerli" +import { safeLoadBasePrice, safeLoadLimitPool } from "./utils/loads" +import { sqrtPriceX96ToTokenPrices } from "./utils/price" + +export function handleV3Swap(event: Swap): void { + let loadPool = safeLoadLimitPool(STABLE_POOL_ADDRESS) + + let pool = loadPool.entity + + pool.poolPrice = event.priceAfter + + let token0: Token; let token1: Token; + + if (STABLE_IS_TOKEN_0) { + token0 = new Token('USDC') + token0.decimals = BigInt.fromString('6') + token1 = new Token('WETH') + token1.decimals = BigInt.fromString('18') + } else { + token0 = new Token('WETH') + token0.decimals = BigInt.fromString('18') + token1 = new Token('USDC') + token1.decimals = BigInt.fromString('6') + } + + let prices = sqrtPriceX96ToTokenPrices(pool.poolPrice, token0, token1) + pool.price0 = prices[0] + pool.price1 = prices[1] + + let loadBasePrice = safeLoadBasePrice('eth', pool) + let basePrice = loadBasePrice.entity + basePrice.save() +} \ No newline at end of file diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index e248cdb3..8f144686 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -113,9 +113,9 @@ dataSources: name: LimitPoolFactory network: arbitrum-one source: - address: '0xd28d620853af6837d76f1360dc65229d57ba5435' + address: '0x40e21ce13f5089bc421c063506bbb0df02bca07e' abi: LimitPoolFactory - startBlock: 158864748 + startBlock: 165077269 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -142,9 +142,9 @@ dataSources: name: LimitPoolManager network: arbitrum-one source: - address: '0x441bb7b76c9932d5c65d7a78b3413978d7370761' + address: '0x6b1ffc44ec8ed486843640b66180d5ee366fee9f' abi: LimitPoolManager - startBlock: 158864726 + startBlock: 165077264 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -177,9 +177,9 @@ dataSources: name: PoolsharkRouter network: arbitrum-one source: - address: '0x5357308a5a4db6c65fa6c4b122dfc3d749f2dd6a' + address: '0x5bb32287ecb48de323760c3384a3285f732950b6' abi: PoolsharkRouter - startBlock: 158865312 + startBlock: 167367213 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -212,4 +212,24 @@ dataSources: file: ./abis/vFIN.json eventHandlers: - event: Transfer(indexed address,indexed address,indexed uint256) - handler: handleTransfer \ No newline at end of file + handler: handleTransfer + - kind: ethereum/contract + name: v3EthUsdcPool + network: arbitrum-one + source: + address: '0xc31e54c7a869b9fcbecc14363cf510d1c41fa443' + abi: v3Pool + startBlock: 161266335 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/v3Pool.ts + entities: + - BasePrice + abis: + - name: v3Pool + file: ./abis/v3Pool.json + eventHandlers: + - event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24) + handler: handleV3Swap \ No newline at end of file diff --git a/tasks/deploy/utils/mintPosition.ts b/tasks/deploy/utils/mintPosition.ts index e7f3d3a5..1e58b615 100644 --- a/tasks/deploy/utils/mintPosition.ts +++ b/tasks/deploy/utils/mintPosition.ts @@ -4,6 +4,7 @@ import { InitialSetup } from '../../../test/utils/setup/initialSetup' import { validateMint as validateMintRange, validateBurn as validateBurnRange } from '../../../test/utils/contracts/rangepool' import { mintSigners20 } from '../../../test/utils/token' import { getNonce } from '../../utils' +import { parseUnits } from 'ethers/lib/utils' export class MintPosition { private initialSetup: InitialSetup @@ -84,48 +85,54 @@ export class MintPosition { // ) // console.log('position snapshot', snapshot.feesOwed0.toString(), snapshot.feesOwed1.toString()) - + const amountIn = parseUnits('1', 16) + const signer = hre.props.alice + let approveTxn = await hre.props.token0.connect(signer).approve(hre.props.poolRouter.address, amountIn) + // let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, amountIn) + await approveTxn.wait() // const aliceId = await validateMintRange({ // signer: hre.props.alice, - // recipient: '0x9dA9409D17DeA285B078af06206941C049F692Dc', - // lower: '-400000', - // upper: '100000', - // amount0: token0Amount.mul(1000), - // amount1: token1Amount.mul(1000), - // balance0Decrease: BigNumber.from('624999999999999999'), - // balance1Decrease: token1Amount.mul(10).sub(1), + // recipient: hre.props.alice.address, + // lower: '-200200', + // upper: '-200000', + // amount0: BN_ZERO, + // amount1: amountIn, + // balance0Decrease: BN_ZERO, + // balance1Decrease: amountIn, // liquidityIncrease: BN_ZERO, // revertMessage: '', // }) // return - const token0Amount = ethers.utils.parseUnits('1', 16) - const signer = hre.props.alice - const amountIn = token0Amount - let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, amountIn) - await approveTxn.wait() - // // for (let i=0; i < 20; i++) { + // const token0Amount = ethers.utils.parseUnits('1', 16) + + // const amountIn = token0Amount + + // // // for (let i=0; i < 20; i++) { + + // 0 => 1 + //0xe80dc9a69853483c745f8e32162f0bd5813cb291 => new factory const zeroForOne = true - const priceLimit = BigNumber.from('2358285847295149069702956253974') + const priceLimit = BigNumber.from('3751434670994143578079625525371') let txn = await hre.props.poolRouter .connect(signer) .multiSwapSplit( - ['0x53C314F6BCdA3C253ECA949B0D66b9679c2bB219'], + ['0x5330588b4eFD339b8ea441aFedF09AAb851f2d48'], [ { to: signer.address, priceLimit: priceLimit, amount: amountIn, zeroForOne: zeroForOne, - exactIn: false, + exactIn: true, callbackData: ethers.utils.formatBytes32String('') }, ]) await txn.wait() // } - return + // return // const globalStateAfter = (await hre.props.limitPool.globalState()) // console.log('sample state', globalStateAfter.pool.samples.index, globalStateAfter.pool.samples.count, globalStateAfter.pool.samples.countMax, globalStateAfter.pool.tickAtPrice) @@ -173,7 +180,7 @@ export class MintPosition { // await getPrice(false, true) // await getLiquidity(false, true) - console.log('position minted') + // console.log('position minted') } public async postDeployment() {} diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index 84dedab5..032ebd07 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -32,8 +32,8 @@ export class InitialSetup { private deployTokens = false private deployContracts = false private deployPools = false - private deployRouter = true - private deployStaker = false + private deployRouter = false + private deployStaker = true constructor() { this.deployAssist = new DeployAssist() From 94817b82cc6c46e5e879060ef1f8a47fe07ba648 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sun, 7 Jan 2024 19:40:47 -0700 Subject: [PATCH 26/39] license updates to SSPL --- contracts/interfaces/IERC20Minimal.sol | 2 +- contracts/interfaces/IPool.sol | 2 +- contracts/interfaces/IPositionERC1155.sol | 2 +- contracts/interfaces/IWETH9.sol | 2 +- .../callbacks/ICoverPoolCallback.sol | 2 +- .../callbacks/ILimitPoolCallback.sol | 2 +- contracts/interfaces/cover/ICoverPool.sol | 2 +- .../interfaces/cover/ICoverPoolFactory.sol | 2 +- contracts/interfaces/cover/ITwapSource.sol | 2 +- contracts/interfaces/limit/ILimitPool.sol | 2 +- .../interfaces/limit/ILimitPoolFactory.sol | 2 +- .../interfaces/limit/ILimitPoolManager.sol | 2 +- .../limit/ILimitPoolStorageView.sol | 2 +- contracts/interfaces/limit/ILimitPoolView.sol | 2 +- contracts/interfaces/range/IRangePool.sol | 2 +- .../interfaces/range/IRangePoolFactory.sol | 2 +- .../interfaces/range/IRangePoolManager.sol | 2 +- contracts/interfaces/staking/IRangeStaker.sol | 2 +- .../interfaces/structs/LimitPoolStructs.sol | 2 +- .../interfaces/structs/PoolsharkStructs.sol | 2 +- .../interfaces/structs/RangePoolStructs.sol | 2 +- subgraph/LICENSE | 675 +++++++++++++++++- tasks/deploy/utils/mintPosition.ts | 6 +- 23 files changed, 672 insertions(+), 51 deletions(-) diff --git a/contracts/interfaces/IERC20Minimal.sol b/contracts/interfaces/IERC20Minimal.sol index e83f0a35..142bccd4 100644 --- a/contracts/interfaces/IERC20Minimal.sol +++ b/contracts/interfaces/IERC20Minimal.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; interface IERC20Minimal { diff --git a/contracts/interfaces/IPool.sol b/contracts/interfaces/IPool.sol index 8549b94c..2f407ebe 100644 --- a/contracts/interfaces/IPool.sol +++ b/contracts/interfaces/IPool.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/IPositionERC1155.sol b/contracts/interfaces/IPositionERC1155.sol index 8d3f216c..eed46d7c 100644 --- a/contracts/interfaces/IPositionERC1155.sol +++ b/contracts/interfaces/IPositionERC1155.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; diff --git a/contracts/interfaces/IWETH9.sol b/contracts/interfaces/IWETH9.sol index accc740e..2947af41 100644 --- a/contracts/interfaces/IWETH9.sol +++ b/contracts/interfaces/IWETH9.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; interface IWETH9 { diff --git a/contracts/interfaces/callbacks/ICoverPoolCallback.sol b/contracts/interfaces/callbacks/ICoverPoolCallback.sol index e2cb33a5..71f7581f 100644 --- a/contracts/interfaces/callbacks/ICoverPoolCallback.sol +++ b/contracts/interfaces/callbacks/ICoverPoolCallback.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; /// @title Callback for mints diff --git a/contracts/interfaces/callbacks/ILimitPoolCallback.sol b/contracts/interfaces/callbacks/ILimitPoolCallback.sol index 5e5452a2..786b07dd 100644 --- a/contracts/interfaces/callbacks/ILimitPoolCallback.sol +++ b/contracts/interfaces/callbacks/ILimitPoolCallback.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; /// @title Callback for range mints diff --git a/contracts/interfaces/cover/ICoverPool.sol b/contracts/interfaces/cover/ICoverPool.sol index 0aaecd74..201edd99 100644 --- a/contracts/interfaces/cover/ICoverPool.sol +++ b/contracts/interfaces/cover/ICoverPool.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity ^0.8.18; import '../structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/cover/ICoverPoolFactory.sol b/contracts/interfaces/cover/ICoverPoolFactory.sol index 9e7e613b..91ad5501 100644 --- a/contracts/interfaces/cover/ICoverPoolFactory.sol +++ b/contracts/interfaces/cover/ICoverPoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; abstract contract ICoverPoolFactory { diff --git a/contracts/interfaces/cover/ITwapSource.sol b/contracts/interfaces/cover/ITwapSource.sol index c76ac188..692b6b98 100644 --- a/contracts/interfaces/cover/ITwapSource.sol +++ b/contracts/interfaces/cover/ITwapSource.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity ^0.8.18; import '../structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPool.sol b/contracts/interfaces/limit/ILimitPool.sol index 8a22f554..b24477d7 100644 --- a/contracts/interfaces/limit/ILimitPool.sol +++ b/contracts/interfaces/limit/ILimitPool.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPoolFactory.sol b/contracts/interfaces/limit/ILimitPoolFactory.sol index fd07669e..13e9f00d 100644 --- a/contracts/interfaces/limit/ILimitPoolFactory.sol +++ b/contracts/interfaces/limit/ILimitPoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../structs/PoolsharkStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPoolManager.sol b/contracts/interfaces/limit/ILimitPoolManager.sol index a35f7f7d..78980b8d 100644 --- a/contracts/interfaces/limit/ILimitPoolManager.sol +++ b/contracts/interfaces/limit/ILimitPoolManager.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; /// @notice LimitPoolManager interface diff --git a/contracts/interfaces/limit/ILimitPoolStorageView.sol b/contracts/interfaces/limit/ILimitPoolStorageView.sol index ee38f56a..b0861401 100644 --- a/contracts/interfaces/limit/ILimitPoolStorageView.sol +++ b/contracts/interfaces/limit/ILimitPoolStorageView.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; diff --git a/contracts/interfaces/limit/ILimitPoolView.sol b/contracts/interfaces/limit/ILimitPoolView.sol index 985d049c..9e0df209 100644 --- a/contracts/interfaces/limit/ILimitPoolView.sol +++ b/contracts/interfaces/limit/ILimitPoolView.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; diff --git a/contracts/interfaces/range/IRangePool.sol b/contracts/interfaces/range/IRangePool.sol index 63abe328..447e9c0b 100644 --- a/contracts/interfaces/range/IRangePool.sol +++ b/contracts/interfaces/range/IRangePool.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../structs/RangePoolStructs.sol'; diff --git a/contracts/interfaces/range/IRangePoolFactory.sol b/contracts/interfaces/range/IRangePoolFactory.sol index e3e9f669..e226f0b9 100644 --- a/contracts/interfaces/range/IRangePoolFactory.sol +++ b/contracts/interfaces/range/IRangePoolFactory.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; interface IRangePoolFactory { diff --git a/contracts/interfaces/range/IRangePoolManager.sol b/contracts/interfaces/range/IRangePoolManager.sol index c914972f..44c9abf5 100644 --- a/contracts/interfaces/range/IRangePoolManager.sol +++ b/contracts/interfaces/range/IRangePoolManager.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../structs/RangePoolStructs.sol'; diff --git a/contracts/interfaces/staking/IRangeStaker.sol b/contracts/interfaces/staking/IRangeStaker.sol index c310dff6..1812f9dc 100644 --- a/contracts/interfaces/staking/IRangeStaker.sol +++ b/contracts/interfaces/staking/IRangeStaker.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; diff --git a/contracts/interfaces/structs/LimitPoolStructs.sol b/contracts/interfaces/structs/LimitPoolStructs.sol index f48099f2..fff9d99d 100644 --- a/contracts/interfaces/structs/LimitPoolStructs.sol +++ b/contracts/interfaces/structs/LimitPoolStructs.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import './PoolsharkStructs.sol'; diff --git a/contracts/interfaces/structs/PoolsharkStructs.sol b/contracts/interfaces/structs/PoolsharkStructs.sol index 1ff35ef9..ccdc7f10 100644 --- a/contracts/interfaces/structs/PoolsharkStructs.sol +++ b/contracts/interfaces/structs/PoolsharkStructs.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../cover/ITwapSource.sol'; diff --git a/contracts/interfaces/structs/RangePoolStructs.sol b/contracts/interfaces/structs/RangePoolStructs.sol index d6dc90ac..87d2e490 100644 --- a/contracts/interfaces/structs/RangePoolStructs.sol +++ b/contracts/interfaces/structs/RangePoolStructs.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import './PoolsharkStructs.sol'; diff --git a/subgraph/LICENSE b/subgraph/LICENSE index 46c3fc0e..e72bfdda 100644 --- a/subgraph/LICENSE +++ b/subgraph/LICENSE @@ -1,51 +1,674 @@ -Business Source License 1.1 + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 -License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. "Business Source License" is a trademark of MariaDB Corporation Ab. + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. -Parameters + Preamble -Licensor: Poolshark Labs Ltd + The GNU General Public License is a free, copyleft license for +software and other kinds of works. -Licensed Work: Poolshark Limit The Licensed Work is (C) 2023 Poolshark Labs Ltd + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. -Additional Use Grant: Any uses listed and defined at limit-license-grants.pshark.eth + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. -Change Date: The earlier of 2025-12-11 or a date specified at limit-license-date.pshark.eth + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. -Change License: GNU Affero General Public License v3.0 only + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. -Terms + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. -The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use. + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. -Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the terms of the Change License, and the rights granted in the paragraph above terminate. + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. -If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work. + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. -All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor. + The precise terms and conditions for copying, distribution and +modification follow. -You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from a third party, the terms and conditions set forth in this License apply to your use of that work. + TERMS AND CONDITIONS -Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work. + 0. Definitions. -This License does not grant you any right in any trademark or logo of Licensor or its affiliates (provided that you may use a trademark or logo of Licensor as expressly required by this License). + "This License" refers to version 3 of the GNU General Public License. -TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE. + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. -MariaDB hereby grants you permission to use this License’s text to license your works, and to refer to it using the trademark "Business Source License", as long as you comply with the Covenants of Licensor below. + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. -Covenants of Licensor + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. -In consideration of the right to use this License’s text and the "Business Source License" name and trademark, Licensor covenants to MariaDB, and to all other recipients of the licensed work to be provided by Licensor: + A "covered work" means either the unmodified Program or a work based +on the Program. -To specify as the Change License the GPL Version 3.0 or any later version, or a license that is compatible with GPL Version 3.0 or a later version, where "compatible" means that software provided under the Change License can be included in a program with software provided under GPL Version 3.0 or a later version. Licensor may specify additional Change Licenses without limitation. + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. -To either: (a) specify an additional grant of rights to use that does not impose any additional restriction on the right granted in this License, as the Additional Use Grant; or (b) insert the text "None". + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. -To specify a Change Date. + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. -Not to modify this License in any other way. + 1. Source Code. -Notice + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. -The Business Source License (this document, or the "License") is not an Open Source license. However, the Licensed Work will eventually be made available under an Open Source License, as stated in this License. \ No newline at end of file + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. \ No newline at end of file diff --git a/tasks/deploy/utils/mintPosition.ts b/tasks/deploy/utils/mintPosition.ts index 1e58b615..ca1c0ba6 100644 --- a/tasks/deploy/utils/mintPosition.ts +++ b/tasks/deploy/utils/mintPosition.ts @@ -1,8 +1,6 @@ import { BigNumber } from 'ethers' -import { BN_ZERO, getLiquidity, getPrice, validateBurn, validateMint, validateSwap, } from '../../../test/utils/contracts/limitpool' +import { BN_ZERO, } from '../../../test/utils/contracts/limitpool' import { InitialSetup } from '../../../test/utils/setup/initialSetup' -import { validateMint as validateMintRange, validateBurn as validateBurnRange } from '../../../test/utils/contracts/rangepool' -import { mintSigners20 } from '../../../test/utils/token' import { getNonce } from '../../utils' import { parseUnits } from 'ethers/lib/utils' @@ -114,7 +112,7 @@ export class MintPosition { const zeroForOne = true - const priceLimit = BigNumber.from('3751434670994143578079625525371') + const priceLimit = BigNumber.from('3721491874816186603690483') let txn = await hre.props.poolRouter .connect(signer) .multiSwapSplit( From 89a3f08298bdf8631f3547b3441471c6b14ee9e0 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Tue, 9 Jan 2024 06:49:24 -0700 Subject: [PATCH 27/39] update licenses; subgraph constants --- LICENSE | 51 +++ contracts/base/events/LimitPoolEvents.sol | 2 +- .../base/events/LimitPoolFactoryEvents.sol | 2 +- .../base/events/LimitPoolManagerEvents.sol | 2 +- .../base/events/PoolsharkRouterEvents.sol | 2 +- .../base/events/PositionERC1155Events.sol | 2 +- contracts/base/events/RangeStakerEvents.sol | 2 +- .../base/storage/LimitPoolFactoryStorage.sol | 2 +- .../base/storage/LimitPoolImmutables.sol | 2 +- .../storage/PositionERC1155Immutables.sol | 2 +- contracts/interfaces/IPositionERC1155.sol | 2 +- contracts/libraries/Samples.sol | 2 +- contracts/libraries/math/ConstantProduct.sol | 2 +- contracts/libraries/math/OverflowMath.sol | 2 +- contracts/libraries/utils/Bytes.sol | 2 +- contracts/libraries/utils/Collect.sol | 2 +- contracts/libraries/utils/PositionTokens.sol | 2 +- contracts/libraries/utils/SafeCast.sol | 2 +- contracts/libraries/utils/SafeTransfers.sol | 2 +- contracts/libraries/utils/String.sol | 2 +- contracts/staking/RangeStaker.sol | 2 +- contracts/test/WETH9.sol | 2 +- contracts/utils/LimitPoolManager.sol | 2 +- contracts/utils/PoolsharkRouter.sol | 15 +- contracts/utils/PositionERC1155.sol | 20 +- hardhat.config.ts | 24 +- scripts/autogen/contract-deployments-keys.ts | 86 +++- scripts/autogen/contract-deployments.json | 399 +++++++++++++----- scripts/constants/supportedNetworks.ts | 2 + subgraph/package.json | 5 +- subgraph/src/constants/arbitrum-goerli.ts | 2 +- subgraph/src/constants/arbitrum-sepolia.ts | 52 +++ subgraph/src/constants/arbitrum.ts | 6 +- subgraph/src/constants/constants.ts | 5 + subgraph/src/constants/scroll-sepolia.ts | 52 +++ subgraph/src/mappings/limit/burn.ts | 2 +- subgraph/src/mappings/limit/mint.ts | 2 +- subgraph/src/mappings/limitpoolfactory.ts | 2 +- subgraph/src/mappings/limitpoolmanager.ts | 2 +- subgraph/src/mappings/pool/swap.ts | 2 +- subgraph/src/mappings/positionerc1155.ts | 2 +- subgraph/src/mappings/range/burn.ts | 2 +- subgraph/src/mappings/range/mint.ts | 2 +- subgraph/src/mappings/staking/rangestaker.ts | 2 +- subgraph/src/mappings/utils/loads.ts | 6 +- subgraph/src/mappings/utils/math.ts | 2 +- subgraph/src/mappings/utils/price.ts | 5 +- subgraph/src/mappings/utils/tvl.ts | 2 +- subgraph/src/mappings/v3pool.ts | 68 ++- subgraph/subgraph.yaml | 40 +- .../arbitrum-sepolia.subgraph.template.yaml | 237 +++++++++++ .../templates/arbitrum.subgraph.template.yaml | 68 ++- .../scroll-sepolia.subgraph.template.yaml | 237 +++++++++++ tasks/deploy/utils/mintPosition.ts | 10 +- tasks/deploy/utils/mintTokens.ts | 10 +- test/contracts/tgedeployer.ts | 25 +- test/utils/contracts/poolsharkrouter.ts | 2 +- test/utils/setup/initialSetup.ts | 17 +- test/utils/token.ts | 12 + 59 files changed, 1280 insertions(+), 242 deletions(-) create mode 100644 LICENSE create mode 100644 subgraph/src/constants/arbitrum-sepolia.ts create mode 100644 subgraph/src/constants/constants.ts create mode 100644 subgraph/src/constants/scroll-sepolia.ts create mode 100644 subgraph/templates/arbitrum-sepolia.subgraph.template.yaml create mode 100644 subgraph/templates/scroll-sepolia.subgraph.template.yaml diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..5653a4da --- /dev/null +++ b/LICENSE @@ -0,0 +1,51 @@ +Business Source License 1.1 + +License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. "Business Source License" is a trademark of MariaDB Corporation Ab. + +Parameters + +Licensor: Poolshark Labs Ltd + +Licensed Work: Poolshark Limit The Licensed Work is (C) 2023 Poolshark Labs Ltd + +Additional Use Grant: Any uses listed and defined at limit-license-grants.pshark.eth + +Change Date: The earlier of 2025-02-09 or a date specified at limit-license-date.pshark.eth + +Change License: Server Side Public License, v 1 + +Terms + +The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use. + +Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the terms of the Change License, and the rights granted in the paragraph above terminate. + +If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work. + +All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor. + +You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from a third party, the terms and conditions set forth in this License apply to your use of that work. + +Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work. + +This License does not grant you any right in any trademark or logo of Licensor or its affiliates (provided that you may use a trademark or logo of Licensor as expressly required by this License). + +TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE. + +MariaDB hereby grants you permission to use this License’s text to license your works, and to refer to it using the trademark "Business Source License", as long as you comply with the Covenants of Licensor below. + +Covenants of Licensor + +In consideration of the right to use this License’s text and the "Business Source License" name and trademark, Licensor covenants to MariaDB, and to all other recipients of the licensed work to be provided by Licensor: + +To specify as the Change License the GPL Version 3.0 or any later version, or a license that is compatible with GPL Version 3.0 or a later version, where "compatible" means that software provided under the Change License can be included in a program with software provided under GPL Version 3.0 or a later version. Licensor may specify additional Change Licenses without limitation. + +To either: (a) specify an additional grant of rights to use that does not impose any additional restriction on the right granted in this License, as the Additional Use Grant; or (b) insert the text "None". + +To specify a Change Date. + +Not to modify this License in any other way. + +Notice + +The Business Source License (this document, or the "License") is not an Open Source license. However, the Licensed Work will eventually be made available under an Open Source License, as stated in this License. \ No newline at end of file diff --git a/contracts/base/events/LimitPoolEvents.sol b/contracts/base/events/LimitPoolEvents.sol index 35c9b72e..d14435e5 100644 --- a/contracts/base/events/LimitPoolEvents.sol +++ b/contracts/base/events/LimitPoolEvents.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; /// @notice Events emitted by the LimitPool contract(s) diff --git a/contracts/base/events/LimitPoolFactoryEvents.sol b/contracts/base/events/LimitPoolFactoryEvents.sol index 0a3981f2..8b4561d2 100644 --- a/contracts/base/events/LimitPoolFactoryEvents.sol +++ b/contracts/base/events/LimitPoolFactoryEvents.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; /// @notice Events emitted by the LimitPoolFactory contract diff --git a/contracts/base/events/LimitPoolManagerEvents.sol b/contracts/base/events/LimitPoolManagerEvents.sol index 499bb0cb..b773daf9 100644 --- a/contracts/base/events/LimitPoolManagerEvents.sol +++ b/contracts/base/events/LimitPoolManagerEvents.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../../interfaces/structs/PoolsharkStructs.sol'; diff --git a/contracts/base/events/PoolsharkRouterEvents.sol b/contracts/base/events/PoolsharkRouterEvents.sol index f2797a50..78a1472b 100644 --- a/contracts/base/events/PoolsharkRouterEvents.sol +++ b/contracts/base/events/PoolsharkRouterEvents.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; /// @notice Events emitted by the PoolsharkRouter contract diff --git a/contracts/base/events/PositionERC1155Events.sol b/contracts/base/events/PositionERC1155Events.sol index eea3bdc7..f838238e 100644 --- a/contracts/base/events/PositionERC1155Events.sol +++ b/contracts/base/events/PositionERC1155Events.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; /// @notice Events emitted by the PositionERC1155 contract(s) diff --git a/contracts/base/events/RangeStakerEvents.sol b/contracts/base/events/RangeStakerEvents.sol index afa5dc16..927f0b99 100644 --- a/contracts/base/events/RangeStakerEvents.sol +++ b/contracts/base/events/RangeStakerEvents.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; /// @notice Events emitted by the RangeStaker contract diff --git a/contracts/base/storage/LimitPoolFactoryStorage.sol b/contracts/base/storage/LimitPoolFactoryStorage.sol index 21c4ed9d..365a2840 100644 --- a/contracts/base/storage/LimitPoolFactoryStorage.sol +++ b/contracts/base/storage/LimitPoolFactoryStorage.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; abstract contract LimitPoolFactoryStorage { diff --git a/contracts/base/storage/LimitPoolImmutables.sol b/contracts/base/storage/LimitPoolImmutables.sol index 0faacec9..4197188e 100644 --- a/contracts/base/storage/LimitPoolImmutables.sol +++ b/contracts/base/storage/LimitPoolImmutables.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import {Clone} from '../../external/solady/Clone.sol'; diff --git a/contracts/base/storage/PositionERC1155Immutables.sol b/contracts/base/storage/PositionERC1155Immutables.sol index 48bec754..9ed5b716 100644 --- a/contracts/base/storage/PositionERC1155Immutables.sol +++ b/contracts/base/storage/PositionERC1155Immutables.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import {Clone} from '../../external/solady/Clone.sol'; diff --git a/contracts/interfaces/IPositionERC1155.sol b/contracts/interfaces/IPositionERC1155.sol index eed46d7c..9dd3aa0b 100644 --- a/contracts/interfaces/IPositionERC1155.sol +++ b/contracts/interfaces/IPositionERC1155.sol @@ -42,7 +42,7 @@ interface IPositionERC1155 is IERC165, PoolsharkStructs { view returns (uint256[] memory batchBalances); - function totalSupply(uint256 id) external view returns (uint256); + function totalSupply() external view returns (uint256); function isApprovedForAll(address owner, address spender) external diff --git a/contracts/libraries/Samples.sol b/contracts/libraries/Samples.sol index 14d02d10..218724b8 100644 --- a/contracts/libraries/Samples.sol +++ b/contracts/libraries/Samples.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import './math/ConstantProduct.sol'; diff --git a/contracts/libraries/math/ConstantProduct.sol b/contracts/libraries/math/ConstantProduct.sol index 1fe52d52..dfd6486c 100644 --- a/contracts/libraries/math/ConstantProduct.sol +++ b/contracts/libraries/math/ConstantProduct.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import './OverflowMath.sol'; diff --git a/contracts/libraries/math/OverflowMath.sol b/contracts/libraries/math/OverflowMath.sol index 36872721..3e3784c4 100644 --- a/contracts/libraries/math/OverflowMath.sol +++ b/contracts/libraries/math/OverflowMath.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; /// @notice Math library that facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision. diff --git a/contracts/libraries/utils/Bytes.sol b/contracts/libraries/utils/Bytes.sol index 833a8288..32ab63d3 100644 --- a/contracts/libraries/utils/Bytes.sol +++ b/contracts/libraries/utils/Bytes.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; library Bytes { diff --git a/contracts/libraries/utils/Collect.sol b/contracts/libraries/utils/Collect.sol index ba40406a..419f256e 100644 --- a/contracts/libraries/utils/Collect.sol +++ b/contracts/libraries/utils/Collect.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; diff --git a/contracts/libraries/utils/PositionTokens.sol b/contracts/libraries/utils/PositionTokens.sol index 827539d3..b9b40cbd 100644 --- a/contracts/libraries/utils/PositionTokens.sol +++ b/contracts/libraries/utils/PositionTokens.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import './Bytes.sol'; diff --git a/contracts/libraries/utils/SafeCast.sol b/contracts/libraries/utils/SafeCast.sol index 80924390..d64b3d5f 100644 --- a/contracts/libraries/utils/SafeCast.sol +++ b/contracts/libraries/utils/SafeCast.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; /// @title Safe casting methods diff --git a/contracts/libraries/utils/SafeTransfers.sol b/contracts/libraries/utils/SafeTransfers.sol index d638b6f9..d3c46e50 100644 --- a/contracts/libraries/utils/SafeTransfers.sol +++ b/contracts/libraries/utils/SafeTransfers.sol @@ -1,4 +1,4 @@ -//SPDX-License-Identifier: AGPL-3.0-only +//SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; diff --git a/contracts/libraries/utils/String.sol b/contracts/libraries/utils/String.sol index 342f4f00..50472658 100644 --- a/contracts/libraries/utils/String.sol +++ b/contracts/libraries/utils/String.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; library String { diff --git a/contracts/staking/RangeStaker.sol b/contracts/staking/RangeStaker.sol index bbb66e07..04a12a96 100644 --- a/contracts/staking/RangeStaker.sol +++ b/contracts/staking/RangeStaker.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../interfaces/IPool.sol'; diff --git a/contracts/test/WETH9.sol b/contracts/test/WETH9.sol index 44867708..8b5c74b6 100644 --- a/contracts/test/WETH9.sol +++ b/contracts/test/WETH9.sol @@ -1,4 +1,4 @@ -//SPDX-License-Identifier: AGPL-3.0-only +//SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; contract WETH9 { diff --git a/contracts/utils/LimitPoolManager.sol b/contracts/utils/LimitPoolManager.sol index 3fc2c897..207f04e5 100644 --- a/contracts/utils/LimitPoolManager.sol +++ b/contracts/utils/LimitPoolManager.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../interfaces/IPool.sol'; diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index 4b4244a1..60c8bb4e 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; import '../interfaces/IPool.sol'; @@ -18,7 +18,7 @@ import '../interfaces/structs/PoolsharkStructs.sol'; import '../external/solady/LibClone.sol'; /** - * @title LimitPoolManager + * @title PoolsharkRouter * @notice The router for all limit and cover pools * @author Poolshark * @author @alphak3y @@ -770,14 +770,13 @@ contract PoolsharkRouter is } function deployTge(address tgePool, address staker) external { - // address staker = 0x373bF43249BeD0518119d29B80c7D9C68d98cFC2; // read pool price RangePoolState memory tgePoolState; ( tgePoolState, ,,,,, ) = IPool(tgePool).globalState(); - uint160 expectedPoolPrice = 2062122576071194503151227620392; + uint160 expectedPoolPrice = 2172618421097231267834892073346; if (tgePoolState.price < expectedPoolPrice) { // move pool price up if below SwapParams memory swapParams = SwapParams({ @@ -795,7 +794,7 @@ contract PoolsharkRouter is ) }); IPool(tgePool).swap(swapParams); - } else if (tgePoolState.price < expectedPoolPrice) { + } else if (tgePoolState.price > expectedPoolPrice) { // move pool price down if above SwapParams memory swapParams = SwapParams({ to: msg.sender, @@ -827,11 +826,11 @@ contract PoolsharkRouter is }); MintRangeParams memory mintRangeParams = MintRangeParams({ to: staker, - lower: 44850, //TODO: change to $10 per FIN = 221.5 = ~54000 + lower: 54000, upper: 77040, positionId: 0, - amount0: 44146000000000000000, //TODO: calculate new amounts - amount1: 42586954683848930000000, //TODO: calculate new amounts + amount0: 39168000000000000000, + amount1: 33000000000000000000000, callbackData: abi.encode(callbackData) }); IRangePool(tgePool).mintRange(mintRangeParams); diff --git a/contracts/utils/PositionERC1155.sol b/contracts/utils/PositionERC1155.sol index 89e968e0..f441de75 100644 --- a/contracts/utils/PositionERC1155.sol +++ b/contracts/utils/PositionERC1155.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-License-Identifier: SSPL-1.0 pragma solidity 0.8.18; @@ -25,8 +25,8 @@ contract PositionERC1155 is IPositionERC1155, PositionERC1155Immutables { /// @dev owner => spender => approved mapping(address => mapping(address => bool)) private _spenderApprovals; - /// @dev token id => total supply - mapping(uint256 => uint256) private _totalSupplyById; + /// @dev total supply for all token ids + uint256 public totalSupply; // eth address for safe withdrawal address public constant ethAddress = address(0); @@ -186,16 +186,6 @@ contract PositionERC1155 is IPositionERC1155, PositionERC1155Immutables { return Bytes.bytes32ToString(tokenSymbol()); } - function totalSupply(uint256 _id) - public - view - virtual - override - returns (uint256) - { - return _totalSupplyById[_id]; - } - function balanceOf(address _account, uint256 _id) public view @@ -232,9 +222,9 @@ contract PositionERC1155 is IPositionERC1155, PositionERC1155Immutables { ) internal virtual { if (_account == address(0)) require(false, 'MintToAddress0()'); _beforeTokenTransfer(address(0), _account, _id, _amount); - _totalSupplyById[_id] += _amount; uint256 _accountBalance = _tokenBalances[_id][_account]; unchecked { + totalSupply += _amount; _tokenBalances[_id][_account] = _accountBalance + _amount; } emit TransferSingle(msg.sender, address(0), _account, _id, _amount); @@ -262,8 +252,8 @@ contract PositionERC1155 is IPositionERC1155, PositionERC1155Immutables { ); _beforeTokenTransfer(_account, address(0), _id, _amount); unchecked { + totalSupply -= _amount; _tokenBalances[_id][_account] = _accountBalance - _amount; - _totalSupplyById[_id] -= _amount; } emit TransferSingle(msg.sender, _account, address(0), _id, _amount); } diff --git a/hardhat.config.ts b/hardhat.config.ts index d5dd2efc..2486caaf 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -39,6 +39,13 @@ const config: HardhatUserConfig = { accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], timeout: 60000, }, + arb_sepolia: { + chainId: 421614, + gasPrice: 15_000_000_000, + url: process.env.ARBITRUM_SEPOLIA_URL || '', + accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], + timeout: 60000, + }, arb_one: { chainId: 42161, url: process.env.ARBITRUM_ONE_URL || '', @@ -47,8 +54,8 @@ const config: HardhatUserConfig = { }, scrollSepolia: { chainId: 534351, + gasPrice: 2_000_000_000, url: "https://sepolia-rpc.scroll.io/" || "", - gasPrice: 1_500_000_000, accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], }, op_goerli: { @@ -63,17 +70,26 @@ const config: HardhatUserConfig = { apiKey: { arbitrumOne: process.env.ARBITRUM_ONE_API_KEY, arbitrumGoerli: process.env.ARBITRUM_GOERLI_API_KEY, - scrollSepolia: 'D62920783A4311EE9D6600155D570C742E', + arb_sepolia: process.env.ARBITRUM_SEPOLIA_API_KEY, + scrollSepolia: process.env.SCROLL_SEPOLIA_API_KEY, }, customChains: [ { network: 'scrollSepolia', chainId: 534351, urls: { - apiURL: 'https://api-sepolia.scrollscan.dev/api', - browserURL: 'https://api-sepolia.scrollscan.dev', + apiURL: 'https://api-sepolia.scrollscan.com/api', + browserURL: 'https://sepolia.scrollscan.com/', }, }, + { + network: 'arb_sepolia', + chainId: 421614, + urls: { + apiURL: 'https://api-sepolia.arbiscan.io/api', + browserURL: 'https://sepolia.arbiscan.io/', + }, + }, ], }, } diff --git a/scripts/autogen/contract-deployments-keys.ts b/scripts/autogen/contract-deployments-keys.ts index 952c1462..614c0c65 100644 --- a/scripts/autogen/contract-deployments-keys.ts +++ b/scripts/autogen/contract-deployments-keys.ts @@ -2,7 +2,91 @@ import { ContractDeploymentsKey } from '../util/files/contractDeploymentsJson' export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ { - networkName: 'arb_one', + networkName: 'arb_sepolia', + objectName: 'token0' + }, + { + networkName: 'arb_sepolia', + objectName: 'weth9' + }, + { + networkName: 'arb_sepolia', + objectName: 'tickMapLib' + }, + { + networkName: 'arb_sepolia', + objectName: 'ticksLib' + }, + { + networkName: 'arb_sepolia', + objectName: 'limitPositionsLib' + }, + { + networkName: 'arb_sepolia', + objectName: 'limitPoolManager' + }, + { + networkName: 'arb_sepolia', + objectName: 'limitPoolFactory' + }, + { + networkName: 'arb_sepolia', + objectName: 'swapCall' + }, + { + networkName: 'arb_sepolia', + objectName: 'mintRangeCall' + }, + { + networkName: 'arb_sepolia', + objectName: 'burnRangeCall' + }, + { + networkName: 'arb_sepolia', + objectName: 'mintLimitCall' + }, + { + networkName: 'arb_sepolia', + objectName: 'burnLimitCall' + }, + { + networkName: 'arb_sepolia', + objectName: 'snapshotLimitCall' + }, + { + networkName: 'arb_sepolia', + objectName: 'quoteCall' + }, + { + networkName: 'arb_sepolia', + objectName: 'feesCall' + }, + { + networkName: 'arb_sepolia', + objectName: 'sampleCall' + }, + { + networkName: 'arb_sepolia', + objectName: 'snapshotRangeCall' + }, + { + networkName: 'arb_sepolia', + objectName: 'limitPoolImpl' + }, + { + networkName: 'arb_sepolia', + objectName: 'positionERC1155' + }, + { + networkName: 'arb_sepolia', + objectName: 'limitPool' + }, + { + networkName: 'arb_sepolia', objectName: 'poolRouter' }, + { + networkName: 'arb_sepolia', + objectName: 'rangeStaker' + }, ]; \ No newline at end of file diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index c100a057..ee7de76b 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -12,11 +12,11 @@ }, "token1": { "contractName": "Token20", - "contractAddress": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", + "contractAddress": "0x903CA00944D0b51e50D9F4fC96167c89F211542A", "constructorArguments": [ - "USD Coin", - "USDC", - 6 + "Poolshark", + "FIN", + 18 ], "created": "2023-08-23T15:56:24.723Z" }, @@ -56,144 +56,144 @@ }, "tickMapLib": { "contractName": "TickMap", - "contractAddress": "0x585006e3f0c9f7630d93c32d723c14f42464c5c2", + "contractAddress": "0xf682a699eac19d8dc7c80f3d3239abea981407e2", "constructorArguments": [], - "created": "2023-12-29T21:39:34.704Z" + "created": "2024-01-08T04:21:38.601Z" }, "ticksLib": { "contractName": "Ticks", - "contractAddress": "0xa294993ec6f2e76891ea25610edfd39555dfbaf9", + "contractAddress": "0x29fca90fa30b696cdaa9068b75fa46114647121c", "constructorArguments": [], - "created": "2023-12-29T21:39:36.032Z" + "created": "2024-01-08T04:21:44.314Z" }, "limitPositionsLib": { "contractName": "LimitPositions", - "contractAddress": "0x5288b2873578d01244228f5219f068c7a8438cf6", + "contractAddress": "0x6620cd0c464c380cf02f8fe60e2d522e61856390", "constructorArguments": [], - "created": "2023-12-29T21:39:37.228Z" + "created": "2024-01-08T04:21:49.836Z" }, "limitPoolManager": { "contractName": "LimitPoolManager", - "contractAddress": "0x6b1ffc44ec8ed486843640b66180d5ee366fee9f", + "contractAddress": "0xc773049bf7131a5ddd85daded4397a1fd4264a3d", "constructorArguments": [], - "created": "2023-12-29T21:39:38.177Z" + "created": "2024-01-08T04:21:51.713Z" }, "limitPoolFactory": { "contractName": "LimitPoolFactory", - "contractAddress": "0x40e21ce13f5089bc421c063506bbb0df02bca07e", + "contractAddress": "0x8bb5db1625adb4ae4beb94a188d33062303f8fb7", "constructorArguments": [ - "0x6b1ffC44ec8eD486843640B66180d5eE366Fee9f" + "0xC773049bF7131A5Ddd85DadED4397a1fd4264a3D" ], - "created": "2023-12-29T21:39:39.294Z" + "created": "2024-01-08T04:21:52.913Z" }, "swapCall": { "contractName": "SwapCall", - "contractAddress": "0x010c25d8aac4f529c65182959f38897a57e784a8", + "contractAddress": "0x3ec67dc00ba6672e9ab03d3942beee3279029af6", "constructorArguments": [], - "created": "2023-12-29T21:39:40.293Z" + "created": "2024-01-08T04:21:58.377Z" }, "mintRangeCall": { "contractName": "MintRangeCall", - "contractAddress": "0xc2c8a6c8f95f6a587f2aeabfb702297ef910227d", + "contractAddress": "0x61ece2c7b9e48d8d32e35238c488a592f0fe1bc4", "constructorArguments": [], - "created": "2023-12-29T21:39:41.426Z" + "created": "2024-01-08T04:22:04.134Z" }, "burnRangeCall": { "contractName": "BurnRangeCall", - "contractAddress": "0xf4bbf76323b4ce1d2aa6f495ad688b4f57439e25", + "contractAddress": "0x9e5791c31fc427ff2e90727aa08cf88dfe0e956c", "constructorArguments": [], - "created": "2023-12-29T21:39:42.507Z" + "created": "2024-01-08T04:22:09.951Z" }, "mintLimitCall": { "contractName": "MintLimitCall", - "contractAddress": "0xa5b68a50e74abbe7da5e6e2660c480f26caf2d74", + "contractAddress": "0x1bbe2d602da38077708544dce2b0c9b3309944de", "constructorArguments": [], - "created": "2023-12-29T21:39:43.549Z" + "created": "2024-01-08T04:22:15.780Z" }, "burnLimitCall": { "contractName": "BurnLimitCall", - "contractAddress": "0xc12d5c3a00a4d1d7d294be93485251de990f7e23", + "contractAddress": "0xaa786d3a63a03cdd2d5857012276b3ef9d1dbfb5", "constructorArguments": [], - "created": "2023-12-29T21:39:44.549Z" + "created": "2024-01-08T04:22:21.430Z" }, "snapshotLimitCall": { "contractName": "SnapshotLimitCall", - "contractAddress": "0x8c0b0083edd1f7a4026bd55d3dbd674c08eedf39", + "contractAddress": "0x248fef9576bbdd1b961d8df4c5812fa0288308d2", "constructorArguments": [], - "created": "2023-12-29T21:39:45.575Z" + "created": "2024-01-08T04:22:27.137Z" }, "quoteCall": { "contractName": "QuoteCall", - "contractAddress": "0xbcb404012d1deeda64ada405421638d2a6caa329", + "contractAddress": "0x54dabefa8feada05b8c00b6c8591511e86db2892", "constructorArguments": [], - "created": "2023-12-29T21:39:46.571Z" + "created": "2024-01-08T04:22:32.825Z" }, "feesCall": { "contractName": "FeesCall", - "contractAddress": "0x88366b13c8d6b9a5dc20e179861753551a894462", + "contractAddress": "0x32e40d1f6de8ec0de03a854b954c59e152f7d4b6", "constructorArguments": [], - "created": "2023-12-29T21:39:47.550Z" + "created": "2024-01-08T04:22:33.851Z" }, "sampleCall": { "contractName": "SampleCall", - "contractAddress": "0xc84c636c328224ecbc77fb76c37bc0cab2603f49", + "contractAddress": "0x2b9fae71f0f0175904583351a03207f476602f66", "constructorArguments": [], - "created": "2023-12-29T21:39:48.553Z" + "created": "2024-01-08T04:22:39.184Z" }, "snapshotRangeCall": { "contractName": "SnapshotRangeCall", - "contractAddress": "0x4b64c1b178f77834a25375a9d908c0d8e2279f1f", + "contractAddress": "0xa2a22ea722317d8cd338345ed7cba99dcab5ec7b", "constructorArguments": [], - "created": "2023-12-29T21:39:49.578Z" + "created": "2024-01-08T04:22:40.812Z" }, "limitPoolImpl": { "contractName": "LimitPool", - "contractAddress": "0x23105bead9cc26a01946f4b8b3477a3876476640", + "contractAddress": "0xb9a8cf9d5f31ba815ddaeba4098a40e643318826", "constructorArguments": [ - "0x40E21Ce13f5089Bc421c063506bbB0dF02BcA07E" + "0x8bB5Db1625ADB4ae4bEb94A188D33062303F8Fb7" ], - "created": "2023-12-29T21:39:50.897Z" + "created": "2024-01-08T04:22:46.296Z" }, "positionERC1155": { "contractName": "PositionERC1155", - "contractAddress": "0x6c4e29e567c7fbc3aa6710fa58b8c5a60309e632", + "contractAddress": "0x0d8b0e3ab967b3cb34af0e5321490debe6597737", "constructorArguments": [ - "0x40E21Ce13f5089Bc421c063506bbB0dF02BcA07E" + "0x8bB5Db1625ADB4ae4bEb94A188D33062303F8Fb7" ], - "created": "2023-12-29T21:39:52.094Z" + "created": "2024-01-08T04:22:52.040Z" }, "limitPool": { "contractName": "LimitPool", - "contractAddress": "0xe80dc9a69853483c745f8e32162f0bd5813cb291", + "contractAddress": "0xa43ddbcc4b78512c316bd7091b4c60f06db0fe42", "constructorArguments": [ "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", - "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", - "1000", + "0x903CA00944D0b51e50D9F4fC96167c89F211542A", + "3000", 0 ], - "created": "2023-12-29T21:39:56.337Z" + "created": "2024-01-08T04:22:55.562Z" }, "poolRouter": { "contractName": "PoolsharkRouter", - "contractAddress": "0x5357308a5a4db6c65fa6c4b122dfc3d749f2dd6a", + "contractAddress": "0x12b7a6dd3a3dfde6a0f112a1bd876f704d933915", "constructorArguments": [ - "0x40e21ce13f5089bc421c063506bbb0df02bca07e", + "0x8bb5db1625adb4ae4beb94a188d33062303f8fb7", "0x0000000000000000000000000000000000000000", "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" ], - "created": "2024-01-05T14:07:58.154Z" + "created": "2024-01-08T13:48:09.773Z" }, "rangeStaker": { "contractName": "RangeStaker", - "contractAddress": "0x7c6a8fffaedc60cb6709bd911c577619d32664e0", + "contractAddress": "0x0e2b069fa52064a7e0b5a044ba25142203210a13", "constructorArguments": [ { - "limitPoolFactory": "0x40e21ce13f5089bc421c063506bbb0df02bca07e", + "limitPoolFactory": "0x8bb5db1625adb4ae4beb94a188d33062303f8fb7", "startTime": 0, "endTime": 2000707154 } ], - "created": "2023-12-29T21:41:33.692Z" + "created": "2024-01-08T04:23:06.825Z" } }, "arb_goerli": { @@ -422,128 +422,128 @@ }, "tickMapLib": { "contractName": "TickMap", - "contractAddress": "0x1fc002005790e473b2cd4f93f1d24a20432da75d", + "contractAddress": "0x3788ce0817512e33130b1ea0490d2957b4ffc406", "constructorArguments": [], - "created": "2023-09-27T23:07:01.909Z" + "created": "2024-01-08T22:52:07.199Z" }, "ticksLib": { "contractName": "Ticks", - "contractAddress": "0x0bdcd4d288eb481dd3cd81a2a35ad68cd5ded30d", - "constructorArguments": [], - "created": "2023-09-27T23:07:07.419Z" - }, - "rangePositionsLib": { - "contractName": "RangePositions", - "contractAddress": "0x29f6547dd4ba11e169173c8d210db7138d09054f", + "contractAddress": "0xeb590779e7f68e12a48f8b146252a0b0200df16f", "constructorArguments": [], - "created": "2023-09-27T23:07:16.463Z" + "created": "2024-01-08T22:52:13.123Z" }, "limitPositionsLib": { "contractName": "LimitPositions", - "contractAddress": "0x599372abed45a59140d85cc25618381103c245fa", + "contractAddress": "0x3b76c880dbf5f660dc0c8290435d7f85e4fa8e2b", "constructorArguments": [], - "created": "2023-09-27T23:07:22.458Z" + "created": "2024-01-08T22:52:18.542Z" }, "limitPoolManager": { "contractName": "LimitPoolManager", - "contractAddress": "0x4e2f0a57ed373261451c1ef30215232be352020e", + "contractAddress": "0x7113aec7595accbaea1e61bc59f97e35d3a87bd5", "constructorArguments": [], - "created": "2023-09-27T23:07:27.645Z" + "created": "2024-01-08T22:52:23.767Z" }, "limitPoolFactory": { "contractName": "LimitPoolFactory", - "contractAddress": "0x55feb5281c4097142870745462775b93c5eef412", + "contractAddress": "0xd506193e48f13438e35a1edc4ef5394876b5efa4", "constructorArguments": [ - "0x4e2f0a57eD373261451C1ef30215232bE352020E" + "0x7113AEC7595aCcbaEa1E61Bc59F97e35D3a87bD5" ], - "created": "2023-09-27T23:07:32.660Z" + "created": "2024-01-08T22:52:28.806Z" }, "swapCall": { "contractName": "SwapCall", - "contractAddress": "0x139e58aca96cd776e3d8e3e20dfc30154c485b46", + "contractAddress": "0xf71889221f81e4475285c2b96dbf04efda194d56", "constructorArguments": [], - "created": "2023-09-27T23:07:37.787Z" + "created": "2024-01-08T22:52:33.873Z" }, "mintRangeCall": { "contractName": "MintRangeCall", - "contractAddress": "0x358242aac3e75da72a4c091c091c80a84f115966", + "contractAddress": "0x86d7513c703767595a2b33f6379e2a7a591f3c6e", "constructorArguments": [], - "created": "2023-09-27T23:07:46.994Z" + "created": "2024-01-08T22:52:39.404Z" }, "burnRangeCall": { "contractName": "BurnRangeCall", - "contractAddress": "0x5f7d98a4443517c3928cb925d7202412b1e34886", + "contractAddress": "0x69781d0ef32aef5052def4da479523536a1e31f8", "constructorArguments": [], - "created": "2023-09-27T23:07:52.436Z" + "created": "2024-01-08T22:52:44.921Z" }, "mintLimitCall": { "contractName": "MintLimitCall", - "contractAddress": "0xef8be53e21994fa411c1a6fb9cbc9c8ad6125f52", + "contractAddress": "0xe25140cb94de68b0ee88342463e64a9294597ed3", "constructorArguments": [], - "created": "2023-09-27T23:08:01.808Z" + "created": "2024-01-08T22:52:50.768Z" }, "burnLimitCall": { "contractName": "BurnLimitCall", - "contractAddress": "0x33df95efe07a3b3e69ba31438ae511d360d89b32", + "contractAddress": "0x30eca6e3b3df59b00f8b6530d0e82c8640a5bdce", "constructorArguments": [], - "created": "2023-09-27T23:08:11.042Z" + "created": "2024-01-08T22:52:56.294Z" }, "snapshotLimitCall": { "contractName": "SnapshotLimitCall", - "contractAddress": "0x62e0671022af1b2e705f08b282767c57d29c7c4c", + "contractAddress": "0x6d4c6630bd48d0a048c5ee7167287df283c0ce1f", "constructorArguments": [], - "created": "2023-09-27T23:08:20.200Z" + "created": "2024-01-08T22:53:01.697Z" }, "quoteCall": { "contractName": "QuoteCall", - "contractAddress": "0xcd453b942f35adf0364d89c05a892518825c1c3b", + "contractAddress": "0x965ab254e03e0faa6c4bcc88d0385b9a52069f57", "constructorArguments": [], - "created": "2023-09-27T23:08:29.653Z" + "created": "2024-01-08T22:53:07.368Z" }, "feesCall": { "contractName": "FeesCall", - "contractAddress": "0x61e80b7f57f3613ce30153c85d29ad571e7e9032", + "contractAddress": "0xe404640ff22538f0725773fb87894870a2e7d0b1", "constructorArguments": [], - "created": "2023-09-27T23:08:38.692Z" + "created": "2024-01-08T22:53:12.421Z" }, "sampleCall": { "contractName": "SampleCall", - "contractAddress": "0x5f6de12756b3e6df4db21bcc4855b4eb52a5d794", + "contractAddress": "0x589b1a293d4797ff467200897c7e61ede8f445ae", "constructorArguments": [], - "created": "2023-09-27T23:08:47.875Z" + "created": "2024-01-08T22:53:17.502Z" }, "snapshotRangeCall": { "contractName": "SnapshotRangeCall", - "contractAddress": "0xd47965e2357f60b4e2062df945afdcc655b5ce3a", + "contractAddress": "0xcf0c39c21dcf16fd9ce84c2cea314c05c96d0f09", "constructorArguments": [], - "created": "2023-09-27T23:08:53.010Z" + "created": "2024-01-08T22:53:22.612Z" }, "limitPoolImpl": { "contractName": "LimitPool", - "contractAddress": "0x0d4371c6d53e10986ba23929e02de11e60ba3b3c", + "contractAddress": "0xcf5784a9f8c8b0604a63c79c71460934f6d62ef6", "constructorArguments": [ - "0x55FEB5281C4097142870745462775B93C5EeF412" + "0xd506193e48f13438e35A1Edc4ef5394876b5eFA4" ], - "created": "2023-09-27T23:09:02.338Z" + "created": "2024-01-08T22:53:28.030Z" }, "positionERC1155": { "contractName": "PositionERC1155", - "contractAddress": "0xed0ef8101900dfed208ac99ce47b723debb6bfe6", + "contractAddress": "0x581b8c5f68c68219e15ca9258e03f92ae35320e8", "constructorArguments": [ - "0x55FEB5281C4097142870745462775B93C5EeF412" + "0xd506193e48f13438e35A1Edc4ef5394876b5eFA4" ], - "created": "2023-09-27T23:09:11.427Z" + "created": "2024-01-08T22:53:28.940Z" }, "limitPool": { "contractName": "LimitPool", - "contractAddress": "0xA3374d366C33E803A3fF4a3db4B38e5Aa4A1f2E5", + "contractAddress": "0xea578474c65f859739c8ec362543eedc04f0d48e", "constructorArguments": [ - "0x434f4e5354414e542d50524f4455435400000000000000000000000000000000", - "0x26323C7e4F000227932d3ed640EA79D6027701DE", - "0x912c3e7d140e4EC85eb8e3910447ACDA8654D523", - "500" + "0x681cfAC3f265b6041FF4648A1CcB214F1c0DcF38", + "0xa9e1ab5e6878621F80E03A4a5F8FB3705F4FFA2B", + "3000", + 0 ], - "created": "2023-09-27T23:09:50.038Z" + "created": "2024-01-08T22:53:41.198Z" + }, + "weth9": { + "contractName": "WETH9", + "contractAddress": "0xf04bf4e3e8157ba5b91bfda16e21be770e7ac790", + "constructorArguments": [], + "created": "2024-01-08T22:55:37.005Z" }, "coverPoolFactory": { "contractName": "CoverPoolFactory", @@ -555,13 +555,202 @@ }, "poolRouter": { "contractName": "PoolsharkRouter", - "contractAddress": "0xf9aa2fb38bf96903b578cc98e6579b5d96d37b89", + "contractAddress": "0x14ce728bf96d5ec0976f332605fe80a42ec0b244", "constructorArguments": [ - "0x55feb5281c4097142870745462775b93c5eef412", - "0x4b0288914ea8ab81da888e74f93da0062212ddcc" + "0xd506193e48f13438e35a1edc4ef5394876b5efa4", + "0x4b0288914ea8ab81da888e74f93da0062212ddcc", + "0xf04bf4e3e8157ba5b91bfda16e21be770e7ac790" ], - "created": "2023-10-02T23:25:47.853Z" + "created": "2024-01-08T13:48:09.773Z" + }, + "rangeStaker": { + "contractName": "RangeStaker", + "contractAddress": "0x83eea57c3536a7ec64ead2c67d5b6bfeb6593720", + "constructorArguments": [ + { + "limitPoolFactory": "0xd506193e48f13438e35a1edc4ef5394876b5efa4", + "startTime": 0, + "endTime": 2000707154 + } + ], + "created": "2024-01-08T22:55:37.005Z" } }, - "undefined": {} + "arb_sepolia": { + "weth9": { + "contractName": "WETH9", + "contractAddress": "0x414b73f989e7ca0653b5c98186749a348405e6d5", + "constructorArguments": [], + "created": "2024-01-09T01:02:26.457Z" + }, + "token0": { + "contractName": "Token20", + "contractAddress": "0xcd453b942f35adf0364d89c05a892518825c1c3b", + "constructorArguments": [ + "Dai Stablecoin", + "DAI", + 18 + ], + "created": "2024-01-09T00:58:39.313Z" + }, + "token1": { + "contractName": "Token20", + "contractAddress": "0x414b73f989e7ca0653b5c98186749a348405e6d5", + "constructorArguments": [ + "Wrapped Ether", + "WETH", + 18 + ], + "created": "2024-01-09T00:58:39.323Z" + }, + "tickMapLib": { + "contractName": "TickMap", + "contractAddress": "0x96c2815f7c750eb15d8d85c522dff85eadaa1cd5", + "constructorArguments": [], + "created": "2024-01-09T01:02:27.708Z" + }, + "ticksLib": { + "contractName": "Ticks", + "contractAddress": "0xd190cf03ec10dec87f51a18285af3851815505e8", + "constructorArguments": [], + "created": "2024-01-09T01:02:29.163Z" + }, + "limitPositionsLib": { + "contractName": "LimitPositions", + "contractAddress": "0x4b6918454f16c991ee5e090940f9209eed3b7097", + "constructorArguments": [], + "created": "2024-01-09T01:02:30.316Z" + }, + "coverPoolFactory": { + "contractName": "CoverPoolFactory", + "contractAddress": "0x0000000000000000000000000000000000000000", + "constructorArguments": [ + "0xF76088F4C5Ad2a138f68870449f019e0E93E1510" + ], + "created": "2023-09-25T01:59:46.159Z" + }, + "limitPoolManager": { + "contractName": "LimitPoolManager", + "contractAddress": "0x0385d0a8d169b1f1510bb5fa0be264463002f6f6", + "constructorArguments": [], + "created": "2024-01-09T01:02:31.636Z" + }, + "limitPoolFactory": { + "contractName": "LimitPoolFactory", + "contractAddress": "0x8e40c68b7546efd009a1a300c92e25da3c8725dc", + "constructorArguments": [ + "0x0385d0a8d169B1F1510BB5fa0be264463002F6f6" + ], + "created": "2024-01-09T01:02:32.869Z" + }, + "swapCall": { + "contractName": "SwapCall", + "contractAddress": "0xe0c1e03b85b22e10718cb20c3e91dd34dd73905e", + "constructorArguments": [], + "created": "2024-01-09T01:02:34.301Z" + }, + "mintRangeCall": { + "contractName": "MintRangeCall", + "contractAddress": "0x38cb66ea3d5b5341201e6a0b1b150660880e7d5d", + "constructorArguments": [], + "created": "2024-01-09T01:02:35.557Z" + }, + "burnRangeCall": { + "contractName": "BurnRangeCall", + "contractAddress": "0xe35d5aa1bac86d9eeaef63e7013094192e57ed21", + "constructorArguments": [], + "created": "2024-01-09T01:02:36.767Z" + }, + "mintLimitCall": { + "contractName": "MintLimitCall", + "contractAddress": "0xc2271a012fbba8098e569be9fa893a1255d73b0f", + "constructorArguments": [], + "created": "2024-01-09T01:02:38.031Z" + }, + "burnLimitCall": { + "contractName": "BurnLimitCall", + "contractAddress": "0x4372ee46dfb52d271139fd40aeda61c5da0c35e5", + "constructorArguments": [], + "created": "2024-01-09T01:02:39.240Z" + }, + "snapshotLimitCall": { + "contractName": "SnapshotLimitCall", + "contractAddress": "0x1fc002005790e473b2cd4f93f1d24a20432da75d", + "constructorArguments": [], + "created": "2024-01-09T01:02:40.378Z" + }, + "quoteCall": { + "contractName": "QuoteCall", + "contractAddress": "0x0bdcd4d288eb481dd3cd81a2a35ad68cd5ded30d", + "constructorArguments": [], + "created": "2024-01-09T01:02:41.639Z" + }, + "feesCall": { + "contractName": "FeesCall", + "contractAddress": "0x29f6547dd4ba11e169173c8d210db7138d09054f", + "constructorArguments": [], + "created": "2024-01-09T01:02:42.855Z" + }, + "sampleCall": { + "contractName": "SampleCall", + "contractAddress": "0x599372abed45a59140d85cc25618381103c245fa", + "constructorArguments": [], + "created": "2024-01-09T01:02:44.139Z" + }, + "snapshotRangeCall": { + "contractName": "SnapshotRangeCall", + "contractAddress": "0x4e2f0a57ed373261451c1ef30215232be352020e", + "constructorArguments": [], + "created": "2024-01-09T01:02:45.390Z" + }, + "limitPoolImpl": { + "contractName": "LimitPool", + "contractAddress": "0x55feb5281c4097142870745462775b93c5eef412", + "constructorArguments": [ + "0x8E40c68b7546efd009a1A300C92e25Da3c8725dc" + ], + "created": "2024-01-09T01:02:46.629Z" + }, + "positionERC1155": { + "contractName": "PositionERC1155", + "contractAddress": "0x139e58aca96cd776e3d8e3e20dfc30154c485b46", + "constructorArguments": [ + "0x8E40c68b7546efd009a1A300C92e25Da3c8725dc" + ], + "created": "2024-01-09T01:02:47.732Z" + }, + "limitPool": { + "contractName": "LimitPool", + "contractAddress": "0x02225f6a3d83648d7906a23856331c819265394d", + "constructorArguments": [ + "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", + "0x414b73f989e7ca0653b5c98186749a348405e6d5", + "3000", + 0 + ], + "created": "2024-01-09T01:02:52.948Z" + }, + "poolRouter": { + "contractName": "PoolsharkRouter", + "contractAddress": "0x33df95efe07a3b3e69ba31438ae511d360d89b32", + "constructorArguments": [ + "0x8e40c68b7546efd009a1a300c92e25da3c8725dc", + "0x0000000000000000000000000000000000000000", + "0xfa807ce77b103129597cce4a7bf7f504f9e7bd9e" + ], + "created": "2024-01-09T01:02:54.485Z" + }, + "rangeStaker": { + "contractName": "RangeStaker", + "contractAddress": "0x62e0671022af1b2e705f08b282767c57d29c7c4c", + "constructorArguments": [ + { + "limitPoolFactory": "0x8e40c68b7546efd009a1a300c92e25da3c8725dc", + "startTime": 0, + "endTime": 2000707154 + } + ], + "created": "2024-01-09T01:02:55.607Z" + } + } } \ No newline at end of file diff --git a/scripts/constants/supportedNetworks.ts b/scripts/constants/supportedNetworks.ts index 4ed8b12d..9f2c0fce 100644 --- a/scripts/constants/supportedNetworks.ts +++ b/scripts/constants/supportedNetworks.ts @@ -5,6 +5,7 @@ export enum SUPPORTED_NETWORKS { /* Testnet Supported Networks */ GOERLI = 'goerli', ARB_GOERLI = 'arb_goerli', + ARB_SEPOLIA = 'arb_sepolia', ARB_ONE = 'arb_one', SCROLL_SEPOLIA = 'scrollSepolia' } @@ -12,6 +13,7 @@ export enum SUPPORTED_NETWORKS { export enum TESTNET_NETWORKS { GOERLI = 'goerli', ARB_GOERLI = 'arb_goerli', + ARB_SEPOLIA = 'arb_sepolia', SCROLL_SEPOLIA = 'scrollSepolia' } diff --git a/subgraph/package.json b/subgraph/package.json index 5106bc7b..f2fb6739 100644 --- a/subgraph/package.json +++ b/subgraph/package.json @@ -26,8 +26,11 @@ "deploy-op-test": "graph deploy --product hosted-service alphak3y/poolshark-cover-op-goerli", "deploy-hs": "graph deploy --product hosted-service alphak3y/poolshark-limit", "deploy-local": "graph deploy example --ipfs http://localhost:5001 --node http://127.0.0.1:8020", + "deploy-arb-sepolia": "graph deploy --version-label v0.1.0 --node https://api.graph-ams.p2pify.com/5e506be8e1163614d8b8d978acfdeca7/deploy --ipfs https://api.graph-ams.p2pify.com/5e506be8e1163614d8b8d978acfdeca7/ipfs limit-arbitrum-sepolia", "deploy-arb-one": "graph deploy --version-label v0.1.1 --node https://api.graph-ams.p2pify.com/e57bc8ecd57c8bac9bfc6222cb8257e7/deploy --ipfs https://api.graph-ams.p2pify.com/e57bc8ecd57c8bac9bfc6222cb8257e7/ipfs limit-arbitrum", - "deploy-arb-one-2": "graph deploy --version-label v0.1.1 --node https://api.graph-ams.p2pify.com/1600f38da0b118b394f0184f131232a1/deploy --ipfs https://api.graph-ams.p2pify.com/1600f38da0b118b394f0184f131232a1/ipfs limit-arbitrum-redeploy", + "deploy-arb-one-2": "graph deploy --version-label v0.1.2 --node https://api.graph-ams.p2pify.com/1600f38da0b118b394f0184f131232a1/deploy --ipfs https://api.graph-ams.p2pify.com/1600f38da0b118b394f0184f131232a1/ipfs limit-arbitrum-redeploy", + "deploy-scroll": "graph deploy --product hosted-service alphak3y/poolshark-limit-scroll-mainnet", + "deploy-scroll-sepolia": "graph deploy --product hosted-service alphak3y/limit-scroll-sepolia", "test": "graph codegen; graph test -v 0.2.0", "prepare:subgraph": "mustache ./config/${NETWORK}.json ./templates/${BLOCKCHAIN}.subgraph.template.yaml > subgraph.yaml && rm -rf generated && npm run generate:schema && npm run subgraph:codegen && npm run subgraph:build", "subgraph:deploy:chainstack:test": "graph deploy --version-label v0.3.1 --node https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/deploy --ipfs https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/ipfs limit-arbitrumGoerli-test", diff --git a/subgraph/src/constants/arbitrum-goerli.ts b/subgraph/src/constants/arbitrum-goerli.ts index 5df8faa0..b2760092 100644 --- a/subgraph/src/constants/arbitrum-goerli.ts +++ b/subgraph/src/constants/arbitrum-goerli.ts @@ -29,7 +29,7 @@ export let STABLE_COINS: string[] = [ export const STABLE_POOL_ADDRESS = '0x4998545e13a668a884272aaebff14ab21c5b4e89' // determines which token to use for eth <-> usd rate, true means stable is token0 in pool above -export const STABLE_IS_TOKEN_0 = true +export const STABLE_IS_TOKEN_0 = false // minimum eth required in pool to count usd values towards global prices export let MINIMUM_ETH_LOCKED = BigDecimal.fromString('0') diff --git a/subgraph/src/constants/arbitrum-sepolia.ts b/subgraph/src/constants/arbitrum-sepolia.ts new file mode 100644 index 00000000..8a3c5f1f --- /dev/null +++ b/subgraph/src/constants/arbitrum-sepolia.ts @@ -0,0 +1,52 @@ +/* eslint-disable */ +import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts' +import { LimitPoolFactory as FactoryContract } from '../../generated/templates/LimitPoolTemplate/LimitPoolFactory' + +// -------------- START REQUIRED CONFIG PER DEPLOYMENT -------------- +export let FACTORY_ADDRESS = '0x8e40c68b7546efd009a1a300c92e25da3c8725dc' +export let RANGE_STAKER_ADDRESS = '0x62e0671022af1b2e705f08b282767c57d29c7c4c' +// used for safe eth pricing +export const STABLE_POOL_ADDRESS = '0x02225f6a3d83648d7906a23856331c819265394d' +// -------------- END REQUIRED CONFIG PER DEPLOYMENT -------------- + +// determines which token to use for eth <-> usd rate, true means stable is token0 in pool above +export const STABLE_IS_TOKEN_0 = true + +// chain WETH address +export let WETH_ADDRESS = '0x414b73f989e7ca0653b5c98186749a348405e6d5' + +// tokens where USD value is safe to use for globals +export let WHITELISTED_TOKENS: string[] = [ + '0x414b73f989e7ca0653b5c98186749a348405e6d5', // WETH + '0x9f479560cd8a531e6c0fe04521cb246264fe6b71', // DAI +] + +//TODO: incentivizing only FIN - WETH 0.3% for now + +export let WHITELISTED_PAIRS: string[] = [ +] + +export let SEASON_1_START_TIME = BigInt.fromString('1574345600') // 11-1-2019 0:00 GMT +export let SEASON_1_END_TIME = BigInt.fromString('1684713600') // 5-22-2024 0:00 GMT + +// used for safe eth pricing +export let STABLE_COINS: string[] = [ + '0x9f479560cd8a531e6c0fe04521cb246264fe6b71', // DAI +] + +// minimum eth required in pool to count usd values towards global prices +export let MINIMUM_ETH_LOCKED = BigDecimal.fromString('0') + +// pool that breaks with subgraph logic +export const ERROR_POOL = '0x0000000000000000000000000000000000000000' + +export let ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' + +export let ZERO_BI = BigInt.fromI32(0) +export let ONE_BI = BigInt.fromI32(1) +export let ZERO_BD = BigDecimal.fromString('0') +export let ONE_BD = BigDecimal.fromString('1') +export let TWO_BD = BigDecimal.fromString('2') +export let BI_18 = BigInt.fromI32(18) + +export let factoryContract = FactoryContract.bind(Address.fromString(FACTORY_ADDRESS)) \ No newline at end of file diff --git a/subgraph/src/constants/arbitrum.ts b/subgraph/src/constants/arbitrum.ts index c7fc1cfc..631894c9 100644 --- a/subgraph/src/constants/arbitrum.ts +++ b/subgraph/src/constants/arbitrum.ts @@ -3,8 +3,8 @@ import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts' import { LimitPoolFactory as FactoryContract } from '../../generated/templates/LimitPoolTemplate/LimitPoolFactory' // -------------- START REQUIRED CONFIG PER DEPLOYMENT -------------- -export let FACTORY_ADDRESS = '0xd28d620853af6837d76f1360dc65229d57ba5435' -export let RANGE_STAKER_ADDRESS = '0x373bf43249bed0518119d29b80c7d9c68d98cfc2' +export let FACTORY_ADDRESS = '0x8bb5db1625adb4ae4beb94a188d33062303f8fb7' +export let RANGE_STAKER_ADDRESS = '0x0e2b069fa52064a7e0b5a044ba25142203210a13' // used for safe eth pricing export const STABLE_POOL_ADDRESS = '0xc31e54c7a869b9fcbecc14363cf510d1c41fa443' // -------------- END REQUIRED CONFIG PER DEPLOYMENT -------------- @@ -25,6 +25,8 @@ export let WHITELISTED_TOKENS: string[] = [ '0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9' // USDT ] +//TODO: incentivizing only FIN - WETH 0.3% for now + export let WHITELISTED_PAIRS: string[] = [ '0x82af49447d8a07e3bd95bd0d56f35241523fbab1-0xaf88d065e77c8cc2239327c5edb3a432268e5831', // WETH - USDC '0xaf88d065e77c8cc2239327c5edb3a432268e5831-0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', // USDC - USDC.e diff --git a/subgraph/src/constants/constants.ts b/subgraph/src/constants/constants.ts new file mode 100644 index 00000000..431c3f1c --- /dev/null +++ b/subgraph/src/constants/constants.ts @@ -0,0 +1,5 @@ +import { + STABLE_IS_TOKEN_0, FACTORY_ADDRESS, RANGE_STAKER_ADDRESS, STABLE_POOL_ADDRESS, WETH_ADDRESS, WHITELISTED_TOKENS, WHITELISTED_PAIRS, SEASON_1_END_TIME, SEASON_1_START_TIME, STABLE_COINS, MINIMUM_ETH_LOCKED, ERROR_POOL, ZERO_ADDRESS, ZERO_BI, ONE_BI, ONE_BD, ZERO_BD, TWO_BD, BI_18, factoryContract +} from "./arbitrum-sepolia"; + +export { STABLE_IS_TOKEN_0, FACTORY_ADDRESS, RANGE_STAKER_ADDRESS, STABLE_POOL_ADDRESS, WETH_ADDRESS, WHITELISTED_TOKENS, WHITELISTED_PAIRS, SEASON_1_END_TIME, SEASON_1_START_TIME, STABLE_COINS, MINIMUM_ETH_LOCKED, ERROR_POOL, ZERO_ADDRESS, ZERO_BI, ONE_BI, ONE_BD, ZERO_BD, TWO_BD, BI_18, factoryContract}; \ No newline at end of file diff --git a/subgraph/src/constants/scroll-sepolia.ts b/subgraph/src/constants/scroll-sepolia.ts new file mode 100644 index 00000000..156302a8 --- /dev/null +++ b/subgraph/src/constants/scroll-sepolia.ts @@ -0,0 +1,52 @@ +/* eslint-disable */ +import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts' +import { LimitPoolFactory as FactoryContract } from '../../generated/templates/LimitPoolTemplate/LimitPoolFactory' + +// -------------- START REQUIRED CONFIG PER DEPLOYMENT -------------- +export let FACTORY_ADDRESS = '0xd506193e48f13438e35a1edc4ef5394876b5efa4' +export let RANGE_STAKER_ADDRESS = '0x83eea57c3536a7ec64ead2c67d5b6bfeb6593720' +// used for safe eth pricing +export const STABLE_POOL_ADDRESS = '0xea578474c65f859739c8ec362543eedc04f0d48e' +// -------------- END REQUIRED CONFIG PER DEPLOYMENT -------------- + +// determines which token to use for eth <-> usd rate, true means stable is token0 in pool above +export const STABLE_IS_TOKEN_0 = false + +// chain WETH address +export let WETH_ADDRESS = '0xf04bf4e3e8157ba5b91bfda16e21be770e7ac790' + +// tokens where USD value is safe to use for globals +export let WHITELISTED_TOKENS: string[] = [ + '0xf04bf4e3e8157ba5b91bfda16e21be770e7ac790', // WETH + '0xda10009cbd5d07dd0cecc66161fc93d7c9000da1', // DAI +] + +//TODO: incentivizing only FIN - WETH 0.3% for now + +export let WHITELISTED_PAIRS: string[] = [ +] + +export let SEASON_1_START_TIME = BigInt.fromString('1674345600') // 1-22-2024 0:00 GMT +export let SEASON_1_END_TIME = BigInt.fromString('1684713600') // 5-22-2024 0:00 GMT + +// used for safe eth pricing +export let STABLE_COINS: string[] = [ + '0xa9e1ab5e6878621F80E03A4a5F8FB3705F4FFA2B', // DAI +] + +// minimum eth required in pool to count usd values towards global prices +export let MINIMUM_ETH_LOCKED = BigDecimal.fromString('0') + +// pool that breaks with subgraph logic +export const ERROR_POOL = '0x0000000000000000000000000000000000000000' + +export let ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' + +export let ZERO_BI = BigInt.fromI32(0) +export let ONE_BI = BigInt.fromI32(1) +export let ZERO_BD = BigDecimal.fromString('0') +export let ONE_BD = BigDecimal.fromString('1') +export let TWO_BD = BigDecimal.fromString('2') +export let BI_18 = BigInt.fromI32(18) + +export let factoryContract = FactoryContract.bind(Address.fromString(FACTORY_ADDRESS)) \ No newline at end of file diff --git a/subgraph/src/mappings/limit/burn.ts b/subgraph/src/mappings/limit/burn.ts index 3090433c..c80c0d66 100644 --- a/subgraph/src/mappings/limit/burn.ts +++ b/subgraph/src/mappings/limit/burn.ts @@ -1,6 +1,6 @@ import { store, BigInt } from "@graphprotocol/graph-ts" import { BurnLimit } from "../../../generated/LimitPoolFactory/LimitPool" -import { FACTORY_ADDRESS, ONE_BI, SEASON_1_END_TIME, SEASON_1_START_TIME } from "../../constants/arbitrum" +import { FACTORY_ADDRESS, ONE_BI, SEASON_1_END_TIME, SEASON_1_START_TIME } from "../../constants/constants" import { BIGINT_ONE, BIGINT_ZERO, convertTokenToDecimal } from "../utils/helpers" import { safeLoadBasePrice, safeLoadBurnLog, safeLoadHistoricalOrder, safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadLimitPosition, safeLoadLimitTick, safeLoadToken, safeLoadTotalSeasonReward, safeLoadTvlUpdateLog, safeLoadUserSeasonReward } from "../utils/loads" import { findEthPerToken } from "../utils/price" diff --git a/subgraph/src/mappings/limit/mint.ts b/subgraph/src/mappings/limit/mint.ts index 56f50860..184b3d90 100644 --- a/subgraph/src/mappings/limit/mint.ts +++ b/subgraph/src/mappings/limit/mint.ts @@ -3,7 +3,7 @@ import { Bytes, } from '@graphprotocol/graph-ts' import { MintLimit } from "../../../generated/LimitPoolFactory/LimitPool" -import { ONE_BI } from "../../constants/arbitrum" +import { ONE_BI } from "../../constants/constants" import { BIGDECIMAL_ZERO, BIGINT_ONE, BIGINT_ZERO, convertTokenToDecimal } from "../utils/helpers" import { safeLoadBasePrice, safeLoadHistoricalOrder, safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadLimitPosition, safeLoadLimitTick, safeLoadToken, safeLoadTvlUpdateLog } from "../utils/loads" import { findEthPerToken } from '../utils/price' diff --git a/subgraph/src/mappings/limitpoolfactory.ts b/subgraph/src/mappings/limitpoolfactory.ts index 8a626f64..8afaa4bd 100644 --- a/subgraph/src/mappings/limitpoolfactory.ts +++ b/subgraph/src/mappings/limitpoolfactory.ts @@ -9,7 +9,7 @@ import { } from './utils/helpers' import { safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadLimitPoolToken, safeLoadToken } from './utils/loads' import { BigInt } from '@graphprotocol/graph-ts' -import { FACTORY_ADDRESS, ONE_BI, RANGE_STAKER_ADDRESS, STABLE_COINS, WETH_ADDRESS } from '../constants/arbitrum' +import { FACTORY_ADDRESS, ONE_BI, RANGE_STAKER_ADDRESS, STABLE_COINS, WETH_ADDRESS } from '../constants/constants' export function handlePoolCreated(event: LimitPoolCreated): void { // grab event parameters diff --git a/subgraph/src/mappings/limitpoolmanager.ts b/subgraph/src/mappings/limitpoolmanager.ts index 25a1d61a..62aa4a88 100644 --- a/subgraph/src/mappings/limitpoolmanager.ts +++ b/subgraph/src/mappings/limitpoolmanager.ts @@ -1,6 +1,6 @@ import { safeLoadManager, safeLoadLimitPoolFactory, safeLoadFeeTier } from './utils/loads' import { BigInt, log } from '@graphprotocol/graph-ts' -import { FACTORY_ADDRESS } from '../constants/arbitrum' +import { FACTORY_ADDRESS } from '../constants/constants' import { FactoryChanged, FeeTierEnabled, FeeToTransfer, PoolTypeEnabled, OwnerTransfer, ProtocolSwapFeesModified, ProtocolFeesCollected, ProtocolFillFeesModified } from '../../generated/LimitPoolManager/LimitPoolManager' export function handleFeeTierEnabled(event: FeeTierEnabled): void { diff --git a/subgraph/src/mappings/pool/swap.ts b/subgraph/src/mappings/pool/swap.ts index b3732e43..0bfa4a01 100644 --- a/subgraph/src/mappings/pool/swap.ts +++ b/subgraph/src/mappings/pool/swap.ts @@ -1,7 +1,7 @@ import { BigDecimal, BigInt, log } from "@graphprotocol/graph-ts" import { safeLoadBasePrice, safeLoadHistoricalOrder, safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadSwap, safeLoadToken, safeLoadTotalSeasonReward, safeLoadTransaction, safeLoadTvlUpdateLog, safeLoadUserSeasonReward } from "../utils/loads" import { BIGDECIMAL_ZERO, BIGINT_ONE, BIGINT_ZERO, convertTokenToDecimal } from "../utils/helpers" -import { ZERO_BD, TWO_BD, ONE_BI, FACTORY_ADDRESS, SEASON_1_END_TIME, SEASON_1_START_TIME } from "../../constants/arbitrum" +import { ZERO_BD, TWO_BD, ONE_BI, FACTORY_ADDRESS, SEASON_1_END_TIME, SEASON_1_START_TIME } from "../../constants/constants" import { AmountType, findEthPerToken, getAdjustedAmounts, getEthPriceInUSD, sqrtPriceX96ToTokenPrices } from "../utils/price" import { updateDerivedTVLAmounts } from "../utils/tvl" import { Swap, SwapLimit } from "../../../generated/LimitPoolFactory/LimitPool" diff --git a/subgraph/src/mappings/positionerc1155.ts b/subgraph/src/mappings/positionerc1155.ts index e2de4ef1..c1f121e1 100644 --- a/subgraph/src/mappings/positionerc1155.ts +++ b/subgraph/src/mappings/positionerc1155.ts @@ -1,6 +1,6 @@ import { RouterDeployed } from "../../generated/PoolsharkRouter/PoolsharkRouter"; import { TransferBatch, TransferSingle } from "../../generated/templates/PositionERC1155Template/PositionERC1155"; -import { RANGE_STAKER_ADDRESS } from "../constants/arbitrum"; +import { RANGE_STAKER_ADDRESS } from "../constants/constants"; import { safeLoadLimitPoolToken, safeLoadLimitPosition, safeLoadPoolRouter, safeLoadRangePosition } from "./utils/loads"; export function handleTransferSingle(event: TransferSingle): void { diff --git a/subgraph/src/mappings/range/burn.ts b/subgraph/src/mappings/range/burn.ts index 826b2e7f..6ff034a2 100644 --- a/subgraph/src/mappings/range/burn.ts +++ b/subgraph/src/mappings/range/burn.ts @@ -3,7 +3,7 @@ import { safeLoadBasePrice, safeLoadBurnLog, safeLoadRangePositionById, safeLoad import { BigInt, } from '@graphprotocol/graph-ts' -import { ONE_BI } from "../../constants/arbitrum" +import { ONE_BI } from "../../constants/constants" import { updateDerivedTVLAmounts } from "../utils/tvl" import { BIGINT_ZERO, convertTokenToDecimal } from "../utils/helpers" import { findEthPerToken } from "../utils/price" diff --git a/subgraph/src/mappings/range/mint.ts b/subgraph/src/mappings/range/mint.ts index d0efde43..ab3fa56a 100644 --- a/subgraph/src/mappings/range/mint.ts +++ b/subgraph/src/mappings/range/mint.ts @@ -12,7 +12,7 @@ import { safeLoadTvlUpdateLog, } from '../utils/loads' import { BIGINT_ONE, convertTokenToDecimal } from '../utils/helpers' -import { ONE_BI } from '../../constants/arbitrum' +import { ONE_BI } from '../../constants/constants' import { updateDerivedTVLAmounts } from '../utils/tvl' import { findEthPerToken } from '../utils/price' import { MintRange } from '../../../generated/LimitPoolFactory/LimitPool' diff --git a/subgraph/src/mappings/staking/rangestaker.ts b/subgraph/src/mappings/staking/rangestaker.ts index b6c67205..032f77f1 100644 --- a/subgraph/src/mappings/staking/rangestaker.ts +++ b/subgraph/src/mappings/staking/rangestaker.ts @@ -1,6 +1,6 @@ import { safeLoadLimitPool, safeLoadRangePosition, safeLoadToken, safeLoadTotalSeasonReward, safeLoadUserSeasonReward } from '../utils/loads' import { FeeToTransfer, OwnerTransfer, StakeRange, StakeRangeAccrued, UnstakeRange } from '../../../generated/templates/RangeStakerTemplate/RangeStaker' -import { FACTORY_ADDRESS, SEASON_1_END_TIME, SEASON_1_START_TIME, WHITELISTED_PAIRS, WHITELISTED_TOKENS } from '../../constants/arbitrum' +import { FACTORY_ADDRESS, SEASON_1_END_TIME, SEASON_1_START_TIME, WHITELISTED_PAIRS, WHITELISTED_TOKENS } from '../../constants/constants' import { convertTokenToDecimal } from '../utils/helpers' export function handleFeeToTransfer(event: FeeToTransfer): void { diff --git a/subgraph/src/mappings/utils/loads.ts b/subgraph/src/mappings/utils/loads.ts index ad59ad49..e0f5785d 100644 --- a/subgraph/src/mappings/utils/loads.ts +++ b/subgraph/src/mappings/utils/loads.ts @@ -1,6 +1,6 @@ import { Address, BigDecimal, BigInt, Bytes, ethereum, log } from '@graphprotocol/graph-ts' import { LimitPool, LimitPoolFactory, LimitPoolManager, LimitPosition, Token, FeeTier, BasePrice, RangePosition, RangeTick, Transaction, LimitTick, Swap, CompoundRangeLog, MintRangeLog, BurnRangeLog, PoolRouter, TvlUpdateLog, HistoricalOrder, TotalSeasonReward, UserSeasonReward, LimitPoolToken, VFinPosition } from '../../../generated/schema' -import { ONE_BD } from '../../constants/arbitrum' +import { ONE_BD } from '../../constants/constants' import { fetchTokenSymbol, fetchTokenName, @@ -26,11 +26,13 @@ export function safeLoadBasePrice(name: string, stablePool: LimitPool | null = n } if (stablePool !== null) { + log.info("pool data: {}", [stablePool.id]) // only non-null when updating v3 pool price basePriceEntity.USD = getEthPriceInUSD(stablePool) + } else { + log.info("pool data is null", []) } - return { entity: basePriceEntity, exists: exists, diff --git a/subgraph/src/mappings/utils/math.ts b/subgraph/src/mappings/utils/math.ts index 64be669b..7835a29f 100644 --- a/subgraph/src/mappings/utils/math.ts +++ b/subgraph/src/mappings/utils/math.ts @@ -1,6 +1,6 @@ /* eslint-disable prefer-const */ import { BigInt, BigDecimal } from '@graphprotocol/graph-ts' -import { ONE_BI, ZERO_BI, ZERO_BD, ONE_BD } from '../../constants/arbitrum' +import { ONE_BI, ZERO_BI, ZERO_BD, ONE_BD } from '../../constants/constants' export function exponentToBigDecimal(decimals: BigInt): BigDecimal { let bd = BigDecimal.fromString('1') diff --git a/subgraph/src/mappings/utils/price.ts b/subgraph/src/mappings/utils/price.ts index a0a243b3..a0de90fc 100644 --- a/subgraph/src/mappings/utils/price.ts +++ b/subgraph/src/mappings/utils/price.ts @@ -1,5 +1,5 @@ /* eslint-disable prefer-const */ -import { MINIMUM_ETH_LOCKED, ONE_BD, STABLE_COINS, STABLE_IS_TOKEN_0, STABLE_POOL_ADDRESS, WETH_ADDRESS, WHITELISTED_TOKENS, ZERO_BD, ZERO_BI } from '../../constants/arbitrum' +import { MINIMUM_ETH_LOCKED, ONE_BD, STABLE_COINS, STABLE_IS_TOKEN_0, STABLE_POOL_ADDRESS, WETH_ADDRESS, WHITELISTED_TOKENS, ZERO_BD, ZERO_BI } from '../../constants/constants' import { BasePrice, LimitPool, Token } from '../../../generated/schema' import { BigDecimal, BigInt, log } from '@graphprotocol/graph-ts' import { exponentToBigDecimal, safeDiv } from './math' @@ -9,6 +9,7 @@ import { safeLoadBasePrice, safeLoadLimitPool, safeLoadToken } from './loads' export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, token1: Token): BigDecimal[] { let num = sqrtPriceX96.times(sqrtPriceX96).toBigDecimal() let Q192 = BigInt.fromI32(2).pow(192).toBigDecimal() + log.warning("before decimals", []) let price1 = num .div(Q192) .times( @@ -17,7 +18,7 @@ export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, t exponentToBigDecimal(token1.decimals) ) ); - + log.warning("after decimals", []) let price0 = BIGDECIMAL_ZERO if (price1.gt(BIGDECIMAL_ZERO)) { price0 = safeDiv(BigDecimal.fromString('1'), price1); diff --git a/subgraph/src/mappings/utils/tvl.ts b/subgraph/src/mappings/utils/tvl.ts index 3017983d..1fc7ce9e 100644 --- a/subgraph/src/mappings/utils/tvl.ts +++ b/subgraph/src/mappings/utils/tvl.ts @@ -3,7 +3,7 @@ import { BigDecimal, log } from '@graphprotocol/graph-ts' import { BasePrice, LimitPoolFactory, LimitPool, Token } from '../../../generated/schema' import { AmountType, getAdjustedAmounts } from './price' -import { ZERO_ADDRESS } from '../../constants/arbitrum' +import { ZERO_ADDRESS } from '../../constants/constants' import { safeLoadBasePrice, safeLoadToken } from './loads' diff --git a/subgraph/src/mappings/v3pool.ts b/subgraph/src/mappings/v3pool.ts index 7b504c89..89aa864e 100644 --- a/subgraph/src/mappings/v3pool.ts +++ b/subgraph/src/mappings/v3pool.ts @@ -1,23 +1,77 @@ -import { BigInt } from "@graphprotocol/graph-ts" -import { Swap, Token } from "../../generated/schema" -import { STABLE_POOL_ADDRESS } from "../constants/arbitrum" -import { STABLE_IS_TOKEN_0 } from "../constants/arbitrum-goerli" +import { BigInt, log } from "@graphprotocol/graph-ts" +import { Token } from "../../generated/schema" +import { STABLE_POOL_ADDRESS, STABLE_IS_TOKEN_0 } from "../constants/constants" import { safeLoadBasePrice, safeLoadLimitPool } from "./utils/loads" import { sqrtPriceX96ToTokenPrices } from "./utils/price" +import { Initialize, Swap } from "../../generated/v3EthUsdcPool/v3Pool" + +export function handleV3Initialize(event: Initialize): void { + let loadPool = safeLoadLimitPool(STABLE_POOL_ADDRESS) + + log.warning("swap event process", []) + + let pool = loadPool.entity + + log.warning("pool entity", []) + + pool.poolPrice = event.params.sqrtPriceX96 + + log.warning("price after", []) + + let token0: Token; let token1: Token; + + if (STABLE_IS_TOKEN_0) { + log.warning("usdc token", []) + token0 = new Token('USDC') + log.warning("usdc decimals", []) + token0.decimals = BigInt.fromString('6') + log.warning("weth token", []) + token1 = new Token('WETH') + log.warning("weth decimals", []) + token1.decimals = BigInt.fromString('18') + } else { + token0 = new Token('WETH') + token0.decimals = BigInt.fromString('18') + token1 = new Token('USDC') + token1.decimals = BigInt.fromString('6') + } + + log.warning("about to convert to prices", []) + + let prices = sqrtPriceX96ToTokenPrices(pool.poolPrice, token0, token1) + pool.price0 = prices[0] + pool.price1 = prices[1] + + log.warning("after price convert", []) + + let loadBasePrice = safeLoadBasePrice('eth', pool) + let basePrice = loadBasePrice.entity + basePrice.save() +} export function handleV3Swap(event: Swap): void { let loadPool = safeLoadLimitPool(STABLE_POOL_ADDRESS) + log.warning("swap event process", []) + let pool = loadPool.entity - pool.poolPrice = event.priceAfter + log.warning("pool entity", []) + + pool.poolPrice = event.params.sqrtPriceX96 + + log.warning("price after", []) let token0: Token; let token1: Token; if (STABLE_IS_TOKEN_0) { + log.warning("usdc token", []) token0 = new Token('USDC') + log.warning("usdc decimals", []) token0.decimals = BigInt.fromString('6') + log.warning("weth token", []) token1 = new Token('WETH') + log.warning("weth decimals", []) token1.decimals = BigInt.fromString('18') } else { token0 = new Token('WETH') @@ -26,10 +80,14 @@ export function handleV3Swap(event: Swap): void { token1.decimals = BigInt.fromString('6') } + log.warning("about to convert to prices", []) + let prices = sqrtPriceX96ToTokenPrices(pool.poolPrice, token0, token1) pool.price0 = prices[0] pool.price1 = prices[1] + log.warning("after price convert", []) + let loadBasePrice = safeLoadBasePrice('eth', pool) let basePrice = loadBasePrice.entity basePrice.save() diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 8f144686..31a6beef 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -6,7 +6,7 @@ schema: templates: - kind: ethereum/contract name: LimitPoolTemplate - network: arbitrum-one + network: arbitrum-sepolia source: abi: LimitPool mapping: @@ -57,11 +57,11 @@ templates: - event: SyncLimitTick(uint32,int24,bool) handler: handleSyncLimitTick - event: Collect(indexed address,address,indexed int24,indexed int24,uint128,uint128) - handler: handleCollectRange0 + handler: handleCollect # ERC-1155 events - kind: ethereum/contract name: PositionERC1155Template - network: arbitrum-one + network: arbitrum-sepolia source: abi: PositionERC1155 mapping: @@ -84,7 +84,7 @@ templates: # RangeStaker events - kind: ethereum/contract name: RangeStakerTemplate - network: arbitrum-one + network: arbitrum-sepolia source: abi: RangeStaker mapping: @@ -111,11 +111,11 @@ templates: dataSources: - kind: ethereum/contract name: LimitPoolFactory - network: arbitrum-one + network: arbitrum-sepolia source: - address: '0x40e21ce13f5089bc421c063506bbb0df02bca07e' + address: '0x8e40c68b7546efd009a1a300c92e25da3c8725dc' abi: LimitPoolFactory - startBlock: 165077269 + startBlock: 5789864 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -140,11 +140,11 @@ dataSources: handler: handlePoolCreated - kind: ethereum/contract name: LimitPoolManager - network: arbitrum-one + network: arbitrum-sepolia source: - address: '0x6b1ffc44ec8ed486843640b66180d5ee366fee9f' + address: '0x0385d0a8d169b1f1510bb5fa0be264463002f6f6' abi: LimitPoolManager - startBlock: 165077264 + startBlock: 5789859 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -175,11 +175,11 @@ dataSources: handler: ProtocolSwapFeesModified - kind: ethereum/contract name: PoolsharkRouter - network: arbitrum-one + network: arbitrum-sepolia source: - address: '0x5bb32287ecb48de323760c3384a3285f732950b6' + address: '0x33df95efe07a3b3e69ba31438ae511d360d89b32' abi: PoolsharkRouter - startBlock: 167367213 + startBlock: 5789931 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -195,11 +195,11 @@ dataSources: handler: handleRouterDeployed - kind: ethereum/contract name: vFIN - network: arbitrum-one + network: arbitrum-sepolia source: - address: '0xFA3e62Aae5DE014c4CD20377Ec90Eb8e59d31169' + address: '0xed0ef8101900dfed208ac99ce47b723debb6bfe6' abi: vFIN - startBlock: 161266335 + startBlock: 5795375 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -215,11 +215,11 @@ dataSources: handler: handleTransfer - kind: ethereum/contract name: v3EthUsdcPool - network: arbitrum-one + network: arbitrum-sepolia source: - address: '0xc31e54c7a869b9fcbecc14363cf510d1c41fa443' + address: '0x02225f6a3d83648d7906a23856331c819265394d' abi: v3Pool - startBlock: 161266335 + startBlock: 5789922 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -231,5 +231,7 @@ dataSources: - name: v3Pool file: ./abis/v3Pool.json eventHandlers: + - event: Initialize(uint160,int24) + handler: handleV3Initialize - event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24) handler: handleV3Swap \ No newline at end of file diff --git a/subgraph/templates/arbitrum-sepolia.subgraph.template.yaml b/subgraph/templates/arbitrum-sepolia.subgraph.template.yaml new file mode 100644 index 00000000..31a6beef --- /dev/null +++ b/subgraph/templates/arbitrum-sepolia.subgraph.template.yaml @@ -0,0 +1,237 @@ +specVersion: 0.0.4 +description: Poolshark is a Directional Liquidity AMM allowing LPs to capture directional strength. +repository: https://github.com/poolshark-protocol/limit +schema: + file: ./schema.graphql +templates: + - kind: ethereum/contract + name: LimitPoolTemplate + network: arbitrum-sepolia + source: + abi: LimitPool + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpool.ts + entities: + - Token + - LimitPool + - Position + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: InitializeLimit(int24,int24,uint160,int24) + handler: handleInitialize + - event: SwapLimit(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + handler: handleSwap + - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) + handler: handleMintLimit + - event: SampleCountIncreased(uint16) + handler: handleSampleCountIncreased + - event: SampleRecorded(int56,uint160) + handler: handleSampleRecorded + - event: MintRange(indexed address,int24,int24,indexed uint32,uint128,int128,int128) + handler: handleMintRange + - event: BurnRange(indexed address,indexed uint256,uint128,int128,int128) + handler: handleBurnRange + - event: CompoundRange(indexed uint32,uint128) + handler: handleCompoundRange + - event: SyncRangeTick(uint200,uint200,int24) + handler: handleSyncRangeTick + - event: BurnLimit(indexed address,uint32,int24,int24,int24,int24,bool,uint128,uint128,uint128) + handler: handleBurnLimit + - event: SyncLimitPool(uint160,uint128,uint32,int24,bool) + handler: handleSyncLimitPool + - event: SyncLimitLiquidity(uint128,int24,bool) + handler: handleSyncLimitLiquidity + - event: SyncLimitTick(uint32,int24,bool) + handler: handleSyncLimitTick + - event: Collect(indexed address,address,indexed int24,indexed int24,uint128,uint128) + handler: handleCollect + # ERC-1155 events + - kind: ethereum/contract + name: PositionERC1155Template + network: arbitrum-sepolia + source: + abi: PositionERC1155 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/positionerc1155.ts + entities: + - Token + - RangePool + - RangePosition + abis: + - name: PositionERC1155 + file: ./abis/PositionERC1155.json + eventHandlers: + - event: TransferSingle(indexed address,indexed address,indexed address,uint256,uint256) + handler: handleTransferSingle + - event: TransferBatch(indexed address,indexed address,indexed address,uint256[],uint256[]) + handler: handleTransferBatch + # RangeStaker events + - kind: ethereum/contract + name: RangeStakerTemplate + network: arbitrum-sepolia + source: + abi: RangeStaker + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/staking/rangestaker.ts + entities: + - RangeStake + abis: + - name: RangeStaker + file: ./abis/RangeStaker.json + eventHandlers: + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: StakeRange(address,uint32,address,uint256,uint256,uint128) + handler: handleStakeRange + - event: StakeRangeAccrued(address,uint32,uint256,uint256) + handler: handleStakeRangeAccrued + - event: UnstakeRange(address,uint32,address) + handler: handleUnstakeRange +dataSources: + - kind: ethereum/contract + name: LimitPoolFactory + network: arbitrum-sepolia + source: + address: '0x8e40c68b7546efd009a1a300c92e25da3c8725dc' + abi: LimitPoolFactory + startBlock: 5789864 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolfactory.ts + entities: + - Token + - LimitPool + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: LimitPoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + handler: handlePoolCreated + - kind: ethereum/contract + name: LimitPoolManager + network: arbitrum-sepolia + source: + address: '0x0385d0a8d169b1f1510bb5fa0be264463002f6f6' + abi: LimitPoolManager + startBlock: 5789859 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolmanager.ts + entities: + - LimitPool + - LimitPoolFactory + abis: + - name: LimitPoolManager + file: ./abis/LimitPoolManager.json + eventHandlers: + - event: FeeTierEnabled(uint16,int16) + handler: handleFeeTierEnabled + - event: PoolTypeEnabled(bytes32,address,address,uint16) + handler: handlePoolTypeEnabled + - event: FactoryChanged(indexed address,indexed address) + handler: handleFactoryChanged + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: ProtocolFeesCollected(address[],uint128[],uint128[]) + handler: handleProtocolFeesCollected + - event: ProtocolFillFeesModified(address[],int16[],int16[]) + handler: handleProtocolFillFeesModified + - event: ProtocolSwapFeesModified(address[],int16[],int16[]) + handler: ProtocolSwapFeesModified + - kind: ethereum/contract + name: PoolsharkRouter + network: arbitrum-sepolia + source: + address: '0x33df95efe07a3b3e69ba31438ae511d360d89b32' + abi: PoolsharkRouter + startBlock: 5789931 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/poolsharkrouter.ts + entities: + - PoolsharkRouter + abis: + - name: PoolsharkRouter + file: ./abis/PoolsharkRouter.json + eventHandlers: + - event: RouterDeployed(address,address,address) + handler: handleRouterDeployed + - kind: ethereum/contract + name: vFIN + network: arbitrum-sepolia + source: + address: '0xed0ef8101900dfed208ac99ce47b723debb6bfe6' + abi: vFIN + startBlock: 5795375 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/vfin.ts + entities: + - VestedFin + abis: + - name: vFIN + file: ./abis/vFIN.json + eventHandlers: + - event: Transfer(indexed address,indexed address,indexed uint256) + handler: handleTransfer + - kind: ethereum/contract + name: v3EthUsdcPool + network: arbitrum-sepolia + source: + address: '0x02225f6a3d83648d7906a23856331c819265394d' + abi: v3Pool + startBlock: 5789922 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/v3Pool.ts + entities: + - BasePrice + abis: + - name: v3Pool + file: ./abis/v3Pool.json + eventHandlers: + - event: Initialize(uint160,int24) + handler: handleV3Initialize + - event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24) + handler: handleV3Swap \ No newline at end of file diff --git a/subgraph/templates/arbitrum.subgraph.template.yaml b/subgraph/templates/arbitrum.subgraph.template.yaml index 3007067f..ff6b5e7d 100644 --- a/subgraph/templates/arbitrum.subgraph.template.yaml +++ b/subgraph/templates/arbitrum.subgraph.template.yaml @@ -6,7 +6,7 @@ schema: templates: - kind: ethereum/contract name: LimitPoolTemplate - network: arbitrum + network: arbitrum-one source: abi: LimitPool mapping: @@ -30,9 +30,9 @@ templates: - name: ERC20NameBytes file: ./abis/ERC20NameBytes.json eventHandlers: - - event: Initialize(int24,int24,uint160,int24) + - event: InitializeLimit(int24,int24,uint160,int24) handler: handleInitialize - - event: Swap(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + - event: SwapLimit(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) handler: handleSwap - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) handler: handleMintLimit @@ -56,10 +56,8 @@ templates: handler: handleSyncLimitLiquidity - event: SyncLimitTick(uint32,int24,bool) handler: handleSyncLimitTick - - event: CollectRange0(uint128) - handler: handleCollectRange0 - - event: CollectRange1(uint128) - handler: handleCollectRange1 + - event: Collect(indexed address,address,indexed int24,indexed int24,uint128,uint128) + handler: handleCollect # ERC-1155 events - kind: ethereum/contract name: PositionERC1155Template @@ -115,9 +113,9 @@ dataSources: name: LimitPoolFactory network: arbitrum-one source: - address: '0xd28d620853af6837d76f1360dc65229d57ba5435' + address: '0x8bb5db1625adb4ae4beb94a188d33062303f8fb7' abi: LimitPoolFactory - startBlock: 158864748 + startBlock: 168223582 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -138,15 +136,15 @@ dataSources: - name: ERC20NameBytes file: ./abis/ERC20NameBytes.json eventHandlers: - - event: PoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + - event: LimitPoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) handler: handlePoolCreated - kind: ethereum/contract name: LimitPoolManager network: arbitrum-one source: - address: '0x441bb7b76c9932d5c65d7a78b3413978d7370761' + address: '0xc773049bf7131a5ddd85daded4397a1fd4264a3d' abi: LimitPoolManager - startBlock: 158864726 + startBlock: 168223575 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -179,9 +177,9 @@ dataSources: name: PoolsharkRouter network: arbitrum-one source: - address: '0x5357308a5a4db6c65fa6c4b122dfc3d749f2dd6a' + address: '0x12b7a6dd3a3dfde6a0f112a1bd876f704d933915' abi: PoolsharkRouter - startBlock: 158865312 + startBlock: 168223826 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -195,5 +193,43 @@ dataSources: eventHandlers: - event: RouterDeployed(address,address,address) handler: handleRouterDeployed - - + - kind: ethereum/contract + name: vFIN + network: arbitrum-one + source: + address: '0xFA3e62Aae5DE014c4CD20377Ec90Eb8e59d31169' + abi: vFIN + startBlock: 161266335 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/vfin.ts + entities: + - VestedFin + abis: + - name: vFIN + file: ./abis/vFIN.json + eventHandlers: + - event: Transfer(indexed address,indexed address,indexed uint256) + handler: handleTransfer + - kind: ethereum/contract + name: v3EthUsdcPool + network: arbitrum-one + source: + address: '0xc31e54c7a869b9fcbecc14363cf510d1c41fa443' + abi: v3Pool + startBlock: 168223500 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/v3Pool.ts + entities: + - BasePrice + abis: + - name: v3Pool + file: ./abis/v3Pool.json + eventHandlers: + - event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24) + handler: handleV3Swap \ No newline at end of file diff --git a/subgraph/templates/scroll-sepolia.subgraph.template.yaml b/subgraph/templates/scroll-sepolia.subgraph.template.yaml new file mode 100644 index 00000000..042fae34 --- /dev/null +++ b/subgraph/templates/scroll-sepolia.subgraph.template.yaml @@ -0,0 +1,237 @@ +specVersion: 0.0.4 +description: Poolshark is a Directional Liquidity AMM allowing LPs to capture directional strength. +repository: https://github.com/poolshark-protocol/limit +schema: + file: ./schema.graphql +templates: + - kind: ethereum/contract + name: LimitPoolTemplate + network: scroll-sepolia + source: + abi: LimitPool + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpool.ts + entities: + - Token + - LimitPool + - Position + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: InitializeLimit(int24,int24,uint160,int24) + handler: handleInitialize + - event: SwapLimit(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + handler: handleSwap + - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) + handler: handleMintLimit + - event: SampleCountIncreased(uint16) + handler: handleSampleCountIncreased + - event: SampleRecorded(int56,uint160) + handler: handleSampleRecorded + - event: MintRange(indexed address,int24,int24,indexed uint32,uint128,int128,int128) + handler: handleMintRange + - event: BurnRange(indexed address,indexed uint256,uint128,int128,int128) + handler: handleBurnRange + - event: CompoundRange(indexed uint32,uint128) + handler: handleCompoundRange + - event: SyncRangeTick(uint200,uint200,int24) + handler: handleSyncRangeTick + - event: BurnLimit(indexed address,uint32,int24,int24,int24,int24,bool,uint128,uint128,uint128) + handler: handleBurnLimit + - event: SyncLimitPool(uint160,uint128,uint32,int24,bool) + handler: handleSyncLimitPool + - event: SyncLimitLiquidity(uint128,int24,bool) + handler: handleSyncLimitLiquidity + - event: SyncLimitTick(uint32,int24,bool) + handler: handleSyncLimitTick + - event: Collect(indexed address,address,indexed int24,indexed int24,uint128,uint128) + handler: handleCollect + # ERC-1155 events + - kind: ethereum/contract + name: PositionERC1155Template + network: scroll-sepolia + source: + abi: PositionERC1155 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/positionerc1155.ts + entities: + - Token + - RangePool + - RangePosition + abis: + - name: PositionERC1155 + file: ./abis/PositionERC1155.json + eventHandlers: + - event: TransferSingle(indexed address,indexed address,indexed address,uint256,uint256) + handler: handleTransferSingle + - event: TransferBatch(indexed address,indexed address,indexed address,uint256[],uint256[]) + handler: handleTransferBatch + # RangeStaker events + - kind: ethereum/contract + name: RangeStakerTemplate + network: scroll-sepolia + source: + abi: RangeStaker + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/staking/rangestaker.ts + entities: + - RangeStake + abis: + - name: RangeStaker + file: ./abis/RangeStaker.json + eventHandlers: + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: StakeRange(address,uint32,address,uint256,uint256,uint128) + handler: handleStakeRange + - event: StakeRangeAccrued(address,uint32,uint256,uint256) + handler: handleStakeRangeAccrued + - event: UnstakeRange(address,uint32,address) + handler: handleUnstakeRange +dataSources: + - kind: ethereum/contract + name: LimitPoolFactory + network: scroll-sepolia + source: + address: '0xd506193e48f13438e35a1edc4ef5394876b5efa4' + abi: LimitPoolFactory + startBlock: 2764010 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolfactory.ts + entities: + - Token + - LimitPool + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: LimitPoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + handler: handlePoolCreated + - kind: ethereum/contract + name: LimitPoolManager + network: scroll-sepolia + source: + address: '0x7113aec7595accbaea1e61bc59f97e35d3a87bd5' + abi: LimitPoolManager + startBlock: 2764009 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolmanager.ts + entities: + - LimitPool + - LimitPoolFactory + abis: + - name: LimitPoolManager + file: ./abis/LimitPoolManager.json + eventHandlers: + - event: FeeTierEnabled(uint16,int16) + handler: handleFeeTierEnabled + - event: PoolTypeEnabled(bytes32,address,address,uint16) + handler: handlePoolTypeEnabled + - event: FactoryChanged(indexed address,indexed address) + handler: handleFactoryChanged + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: ProtocolFeesCollected(address[],uint128[],uint128[]) + handler: handleProtocolFeesCollected + - event: ProtocolFillFeesModified(address[],int16[],int16[]) + handler: handleProtocolFillFeesModified + - event: ProtocolSwapFeesModified(address[],int16[],int16[]) + handler: ProtocolSwapFeesModified + - kind: ethereum/contract + name: PoolsharkRouter + network: scroll-sepolia + source: + address: '0x14ce728bf96d5ec0976f332605fe80a42ec0b244' + abi: PoolsharkRouter + startBlock: 2764065 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/poolsharkrouter.ts + entities: + - PoolsharkRouter + abis: + - name: PoolsharkRouter + file: ./abis/PoolsharkRouter.json + eventHandlers: + - event: RouterDeployed(address,address,address) + handler: handleRouterDeployed + - kind: ethereum/contract + name: vFIN + network: scroll-sepolia + source: + address: '0x1a16415867fde435d3f8a6631ef308ae7bf96b3b' + abi: vFIN + startBlock: 2764254 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/vfin.ts + entities: + - VestedFin + abis: + - name: vFIN + file: ./abis/vFIN.json + eventHandlers: + - event: Transfer(indexed address,indexed address,indexed uint256) + handler: handleTransfer + - kind: ethereum/contract + name: v3EthUsdcPool + network: scroll-sepolia + source: + address: '0xea578474c65f859739c8ec362543eedc04f0d48e' + abi: v3Pool + startBlock: 2764030 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/v3Pool.ts + entities: + - BasePrice + abis: + - name: v3Pool + file: ./abis/v3Pool.json + eventHandlers: + - event: Initialize(uint160,int24) + handler: handleV3Initialize + - event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24) + handler: handleV3Swap \ No newline at end of file diff --git a/tasks/deploy/utils/mintPosition.ts b/tasks/deploy/utils/mintPosition.ts index ca1c0ba6..55bcc863 100644 --- a/tasks/deploy/utils/mintPosition.ts +++ b/tasks/deploy/utils/mintPosition.ts @@ -85,8 +85,8 @@ export class MintPosition { // console.log('position snapshot', snapshot.feesOwed0.toString(), snapshot.feesOwed1.toString()) const amountIn = parseUnits('1', 16) const signer = hre.props.alice - let approveTxn = await hre.props.token0.connect(signer).approve(hre.props.poolRouter.address, amountIn) - // let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, amountIn) + // let approveTxn = await hre.props.token0.connect(signer).approve(hre.props.poolRouter.address, amountIn) + let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, amountIn) await approveTxn.wait() // const aliceId = await validateMintRange({ // signer: hre.props.alice, @@ -110,13 +110,13 @@ export class MintPosition { // 0 => 1 //0xe80dc9a69853483c745f8e32162f0bd5813cb291 => new factory - const zeroForOne = true + const zeroForOne = false - const priceLimit = BigNumber.from('3721491874816186603690483') + const priceLimit = BigNumber.from('2172618421097231267834892073346') let txn = await hre.props.poolRouter .connect(signer) .multiSwapSplit( - ['0x5330588b4eFD339b8ea441aFedF09AAb851f2d48'], + ['0xa43ddbcc4b78512c316bd7091b4c60f06db0fe42'], [ { to: signer.address, diff --git a/tasks/deploy/utils/mintTokens.ts b/tasks/deploy/utils/mintTokens.ts index 42a5eede..69eea2c2 100644 --- a/tasks/deploy/utils/mintTokens.ts +++ b/tasks/deploy/utils/mintTokens.ts @@ -1,5 +1,5 @@ import { InitialSetup } from '../../../test/utils/setup/initialSetup' -import { mintSigners20 } from '../../../test/utils/token' +import { mintSigners20, mintSigners20WithRecipient } from '../../../test/utils/token' import { getNonce } from '../../utils' export class MintTokens { @@ -28,8 +28,12 @@ export class MintTokens { await this.initialSetup.readLimitPoolSetup(this.nonce) const token0Amount = ethers.utils.parseUnits('100', await hre.props.token0.decimals()) const token1Amount = ethers.utils.parseUnits('100', await hre.props.token1.decimals()) - await mintSigners20(hre.props.token0, token0Amount.mul(100), [hre.props.alice]) - await mintSigners20(hre.props.token1, token1Amount.mul(100), [hre.props.alice]) + // 0x65f5B282E024e3d6CaAD112e848dEc3317dB0902 + // 0x1DcF623EDf118E4B21b4C5Dc263bb735E170F9B8 + // 0x9dA9409D17DeA285B078af06206941C049F692Dc + // 0xBd5db4c7D55C086107f4e9D17c4c34395D1B1E1E + await mintSigners20WithRecipient(hre.props.token0, token0Amount.mul(100), [hre.props.alice], '0x65f5B282E024e3d6CaAD112e848dEc3317dB0902') + // await mintSigners20WithRecipient(hre.props.token1, token1Amount.mul(100), [hre.props.alice], '0x65f5B282E024e3d6CaAD112e848dEc3317dB0902') // const token0Balance = await hre.props.token0.balanceOf( // '0x50924f626d1Ae4813e4a81E2c5589EC3882C13ca' diff --git a/test/contracts/tgedeployer.ts b/test/contracts/tgedeployer.ts index 71b3ea83..22ef1aa7 100644 --- a/test/contracts/tgedeployer.ts +++ b/test/contracts/tgedeployer.ts @@ -106,16 +106,21 @@ describe('TGE Deployment Tests', function () { await mintSigners20(hre.props.token0, tokenAmount.mul(55000), [hre.props.alice, hre.props.bob]) await mintSigners20(hre.props.token1, tokenAmount.mul(55000), [hre.props.alice, hre.props.bob]) + const aliceLiquidity = BigNumber.from('2572549719381782803480') + + const amount0 = BigNumber.from('39168000000000000000') + const amount1 = BigNumber.from('32271546804490624383438') + const aliceId = await validateDeployTge({ signer: hre.props.alice, recipient: hre.props.alice.address, - lower: '44850', // $25 per FIN + lower: '54000', // $10 per FIN upper: '77040', // $1 per FIN - amount0: parseUnits('52000', 18), - amount1: parseUnits('52000', 18), - balance0Decrease: BigNumber.from('31568903742987611804'), // 31.568 ETH - balance1Decrease: BigNumber.from('51999999999999999999996'), // 52k FIN - liquidityIncrease: BigNumber.from('2555287091759866264142'), + amount0: parseUnits('41', 18), + amount1: parseUnits('34000', 18), + balance0Decrease: amount0, // TODO: change to correct value + balance1Decrease: amount1, // 52k FIN + liquidityIncrease: aliceLiquidity, revertMessage: '', stake: true }) @@ -123,12 +128,12 @@ describe('TGE Deployment Tests', function () { await validateBurn({ signer: hre.props.alice, recipient: hre.props.alice.address, - lower: '44850', // $25 per FIN + lower: '54000', // $10 per FIN upper: '77040', // $1 per FIN, positionId: aliceId, - liquidityAmount: BigNumber.from('2555287091759866264142'), - balance0Increase: BigNumber.from('31568903742987611803'), // 31.568 ETH - balance1Increase: BigNumber.from('51999999999999999999995'), // 52k FIN + liquidityAmount: aliceLiquidity, + balance0Increase: amount0.sub(1), // 39.168 ETH + balance1Increase: amount1.sub(1), // 52k FIN revertMessage: '', staked: true }) diff --git a/test/utils/contracts/poolsharkrouter.ts b/test/utils/contracts/poolsharkrouter.ts index 406d385e..00340abe 100644 --- a/test/utils/contracts/poolsharkrouter.ts +++ b/test/utils/contracts/poolsharkrouter.ts @@ -118,7 +118,7 @@ export async function validateDeployTge(params: ValidateMintParams): Promise { + for (let signer of signers) { + const txn = await token.connect(hre.props.alice).mint(recipient, amount) + await txn.wait() + } +} + export async function mintAddresses20( token: Contract, amount: BigNumberish, From 691941aca980165a9704ab8c93bdcb94f73c145d Mon Sep 17 00:00:00 2001 From: alphak3y Date: Tue, 9 Jan 2024 08:06:07 -0700 Subject: [PATCH 28/39] update subgraph deps --- subgraph/package.json | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/subgraph/package.json b/subgraph/package.json index f2fb6739..4a310f89 100644 --- a/subgraph/package.json +++ b/subgraph/package.json @@ -37,23 +37,21 @@ "subgraph:deploy:chainstack:ordertest": "graph deploy --version-label v0.1.0 --node https://api.graph-ams.p2pify.com/719c33840eec545dba4ed49d362448f0/deploy --ipfs https://api.graph-ams.p2pify.com/719c33840eec545dba4ed49d362448f0/ipfs limit-arbitrumGoerli-order-testing", "deploy:chainstack:arb-goerli": "BLOCKCHAIN=arbitrum =arbitrum-goerli env-cmd npm run prepare:subgraph && SLUG=aave/protocol-v2-goerli env-cmd npm run subgraph:deploy:chainstack:ordertest" }, - "devDependencies": { - "@graphprotocol/graph-cli": "0.25.1", - "@graphprotocol/graph-ts": "0.24.1", - "eslint": "7.32.0", - "eslint-config-prettier": "8.5.0", - "eslint-config-standard": "16.0.3", - "eslint-plugin-prettier": "3.4.1", - "libpq": "1.8.12", - "matchstick-as": "0.2.0" - }, "dependencies": { - "assemblyscript": "0.20.19", - "assemblyscript-json": "1.1.0", - "json-as": "0.2.6", - "node-gyp": "9.1.0", - "source-map-support": "0.5.21", - "mustache": "^3.1.0", - "env-cmd": "^10.1.0" + "babel-polyfill": "^6.26.0", + "babel-register": "^6.26.0" + }, + "devDependencies": { + "@graphprotocol/graph-cli": "^0.60.0", + "@graphprotocol/graph-ts": "^0.31.0", + "@trivago/prettier-plugin-sort-imports": "4.1.0", + "@typescript-eslint/eslint-plugin": "^5.59.11", + "@typescript-eslint/parser": "^5.59.11", + "eslint": "^8.25.0", + "eslint-config-prettier": "^8.5.0", + "mustache": "^4.2.0", + "prettier": "^2.8.8", + "rimraf": "^3.0.2", + "typescript": "5.1.3" } } From 3af2fe416dfbd874a3a31a0a17bfab449f76dd66 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Thu, 11 Jan 2024 15:13:17 -0700 Subject: [PATCH 29/39] ethers js router swap --- subgraph/package.json | 2 +- subgraph/src/constants/constants.ts | 2 +- subgraph/subgraph.yaml | 36 ++++++++++++++--------------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/subgraph/package.json b/subgraph/package.json index 4a310f89..47c8a9a3 100644 --- a/subgraph/package.json +++ b/subgraph/package.json @@ -1,5 +1,5 @@ { - "name": "poolsharkhedgepool-subgraph", + "name": "poolshark-limit-subgraph", "version": "0.0.1", "repository": "https://github.com/poolshark-protocol/limit", "license": "MIT", diff --git a/subgraph/src/constants/constants.ts b/subgraph/src/constants/constants.ts index 431c3f1c..61c08ad8 100644 --- a/subgraph/src/constants/constants.ts +++ b/subgraph/src/constants/constants.ts @@ -1,5 +1,5 @@ import { STABLE_IS_TOKEN_0, FACTORY_ADDRESS, RANGE_STAKER_ADDRESS, STABLE_POOL_ADDRESS, WETH_ADDRESS, WHITELISTED_TOKENS, WHITELISTED_PAIRS, SEASON_1_END_TIME, SEASON_1_START_TIME, STABLE_COINS, MINIMUM_ETH_LOCKED, ERROR_POOL, ZERO_ADDRESS, ZERO_BI, ONE_BI, ONE_BD, ZERO_BD, TWO_BD, BI_18, factoryContract -} from "./arbitrum-sepolia"; +} from "./scroll-sepolia"; export { STABLE_IS_TOKEN_0, FACTORY_ADDRESS, RANGE_STAKER_ADDRESS, STABLE_POOL_ADDRESS, WETH_ADDRESS, WHITELISTED_TOKENS, WHITELISTED_PAIRS, SEASON_1_END_TIME, SEASON_1_START_TIME, STABLE_COINS, MINIMUM_ETH_LOCKED, ERROR_POOL, ZERO_ADDRESS, ZERO_BI, ONE_BI, ONE_BD, ZERO_BD, TWO_BD, BI_18, factoryContract}; \ No newline at end of file diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 31a6beef..042fae34 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -6,7 +6,7 @@ schema: templates: - kind: ethereum/contract name: LimitPoolTemplate - network: arbitrum-sepolia + network: scroll-sepolia source: abi: LimitPool mapping: @@ -61,7 +61,7 @@ templates: # ERC-1155 events - kind: ethereum/contract name: PositionERC1155Template - network: arbitrum-sepolia + network: scroll-sepolia source: abi: PositionERC1155 mapping: @@ -84,7 +84,7 @@ templates: # RangeStaker events - kind: ethereum/contract name: RangeStakerTemplate - network: arbitrum-sepolia + network: scroll-sepolia source: abi: RangeStaker mapping: @@ -111,11 +111,11 @@ templates: dataSources: - kind: ethereum/contract name: LimitPoolFactory - network: arbitrum-sepolia + network: scroll-sepolia source: - address: '0x8e40c68b7546efd009a1a300c92e25da3c8725dc' + address: '0xd506193e48f13438e35a1edc4ef5394876b5efa4' abi: LimitPoolFactory - startBlock: 5789864 + startBlock: 2764010 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -140,11 +140,11 @@ dataSources: handler: handlePoolCreated - kind: ethereum/contract name: LimitPoolManager - network: arbitrum-sepolia + network: scroll-sepolia source: - address: '0x0385d0a8d169b1f1510bb5fa0be264463002f6f6' + address: '0x7113aec7595accbaea1e61bc59f97e35d3a87bd5' abi: LimitPoolManager - startBlock: 5789859 + startBlock: 2764009 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -175,11 +175,11 @@ dataSources: handler: ProtocolSwapFeesModified - kind: ethereum/contract name: PoolsharkRouter - network: arbitrum-sepolia + network: scroll-sepolia source: - address: '0x33df95efe07a3b3e69ba31438ae511d360d89b32' + address: '0x14ce728bf96d5ec0976f332605fe80a42ec0b244' abi: PoolsharkRouter - startBlock: 5789931 + startBlock: 2764065 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -195,11 +195,11 @@ dataSources: handler: handleRouterDeployed - kind: ethereum/contract name: vFIN - network: arbitrum-sepolia + network: scroll-sepolia source: - address: '0xed0ef8101900dfed208ac99ce47b723debb6bfe6' + address: '0x1a16415867fde435d3f8a6631ef308ae7bf96b3b' abi: vFIN - startBlock: 5795375 + startBlock: 2764254 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -215,11 +215,11 @@ dataSources: handler: handleTransfer - kind: ethereum/contract name: v3EthUsdcPool - network: arbitrum-sepolia + network: scroll-sepolia source: - address: '0x02225f6a3d83648d7906a23856331c819265394d' + address: '0xea578474c65f859739c8ec362543eedc04f0d48e' abi: v3Pool - startBlock: 5789922 + startBlock: 2764030 mapping: kind: ethereum/events apiVersion: 0.0.6 From 0572583bb28a71411576cdfe37b67e6aa43f1fad Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sat, 13 Jan 2024 07:22:23 -0700 Subject: [PATCH 30/39] add tick quoter --- .../limit/ILimitPoolStorageView.sol | 8 ++ .../interfaces/structs/PoolsharkStructs.sol | 2 +- contracts/utils/TickQuoter.sol | 111 ++++++++++++++++++ 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 contracts/utils/TickQuoter.sol diff --git a/contracts/interfaces/limit/ILimitPoolStorageView.sol b/contracts/interfaces/limit/ILimitPoolStorageView.sol index b0861401..16d05a46 100644 --- a/contracts/interfaces/limit/ILimitPoolStorageView.sol +++ b/contracts/interfaces/limit/ILimitPoolStorageView.sol @@ -16,4 +16,12 @@ interface ILimitPoolStorageView is LimitPoolStructs { uint32 epoch, uint8 unlocked ); + + function ticks(int24 tickIndex) + external + view + returns ( + RangeTick memory, + LimitTick memory + ); } diff --git a/contracts/interfaces/structs/PoolsharkStructs.sol b/contracts/interfaces/structs/PoolsharkStructs.sol index ccdc7f10..2c01abe2 100644 --- a/contracts/interfaces/structs/PoolsharkStructs.sol +++ b/contracts/interfaces/structs/PoolsharkStructs.sol @@ -443,7 +443,7 @@ interface PoolsharkStructs { struct TickMap { uint256 blocks; /// @dev - sets of words - mapping(uint256 => uint256) words; /// @dev - sets to words + mapping(uint256 => uint256) words; /// @dev - blocks to words mapping(uint256 => uint256) ticks; /// @dev - words to ticks mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) epochs0; /// @dev - ticks to epochs mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) epochs1; /// @dev - ticks to epochs diff --git a/contracts/utils/TickQuoter.sol b/contracts/utils/TickQuoter.sol new file mode 100644 index 00000000..8e6f8441 --- /dev/null +++ b/contracts/utils/TickQuoter.sol @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; + +import '../interfaces/limit/ILimitPoolView.sol'; +import '../interfaces/limit/ILimitPoolStorageView.sol'; +import '../interfaces/structs/PoolsharkStructs.sol'; +import '../libraries/math/ConstantProduct.sol'; + +/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#%@@@@%@@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@%%%%%%@@%==========+++**#@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@%%##%%%@@%%%@@%%*-=====+++**#**+*#*#*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@=+===========+#@@@@@%#@%%%%%%*#%%%%%##%@@@@@@@@#****++#%#**#%=%@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@+-==+++***++++=++@@%%%%%%%%*****#%#*#@%@@@@@@@@@@@@@%#+#%#*##*=#@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@+=+*******+++%%##%%####++++++**@@%%@@@@@@@@@@@@@@@###*#%#+==@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%=**##**+#%###########+====*@%@@%%@@@@@@@@@@@@@%###%@+%=+=*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@#%%####*##*##*#**#**+=+=-=*%%%###%@@@@@@@@@@@####@#=====+*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@#%#***#***#*****=*+=+==--=@@%#**###@@@@@@@@@@@###@@%=+++=++#@@@@*@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@##*********+***##++*+#%@*%@@@@@####@@@@#=%@@@@@@@@@@@+==++=+=%%#+*@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@##*****+**+*********++=%%%@@@@@@@@@*@@@%===@@@@@@@@@*===++=+++**#%#@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@#**++++*+++++===+***+==#%%#@@@@@@@@*##@*===+%@@@@%+==+=+++++++*%@%%@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@**++++++++====----+++====+###@@@@@@@%#%%*==++*+#+===+++++++++++*%%@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%###*++++++====---=*@@@@+===-%@**%@@@@@@@#%%%===*+****+++*++++*+*+*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@*+++*+++++===--=-*@@@@@@@@=--=@@@#%@@@@@@@@#%%*++++**#+++*++**++***#*@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@%===+++++=+++===*@@@@@@@@@@=-=%@@@@@%#@@@@@@@*===*++=++*++++****+**#%+@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@*=+@@#==+==+=-=@@@%%@@@@@@@@@@@@#***%%@@@@%+===+++*+*******+**++*#%*#@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@======-==#***%#@@@@@@@@@@@##%%%%%%@#====****+*****#**###*+++#*@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-===*=++**###@@@@@@@@@@@@@+#%%%%@*======+*********###+++++++++%@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-=+#%%+*###%@@@@@@@@@@@@@@@#%#@%++=+++===**#*#****#%=====+++++++%@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@-=#****####@@@@@@@@@@@@@@@@#%@*++*#++++=+#*#####*#%%==============*@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@*=#**+*###@@@@@@@@@@@@@@@@@#%+**###**+++######%%#+-==--=-=------======#@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@++#++++*##%@@@@@@@@@@@@@@@%+*#%%######*##%#+=-=#@@@@@*----------------*@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@%+#*+++==+***#@@@@@@@@@@@%#%%####%%#*+#*++%@@@@@@@@@@@@@%+=======*@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@*+**+========------=-=%@#%%%%%%%%%%%%#%#%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@#=====-==-------=#@@@@########%%%%%%##++#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%##%%@@@@@@@@@@@@%#######%%#*+=++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#*####++++=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##**+=#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ + +/** + * @title TickData + * @notice A contract to query tick data + * @author Poolshark + * @author @alphak3y + */ +contract TickQuoter is PoolsharkStructs { + + struct TickData { + int24 tick; + int128 liquidityNet; + uint128 liquidityGross; + } + + function getTickDataInWord( + address pool, + int16 tickBitmapIndex + ) public view returns ( + TickData[] memory populatedTicks + ) { + int16 tickSpacing = (ILimitPoolView(pool).immutables()).tickSpacing; + int24 startTick = (int24(tickBitmapIndex) << 8) * tickSpacing / 2; + uint8 ticksCount; + + // array for tick data found; max size of 256 + TickData[] memory foundTicks = new TickData[](256); + + + for (int24 i = 0; i < 256;) { + int24 currentTick = startTick + i * (tickSpacing / 2); + RangeTick memory rangeTick; LimitTick memory limitTick; + ( + rangeTick, + limitTick + ) = ILimitPoolStorageView(pool).ticks(currentTick); + ((int24(tickBitmapIndex) << 8) + int24(i)) * tickSpacing; + if (rangeTick.liquidityAbsolute + limitTick.liquidityAbsolute > 0) { + // push active tick to array + foundTicks[ticksCount] = TickData({ + tick: currentTick, + liquidityNet: rangeTick.liquidityDelta + limitTick.liquidityDelta, + liquidityGross: rangeTick.liquidityAbsolute + limitTick.liquidityAbsolute + }); + unchecked { + // count number of active ticks + ++ticksCount; + } + } + unchecked { + ++i; + } + } + + // resize array based on ticksCount + populatedTicks = new TickData[](ticksCount); + + // push tick data to returned array + for (uint i; i < ticksCount;) { + populatedTicks[i] = foundTicks[i]; + unchecked { + ++i; + } + } + } +} From 015166a1c7b4af7cf370cc1738b7f26de45f1039 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sat, 13 Jan 2024 07:22:57 -0700 Subject: [PATCH 31/39] add tick quoter --- contracts/utils/TickQuoter.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/utils/TickQuoter.sol b/contracts/utils/TickQuoter.sol index 8e6f8441..56547741 100644 --- a/contracts/utils/TickQuoter.sol +++ b/contracts/utils/TickQuoter.sol @@ -45,7 +45,7 @@ import '../libraries/math/ConstantProduct.sol'; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ /** - * @title TickData + * @title TickQuoter * @notice A contract to query tick data * @author Poolshark * @author @alphak3y From 40400b9b21a18d0be49b6b878f01a72f58abc860 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sat, 13 Jan 2024 07:27:19 -0700 Subject: [PATCH 32/39] prettier formatting --- contracts/utils/LimitPoolManager.sol | 15 +++++++--- contracts/utils/PoolsharkRouter.sol | 25 +++++------------ contracts/utils/TickQuoter.sol | 41 +++++++++++++++------------- 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/contracts/utils/LimitPoolManager.sol b/contracts/utils/LimitPoolManager.sol index 207f04e5..16ef578a 100644 --- a/contracts/utils/LimitPoolManager.sol +++ b/contracts/utils/LimitPoolManager.sol @@ -58,7 +58,7 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { address public factory; // fee delta const for dynamic fees uint16 public feeDeltaConst; - mapping (address => uint16) poolFeeDeltaConsts; + mapping(address => uint16) poolFeeDeltaConsts; // max protocol fees uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; /// @dev - max protocol swap fee of 100% uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; /// @dev - max protocol fill fee of 1% @@ -184,14 +184,21 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { factory = factory_; } - function setFeeDeltaConst(address pool, uint16 feeDeltaConst_) external onlyOwner { + function setFeeDeltaConst(address pool, uint16 feeDeltaConst_) + external + onlyOwner + { if (feeDeltaConst_ > 10000) require(false, 'FeeDeltaConstCeilingExceeded()'); if (pool == address(0)) { emit FeeDeltaConstChanged(feeDeltaConst, feeDeltaConst_); - feeDeltaConst = feeDeltaConst_; + feeDeltaConst = feeDeltaConst_; } else { - emit PoolFeeDeltaConstChanged(pool, poolFeeDeltaConsts[pool], feeDeltaConst_); + emit PoolFeeDeltaConstChanged( + pool, + poolFeeDeltaConsts[pool], + feeDeltaConst_ + ); poolFeeDeltaConsts[pool] = feeDeltaConst_; } } diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index 60c8bb4e..485110a2 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -748,7 +748,6 @@ contract PoolsharkRouter is return locals.prunedResults; } - function multiCall(address[] memory pools, SwapParams[] memory params) external { @@ -772,10 +771,7 @@ contract PoolsharkRouter is function deployTge(address tgePool, address staker) external { // read pool price RangePoolState memory tgePoolState; - ( - tgePoolState, - ,,,,, - ) = IPool(tgePool).globalState(); + (tgePoolState, , , , , , ) = IPool(tgePool).globalState(); uint160 expectedPoolPrice = 2172618421097231267834892073346; if (tgePoolState.price < expectedPoolPrice) { // move pool price up if below @@ -813,16 +809,13 @@ contract PoolsharkRouter is IPool(tgePool).swap(swapParams); } // read pool price - ( - tgePoolState, - ,,,,, - ) = IPool(tgePool).globalState(); - if(tgePoolState.price != expectedPoolPrice) + (tgePoolState, , , , , , ) = IPool(tgePool).globalState(); + if (tgePoolState.price != expectedPoolPrice) require(false, 'PoolPriceMismatch()'); MintRangeCallbackData memory callbackData = MintRangeCallbackData({ - sender: msg.sender, - recipient: staker != address(0) ? staker : msg.sender, - wrapped: false + sender: msg.sender, + recipient: staker != address(0) ? staker : msg.sender, + wrapped: false }); MintRangeParams memory mintRangeParams = MintRangeParams({ to: staker, @@ -836,11 +829,7 @@ contract PoolsharkRouter is IRangePool(tgePool).mintRange(mintRangeParams); if (staker != address(0)) { IRangeStaker(staker).stakeRange( - StakeRangeParams({ - to: msg.sender, - pool: tgePool, - positionId: 0 - }) + StakeRangeParams({to: msg.sender, pool: tgePool, positionId: 0}) ); } } diff --git a/contracts/utils/TickQuoter.sol b/contracts/utils/TickQuoter.sol index 56547741..f03588ee 100644 --- a/contracts/utils/TickQuoter.sol +++ b/contracts/utils/TickQuoter.sol @@ -51,41 +51,44 @@ import '../libraries/math/ConstantProduct.sol'; * @author @alphak3y */ contract TickQuoter is PoolsharkStructs { - struct TickData { int24 tick; int128 liquidityNet; uint128 liquidityGross; } - function getTickDataInWord( - address pool, - int16 tickBitmapIndex - ) public view returns ( - TickData[] memory populatedTicks - ) { + function getTickDataInWord(address pool, int16 tickBitmapIndex) + public + view + returns (TickData[] memory populatedTicks) + { int16 tickSpacing = (ILimitPoolView(pool).immutables()).tickSpacing; - int24 startTick = (int24(tickBitmapIndex) << 8) * tickSpacing / 2; + int24 startTick = ((int24(tickBitmapIndex) << 8) * tickSpacing) / 2; uint8 ticksCount; // array for tick data found; max size of 256 TickData[] memory foundTicks = new TickData[](256); - - for (int24 i = 0; i < 256;) { + for (int24 i = 0; i < 256; ) { + // offset currenTick from startTick int24 currentTick = startTick + i * (tickSpacing / 2); - RangeTick memory rangeTick; LimitTick memory limitTick; - ( - rangeTick, - limitTick - ) = ILimitPoolStorageView(pool).ticks(currentTick); - ((int24(tickBitmapIndex) << 8) + int24(i)) * tickSpacing; + + // read tick from storage + RangeTick memory rangeTick; + LimitTick memory limitTick; + (rangeTick, limitTick) = ILimitPoolStorageView(pool).ticks( + currentTick + ); + + // check for non-zero liquidity if (rangeTick.liquidityAbsolute + limitTick.liquidityAbsolute > 0) { // push active tick to array foundTicks[ticksCount] = TickData({ tick: currentTick, - liquidityNet: rangeTick.liquidityDelta + limitTick.liquidityDelta, - liquidityGross: rangeTick.liquidityAbsolute + limitTick.liquidityAbsolute + liquidityNet: rangeTick.liquidityDelta + + limitTick.liquidityDelta, + liquidityGross: rangeTick.liquidityAbsolute + + limitTick.liquidityAbsolute }); unchecked { // count number of active ticks @@ -101,7 +104,7 @@ contract TickQuoter is PoolsharkStructs { populatedTicks = new TickData[](ticksCount); // push tick data to returned array - for (uint i; i < ticksCount;) { + for (uint256 i; i < ticksCount; ) { populatedTicks[i] = foundTicks[i]; unchecked { ++i; From e63cb3580b512b7f74fb90f5889f1bc4391ec126 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sat, 13 Jan 2024 07:29:05 -0700 Subject: [PATCH 33/39] prettier formatting --- contracts/utils/TickQuoter.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/utils/TickQuoter.sol b/contracts/utils/TickQuoter.sol index f03588ee..d4760658 100644 --- a/contracts/utils/TickQuoter.sol +++ b/contracts/utils/TickQuoter.sol @@ -70,7 +70,7 @@ contract TickQuoter is PoolsharkStructs { TickData[] memory foundTicks = new TickData[](256); for (int24 i = 0; i < 256; ) { - // offset currenTick from startTick + // offset currentTick from startTick int24 currentTick = startTick + i * (tickSpacing / 2); // read tick from storage From a48c891e29441b1af09d385cb5370412d0c1245e Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sat, 13 Jan 2024 19:13:37 -0700 Subject: [PATCH 34/39] test tick quoter; fix tick return ordering --- contracts/utils/TickQuoter.sol | 95 ++++++++++++-- test/contracts/rangepool.ts | 1 + test/contracts/tickquoter.ts | 186 ++++++++++++++++++++++++++++ test/utils/setup/beforeEachProps.ts | 4 + test/utils/setup/initialSetup.ts | 12 +- 5 files changed, 288 insertions(+), 10 deletions(-) create mode 100644 test/contracts/tickquoter.ts diff --git a/contracts/utils/TickQuoter.sol b/contracts/utils/TickQuoter.sol index d4760658..cb833008 100644 --- a/contracts/utils/TickQuoter.sol +++ b/contracts/utils/TickQuoter.sol @@ -5,6 +5,7 @@ import '../interfaces/limit/ILimitPoolView.sol'; import '../interfaces/limit/ILimitPoolStorageView.sol'; import '../interfaces/structs/PoolsharkStructs.sol'; import '../libraries/math/ConstantProduct.sol'; +import '../external/solady/LibClone.sol'; /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ -51,10 +52,34 @@ import '../libraries/math/ConstantProduct.sol'; * @author @alphak3y */ contract TickQuoter is PoolsharkStructs { + address public immutable limitPoolFactory; + struct TickData { + /** + * @custom:field tick + * @notice The index of the tick + */ int24 tick; - int128 liquidityNet; - uint128 liquidityGross; + + /** + * @custom:field liquidityDelta + * @notice The +/- liquidity change at the tick + * @notice Delta applied for upward crosses + * @notice Opposite delta applied for downward crosses + */ + int128 liquidityDelta; + + /** + * @custom:field liquidityAbsolute + * @notice The absolute value of liquidity at the tick + */ + uint128 liquidityAbsolute; + } + + constructor( + address limitPoolFactory_ + ) { + limitPoolFactory = limitPoolFactory_; } function getTickDataInWord(address pool, int16 tickBitmapIndex) @@ -62,14 +87,20 @@ contract TickQuoter is PoolsharkStructs { view returns (TickData[] memory populatedTicks) { - int16 tickSpacing = (ILimitPoolView(pool).immutables()).tickSpacing; - int24 startTick = ((int24(tickBitmapIndex) << 8) * tickSpacing) / 2; + // read constants from pool + LimitImmutables memory constants = ILimitPoolView(pool).immutables(); + + // validate address is a canonical limit pool + canonicalLimitPoolsOnly(pool, constants); + + int16 tickSpacing = constants.tickSpacing; + int24 startTick = (int24(tickBitmapIndex) << 8) * (tickSpacing / 2); uint8 ticksCount; // array for tick data found; max size of 256 TickData[] memory foundTicks = new TickData[](256); - for (int24 i = 0; i < 256; ) { + for (int24 i = 0; i < 256;) { // offset currentTick from startTick int24 currentTick = startTick + i * (tickSpacing / 2); @@ -85,9 +116,9 @@ contract TickQuoter is PoolsharkStructs { // push active tick to array foundTicks[ticksCount] = TickData({ tick: currentTick, - liquidityNet: rangeTick.liquidityDelta + + liquidityDelta: rangeTick.liquidityDelta + limitTick.liquidityDelta, - liquidityGross: rangeTick.liquidityAbsolute + + liquidityAbsolute: rangeTick.liquidityAbsolute + limitTick.liquidityAbsolute }); unchecked { @@ -105,10 +136,56 @@ contract TickQuoter is PoolsharkStructs { // push tick data to returned array for (uint256 i; i < ticksCount; ) { - populatedTicks[i] = foundTicks[i]; + populatedTicks[i] = foundTicks[ticksCount - i - 1]; unchecked { ++i; } } } -} + + function canonicalLimitPoolsOnly( + address pool, + PoolsharkStructs.LimitImmutables memory constants + ) private view { + // generate key for pool + bytes32 key = keccak256( + abi.encode( + constants.poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); + + // compute address + address predictedAddress = LibClone.predictDeterministicAddress( + constants.poolImpl, + encodeLimit(constants), + key, + limitPoolFactory + ); + + // revert on sender mismatch + if (pool != predictedAddress) + require(false, 'InvalidPoolAddress()'); + } + + function encodeLimit(LimitImmutables memory constants) + private + pure + returns (bytes memory) + { + return + abi.encodePacked( + constants.owner, + constants.token0, + constants.token1, + constants.poolToken, + constants.bounds.min, + constants.bounds.max, + constants.genesisTime, + constants.tickSpacing, + constants.swapFee + ); + } +} \ No newline at end of file diff --git a/test/contracts/rangepool.ts b/test/contracts/rangepool.ts index 27c92b0c..6cd9b81c 100644 --- a/test/contracts/rangepool.ts +++ b/test/contracts/rangepool.ts @@ -995,6 +995,7 @@ describe('RangePool Exact In Tests', function () { }) it('pool - Should mint position inside the other 17', async function () { + // await getPrice() const pool: RangePoolState = (await hre.props.limitPool.globalState()).pool const aliceLiquidity = BigNumber.from('7705754408611783555308') const bobLiquidity = BigNumber.from('12891478442546858467877') diff --git a/test/contracts/tickquoter.ts b/test/contracts/tickquoter.ts new file mode 100644 index 00000000..d15c4fc1 --- /dev/null +++ b/test/contracts/tickquoter.ts @@ -0,0 +1,186 @@ +/* global describe it before ethers */ +const hardhat = require('hardhat') +const { expect } = require('chai') +import { gBefore } from '../utils/hooks.test' +import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' +import { BigNumber } from 'ethers' +import { mintSigners20 } from '../utils/token' +import { + validateMint, + BN_ZERO, + validateSwap, + validateBurn, + getTickAtPrice, + getRangeBalanceOf, + getSnapshot, + getPrice, + getRangeLiquidity, + getTickLiquidity, +} from '../utils/contracts/rangepool' +import { RangePoolState, ZERO_ADDRESS } from '../utils/contracts/limitpool' +import { parseUnits } from 'ethers/lib/utils' +import { validateDeployTge } from '../utils/contracts/poolsharkrouter' + +alice: SignerWithAddress +describe('TickQuoter Tests', function () { + let tokenAmount: BigNumber + let token0Decimals: number + let token1Decimals: number + let minPrice: BigNumber + let maxPrice: BigNumber + + let alice: SignerWithAddress + let bob: SignerWithAddress + let carol: SignerWithAddress + + ////////// DEBUG FLAGS ////////// + let debugMode = false + let balanceCheck = false + + const liquidityAmount = BigNumber.from('49902591570441687020675') + const liquidityAmount2 = BigNumber.from('50102591670431696268925') + const liquidityAmount3 = BigNumber.from('3852877204305891777654') + const minTickIdx = BigNumber.from('-887272') + const maxTickIdx = BigNumber.from('887272') + + before(async function () { + await gBefore() + let currentBlock = await ethers.provider.getBlockNumber() + const pool: RangePoolState = (await hre.props.limitPool.globalState()).pool + const liquidity = pool.liquidity + const feeGrowthGlobal0 = pool.feeGrowthGlobal0 + const feeGrowthGlobal1 = pool.feeGrowthGlobal1 + const price = pool.price + const nearestTick = pool.tickAtPrice + + expect(liquidity).to.be.equal(BN_ZERO) + + minPrice = BigNumber.from('4295128739') + maxPrice = BigNumber.from('1461446703485210103287273052203988822378723970341') + token0Decimals = await hre.props.token0.decimals() + token1Decimals = await hre.props.token1.decimals() + tokenAmount = ethers.utils.parseUnits('100', token0Decimals) + tokenAmount = ethers.utils.parseUnits('100', token1Decimals) + alice = hre.props.alice + bob = hre.props.bob + carol = hre.props.carol + }) + + this.beforeEach(async function () { + await mintSigners20(hre.props.token0, tokenAmount.mul(10), [hre.props.alice, hre.props.bob]) + await mintSigners20(hre.props.token1, tokenAmount.mul(10), [hre.props.alice, hre.props.bob]) + }) + + it('pool - Should quote ticks', async function () { + // 82169065928981720851271231910 + // 177159557114295710296101716160 + if (debugMode) await getPrice() + const pool: RangePoolState = (await hre.props.limitPool.globalState()).pool + const aliceLiquidity = BigNumber.from('7705754408611783555308') + const bobLiquidity = BigNumber.from('12891478442546858467877') + const bobLiquidity2 = BigNumber.from('4901161634764542438930') + + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: true, + amount: tokenAmount, + sqrtPriceLimitX96: BigNumber.from('82169065928981720851271231910'), + balanceInDecrease: BigNumber.from('0'), // token1 increase in pool + balanceOutIncrease: BigNumber.from('0'), // token0 decrease in pool + revertMessage: '', + }) + + if (debugMode) await getPrice() + const aliceId = await validateMint({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '500', + upper: '1000', + amount0: tokenAmount, + amount1: tokenAmount, + balance0Decrease: BigNumber.from('100000000000000000000'), + balance1Decrease: BigNumber.from('90970905615086187051'), + liquidityIncrease: aliceLiquidity, + revertMessage: '', + collectRevertMessage: '' + }) + + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: false, + amount: tokenAmount, + sqrtPriceLimitX96: BigNumber.from('82255474610179467046984074964'), + balanceInDecrease: BigNumber.from('8404133769503785680'), // token1 increase in pool + balanceOutIncrease: BigNumber.from('7801206245756322179'), // token0 decrease in pool + revertMessage: '', + }) + + const bobId = await validateMint({ + signer: hre.props.bob, + recipient: hre.props.bob.address, + lower: '600', + upper: '800', + amount0: tokenAmount, + amount1: tokenAmount, + balance0Decrease: BigNumber.from('31002239349424966834'), + balance1Decrease: BigNumber.from('100000000000000000000'), + liquidityIncrease: bobLiquidity, + revertMessage: '', + }) + + // quote ticks + const populatedTicks = await hre.props.tickQuoter.getTickDataInWord(hre.props.limitPool.address, 0) + + expect(populatedTicks[0][0]).to.be.equal(1000) + expect(populatedTicks[1][0]).to.be.equal(800) + expect(populatedTicks[2][0]).to.be.equal(600) + expect(populatedTicks[3][0]).to.be.equal(500) + expect(populatedTicks.length).to.be.equal(4) + + if (debugMode) await getSnapshot(bobId) + if (debugMode) console.log('FIRST BURN') + await validateBurn({ + signer: hre.props.bob, + lower: '600', + upper: '800', + positionId: bobId, + liquidityAmount: BigNumber.from('3867443532764057540363'), + burnPercent: ethers.utils.parseUnits('3', 37), + balance0Increase: BigNumber.from('9300671804827490049'), + balance1Increase: BigNumber.from('29999999999999999999'), + revertMessage: '', + }) + if (debugMode) await getSnapshot(bobId) + if (debugMode) console.log('SECOND BURN') + await validateBurn({ + signer: hre.props.bob, + lower: '600', + upper: '800', + positionId: bobId, + liquidityAmount: BigNumber.from('9024034909782800927514'), + burnPercent: ethers.utils.parseUnits('1', 38), + balance0Increase: BigNumber.from('21701567544597476783'), + balance1Increase: BigNumber.from('69999999999999999999'), + revertMessage: '', + }) + if (debugMode) await getSnapshot(aliceId) + await validateBurn({ + signer: hre.props.alice, + lower: '500', + upper: '1000', + positionId: aliceId, + liquidityAmount: aliceLiquidity, + burnPercent: ethers.utils.parseUnits('1', 38), + balance0Increase: BigNumber.from('92198793754243677819'), + balance1Increase: BigNumber.from('99375039384589972730'), + revertMessage: '', + }) + + if (balanceCheck) { + console.log('balance after token0:', (await hre.props.token0.balanceOf(hre.props.limitPool.address)).toString()) + console.log('balance after token1:', (await hre.props.token1.balanceOf(hre.props.limitPool.address)).toString()) + } + }) +}) \ No newline at end of file diff --git a/test/utils/setup/beforeEachProps.ts b/test/utils/setup/beforeEachProps.ts index c25cbce1..b0c36e43 100644 --- a/test/utils/setup/beforeEachProps.ts +++ b/test/utils/setup/beforeEachProps.ts @@ -19,6 +19,7 @@ import { SnapshotLimitCall, WETH9, RangeStaker, + TickQuoter, } from '../../../typechain' import { InitialSetup } from './initialSetup' import { MintRangeCall } from '../../../typechain' @@ -37,6 +38,7 @@ export interface BeforeEachProps { kyberPoolToken: PositionERC1155 limitPoolManager: LimitPoolManager limitPoolFactory: LimitPoolFactory + tickQuoter: TickQuoter poolRouter: PoolsharkRouter ticksLib: Ticks tickMapLib: TickMap @@ -100,6 +102,7 @@ export class GetBeforeEach { let kyberPoolToken: PositionERC1155 let limitPoolManager: LimitPoolManager let limitPoolFactory: LimitPoolFactory + let tickQuoter: TickQuoter let poolRouter: PoolsharkRouter let tickMapLib: TickMap let ticksLib: Ticks @@ -141,6 +144,7 @@ export class GetBeforeEach { kyberPoolToken, limitPoolManager, limitPoolFactory, + tickQuoter, poolRouter, tickMapLib, ticksLib, diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index d85cad87..f2f373c3 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -3,7 +3,7 @@ import { SUPPORTED_NETWORKS } from '../../../scripts/constants/supportedNetworks import { DeployAssist } from '../../../scripts/util/deployAssist' import { ContractDeploymentsKeys } from '../../../scripts/util/files/contractDeploymentKeys' import { ContractDeploymentsJson } from '../../../scripts/util/files/contractDeploymentsJson' -import { BurnLimitCall__factory, LimitPool__factory, MintLimitCall__factory, LimitPositions__factory, QuoteCall__factory, PositionERC1155__factory, LimitTicks__factory, FeesCall__factory, SampleCall__factory, SnapshotRangeCall__factory, SnapshotLimitCall__factory, WETH9__factory, RangeStaker, RangeStaker__factory } from '../../../typechain' +import { BurnLimitCall__factory, LimitPool__factory, MintLimitCall__factory, LimitPositions__factory, QuoteCall__factory, PositionERC1155__factory, LimitTicks__factory, FeesCall__factory, SampleCall__factory, SnapshotRangeCall__factory, SnapshotLimitCall__factory, WETH9__factory, RangeStaker, RangeStaker__factory, TickQuoter__factory } from '../../../typechain' import { BurnRangeCall__factory } from '../../../typechain' import { SwapCall__factory } from '../../../typechain' import { MintRangeCall__factory } from '../../../typechain' @@ -168,6 +168,16 @@ export class InitialSetup { ], ) + await this.deployAssist.deployContractWithRetry( + network, + // @ts-ignore + TickQuoter__factory, + 'tickQuoter', + [ + hre.props.limitPoolFactory.address + ], + ) + await this.deployAssist.deployContractWithRetry( network, // @ts-ignore From 19b287a484ad9ce05af3380e7d3518001e0258fc Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sat, 13 Jan 2024 20:06:11 -0700 Subject: [PATCH 35/39] tick quoter address --- scripts/autogen/contract-deployments.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index ee7de76b..7a6dcbda 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -194,6 +194,14 @@ } ], "created": "2024-01-08T04:23:06.825Z" + }, + "tickQuoter": { + "contractName": "TickQuoter", + "contractAddress": "0xc7cdb74fbc16fc9e7c57836d00ef2eb5e9c499bc", + "constructorArguments": [ + "0x8bb5db1625adb4ae4beb94a188d33062303f8fb7" + ], + "created": "2024-01-14T02:58:50.837Z" } }, "arb_goerli": { From f1f9fd760cbbe6c619b61f5b1711d0c356765112 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Mon, 15 Jan 2024 20:39:14 -0700 Subject: [PATCH 36/39] set default values for entities --- subgraph/schema.graphql | 2 +- subgraph/src/mappings/limitpoolmanager.ts | 4 +- subgraph/src/mappings/utils/loads.ts | 525 +++++++++++++++------- subgraph/subgraph.yaml | 2 +- 4 files changed, 366 insertions(+), 167 deletions(-) diff --git a/subgraph/schema.graphql b/subgraph/schema.graphql index 196d233f..374b2522 100644 --- a/subgraph/schema.graphql +++ b/subgraph/schema.graphql @@ -201,7 +201,7 @@ type Transaction @entity { timestamp: BigInt! gasLimit: BigInt! gasPrice: BigInt! - swaps: [Swap!]! @derivedFrom(field: "transaction") + swaps: [Swap!]! } type LimitPoolType @entity { diff --git a/subgraph/src/mappings/limitpoolmanager.ts b/subgraph/src/mappings/limitpoolmanager.ts index 62aa4a88..c5278bfb 100644 --- a/subgraph/src/mappings/limitpoolmanager.ts +++ b/subgraph/src/mappings/limitpoolmanager.ts @@ -2,6 +2,7 @@ import { safeLoadManager, safeLoadLimitPoolFactory, safeLoadFeeTier } from './ut import { BigInt, log } from '@graphprotocol/graph-ts' import { FACTORY_ADDRESS } from '../constants/constants' import { FactoryChanged, FeeTierEnabled, FeeToTransfer, PoolTypeEnabled, OwnerTransfer, ProtocolSwapFeesModified, ProtocolFeesCollected, ProtocolFillFeesModified } from '../../generated/LimitPoolManager/LimitPoolManager' +import { FeeTier } from '../../generated/schema' export function handleFeeTierEnabled(event: FeeTierEnabled): void { let swapFeeParam = event.params.swapFee @@ -72,10 +73,9 @@ export function handleOwnerTransfer(event: OwnerTransfer): void { if(!loadManager.exists) { manager.feeTo = newOwnerParam - // manager.factory = FACTORY_ADDRESS } if(!loadFactory.exists) { - //factory.owner = manager.id + factory.manager = manager.id } manager.owner = newOwnerParam diff --git a/subgraph/src/mappings/utils/loads.ts b/subgraph/src/mappings/utils/loads.ts index e0f5785d..213b340e 100644 --- a/subgraph/src/mappings/utils/loads.ts +++ b/subgraph/src/mappings/utils/loads.ts @@ -1,15 +1,73 @@ import { Address, BigDecimal, BigInt, Bytes, ethereum, log } from '@graphprotocol/graph-ts' import { LimitPool, LimitPoolFactory, LimitPoolManager, LimitPosition, Token, FeeTier, BasePrice, RangePosition, RangeTick, Transaction, LimitTick, Swap, CompoundRangeLog, MintRangeLog, BurnRangeLog, PoolRouter, TvlUpdateLog, HistoricalOrder, TotalSeasonReward, UserSeasonReward, LimitPoolToken, VFinPosition } from '../../../generated/schema' -import { ONE_BD } from '../../constants/constants' +import { FACTORY_ADDRESS, ONE_BD } from '../../constants/constants' import { fetchTokenSymbol, fetchTokenName, fetchTokenDecimals, BIGINT_ZERO, BIGDECIMAL_ZERO, + BIGINT_ONE, } from './helpers' import { bigDecimalExponated, safeDiv } from './math' import { getEthPriceInUSD } from './price' +import { ZERO_ADDRESS } from '../../constants/arbitrum-goerli' + +class LoadLimitPoolFactoryRet { + entity: LimitPoolFactory + exists: boolean +} +export function safeLoadLimitPoolFactory(factoryAddress: string): LoadLimitPoolFactoryRet { + let exists = true + let limitPoolFactoryEntity = LimitPoolFactory.load(factoryAddress) + + if (!limitPoolFactoryEntity) { + limitPoolFactoryEntity = new LimitPoolFactory(factoryAddress) + + limitPoolFactoryEntity.manager = ZERO_ADDRESS + limitPoolFactoryEntity.poolCount = BIGINT_ZERO + limitPoolFactoryEntity.txnCount = BIGINT_ZERO + limitPoolFactoryEntity.volumeEthTotal = BIGDECIMAL_ZERO + limitPoolFactoryEntity.volumeUsdTotal = BIGDECIMAL_ZERO + limitPoolFactoryEntity.feesUsdTotal = BIGDECIMAL_ZERO + limitPoolFactoryEntity.feesEthTotal = BIGDECIMAL_ZERO + limitPoolFactoryEntity.totalValueLockedUsd = BIGDECIMAL_ZERO + limitPoolFactoryEntity.totalValueLockedEth = BIGDECIMAL_ZERO + + exists = false + } + + return { + entity: limitPoolFactoryEntity, + exists: exists, + } +} + +class LoadManagerRet { + entity: LimitPoolManager + exists: boolean +} +export function safeLoadManager(address: string): LoadManagerRet { + let exists = true + + let managerEntity = LimitPoolManager.load(address) + + if (!managerEntity) { + managerEntity = new LimitPoolManager(address) + managerEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + managerEntity.feeTo = Bytes.fromHexString(ZERO_ADDRESS) + managerEntity.factory = FACTORY_ADDRESS + managerEntity.feeTiers = new Array() + managerEntity.poolTypes = new Array() + + exists = false + } + + return { + entity: managerEntity, + exists: exists, + } +} class LoadBasePriceRet { entity: BasePrice @@ -39,27 +97,31 @@ export function safeLoadBasePrice(name: string, stablePool: LimitPool | null = n } } -class LoadTransactionRet { - entity: Transaction +class LoadFeeTierRet { + entity: FeeTier exists: boolean } -export function safeLoadTransaction(event: ethereum.Event): LoadTransactionRet { +export function safeLoadFeeTier(feeTier: BigInt): LoadFeeTierRet { let exists = true - let transactionEntity = Transaction.load(event.transaction.hash.toHex()) + let feeTierId = feeTier.toString() + log.debug('pool volatility tier id: {}', [feeTierId]) + + let feeTierEntity = FeeTier.load(feeTierId) + + if (!feeTierEntity) { + feeTierEntity = new FeeTier(feeTierId) + feeTierEntity.feeAmount = BIGINT_ZERO + feeTierEntity.tickSpacing = BIGINT_ZERO + + feeTierEntity.createdAtBlockNumber = BIGINT_ZERO + feeTierEntity.createdAtTimestamp = BIGINT_ZERO - if (!transactionEntity) { - transactionEntity = new Transaction(event.transaction.hash.toHex()) - transactionEntity.sender = event.transaction.from - transactionEntity.blockNumber = event.block.number - transactionEntity.timestamp = event.block.timestamp - transactionEntity.gasLimit = event.transaction.gasLimit - transactionEntity.gasPrice = event.transaction.gasPrice exists = false } return { - entity: transactionEntity, + entity: feeTierEntity, exists: exists, } } @@ -77,10 +139,30 @@ export function safeLoadToken(address: string): LoadTokenRet { tokenEntity = new Token(address) log.info('{}', [address]) let tokenAddress = Address.fromString(address) + tokenEntity.symbol = fetchTokenSymbol(tokenAddress) tokenEntity.name = fetchTokenName(tokenAddress) tokenEntity.decimals = fetchTokenDecimals(tokenAddress) + tokenEntity.ethPrice = BIGDECIMAL_ZERO + tokenEntity.usdPrice = BIGDECIMAL_ZERO + + tokenEntity.pools = new Array() + + tokenEntity.volume = BIGDECIMAL_ZERO + tokenEntity.volumeUsd = BIGDECIMAL_ZERO + tokenEntity.volumeEth = BIGDECIMAL_ZERO + tokenEntity.txnCount = BIGINT_ZERO + + tokenEntity.feesUsdTotal = BIGDECIMAL_ZERO + tokenEntity.feesEthTotal = BIGDECIMAL_ZERO + + tokenEntity.totalValueLocked = BIGDECIMAL_ZERO + tokenEntity.totalValueLockedEth = BIGDECIMAL_ZERO + tokenEntity.totalValueLockedUsd = BIGDECIMAL_ZERO + + tokenEntity.whitelistPools = new Array() + exists = false } @@ -90,298 +172,391 @@ export function safeLoadToken(address: string): LoadTokenRet { } } -class LoadManagerRet { - entity: LimitPoolManager +class LoadLimitPoolRet { + entity: LimitPool exists: boolean } -export function safeLoadManager(address: string): LoadManagerRet { +export function safeLoadLimitPool(poolAddress: string): LoadLimitPoolRet { let exists = true + let limitPoolEntity = LimitPool.load(poolAddress) - let managerEntity = LimitPoolManager.load(address) + if (!limitPoolEntity) { + limitPoolEntity = new LimitPool(poolAddress) + + limitPoolEntity.token0 = 'token0' + limitPoolEntity.token1 = 'token1' + + limitPoolEntity.feeTier = 'default' + limitPoolEntity.swapFee = BIGINT_ZERO + limitPoolEntity.tickSpacing = BIGINT_ZERO + limitPoolEntity.factory = ZERO_ADDRESS + limitPoolEntity.poolType = 'default' + limitPoolEntity.poolToken = Bytes.fromHexString(ZERO_ADDRESS) + + limitPoolEntity.liquidity = BIGINT_ZERO + limitPoolEntity.liquidityGlobal = BIGINT_ZERO + limitPoolEntity.positionIdNext = BIGINT_ONE + limitPoolEntity.epoch = BIGINT_ONE + limitPoolEntity.poolPrice = BIGINT_ZERO + limitPoolEntity.pool0Price = BIGINT_ZERO + limitPoolEntity.pool1Price = BIGINT_ZERO + limitPoolEntity.poolLiquidity = BIGINT_ZERO + limitPoolEntity.pool0Liquidity = BIGINT_ZERO + limitPoolEntity.pool1Liquidity = BIGINT_ZERO + limitPoolEntity.tickAtPrice = BIGINT_ZERO + + limitPoolEntity.feeGrowthGlobal0 = BIGINT_ZERO + limitPoolEntity.feeGrowthGlobal1 = BIGINT_ZERO + + limitPoolEntity.samplesLength = BigInt.fromString('5') + + limitPoolEntity.price0 = BIGDECIMAL_ZERO + limitPoolEntity.price1 = BIGDECIMAL_ZERO + + limitPoolEntity.volumeToken0 = BIGDECIMAL_ZERO + limitPoolEntity.volumeToken1 = BIGDECIMAL_ZERO + limitPoolEntity.volumeEth = BIGDECIMAL_ZERO + limitPoolEntity.volumeUsd = BIGDECIMAL_ZERO + limitPoolEntity.feesUsd = BIGDECIMAL_ZERO + limitPoolEntity.feesEth = BIGDECIMAL_ZERO + limitPoolEntity.txnCount = BIGINT_ZERO + + limitPoolEntity.totalValueLocked0 = BIGDECIMAL_ZERO + limitPoolEntity.totalValueLocked1 = BIGDECIMAL_ZERO + limitPoolEntity.totalValueLockedUsd = BIGDECIMAL_ZERO + limitPoolEntity.totalValueLockedEth = BIGDECIMAL_ZERO - if (!managerEntity) { - managerEntity = new LimitPoolManager(address) exists = false } return { - entity: managerEntity, + entity: limitPoolEntity, exists: exists, } } -class LoadFeeTierRet { - entity: FeeTier + +class LoadLimitPoolTokenRet { + entity: LimitPoolToken exists: boolean } -export function safeLoadFeeTier(feeTier: BigInt): LoadFeeTierRet { +export function safeLoadLimitPoolToken(poolTokenAddress: string): LoadLimitPoolTokenRet { let exists = true + let limitPoolTokenEntity = LimitPoolToken.load(poolTokenAddress) - let feeTierId = feeTier.toString() - log.debug('pool volatility tier id: {}', [feeTierId]) + if (!limitPoolTokenEntity) { + limitPoolTokenEntity = new LimitPoolToken(poolTokenAddress) - let feeTierEntity = FeeTier.load(feeTierId) + limitPoolTokenEntity.pool = 'default' - if (!feeTierEntity) { - feeTierEntity = new FeeTier(feeTierId) exists = false } return { - entity: feeTierEntity, + entity: limitPoolTokenEntity, exists: exists, } } -class LoadRangeTickRet { - entity: RangeTick +class LoadLimitPositionRet { + entity: LimitPosition exists: boolean } -export function safeLoadRangeTick(address: string, index: BigInt): LoadRangeTickRet { +export function safeLoadLimitPosition( + poolAddress: string, + positionId: BigInt +): LoadLimitPositionRet { let exists = true + let fromToken: string - let tickId = address - .concat(index.toString()) + let limitPositionId = poolAddress + .concat(positionId.toString()) - let tickEntity = RangeTick.load(tickId) + let positionEntity = LimitPosition.load(limitPositionId) + + if (!positionEntity) { + positionEntity = new LimitPosition(limitPositionId) + + positionEntity.positionId = BIGINT_ZERO + positionEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + positionEntity.lower = BIGINT_ZERO + positionEntity.upper = BIGINT_ZERO + positionEntity.zeroForOne = true + positionEntity.liquidity = BIGINT_ZERO + positionEntity.pool = 'default' + positionEntity.epochLast = BIGINT_ZERO + positionEntity.claimPriceLast = BIGINT_ZERO + positionEntity.amountIn = BIGINT_ZERO + positionEntity.amountFilled = BIGINT_ZERO + positionEntity.tokenIn = 'tokenIn' + positionEntity.tokenOut = 'tokenOut' + + positionEntity.txnHash = Bytes.fromHexString(ZERO_ADDRESS) + positionEntity.createdBy = Bytes.fromHexString(ZERO_ADDRESS) + positionEntity.createdAtTimestamp = BIGINT_ZERO - if (!tickEntity) { - tickEntity = new RangeTick(tickId) - tickEntity.pool = address - tickEntity.index = index - // 1.0001^tick is token1/token0. - tickEntity.price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickEntity.index.toI32())) - tickEntity.price1 = safeDiv(ONE_BD, tickEntity.price0) exists = false } return { - entity: tickEntity, + entity: positionEntity, exists: exists, } } -class LoadLimitTickRet { - entity: LimitTick +class LoadRangePositionRet { + entity: RangePosition exists: boolean } -export function safeLoadLimitTick(address: string, index: BigInt): LoadLimitTickRet { +export function safeLoadRangePosition( + poolAddress: string, + positionId: BigInt +): LoadRangePositionRet { let exists = true + let fromToken: string - let tickId = address - .concat(index.toString()) + let rangePositionId = poolAddress + .concat(positionId.toString()) - let tickEntity = LimitTick.load(tickId) + let positionEntity = RangePosition.load(rangePositionId) - if (!tickEntity) { - tickEntity = new LimitTick(tickId) - tickEntity.pool = address - tickEntity.index = index - // 1.0001^tick is token1/token0. - tickEntity.price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickEntity.index.toI32())) - tickEntity.price1 = safeDiv(ONE_BD, tickEntity.price0) - tickEntity.active = true - exists = false - } + if (!positionEntity) { + positionEntity = new RangePosition(rangePositionId) - return { - entity: tickEntity, - exists: exists, - } -} + positionEntity.positionId = BIGINT_ZERO + positionEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + positionEntity.lower = BIGINT_ZERO + positionEntity.upper = BIGINT_ZERO + positionEntity.liquidity = BIGINT_ZERO + positionEntity.pool = 'default' + positionEntity.staked = false -class LoadLimitPoolFactoryRet { - entity: LimitPoolFactory - exists: boolean -} -export function safeLoadLimitPoolFactory(factoryAddress: string): LoadLimitPoolFactoryRet { - let exists = true - let coverPoolFactoryEntity = LimitPoolFactory.load(factoryAddress) + positionEntity.createdAtTimestamp = BIGINT_ZERO + positionEntity.createdAtBlockNumber = BIGINT_ZERO + positionEntity.updatedAtTimestamp = BIGINT_ZERO + positionEntity.updatedAtBlockNumber = BIGINT_ZERO - if (!coverPoolFactoryEntity) { - coverPoolFactoryEntity = new LimitPoolFactory(factoryAddress) - coverPoolFactoryEntity.poolCount = BIGINT_ZERO exists = false } return { - entity: coverPoolFactoryEntity, + entity: positionEntity, exists: exists, } } - -class LoadLimitPoolRet { - entity: LimitPool - exists: boolean -} -export function safeLoadLimitPool(poolAddress: string): LoadLimitPoolRet { +export function safeLoadRangePositionById( + rangePositionId: string +): LoadRangePositionRet { let exists = true - let limitPoolEntity = LimitPool.load(poolAddress) + let fromToken: string - if (!limitPoolEntity) { - limitPoolEntity = new LimitPool(poolAddress) - exists = false - } + let positionEntity = RangePosition.load(rangePositionId) - return { - entity: limitPoolEntity, - exists: exists, - } -} + if (!positionEntity) { + positionEntity = new RangePosition(rangePositionId) -class LoadLimitPoolTokenRet { - entity: LimitPoolToken - exists: boolean -} -export function safeLoadLimitPoolToken(poolTokenAddress: string): LoadLimitPoolTokenRet { - let exists = true - let limitPoolTokenEntity = LimitPoolToken.load(poolTokenAddress) + positionEntity.positionId = BIGINT_ZERO + positionEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + positionEntity.lower = BIGINT_ZERO + positionEntity.upper = BIGINT_ZERO + positionEntity.liquidity = BIGINT_ZERO + positionEntity.pool = 'default' + positionEntity.staked = false + + positionEntity.createdAtTimestamp = BIGINT_ZERO + positionEntity.createdAtBlockNumber = BIGINT_ZERO + positionEntity.updatedAtTimestamp = BIGINT_ZERO + positionEntity.updatedAtBlockNumber = BIGINT_ZERO - if (!limitPoolTokenEntity) { - limitPoolTokenEntity = new LimitPoolToken(poolTokenAddress) exists = false } return { - entity: limitPoolTokenEntity, + entity: positionEntity, exists: exists, } } -class LoadPoolRouterRet { - entity: PoolRouter +class LoadSwapRet { + entity: Swap exists: boolean } -export function safeLoadPoolRouter(routerAddress: string): LoadPoolRouterRet { +export function safeLoadSwap(event: ethereum.Event, pool: LimitPool): LoadSwapRet { let exists = true - let poolRouterEntity = PoolRouter.load(routerAddress) - if (!poolRouterEntity) { - poolRouterEntity = new PoolRouter(routerAddress) + let swapId = event.transaction.hash.toHex() + .concat('-') + .concat(pool.txnCount.toString()) + let swapEntity = Swap.load(swapId) + + if (!swapEntity) { + swapEntity = new Swap(swapId) + + swapEntity.transaction = 'default' + swapEntity.recipient = Bytes.fromHexString(ZERO_ADDRESS) + swapEntity.timestamp = BIGINT_ZERO + swapEntity.pool = pool.id + swapEntity.zeroForOne = true + swapEntity.amount0 = BIGDECIMAL_ZERO + swapEntity.amount1 = BIGDECIMAL_ZERO + swapEntity.amountUsd = BIGDECIMAL_ZERO + swapEntity.priceAfter = BIGINT_ZERO + swapEntity.tickAfter = BIGINT_ZERO + swapEntity.txnIndex = BIGINT_ZERO + exists = false } return { - entity: poolRouterEntity, + entity: swapEntity, exists: exists, } } -class LoadVFinPositionRet { - entity: VFinPosition +class LoadTransactionRet { + entity: Transaction exists: boolean } -export function safeLoadVFinPosition(vFinAddress: string, positionId: BigInt): LoadVFinPositionRet { +export function safeLoadTransaction(event: ethereum.Event): LoadTransactionRet { let exists = true - let vFinPositionId = vFinAddress.concat(positionId.toString()) + let transactionEntity = Transaction.load(event.transaction.hash.toHex()) - let vFinPositionEntity = VFinPosition.load(vFinPositionId) + if (!transactionEntity) { + transactionEntity = new Transaction(event.transaction.hash.toHex()) + + transactionEntity.sender = event.transaction.from + transactionEntity.blockNumber = event.block.number + transactionEntity.timestamp = event.block.timestamp + transactionEntity.gasLimit = event.transaction.gasLimit + transactionEntity.gasPrice = event.transaction.gasPrice + transactionEntity.swaps = new Array() - if (!vFinPositionEntity) { - vFinPositionEntity = new VFinPosition(vFinPositionId) exists = false } return { - entity: vFinPositionEntity, + entity: transactionEntity, exists: exists, } } -class LoadLimitPositionRet { - entity: LimitPosition +class LoadRangeTickRet { + entity: RangeTick exists: boolean } -export function safeLoadLimitPosition( - poolAddress: string, - positionId: BigInt -): LoadLimitPositionRet { +export function safeLoadRangeTick(address: string, index: BigInt): LoadRangeTickRet { let exists = true - let fromToken: string - let limitPositionId = poolAddress - .concat(positionId.toString()) + let tickId = address + .concat(index.toString()) - let positionEntity = LimitPosition.load(limitPositionId) + let tickEntity = RangeTick.load(tickId) + + if (!tickEntity) { + tickEntity = new RangeTick(tickId) + tickEntity.pool = address + tickEntity.index = index + // 1.0001^tick is token1/token0. + tickEntity.price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickEntity.index.toI32())) + tickEntity.price1 = safeDiv(ONE_BD, tickEntity.price0) + + tickEntity.liquidityDelta = BIGINT_ZERO + tickEntity.liquidityAbsolute = BIGINT_ZERO + + tickEntity.feeGrowthOutside0 = BIGINT_ZERO + tickEntity.feeGrowthOutside1 = BIGINT_ZERO - if (!positionEntity) { - positionEntity = new LimitPosition(limitPositionId) exists = false } return { - entity: positionEntity, + entity: tickEntity, exists: exists, } } -class LoadSwapRet { - entity: Swap +class LoadLimitTickRet { + entity: LimitTick exists: boolean } -export function safeLoadSwap(event: ethereum.Event, pool: LimitPool): LoadSwapRet { +export function safeLoadLimitTick(address: string, index: BigInt): LoadLimitTickRet { let exists = true - let swapId = event.transaction.hash.toHex() - .concat('-') - .concat(pool.txnCount.toString()) - let swapEntity = Swap.load(swapId) + let tickId = address + .concat(index.toString()) + + let tickEntity = LimitTick.load(tickId) + + if (!tickEntity) { + tickEntity = new LimitTick(tickId) + tickEntity.pool = address + tickEntity.index = index + // 1.0001^tick is token1/token0. + tickEntity.active = true + tickEntity.price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickEntity.index.toI32())) + tickEntity.price1 = safeDiv(ONE_BD, tickEntity.price0) + + tickEntity.liquidityDelta = BIGINT_ZERO + tickEntity.liquidityAbsolute = BIGINT_ZERO + + tickEntity.epochLast0 = BIGINT_ZERO + tickEntity.epochLast1 = BIGINT_ZERO - if (!swapEntity) { - swapEntity = new Swap(swapId) - swapEntity.pool = pool.id exists = false } return { - entity: swapEntity, + entity: tickEntity, exists: exists, } } -class LoadRangePositionRet { - entity: RangePosition +class LoadPoolRouterRet { + entity: PoolRouter exists: boolean } -export function safeLoadRangePosition( - poolAddress: string, - positionId: BigInt -): LoadRangePositionRet { +export function safeLoadPoolRouter(routerAddress: string): LoadPoolRouterRet { let exists = true - let fromToken: string - - let rangePositionId = poolAddress - .concat(positionId.toString()) + let poolRouterEntity = PoolRouter.load(routerAddress) - let positionEntity = RangePosition.load(rangePositionId) + if (!poolRouterEntity) { + poolRouterEntity = new PoolRouter(routerAddress) - if (!positionEntity) { - positionEntity = new RangePosition(rangePositionId) + poolRouterEntity.limitPoolFactory = Bytes.fromHexString(ZERO_ADDRESS) + poolRouterEntity.coverPoolFactory = Bytes.fromHexString(ZERO_ADDRESS) exists = false } return { - entity: positionEntity, + entity: poolRouterEntity, exists: exists, } } -export function safeLoadRangePositionById( - rangePositionId: string -): LoadRangePositionRet { + +class LoadVFinPositionRet { + entity: VFinPosition + exists: boolean +} +export function safeLoadVFinPosition(vFinAddress: string, positionId: BigInt): LoadVFinPositionRet { let exists = true - let fromToken: string - let positionEntity = RangePosition.load(rangePositionId) + let vFinPositionId = vFinAddress.concat(positionId.toString()) - if (!positionEntity) { - positionEntity = new RangePosition(rangePositionId) + let vFinPositionEntity = VFinPosition.load(vFinPositionId) + + if (!vFinPositionEntity) { + vFinPositionEntity = new VFinPosition(vFinPositionId) + + vFinPositionEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + vFinPositionEntity.positionId = BIGINT_ZERO + vFinPositionEntity.vFinAddress = Bytes.fromHexString(ZERO_ADDRESS) exists = false } return { - entity: positionEntity, + entity: vFinPositionEntity, exists: exists, } } @@ -507,11 +682,24 @@ export function safeLoadHistoricalOrder(tokenInAddress: string, tokenOutAddress: if (!historicalOrderEntity) { historicalOrderEntity = new HistoricalOrder(historicalOrderId) - historicalOrderEntity.positionId = BIGINT_ZERO + + historicalOrderEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + + historicalOrderEntity.pool = 'default' historicalOrderEntity.tokenIn = tokenInAddress historicalOrderEntity.tokenOut = tokenOutAddress + historicalOrderEntity.txnHash = Bytes.fromHexString(ZERO_ADDRESS) + historicalOrderEntity.amountIn = BIGDECIMAL_ZERO historicalOrderEntity.amountOut = BIGDECIMAL_ZERO + historicalOrderEntity.averagePrice = BIGDECIMAL_ZERO + historicalOrderEntity.completedAtTimestamp = BIGINT_ZERO + + historicalOrderEntity.usdValue = BIGDECIMAL_ZERO + + historicalOrderEntity.positionId = BIGINT_ZERO + historicalOrderEntity.touches = BIGINT_ZERO + historicalOrderEntity.completed = true exists = false @@ -534,6 +722,12 @@ export function safeLoadTotalSeasonReward(factoryAddress: string): LoadTotalSeas if (!totalSeasonRewardEntity) { totalSeasonRewardEntity = new TotalSeasonReward(factoryAddress) + + totalSeasonRewardEntity.whitelistedFeesUsd = BIGDECIMAL_ZERO + totalSeasonRewardEntity.nonWhitelistedFeesUsd = BIGDECIMAL_ZERO + totalSeasonRewardEntity.volumeTradedUsd = BIGDECIMAL_ZERO + totalSeasonRewardEntity.stakingPoints = BIGINT_ZERO + exists = false } @@ -555,6 +749,11 @@ export function safeLoadUserSeasonReward(userAddress: string): LoadUserSeasonRew if (!userSeasonRewardEntity) { userSeasonRewardEntity = new UserSeasonReward(userAddress) + userSeasonRewardEntity.whitelistedFeesUsd = BIGDECIMAL_ZERO + userSeasonRewardEntity.nonWhitelistedFeesUsd = BIGDECIMAL_ZERO + userSeasonRewardEntity.volumeTradedUsd = BIGDECIMAL_ZERO + userSeasonRewardEntity.stakingPoints = BIGINT_ZERO + exists = false } diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 042fae34..2c173b5b 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -172,7 +172,7 @@ dataSources: - event: ProtocolFillFeesModified(address[],int16[],int16[]) handler: handleProtocolFillFeesModified - event: ProtocolSwapFeesModified(address[],int16[],int16[]) - handler: ProtocolSwapFeesModified + handler: handleProtocolSwapFeesModified - kind: ethereum/contract name: PoolsharkRouter network: scroll-sepolia From 8d6f2cf928ff54e95e05149ce15be84bec5f1187 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Wed, 17 Jan 2024 10:28:32 -0700 Subject: [PATCH 37/39] arbitrum goerli cleanup --- scripts/autogen/contract-deployments.json | 203 ---------------------- subgraph/package.json | 14 -- tasks/deploy/utils/mintPosition.ts | 15 +- 3 files changed, 8 insertions(+), 224 deletions(-) diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index 7a6dcbda..15a2c775 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -204,209 +204,6 @@ "created": "2024-01-14T02:58:50.837Z" } }, - "arb_goerli": { - "token0": { - "contractName": "Token20", - "contractAddress": "0xebff7a98149b4774c9743c5d1f382305fe5422c9", - "constructorArguments": [ - "USD Coin", - "USDC", - 6 - ], - "created": "2023-08-23T15:56:24.723Z" - }, - "token1": { - "contractName": "Token20", - "contractAddress": "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7", - "constructorArguments": [ - "Wrapped Ether", - "WETH", - 18 - ], - "created": "2023-08-23T15:56:24.715Z" - }, - "tickMapLib": { - "contractName": "TickMap", - "contractAddress": "0xfc2bb8b23eecd9592ecb84453a41578a39008314", - "constructorArguments": [], - "created": "2023-11-25T12:44:23.611Z" - }, - "ticksLib": { - "contractName": "Ticks", - "contractAddress": "0x8551385fffec5f0266093a2d6c5a08b917bc781e", - "constructorArguments": [], - "created": "2023-11-25T12:44:25.752Z" - }, - "rangePositionsLib": { - "contractName": "RangePositions", - "contractAddress": "0x4dfea8437c7eb1667805ddf95c096cd2ab925f56", - "constructorArguments": [], - "created": "2023-11-25T12:44:27.302Z" - }, - "limitPositionsLib": { - "contractName": "LimitPositions", - "contractAddress": "0x436f4c3fc60369cd0eb99acd716de4e303c30f36", - "constructorArguments": [], - "created": "2023-11-25T12:44:28.911Z" - }, - "limitPoolManager": { - "contractName": "LimitPoolManager", - "contractAddress": "0x6d3af137e75097b892683832a2c0132f99625d0e", - "constructorArguments": [], - "created": "2023-11-25T12:44:30.623Z" - }, - "limitPoolFactory": { - "contractName": "LimitPoolFactory", - "contractAddress": "0x1b215002e688135549cc0290d6cf1f94e3aa425c", - "constructorArguments": [ - "0x6D3AF137E75097B892683832a2c0132f99625d0e" - ], - "created": "2023-11-25T12:44:32.085Z" - }, - "swapCall": { - "contractName": "SwapCall", - "contractAddress": "0x4017cade72ea448a46ee0176eacef49c0e91d6f5", - "constructorArguments": [], - "created": "2023-11-25T12:44:33.671Z" - }, - "mintRangeCall": { - "contractName": "MintRangeCall", - "contractAddress": "0x767ace5b40c9c70159051b55895f1dc4d5ad770d", - "constructorArguments": [], - "created": "2023-11-25T12:44:35.331Z" - }, - "burnRangeCall": { - "contractName": "BurnRangeCall", - "contractAddress": "0x9d9102bb05a935c5aa2cd350e7fa956aed2e283c", - "constructorArguments": [], - "created": "2023-11-25T12:44:36.992Z" - }, - "mintLimitCall": { - "contractName": "MintLimitCall", - "contractAddress": "0x57b6524810baaacd9726c26942bfe04500569271", - "constructorArguments": [], - "created": "2023-11-25T12:44:38.947Z" - }, - "burnLimitCall": { - "contractName": "BurnLimitCall", - "contractAddress": "0x581a56e3140beb3ff2f34d549daff05ac5553d36", - "constructorArguments": [], - "created": "2023-11-25T12:44:40.594Z" - }, - "snapshotLimitCall": { - "contractName": "SnapshotLimitCall", - "contractAddress": "0x33bb5144306c7e29abf6fc5663cf2f370648f651", - "constructorArguments": [], - "created": "2023-11-25T12:44:42.153Z" - }, - "quoteCall": { - "contractName": "QuoteCall", - "contractAddress": "0x1bcb9a43ea1a76c326341167e2d3b06267740dad", - "constructorArguments": [], - "created": "2023-11-25T12:44:43.948Z" - }, - "feesCall": { - "contractName": "FeesCall", - "contractAddress": "0x22c2269dc214d93d61afceb7e9228ae8e3d27eac", - "constructorArguments": [], - "created": "2023-11-25T12:44:45.406Z" - }, - "sampleCall": { - "contractName": "SampleCall", - "contractAddress": "0x41fc7610d53358fb9aa75e7e4c1c4558a3509905", - "constructorArguments": [], - "created": "2023-11-25T12:44:47.083Z" - }, - "snapshotRangeCall": { - "contractName": "SnapshotRangeCall", - "contractAddress": "0xf6485573b3e88e9906d87bc51f5e526990a8209d", - "constructorArguments": [], - "created": "2023-11-25T12:44:48.554Z" - }, - "limitPoolImpl": { - "contractName": "LimitPool", - "contractAddress": "0x50903014ec42471b8f628ce137acec4f860b2d8c", - "constructorArguments": [ - "0x1b215002E688135549CC0290d6Cf1F94E3AA425c" - ], - "created": "2023-11-25T12:44:50.187Z" - }, - "positionERC1155": { - "contractName": "PositionERC1155", - "contractAddress": "0xcf5b954159719a3eb768d18b16d23d85d516ec39", - "constructorArguments": [ - "0x1b215002E688135549CC0290d6Cf1F94E3AA425c" - ], - "created": "2023-11-25T12:44:51.809Z" - }, - "poolRouter": { - "contractName": "PoolsharkRouter", - "contractAddress": "0xd4f769b054252a3bcbabab3f280315ce67346a65", - "constructorArguments": [ - "0x1b215002e688135549cc0290d6cf1f94e3aa425c", - "0xf8f75a39663e97d36da4be2882608513512f4cf0", - "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7" - ], - "created": "2023-12-08T18:17:44.533Z" - }, - "limitPool": { - "contractName": "LimitPool", - "contractAddress": "0x4998545e13a668a884272aaebff14ab21c5b4e89", - "constructorArguments": [ - "0xebff7a98149b4774c9743c5d1f382305fe5422c9", - "0xEfb283eF3167CA2eE9D93B201af15e2af3f6e8c7", - "1000", - 0 - ], - "created": "2023-11-25T12:45:02.690Z" - }, - "coverPoolFactory": { - "contractName": "CoverPoolFactory", - "contractAddress": "0xf8f75a39663e97d36da4be2882608513512f4cf0", - "constructorArguments": [ - "0x7877A681D1290d80ed0272A920b9a5CeE8Fd2fAC" - ], - "created": "2023-10-01T13:30:41.488Z" - }, - "tokenA": { - "contractName": "Token20", - "contractAddress": "0xd1c4abd91dc01684ac47bf26c5109db19b636c15", - "constructorArguments": [ - "ChainLink Token", - "LINK", - 18 - ], - "created": "2023-10-20T14:15:25.994Z" - }, - "tokenB": { - "contractName": "Token20", - "contractAddress": "0xaf412fe6adac79b04f22140ecb583b2fed3a0d8e", - "constructorArguments": [ - "Wrapped BTC", - "WBTC", - 8 - ], - "created": "2023-10-20T14:15:30.017Z" - }, - "weth9": { - "contractName": "WETH9", - "contractAddress": "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7", - "constructorArguments": [], - "created": "2023-11-25T12:32:49.206Z" - }, - "rangeStaker": { - "contractName": "RangeStaker", - "contractAddress": "0xe09aebe3c2a008fc9a3528893e9ead1f30dd6d5a", - "constructorArguments": [ - { - "limitPoolFactory": "0x1b215002e688135549cc0290d6cf1f94e3aa425c", - "startTime": 0, - "endTime": 2000707154 - } - ], - "created": "2023-12-08T18:17:46.387Z" - } - }, "scrollSepolia": { "token0": { "contractName": "Token20", diff --git a/subgraph/package.json b/subgraph/package.json index 47c8a9a3..3cc54d57 100644 --- a/subgraph/package.json +++ b/subgraph/package.json @@ -17,24 +17,10 @@ "codegen": "graph codegen", "build": "graph build", "deploy": "graph deploy --product hosted-service alphak3y/poolshark-limit", - "deploy-sats": "graph deploy limit-arbitrumGoerli --version-label v0.1.0 --node https://app.satsuma.xyz/api/subgraphs/deploy --deploy-key 7NoUUXPcOGfBX --ipfs https://ipfs.satsuma.xyz", - "deploy-staging": "graph deploy --version-label v0.3.1 --node https://api.graph-eu.p2pify.com/cc955503f93d46512a78fb9a70796dac/deploy --ipfs https://api.graph-eu.p2pify.com/cc955503f93d46512a78fb9a70796dac/ipfs staging-limit-arbitrumGoerli", - "deploy-beta2": "graph deploy --version-label v0.4.0 --node https://api.graph-ams.p2pify.com/3bd8a0adf573b40768f68ef92c7bd843/deploy --ipfs https://api.graph-ams.p2pify.com/3bd8a0adf573b40768f68ef92c7bd843/ipfs limit-arbitrumGoerli-beta2", - "deploy-test": "graph deploy --version-label v0.3.0 --node https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/deploy --ipfs https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/ipfs limit-arbitrumGoerli-test", - "deploy-ordertest": "graph deploy --version-label v0.1.0 --node https://api.graph-ams.p2pify.com/719c33840eec545dba4ed49d362448f0/deploy --ipfs https://api.graph-ams.p2pify.com/719c33840eec545dba4ed49d362448f0/ipfs limit-arbitrumGoerli-order-testing", - "deploy-chainstack": "graph deploy --node https://api.graph-eu.p2pify.com/cc955503f93d46512a78fb9a70796dac/deploy --ipfs https://api.graph-eu.p2pify.com/cc955503f93d46512a78fb9a70796dac/ipfs staging-limit-arbitrumGoerli", - "deploy-op-test": "graph deploy --product hosted-service alphak3y/poolshark-cover-op-goerli", - "deploy-hs": "graph deploy --product hosted-service alphak3y/poolshark-limit", - "deploy-local": "graph deploy example --ipfs http://localhost:5001 --node http://127.0.0.1:8020", - "deploy-arb-sepolia": "graph deploy --version-label v0.1.0 --node https://api.graph-ams.p2pify.com/5e506be8e1163614d8b8d978acfdeca7/deploy --ipfs https://api.graph-ams.p2pify.com/5e506be8e1163614d8b8d978acfdeca7/ipfs limit-arbitrum-sepolia", - "deploy-arb-one": "graph deploy --version-label v0.1.1 --node https://api.graph-ams.p2pify.com/e57bc8ecd57c8bac9bfc6222cb8257e7/deploy --ipfs https://api.graph-ams.p2pify.com/e57bc8ecd57c8bac9bfc6222cb8257e7/ipfs limit-arbitrum", - "deploy-arb-one-2": "graph deploy --version-label v0.1.2 --node https://api.graph-ams.p2pify.com/1600f38da0b118b394f0184f131232a1/deploy --ipfs https://api.graph-ams.p2pify.com/1600f38da0b118b394f0184f131232a1/ipfs limit-arbitrum-redeploy", "deploy-scroll": "graph deploy --product hosted-service alphak3y/poolshark-limit-scroll-mainnet", "deploy-scroll-sepolia": "graph deploy --product hosted-service alphak3y/limit-scroll-sepolia", "test": "graph codegen; graph test -v 0.2.0", "prepare:subgraph": "mustache ./config/${NETWORK}.json ./templates/${BLOCKCHAIN}.subgraph.template.yaml > subgraph.yaml && rm -rf generated && npm run generate:schema && npm run subgraph:codegen && npm run subgraph:build", - "subgraph:deploy:chainstack:test": "graph deploy --version-label v0.3.1 --node https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/deploy --ipfs https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/ipfs limit-arbitrumGoerli-test", - "subgraph:deploy:chainstack:ordertest": "graph deploy --version-label v0.1.0 --node https://api.graph-ams.p2pify.com/719c33840eec545dba4ed49d362448f0/deploy --ipfs https://api.graph-ams.p2pify.com/719c33840eec545dba4ed49d362448f0/ipfs limit-arbitrumGoerli-order-testing", "deploy:chainstack:arb-goerli": "BLOCKCHAIN=arbitrum =arbitrum-goerli env-cmd npm run prepare:subgraph && SLUG=aave/protocol-v2-goerli env-cmd npm run subgraph:deploy:chainstack:ordertest" }, "dependencies": { diff --git a/tasks/deploy/utils/mintPosition.ts b/tasks/deploy/utils/mintPosition.ts index 55bcc863..9bb30265 100644 --- a/tasks/deploy/utils/mintPosition.ts +++ b/tasks/deploy/utils/mintPosition.ts @@ -3,6 +3,7 @@ import { BN_ZERO, } from '../../../test/utils/contracts/limitpool' import { InitialSetup } from '../../../test/utils/setup/initialSetup' import { getNonce } from '../../utils' import { parseUnits } from 'ethers/lib/utils' +import { mintSigners20 } from '../../../test/utils/token' export class MintPosition { private initialSetup: InitialSetup @@ -31,9 +32,9 @@ export class MintPosition { await this.initialSetup.readLimitPoolSetup(this.nonce) console.log('read positions') - // const token1Amount = ethers.utils.parseUnits('100', await hre.props.token1.decimals()) + const token0Amount = ethers.utils.parseUnits('100', await hre.props.token0.decimals()) - // await mintSigners20(hre.props.token0, token0Amount.mul(10000), [hre.props.alice]) + await mintSigners20(hre.props.token0, token0Amount, [hre.props.alice]) // await mintSigners20(hre.props.token1, token1Amount.mul(10000), [hre.props.alice]) const liquidityAmount = '49802891105937278098768' @@ -83,10 +84,10 @@ export class MintPosition { // ) // console.log('position snapshot', snapshot.feesOwed0.toString(), snapshot.feesOwed1.toString()) - const amountIn = parseUnits('1', 16) + const amountIn = parseUnits('20', 18) const signer = hre.props.alice // let approveTxn = await hre.props.token0.connect(signer).approve(hre.props.poolRouter.address, amountIn) - let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, amountIn) + let approveTxn = await hre.props.token0.connect(signer).approve(hre.props.poolRouter.address, amountIn) await approveTxn.wait() // const aliceId = await validateMintRange({ // signer: hre.props.alice, @@ -108,11 +109,11 @@ export class MintPosition { // // // for (let i=0; i < 20; i++) { // 0 => 1 - //0xe80dc9a69853483c745f8e32162f0bd5813cb291 => new factory + //0xe80dc9a69853483c745f8e32162f0bd5813c b291 => new factory - const zeroForOne = false + const zeroForOne = true - const priceLimit = BigNumber.from('2172618421097231267834892073346') + const priceLimit = BigNumber.from('1553792102639747119534487568') let txn = await hre.props.poolRouter .connect(signer) .multiSwapSplit( From 817c3dc62ce89a0b962776ed1810e12191088867 Mon Sep 17 00:00:00 2001 From: alphak3y Date: Sat, 20 Jan 2024 14:03:51 -0700 Subject: [PATCH 38/39] sync branch --- scripts/autogen/contract-deployments-keys.ts | 84 ------------------- scripts/autogen/contract-deployments.json | 8 +- subgraph/src/constants/arbitrum-goerli.ts | 49 ----------- subgraph/src/constants/arbitrum-sepolia.ts | 3 +- subgraph/src/constants/arbitrum.ts | 1 + subgraph/src/constants/constants.ts | 6 +- subgraph/src/mappings/utils/loads.ts | 2 +- subgraph/src/mappings/v3pool.ts | 14 ++-- subgraph/subgraph.yaml | 36 ++++---- .../scroll-sepolia.subgraph.template.yaml | 2 +- tasks/deploy/utils/mintPosition.ts | 20 +++-- test/utils/setup/initialSetup.ts | 6 +- 12 files changed, 51 insertions(+), 180 deletions(-) delete mode 100644 subgraph/src/constants/arbitrum-goerli.ts diff --git a/scripts/autogen/contract-deployments-keys.ts b/scripts/autogen/contract-deployments-keys.ts index 614c0c65..d500f8da 100644 --- a/scripts/autogen/contract-deployments-keys.ts +++ b/scripts/autogen/contract-deployments-keys.ts @@ -1,92 +1,8 @@ import { ContractDeploymentsKey } from '../util/files/contractDeploymentsJson' export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ - { - networkName: 'arb_sepolia', - objectName: 'token0' - }, - { - networkName: 'arb_sepolia', - objectName: 'weth9' - }, - { - networkName: 'arb_sepolia', - objectName: 'tickMapLib' - }, - { - networkName: 'arb_sepolia', - objectName: 'ticksLib' - }, - { - networkName: 'arb_sepolia', - objectName: 'limitPositionsLib' - }, - { - networkName: 'arb_sepolia', - objectName: 'limitPoolManager' - }, - { - networkName: 'arb_sepolia', - objectName: 'limitPoolFactory' - }, - { - networkName: 'arb_sepolia', - objectName: 'swapCall' - }, - { - networkName: 'arb_sepolia', - objectName: 'mintRangeCall' - }, - { - networkName: 'arb_sepolia', - objectName: 'burnRangeCall' - }, - { - networkName: 'arb_sepolia', - objectName: 'mintLimitCall' - }, - { - networkName: 'arb_sepolia', - objectName: 'burnLimitCall' - }, - { - networkName: 'arb_sepolia', - objectName: 'snapshotLimitCall' - }, - { - networkName: 'arb_sepolia', - objectName: 'quoteCall' - }, - { - networkName: 'arb_sepolia', - objectName: 'feesCall' - }, - { - networkName: 'arb_sepolia', - objectName: 'sampleCall' - }, - { - networkName: 'arb_sepolia', - objectName: 'snapshotRangeCall' - }, - { - networkName: 'arb_sepolia', - objectName: 'limitPoolImpl' - }, - { - networkName: 'arb_sepolia', - objectName: 'positionERC1155' - }, - { - networkName: 'arb_sepolia', - objectName: 'limitPool' - }, { networkName: 'arb_sepolia', objectName: 'poolRouter' }, - { - networkName: 'arb_sepolia', - objectName: 'rangeStaker' - }, ]; \ No newline at end of file diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index 15a2c775..ba3a29c8 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -390,7 +390,7 @@ }, "token0": { "contractName": "Token20", - "contractAddress": "0xcd453b942f35adf0364d89c05a892518825c1c3b", + "contractAddress": "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", "constructorArguments": [ "Dai Stablecoin", "DAI", @@ -537,13 +537,13 @@ }, "poolRouter": { "contractName": "PoolsharkRouter", - "contractAddress": "0x33df95efe07a3b3e69ba31438ae511d360d89b32", + "contractAddress": "0x73ac9a2e665925719d9c272a3df60b97dbc3e50d", "constructorArguments": [ "0x8e40c68b7546efd009a1a300c92e25da3c8725dc", "0x0000000000000000000000000000000000000000", - "0xfa807ce77b103129597cce4a7bf7f504f9e7bd9e" + "0x414b73f989e7ca0653b5c98186749a348405e6d5" ], - "created": "2024-01-09T01:02:54.485Z" + "created": "2024-01-17T00:16:04.942Z" }, "rangeStaker": { "contractName": "RangeStaker", diff --git a/subgraph/src/constants/arbitrum-goerli.ts b/subgraph/src/constants/arbitrum-goerli.ts deleted file mode 100644 index b2760092..00000000 --- a/subgraph/src/constants/arbitrum-goerli.ts +++ /dev/null @@ -1,49 +0,0 @@ -/* eslint-disable */ -import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts' -import { LimitPoolFactory as FactoryContract } from '../../generated/templates/LimitPoolTemplate/LimitPoolFactory' -export let FACTORY_ADDRESS = '0x1b215002e688135549cc0290d6cf1f94e3aa425c' -export let RANGE_STAKER_ADDRESS = '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' -export let WETH_ADDRESS = '0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7' - -// tokens where USD value is safe to use for globals -export let WHITELISTED_TOKENS: string[] = [ - '0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7', // WETH - '0x19bee8e887a5db5cf20a841eb4daacbcacf14b1b', // DAI - '0xebff7a98149b4774c9743c5d1f382305fe5422c9' // USDC -] - -export let WHITELISTED_PAIRS: string[] = [ - '0xebff7a98149b4774c9743c5d1f382305fe5422c9-0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7' // WETH - USDC -] - -export let SEASON_1_START_TIME = BigInt.fromString('0') -export let SEASON_1_END_TIME = BigInt.fromString('2000707154') - -// used for safe eth pricing -export let STABLE_COINS: string[] = [ - '0x19bee8e887a5db5cf20a841eb4daacbcacf14b1b', //DAI - '0xebff7a98149b4774c9743c5d1f382305fe5422c9' //USDC -] - -// used for safe eth pricing -export const STABLE_POOL_ADDRESS = '0x4998545e13a668a884272aaebff14ab21c5b4e89' - -// determines which token to use for eth <-> usd rate, true means stable is token0 in pool above -export const STABLE_IS_TOKEN_0 = false - -// minimum eth required in pool to count usd values towards global prices -export let MINIMUM_ETH_LOCKED = BigDecimal.fromString('0') - -// pool that breaks with subgraph logic -export const ERROR_POOL = '0x0000000000000000000000000000000000000000' - -export let ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' - -export let ZERO_BI = BigInt.fromI32(0) -export let ONE_BI = BigInt.fromI32(1) -export let ZERO_BD = BigDecimal.fromString('0') -export let ONE_BD = BigDecimal.fromString('1') -export let TWO_BD = BigDecimal.fromString('2') -export let BI_18 = BigInt.fromI32(18) - -export let factoryContract = FactoryContract.bind(Address.fromString(FACTORY_ADDRESS)) \ No newline at end of file diff --git a/subgraph/src/constants/arbitrum-sepolia.ts b/subgraph/src/constants/arbitrum-sepolia.ts index 8a3c5f1f..aa0c929b 100644 --- a/subgraph/src/constants/arbitrum-sepolia.ts +++ b/subgraph/src/constants/arbitrum-sepolia.ts @@ -10,7 +10,8 @@ export const STABLE_POOL_ADDRESS = '0x02225f6a3d83648d7906a23856331c819265394d' // -------------- END REQUIRED CONFIG PER DEPLOYMENT -------------- // determines which token to use for eth <-> usd rate, true means stable is token0 in pool above -export const STABLE_IS_TOKEN_0 = true +export const STABLE_IS_TOKEN_0 = false +export const STABLE_TOKEN_DECIMALS = '18' // chain WETH address export let WETH_ADDRESS = '0x414b73f989e7ca0653b5c98186749a348405e6d5' diff --git a/subgraph/src/constants/arbitrum.ts b/subgraph/src/constants/arbitrum.ts index 631894c9..662446d7 100644 --- a/subgraph/src/constants/arbitrum.ts +++ b/subgraph/src/constants/arbitrum.ts @@ -11,6 +11,7 @@ export const STABLE_POOL_ADDRESS = '0xc31e54c7a869b9fcbecc14363cf510d1c41fa443' // determines which token to use for eth <-> usd rate, true means stable is token0 in pool above export const STABLE_IS_TOKEN_0 = false +export const STABLE_TOKEN_DECIMALS = '6' // chain WETH address export let WETH_ADDRESS = '0x82af49447d8a07e3bd95bd0d56f35241523fbab1' diff --git a/subgraph/src/constants/constants.ts b/subgraph/src/constants/constants.ts index 61c08ad8..04d6d883 100644 --- a/subgraph/src/constants/constants.ts +++ b/subgraph/src/constants/constants.ts @@ -1,5 +1,5 @@ import { - STABLE_IS_TOKEN_0, FACTORY_ADDRESS, RANGE_STAKER_ADDRESS, STABLE_POOL_ADDRESS, WETH_ADDRESS, WHITELISTED_TOKENS, WHITELISTED_PAIRS, SEASON_1_END_TIME, SEASON_1_START_TIME, STABLE_COINS, MINIMUM_ETH_LOCKED, ERROR_POOL, ZERO_ADDRESS, ZERO_BI, ONE_BI, ONE_BD, ZERO_BD, TWO_BD, BI_18, factoryContract -} from "./scroll-sepolia"; + STABLE_IS_TOKEN_0, FACTORY_ADDRESS, RANGE_STAKER_ADDRESS, STABLE_POOL_ADDRESS, STABLE_TOKEN_DECIMALS, WETH_ADDRESS, WHITELISTED_TOKENS, WHITELISTED_PAIRS, SEASON_1_END_TIME, SEASON_1_START_TIME, STABLE_COINS, MINIMUM_ETH_LOCKED, ERROR_POOL, ZERO_ADDRESS, ZERO_BI, ONE_BI, ONE_BD, ZERO_BD, TWO_BD, BI_18, factoryContract +} from "./arbitrum-sepolia"; -export { STABLE_IS_TOKEN_0, FACTORY_ADDRESS, RANGE_STAKER_ADDRESS, STABLE_POOL_ADDRESS, WETH_ADDRESS, WHITELISTED_TOKENS, WHITELISTED_PAIRS, SEASON_1_END_TIME, SEASON_1_START_TIME, STABLE_COINS, MINIMUM_ETH_LOCKED, ERROR_POOL, ZERO_ADDRESS, ZERO_BI, ONE_BI, ONE_BD, ZERO_BD, TWO_BD, BI_18, factoryContract}; \ No newline at end of file +export { STABLE_IS_TOKEN_0, FACTORY_ADDRESS, RANGE_STAKER_ADDRESS, STABLE_POOL_ADDRESS, STABLE_TOKEN_DECIMALS, WETH_ADDRESS, WHITELISTED_TOKENS, WHITELISTED_PAIRS, SEASON_1_END_TIME, SEASON_1_START_TIME, STABLE_COINS, MINIMUM_ETH_LOCKED, ERROR_POOL, ZERO_ADDRESS, ZERO_BI, ONE_BI, ONE_BD, ZERO_BD, TWO_BD, BI_18, factoryContract}; \ No newline at end of file diff --git a/subgraph/src/mappings/utils/loads.ts b/subgraph/src/mappings/utils/loads.ts index 213b340e..8c701450 100644 --- a/subgraph/src/mappings/utils/loads.ts +++ b/subgraph/src/mappings/utils/loads.ts @@ -11,7 +11,7 @@ import { } from './helpers' import { bigDecimalExponated, safeDiv } from './math' import { getEthPriceInUSD } from './price' -import { ZERO_ADDRESS } from '../../constants/arbitrum-goerli' +import { ZERO_ADDRESS } from '../../constants/constants' class LoadLimitPoolFactoryRet { entity: LimitPoolFactory diff --git a/subgraph/src/mappings/v3pool.ts b/subgraph/src/mappings/v3pool.ts index 89aa864e..713237d5 100644 --- a/subgraph/src/mappings/v3pool.ts +++ b/subgraph/src/mappings/v3pool.ts @@ -1,6 +1,6 @@ import { BigInt, log } from "@graphprotocol/graph-ts" import { Token } from "../../generated/schema" -import { STABLE_POOL_ADDRESS, STABLE_IS_TOKEN_0 } from "../constants/constants" +import { STABLE_POOL_ADDRESS, STABLE_IS_TOKEN_0, STABLE_TOKEN_DECIMALS } from "../constants/constants" import { safeLoadBasePrice, safeLoadLimitPool } from "./utils/loads" import { sqrtPriceX96ToTokenPrices } from "./utils/price" import { Initialize, Swap } from "../../generated/v3EthUsdcPool/v3Pool" @@ -22,9 +22,9 @@ export function handleV3Initialize(event: Initialize): void { if (STABLE_IS_TOKEN_0) { log.warning("usdc token", []) - token0 = new Token('USDC') + token0 = new Token('STABLE') log.warning("usdc decimals", []) - token0.decimals = BigInt.fromString('6') + token0.decimals = BigInt.fromString(STABLE_TOKEN_DECIMALS) log.warning("weth token", []) token1 = new Token('WETH') log.warning("weth decimals", []) @@ -32,8 +32,8 @@ export function handleV3Initialize(event: Initialize): void { } else { token0 = new Token('WETH') token0.decimals = BigInt.fromString('18') - token1 = new Token('USDC') - token1.decimals = BigInt.fromString('6') + token1 = new Token('STABLE') + token1.decimals = BigInt.fromString(STABLE_TOKEN_DECIMALS) } log.warning("about to convert to prices", []) @@ -68,7 +68,7 @@ export function handleV3Swap(event: Swap): void { log.warning("usdc token", []) token0 = new Token('USDC') log.warning("usdc decimals", []) - token0.decimals = BigInt.fromString('6') + token0.decimals = BigInt.fromString(STABLE_TOKEN_DECIMALS) log.warning("weth token", []) token1 = new Token('WETH') log.warning("weth decimals", []) @@ -77,7 +77,7 @@ export function handleV3Swap(event: Swap): void { token0 = new Token('WETH') token0.decimals = BigInt.fromString('18') token1 = new Token('USDC') - token1.decimals = BigInt.fromString('6') + token1.decimals = BigInt.fromString(STABLE_TOKEN_DECIMALS) } log.warning("about to convert to prices", []) diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 2c173b5b..f15203dd 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -6,7 +6,7 @@ schema: templates: - kind: ethereum/contract name: LimitPoolTemplate - network: scroll-sepolia + network: arbitrum-sepolia source: abi: LimitPool mapping: @@ -61,7 +61,7 @@ templates: # ERC-1155 events - kind: ethereum/contract name: PositionERC1155Template - network: scroll-sepolia + network: arbitrum-sepolia source: abi: PositionERC1155 mapping: @@ -84,7 +84,7 @@ templates: # RangeStaker events - kind: ethereum/contract name: RangeStakerTemplate - network: scroll-sepolia + network: arbitrum-sepolia source: abi: RangeStaker mapping: @@ -111,11 +111,11 @@ templates: dataSources: - kind: ethereum/contract name: LimitPoolFactory - network: scroll-sepolia + network: arbitrum-sepolia source: - address: '0xd506193e48f13438e35a1edc4ef5394876b5efa4' + address: '0x8e40c68b7546efd009a1a300c92e25da3c8725dc' abi: LimitPoolFactory - startBlock: 2764010 + startBlock: 5789864 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -140,11 +140,11 @@ dataSources: handler: handlePoolCreated - kind: ethereum/contract name: LimitPoolManager - network: scroll-sepolia + network: arbitrum-sepolia source: - address: '0x7113aec7595accbaea1e61bc59f97e35d3a87bd5' + address: '0x0385d0a8d169b1f1510bb5fa0be264463002f6f6' abi: LimitPoolManager - startBlock: 2764009 + startBlock: 5789859 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -175,11 +175,11 @@ dataSources: handler: handleProtocolSwapFeesModified - kind: ethereum/contract name: PoolsharkRouter - network: scroll-sepolia + network: arbitrum-sepolia source: - address: '0x14ce728bf96d5ec0976f332605fe80a42ec0b244' + address: '0x33df95efe07a3b3e69ba31438ae511d360d89b32' abi: PoolsharkRouter - startBlock: 2764065 + startBlock: 5789931 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -195,11 +195,11 @@ dataSources: handler: handleRouterDeployed - kind: ethereum/contract name: vFIN - network: scroll-sepolia + network: arbitrum-sepolia source: - address: '0x1a16415867fde435d3f8a6631ef308ae7bf96b3b' + address: '0xed0ef8101900dfed208ac99ce47b723debb6bfe6' abi: vFIN - startBlock: 2764254 + startBlock: 5795375 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -215,11 +215,11 @@ dataSources: handler: handleTransfer - kind: ethereum/contract name: v3EthUsdcPool - network: scroll-sepolia + network: arbitrum-sepolia source: - address: '0xea578474c65f859739c8ec362543eedc04f0d48e' + address: '0x02225f6a3d83648d7906a23856331c819265394d' abi: v3Pool - startBlock: 2764030 + startBlock: 5789922 mapping: kind: ethereum/events apiVersion: 0.0.6 diff --git a/subgraph/templates/scroll-sepolia.subgraph.template.yaml b/subgraph/templates/scroll-sepolia.subgraph.template.yaml index 042fae34..2c173b5b 100644 --- a/subgraph/templates/scroll-sepolia.subgraph.template.yaml +++ b/subgraph/templates/scroll-sepolia.subgraph.template.yaml @@ -172,7 +172,7 @@ dataSources: - event: ProtocolFillFeesModified(address[],int16[],int16[]) handler: handleProtocolFillFeesModified - event: ProtocolSwapFeesModified(address[],int16[],int16[]) - handler: ProtocolSwapFeesModified + handler: handleProtocolSwapFeesModified - kind: ethereum/contract name: PoolsharkRouter network: scroll-sepolia diff --git a/tasks/deploy/utils/mintPosition.ts b/tasks/deploy/utils/mintPosition.ts index 9bb30265..cbfa2656 100644 --- a/tasks/deploy/utils/mintPosition.ts +++ b/tasks/deploy/utils/mintPosition.ts @@ -32,11 +32,11 @@ export class MintPosition { await this.initialSetup.readLimitPoolSetup(this.nonce) console.log('read positions') - const token0Amount = ethers.utils.parseUnits('100', await hre.props.token0.decimals()) - - await mintSigners20(hre.props.token0, token0Amount, [hre.props.alice]) - // await mintSigners20(hre.props.token1, token1Amount.mul(10000), [hre.props.alice]) - + const token0Amount = ethers.utils.parseUnits('1000', 18) + console.log('mint tokens', hre.props.alice.address) + // await mintSigners20(hre.props.token0, token0Amount.mul(10000), [hre.props.alice]) + await mintSigners20(hre.props.token1, token0Amount, [hre.props.alice]) + console.log('minted tokens') const liquidityAmount = '49802891105937278098768' // await getPrice(true) @@ -84,10 +84,11 @@ export class MintPosition { // ) // console.log('position snapshot', snapshot.feesOwed0.toString(), snapshot.feesOwed1.toString()) - const amountIn = parseUnits('20', 18) + const amountIn = parseUnits('100', 18) const signer = hre.props.alice // let approveTxn = await hre.props.token0.connect(signer).approve(hre.props.poolRouter.address, amountIn) - let approveTxn = await hre.props.token0.connect(signer).approve(hre.props.poolRouter.address, amountIn) + console.log('approve tokens') + let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, amountIn) await approveTxn.wait() // const aliceId = await validateMintRange({ // signer: hre.props.alice, @@ -113,11 +114,12 @@ export class MintPosition { const zeroForOne = true - const priceLimit = BigNumber.from('1553792102639747119534487568') + const priceLimit = BigNumber.from('4039859466863342510789667678182') + console.log('before swap') let txn = await hre.props.poolRouter .connect(signer) .multiSwapSplit( - ['0xa43ddbcc4b78512c316bd7091b4c60f06db0fe42'], + ['0x02225f6a3d83648d7906a23856331c819265394d'], [ { to: signer.address, diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index f2f373c3..6b93494c 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -30,10 +30,10 @@ export class InitialSetup { /// DEPLOY CONFIG private deployTokens = false - private deployContracts = true - private deployPools = true + private deployContracts = false + private deployPools = false private deployRouter = true - private deployStaker = true + private deployStaker = false constructor() { this.deployAssist = new DeployAssist() From e37a40bd21ae029f16c460e4e966e5b8099668da Mon Sep 17 00:00:00 2001 From: alphak3y <84204260+alphak3y@users.noreply.github.com> Date: Tue, 30 Jan 2024 08:49:32 -0700 Subject: [PATCH 39/39] Update README.md --- subgraph/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subgraph/README.md b/subgraph/README.md index 95e0ac2c..5fde2737 100644 --- a/subgraph/README.md +++ b/subgraph/README.md @@ -1 +1 @@ -# oceanbook-v1-subgraph +# Poolshark Limit Subgraph