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 +9 -9
- package/dist/sdk.d.mts +4 -0
- package/dist/sdk.d.ts +4 -0
- package/dist/sdk.js +1 -1
- package/dist/sdk.mjs +1 -1
- package/package.json +5 -3
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
|
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
|
|
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.
|
|
4
|
-
"description": "An SDK to calculate and execute swaps on the Danogo liquidity platform
|
|
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": {
|