danogo-clmm 0.0.0 → 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
@@ -5,7 +5,7 @@ An SDK to calculate and execute swaps on the Danogo liquidity platform on the Ca
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- npm install danogo-clmm-sdk
8
+ npm install danogo-clmm
9
9
  ```
10
10
 
11
11
  ## Usage
@@ -19,7 +19,7 @@ This SDK relies on `@lucid-evolution/lucid` for wallet management and transactio
19
19
  Initialize the SDK with the Danogo API URL. Optionally, you can provide a custom pool script hash.
20
20
 
21
21
  ```typescript
22
- import DanogoSwap from "danogo-clmm-sdk";
22
+ import DanogoSwap from "danogo-clmm";
23
23
 
24
24
  const sdk = new DanogoSwap();
25
25
  ```
@@ -29,7 +29,7 @@ const sdk = new DanogoSwap();
29
29
  Calculate the expected output of a swap without submitting a transaction. This is useful for UI previews or checking rates.
30
30
 
31
31
  ```typescript
32
- import DanogoSwap from "danogo-clmm-sdk";
32
+ import DanogoSwap from "danogo-clmm";
33
33
  import { Lucid, Kupmios } from "@lucid-evolution/lucid";
34
34
 
35
35
  const sdk = new DanogoSwap();
@@ -68,7 +68,7 @@ async function main() {
68
68
  Build and submit a swap transaction using a Lucid instance.
69
69
 
70
70
  ```typescript
71
- import DanogoSwap from "danogo-clmm-sdk";
71
+ import DanogoSwap from "danogo-clmm";
72
72
  import { Lucid, Kupmios } from "@lucid-evolution/lucid";
73
73
 
74
74
  const sdk = new DanogoSwap();
@@ -131,7 +131,7 @@ Note for Kupmios Users: There is currently a known issue with lucid-evolution wh
131
131
  Extract pool data directly from an Ogmios transaction object.
132
132
 
133
133
  ```typescript
134
- import DanogoSwap from "danogo-clmm-sdk";
134
+ import DanogoSwap from "danogo-clmm";
135
135
  import { createInteractionContext, createChainSynchronizationClient } from "@cardano-ogmios/client";
136
136
 
137
137
  const sdk = new DanogoSwap();
@@ -141,11 +141,11 @@ async function main() {
141
141
  console.error,
142
142
  () => console.log("closed"),
143
143
  {
144
+ // example with demeter
144
145
  connection: {
145
- address: {
146
- http: "YOUR_HTTP_ENDPOINT",
147
- webSocket: "YOUR_WS_ENDPOINT",
148
- },
146
+ host: "ogmios1xxxxxxxxxxxx.cardano-preprod-v6.ogmios-m1.dmtr.host",
147
+ port: 443,
148
+ tls: true
149
149
  },
150
150
  }
151
151
  );
package/dist/sdk.d.mts CHANGED
@@ -11,6 +11,7 @@ interface PoolDatum {
11
11
  lpFeeRate: number;
12
12
  platformFeeX: bigint;
13
13
  platformFeeY: bigint;
14
+ totalSwapFee: bigint;
14
15
  minXChange: bigint;
15
16
  minYChange: bigint;
16
17
  circulatingLPToken: bigint;
@@ -43,6 +44,7 @@ interface ConcentratedPool {
43
44
  minBChange: bigint;
44
45
  lpTokenTotalSupply: bigint;
45
46
  lastWithdrawEpoch: number;
47
+ totalSwapFee: bigint;
46
48
  }
47
49
  interface SwapRequest {
48
50
  poolOutRef: OutRef;
@@ -50,11 +52,13 @@ interface SwapRequest {
50
52
  deltaAmount: bigint;
51
53
  minOutChangeAmount: bigint;
52
54
  stakingOutRef?: OutRef;
55
+ protocolConfigOutRef: OutRef;
53
56
  }
54
57
  interface QuoteSwapRequest {
55
58
  poolOutRef: OutRef;
56
59
  deltaAmount: bigint;
57
60
  stakingOutRef?: OutRef;
61
+ protocolConfigOutRef: OutRef;
58
62
  }
59
63
 
60
64
  declare class DanogoSwap {
package/dist/sdk.d.ts CHANGED
@@ -11,6 +11,7 @@ interface PoolDatum {
11
11
  lpFeeRate: number;
12
12
  platformFeeX: bigint;
13
13
  platformFeeY: bigint;
14
+ totalSwapFee: bigint;
14
15
  minXChange: bigint;
15
16
  minYChange: bigint;
16
17
  circulatingLPToken: bigint;
@@ -43,6 +44,7 @@ interface ConcentratedPool {
43
44
  minBChange: bigint;
44
45
  lpTokenTotalSupply: bigint;
45
46
  lastWithdrawEpoch: number;
47
+ totalSwapFee: bigint;
46
48
  }
47
49
  interface SwapRequest {
48
50
  poolOutRef: OutRef;
@@ -50,11 +52,13 @@ interface SwapRequest {
50
52
  deltaAmount: bigint;
51
53
  minOutChangeAmount: bigint;
52
54
  stakingOutRef?: OutRef;
55
+ protocolConfigOutRef: OutRef;
53
56
  }
54
57
  interface QuoteSwapRequest {
55
58
  poolOutRef: OutRef;
56
59
  deltaAmount: bigint;
57
60
  stakingOutRef?: OutRef;
61
+ protocolConfigOutRef: OutRef;
58
62
  }
59
63
 
60
64
  declare class DanogoSwap {
package/dist/sdk.js CHANGED
@@ -1 +1 @@
1
- "use strict";var _=Object.create;var T=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var Q=Object.getOwnPropertyNames;var V=Object.getPrototypeOf,z=Object.prototype.hasOwnProperty;var J=(n,e)=>{for(var t in e)T(n,t,{get:e[t],enumerable:!0})},q=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Q(e))!z.call(n,r)&&r!==t&&T(n,r,{get:()=>e[r],enumerable:!(o=G(e,r))||o.enumerable});return n};var K=(n,e,t)=>(t=n!=null?_(V(n)):{},q(e||!n||!n.__esModule?T(t,"default",{value:n,enumerable:!0}):t,n)),Z=n=>q(T({},"__esModule",{value:!0}),n);var ot={};J(ot,{default:()=>nt});module.exports=Z(ot);var S=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 l=0;l<r;l++)i[l+s]=parseInt(o.slice(l*2,l*2+2),16);return i}var v=(n,e)=>{try{return{kind:"selected",inputs:n,makeRedeemer:o=>{let r=3n,i=D(o[0],1),s=D(r,1),l=D(0n,1),g=D(e[0],32),p=i.length+s.length+i.length+l.length+g.length,u=new Uint8Array(p),a=0;return u.set(i,a),a+=i.length,u.set(s,a),a+=s.length,u.set(i,a),a+=i.length,u.set(l,a),a+=l.length,u.set(g,a),"5824"+Buffer.from(u).toString("hex")}}}catch(t){throw console.error("Error creating pool redeemer:",t),t}};var f=require("@lucid-evolution/lucid");var L=require("@lucid-evolution/lucid");function C(n,e,t){return L.Data.to(n,e,t)}var B=K(require("cbor")),Y=n=>{let e=f.Data.Tuple([f.Data.Bytes(),f.Data.Bytes()]),t=f.Data.Tuple([f.Data.Integer(),f.Data.Integer()],{hasConstr:!0}),o=f.Data.Tuple([e,e,f.Data.Integer(),f.Data.Integer(),f.Data.Integer(),t,t,f.Data.Integer(),f.Data.Integer(),f.Data.Integer(),f.Data.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.sqrtLowerPriceNum),BigInt(n.sqrtLowerPriceDen)],[BigInt(n.sqrtUpperPriceNum),BigInt(n.sqrtUpperPriceDen)],BigInt(n.minXChange),BigInt(n.minYChange),BigInt(n.circulatingLPToken),BigInt(n.lastWithdrawEpoch)];return C(s,o)},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}`)}},F=n=>{let e=B.decodeFirstSync(Buffer.from(n,"hex")),t=e instanceof B.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 B.Tagged?i.value:i;if(Array.isArray(s)&&s.length===2){let l=s[0].toString("hex"),g=s[1].toString("hex");return l+g}throw new Error("Invalid AssetClass structure")},r=i=>{let s=i instanceof B.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(),sqrtLowerPriceNum:r(t[5]).num,sqrtLowerPriceDen:r(t[5]).den,sqrtUpperPriceNum:r(t[6]).num,sqrtUpperPriceDen:r(t[6]).den,minXChange:t[7].toString(),minYChange:t[8].toString(),circulatingLPToken:t[9].toString(),lastWithdrawEpoch:Number(t[10])}};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 W=(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){let i=o<0n?-o:o,s=t.tokenX===""?3000000n:0n,l=BigInt(n)-BigInt(t.platformFeeX)+r-s,g=BigInt(e)-BigInt(t.platformFeeY),p=tt(l,g,[BigInt(t.sqrtLowerPriceNum),BigInt(t.sqrtLowerPriceDen)],[BigInt(t.sqrtUpperPriceNum),BigInt(t.sqrtUpperPriceDen)]),u=H(p[0]*BigInt(t.sqrtUpperPriceDen),p[1]*BigInt(t.sqrtUpperPriceNum))+l,a=H(p[0]*BigInt(t.sqrtLowerPriceNum),p[1]*BigInt(t.sqrtLowerPriceDen))+g;return o>0n?$(i,u,a,g,BigInt(t.lpFeeRate)):$(i,a,u,l,BigInt(t.lpFeeRate))}var $=(n,e,t,o,r)=>{let i=10000n,l=n*r/i*5n/100n,g=i-r,p=e*i+n*g,u=e*t,m=(t*p-u*i)/p;if(m>o)throw new Error("pool out exceeded");return[m,l]},tt=(n,e,t,o)=>{let r=t[1]*o[1],i=t[0]*o[0],s=(e*r-n*i)*(e*r-n*i),l=4n*n*e*t[1]*t[1]*o[0]*o[0],g=et(s+l),p=e*r+n*i+g,u=2n*(o[0]*t[1]-o[1]*t[0]);return[p,u]};function et(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 H(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}var U=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=F(o.datum),s=i.tokenX||"lovelace",l=i.tokenY,g=o.assets.lovelace,p=d=>{if(d==="lovelace")return g;for(let[b,x]of Object.entries(o.assets))if(b===d)return x;return 0n},u=e.newTx(),a=0n,m="";if(s=="lovelace"){m=(0,S.validatorToRewardAddress)(e.config().network,r.scriptRef);try{a=(await e.delegationAt(m)).rewards}catch{a=0n}}let[h,c]=N(p(s),p(l),i,t.deltaAmount,a);return h}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=F(o.datum),l=s.tokenX||"lovelace",g=s.tokenY,p=o.assets.lovelace,u=R=>{if(R==="lovelace")return p;for(let[O,j]of Object.entries(o.assets))if(O===R)return j;return 0n},a=t.deltaAmount,m=a>0?l:g,h=a>0?g:l,c=e.newTx(),b=(await e.wallet().getUtxos()).reduce((R,O)=>R+(O.assets[m]||0n),0n),x=a<0n?-a:a;if(b<x)throw new Error(`Insufficient ${m} balance. Required: ${x}, Available: ${b}`);c=c.readFrom([r]),i&&(c=c.readFrom([i]));let w=0n,A="";if(l=="lovelace"){A=(0,S.validatorToRewardAddress)(e.config().network,i.scriptRef);try{w=(await e.delegationAt(A)).rewards}catch{w=0n}}let[y,E]=N(u(l),u(g),s,a,w);if(y<t.minOutChangeAmount)throw new Error(`Slippage too high. Expected at least ${t.minOutChangeAmount} but got ${y}`);let I=W(Date.now(),e.config().network),P=Y({...s,platformFeeX:BigInt(s.platformFeeX)+(m===s.tokenX?E:0n),platformFeeY:BigInt(s.platformFeeY)+(m===s.tokenY?E:0n),lastWithdrawEpoch:I}),k={...o.assets};return k[m]=k[m]+(a>0n?a:-a),k[h]=BigInt(k[h])-BigInt(y),c=c.pay.ToAddressWithData(o.address,{kind:"inline",value:P},k),c=c.attachMetadata(674,{msg:["Danogo Liquidity Pair: Swap"]}),c=c.collectFrom([o],v([o],[a])),c=c.withdraw((0,S.validatorToRewardAddress)(e.config().network,r.scriptRef),0n,v([o],[a])),l==="lovelace"&&I>s.lastWithdrawEpoch&&(c=c.withdraw(A,w,v([o],[a]))),c=c.validFrom(Date.now()-12e4).validTo(Date.now()+24e4).setMinFee(17000n).addSigner(await e.wallet().address()),await(await(await c.complete({localUPLCEval:!1})).sign.withWallet().complete()).submit()}getPoolsFromOgmiosTx(e,t){let o=[];return e.outputs.forEach((r,i)=>{let s=r.value,l=s[t];if(l&&r.datum){for(let[g,p]of Object.entries(l))if(p===1n){let u=t+g,a=`${e.id}#${i}`,m=s.ada.lovelace,h=M(s),c=F(r.datum),d=c.tokenX,b=c.tokenY,x=w=>{if(w==="lovelace"||w==="")return m;let A=w.slice(0,56),y=w.slice(56),I=h.find(P=>P.policyId===A)?.assets.find(P=>P.name===y);return I?I.value:0n};o.push({outRef:a,address:r.address,coin:m,multiAssets:h,validityNft:u,tokenA:d,tokenAReserve:x(d),tokenB:b,tokenBReserve:x(b),lpFeeRate:c.lpFeeRate,priceLowerNum:c.sqrtLowerPriceNum,priceLowerDen:c.sqrtLowerPriceDen,priceUpperNum:c.sqrtUpperPriceNum,priceUpperDen:c.sqrtUpperPriceDen,platformFeeA:c.platformFeeX,platformFeeB:c.platformFeeY,minAChange:c.minXChange,minBChange:c.minYChange,lpTokenTotalSupply:c.circulatingLPToken,lastWithdrawEpoch:c.lastWithdrawEpoch})}}}),o}},nt=U;
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 O}from"@lucid-evolution/lucid";function T(n,e){let o=(n>=0n?n:n+(1n<<256n)).toString(16);o.length%2&&(o="0"+o);let s=o.length/2;if(s>e)throw new Error(`Number ${n} requires ${s} bytes, but target length is ${e}.`);let r=new Uint8Array(e),i=e-s;for(let l=0;l<s;l++)r[l+i]=parseInt(o.slice(l*2,l*2+2),16);return r}var D=(n,e)=>{try{return{kind:"selected",inputs:n,makeRedeemer:o=>{let s=3n,r=T(o[0],1),i=T(s,1),l=T(0n,1),g=T(e[0],32),p=r.length+i.length+r.length+l.length+g.length,u=new Uint8Array(p),a=0;return u.set(r,a),a+=r.length,u.set(i,a),a+=i.length,u.set(r,a),a+=r.length,u.set(l,a),a+=l.length,u.set(g,a),"5824"+Buffer.from(u).toString("hex")}}}catch(t){throw console.error("Error creating pool redeemer:",t),t}};import{Data as f}from"@lucid-evolution/lucid";import{Data as H}from"@lucid-evolution/lucid";function U(n,e,t){return H.to(n,e,t)}import*as B from"cbor";var L=n=>{let e=f.Tuple([f.Bytes(),f.Bytes()]),t=f.Tuple([f.Integer(),f.Integer()],{hasConstr:!0}),o=f.Tuple([e,e,f.Integer(),f.Integer(),f.Integer(),t,t,f.Integer(),f.Integer(),f.Integer(),f.Integer()],{hasConstr:!0}),s=q(n.tokenX),r=q(n.tokenY),i=[s,r,BigInt(n.lpFeeRate),BigInt(n.platformFeeX),BigInt(n.platformFeeY),[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 U(i,o)},q=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=B.decodeFirstSync(Buffer.from(n,"hex")),t=e instanceof B.Tagged?e.value:e;if(!Array.isArray(t))throw new Error("Invalid datum structure: expected array of fields");let o=r=>{let i=r instanceof B.Tagged?r.value:r;if(Array.isArray(i)&&i.length===2){let l=i[0].toString("hex"),g=i[1].toString("hex");return l+g}throw new Error("Invalid AssetClass structure")},s=r=>{let i=r instanceof B.Tagged?r.value:r;if(Array.isArray(i)&&i.length===2)return{num:i[0],den:i[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(),sqrtLowerPriceNum:s(t[5]).num,sqrtLowerPriceDen:s(t[5]).den,sqrtUpperPriceNum:s(t[6]).num,sqrtUpperPriceDen:s(t[6]).den,minXChange:t[7].toString(),minYChange:t[8].toString(),circulatingLPToken:t[9].toString(),lastWithdrawEpoch:Number(t[10])}};var C=n=>{if(!n||Object.keys(n).length===0)return[];let e=[];for(let[t,o]of Object.entries(n)){if(t==="ada")continue;let s=[];for(let[r,i]of Object.entries(o))s.push({name:r,value:i});e.push({policyId:t,assets:s})}return e};var M=(n,e)=>{let t=432e6,o=1647899091e3,s=328;return e!=="Mainnet"&&(t=18e5),Math.floor((n-o)/t)+s};function E(n,e,t,o,s=0n){let r=o<0n?-o:o,i=t.tokenX===""?3000000n:0n,l=BigInt(n)-BigInt(t.platformFeeX)+s-i,g=BigInt(e)-BigInt(t.platformFeeY),p=W(l,g,[BigInt(t.sqrtLowerPriceNum),BigInt(t.sqrtLowerPriceDen)],[BigInt(t.sqrtUpperPriceNum),BigInt(t.sqrtUpperPriceDen)]),u=Y(p[0]*BigInt(t.sqrtUpperPriceDen),p[1]*BigInt(t.sqrtUpperPriceNum))+l,a=Y(p[0]*BigInt(t.sqrtLowerPriceNum),p[1]*BigInt(t.sqrtLowerPriceDen))+g;return o>0n?X(r,u,a,g,BigInt(t.lpFeeRate)):X(r,a,u,l,BigInt(t.lpFeeRate))}var X=(n,e,t,o,s)=>{let r=10000n,l=n*s/r*5n/100n,g=r-s,p=e*r+n*g,u=e*t,m=(t*p-u*r)/p;if(m>o)throw new Error("pool out exceeded");return[m,l]},W=(n,e,t,o)=>{let s=t[1]*o[1],r=t[0]*o[0],i=(e*s-n*r)*(e*s-n*r),l=4n*n*e*t[1]*t[1]*o[0]*o[0],g=j(i+l),p=e*s+n*r+g,u=2n*(o[0]*t[1]-o[1]*t[0]);return[p,u]};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 Y(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}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],s=null;if(t.stakingOutRef&&(s=(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 r=v(o.datum),i=r.tokenX||"lovelace",l=r.tokenY,g=o.assets.lovelace,p=d=>{if(d==="lovelace")return g;for(let[b,x]of Object.entries(o.assets))if(b===d)return x;return 0n},u=e.newTx(),a=0n,m="";if(i=="lovelace"){m=O(e.config().network,s.scriptRef);try{a=(await e.delegationAt(m)).rewards}catch{a=0n}}let[h,c]=E(p(i),p(l),r,t.deltaAmount,a);return h}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],s=(await e.utxosByOutRef([{txHash:t.poolScriptOutRef.txHash,outputIndex:t.poolScriptOutRef.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=v(o.datum),l=i.tokenX||"lovelace",g=i.tokenY,p=o.assets.lovelace,u=R=>{if(R==="lovelace")return p;for(let[S,$]of Object.entries(o.assets))if(S===R)return $;return 0n},a=t.deltaAmount,m=a>0?l:g,h=a>0?g:l,c=e.newTx(),b=(await e.wallet().getUtxos()).reduce((R,S)=>R+(S.assets[m]||0n),0n),x=a<0n?-a:a;if(b<x)throw new Error(`Insufficient ${m} balance. Required: ${x}, Available: ${b}`);c=c.readFrom([s]),r&&(c=c.readFrom([r]));let w=0n,A="";if(l=="lovelace"){A=O(e.config().network,r.scriptRef);try{w=(await e.delegationAt(A)).rewards}catch{w=0n}}let[y,F]=E(u(l),u(g),i,a,w);if(y<t.minOutChangeAmount)throw new Error(`Slippage too high. Expected at least ${t.minOutChangeAmount} but got ${y}`);let I=M(Date.now(),e.config().network),P=L({...i,platformFeeX:BigInt(i.platformFeeX)+(m===i.tokenX?F:0n),platformFeeY:BigInt(i.platformFeeY)+(m===i.tokenY?F:0n),lastWithdrawEpoch:I}),k={...o.assets};return k[m]=k[m]+(a>0n?a:-a),k[h]=BigInt(k[h])-BigInt(y),c=c.pay.ToAddressWithData(o.address,{kind:"inline",value:P},k),c=c.attachMetadata(674,{msg:["Danogo Liquidity Pair: Swap"]}),c=c.collectFrom([o],D([o],[a])),c=c.withdraw(O(e.config().network,s.scriptRef),0n,D([o],[a])),l==="lovelace"&&I>i.lastWithdrawEpoch&&(c=c.withdraw(A,w,D([o],[a]))),c=c.validFrom(Date.now()-12e4).validTo(Date.now()+24e4).setMinFee(17000n).addSigner(await e.wallet().address()),await(await(await c.complete({localUPLCEval:!1})).sign.withWallet().complete()).submit()}getPoolsFromOgmiosTx(e,t){let o=[];return e.outputs.forEach((s,r)=>{let i=s.value,l=i[t];if(l&&s.datum){for(let[g,p]of Object.entries(l))if(p===1n){let u=t+g,a=`${e.id}#${r}`,m=i.ada.lovelace,h=C(i),c=v(s.datum),d=c.tokenX,b=c.tokenY,x=w=>{if(w==="lovelace"||w==="")return m;let A=w.slice(0,56),y=w.slice(56),I=h.find(P=>P.policyId===A)?.assets.find(P=>P.name===y);return I?I.value:0n};o.push({outRef:a,address:s.address,coin:m,multiAssets:h,validityNft:u,tokenA:d,tokenAReserve:x(d),tokenB:b,tokenBReserve:x(b),lpFeeRate:c.lpFeeRate,priceLowerNum:c.sqrtLowerPriceNum,priceLowerDen:c.sqrtLowerPriceDen,priceUpperNum:c.sqrtUpperPriceNum,priceUpperDen:c.sqrtUpperPriceDen,platformFeeA:c.platformFeeX,platformFeeB:c.platformFeeY,minAChange:c.minXChange,minBChange:c.minYChange,lpTokenTotalSupply:c.circulatingLPToken,lastWithdrawEpoch:c.lastWithdrawEpoch})}}}),o}},ut=N;export{ut 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,7 +1,7 @@
1
1
  {
2
2
  "name": "danogo-clmm",
3
- "version": "0.0.0",
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",
@@ -9,6 +9,7 @@
9
9
  "dist"
10
10
  ],
11
11
  "scripts": {
12
+ "dev": "tsc && node dist/main.js",
12
13
  "build": "tsup src/sdk.ts --format cjs,esm --dts --minify --clean --no-splitting",
13
14
  "prepublishOnly": "npm run build"
14
15
  },
@@ -17,11 +18,12 @@
17
18
  },
18
19
  "dependencies": {
19
20
  "@lucid-evolution/lucid": "0.4.29",
21
+ "axios": "^1.13.2",
20
22
  "cbor": "^10.0.11"
21
23
  },
22
24
  "devDependencies": {
23
- "ts-node": "^10.9.2",
24
25
  "tsup": "^8.0.0",
26
+ "ts-node": "^10.9.2",
25
27
  "typescript": "^5.9.2"
26
28
  },
27
29
  "peerDependencies": {