@scallop-io/scallop-deepbook-kit 0.3.0 → 0.4.1

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.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';var client=require('@mysten/sui/client'),ed25519=require('@mysten/sui/keypairs/ed25519'),transactions=require('@mysten/sui/transactions'),deepbookV3=require('@mysten/deepbook-v3'),cryptography=require('@mysten/sui/cryptography'),bcs$1=require('@mysten/bcs'),bcs=require('@mysten/sui/bcs'),bignumber_js=require('bignumber.js');var H=s=>/^0x[0-9a-fA-F]+$|^[0-9a-fA-F]+$/.test(s),V=s=>/^[a-zA-Z0-9+/]+={0,2}$/g.test(s),B=s=>{if(H(s))return bcs$1.fromHex(s);if(V(s))return bcs$1.fromBase64(s);throw new Error("The string is not a valid hex or base64 string.")},S=s=>{if(s.length===cryptography.LEGACY_PRIVATE_KEY_SIZE)return s.slice(0,cryptography.PRIVATE_KEY_SIZE);if(s.length===cryptography.PRIVATE_KEY_SIZE+1&&s[0]===0)return s.slice(1);if(s.length===cryptography.PRIVATE_KEY_SIZE)return s;throw new Error("invalid secret key")};var A=class{suiClient;keypair;address;marginPoolContract;supplierCapId;dbConfig;supplierCapPackageId;constructor({network:r,fullnodeUrl:e,supplierCapId:t,privateKey:o,supplierCapPackageId:i,dbConfig:n}){let a=e??client.getFullnodeUrl(r);this.suiClient=new client.SuiClient({url:a}),this.keypair=this.#r(o),this.address=this.keypair.getPublicKey().toSuiAddress(),this.supplierCapId=t,this.dbConfig=n??new deepbookV3.DeepBookConfig({network:r,address:this.address}),this.marginPoolContract=new deepbookV3.MarginPoolContract(this.dbConfig),this.supplierCapPackageId=i??this.dbConfig.MARGIN_PACKAGE_ID;}#r(r){if(r.startsWith(cryptography.SUI_PRIVATE_KEY_PREFIX)){let{secretKey:e}=cryptography.decodeSuiPrivateKey(r);return ed25519.Ed25519Keypair.fromSecretKey(S(e))}return ed25519.Ed25519Keypair.fromSecretKey(S(B(r)))}async#t(){let r=`${this.dbConfig.MARGIN_PACKAGE_ID}::margin_pool::SupplierCap`;return (await this.suiClient.getOwnedObjects({owner:this.address,filter:{StructType:r},options:{showType:true}})).data?.[0]?.data?.objectId}async initialize(){if(this.supplierCapId)return this.supplierCapId;let r=await this.#t();if(r)return this.supplierCapId=r,r;let e=await this.createSupplierCap();if(!e)throw new Error("Failed to create Supplier Cap");return this.supplierCapId=e,e}async createSupplierCap(){try{let r=new transactions.Transaction;r.setSender(this.address);let e=r.moveCall({target:`${this.supplierCapPackageId}::margin_pool::mint_supplier_cap`,arguments:[r.object(this.dbConfig.MARGIN_REGISTRY_ID),r.object.clock()]});r.transferObjects([e],r.pure.address(this.address));let t=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:r,options:{showEffects:!0,showObjectChanges:!0}});if(t.errors&&t.errors.length>0)throw new Error(`Transaction failed with errors: ${t.errors.map(o=>o.toString()).join(", ")}`);if(t.objectChanges){for(let o of t.objectChanges)if(o.type==="created"&&o.objectType.includes("SupplierCap"))return o.objectId}return null}catch(r){throw new Error(`Failed to create Supplier Cap: ${r.message||r}`)}}async createSupplyReferral(r){try{let e=new transactions.Transaction;e.setSender(this.address);let t=this.dbConfig.getMarginPool(r);if(!t)throw new Error(`Margin pool configuration not found for coin: ${r}`);e.moveCall({target:`${this.dbConfig.MARGIN_PACKAGE_ID}::margin_pool::mint_supply_referral`,arguments:[e.object(t.address),e.object(this.dbConfig.MARGIN_REGISTRY_ID),e.object.clock()],typeArguments:[t.type]});let o=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:e,options:{showEffects:!0,showObjectChanges:!0}});if(o.errors&&o.errors.length>0)throw new Error(`Transaction failed with errors: ${o.errors.map(i=>i.toString()).join(", ")}`);if(o.objectChanges){for(let i of o.objectChanges)if(i.type==="created"&&i.objectType.includes("SupplyReferral"))return i.objectId}return null}catch(e){throw new Error(`Failed to create Supply Referral: ${e.message||e}`)}}async supplyToMarginPool(r,e,t){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let o=new transactions.Transaction;o.setSender(this.address);let i=o.object(this.supplierCapId);o.add(this.marginPoolContract.supplyToMarginPool(r,i,e,t));let{errors:n}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:o,options:{showEffects:!0,showObjectChanges:!0}});if(n&&n.length>0)throw new Error(`Transaction failed with errors: ${n.map(a=>a.toString()).join(", ")}`);return !0}catch(o){throw new Error(`Failed to supply to margin pool: ${o.message||o}`)}}async withdrawFromMarginPool(r,e){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let t=new transactions.Transaction,o=t.object(this.supplierCapId),n=this.marginPoolContract.withdrawFromMarginPool(r,o,e)(t);t.transferObjects([n],this.address);let{errors:a}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:t,options:{showEffects:!0,showObjectChanges:!0}});if(a&&a.length>0)throw new Error(`Transaction failed with errors: ${a.map(p=>p.toString()).join(", ")}`);return !0}catch(t){throw new Error(`Failed to withdraw from margin pool: ${t.message||t}`)}}async withdrawReferralFees(r,e){try{let t=new transactions.Transaction;t.add(this.marginPoolContract.withdrawReferralFees(r,e));let{errors:o}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:t,options:{showEffects:!0,showObjectChanges:!0,showBalanceChanges:!0}});if(o&&o.length>0)throw new Error(`Transaction failed with errors: ${o.map(i=>i.toString()).join(", ")}`);return !0}catch(t){throw new Error(`Failed to withdraw referral fees: ${t.message||t}`)}}async getBalance(r){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let e=new transactions.Transaction;e.add(this.marginPoolContract.userSupplyAmount(r,this.supplierCapId));let t=await this.suiClient.devInspectTransactionBlock({sender:this.address,transactionBlock:e}),o=0;if(t&&t.results&&t.results[0]&&t.results[0].returnValues){let c=t.results[0].returnValues[0];if(c&&c[0]){let u=Buffer.from(c[0]).readBigUInt64LE(),g=this.dbConfig.getCoin(r).scalar;o=Number(u)/g;}}let i=this.dbConfig.getCoin(r).type,n=await this.suiClient.getBalance({owner:this.address,coinType:i}),a=this.dbConfig.getCoin(r).scalar,p=Number(n.totalBalance)/a;return {userSupplyAmount:o,walletBalance:p}}catch(e){throw new Error(`Failed to get balance: ${e.message||e}`)}}getSupplierCapId(){return this.supplierCapId}getAddress(){return this.address}};var C=["supplyCap","maxUtilizationRate","protocolSpread","minBorrow","interestRate","totalSupply","supplyShares","totalBorrow","borrowShares","lastUpdateTimestamp"],P=["userSupplyShares","userSupplyAmount"],_={supplyCap:"U64",maxUtilizationRate:"U64",protocolSpread:"U64",minBorrow:"U64",interestRate:"U64",totalSupply:"U64",supplyShares:"U64",totalBorrow:"U64",borrowShares:"U64",lastUpdateTimestamp:"U64",userSupplyShares:"U64",userSupplyAmount:"U64"};var f=(s,r)=>s*r/BigInt(deepbookV3.FLOAT_SCALAR),d=s=>Number(s)/deepbookV3.FLOAT_SCALAR;var nr=new Set(P),ar=s=>nr.has(s),R=class{marginPoolContract;dbConfig;suiClient;constructor({network:r,address:e="",suiClient:t,dbConfig:o}={}){let i=r??o?.network??"mainnet";if(this.dbConfig=o??new deepbookV3.DeepBookConfig({network:i,address:e}),this.suiClient=t??new client.SuiClient({url:client.getFullnodeUrl(i)}),this.marginPoolContract=new deepbookV3.MarginPoolContract(this.dbConfig),r!==void 0&&o!==void 0&&r!==o.network)throw new Error(`Mismatch between provided network (${r}) and dbConfig network (${o.network}).`)}get network(){return this.dbConfig.network}#r(r,e,t,o){if(ar(e)){let i=this.marginPoolContract[e];if(o==null)throw new Error(`supplierCap is required for '${e}'.`);r.add(i(t,o));}else {let i=this.marginPoolContract[e];r.add(i(t));}}parseInspectResultToBcsStructs(r,e){let t=r.results;if(!t)throw new Error("No results found in DevInspect output.");return e.reduce((o,i,n)=>{let a=t[n]?.returnValues?.[0]?.[0];if(!a)return o;let p=bcs.bcs[_[i]];return o[i]=p.parse(new Uint8Array(a)),o},{})}formatResult(r,e){let t=this.dbConfig.getCoin(e),o={supplyCap:0,maxUtilizationRate:0,protocolSpread:0,minBorrow:0,interestRate:0,totalSupply:0,supplyShares:0,totalBorrow:0,borrowShares:0,lastUpdateTimestamp:0,userSupplyShares:0,userSupplyAmount:0,decimals:t.scalar.toString().length-1,highKink:0,baseBorrowApr:0,borrowAprOnHighKink:0,maxBorrowApr:0,supplyApr:0,utilizationRate:0,...t};if(!t)return o;let i=new Set(["interestRate","maxUtilizationRate","protocolSpread"]);for(let[n,a]of Object.entries(r))n==="lastUpdateTimestamp"?o[n]=Number(a):i.has(n)?o[n]=new bignumber_js.BigNumber(a).dividedBy(deepbookV3.FLOAT_SCALAR).toNumber():o[n]=new bignumber_js.BigNumber(a).dividedBy(t.scalar).toNumber();return o}computeBorrowAprAtUtil(r,e){let t=BigInt(e.base_rate),o=BigInt(e.base_slope),i=BigInt(e.excess_slope),n=BigInt(e.optimal_utilization);return r<n?t+f(r,o):t+f(n,o)+f(r-n,i)}calculateKinksAndRate(r,e,t,o){let i=BigInt(r.optimal_utilization),n=BigInt(e.max_utilization_rate),a=this.computeBorrowAprAtUtil(i,r),p=this.computeBorrowAprAtUtil(n,r),c=BigInt(bignumber_js.BigNumber(t.total_borrow).dividedBy(t.total_supply).shiftedBy(9).decimalPlaces(0).toString()),u=f(f(o,c),BigInt(deepbookV3.FLOAT_SCALAR)-BigInt(e.protocol_spread));return {raw:{baseBorrowApr:r.base_rate,highKink:i,borrowAprOnHighKink:a,maxBorrowApr:p,supplyApr:u,utilizationRate:c},normalized:{baseBorrowApr:d(BigInt(r.base_rate)),highKink:d(i),borrowAprOnHighKink:d(a),maxBorrowApr:d(p),supplyApr:d(u),utilizationRate:d(c)}}}async#t(r,e,t){let{address:o}=this.dbConfig.getMarginPool(r);t??=await this.suiClient.getObject({id:o,options:{showContent:true}});let i=(t.data?.content).fields,n=i.config.fields,a=n.interest_config.fields,p=n.margin_pool_config.fields,c=i.state.fields,{normalized:u}=this.calculateKinksAndRate(a,p,c,e);return u}async getPoolParameters(r,e,t=new transactions.Transaction,o=true){if(C.forEach(g=>this.#r(t,g,r)),e&&P.forEach(g=>this.#r(t,g,r,e)),!o)return t;let i=[...C,...P],n=await this.suiClient.devInspectTransactionBlock({transactionBlock:t,sender:this.dbConfig.address}),a=this.parseInspectResultToBcsStructs(n,i),p=this.formatResult(a,r),c=BigInt(a.interestRate??0),u=await this.#t(r,c);return {...p,...u}}async getPoolsParameters(r,e,t=new transactions.Transaction,o=true){for(let l of r)C.forEach(m=>this.#r(t,m,l)),e&&P.forEach(m=>this.#r(t,m,l,e));if(!o)return t;let i=[...C,...e?P:[]],n=i.length,a=r.map(l=>this.dbConfig.getMarginPool(l).address),p=50,c=[];for(let l=0;l<a.length;l+=p)c.push(a.slice(l,l+p));let[u,...g]=await Promise.all([this.suiClient.devInspectTransactionBlock({transactionBlock:t,sender:this.dbConfig.address}),...c.map(l=>this.suiClient.multiGetObjects({ids:l,options:{showContent:true}}))]),v=g.flat(),I=u.results;if(!I)throw new Error("No results found in DevInspect output.");return Promise.all(r.map(async(l,m)=>{let N=m*n,T=i.reduce((y,E,W)=>{let K=I[N+W]?.returnValues?.[0]?.[0];if(!K)return y;let $=bcs.bcs[_[E]];return y[E]=$.parse(new Uint8Array(K)),y},{}),z=this.formatResult(T,l),G=BigInt(T.interestRate??0),w=v[m];if(!w||w.error)throw new Error(`Failed to fetch interest config for ${l}`);let F=await this.#t(l,G,w);return {...z,...F}}))}};exports.DeepBookMarginPool=R;exports.DeepBookMarginToolkit=A;
1
+ 'use strict';var client=require('@mysten/sui/client'),ed25519=require('@mysten/sui/keypairs/ed25519'),transactions=require('@mysten/sui/transactions'),deepbookV3=require('@mysten/deepbook-v3'),cryptography=require('@mysten/sui/cryptography'),bcs$1=require('@mysten/bcs'),bcs=require('@mysten/sui/bcs'),bignumber_js=require('bignumber.js'),utils=require('@mysten/sui/utils');var H=s=>/^0x[0-9a-fA-F]+$|^[0-9a-fA-F]+$/.test(s),V=s=>/^[a-zA-Z0-9+/]+={0,2}$/g.test(s),B=s=>{if(H(s))return bcs$1.fromHex(s);if(V(s))return bcs$1.fromBase64(s);throw new Error("The string is not a valid hex or base64 string.")},S=s=>{if(s.length===cryptography.LEGACY_PRIVATE_KEY_SIZE)return s.slice(0,cryptography.PRIVATE_KEY_SIZE);if(s.length===cryptography.PRIVATE_KEY_SIZE+1&&s[0]===0)return s.slice(1);if(s.length===cryptography.PRIVATE_KEY_SIZE)return s;throw new Error("invalid secret key")};var A=class{suiClient;keypair;address;marginPoolContract;supplierCapId;dbConfig;supplierCapPackageId;constructor({network:t,fullnodeUrl:e,supplierCapId:r,privateKey:o,supplierCapPackageId:i,dbConfig:a}){let n=e??client.getFullnodeUrl(t);this.suiClient=new client.SuiClient({url:n}),this.keypair=this.#t(o),this.address=this.keypair.getPublicKey().toSuiAddress(),this.supplierCapId=r,this.dbConfig=a??new deepbookV3.DeepBookConfig({network:t,address:this.address}),this.marginPoolContract=new deepbookV3.MarginPoolContract(this.dbConfig),this.supplierCapPackageId=i??this.dbConfig.MARGIN_PACKAGE_ID;}#t(t){if(t.startsWith(cryptography.SUI_PRIVATE_KEY_PREFIX)){let{secretKey:e}=cryptography.decodeSuiPrivateKey(t);return ed25519.Ed25519Keypair.fromSecretKey(S(e))}return ed25519.Ed25519Keypair.fromSecretKey(S(B(t)))}async#r(){let t=`${this.dbConfig.MARGIN_PACKAGE_ID}::margin_pool::SupplierCap`;return (await this.suiClient.getOwnedObjects({owner:this.address,filter:{StructType:t},options:{showType:true}})).data?.[0]?.data?.objectId}async initialize(){if(this.supplierCapId)return this.supplierCapId;let t=await this.#r();if(t)return this.supplierCapId=t,t;let e=await this.createSupplierCap();if(!e)throw new Error("Failed to create Supplier Cap");return this.supplierCapId=e,e}async createSupplierCap(){try{let t=new transactions.Transaction;t.setSender(this.address);let e=t.moveCall({target:`${this.supplierCapPackageId}::margin_pool::mint_supplier_cap`,arguments:[t.object(this.dbConfig.MARGIN_REGISTRY_ID),t.object.clock()]});t.transferObjects([e],t.pure.address(this.address));let r=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:t,options:{showEffects:!0,showObjectChanges:!0}});if(r.errors&&r.errors.length>0)throw new Error(`Transaction failed with errors: ${r.errors.map(o=>o.toString()).join(", ")}`);if(r.objectChanges){for(let o of r.objectChanges)if(o.type==="created"&&o.objectType.includes("SupplierCap"))return o.objectId}return null}catch(t){throw new Error(`Failed to create Supplier Cap: ${t.message||t}`)}}async createSupplyReferral(t){try{let e=new transactions.Transaction;e.setSender(this.address);let r=this.dbConfig.getMarginPool(t);if(!r)throw new Error(`Margin pool configuration not found for coin: ${t}`);e.moveCall({target:`${this.dbConfig.MARGIN_PACKAGE_ID}::margin_pool::mint_supply_referral`,arguments:[e.object(r.address),e.object(this.dbConfig.MARGIN_REGISTRY_ID),e.object.clock()],typeArguments:[r.type]});let o=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:e,options:{showEffects:!0,showObjectChanges:!0}});if(o.errors&&o.errors.length>0)throw new Error(`Transaction failed with errors: ${o.errors.map(i=>i.toString()).join(", ")}`);if(o.objectChanges){for(let i of o.objectChanges)if(i.type==="created"&&i.objectType.includes("SupplyReferral"))return i.objectId}return null}catch(e){throw new Error(`Failed to create Supply Referral: ${e.message||e}`)}}async supplyToMarginPool(t,e,r){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let o=new transactions.Transaction;o.setSender(this.address);let i=o.object(this.supplierCapId);o.add(this.marginPoolContract.supplyToMarginPool(t,i,e,r));let{errors:a}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:o,options:{showEffects:!0,showObjectChanges:!0}});if(a&&a.length>0)throw new Error(`Transaction failed with errors: ${a.map(n=>n.toString()).join(", ")}`);return !0}catch(o){throw new Error(`Failed to supply to margin pool: ${o.message||o}`)}}async withdrawFromMarginPool(t,e){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let r=new transactions.Transaction,o=r.object(this.supplierCapId),a=this.marginPoolContract.withdrawFromMarginPool(t,o,e)(r);r.transferObjects([a],this.address);let{errors:n}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:r,options:{showEffects:!0,showObjectChanges:!0}});if(n&&n.length>0)throw new Error(`Transaction failed with errors: ${n.map(l=>l.toString()).join(", ")}`);return !0}catch(r){throw new Error(`Failed to withdraw from margin pool: ${r.message||r}`)}}async withdrawReferralFees(t,e){try{let r=new transactions.Transaction;r.add(this.marginPoolContract.withdrawReferralFees(t,e));let{errors:o}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:r,options:{showEffects:!0,showObjectChanges:!0,showBalanceChanges:!0}});if(o&&o.length>0)throw new Error(`Transaction failed with errors: ${o.map(i=>i.toString()).join(", ")}`);return !0}catch(r){throw new Error(`Failed to withdraw referral fees: ${r.message||r}`)}}async getBalance(t){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let e=new transactions.Transaction;e.add(this.marginPoolContract.userSupplyAmount(t,this.supplierCapId));let r=await this.suiClient.devInspectTransactionBlock({sender:this.address,transactionBlock:e}),o=0;if(r&&r.results&&r.results[0]&&r.results[0].returnValues){let p=r.results[0].returnValues[0];if(p&&p[0]){let u=Buffer.from(p[0]).readBigUInt64LE(),g=this.dbConfig.getCoin(t).scalar;o=Number(u)/g;}}let i=this.dbConfig.getCoin(t).type,a=await this.suiClient.getBalance({owner:this.address,coinType:i}),n=this.dbConfig.getCoin(t).scalar,l=Number(a.totalBalance)/n;return {userSupplyAmount:o,walletBalance:l}}catch(e){throw new Error(`Failed to get balance: ${e.message||e}`)}}getSupplierCapId(){return this.supplierCapId}getAddress(){return this.address}};var C=["supplyCap","maxUtilizationRate","protocolSpread","minBorrow","interestRate","totalSupply","supplyShares","totalBorrow","borrowShares","lastUpdateTimestamp"],f=["userSupplyShares","userSupplyAmount"],_={supplyCap:"U64",maxUtilizationRate:"U64",protocolSpread:"U64",minBorrow:"U64",interestRate:"U64",totalSupply:"U64",supplyShares:"U64",totalBorrow:"U64",borrowShares:"U64",lastUpdateTimestamp:"U64",userSupplyShares:"U64",userSupplyAmount:"U64"};var P=(s,t)=>s*t/BigInt(deepbookV3.FLOAT_SCALAR),d=s=>Number(s)/deepbookV3.FLOAT_SCALAR;var nt=new Set(f),at=s=>nt.has(s),I=class{marginPoolContract;dbConfig;suiClient;constructor({network:t,address:e="",suiClient:r,dbConfig:o}={}){let i=t??o?.network??"mainnet";if(this.dbConfig=o??new deepbookV3.DeepBookConfig({network:i,address:e}),this.suiClient=r??new client.SuiClient({url:client.getFullnodeUrl(i)}),this.marginPoolContract=new deepbookV3.MarginPoolContract(this.dbConfig),t!==void 0&&o!==void 0&&t!==o.network)throw new Error(`Mismatch between provided network (${t}) and dbConfig network (${o.network}).`)}get network(){return this.dbConfig.network}#t(t,e,r,o){if(at(e)){let i=this.marginPoolContract[e];if(o==null)throw new Error(`supplierCap is required for '${e}'.`);t.add(i(r,o));}else {let i=this.marginPoolContract[e];t.add(i(r));}}parseInspectResultToBcsStructs(t,e){let r=t.results;if(!r)throw new Error("No results found in DevInspect output.");return e.reduce((o,i,a)=>{let n=r[a]?.returnValues?.[0]?.[0];if(!n)return o;let l=bcs.bcs[_[i]];return o[i]=l.parse(new Uint8Array(n)),o},{})}formatResult(t,e){let r=this.dbConfig.getCoin(e),o={supplyCap:0,maxUtilizationRate:0,protocolSpread:0,minBorrow:0,interestRate:0,totalSupply:0,supplyShares:0,totalBorrow:0,borrowShares:0,lastUpdateTimestamp:0,userSupplyShares:0,userSupplyAmount:0,decimals:r.scalar.toString().length-1,highKink:0,baseBorrowApr:0,borrowAprOnHighKink:0,maxBorrowApr:0,supplyApr:0,utilizationRate:0,...r};if(!r)return o;let i=new Set(["interestRate","maxUtilizationRate","protocolSpread"]);for(let[a,n]of Object.entries(t))a==="lastUpdateTimestamp"?o[a]=Number(n):i.has(a)?o[a]=new bignumber_js.BigNumber(n).dividedBy(deepbookV3.FLOAT_SCALAR).toNumber():o[a]=new bignumber_js.BigNumber(n).dividedBy(r.scalar).toNumber();return o}computeBorrowAprAtUtil(t,e){let r=BigInt(e.base_rate),o=BigInt(e.base_slope),i=BigInt(e.excess_slope),a=BigInt(e.optimal_utilization);return t<a?r+P(t,o):r+P(a,o)+P(t-a,i)}calculateKinksAndRate(t,e,r,o){let i=BigInt(t.optimal_utilization),a=BigInt(e.max_utilization_rate),n=this.computeBorrowAprAtUtil(i,t),l=this.computeBorrowAprAtUtil(a,t),p=BigInt(bignumber_js.BigNumber(r.total_borrow).dividedBy(r.total_supply).shiftedBy(9).decimalPlaces(0).toString()),u=P(P(o,p),BigInt(deepbookV3.FLOAT_SCALAR)-BigInt(e.protocol_spread));return {raw:{baseBorrowApr:t.base_rate,highKink:i,borrowAprOnHighKink:n,maxBorrowApr:l,supplyApr:u,utilizationRate:p},normalized:{baseBorrowApr:d(BigInt(t.base_rate)),highKink:d(i),borrowAprOnHighKink:d(n),maxBorrowApr:d(l),supplyApr:d(u),utilizationRate:d(p)}}}async#r(t,e,r){let{address:o}=this.dbConfig.getMarginPool(t);r??=await this.suiClient.getObject({id:o,options:{showContent:true}});let i=(r.data?.content).fields,a=i.config.fields,n=a.interest_config.fields,l=a.margin_pool_config.fields,p=i.state.fields,{normalized:u}=this.calculateKinksAndRate(n,l,p,e);return u}async getPoolParameters(t,e,r=new transactions.Transaction,o=true){if(C.forEach(g=>this.#t(r,g,t)),e&&f.forEach(g=>this.#t(r,g,t,e)),!o)return r;let i=[...C,...f],a=await this.suiClient.devInspectTransactionBlock({transactionBlock:r,sender:this.dbConfig.address}),n=this.parseInspectResultToBcsStructs(a,i),l=this.formatResult(n,t),p=BigInt(n.interestRate??0),u=await this.#r(t,p);return {...l,...u}}async getPoolsParameters(t,e,r=new transactions.Transaction,o=true){for(let c of t)C.forEach(m=>this.#t(r,m,c)),e&&f.forEach(m=>this.#t(r,m,c,e));if(!o)return r;let i=[...C,...e?f:[]],a=i.length,n=t.map(c=>this.dbConfig.getMarginPool(c).address),l=50,p=[];for(let c=0;c<n.length;c+=l)p.push(n.slice(c,c+l));let[u,...g]=await Promise.all([this.suiClient.devInspectTransactionBlock({transactionBlock:r,sender:this.dbConfig.address}),...p.map(c=>this.suiClient.multiGetObjects({ids:c,options:{showContent:true}}))]),v=g.flat(),R=u.results;if(!R)throw new Error("No results found in DevInspect output.");return Promise.all(t.map(async(c,m)=>{let N=m*a,T=i.reduce((y,E,L)=>{let K=R[N+L]?.returnValues?.[0]?.[0];if(!K)return y;let $=bcs.bcs[_[E]];return y[E]=$.parse(new Uint8Array(K)),y},{}),z=this.formatResult(T,c),G=BigInt(T.interestRate??0),w=v[m];if(!w||w.error)throw new Error(`Failed to fetch interest config for ${c}`);let F=await this.#r(c,G,w);return {...z,...F}}))}};var ct="0x7f7351ef7e5089dfddf17f55abe028d719c45ca91d2c23e45a441ba65897f804",ut=async({suiClient:s=new client.SuiClient({url:client.getFullnodeUrl("mainnet")}),tableId:t=ct})=>{let e=[],r=[],o=null,i=true;for(;i;){let{data:n,nextCursor:l,hasNextPage:p}=await s.getDynamicFields({parentId:t,cursor:o,limit:50});if(r.push(...n.map(u=>u.objectId)),n.length===0)break;o=l,i=p;}let a=await s.multiGetObjects({ids:r,options:{showContent:true}});for(let n of a){let l=n.data?.content;if(!l||l.dataType!=="moveObject")continue;let p=l.fields;e.push({address:p.value,type:`0x${p.name?.fields?.name}`});}return e.reduce((n,l)=>{let{name:p}=utils.parseStructTag(l.type);return n[p.replace(/_/g,"")]=l,n},{})};exports.DeepBookMarginPool=I;exports.DeepBookMarginToolkit=A;exports.getOnChainMarginPools=ut;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { DeepBookConfig, MarginPoolContract, Coin } from '@mysten/deepbook-v3';
1
+ import { DeepBookConfig, MarginPoolContract, Coin, MarginPool } from '@mysten/deepbook-v3';
2
2
  import { SuiClient } from '@mysten/sui/client';
3
3
  import { Transaction } from '@mysten/sui/transactions';
4
4
 
@@ -89,4 +89,9 @@ declare class DeepBookMarginPool {
89
89
  getPoolsParameters(coinKeys: string[], supplierCapId: string | undefined, tx: Transaction, inspect: false): Promise<Transaction>;
90
90
  }
91
91
 
92
- export { DeepBookMarginPool, DeepBookMarginToolkit, type MarginBalance, type MarginCoinType, type MarginPoolParams, type NetworkType, type ReferralInfo, type ToolkitConfig, type TransactionResult };
92
+ declare const getOnChainMarginPools: ({ suiClient, tableId, }: {
93
+ suiClient?: SuiClient;
94
+ tableId?: string;
95
+ }) => Promise<Record<string, MarginPool>>;
96
+
97
+ export { DeepBookMarginPool, DeepBookMarginToolkit, type MarginBalance, type MarginCoinType, type MarginPoolParams, type NetworkType, type ReferralInfo, type ToolkitConfig, type TransactionResult, getOnChainMarginPools };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { DeepBookConfig, MarginPoolContract, Coin } from '@mysten/deepbook-v3';
1
+ import { DeepBookConfig, MarginPoolContract, Coin, MarginPool } from '@mysten/deepbook-v3';
2
2
  import { SuiClient } from '@mysten/sui/client';
3
3
  import { Transaction } from '@mysten/sui/transactions';
4
4
 
@@ -89,4 +89,9 @@ declare class DeepBookMarginPool {
89
89
  getPoolsParameters(coinKeys: string[], supplierCapId: string | undefined, tx: Transaction, inspect: false): Promise<Transaction>;
90
90
  }
91
91
 
92
- export { DeepBookMarginPool, DeepBookMarginToolkit, type MarginBalance, type MarginCoinType, type MarginPoolParams, type NetworkType, type ReferralInfo, type ToolkitConfig, type TransactionResult };
92
+ declare const getOnChainMarginPools: ({ suiClient, tableId, }: {
93
+ suiClient?: SuiClient;
94
+ tableId?: string;
95
+ }) => Promise<Record<string, MarginPool>>;
96
+
97
+ export { DeepBookMarginPool, DeepBookMarginToolkit, type MarginBalance, type MarginCoinType, type MarginPoolParams, type NetworkType, type ReferralInfo, type ToolkitConfig, type TransactionResult, getOnChainMarginPools };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import {getFullnodeUrl,SuiClient}from'@mysten/sui/client';import {Ed25519Keypair}from'@mysten/sui/keypairs/ed25519';import {Transaction}from'@mysten/sui/transactions';import {DeepBookConfig,MarginPoolContract,FLOAT_SCALAR}from'@mysten/deepbook-v3';import {SUI_PRIVATE_KEY_PREFIX,decodeSuiPrivateKey,LEGACY_PRIVATE_KEY_SIZE,PRIVATE_KEY_SIZE}from'@mysten/sui/cryptography';import {fromHex,fromBase64}from'@mysten/bcs';import {bcs}from'@mysten/sui/bcs';import {BigNumber}from'bignumber.js';var H=s=>/^0x[0-9a-fA-F]+$|^[0-9a-fA-F]+$/.test(s),V=s=>/^[a-zA-Z0-9+/]+={0,2}$/g.test(s),B=s=>{if(H(s))return fromHex(s);if(V(s))return fromBase64(s);throw new Error("The string is not a valid hex or base64 string.")},S=s=>{if(s.length===LEGACY_PRIVATE_KEY_SIZE)return s.slice(0,PRIVATE_KEY_SIZE);if(s.length===PRIVATE_KEY_SIZE+1&&s[0]===0)return s.slice(1);if(s.length===PRIVATE_KEY_SIZE)return s;throw new Error("invalid secret key")};var A=class{suiClient;keypair;address;marginPoolContract;supplierCapId;dbConfig;supplierCapPackageId;constructor({network:r,fullnodeUrl:e,supplierCapId:t,privateKey:o,supplierCapPackageId:i,dbConfig:n}){let a=e??getFullnodeUrl(r);this.suiClient=new SuiClient({url:a}),this.keypair=this.#r(o),this.address=this.keypair.getPublicKey().toSuiAddress(),this.supplierCapId=t,this.dbConfig=n??new DeepBookConfig({network:r,address:this.address}),this.marginPoolContract=new MarginPoolContract(this.dbConfig),this.supplierCapPackageId=i??this.dbConfig.MARGIN_PACKAGE_ID;}#r(r){if(r.startsWith(SUI_PRIVATE_KEY_PREFIX)){let{secretKey:e}=decodeSuiPrivateKey(r);return Ed25519Keypair.fromSecretKey(S(e))}return Ed25519Keypair.fromSecretKey(S(B(r)))}async#t(){let r=`${this.dbConfig.MARGIN_PACKAGE_ID}::margin_pool::SupplierCap`;return (await this.suiClient.getOwnedObjects({owner:this.address,filter:{StructType:r},options:{showType:true}})).data?.[0]?.data?.objectId}async initialize(){if(this.supplierCapId)return this.supplierCapId;let r=await this.#t();if(r)return this.supplierCapId=r,r;let e=await this.createSupplierCap();if(!e)throw new Error("Failed to create Supplier Cap");return this.supplierCapId=e,e}async createSupplierCap(){try{let r=new Transaction;r.setSender(this.address);let e=r.moveCall({target:`${this.supplierCapPackageId}::margin_pool::mint_supplier_cap`,arguments:[r.object(this.dbConfig.MARGIN_REGISTRY_ID),r.object.clock()]});r.transferObjects([e],r.pure.address(this.address));let t=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:r,options:{showEffects:!0,showObjectChanges:!0}});if(t.errors&&t.errors.length>0)throw new Error(`Transaction failed with errors: ${t.errors.map(o=>o.toString()).join(", ")}`);if(t.objectChanges){for(let o of t.objectChanges)if(o.type==="created"&&o.objectType.includes("SupplierCap"))return o.objectId}return null}catch(r){throw new Error(`Failed to create Supplier Cap: ${r.message||r}`)}}async createSupplyReferral(r){try{let e=new Transaction;e.setSender(this.address);let t=this.dbConfig.getMarginPool(r);if(!t)throw new Error(`Margin pool configuration not found for coin: ${r}`);e.moveCall({target:`${this.dbConfig.MARGIN_PACKAGE_ID}::margin_pool::mint_supply_referral`,arguments:[e.object(t.address),e.object(this.dbConfig.MARGIN_REGISTRY_ID),e.object.clock()],typeArguments:[t.type]});let o=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:e,options:{showEffects:!0,showObjectChanges:!0}});if(o.errors&&o.errors.length>0)throw new Error(`Transaction failed with errors: ${o.errors.map(i=>i.toString()).join(", ")}`);if(o.objectChanges){for(let i of o.objectChanges)if(i.type==="created"&&i.objectType.includes("SupplyReferral"))return i.objectId}return null}catch(e){throw new Error(`Failed to create Supply Referral: ${e.message||e}`)}}async supplyToMarginPool(r,e,t){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let o=new Transaction;o.setSender(this.address);let i=o.object(this.supplierCapId);o.add(this.marginPoolContract.supplyToMarginPool(r,i,e,t));let{errors:n}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:o,options:{showEffects:!0,showObjectChanges:!0}});if(n&&n.length>0)throw new Error(`Transaction failed with errors: ${n.map(a=>a.toString()).join(", ")}`);return !0}catch(o){throw new Error(`Failed to supply to margin pool: ${o.message||o}`)}}async withdrawFromMarginPool(r,e){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let t=new Transaction,o=t.object(this.supplierCapId),n=this.marginPoolContract.withdrawFromMarginPool(r,o,e)(t);t.transferObjects([n],this.address);let{errors:a}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:t,options:{showEffects:!0,showObjectChanges:!0}});if(a&&a.length>0)throw new Error(`Transaction failed with errors: ${a.map(p=>p.toString()).join(", ")}`);return !0}catch(t){throw new Error(`Failed to withdraw from margin pool: ${t.message||t}`)}}async withdrawReferralFees(r,e){try{let t=new Transaction;t.add(this.marginPoolContract.withdrawReferralFees(r,e));let{errors:o}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:t,options:{showEffects:!0,showObjectChanges:!0,showBalanceChanges:!0}});if(o&&o.length>0)throw new Error(`Transaction failed with errors: ${o.map(i=>i.toString()).join(", ")}`);return !0}catch(t){throw new Error(`Failed to withdraw referral fees: ${t.message||t}`)}}async getBalance(r){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let e=new Transaction;e.add(this.marginPoolContract.userSupplyAmount(r,this.supplierCapId));let t=await this.suiClient.devInspectTransactionBlock({sender:this.address,transactionBlock:e}),o=0;if(t&&t.results&&t.results[0]&&t.results[0].returnValues){let c=t.results[0].returnValues[0];if(c&&c[0]){let u=Buffer.from(c[0]).readBigUInt64LE(),g=this.dbConfig.getCoin(r).scalar;o=Number(u)/g;}}let i=this.dbConfig.getCoin(r).type,n=await this.suiClient.getBalance({owner:this.address,coinType:i}),a=this.dbConfig.getCoin(r).scalar,p=Number(n.totalBalance)/a;return {userSupplyAmount:o,walletBalance:p}}catch(e){throw new Error(`Failed to get balance: ${e.message||e}`)}}getSupplierCapId(){return this.supplierCapId}getAddress(){return this.address}};var C=["supplyCap","maxUtilizationRate","protocolSpread","minBorrow","interestRate","totalSupply","supplyShares","totalBorrow","borrowShares","lastUpdateTimestamp"],P=["userSupplyShares","userSupplyAmount"],_={supplyCap:"U64",maxUtilizationRate:"U64",protocolSpread:"U64",minBorrow:"U64",interestRate:"U64",totalSupply:"U64",supplyShares:"U64",totalBorrow:"U64",borrowShares:"U64",lastUpdateTimestamp:"U64",userSupplyShares:"U64",userSupplyAmount:"U64"};var f=(s,r)=>s*r/BigInt(FLOAT_SCALAR),d=s=>Number(s)/FLOAT_SCALAR;var nr=new Set(P),ar=s=>nr.has(s),R=class{marginPoolContract;dbConfig;suiClient;constructor({network:r,address:e="",suiClient:t,dbConfig:o}={}){let i=r??o?.network??"mainnet";if(this.dbConfig=o??new DeepBookConfig({network:i,address:e}),this.suiClient=t??new SuiClient({url:getFullnodeUrl(i)}),this.marginPoolContract=new MarginPoolContract(this.dbConfig),r!==void 0&&o!==void 0&&r!==o.network)throw new Error(`Mismatch between provided network (${r}) and dbConfig network (${o.network}).`)}get network(){return this.dbConfig.network}#r(r,e,t,o){if(ar(e)){let i=this.marginPoolContract[e];if(o==null)throw new Error(`supplierCap is required for '${e}'.`);r.add(i(t,o));}else {let i=this.marginPoolContract[e];r.add(i(t));}}parseInspectResultToBcsStructs(r,e){let t=r.results;if(!t)throw new Error("No results found in DevInspect output.");return e.reduce((o,i,n)=>{let a=t[n]?.returnValues?.[0]?.[0];if(!a)return o;let p=bcs[_[i]];return o[i]=p.parse(new Uint8Array(a)),o},{})}formatResult(r,e){let t=this.dbConfig.getCoin(e),o={supplyCap:0,maxUtilizationRate:0,protocolSpread:0,minBorrow:0,interestRate:0,totalSupply:0,supplyShares:0,totalBorrow:0,borrowShares:0,lastUpdateTimestamp:0,userSupplyShares:0,userSupplyAmount:0,decimals:t.scalar.toString().length-1,highKink:0,baseBorrowApr:0,borrowAprOnHighKink:0,maxBorrowApr:0,supplyApr:0,utilizationRate:0,...t};if(!t)return o;let i=new Set(["interestRate","maxUtilizationRate","protocolSpread"]);for(let[n,a]of Object.entries(r))n==="lastUpdateTimestamp"?o[n]=Number(a):i.has(n)?o[n]=new BigNumber(a).dividedBy(FLOAT_SCALAR).toNumber():o[n]=new BigNumber(a).dividedBy(t.scalar).toNumber();return o}computeBorrowAprAtUtil(r,e){let t=BigInt(e.base_rate),o=BigInt(e.base_slope),i=BigInt(e.excess_slope),n=BigInt(e.optimal_utilization);return r<n?t+f(r,o):t+f(n,o)+f(r-n,i)}calculateKinksAndRate(r,e,t,o){let i=BigInt(r.optimal_utilization),n=BigInt(e.max_utilization_rate),a=this.computeBorrowAprAtUtil(i,r),p=this.computeBorrowAprAtUtil(n,r),c=BigInt(BigNumber(t.total_borrow).dividedBy(t.total_supply).shiftedBy(9).decimalPlaces(0).toString()),u=f(f(o,c),BigInt(FLOAT_SCALAR)-BigInt(e.protocol_spread));return {raw:{baseBorrowApr:r.base_rate,highKink:i,borrowAprOnHighKink:a,maxBorrowApr:p,supplyApr:u,utilizationRate:c},normalized:{baseBorrowApr:d(BigInt(r.base_rate)),highKink:d(i),borrowAprOnHighKink:d(a),maxBorrowApr:d(p),supplyApr:d(u),utilizationRate:d(c)}}}async#t(r,e,t){let{address:o}=this.dbConfig.getMarginPool(r);t??=await this.suiClient.getObject({id:o,options:{showContent:true}});let i=(t.data?.content).fields,n=i.config.fields,a=n.interest_config.fields,p=n.margin_pool_config.fields,c=i.state.fields,{normalized:u}=this.calculateKinksAndRate(a,p,c,e);return u}async getPoolParameters(r,e,t=new Transaction,o=true){if(C.forEach(g=>this.#r(t,g,r)),e&&P.forEach(g=>this.#r(t,g,r,e)),!o)return t;let i=[...C,...P],n=await this.suiClient.devInspectTransactionBlock({transactionBlock:t,sender:this.dbConfig.address}),a=this.parseInspectResultToBcsStructs(n,i),p=this.formatResult(a,r),c=BigInt(a.interestRate??0),u=await this.#t(r,c);return {...p,...u}}async getPoolsParameters(r,e,t=new Transaction,o=true){for(let l of r)C.forEach(m=>this.#r(t,m,l)),e&&P.forEach(m=>this.#r(t,m,l,e));if(!o)return t;let i=[...C,...e?P:[]],n=i.length,a=r.map(l=>this.dbConfig.getMarginPool(l).address),p=50,c=[];for(let l=0;l<a.length;l+=p)c.push(a.slice(l,l+p));let[u,...g]=await Promise.all([this.suiClient.devInspectTransactionBlock({transactionBlock:t,sender:this.dbConfig.address}),...c.map(l=>this.suiClient.multiGetObjects({ids:l,options:{showContent:true}}))]),v=g.flat(),I=u.results;if(!I)throw new Error("No results found in DevInspect output.");return Promise.all(r.map(async(l,m)=>{let N=m*n,T=i.reduce((y,E,W)=>{let K=I[N+W]?.returnValues?.[0]?.[0];if(!K)return y;let $=bcs[_[E]];return y[E]=$.parse(new Uint8Array(K)),y},{}),z=this.formatResult(T,l),G=BigInt(T.interestRate??0),w=v[m];if(!w||w.error)throw new Error(`Failed to fetch interest config for ${l}`);let F=await this.#t(l,G,w);return {...z,...F}}))}};export{R as DeepBookMarginPool,A as DeepBookMarginToolkit};
1
+ import {getFullnodeUrl,SuiClient}from'@mysten/sui/client';import {Ed25519Keypair}from'@mysten/sui/keypairs/ed25519';import {Transaction}from'@mysten/sui/transactions';import {DeepBookConfig,MarginPoolContract,FLOAT_SCALAR}from'@mysten/deepbook-v3';import {SUI_PRIVATE_KEY_PREFIX,decodeSuiPrivateKey,LEGACY_PRIVATE_KEY_SIZE,PRIVATE_KEY_SIZE}from'@mysten/sui/cryptography';import {fromHex,fromBase64}from'@mysten/bcs';import {bcs}from'@mysten/sui/bcs';import {BigNumber}from'bignumber.js';import {parseStructTag}from'@mysten/sui/utils';var H=s=>/^0x[0-9a-fA-F]+$|^[0-9a-fA-F]+$/.test(s),V=s=>/^[a-zA-Z0-9+/]+={0,2}$/g.test(s),B=s=>{if(H(s))return fromHex(s);if(V(s))return fromBase64(s);throw new Error("The string is not a valid hex or base64 string.")},S=s=>{if(s.length===LEGACY_PRIVATE_KEY_SIZE)return s.slice(0,PRIVATE_KEY_SIZE);if(s.length===PRIVATE_KEY_SIZE+1&&s[0]===0)return s.slice(1);if(s.length===PRIVATE_KEY_SIZE)return s;throw new Error("invalid secret key")};var A=class{suiClient;keypair;address;marginPoolContract;supplierCapId;dbConfig;supplierCapPackageId;constructor({network:t,fullnodeUrl:e,supplierCapId:r,privateKey:o,supplierCapPackageId:i,dbConfig:a}){let n=e??getFullnodeUrl(t);this.suiClient=new SuiClient({url:n}),this.keypair=this.#t(o),this.address=this.keypair.getPublicKey().toSuiAddress(),this.supplierCapId=r,this.dbConfig=a??new DeepBookConfig({network:t,address:this.address}),this.marginPoolContract=new MarginPoolContract(this.dbConfig),this.supplierCapPackageId=i??this.dbConfig.MARGIN_PACKAGE_ID;}#t(t){if(t.startsWith(SUI_PRIVATE_KEY_PREFIX)){let{secretKey:e}=decodeSuiPrivateKey(t);return Ed25519Keypair.fromSecretKey(S(e))}return Ed25519Keypair.fromSecretKey(S(B(t)))}async#r(){let t=`${this.dbConfig.MARGIN_PACKAGE_ID}::margin_pool::SupplierCap`;return (await this.suiClient.getOwnedObjects({owner:this.address,filter:{StructType:t},options:{showType:true}})).data?.[0]?.data?.objectId}async initialize(){if(this.supplierCapId)return this.supplierCapId;let t=await this.#r();if(t)return this.supplierCapId=t,t;let e=await this.createSupplierCap();if(!e)throw new Error("Failed to create Supplier Cap");return this.supplierCapId=e,e}async createSupplierCap(){try{let t=new Transaction;t.setSender(this.address);let e=t.moveCall({target:`${this.supplierCapPackageId}::margin_pool::mint_supplier_cap`,arguments:[t.object(this.dbConfig.MARGIN_REGISTRY_ID),t.object.clock()]});t.transferObjects([e],t.pure.address(this.address));let r=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:t,options:{showEffects:!0,showObjectChanges:!0}});if(r.errors&&r.errors.length>0)throw new Error(`Transaction failed with errors: ${r.errors.map(o=>o.toString()).join(", ")}`);if(r.objectChanges){for(let o of r.objectChanges)if(o.type==="created"&&o.objectType.includes("SupplierCap"))return o.objectId}return null}catch(t){throw new Error(`Failed to create Supplier Cap: ${t.message||t}`)}}async createSupplyReferral(t){try{let e=new Transaction;e.setSender(this.address);let r=this.dbConfig.getMarginPool(t);if(!r)throw new Error(`Margin pool configuration not found for coin: ${t}`);e.moveCall({target:`${this.dbConfig.MARGIN_PACKAGE_ID}::margin_pool::mint_supply_referral`,arguments:[e.object(r.address),e.object(this.dbConfig.MARGIN_REGISTRY_ID),e.object.clock()],typeArguments:[r.type]});let o=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:e,options:{showEffects:!0,showObjectChanges:!0}});if(o.errors&&o.errors.length>0)throw new Error(`Transaction failed with errors: ${o.errors.map(i=>i.toString()).join(", ")}`);if(o.objectChanges){for(let i of o.objectChanges)if(i.type==="created"&&i.objectType.includes("SupplyReferral"))return i.objectId}return null}catch(e){throw new Error(`Failed to create Supply Referral: ${e.message||e}`)}}async supplyToMarginPool(t,e,r){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let o=new Transaction;o.setSender(this.address);let i=o.object(this.supplierCapId);o.add(this.marginPoolContract.supplyToMarginPool(t,i,e,r));let{errors:a}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:o,options:{showEffects:!0,showObjectChanges:!0}});if(a&&a.length>0)throw new Error(`Transaction failed with errors: ${a.map(n=>n.toString()).join(", ")}`);return !0}catch(o){throw new Error(`Failed to supply to margin pool: ${o.message||o}`)}}async withdrawFromMarginPool(t,e){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let r=new Transaction,o=r.object(this.supplierCapId),a=this.marginPoolContract.withdrawFromMarginPool(t,o,e)(r);r.transferObjects([a],this.address);let{errors:n}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:r,options:{showEffects:!0,showObjectChanges:!0}});if(n&&n.length>0)throw new Error(`Transaction failed with errors: ${n.map(l=>l.toString()).join(", ")}`);return !0}catch(r){throw new Error(`Failed to withdraw from margin pool: ${r.message||r}`)}}async withdrawReferralFees(t,e){try{let r=new Transaction;r.add(this.marginPoolContract.withdrawReferralFees(t,e));let{errors:o}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:r,options:{showEffects:!0,showObjectChanges:!0,showBalanceChanges:!0}});if(o&&o.length>0)throw new Error(`Transaction failed with errors: ${o.map(i=>i.toString()).join(", ")}`);return !0}catch(r){throw new Error(`Failed to withdraw referral fees: ${r.message||r}`)}}async getBalance(t){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let e=new Transaction;e.add(this.marginPoolContract.userSupplyAmount(t,this.supplierCapId));let r=await this.suiClient.devInspectTransactionBlock({sender:this.address,transactionBlock:e}),o=0;if(r&&r.results&&r.results[0]&&r.results[0].returnValues){let p=r.results[0].returnValues[0];if(p&&p[0]){let u=Buffer.from(p[0]).readBigUInt64LE(),g=this.dbConfig.getCoin(t).scalar;o=Number(u)/g;}}let i=this.dbConfig.getCoin(t).type,a=await this.suiClient.getBalance({owner:this.address,coinType:i}),n=this.dbConfig.getCoin(t).scalar,l=Number(a.totalBalance)/n;return {userSupplyAmount:o,walletBalance:l}}catch(e){throw new Error(`Failed to get balance: ${e.message||e}`)}}getSupplierCapId(){return this.supplierCapId}getAddress(){return this.address}};var C=["supplyCap","maxUtilizationRate","protocolSpread","minBorrow","interestRate","totalSupply","supplyShares","totalBorrow","borrowShares","lastUpdateTimestamp"],f=["userSupplyShares","userSupplyAmount"],_={supplyCap:"U64",maxUtilizationRate:"U64",protocolSpread:"U64",minBorrow:"U64",interestRate:"U64",totalSupply:"U64",supplyShares:"U64",totalBorrow:"U64",borrowShares:"U64",lastUpdateTimestamp:"U64",userSupplyShares:"U64",userSupplyAmount:"U64"};var P=(s,t)=>s*t/BigInt(FLOAT_SCALAR),d=s=>Number(s)/FLOAT_SCALAR;var nt=new Set(f),at=s=>nt.has(s),I=class{marginPoolContract;dbConfig;suiClient;constructor({network:t,address:e="",suiClient:r,dbConfig:o}={}){let i=t??o?.network??"mainnet";if(this.dbConfig=o??new DeepBookConfig({network:i,address:e}),this.suiClient=r??new SuiClient({url:getFullnodeUrl(i)}),this.marginPoolContract=new MarginPoolContract(this.dbConfig),t!==void 0&&o!==void 0&&t!==o.network)throw new Error(`Mismatch between provided network (${t}) and dbConfig network (${o.network}).`)}get network(){return this.dbConfig.network}#t(t,e,r,o){if(at(e)){let i=this.marginPoolContract[e];if(o==null)throw new Error(`supplierCap is required for '${e}'.`);t.add(i(r,o));}else {let i=this.marginPoolContract[e];t.add(i(r));}}parseInspectResultToBcsStructs(t,e){let r=t.results;if(!r)throw new Error("No results found in DevInspect output.");return e.reduce((o,i,a)=>{let n=r[a]?.returnValues?.[0]?.[0];if(!n)return o;let l=bcs[_[i]];return o[i]=l.parse(new Uint8Array(n)),o},{})}formatResult(t,e){let r=this.dbConfig.getCoin(e),o={supplyCap:0,maxUtilizationRate:0,protocolSpread:0,minBorrow:0,interestRate:0,totalSupply:0,supplyShares:0,totalBorrow:0,borrowShares:0,lastUpdateTimestamp:0,userSupplyShares:0,userSupplyAmount:0,decimals:r.scalar.toString().length-1,highKink:0,baseBorrowApr:0,borrowAprOnHighKink:0,maxBorrowApr:0,supplyApr:0,utilizationRate:0,...r};if(!r)return o;let i=new Set(["interestRate","maxUtilizationRate","protocolSpread"]);for(let[a,n]of Object.entries(t))a==="lastUpdateTimestamp"?o[a]=Number(n):i.has(a)?o[a]=new BigNumber(n).dividedBy(FLOAT_SCALAR).toNumber():o[a]=new BigNumber(n).dividedBy(r.scalar).toNumber();return o}computeBorrowAprAtUtil(t,e){let r=BigInt(e.base_rate),o=BigInt(e.base_slope),i=BigInt(e.excess_slope),a=BigInt(e.optimal_utilization);return t<a?r+P(t,o):r+P(a,o)+P(t-a,i)}calculateKinksAndRate(t,e,r,o){let i=BigInt(t.optimal_utilization),a=BigInt(e.max_utilization_rate),n=this.computeBorrowAprAtUtil(i,t),l=this.computeBorrowAprAtUtil(a,t),p=BigInt(BigNumber(r.total_borrow).dividedBy(r.total_supply).shiftedBy(9).decimalPlaces(0).toString()),u=P(P(o,p),BigInt(FLOAT_SCALAR)-BigInt(e.protocol_spread));return {raw:{baseBorrowApr:t.base_rate,highKink:i,borrowAprOnHighKink:n,maxBorrowApr:l,supplyApr:u,utilizationRate:p},normalized:{baseBorrowApr:d(BigInt(t.base_rate)),highKink:d(i),borrowAprOnHighKink:d(n),maxBorrowApr:d(l),supplyApr:d(u),utilizationRate:d(p)}}}async#r(t,e,r){let{address:o}=this.dbConfig.getMarginPool(t);r??=await this.suiClient.getObject({id:o,options:{showContent:true}});let i=(r.data?.content).fields,a=i.config.fields,n=a.interest_config.fields,l=a.margin_pool_config.fields,p=i.state.fields,{normalized:u}=this.calculateKinksAndRate(n,l,p,e);return u}async getPoolParameters(t,e,r=new Transaction,o=true){if(C.forEach(g=>this.#t(r,g,t)),e&&f.forEach(g=>this.#t(r,g,t,e)),!o)return r;let i=[...C,...f],a=await this.suiClient.devInspectTransactionBlock({transactionBlock:r,sender:this.dbConfig.address}),n=this.parseInspectResultToBcsStructs(a,i),l=this.formatResult(n,t),p=BigInt(n.interestRate??0),u=await this.#r(t,p);return {...l,...u}}async getPoolsParameters(t,e,r=new Transaction,o=true){for(let c of t)C.forEach(m=>this.#t(r,m,c)),e&&f.forEach(m=>this.#t(r,m,c,e));if(!o)return r;let i=[...C,...e?f:[]],a=i.length,n=t.map(c=>this.dbConfig.getMarginPool(c).address),l=50,p=[];for(let c=0;c<n.length;c+=l)p.push(n.slice(c,c+l));let[u,...g]=await Promise.all([this.suiClient.devInspectTransactionBlock({transactionBlock:r,sender:this.dbConfig.address}),...p.map(c=>this.suiClient.multiGetObjects({ids:c,options:{showContent:true}}))]),v=g.flat(),R=u.results;if(!R)throw new Error("No results found in DevInspect output.");return Promise.all(t.map(async(c,m)=>{let N=m*a,T=i.reduce((y,E,L)=>{let K=R[N+L]?.returnValues?.[0]?.[0];if(!K)return y;let $=bcs[_[E]];return y[E]=$.parse(new Uint8Array(K)),y},{}),z=this.formatResult(T,c),G=BigInt(T.interestRate??0),w=v[m];if(!w||w.error)throw new Error(`Failed to fetch interest config for ${c}`);let F=await this.#r(c,G,w);return {...z,...F}}))}};var ct="0x7f7351ef7e5089dfddf17f55abe028d719c45ca91d2c23e45a441ba65897f804",ut=async({suiClient:s=new SuiClient({url:getFullnodeUrl("mainnet")}),tableId:t=ct})=>{let e=[],r=[],o=null,i=true;for(;i;){let{data:n,nextCursor:l,hasNextPage:p}=await s.getDynamicFields({parentId:t,cursor:o,limit:50});if(r.push(...n.map(u=>u.objectId)),n.length===0)break;o=l,i=p;}let a=await s.multiGetObjects({ids:r,options:{showContent:true}});for(let n of a){let l=n.data?.content;if(!l||l.dataType!=="moveObject")continue;let p=l.fields;e.push({address:p.value,type:`0x${p.name?.fields?.name}`});}return e.reduce((n,l)=>{let{name:p}=parseStructTag(l.type);return n[p.replace(/_/g,"")]=l,n},{})};export{I as DeepBookMarginPool,A as DeepBookMarginToolkit,ut as getOnChainMarginPools};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@scallop-io/scallop-deepbook-kit",
4
- "version": "0.3.0",
4
+ "version": "0.4.1",
5
5
  "description": "A toolkit for integrating Scallop with DeepBook functionality",
6
6
  "keywords": [
7
7
  "scallop",
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './toolkit';
2
+ export * from './queries';
@@ -0,0 +1,65 @@
1
+ import { MarginPool } from '@mysten/deepbook-v3';
2
+ import { getFullnodeUrl, SuiClient } from '@mysten/sui/client';
3
+ import { parseStructTag } from '@mysten/sui/utils';
4
+
5
+ const MARGIN_POOLS_TABLE_ID = '0x7f7351ef7e5089dfddf17f55abe028d719c45ca91d2c23e45a441ba65897f804';
6
+
7
+ /**
8
+ * Fetch all margin pool addresses and their types from the on-chain dynamic fields table.
9
+ * @returns Record<coinKey, { address: string; type: string }>
10
+ */
11
+ export const getOnChainMarginPools = async ({
12
+ suiClient = new SuiClient({
13
+ url: getFullnodeUrl('mainnet'),
14
+ }),
15
+ tableId = MARGIN_POOLS_TABLE_ID,
16
+ }: {
17
+ suiClient?: SuiClient;
18
+ tableId?: string;
19
+ }) => {
20
+ const marginPools: Array<{ address: string; type: string }> = [];
21
+ const ids: string[] = [];
22
+
23
+ let cursor: string | null = null;
24
+ let nextPage = true;
25
+
26
+ while (nextPage) {
27
+ const { data, nextCursor, hasNextPage } = await suiClient.getDynamicFields({
28
+ parentId: tableId,
29
+ cursor,
30
+ limit: 50,
31
+ });
32
+
33
+ ids.push(...data.map((item) => item.objectId));
34
+
35
+ if (data.length === 0) {
36
+ break;
37
+ }
38
+
39
+ cursor = nextCursor;
40
+ nextPage = hasNextPage;
41
+ }
42
+
43
+ // Fetch all the object ids
44
+ const objects = await suiClient.multiGetObjects({ ids, options: { showContent: true } });
45
+
46
+ for (const obj of objects) {
47
+ const content = obj.data?.content;
48
+ if (!content || content.dataType !== 'moveObject') continue;
49
+ const fields = content.fields as Record<string, any>;
50
+ marginPools.push({
51
+ address: fields.value,
52
+ type: `0x${fields.name?.fields?.name}`,
53
+ });
54
+ }
55
+
56
+ return marginPools.reduce(
57
+ (acc, pool) => {
58
+ // Get symbol from type
59
+ const { name } = parseStructTag(pool.type);
60
+ acc[name.replace(/_/g, '')] = pool;
61
+ return acc;
62
+ },
63
+ {} as Record<string, MarginPool>
64
+ );
65
+ };
@@ -0,0 +1 @@
1
+ export { getOnChainMarginPools } from './getOnChainMarginPools';
@@ -92,7 +92,12 @@ export class DeepBookMarginPool {
92
92
  // If dbConfig is provided and network is not, derive network from dbConfig
93
93
  const resolvedNetwork = network ?? dbConfig?.network ?? 'mainnet';
94
94
 
95
- this.dbConfig = dbConfig ?? new DeepBookConfig({ network: resolvedNetwork, address });
95
+ this.dbConfig =
96
+ dbConfig ??
97
+ new DeepBookConfig({
98
+ network: resolvedNetwork,
99
+ address,
100
+ });
96
101
  this.suiClient = suiClient ?? new SuiClient({ url: getFullnodeUrl(resolvedNetwork) });
97
102
 
98
103
  // Initialize smart contract wrapper