@scallop-io/scallop-deepbook-kit 0.4.1 → 0.4.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.
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'),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;
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.")},A=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 _=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(A(e))}return ed25519.Ed25519Keypair.fromSecretKey(A(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"],M={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[M[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=bignumber_js.BigNumber(r.total_supply).isZero()?0n: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((b,E,L)=>{let K=R[N+L]?.returnValues?.[0]?.[0];if(!K)return b;let $=bcs.bcs[M[E]];return b[E]=$.parse(new Uint8Array(K)),b},{}),z=this.formatResult(T,c),G=BigInt(T.interestRate??0),y=v[m];if(!y||y.error)throw new Error(`Failed to fetch interest config for ${c}`);let F=await this.#r(c,G,y);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=_;exports.getOnChainMarginPools=ut;
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';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};
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.")},A=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 _=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(A(e))}return Ed25519Keypair.fromSecretKey(A(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"],M={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[M[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=BigNumber(r.total_supply).isZero()?0n: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((b,E,L)=>{let K=R[N+L]?.returnValues?.[0]?.[0];if(!K)return b;let $=bcs[M[E]];return b[E]=$.parse(new Uint8Array(K)),b},{}),z=this.formatResult(T,c),G=BigInt(T.interestRate??0),y=v[m];if(!y||y.error)throw new Error(`Failed to fetch interest config for ${c}`);let F=await this.#r(c,G,y);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,_ 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.4.1",
4
+ "version": "0.4.2",
5
5
  "description": "A toolkit for integrating Scallop with DeepBook functionality",
6
6
  "keywords": [
7
7
  "scallop",
@@ -281,13 +281,15 @@ export class DeepBookMarginPool {
281
281
  const borrowAprOnHighKink = this.computeBorrowAprAtUtil(highKink, interestConfig);
282
282
  const maxBorrowApr = this.computeBorrowAprAtUtil(maxKink, interestConfig);
283
283
 
284
- const utilizationRate = BigInt(
285
- BigNumber(state.total_borrow)
286
- .dividedBy(state.total_supply)
287
- .shiftedBy(9)
288
- .decimalPlaces(0)
289
- .toString()
290
- );
284
+ const utilizationRate = BigNumber(state.total_supply).isZero()
285
+ ? 0n
286
+ : BigInt(
287
+ BigNumber(state.total_borrow)
288
+ .dividedBy(state.total_supply)
289
+ .shiftedBy(9)
290
+ .decimalPlaces(0)
291
+ .toString()
292
+ );
291
293
 
292
294
  const supplyApr = mul(
293
295
  mul(borrowAprScaled, utilizationRate),
@@ -496,6 +498,7 @@ export class DeepBookMarginPool {
496
498
  })
497
499
  ),
498
500
  ]);
501
+
499
502
  const interestConfigs = objectBatches.flat();
500
503
 
501
504
  const allResults = inspectResult.results;