@goat-sdk/plugin-plunderswap
Version:
<div align="center"> <a href="https://github.com/goat-sdk/goat">
2 lines (1 loc) • 18.1 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});var _chunkUKH3CKNYjs = require('./chunk-UKH3CKNY.js');var _core = require('@goat-sdk/core');var _walletzilliqa = require('@goat-sdk/wallet-zilliqa');var _viem = require('viem');var $=["event Approval(address indexed owner, address indexed spender, uint256 value)","event Transfer(address indexed from, address indexed to, uint256 value)","function allowance(address owner, address spender) view returns (uint256)","function approve(address spender, uint256 value) returns (bool)","function balanceOf(address account) view returns (uint256)","function decimals() view returns (uint8)","function name() view returns (string)","function symbol() view returns (string)","function totalSupply() view returns (uint256)","function transfer(address to, uint256 value) returns (bool)","function transferFrom(address from, address to, uint256 value) returns (bool)"];var j=["event PairCreated(address indexed token0, address indexed token1, address pair, uint256)","function INIT_CODE_PAIR_HASH() view returns (bytes32)","function allPairs(uint256) view returns (address pair)","function allPairsLength() view returns (uint256)","function createPair(address tokenA, address tokenB) returns (address pair)","function feeTo() view returns (address)","function feeToSetter() view returns (address)","function getPair(address tokenA, address tokenB) view returns (address pair)","function setFeeTo(address)","function setFeeToSetter(address)"];var Z=["event Approval(address indexed owner, address indexed spender, uint256 value)","event Burn(address indexed sender, uint256 amount0, uint256 amount1, address indexed to)","event Mint(address indexed sender, uint256 amount0, uint256 amount1)","event Swap(address indexed sender, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out, address indexed to)","event Sync(uint112 reserve0, uint112 reserve1)","event Transfer(address indexed from, address indexed to, uint256 value)","function DOMAIN_SEPARATOR() view returns (bytes32)","function MINIMUM_LIQUIDITY() pure returns (uint256)","function PERMIT_TYPEHASH() pure returns (bytes32)","function allowance(address owner, address spender) view returns (uint256)","function approve(address spender, uint256 value) returns (bool)","function balanceOf(address owner) view returns (uint256)","function burn(address to) returns (uint256 amount0, uint256 amount1)","function decimals() pure returns (uint8)","function factory() view returns (address)","function getReserves() view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast)","function initialize(address, address)","function kLast() view returns (uint256)","function mint(address to) returns (uint256 liquidity)","function name() pure returns (string)","function nonces(address owner) view returns (uint256)","function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)","function price0CumulativeLast() view returns (uint256)","function price1CumulativeLast() view returns (uint256)","function skim(address to)","function swap(uint256 amount0Out, uint256 amount1Out, address to, bytes data)","function symbol() pure returns (string)","function sync()","function token0() view returns (address)","function token1() view returns (address)","function totalSupply() view returns (uint256)","function transfer(address to, uint256 value) returns (bool)","function transferFrom(address from, address to, uint256 value) returns (bool)"];var R=["function WETH() pure returns (address)","function addLiquidity(address tokenA, address tokenB, uint256 amountADesired, uint256 amountBDesired, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline) returns (uint256 amountA, uint256 amountB, uint256 liquidity)","function addLiquidityETH(address token, uint256 amountTokenDesired, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline) payable returns (uint256 amountToken, uint256 amountETH, uint256 liquidity)","function factory() pure returns (address)","function getAmountIn(uint256 amountOut, uint256 reserveIn, uint256 reserveOut) pure returns (uint256 amountIn)","function getAmountOut(uint256 amountIn, uint256 reserveIn, uint256 reserveOut) pure returns (uint256 amountOut)","function getAmountsIn(uint256 amountOut, address[] path) view returns (uint256[] amounts)","function getAmountsOut(uint256 amountIn, address[] path) view returns (uint256[] amounts)","function quote(uint256 amountA, uint256 reserveA, uint256 reserveB) pure returns (uint256 amountB)","function removeLiquidity(address tokenA, address tokenB, uint256 liquidity, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline) returns (uint256 amountA, uint256 amountB)","function removeLiquidityETH(address token, uint256 liquidity, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline) returns (uint256 amountToken, uint256 amountETH)","function removeLiquidityETHWithPermit(address token, uint256 liquidity, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) returns (uint256 amountToken, uint256 amountETH)","function removeLiquidityWithPermit(address tokenA, address tokenB, uint256 liquidity, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) returns (uint256 amountA, uint256 amountB)","function swapETHForExactTokens(uint256 amountOut, address[] path, address to, uint256 deadline) payable returns (uint256[] amounts)","function swapExactETHForTokens(uint256 amountOutMin, address[] path, address to, uint256 deadline) payable returns (uint256[] amounts)","function swapExactTokensForETH(uint256 amountIn, uint256 amountOutMin, address[] path, address to, uint256 deadline) returns (uint256[] amounts)","function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] path, address to, uint256 deadline) returns (uint256[] amounts)","function swapTokensForExactETH(uint256 amountOut, uint256 amountInMax, address[] path, address to, uint256 deadline) returns (uint256[] amounts)","function swapTokensForExactTokens(uint256 amountOut, uint256 amountInMax, address[] path, address to, uint256 deadline) returns (uint256[] amounts)"];var D=["function name() view returns (string)","function approve(address guy, uint256 wad) returns (bool)","function totalSupply() view returns (uint256)","function transferFrom(address src, address dst, uint256 wad) returns (bool)","function withdraw(uint256 wad)","function decimals() view returns (uint8)","function balanceOf(address) view returns (uint256)","function symbol() view returns (string)","function transfer(address dst, uint256 wad) returns (bool)","function deposit() payable","function allowance(address, address) view returns (uint256)","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)"];function T(y,n,e,s){var o=arguments.length,t=o<3?n:s===null?s=Object.getOwnPropertyDescriptor(n,e):s,r;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")t=Reflect.decorate(y,n,e,s);else for(var i=y.length-1;i>=0;i--)(r=y[i])&&(t=(o<3?r(t):o>3?r(n,e,t):r(n,e))||t);return o>3&&t&&Object.defineProperty(n,e,t),t}_chunkUKH3CKNYjs.a.call(void 0, T,"_ts_decorate");function l(y,n){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(y,n)}_chunkUKH3CKNYjs.a.call(void 0, l,"_ts_metadata");var C="WZIL";function S(y){if(y===32769)return{factory:"0xf42d1058f233329185A36B04B7f96105afa1adD2",router:"0x33C6a20D2a605da9Fd1F506ddEd449355f0564fe"};if(y===33101)return{factory:"0xd0156eFCA4D847E4c4aD3F9ECa7FA697bb105cC0",router:"0x144e7AEee22F388350E9EAEFBb626A021fcd0250"};throw`unknown chain ${y}`}_chunkUKH3CKNYjs.a.call(void 0, S,"getContractAddresses");var W=class W{constructor(){_chunkUKH3CKNYjs.b.call(void 0, this,"erc20Abi",_viem.parseAbi.call(void 0, $));_chunkUKH3CKNYjs.b.call(void 0, this,"pairAbi",_viem.parseAbi.call(void 0, Z));_chunkUKH3CKNYjs.b.call(void 0, this,"factoryAbi",_viem.parseAbi.call(void 0, j));_chunkUKH3CKNYjs.b.call(void 0, this,"routerAbi",_viem.parseAbi.call(void 0, R));_chunkUKH3CKNYjs.b.call(void 0, this,"wZilAbi",_viem.parseAbi.call(void 0, D));_chunkUKH3CKNYjs.b.call(void 0, this,"tokensByChain",new Map)}ensureTokens(n){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){if(this.tokensByChain.has(n))return;let e;if(n===32769)e="https://plunderswap.github.io/token-lists/default-mainnet.json";else if(n===33101)e="https://plunderswap.com/lists/default-testnet.json";else throw`unknown chain ${n}`;let s=yield fetch(e);if(!s.ok)throw`failed to fetch tokens for chain ${n}: ${s.statusText}`;let t=(yield s.json()).tokens,r=new Map,i=new Map;for(let a of t)r.set(a.symbol,a),i.set(a.address.toLowerCase(),a);this.tokensByChain.set(n,{bySymbol:r,byAddress:i})})}getLiquidityPair(n,e,s){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){let o=_viem.getContract.call(void 0, {address:s,abi:this.pairAbi,client:n}),t=yield o.read.token0(),r=yield o.read.token1(),i=e==null?void 0:e.byAddress.get(t.toLowerCase()),a=e==null?void 0:e.byAddress.get(r.toLowerCase());if(!i||!a)return null;let c=yield o.read.getReserves(),u=c[0],m=c[1];return[u,m].includes(BigInt(0))?null:{tokens:[i,a],reserves:[u,m]}})}getLiquidityPairs(n,e,s){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){let o=_viem.getContract.call(void 0, {address:e.factory,abi:this.factoryAbi,client:n}),t=yield o.read.allPairsLength(),r=[],i=4;for(let a=0;a<t;a+=i){let c=[];for(let u=0;u<i&&a+u<t;u++)c.push(o.read.allPairs([a+u]).then(m=>this.getLiquidityPair(n,s,m)));r.push(...(yield Promise.all(c)).filter(u=>u!==null))}return r})}planSwap(n,e,s,o){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){if(e===o)throw"tokens must differ";let t=[],r=[],i=[];for(let f=0;f<n.length;f++){let d=n[f],h=d.tokens.includes(e),v=d.tokens.includes(o);h?v?e===d.tokens[0]?t.push({reserves:{from:d.reserves[0],to:d.reserves[1]}}):t.push({reserves:{from:d.reserves[1],to:d.reserves[0]}}):e===d.tokens[0]?r.push({reserves:{from:d.reserves[0],to:d.reserves[1]},tokenTo:d.tokens[1]}):r.push({reserves:{from:d.reserves[1],to:d.reserves[0]},tokenTo:d.tokens[0]}):v&&(o===d.tokens[1]?i.push({reserves:{from:d.reserves[0],to:d.reserves[1]},tokenFrom:d.tokens[0]}):i.push({reserves:{from:d.reserves[1],to:d.reserves[0]},tokenFrom:d.tokens[1]}))}let a=BigInt(0),c=[],u=BigInt(9965),m=BigInt(1e4);for(let f of t){if(f.reserves.from<s)continue;let d=u*s,h=d*f.reserves.to/(m*f.reserves.from+d);f.reserves.to<h||h>a&&(a=h,c=[e,o])}for(let f of r)for(let d of i)if(f.tokenTo===d.tokenFrom){if(f.reserves.from<s)continue;let h=u*s,v=h*f.reserves.to/(m*f.reserves.from+h);if(f.reserves.to<v||d.reserves.from<v)continue;let P=u*v,x=P*d.reserves.to/(m*d.reserves.from+P);if(d.reserves.to<x)continue;x>a&&(a=x,c=[e,f.tokenTo,o])}if(a===BigInt(0))throw"cannot make that swap";return{tokenPath:c,quote:a}})}ensureApproval(n,e,s,o){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){let t=n.getViemPublicClient(),r=n.getViemWalletClient(),i=_viem.getContract.call(void 0, {address:e,abi:this.erc20Abi,client:{public:t,wallet:r}}),[a]=yield r.getAddresses(),c=yield i.read.allowance([a,s]);if(o<=c)return null;let u=yield i.write.approve([s,o]);return yield t.waitForTransactionReceipt({hash:u}),u})}getTokens(n,e){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){let s=n.getViemPublicClient(),o=yield s.getChainId(),t=S(o);yield this.ensureTokens(o);let r=this.tokensByChain.get(o);if(!r)return{tokens:[]};let i=yield this.getLiquidityPairs(s,t,r),a=new Set;for(let c of i)for(let u of c.tokens)a.add(u.symbol);return{tokens:Array.from(a)}})}getBalance(n,e){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){let s=n.getViemPublicClient(),o=n.getViemWalletClient(),t=yield s.getChainId();yield this.ensureTokens(t);let r=this.tokensByChain.get(t),i=r==null?void 0:r.bySymbol.get(e.token);if(!i)throw`unknown token ${e.token} on chain ${t}`;let a=_viem.getContract.call(void 0, {address:i.address,abi:this.erc20Abi,client:s}),[c]=yield o.getAddresses(),u=yield a.read.balanceOf([c]);return{amount:_viem.formatUnits.call(void 0, u,i.decimals)}})}getQuote(n,e){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){if(e.fromToken===e.toToken)throw"tokens must differ";let s=n.getViemPublicClient(),o=yield s.getChainId(),t=S(o);yield this.ensureTokens(o);let r=this.tokensByChain.get(o);if(!r)throw`unknown chain ${o}`;let i=r.bySymbol.get(e.fromToken),a=r.bySymbol.get(e.toToken);if(!i)throw`unknown token ${e.fromToken} on chain ${o}`;if(!a)throw`unknown token ${e.toToken} on chain ${o}`;let c=_viem.parseUnits.call(void 0, e.fromAmount,i.decimals),u=yield this.getLiquidityPairs(s,t,r),m=yield this.planSwap(u,i,c,a);return{amount:_viem.formatUnits.call(void 0, m.quote,a.decimals),tokenPath:m.tokenPath.map(f=>f.symbol)}})}swap(n,e){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){if(e.fromToken===e.toToken)throw"tokens must differ";let s=n.getViemPublicClient(),o=n.getViemWalletClient(),t=yield s.getChainId(),r=S(t);yield this.ensureTokens(t);let i=this.tokensByChain.get(t);if(!i)throw`unknown chain ${t}`;let a=i.bySymbol.get(e.fromToken),c=i.bySymbol.get(e.toToken);if(!a)throw`unknown token ${e.fromToken} on chain ${t}`;if(!c)throw`unknown token ${e.toToken} on chain ${t}`;let u=_viem.parseUnits.call(void 0, e.fromAmount,a.decimals),m=_viem.parseUnits.call(void 0, e.toAmount,c.decimals),f=Math.round(e.deadline.getTime()/1e3),d=yield this.getLiquidityPairs(s,r,i),h=yield this.planSwap(d,a,u,c),v=[],P=yield this.ensureApproval(n,h.tokenPath[0].address,r.router,u);P&&v.push(P);let x=_viem.getContract.call(void 0, {address:r.router,abi:this.routerAbi,client:{public:s,wallet:o}}),z=h.tokenPath.map(G=>G.address),[U]=yield o.getAddresses(),_=yield x.write.swapExactTokensForTokens([u,m,z,U,f]);return v.push(_),yield s.waitForTransactionReceipt({hash:_}),{txHashes:v}})}wzil_deposit(n,e){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){let s=n.getViemPublicClient(),o=n.getViemWalletClient(),t=yield s.getChainId();yield this.ensureTokens(t);let r=this.tokensByChain.get(t);if(!r)throw`unknown chain ${t}`;let i=r.bySymbol.get(C);if(!i)throw`unknown token ${C} on chain ${t}`;let a=_viem.parseUnits.call(void 0, e.amount,i.decimals),u=yield _viem.getContract.call(void 0, {address:i.address,abi:this.wZilAbi,client:{public:s,wallet:o}}).write.deposit([],{value:a});return yield s.waitForTransactionReceipt({hash:u}),{txHash:u}})}wzil_withdraw(n,e){return _chunkUKH3CKNYjs.c.call(void 0, this,null,function*(){let s=n.getViemPublicClient(),o=n.getViemWalletClient(),t=yield s.getChainId();yield this.ensureTokens(t);let r=this.tokensByChain.get(t);if(!r)throw`unknown chain ${t}`;let i=r.bySymbol.get(C);if(!i)throw`unknown token ${C} on chain ${t}`;let a=_viem.parseUnits.call(void 0, e.amount,i.decimals),u=yield _viem.getContract.call(void 0, {address:i.address,abi:this.wZilAbi,client:{public:s,wallet:o}}).write.withdraw([a]);return yield s.waitForTransactionReceipt({hash:u}),{txHash:u}})}};_chunkUKH3CKNYjs.a.call(void 0, W,"PlunderSwapService");var k=W;T([_core.Tool.call(void 0, {name:"plunderswap_tokens",description:"Get the symbols for the tokens on the current blockchain that can be exchaged using PlunderSwap."}),l("design:type",Function),l("design:paramtypes",[typeof _walletzilliqa.ZilliqaWalletClientViemOnly=="undefined"?Object:_walletzilliqa.ZilliqaWalletClientViemOnly,typeof _chunkUKH3CKNYjs.d=="undefined"?Object:_chunkUKH3CKNYjs.d]),l("design:returntype",Promise)],k.prototype,"getTokens",null);T([_core.Tool.call(void 0, {name:"plunderswap_balance",description:"Get the user's balance for a token that is registered with PlunderSwap."}),l("design:type",Function),l("design:paramtypes",[typeof _walletzilliqa.ZilliqaWalletClientViemOnly=="undefined"?Object:_walletzilliqa.ZilliqaWalletClientViemOnly,typeof _chunkUKH3CKNYjs.f=="undefined"?Object:_chunkUKH3CKNYjs.f]),l("design:returntype",Promise)],k.prototype,"getBalance",null);T([_core.Tool.call(void 0, {name:"plunderswap_quote",description:"Get a quote for how many tokens would be received if the given tokens were swapped for another token."}),l("design:type",Function),l("design:paramtypes",[typeof _walletzilliqa.ZilliqaWalletClientViemOnly=="undefined"?Object:_walletzilliqa.ZilliqaWalletClientViemOnly,typeof _chunkUKH3CKNYjs.h=="undefined"?Object:_chunkUKH3CKNYjs.h]),l("design:returntype",Promise)],k.prototype,"getQuote",null);T([_core.Tool.call(void 0, {name:"plunderswap_swap",description:"Exchange the given tokens for another token."}),l("design:type",Function),l("design:paramtypes",[typeof _walletzilliqa.ZilliqaWalletClientViemOnly=="undefined"?Object:_walletzilliqa.ZilliqaWalletClientViemOnly,typeof _chunkUKH3CKNYjs.j=="undefined"?Object:_chunkUKH3CKNYjs.j]),l("design:returntype",Promise)],k.prototype,"swap",null);T([_core.Tool.call(void 0, {name:"plunderswap_zil_wrap",description:"Wrap native ZIL in an ERC-20 wrapper: change ZIL for WZIL."}),l("design:type",Function),l("design:paramtypes",[typeof _walletzilliqa.ZilliqaWalletClientViemOnly=="undefined"?Object:_walletzilliqa.ZilliqaWalletClientViemOnly,typeof _chunkUKH3CKNYjs.l=="undefined"?Object:_chunkUKH3CKNYjs.l]),l("design:returntype",Promise)],k.prototype,"wzil_deposit",null);T([_core.Tool.call(void 0, {name:"plunderswap_zil_unwrap",description:"Unwrap native ZIL from an ERC-20 wrapper: change WZIL for ZIL."}),l("design:type",Function),l("design:paramtypes",[typeof _walletzilliqa.ZilliqaWalletClientViemOnly=="undefined"?Object:_walletzilliqa.ZilliqaWalletClientViemOnly,typeof _chunkUKH3CKNYjs.n=="undefined"?Object:_chunkUKH3CKNYjs.n]),l("design:returntype",Promise)],k.prototype,"wzil_withdraw",null);exports.a = k;