danogo-clmm 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of danogo-clmm might be problematic. Click here for more details.

package/README.md CHANGED
@@ -16,7 +16,8 @@ This SDK relies on `@lucid-evolution/lucid` for wallet management and transactio
16
16
 
17
17
  ### Initialization
18
18
 
19
- Initialize the SDK.
19
+ Initialize the SDK with the Danogo API URL. Optionally, you can provide a custom pool script hash.
20
+
20
21
  ```typescript
21
22
  import DanogoSwap from "danogo-clmm";
22
23
 
package/dist/sdk.js CHANGED
@@ -1 +1 @@
1
- "use strict";var E=Object.defineProperty;var J=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var Z=Object.prototype.hasOwnProperty;var tt=(n,e)=>{for(var t in e)E(n,t,{get:e[t],enumerable:!0})},et=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of K(e))!Z.call(n,r)&&r!==t&&E(n,r,{get:()=>e[r],enumerable:!(o=J(e,r))||o.enumerable});return n};var nt=n=>et(E({},"__esModule",{value:!0}),n);var st={};tt(st,{default:()=>it});module.exports=nt(st);var v=require("@lucid-evolution/lucid");function D(n,e){let o=(n>=0n?n:n+(1n<<256n)).toString(16);o.length%2&&(o="0"+o);let r=o.length/2;if(r>e)throw new Error(`Number ${n} requires ${r} bytes, but target length is ${e}.`);let i=new Uint8Array(e),s=e-r;for(let g=0;g<r;g++)i[g+s]=parseInt(o.slice(g*2,g*2+2),16);return i}var T=(n,e,t,o)=>{try{return{kind:"selected",inputs:n,makeRedeemer:i=>{let s=3n,g=D(o?t:i[0],1),c=D(i[0],1),p=D(s,1),f=D(0n,1),w=D(e[0],32),d=g.length+p.length+c.length+f.length+w.length,l=new Uint8Array(d),a=0;return l.set(g,a),a+=g.length,l.set(p,a),a+=p.length,l.set(c,a),a+=c.length,l.set(f,a),a+=f.length,l.set(w,a),"5824"+Buffer.from(l).toString("hex")}}}catch(r){throw console.error("Error creating pool redeemer:",r),r}};var m=require("@lucid-evolution/lucid");var X=require("@lucid-evolution/lucid");function Y(n,e,t){return X.Data.to(n,e,t)}var x=require("cbor"),$=n=>{let e=m.Data.Tuple([m.Data.Bytes(),m.Data.Bytes()]),t=m.Data.Tuple([m.Data.Integer(),m.Data.Integer()],{hasConstr:!0}),o=m.Data.Tuple([e,e,m.Data.Integer(),m.Data.Integer(),m.Data.Integer(),m.Data.Integer(),t,t,m.Data.Integer(),m.Data.Integer(),m.Data.Integer(),m.Data.Integer()],{hasConstr:!0}),r=M(n.tokenX),i=M(n.tokenY),s=[r,i,BigInt(n.lpFeeRate),BigInt(n.platformFeeX),BigInt(n.platformFeeY),BigInt(n.totalSwapFee),[BigInt(n.sqrtLowerPriceNum),BigInt(n.sqrtLowerPriceDen)],[BigInt(n.sqrtUpperPriceNum),BigInt(n.sqrtUpperPriceDen)],BigInt(n.minXChange),BigInt(n.minYChange),BigInt(n.circulatingLPToken),BigInt(n.lastWithdrawEpoch)];return Y(s,o)};var M=n=>{if(!n)return["",""];try{if(n.includes(".")){let o=n.split(".");return o.length===2?[o[0],o[1]]:[n,""]}let e=n.slice(0,56),t=n.slice(56);return[e,t]}catch(e){throw console.error(`Error parsing token ID "${n}":`,e),new Error(`Failed to parse token ID: ${n}`)}},S=n=>{let e=(0,x.decodeFirstSync)(Buffer.from(n,"hex")),t=e instanceof x.Tagged?e.value:e;if(!Array.isArray(t))throw new Error("Invalid datum structure: expected array of fields");let o=i=>{let s=i instanceof x.Tagged?i.value:i;if(Array.isArray(s)&&s.length===2){let g=s[0].toString("hex"),c=s[1].toString("hex");return g+c}throw new Error("Invalid AssetClass structure")},r=i=>{let s=i instanceof x.Tagged?i.value:i;if(Array.isArray(s)&&s.length===2)return{num:s[0],den:s[1]};throw new Error("Invalid Ratio structure")};return{tokenX:o(t[0]),tokenY:o(t[1]),lpFeeRate:Number(t[2]),platformFeeX:t[3].toString(),platformFeeY:t[4].toString(),totalSwapFee:t[5].toString(),sqrtLowerPriceNum:r(t[6]).num,sqrtLowerPriceDen:r(t[6]).den,sqrtUpperPriceNum:r(t[7]).num,sqrtUpperPriceDen:r(t[7]).den,minXChange:t[8].toString(),minYChange:t[9].toString(),circulatingLPToken:t[10].toString(),lastWithdrawEpoch:Number(t[11])}},U=n=>{let e=(0,x.decodeFirstSync)(Buffer.from(n,"hex")),t=e instanceof x.Tagged?e.value:e;if(!Array.isArray(t))throw new Error("Invalid datum structure: expected array of fields");return{platformFeeRate:t[0].toString(),swapFee:t[1].toString()}};var W=n=>{if(!n||Object.keys(n).length===0)return[];let e=[];for(let[t,o]of Object.entries(n)){if(t==="ada")continue;let r=[];for(let[i,s]of Object.entries(o))r.push({name:i,value:s});e.push({policyId:t,assets:r})}return e};var G=(n,e)=>{let t=432e6,o=1647899091e3,r=328;return e!=="Mainnet"&&(t=18e5),Math.floor((n-o)/t)+r};function N(n,e,t,o,r=0n,i){let s=o<0n?-o:o,g=t.tokenX===""?3000000n+BigInt(t.totalSwapFee):0n,c=BigInt(n)-BigInt(t.platformFeeX)+r-g,p=BigInt(e)-BigInt(t.platformFeeY),f=ot(c,p,[BigInt(t.sqrtLowerPriceNum),BigInt(t.sqrtLowerPriceDen)],[BigInt(t.sqrtUpperPriceNum),BigInt(t.sqrtUpperPriceDen)]),w=_(f[0]*BigInt(t.sqrtUpperPriceDen),f[1]*BigInt(t.sqrtUpperPriceNum))+c,d=_(f[0]*BigInt(t.sqrtLowerPriceNum),f[1]*BigInt(t.sqrtLowerPriceDen))+p;return o>0n?j(s,w,d,p,BigInt(t.lpFeeRate),i):j(s,d,w,c,BigInt(t.lpFeeRate),i)}var j=(n,e,t,o,r,i)=>{let s=10000n,c=n*r/s*BigInt(i)/10000n,p=s-r,f=e*s+n*p,w=e*t,l=(t*f-w*s)/f;if(l>o)throw new Error("pool out exceeded");return[l,c]},ot=(n,e,t,o)=>{let r=t[1]*o[1],i=t[0]*o[0],s=(e*r-n*i)*(e*r-n*i),g=4n*n*e*t[1]*t[1]*o[0]*o[0],c=rt(s+g),p=e*r+n*i+c,f=2n*(o[0]*t[1]-o[1]*t[0]);return[p,f]};function rt(n){if(n<0n)throw new Error("Square root of negative number");if(n<2n)return n;let e=n,t=e+n/e>>1n;for(;t<e;)e=t,t=e+n/e>>1n;return e}function _(n,e){if(e===0n)throw new Error("Division by zero");let t=n/e;return n%e===0n||n<0n!=e<0n?t:t+1n}function Q(n,e){let o=[...e].sort((r,i)=>r.txHash===i.txHash?r.outputIndex-i.outputIndex:r.txHash<i.txHash?-1:1).findIndex(r=>r.txHash===n.txHash&&r.outputIndex===n.outputIndex);if(o===-1)throw new Error("Protocol config out ref not found in reference inputs");return BigInt(o)}var L=class{constructor(){}async calculateSwapOut(e,t){if(!e||!e.wallet())throw new Error("Please connect a wallet first.");let o=(await e.utxosByOutRef([{txHash:t.poolOutRef.txHash,outputIndex:t.poolOutRef.outputIndex}]))[0],r=null;if(t.stakingOutRef&&(r=(await e.utxosByOutRef([{txHash:t.stakingOutRef.txHash,outputIndex:t.stakingOutRef.outputIndex}]))[0]),!o.datum)throw new Error("Pool input UTxO does not contain a datum.");let i=(await e.utxosByOutRef([{txHash:t.protocolConfigOutRef.txHash,outputIndex:t.protocolConfigOutRef.outputIndex}]))[0];if(!i.datum)throw new Error("Protocol config UTxO does not contain a datum.");let s=U(i.datum),g=S(o.datum),c=g.tokenX||"lovelace",p=g.tokenY,f=o.assets.lovelace,w=u=>{if(u==="lovelace")return f;for(let[A,h]of Object.entries(o.assets))if(A===u)return h;return 0n},d=0n,l="";if(c=="lovelace"){l=(0,v.validatorToRewardAddress)(e.config().network,r.scriptRef);try{d=(await e.delegationAt(l)).rewards}catch{d=0n}}let[a,b]=N(w(c),w(p),g,t.deltaAmount,d,s.platformFeeRate);return a}async submitSwap(e,t){if(!e||!e.wallet())throw new Error("Please connect a wallet first.");let o=(await e.utxosByOutRef([{txHash:t.poolOutRef.txHash,outputIndex:t.poolOutRef.outputIndex}]))[0],r=(await e.utxosByOutRef([{txHash:t.poolScriptOutRef.txHash,outputIndex:t.poolScriptOutRef.outputIndex}]))[0],i=null;if(t.stakingOutRef&&(i=(await e.utxosByOutRef([{txHash:t.stakingOutRef.txHash,outputIndex:t.stakingOutRef.outputIndex}]))[0]),!o.datum)throw new Error("Pool input UTxO does not contain a datum.");let s=(await e.utxosByOutRef([{txHash:t.protocolConfigOutRef.txHash,outputIndex:t.protocolConfigOutRef.outputIndex}]))[0];if(!s.datum)throw new Error("Protocol config UTxO does not contain a datum.");let g=U(s.datum),c=S(o.datum),p=c.tokenX||"lovelace",f=c.tokenY,w=o.assets.lovelace,d=R=>{if(R==="lovelace")return w;for(let[C,z]of Object.entries(o.assets))if(C===R)return z;return 0n},l=t.deltaAmount,a=l>0?p:f,b=l>0?f:p,u=e.newTx(),h=(await e.wallet().getUtxos()).reduce((R,C)=>R+(C.assets[a]||0n),0n),F=l<0n?-l:l;if(h<F)throw new Error(`Insufficient ${a} balance. Required: ${F}, Available: ${h}`);u=u.readFrom([r,s]),i&&(u=u.readFrom([i]));let B=0n,k="";if(p=="lovelace"){k=(0,v.validatorToRewardAddress)(e.config().network,i.scriptRef);try{B=(await e.delegationAt(k)).rewards}catch{B=0n}}let[P,y]=N(d(p),d(f),c,l,B,g.platformFeeRate);if(P<t.minOutChangeAmount)throw new Error(`Slippage too high. Expected at least ${t.minOutChangeAmount} but got ${P}`);let q=G(Date.now(),e.config().network),V=$({...c,platformFeeX:BigInt(c.platformFeeX)+(a===(c.tokenX||"lovelace")?y:0n),platformFeeY:BigInt(c.platformFeeY)+(a===c.tokenY?y:0n),lastWithdrawEpoch:q,totalSwapFee:BigInt(c.totalSwapFee)+BigInt(g.swapFee)}),I={...o.assets};I[a]=I[a]+(l>0n?l:-l),I[b]=BigInt(I[b])-BigInt(P),I.lovelace=BigInt(I.lovelace)+BigInt(g.swapFee),u=u.pay.ToAddressWithData(o.address,{kind:"inline",value:V},I),u=u.attachMetadata(674,{msg:["Danogo Liquidity Pair: Swap"]});let H=[r,s];i&&H.push(i);let O=Q(s,H);return u=u.collectFrom([o],T([o],[l],O,!1)),u=u.withdraw((0,v.validatorToRewardAddress)(e.config().network,r.scriptRef),0n,T([o],[l],O,!0)),p==="lovelace"&&q>c.lastWithdrawEpoch&&(u=u.withdraw(k,B,T([o],[l],O,!1))),u=u.validFrom(Date.now()-12e4).validTo(Date.now()+24e4).setMinFee(17000n).addSigner(await e.wallet().address()),await(await(await u.complete({localUPLCEval:!1})).sign.withWallet().complete()).submit()}getPoolsFromOgmiosTx(e,t){let o=[];return e.outputs.forEach((r,i)=>{let s=r.value,g=s[t];if(g&&r.datum){for(let[c,p]of Object.entries(g))if(p===1n){let f=t+c,w=`${e.id}#${i}`,d=s.ada.lovelace,l=W(s),a=S(r.datum),b=a.tokenX,u=a.tokenY,A=h=>{if(h==="lovelace"||h==="")return d;let F=h.slice(0,56),B=h.slice(56),P=l.find(y=>y.policyId===F)?.assets.find(y=>y.name===B);return P?P.value:0n};o.push({outRef:w,address:r.address,coin:d,multiAssets:l,validityNft:f,tokenA:b,tokenAReserve:A(b),tokenB:u,tokenBReserve:A(u),lpFeeRate:a.lpFeeRate,priceLowerNum:a.sqrtLowerPriceNum,priceLowerDen:a.sqrtLowerPriceDen,priceUpperNum:a.sqrtUpperPriceNum,priceUpperDen:a.sqrtUpperPriceDen,platformFeeA:a.platformFeeX,platformFeeB:a.platformFeeY,minAChange:a.minXChange,minBChange:a.minYChange,lpTokenTotalSupply:a.circulatingLPToken,lastWithdrawEpoch:a.lastWithdrawEpoch,totalSwapFee:a.totalSwapFee})}}}),o}},it=L;
1
+ "use strict";var K=Object.create;var k=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var tt=Object.getOwnPropertyNames;var et=Object.getPrototypeOf,nt=Object.prototype.hasOwnProperty;var ot=(n,e)=>{for(var t in e)k(n,t,{get:e[t],enumerable:!0})},X=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of tt(e))!nt.call(n,r)&&r!==t&&k(n,r,{get:()=>e[r],enumerable:!(o=Z(e,r))||o.enumerable});return n};var rt=(n,e,t)=>(t=n!=null?K(et(n)):{},X(e||!n||!n.__esModule?k(t,"default",{value:n,enumerable:!0}):t,n)),it=n=>X(k({},"__esModule",{value:!0}),n);var lt={};ot(lt,{default:()=>ct});module.exports=it(lt);var O=require("@lucid-evolution/lucid");function D(n,e){let o=(n>=0n?n:n+(1n<<256n)).toString(16);o.length%2&&(o="0"+o);let r=o.length/2;if(r>e)throw new Error(`Number ${n} requires ${r} bytes, but target length is ${e}.`);let i=new Uint8Array(e),s=e-r;for(let g=0;g<r;g++)i[g+s]=parseInt(o.slice(g*2,g*2+2),16);return i}var S=(n,e,t,o)=>{try{return{kind:"selected",inputs:n,makeRedeemer:i=>{let s=3n,g=D(o?t:i[0],1),c=D(i[0],1),p=D(s,1),f=D(0n,1),w=D(e[0],32),d=g.length+p.length+c.length+f.length+w.length,l=new Uint8Array(d),a=0;return l.set(g,a),a+=g.length,l.set(p,a),a+=p.length,l.set(c,a),a+=c.length,l.set(f,a),a+=f.length,l.set(w,a),"5824"+Buffer.from(l).toString("hex")}}}catch(r){throw console.error("Error creating pool redeemer:",r),r}};var m=require("@lucid-evolution/lucid");var Y=require("@lucid-evolution/lucid");function M(n,e,t){return Y.Data.to(n,e,t)}var x=rt(require("cbor")),W=n=>{let e=m.Data.Tuple([m.Data.Bytes(),m.Data.Bytes()]),t=m.Data.Tuple([m.Data.Integer(),m.Data.Integer()],{hasConstr:!0}),o=m.Data.Tuple([e,e,m.Data.Integer(),m.Data.Integer(),m.Data.Integer(),m.Data.Integer(),t,t,m.Data.Integer(),m.Data.Integer(),m.Data.Integer(),m.Data.Integer()],{hasConstr:!0}),r=$(n.tokenX),i=$(n.tokenY),s=[r,i,BigInt(n.lpFeeRate),BigInt(n.platformFeeX),BigInt(n.platformFeeY),BigInt(n.totalSwapFee),[BigInt(n.sqrtLowerPriceNum),BigInt(n.sqrtLowerPriceDen)],[BigInt(n.sqrtUpperPriceNum),BigInt(n.sqrtUpperPriceDen)],BigInt(n.minXChange),BigInt(n.minYChange),BigInt(n.circulatingLPToken),BigInt(n.lastWithdrawEpoch)];return M(s,o)};var $=n=>{if(!n)return["",""];try{if(n.includes(".")){let o=n.split(".");return o.length===2?[o[0],o[1]]:[n,""]}let e=n.slice(0,56),t=n.slice(56);return[e,t]}catch(e){throw console.error(`Error parsing token ID "${n}":`,e),new Error(`Failed to parse token ID: ${n}`)}},v=n=>{let e=x.decodeFirstSync(Buffer.from(n,"hex")),t=e instanceof x.Tagged?e.value:e;if(!Array.isArray(t))throw new Error("Invalid datum structure: expected array of fields");let o=i=>{let s=i instanceof x.Tagged?i.value:i;if(Array.isArray(s)&&s.length===2){let g=s[0].toString("hex"),c=s[1].toString("hex");return g+c}throw new Error("Invalid AssetClass structure")},r=i=>{let s=i instanceof x.Tagged?i.value:i;if(Array.isArray(s)&&s.length===2)return{num:s[0],den:s[1]};throw new Error("Invalid Ratio structure")};return{tokenX:o(t[0]),tokenY:o(t[1]),lpFeeRate:Number(t[2]),platformFeeX:t[3].toString(),platformFeeY:t[4].toString(),totalSwapFee:t[5].toString(),sqrtLowerPriceNum:r(t[6]).num,sqrtLowerPriceDen:r(t[6]).den,sqrtUpperPriceNum:r(t[7]).num,sqrtUpperPriceDen:r(t[7]).den,minXChange:t[8].toString(),minYChange:t[9].toString(),circulatingLPToken:t[10].toString(),lastWithdrawEpoch:Number(t[11])}},U=n=>{let e=x.decodeFirstSync(Buffer.from(n,"hex")),t=e instanceof x.Tagged?e.value:e;if(!Array.isArray(t))throw new Error("Invalid datum structure: expected array of fields");return{platformFeeRate:t[0].toString(),swapFee:t[1].toString()}};var j=n=>{if(!n||Object.keys(n).length===0)return[];let e=[];for(let[t,o]of Object.entries(n)){if(t==="ada")continue;let r=[];for(let[i,s]of Object.entries(o))r.push({name:i,value:s});e.push({policyId:t,assets:r})}return e};var Q=(n,e)=>{let t=432e6,o=1647899091e3,r=328;return e!=="Mainnet"&&(t=18e5),Math.floor((n-o)/t)+r};function N(n,e,t,o,r=0n,i){let s=o<0n?-o:o,g=t.tokenX===""?3000000n+BigInt(t.totalSwapFee):0n,c=BigInt(n)-BigInt(t.platformFeeX)+r-g,p=BigInt(e)-BigInt(t.platformFeeY),f=st(c,p,[BigInt(t.sqrtLowerPriceNum),BigInt(t.sqrtLowerPriceDen)],[BigInt(t.sqrtUpperPriceNum),BigInt(t.sqrtUpperPriceDen)]),w=G(f[0]*BigInt(t.sqrtUpperPriceDen),f[1]*BigInt(t.sqrtUpperPriceNum))+c,d=G(f[0]*BigInt(t.sqrtLowerPriceNum),f[1]*BigInt(t.sqrtLowerPriceDen))+p;return o>0n?_(s,w,d,p,BigInt(t.lpFeeRate),i):_(s,d,w,c,BigInt(t.lpFeeRate),i)}var _=(n,e,t,o,r,i)=>{let s=10000n,c=n*r/s*BigInt(i)/10000n,p=s-r,f=e*s+n*p,w=e*t,l=(t*f-w*s)/f;if(l>o)throw new Error("pool out exceeded");return[l,c]},st=(n,e,t,o)=>{let r=t[1]*o[1],i=t[0]*o[0],s=(e*r-n*i)*(e*r-n*i),g=4n*n*e*t[1]*t[1]*o[0]*o[0],c=at(s+g),p=e*r+n*i+c,f=2n*(o[0]*t[1]-o[1]*t[0]);return[p,f]};function at(n){if(n<0n)throw new Error("Square root of negative number");if(n<2n)return n;let e=n,t=e+n/e>>1n;for(;t<e;)e=t,t=e+n/e>>1n;return e}function G(n,e){if(e===0n)throw new Error("Division by zero");let t=n/e;return n%e===0n||n<0n!=e<0n?t:t+1n}function V(n,e){let o=[...e].sort((r,i)=>r.txHash===i.txHash?r.outputIndex-i.outputIndex:r.txHash<i.txHash?-1:1).findIndex(r=>r.txHash===n.txHash&&r.outputIndex===n.outputIndex);if(o===-1)throw new Error("Protocol config out ref not found in reference inputs");return BigInt(o)}var L=class{constructor(){}async calculateSwapOut(e,t){if(!e||!e.wallet())throw new Error("Please connect a wallet first.");let o=(await e.utxosByOutRef([{txHash:t.poolOutRef.txHash,outputIndex:t.poolOutRef.outputIndex}]))[0],r=null;if(t.stakingOutRef&&(r=(await e.utxosByOutRef([{txHash:t.stakingOutRef.txHash,outputIndex:t.stakingOutRef.outputIndex}]))[0]),!o.datum)throw new Error("Pool input UTxO does not contain a datum.");let i=(await e.utxosByOutRef([{txHash:t.protocolConfigOutRef.txHash,outputIndex:t.protocolConfigOutRef.outputIndex}]))[0];if(!i.datum)throw new Error("Protocol config UTxO does not contain a datum.");let s=U(i.datum),g=v(o.datum),c=g.tokenX||"lovelace",p=g.tokenY,f=o.assets.lovelace,w=u=>{if(u==="lovelace")return f;for(let[A,h]of Object.entries(o.assets))if(A===u)return h;return 0n},d=0n,l="";if(c=="lovelace"){l=(0,O.validatorToRewardAddress)(e.config().network,r.scriptRef);try{d=(await e.delegationAt(l)).rewards}catch{d=0n}}let[a,b]=N(w(c),w(p),g,t.deltaAmount,d,s.platformFeeRate);return a}async submitSwap(e,t){if(!e||!e.wallet())throw new Error("Please connect a wallet first.");let o=(await e.utxosByOutRef([{txHash:t.poolOutRef.txHash,outputIndex:t.poolOutRef.outputIndex}]))[0],r=(await e.utxosByOutRef([{txHash:t.poolScriptOutRef.txHash,outputIndex:t.poolScriptOutRef.outputIndex}]))[0],i=null;if(t.stakingOutRef&&(i=(await e.utxosByOutRef([{txHash:t.stakingOutRef.txHash,outputIndex:t.stakingOutRef.outputIndex}]))[0]),!o.datum)throw new Error("Pool input UTxO does not contain a datum.");let s=(await e.utxosByOutRef([{txHash:t.protocolConfigOutRef.txHash,outputIndex:t.protocolConfigOutRef.outputIndex}]))[0];if(!s.datum)throw new Error("Protocol config UTxO does not contain a datum.");let g=U(s.datum),c=v(o.datum),p=c.tokenX||"lovelace",f=c.tokenY,w=o.assets.lovelace,d=R=>{if(R==="lovelace")return w;for(let[E,J]of Object.entries(o.assets))if(E===R)return J;return 0n},l=t.deltaAmount,a=l>0?p:f,b=l>0?f:p,u=e.newTx(),h=(await e.wallet().getUtxos()).reduce((R,E)=>R+(E.assets[a]||0n),0n),F=l<0n?-l:l;if(h<F)throw new Error(`Insufficient ${a} balance. Required: ${F}, Available: ${h}`);u=u.readFrom([r,s]),i&&(u=u.readFrom([i]));let B=0n,T="";if(p=="lovelace"){T=(0,O.validatorToRewardAddress)(e.config().network,i.scriptRef);try{B=(await e.delegationAt(T)).rewards}catch{B=0n}}let[P,y]=N(d(p),d(f),c,l,B,g.platformFeeRate);if(P<t.minOutChangeAmount)throw new Error(`Slippage too high. Expected at least ${t.minOutChangeAmount} but got ${P}`);let q=Q(Date.now(),e.config().network),z=W({...c,platformFeeX:BigInt(c.platformFeeX)+(a===(c.tokenX||"lovelace")?y:0n),platformFeeY:BigInt(c.platformFeeY)+(a===c.tokenY?y:0n),lastWithdrawEpoch:q,totalSwapFee:BigInt(c.totalSwapFee)+BigInt(g.swapFee)}),I={...o.assets};I[a]=I[a]+(l>0n?l:-l),I[b]=BigInt(I[b])-BigInt(P),I.lovelace=BigInt(I.lovelace)+BigInt(g.swapFee),u=u.pay.ToAddressWithData(o.address,{kind:"inline",value:z},I),u=u.attachMetadata(674,{msg:["Danogo Liquidity Pair: Swap"]});let H=[r,s];i&&H.push(i);let C=V(s,H);return u=u.collectFrom([o],S([o],[l],C,!1)),u=u.withdraw((0,O.validatorToRewardAddress)(e.config().network,r.scriptRef),0n,S([o],[l],C,!0)),p==="lovelace"&&q>c.lastWithdrawEpoch&&(u=u.withdraw(T,B,S([o],[l],C,!1))),u=u.validFrom(Date.now()-12e4).validTo(Date.now()+24e4).setMinFee(17000n).addSigner(await e.wallet().address()),await(await(await u.complete({localUPLCEval:!1})).sign.withWallet().complete()).submit()}getPoolsFromOgmiosTx(e,t){let o=[];return e.outputs.forEach((r,i)=>{let s=r.value,g=s[t];if(g&&r.datum){for(let[c,p]of Object.entries(g))if(p===1n){let f=t+c,w=`${e.id}#${i}`,d=s.ada.lovelace,l=j(s),a=v(r.datum),b=a.tokenX,u=a.tokenY,A=h=>{if(h==="lovelace"||h==="")return d;let F=h.slice(0,56),B=h.slice(56),P=l.find(y=>y.policyId===F)?.assets.find(y=>y.name===B);return P?P.value:0n};o.push({outRef:w,address:r.address,coin:d,multiAssets:l,validityNft:f,tokenA:b,tokenAReserve:A(b),tokenB:u,tokenBReserve:A(u),lpFeeRate:a.lpFeeRate,priceLowerNum:a.sqrtLowerPriceNum,priceLowerDen:a.sqrtLowerPriceDen,priceUpperNum:a.sqrtUpperPriceNum,priceUpperDen:a.sqrtUpperPriceDen,platformFeeA:a.platformFeeX,platformFeeB:a.platformFeeY,minAChange:a.minXChange,minBChange:a.minYChange,lpTokenTotalSupply:a.circulatingLPToken,lastWithdrawEpoch:a.lastWithdrawEpoch,totalSwapFee:a.totalSwapFee})}}}),o}},ct=L;
package/dist/sdk.mjs CHANGED
@@ -1 +1 @@
1
- import{validatorToRewardAddress as U}from"@lucid-evolution/lucid";function R(n,e){let o=(n>=0n?n:n+(1n<<256n)).toString(16);o.length%2&&(o="0"+o);let r=o.length/2;if(r>e)throw new Error(`Number ${n} requires ${r} bytes, but target length is ${e}.`);let i=new Uint8Array(e),s=e-r;for(let g=0;g<r;g++)i[g+s]=parseInt(o.slice(g*2,g*2+2),16);return i}var k=(n,e,t,o)=>{try{return{kind:"selected",inputs:n,makeRedeemer:i=>{let s=3n,g=R(o?t:i[0],1),c=R(i[0],1),p=R(s,1),f=R(0n,1),w=R(e[0],32),m=g.length+p.length+c.length+f.length+w.length,l=new Uint8Array(m),a=0;return l.set(g,a),a+=g.length,l.set(p,a),a+=p.length,l.set(c,a),a+=c.length,l.set(f,a),a+=f.length,l.set(w,a),"5824"+Buffer.from(l).toString("hex")}}}catch(r){throw console.error("Error creating pool redeemer:",r),r}};import{Data as d}from"@lucid-evolution/lucid";import{Data as z}from"@lucid-evolution/lucid";function H(n,e,t){return z.to(n,e,t)}import{Tagged as T,decodeFirstSync as Y}from"cbor";var M=n=>{let e=d.Tuple([d.Bytes(),d.Bytes()]),t=d.Tuple([d.Integer(),d.Integer()],{hasConstr:!0}),o=d.Tuple([e,e,d.Integer(),d.Integer(),d.Integer(),d.Integer(),t,t,d.Integer(),d.Integer(),d.Integer(),d.Integer()],{hasConstr:!0}),r=X(n.tokenX),i=X(n.tokenY),s=[r,i,BigInt(n.lpFeeRate),BigInt(n.platformFeeX),BigInt(n.platformFeeY),BigInt(n.totalSwapFee),[BigInt(n.sqrtLowerPriceNum),BigInt(n.sqrtLowerPriceDen)],[BigInt(n.sqrtUpperPriceNum),BigInt(n.sqrtUpperPriceDen)],BigInt(n.minXChange),BigInt(n.minYChange),BigInt(n.circulatingLPToken),BigInt(n.lastWithdrawEpoch)];return H(s,o)};var X=n=>{if(!n)return["",""];try{if(n.includes(".")){let o=n.split(".");return o.length===2?[o[0],o[1]]:[n,""]}let e=n.slice(0,56),t=n.slice(56);return[e,t]}catch(e){throw console.error(`Error parsing token ID "${n}":`,e),new Error(`Failed to parse token ID: ${n}`)}},S=n=>{let e=Y(Buffer.from(n,"hex")),t=e instanceof T?e.value:e;if(!Array.isArray(t))throw new Error("Invalid datum structure: expected array of fields");let o=i=>{let s=i instanceof T?i.value:i;if(Array.isArray(s)&&s.length===2){let g=s[0].toString("hex"),c=s[1].toString("hex");return g+c}throw new Error("Invalid AssetClass structure")},r=i=>{let s=i instanceof T?i.value:i;if(Array.isArray(s)&&s.length===2)return{num:s[0],den:s[1]};throw new Error("Invalid Ratio structure")};return{tokenX:o(t[0]),tokenY:o(t[1]),lpFeeRate:Number(t[2]),platformFeeX:t[3].toString(),platformFeeY:t[4].toString(),totalSwapFee:t[5].toString(),sqrtLowerPriceNum:r(t[6]).num,sqrtLowerPriceDen:r(t[6]).den,sqrtUpperPriceNum:r(t[7]).num,sqrtUpperPriceDen:r(t[7]).den,minXChange:t[8].toString(),minYChange:t[9].toString(),circulatingLPToken:t[10].toString(),lastWithdrawEpoch:Number(t[11])}},C=n=>{let e=Y(Buffer.from(n,"hex")),t=e instanceof T?e.value:e;if(!Array.isArray(t))throw new Error("Invalid datum structure: expected array of fields");return{platformFeeRate:t[0].toString(),swapFee:t[1].toString()}};var $=n=>{if(!n||Object.keys(n).length===0)return[];let e=[];for(let[t,o]of Object.entries(n)){if(t==="ada")continue;let r=[];for(let[i,s]of Object.entries(o))r.push({name:i,value:s});e.push({policyId:t,assets:r})}return e};var _=(n,e)=>{let t=432e6,o=1647899091e3,r=328;return e!=="Mainnet"&&(t=18e5),Math.floor((n-o)/t)+r};function E(n,e,t,o,r=0n,i){let s=o<0n?-o:o,g=t.tokenX===""?3000000n+BigInt(t.totalSwapFee):0n,c=BigInt(n)-BigInt(t.platformFeeX)+r-g,p=BigInt(e)-BigInt(t.platformFeeY),f=J(c,p,[BigInt(t.sqrtLowerPriceNum),BigInt(t.sqrtLowerPriceDen)],[BigInt(t.sqrtUpperPriceNum),BigInt(t.sqrtUpperPriceDen)]),w=j(f[0]*BigInt(t.sqrtUpperPriceDen),f[1]*BigInt(t.sqrtUpperPriceNum))+c,m=j(f[0]*BigInt(t.sqrtLowerPriceNum),f[1]*BigInt(t.sqrtLowerPriceDen))+p;return o>0n?W(s,w,m,p,BigInt(t.lpFeeRate),i):W(s,m,w,c,BigInt(t.lpFeeRate),i)}var W=(n,e,t,o,r,i)=>{let s=10000n,c=n*r/s*BigInt(i)/10000n,p=s-r,f=e*s+n*p,w=e*t,l=(t*f-w*s)/f;if(l>o)throw new Error("pool out exceeded");return[l,c]},J=(n,e,t,o)=>{let r=t[1]*o[1],i=t[0]*o[0],s=(e*r-n*i)*(e*r-n*i),g=4n*n*e*t[1]*t[1]*o[0]*o[0],c=K(s+g),p=e*r+n*i+c,f=2n*(o[0]*t[1]-o[1]*t[0]);return[p,f]};function K(n){if(n<0n)throw new Error("Square root of negative number");if(n<2n)return n;let e=n,t=e+n/e>>1n;for(;t<e;)e=t,t=e+n/e>>1n;return e}function j(n,e){if(e===0n)throw new Error("Division by zero");let t=n/e;return n%e===0n||n<0n!=e<0n?t:t+1n}function G(n,e){let o=[...e].sort((r,i)=>r.txHash===i.txHash?r.outputIndex-i.outputIndex:r.txHash<i.txHash?-1:1).findIndex(r=>r.txHash===n.txHash&&r.outputIndex===n.outputIndex);if(o===-1)throw new Error("Protocol config out ref not found in reference inputs");return BigInt(o)}var N=class{constructor(){}async calculateSwapOut(e,t){if(!e||!e.wallet())throw new Error("Please connect a wallet first.");let o=(await e.utxosByOutRef([{txHash:t.poolOutRef.txHash,outputIndex:t.poolOutRef.outputIndex}]))[0],r=null;if(t.stakingOutRef&&(r=(await e.utxosByOutRef([{txHash:t.stakingOutRef.txHash,outputIndex:t.stakingOutRef.outputIndex}]))[0]),!o.datum)throw new Error("Pool input UTxO does not contain a datum.");let i=(await e.utxosByOutRef([{txHash:t.protocolConfigOutRef.txHash,outputIndex:t.protocolConfigOutRef.outputIndex}]))[0];if(!i.datum)throw new Error("Protocol config UTxO does not contain a datum.");let s=C(i.datum),g=S(o.datum),c=g.tokenX||"lovelace",p=g.tokenY,f=o.assets.lovelace,w=u=>{if(u==="lovelace")return f;for(let[y,h]of Object.entries(o.assets))if(y===u)return h;return 0n},m=0n,l="";if(c=="lovelace"){l=U(e.config().network,r.scriptRef);try{m=(await e.delegationAt(l)).rewards}catch{m=0n}}let[a,x]=E(w(c),w(p),g,t.deltaAmount,m,s.platformFeeRate);return a}async submitSwap(e,t){if(!e||!e.wallet())throw new Error("Please connect a wallet first.");let o=(await e.utxosByOutRef([{txHash:t.poolOutRef.txHash,outputIndex:t.poolOutRef.outputIndex}]))[0],r=(await e.utxosByOutRef([{txHash:t.poolScriptOutRef.txHash,outputIndex:t.poolScriptOutRef.outputIndex}]))[0],i=null;if(t.stakingOutRef&&(i=(await e.utxosByOutRef([{txHash:t.stakingOutRef.txHash,outputIndex:t.stakingOutRef.outputIndex}]))[0]),!o.datum)throw new Error("Pool input UTxO does not contain a datum.");let s=(await e.utxosByOutRef([{txHash:t.protocolConfigOutRef.txHash,outputIndex:t.protocolConfigOutRef.outputIndex}]))[0];if(!s.datum)throw new Error("Protocol config UTxO does not contain a datum.");let g=C(s.datum),c=S(o.datum),p=c.tokenX||"lovelace",f=c.tokenY,w=o.assets.lovelace,m=A=>{if(A==="lovelace")return w;for(let[O,V]of Object.entries(o.assets))if(O===A)return V;return 0n},l=t.deltaAmount,a=l>0?p:f,x=l>0?f:p,u=e.newTx(),h=(await e.wallet().getUtxos()).reduce((A,O)=>A+(O.assets[a]||0n),0n),D=l<0n?-l:l;if(h<D)throw new Error(`Insufficient ${a} balance. Required: ${D}, Available: ${h}`);u=u.readFrom([r,s]),i&&(u=u.readFrom([i]));let I=0n,F="";if(p=="lovelace"){F=U(e.config().network,i.scriptRef);try{I=(await e.delegationAt(F)).rewards}catch{I=0n}}let[B,P]=E(m(p),m(f),c,l,I,g.platformFeeRate);if(B<t.minOutChangeAmount)throw new Error(`Slippage too high. Expected at least ${t.minOutChangeAmount} but got ${B}`);let L=_(Date.now(),e.config().network),Q=M({...c,platformFeeX:BigInt(c.platformFeeX)+(a===(c.tokenX||"lovelace")?P:0n),platformFeeY:BigInt(c.platformFeeY)+(a===c.tokenY?P:0n),lastWithdrawEpoch:L,totalSwapFee:BigInt(c.totalSwapFee)+BigInt(g.swapFee)}),b={...o.assets};b[a]=b[a]+(l>0n?l:-l),b[x]=BigInt(b[x])-BigInt(B),b.lovelace=BigInt(b.lovelace)+BigInt(g.swapFee),u=u.pay.ToAddressWithData(o.address,{kind:"inline",value:Q},b),u=u.attachMetadata(674,{msg:["Danogo Liquidity Pair: Swap"]});let q=[r,s];i&&q.push(i);let v=G(s,q);return u=u.collectFrom([o],k([o],[l],v,!1)),u=u.withdraw(U(e.config().network,r.scriptRef),0n,k([o],[l],v,!0)),p==="lovelace"&&L>c.lastWithdrawEpoch&&(u=u.withdraw(F,I,k([o],[l],v,!1))),u=u.validFrom(Date.now()-12e4).validTo(Date.now()+24e4).setMinFee(17000n).addSigner(await e.wallet().address()),await(await(await u.complete({localUPLCEval:!1})).sign.withWallet().complete()).submit()}getPoolsFromOgmiosTx(e,t){let o=[];return e.outputs.forEach((r,i)=>{let s=r.value,g=s[t];if(g&&r.datum){for(let[c,p]of Object.entries(g))if(p===1n){let f=t+c,w=`${e.id}#${i}`,m=s.ada.lovelace,l=$(s),a=S(r.datum),x=a.tokenX,u=a.tokenY,y=h=>{if(h==="lovelace"||h==="")return m;let D=h.slice(0,56),I=h.slice(56),B=l.find(P=>P.policyId===D)?.assets.find(P=>P.name===I);return B?B.value:0n};o.push({outRef:w,address:r.address,coin:m,multiAssets:l,validityNft:f,tokenA:x,tokenAReserve:y(x),tokenB:u,tokenBReserve:y(u),lpFeeRate:a.lpFeeRate,priceLowerNum:a.sqrtLowerPriceNum,priceLowerDen:a.sqrtLowerPriceDen,priceUpperNum:a.sqrtUpperPriceNum,priceUpperDen:a.sqrtUpperPriceDen,platformFeeA:a.platformFeeX,platformFeeB:a.platformFeeY,minAChange:a.minXChange,minBChange:a.minYChange,lpTokenTotalSupply:a.circulatingLPToken,lastWithdrawEpoch:a.lastWithdrawEpoch,totalSwapFee:a.totalSwapFee})}}}),o}},bt=N;export{bt as default};
1
+ import{validatorToRewardAddress as U}from"@lucid-evolution/lucid";function D(n,e){let o=(n>=0n?n:n+(1n<<256n)).toString(16);o.length%2&&(o="0"+o);let r=o.length/2;if(r>e)throw new Error(`Number ${n} requires ${r} bytes, but target length is ${e}.`);let i=new Uint8Array(e),s=e-r;for(let g=0;g<r;g++)i[g+s]=parseInt(o.slice(g*2,g*2+2),16);return i}var k=(n,e,t,o)=>{try{return{kind:"selected",inputs:n,makeRedeemer:i=>{let s=3n,g=D(o?t:i[0],1),c=D(i[0],1),p=D(s,1),f=D(0n,1),w=D(e[0],32),m=g.length+p.length+c.length+f.length+w.length,l=new Uint8Array(m),a=0;return l.set(g,a),a+=g.length,l.set(p,a),a+=p.length,l.set(c,a),a+=c.length,l.set(f,a),a+=f.length,l.set(w,a),"5824"+Buffer.from(l).toString("hex")}}}catch(r){throw console.error("Error creating pool redeemer:",r),r}};import{Data as d}from"@lucid-evolution/lucid";import{Data as V}from"@lucid-evolution/lucid";function H(n,e,t){return V.to(n,e,t)}import*as x from"cbor";var Y=n=>{let e=d.Tuple([d.Bytes(),d.Bytes()]),t=d.Tuple([d.Integer(),d.Integer()],{hasConstr:!0}),o=d.Tuple([e,e,d.Integer(),d.Integer(),d.Integer(),d.Integer(),t,t,d.Integer(),d.Integer(),d.Integer(),d.Integer()],{hasConstr:!0}),r=X(n.tokenX),i=X(n.tokenY),s=[r,i,BigInt(n.lpFeeRate),BigInt(n.platformFeeX),BigInt(n.platformFeeY),BigInt(n.totalSwapFee),[BigInt(n.sqrtLowerPriceNum),BigInt(n.sqrtLowerPriceDen)],[BigInt(n.sqrtUpperPriceNum),BigInt(n.sqrtUpperPriceDen)],BigInt(n.minXChange),BigInt(n.minYChange),BigInt(n.circulatingLPToken),BigInt(n.lastWithdrawEpoch)];return H(s,o)};var X=n=>{if(!n)return["",""];try{if(n.includes(".")){let o=n.split(".");return o.length===2?[o[0],o[1]]:[n,""]}let e=n.slice(0,56),t=n.slice(56);return[e,t]}catch(e){throw console.error(`Error parsing token ID "${n}":`,e),new Error(`Failed to parse token ID: ${n}`)}},S=n=>{let e=x.decodeFirstSync(Buffer.from(n,"hex")),t=e instanceof x.Tagged?e.value:e;if(!Array.isArray(t))throw new Error("Invalid datum structure: expected array of fields");let o=i=>{let s=i instanceof x.Tagged?i.value:i;if(Array.isArray(s)&&s.length===2){let g=s[0].toString("hex"),c=s[1].toString("hex");return g+c}throw new Error("Invalid AssetClass structure")},r=i=>{let s=i instanceof x.Tagged?i.value:i;if(Array.isArray(s)&&s.length===2)return{num:s[0],den:s[1]};throw new Error("Invalid Ratio structure")};return{tokenX:o(t[0]),tokenY:o(t[1]),lpFeeRate:Number(t[2]),platformFeeX:t[3].toString(),platformFeeY:t[4].toString(),totalSwapFee:t[5].toString(),sqrtLowerPriceNum:r(t[6]).num,sqrtLowerPriceDen:r(t[6]).den,sqrtUpperPriceNum:r(t[7]).num,sqrtUpperPriceDen:r(t[7]).den,minXChange:t[8].toString(),minYChange:t[9].toString(),circulatingLPToken:t[10].toString(),lastWithdrawEpoch:Number(t[11])}},C=n=>{let e=x.decodeFirstSync(Buffer.from(n,"hex")),t=e instanceof x.Tagged?e.value:e;if(!Array.isArray(t))throw new Error("Invalid datum structure: expected array of fields");return{platformFeeRate:t[0].toString(),swapFee:t[1].toString()}};var M=n=>{if(!n||Object.keys(n).length===0)return[];let e=[];for(let[t,o]of Object.entries(n)){if(t==="ada")continue;let r=[];for(let[i,s]of Object.entries(o))r.push({name:i,value:s});e.push({policyId:t,assets:r})}return e};var j=(n,e)=>{let t=432e6,o=1647899091e3,r=328;return e!=="Mainnet"&&(t=18e5),Math.floor((n-o)/t)+r};function E(n,e,t,o,r=0n,i){let s=o<0n?-o:o,g=t.tokenX===""?3000000n+BigInt(t.totalSwapFee):0n,c=BigInt(n)-BigInt(t.platformFeeX)+r-g,p=BigInt(e)-BigInt(t.platformFeeY),f=z(c,p,[BigInt(t.sqrtLowerPriceNum),BigInt(t.sqrtLowerPriceDen)],[BigInt(t.sqrtUpperPriceNum),BigInt(t.sqrtUpperPriceDen)]),w=W(f[0]*BigInt(t.sqrtUpperPriceDen),f[1]*BigInt(t.sqrtUpperPriceNum))+c,m=W(f[0]*BigInt(t.sqrtLowerPriceNum),f[1]*BigInt(t.sqrtLowerPriceDen))+p;return o>0n?$(s,w,m,p,BigInt(t.lpFeeRate),i):$(s,m,w,c,BigInt(t.lpFeeRate),i)}var $=(n,e,t,o,r,i)=>{let s=10000n,c=n*r/s*BigInt(i)/10000n,p=s-r,f=e*s+n*p,w=e*t,l=(t*f-w*s)/f;if(l>o)throw new Error("pool out exceeded");return[l,c]},z=(n,e,t,o)=>{let r=t[1]*o[1],i=t[0]*o[0],s=(e*r-n*i)*(e*r-n*i),g=4n*n*e*t[1]*t[1]*o[0]*o[0],c=J(s+g),p=e*r+n*i+c,f=2n*(o[0]*t[1]-o[1]*t[0]);return[p,f]};function J(n){if(n<0n)throw new Error("Square root of negative number");if(n<2n)return n;let e=n,t=e+n/e>>1n;for(;t<e;)e=t,t=e+n/e>>1n;return e}function W(n,e){if(e===0n)throw new Error("Division by zero");let t=n/e;return n%e===0n||n<0n!=e<0n?t:t+1n}function _(n,e){let o=[...e].sort((r,i)=>r.txHash===i.txHash?r.outputIndex-i.outputIndex:r.txHash<i.txHash?-1:1).findIndex(r=>r.txHash===n.txHash&&r.outputIndex===n.outputIndex);if(o===-1)throw new Error("Protocol config out ref not found in reference inputs");return BigInt(o)}var N=class{constructor(){}async calculateSwapOut(e,t){if(!e||!e.wallet())throw new Error("Please connect a wallet first.");let o=(await e.utxosByOutRef([{txHash:t.poolOutRef.txHash,outputIndex:t.poolOutRef.outputIndex}]))[0],r=null;if(t.stakingOutRef&&(r=(await e.utxosByOutRef([{txHash:t.stakingOutRef.txHash,outputIndex:t.stakingOutRef.outputIndex}]))[0]),!o.datum)throw new Error("Pool input UTxO does not contain a datum.");let i=(await e.utxosByOutRef([{txHash:t.protocolConfigOutRef.txHash,outputIndex:t.protocolConfigOutRef.outputIndex}]))[0];if(!i.datum)throw new Error("Protocol config UTxO does not contain a datum.");let s=C(i.datum),g=S(o.datum),c=g.tokenX||"lovelace",p=g.tokenY,f=o.assets.lovelace,w=u=>{if(u==="lovelace")return f;for(let[A,h]of Object.entries(o.assets))if(A===u)return h;return 0n},m=0n,l="";if(c=="lovelace"){l=U(e.config().network,r.scriptRef);try{m=(await e.delegationAt(l)).rewards}catch{m=0n}}let[a,b]=E(w(c),w(p),g,t.deltaAmount,m,s.platformFeeRate);return a}async submitSwap(e,t){if(!e||!e.wallet())throw new Error("Please connect a wallet first.");let o=(await e.utxosByOutRef([{txHash:t.poolOutRef.txHash,outputIndex:t.poolOutRef.outputIndex}]))[0],r=(await e.utxosByOutRef([{txHash:t.poolScriptOutRef.txHash,outputIndex:t.poolScriptOutRef.outputIndex}]))[0],i=null;if(t.stakingOutRef&&(i=(await e.utxosByOutRef([{txHash:t.stakingOutRef.txHash,outputIndex:t.stakingOutRef.outputIndex}]))[0]),!o.datum)throw new Error("Pool input UTxO does not contain a datum.");let s=(await e.utxosByOutRef([{txHash:t.protocolConfigOutRef.txHash,outputIndex:t.protocolConfigOutRef.outputIndex}]))[0];if(!s.datum)throw new Error("Protocol config UTxO does not contain a datum.");let g=C(s.datum),c=S(o.datum),p=c.tokenX||"lovelace",f=c.tokenY,w=o.assets.lovelace,m=R=>{if(R==="lovelace")return w;for(let[O,Q]of Object.entries(o.assets))if(O===R)return Q;return 0n},l=t.deltaAmount,a=l>0?p:f,b=l>0?f:p,u=e.newTx(),h=(await e.wallet().getUtxos()).reduce((R,O)=>R+(O.assets[a]||0n),0n),F=l<0n?-l:l;if(h<F)throw new Error(`Insufficient ${a} balance. Required: ${F}, Available: ${h}`);u=u.readFrom([r,s]),i&&(u=u.readFrom([i]));let B=0n,T="";if(p=="lovelace"){T=U(e.config().network,i.scriptRef);try{B=(await e.delegationAt(T)).rewards}catch{B=0n}}let[P,y]=E(m(p),m(f),c,l,B,g.platformFeeRate);if(P<t.minOutChangeAmount)throw new Error(`Slippage too high. Expected at least ${t.minOutChangeAmount} but got ${P}`);let L=j(Date.now(),e.config().network),G=Y({...c,platformFeeX:BigInt(c.platformFeeX)+(a===(c.tokenX||"lovelace")?y:0n),platformFeeY:BigInt(c.platformFeeY)+(a===c.tokenY?y:0n),lastWithdrawEpoch:L,totalSwapFee:BigInt(c.totalSwapFee)+BigInt(g.swapFee)}),I={...o.assets};I[a]=I[a]+(l>0n?l:-l),I[b]=BigInt(I[b])-BigInt(P),I.lovelace=BigInt(I.lovelace)+BigInt(g.swapFee),u=u.pay.ToAddressWithData(o.address,{kind:"inline",value:G},I),u=u.attachMetadata(674,{msg:["Danogo Liquidity Pair: Swap"]});let q=[r,s];i&&q.push(i);let v=_(s,q);return u=u.collectFrom([o],k([o],[l],v,!1)),u=u.withdraw(U(e.config().network,r.scriptRef),0n,k([o],[l],v,!0)),p==="lovelace"&&L>c.lastWithdrawEpoch&&(u=u.withdraw(T,B,k([o],[l],v,!1))),u=u.validFrom(Date.now()-12e4).validTo(Date.now()+24e4).setMinFee(17000n).addSigner(await e.wallet().address()),await(await(await u.complete({localUPLCEval:!1})).sign.withWallet().complete()).submit()}getPoolsFromOgmiosTx(e,t){let o=[];return e.outputs.forEach((r,i)=>{let s=r.value,g=s[t];if(g&&r.datum){for(let[c,p]of Object.entries(g))if(p===1n){let f=t+c,w=`${e.id}#${i}`,m=s.ada.lovelace,l=M(s),a=S(r.datum),b=a.tokenX,u=a.tokenY,A=h=>{if(h==="lovelace"||h==="")return m;let F=h.slice(0,56),B=h.slice(56),P=l.find(y=>y.policyId===F)?.assets.find(y=>y.name===B);return P?P.value:0n};o.push({outRef:w,address:r.address,coin:m,multiAssets:l,validityNft:f,tokenA:b,tokenAReserve:A(b),tokenB:u,tokenBReserve:A(u),lpFeeRate:a.lpFeeRate,priceLowerNum:a.sqrtLowerPriceNum,priceLowerDen:a.sqrtLowerPriceDen,priceUpperNum:a.sqrtUpperPriceNum,priceUpperDen:a.sqrtUpperPriceDen,platformFeeA:a.platformFeeX,platformFeeB:a.platformFeeY,minAChange:a.minXChange,minBChange:a.minYChange,lpTokenTotalSupply:a.circulatingLPToken,lastWithdrawEpoch:a.lastWithdrawEpoch,totalSwapFee:a.totalSwapFee})}}}),o}},ht=N;export{ht as default};
package/package.json CHANGED
@@ -1,28 +1,15 @@
1
1
  {
2
2
  "name": "danogo-clmm",
3
- "version": "0.0.1",
4
- "description": "An SDK to calculate and execute swaps on the Danogo liquidity platform with Lucid Evolution.",
3
+ "version": "0.0.2",
4
+ "description": "An SDK to calculate and execute swaps on the Danogo liquidity platform.",
5
5
  "main": "./dist/sdk.js",
6
6
  "module": "./dist/sdk.mjs",
7
7
  "types": "./dist/sdk.d.ts",
8
8
  "files": [
9
9
  "dist"
10
10
  ],
11
- "repository": {
12
- "type": "git",
13
- "url": "https://github.com/danogo-finance/clmm-sdk.git"
14
- },
15
- "homepage": "https://github.com/danogo-finance/clmm-sdk#readme",
16
- "license": "MIT",
17
- "keywords": [
18
- "cardano",
19
- "lucid",
20
- "danogo",
21
- "clmm",
22
- "swap"
23
- ],
24
11
  "scripts": {
25
- "dev": "tsx watch src/main.ts",
12
+ "dev": "tsc && node dist/main.js",
26
13
  "build": "tsup src/sdk.ts --format cjs,esm --dts --minify --clean --no-splitting",
27
14
  "prepublishOnly": "npm run build"
28
15
  },
@@ -30,12 +17,13 @@
30
17
  "access": "public"
31
18
  },
32
19
  "dependencies": {
20
+ "@lucid-evolution/lucid": "0.4.29",
21
+ "axios": "^1.13.2",
33
22
  "cbor": "^10.0.11"
34
23
  },
35
24
  "devDependencies": {
36
- "dotenv": "^17.3.1",
37
25
  "tsup": "^8.0.0",
38
- "tsx": "^4.21.0",
26
+ "ts-node": "^10.9.2",
39
27
  "typescript": "^5.9.2"
40
28
  },
41
29
  "peerDependencies": {