{"_id":"@astonic-io/astonic-router-ts","name":"@astonic-io/astonic-router-ts","dist-tags":{"latest":"0.2.0"},"versions":{"0.2.0":{"name":"@astonic-io/astonic-router-ts","description":"Astonic Router","license":"GPL-3.0-or-later","version":"0.2.0","main":"dist/index.js","types":"dist/index.d.ts","author":{"name":"astonic-io"},"repository":{"type":"git","url":"git+https://github.com/astonic-io/astonic-router-ts.git"},"devDependencies":{"@commitlint/cli":"^17.0.3","@commitlint/config-conventional":"^17.0.3","@ethersproject/abi":"^5.0.0","@ethersproject/bytes":"^5.0.0","@ethersproject/providers":"^5.0.0","@typechain/ethers-v5":"^10.1.1","@types/node":"18.11.9","commitizen":"^4.2.4","cz-conventional-changelog":"^3.3.0","ethers":"^5.1.3","husky":"^8.0.0","lint-staged":"^13.0.3","pinst":"^3.0.0","prettier":"^2.7.1","typechain":"^8.1.1","typescript":"^4.9.3"},"keywords":["astonic","planq","solidity"],"scripts":{"lint":"yarn prettier","lint:check":"yarn prettier:check","prepare":"husky install","prettier":"prettier --config \"./.prettierrc.yml\" --write \"**/*.{json,md,js,yml}\"","prettier:check":"prettier --config \"./.prettierrc.yml\" --check \"**/*.{json,md,js,yml}\"","generatetypes":"node generateTypes.js","build":"tsc"},"_id":"@astonic-io/astonic-router-ts@0.2.0","gitHead":"5b35ed67d2469632cf3d43a36d0c300d0edd0c01","bugs":{"url":"https://github.com/astonic-io/astonic-router-ts/issues"},"homepage":"https://github.com/astonic-io/astonic-router-ts#readme","_nodeVersion":"20.11.0","_npmVersion":"10.2.4","dist":{"integrity":"sha512-hdChdcf4AATqJvYBYZa+k9bkmNKvk+XJzAzHzfttuAcgf3tAUPpNSUYPZwpij3nlp8QeC0sjzrgwwN1c/K8h4Q==","shasum":"0856a3c6039c00f7138c1b202649b7dd64c219e3","tarball":"https://registry.npmjs.org/@astonic-io/astonic-router-ts/-/astonic-router-ts-0.2.0.tgz","fileCount":20,"unpackedSize":96405,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCnLmXQ+WJhH488P3a/nfj0uZ8j/eMHuzzQv0Qzq4mloAIgfwnQlpX9yD3VEHTyjtOzaC/NlTawt3xNdRIXoYNowAw="}]},"_npmUser":{"name":"astonic-dev","email":"info@astonic.io"},"directories":{},"maintainers":[{"name":"astonic-dev","email":"info@astonic.io"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/astonic-router-ts_0.2.0_1740752933105_0.20140461945611543"},"_hasShrinkwrap":false}},"time":{"created":"2025-02-28T14:28:53.023Z","0.2.0":"2025-02-28T14:28:53.329Z","modified":"2025-02-28T14:28:53.621Z"},"maintainers":[{"name":"astonic-dev","email":"info@astonic.io"}],"description":"Astonic Router","homepage":"https://github.com/astonic-io/astonic-router-ts#readme","keywords":["astonic","planq","solidity"],"repository":{"type":"git","url":"git+https://github.com/astonic-io/astonic-router-ts.git"},"author":{"name":"astonic-io"},"bugs":{"url":"https://github.com/astonic-io/astonic-router-ts/issues"},"license":"GPL-3.0-or-later","readme":"## AstonicRouter\r\n\r\nA UniswapRouter-style Router for the Astonic AMM.\r\n\r\n### How does it work?\r\n\r\nThe router's aim is to execute a series of chained swaps on pairs in the Astonic AMM.\r\nAs an example we can look at the path USDC->aUSD->aKES. But this can be anything as long as two adjecent assets represent a tradable pair in the Broker.\r\n\r\nThis path has two swaps or steps: USDC->aUSD and aUSD->aKES.\r\nIn order to specificy a path the AstonicRouter defines the following struct (as seen in [IAstonicRouter.sol](./src/IAstonicRouter.sol)):\r\n\r\n```solidity\r\nstruct Step {\r\n    address exchangeProvider;\r\n    bytes32 exchangeId;\r\n    address assetIn;\r\n    address assetOut;\r\n}\r\n```\r\n\r\nSo for USDC->aUSD, the step is:\r\n\r\n```solidity\r\nIAstonicRouter.Step memory USDC_to_aUSD = IAstonicRouter.Step({\r\n    exchangeProvider: 0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901,\r\n    exchangeId: 0xacc988382b66ee5456086643dcfd9a5ca43dd8f428f6ef22503d8b8013bcffd7,\r\n    assetIn: 0xcebA9300f2b948710d2653dD7B07f33A8B32118C,\r\n    assetOut: 0x765DE816845861e75A25fCA122bb6898B8B1282a\r\n});\r\n```\r\n\r\nAnd for aUSD->aKES, the step is:\r\n\r\n```solidity\r\nIAstonicRouter.Step memory aUSD_to_aKES = IAstonicRouter.Step({\r\n    exchangeProvider: 0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901,\r\n    exchangeId: 0x89de88b8eb790de26f4649f543cb6893d93635c728ac857f0926e842fb0d298b,\r\n    assetIn: 0x765DE816845861e75A25fCA122bb6898B8B1282a,\r\n    assetOut: 0x456a3D042C0DbD3db53D5489e98dFb038553B0d0\r\n});\r\n```\r\n\r\nThe parameters to a step are consistent to the parameters needed to call the Astonic Broker.\r\n\r\nThis lets us build a path:\r\n\r\n```solidity\r\nIAstonicRouter.Step[] memory path = new IAstonicRouter.Step[](2);\r\npath[0] = USDC_to_aUSD;\r\npath[1] = aUSD_to_aKES;\r\n```\r\n\r\nNow that we have a path thare two ways we can execute it:\r\n\r\n- `swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, Step[] path)` - which fixes the `amountIn` of input token, in our example USDC, and ensures at least `amountOutMin` of the output token, in our case aKES, is returned.\r\n- `swapTokensForExactTokens(uint256 amountOut, uint256 amountInMax, Step[] path)` - which fixes the `amountOut` of output token, in our example aKES, and ensures at most `amountInMax` of the input token, in our case USDC, is spent.\r\n\r\nIn order to execute a swap you also need to give approval to the Router for the asset at the begining of the chain and the amount of either `amountIn` for (1) or `amountInMax` for (2).\r\nFull Example:\r\n\r\n```solidity\r\nIAstonicRouter.Step[] memory path = new IAstonicRouter.Step[](2);\r\npath[0] = IAstonicRouter.Step({\r\n    exchangeProvider: biPoolManager,\r\n    exchangeId: aUSD_USDC_exchangeID,\r\n    assetIn: USDC,\r\n    assetOut: aUSD\r\n});\r\npath[1] = IAstonicRouter.Step({\r\n    exchangeProvider: bpm,\r\n    exchangeId: aUSD_aKES_exchangeID,\r\n    assetIn: aUSD,\r\n    assetOut: aKES\r\n});\r\n\r\nIERC20(USDC).approve(address(astonicRouter), 1e3);\r\nastonicRouter.swapExactTokensForTokens(1e3, 0, path);\r\n```\r\n\r\nYou can see more examples in the [Swap Script](./script/Swap.s.sol) or the [Test](./test/AstonicRouter.t.sol);\r\n\r\nYou can also estimate a path by calling one of the two functions:\r\n\r\n- `getAmountOut(uint256 amountIn, Step[] path)` - which returns how much of the last asset in the chain you will get for a given amount of the first.\r\n- `getAmountIn(uint256 amuntOut, Step[], path)` - which returns how much of the first asset in the chain is required to get a certain amount of the last asset in the chain.\r\n\r\nThese functions should be used in conjuction with the `amountInMax` and `amountOutMin` variables of the swap functions in order to control slippage.\r\n","readmeFilename":"README.md"}