@scallop-io/scallop-deepbook-kit 0.2.0 → 0.3.0
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 +1 -1
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -1
- package/package.json +3 -3
- package/src/examples/margin-pool-demo.ts +15 -6
- package/src/toolkit/DeepBookMarginPool.ts +129 -4
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 T=n=>/^0x[0-9a-fA-F]+$|^[0-9a-fA-F]+$/.test(n),B=n=>/^[a-zA-Z0-9+/]+={0,2}$/g.test(n),A=n=>{if(T(n))return bcs$1.fromHex(n);if(B(n))return bcs$1.fromBase64(n);throw new Error("The string is not a valid hex or base64 string.")},P=n=>{if(n.length===cryptography.LEGACY_PRIVATE_KEY_SIZE)return n.slice(0,cryptography.PRIVATE_KEY_SIZE);if(n.length===cryptography.PRIVATE_KEY_SIZE+1&&n[0]===0)return n.slice(1);if(n.length===cryptography.PRIVATE_KEY_SIZE)return n;throw new Error("invalid secret key")};var C=class{suiClient;keypair;address;marginPoolContract;supplierCapId;dbConfig;supplierCapPackageId;constructor({network:r,fullnodeUrl:i,supplierCapId:t,privateKey:e,supplierCapPackageId:o,dbConfig:a}){let s=i??client.getFullnodeUrl(r);this.suiClient=new client.SuiClient({url:s}),this.keypair=this.#r(e),this.address=this.keypair.getPublicKey().toSuiAddress(),this.supplierCapId=t,this.dbConfig=a??new deepbookV3.DeepBookConfig({network:r,address:this.address}),this.marginPoolContract=new deepbookV3.MarginPoolContract(this.dbConfig),this.supplierCapPackageId=o??this.dbConfig.MARGIN_PACKAGE_ID;}#r(r){if(r.startsWith(cryptography.SUI_PRIVATE_KEY_PREFIX)){let{secretKey:i}=cryptography.decodeSuiPrivateKey(r);return ed25519.Ed25519Keypair.fromSecretKey(P(i))}return ed25519.Ed25519Keypair.fromSecretKey(P(A(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 i=await this.createSupplierCap();if(!i)throw new Error("Failed to create Supplier Cap");return this.supplierCapId=i,i}async createSupplierCap(){try{let r=new transactions.Transaction;r.setSender(this.address);let i=r.moveCall({target:`${this.supplierCapPackageId}::margin_pool::mint_supplier_cap`,arguments:[r.object(this.dbConfig.MARGIN_REGISTRY_ID),r.object.clock()]});r.transferObjects([i],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(e=>e.toString()).join(", ")}`);if(t.objectChanges){for(let e of t.objectChanges)if(e.type==="created"&&e.objectType.includes("SupplierCap"))return e.objectId}return null}catch(r){throw new Error(`Failed to create Supplier Cap: ${r.message||r}`)}}async createSupplyReferral(r){try{let i=new transactions.Transaction;i.setSender(this.address);let t=this.dbConfig.getMarginPool(r);if(!t)throw new Error(`Margin pool configuration not found for coin: ${r}`);i.moveCall({target:`${this.dbConfig.MARGIN_PACKAGE_ID}::margin_pool::mint_supply_referral`,arguments:[i.object(t.address),i.object(this.dbConfig.MARGIN_REGISTRY_ID),i.object.clock()],typeArguments:[t.type]});let e=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:i,options:{showEffects:!0,showObjectChanges:!0}});if(e.errors&&e.errors.length>0)throw new Error(`Transaction failed with errors: ${e.errors.map(o=>o.toString()).join(", ")}`);if(e.objectChanges){for(let o of e.objectChanges)if(o.type==="created"&&o.objectType.includes("SupplyReferral"))return o.objectId}return null}catch(i){throw new Error(`Failed to create Supply Referral: ${i.message||i}`)}}async supplyToMarginPool(r,i,t){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let e=new transactions.Transaction;e.setSender(this.address);let o=e.object(this.supplierCapId);e.add(this.marginPoolContract.supplyToMarginPool(r,o,i,t));let{errors:a}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:e,options:{showEffects:!0,showObjectChanges:!0}});if(a&&a.length>0)throw new Error(`Transaction failed with errors: ${a.map(s=>s.toString()).join(", ")}`);return !0}catch(e){throw new Error(`Failed to supply to margin pool: ${e.message||e}`)}}async withdrawFromMarginPool(r,i){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let t=new transactions.Transaction,e=t.object(this.supplierCapId),a=this.marginPoolContract.withdrawFromMarginPool(r,e,i)(t);t.transferObjects([a],this.address);let{errors:s}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:t,options:{showEffects:!0,showObjectChanges:!0}});if(s&&s.length>0)throw new Error(`Transaction failed with errors: ${s.map(p=>p.toString()).join(", ")}`);return !0}catch(t){throw new Error(`Failed to withdraw from margin pool: ${t.message||t}`)}}async withdrawReferralFees(r,i){try{let t=new transactions.Transaction;t.add(this.marginPoolContract.withdrawReferralFees(r,i));let{errors:e}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:t,options:{showEffects:!0,showObjectChanges:!0,showBalanceChanges:!0}});if(e&&e.length>0)throw new Error(`Transaction failed with errors: ${e.map(o=>o.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 i=new transactions.Transaction;i.add(this.marginPoolContract.userSupplyAmount(r,this.supplierCapId));let t=await this.suiClient.devInspectTransactionBlock({sender:this.address,transactionBlock:i}),e=0;if(t&&t.results&&t.results[0]&&t.results[0].returnValues){let l=t.results[0].returnValues[0];if(l&&l[0]){let c=Buffer.from(l[0]).readBigUInt64LE(),m=this.dbConfig.getCoin(r).scalar;e=Number(c)/m;}}let o=this.dbConfig.getCoin(r).type,a=await this.suiClient.getBalance({owner:this.address,coinType:o}),s=this.dbConfig.getCoin(r).scalar,p=Number(a.totalBalance)/s;return {userSupplyAmount:e,walletBalance:p}}catch(i){throw new Error(`Failed to get balance: ${i.message||i}`)}}getSupplierCapId(){return this.supplierCapId}getAddress(){return this.address}};var w=["supplyCap","maxUtilizationRate","protocolSpread","minBorrow","interestRate","totalSupply","supplyShares","totalBorrow","borrowShares","lastUpdateTimestamp"],h=["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 d=(n,r)=>n*r/BigInt(deepbookV3.FLOAT_SCALAR),u=n=>Number(n)/deepbookV3.FLOAT_SCALAR;var W=new Set(h),D=n=>W.has(n),b=class{marginPoolContract;dbConfig;suiClient;constructor({network:r,address:i="",suiClient:t,dbConfig:e}={}){let o=r??e?.network??"mainnet";if(this.dbConfig=e??new deepbookV3.DeepBookConfig({network:o,address:i}),this.suiClient=t??new client.SuiClient({url:client.getFullnodeUrl(o)}),this.marginPoolContract=new deepbookV3.MarginPoolContract(this.dbConfig),r!==void 0&&e!==void 0&&r!==e.network)throw new Error(`Mismatch between provided network (${r}) and dbConfig network (${e.network}).`)}get network(){return this.dbConfig.network}#r(r,i,t,e){if(D(i)){let o=this.marginPoolContract[i];if(e==null)throw new Error(`supplierCap is required for '${i}'.`);r.add(o(t,e));}else {let o=this.marginPoolContract[i];r.add(o(t));}}parseInspectResultToBcsStructs(r,i){let t=r.results;if(!t)throw new Error("No results found in DevInspect output.");return i.reduce((e,o,a)=>{let s=t[a]?.returnValues?.[0]?.[0];if(!s)return e;let p=bcs.bcs[_[o]];return e[o]=p.parse(new Uint8Array(s)),e},{})}formatResult(r,i){let t=this.dbConfig.getCoin(i),e={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 e;let o=new Set(["interestRate","maxUtilizationRate","protocolSpread"]);for(let[a,s]of Object.entries(r))a==="lastUpdateTimestamp"?e[a]=Number(s):o.has(a)?e[a]=new bignumber_js.BigNumber(s).dividedBy(deepbookV3.FLOAT_SCALAR).toNumber():e[a]=new bignumber_js.BigNumber(s).dividedBy(t.scalar).toNumber();return e}computeBorrowAprAtUtil(r,i){let t=BigInt(i.base_rate),e=BigInt(i.base_slope),o=BigInt(i.excess_slope),a=BigInt(i.optimal_utilization);return r<a?t+d(r,e):t+d(a,e)+d(r-a,o)}calculateKinksAndRate(r,i,t,e){let o=BigInt(r.optimal_utilization),a=BigInt(i.max_utilization_rate),s=this.computeBorrowAprAtUtil(o,r),p=this.computeBorrowAprAtUtil(a,r),l=BigInt(bignumber_js.BigNumber(t.total_borrow).dividedBy(t.total_supply).shiftedBy(9).decimalPlaces(0).toString()),c=d(d(e,l),BigInt(deepbookV3.FLOAT_SCALAR)-BigInt(i.protocol_spread));return {raw:{baseBorrowApr:r.base_rate,highKink:o,borrowAprOnHighKink:s,maxBorrowApr:p,supplyApr:c,utilizationRate:l},normalized:{baseBorrowApr:u(BigInt(r.base_rate)),highKink:u(o),borrowAprOnHighKink:u(s),maxBorrowApr:u(p),supplyApr:u(c),utilizationRate:u(l)}}}async#t(r,i){let{address:t}=this.dbConfig.getMarginPool(r),o=((await this.suiClient.getObject({id:t,options:{showContent:true}})).data?.content).fields,a=o.config.fields,s=a.interest_config.fields,p=a.margin_pool_config.fields,l=o.state.fields,{normalized:c}=this.calculateKinksAndRate(s,p,l,i);return c}async getPoolParameters(r,i,t=new transactions.Transaction,e=true){if(w.forEach(m=>this.#r(t,m,r)),i&&h.forEach(m=>this.#r(t,m,r,i)),!e)return t;let o=[...w,...h],a=await this.suiClient.devInspectTransactionBlock({transactionBlock:t,sender:this.dbConfig.address}),s=this.parseInspectResultToBcsStructs(a,o),p=this.formatResult(s,r),l=BigInt(s.interestRate??0),c=await this.#t(r,l);return {...p,...c}}};exports.DeepBookMarginPool=b;exports.DeepBookMarginToolkit=C;
|
|
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;
|
package/dist/index.d.cts
CHANGED
|
@@ -84,6 +84,9 @@ declare class DeepBookMarginPool {
|
|
|
84
84
|
getPoolParameters(coinKey: string, supplierCapId?: string, tx?: Transaction): Promise<MarginPoolParams>;
|
|
85
85
|
getPoolParameters(coinKey: string, supplierCapId: string | undefined, tx: Transaction, inspect: true): Promise<MarginPoolParams>;
|
|
86
86
|
getPoolParameters(coinKey: string, supplierCapId: string | undefined, tx: Transaction, inspect: false): Promise<Transaction>;
|
|
87
|
+
getPoolsParameters(coinKeys: string[], supplierCapId?: string, tx?: Transaction): Promise<MarginPoolParams[]>;
|
|
88
|
+
getPoolsParameters(coinKeys: string[], supplierCapId: string | undefined, tx: Transaction, inspect: true): Promise<MarginPoolParams[]>;
|
|
89
|
+
getPoolsParameters(coinKeys: string[], supplierCapId: string | undefined, tx: Transaction, inspect: false): Promise<Transaction>;
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
export { DeepBookMarginPool, DeepBookMarginToolkit, type MarginBalance, type MarginCoinType, type MarginPoolParams, type NetworkType, type ReferralInfo, type ToolkitConfig, type TransactionResult };
|
package/dist/index.d.ts
CHANGED
|
@@ -84,6 +84,9 @@ declare class DeepBookMarginPool {
|
|
|
84
84
|
getPoolParameters(coinKey: string, supplierCapId?: string, tx?: Transaction): Promise<MarginPoolParams>;
|
|
85
85
|
getPoolParameters(coinKey: string, supplierCapId: string | undefined, tx: Transaction, inspect: true): Promise<MarginPoolParams>;
|
|
86
86
|
getPoolParameters(coinKey: string, supplierCapId: string | undefined, tx: Transaction, inspect: false): Promise<Transaction>;
|
|
87
|
+
getPoolsParameters(coinKeys: string[], supplierCapId?: string, tx?: Transaction): Promise<MarginPoolParams[]>;
|
|
88
|
+
getPoolsParameters(coinKeys: string[], supplierCapId: string | undefined, tx: Transaction, inspect: true): Promise<MarginPoolParams[]>;
|
|
89
|
+
getPoolsParameters(coinKeys: string[], supplierCapId: string | undefined, tx: Transaction, inspect: false): Promise<Transaction>;
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
export { DeepBookMarginPool, DeepBookMarginToolkit, type MarginBalance, type MarginCoinType, type MarginPoolParams, type NetworkType, type ReferralInfo, type ToolkitConfig, type TransactionResult };
|
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 T=n=>/^0x[0-9a-fA-F]+$|^[0-9a-fA-F]+$/.test(n),B=n=>/^[a-zA-Z0-9+/]+={0,2}$/g.test(n),A=n=>{if(T(n))return fromHex(n);if(B(n))return fromBase64(n);throw new Error("The string is not a valid hex or base64 string.")},P=n=>{if(n.length===LEGACY_PRIVATE_KEY_SIZE)return n.slice(0,PRIVATE_KEY_SIZE);if(n.length===PRIVATE_KEY_SIZE+1&&n[0]===0)return n.slice(1);if(n.length===PRIVATE_KEY_SIZE)return n;throw new Error("invalid secret key")};var C=class{suiClient;keypair;address;marginPoolContract;supplierCapId;dbConfig;supplierCapPackageId;constructor({network:r,fullnodeUrl:i,supplierCapId:t,privateKey:e,supplierCapPackageId:o,dbConfig:a}){let s=i??getFullnodeUrl(r);this.suiClient=new SuiClient({url:s}),this.keypair=this.#r(e),this.address=this.keypair.getPublicKey().toSuiAddress(),this.supplierCapId=t,this.dbConfig=a??new DeepBookConfig({network:r,address:this.address}),this.marginPoolContract=new MarginPoolContract(this.dbConfig),this.supplierCapPackageId=o??this.dbConfig.MARGIN_PACKAGE_ID;}#r(r){if(r.startsWith(SUI_PRIVATE_KEY_PREFIX)){let{secretKey:i}=decodeSuiPrivateKey(r);return Ed25519Keypair.fromSecretKey(P(i))}return Ed25519Keypair.fromSecretKey(P(A(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 i=await this.createSupplierCap();if(!i)throw new Error("Failed to create Supplier Cap");return this.supplierCapId=i,i}async createSupplierCap(){try{let r=new Transaction;r.setSender(this.address);let i=r.moveCall({target:`${this.supplierCapPackageId}::margin_pool::mint_supplier_cap`,arguments:[r.object(this.dbConfig.MARGIN_REGISTRY_ID),r.object.clock()]});r.transferObjects([i],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(e=>e.toString()).join(", ")}`);if(t.objectChanges){for(let e of t.objectChanges)if(e.type==="created"&&e.objectType.includes("SupplierCap"))return e.objectId}return null}catch(r){throw new Error(`Failed to create Supplier Cap: ${r.message||r}`)}}async createSupplyReferral(r){try{let i=new Transaction;i.setSender(this.address);let t=this.dbConfig.getMarginPool(r);if(!t)throw new Error(`Margin pool configuration not found for coin: ${r}`);i.moveCall({target:`${this.dbConfig.MARGIN_PACKAGE_ID}::margin_pool::mint_supply_referral`,arguments:[i.object(t.address),i.object(this.dbConfig.MARGIN_REGISTRY_ID),i.object.clock()],typeArguments:[t.type]});let e=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:i,options:{showEffects:!0,showObjectChanges:!0}});if(e.errors&&e.errors.length>0)throw new Error(`Transaction failed with errors: ${e.errors.map(o=>o.toString()).join(", ")}`);if(e.objectChanges){for(let o of e.objectChanges)if(o.type==="created"&&o.objectType.includes("SupplyReferral"))return o.objectId}return null}catch(i){throw new Error(`Failed to create Supply Referral: ${i.message||i}`)}}async supplyToMarginPool(r,i,t){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let e=new Transaction;e.setSender(this.address);let o=e.object(this.supplierCapId);e.add(this.marginPoolContract.supplyToMarginPool(r,o,i,t));let{errors:a}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:e,options:{showEffects:!0,showObjectChanges:!0}});if(a&&a.length>0)throw new Error(`Transaction failed with errors: ${a.map(s=>s.toString()).join(", ")}`);return !0}catch(e){throw new Error(`Failed to supply to margin pool: ${e.message||e}`)}}async withdrawFromMarginPool(r,i){try{if(!this.supplierCapId)throw new Error("Supplier Cap not initialized. Call initialize() first.");let t=new Transaction,e=t.object(this.supplierCapId),a=this.marginPoolContract.withdrawFromMarginPool(r,e,i)(t);t.transferObjects([a],this.address);let{errors:s}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:t,options:{showEffects:!0,showObjectChanges:!0}});if(s&&s.length>0)throw new Error(`Transaction failed with errors: ${s.map(p=>p.toString()).join(", ")}`);return !0}catch(t){throw new Error(`Failed to withdraw from margin pool: ${t.message||t}`)}}async withdrawReferralFees(r,i){try{let t=new Transaction;t.add(this.marginPoolContract.withdrawReferralFees(r,i));let{errors:e}=await this.suiClient.signAndExecuteTransaction({signer:this.keypair,transaction:t,options:{showEffects:!0,showObjectChanges:!0,showBalanceChanges:!0}});if(e&&e.length>0)throw new Error(`Transaction failed with errors: ${e.map(o=>o.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 i=new Transaction;i.add(this.marginPoolContract.userSupplyAmount(r,this.supplierCapId));let t=await this.suiClient.devInspectTransactionBlock({sender:this.address,transactionBlock:i}),e=0;if(t&&t.results&&t.results[0]&&t.results[0].returnValues){let l=t.results[0].returnValues[0];if(l&&l[0]){let c=Buffer.from(l[0]).readBigUInt64LE(),m=this.dbConfig.getCoin(r).scalar;e=Number(c)/m;}}let o=this.dbConfig.getCoin(r).type,a=await this.suiClient.getBalance({owner:this.address,coinType:o}),s=this.dbConfig.getCoin(r).scalar,p=Number(a.totalBalance)/s;return {userSupplyAmount:e,walletBalance:p}}catch(i){throw new Error(`Failed to get balance: ${i.message||i}`)}}getSupplierCapId(){return this.supplierCapId}getAddress(){return this.address}};var w=["supplyCap","maxUtilizationRate","protocolSpread","minBorrow","interestRate","totalSupply","supplyShares","totalBorrow","borrowShares","lastUpdateTimestamp"],h=["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 d=(n,r)=>n*r/BigInt(FLOAT_SCALAR),u=n=>Number(n)/FLOAT_SCALAR;var W=new Set(h),D=n=>W.has(n),b=class{marginPoolContract;dbConfig;suiClient;constructor({network:r,address:i="",suiClient:t,dbConfig:e}={}){let o=r??e?.network??"mainnet";if(this.dbConfig=e??new DeepBookConfig({network:o,address:i}),this.suiClient=t??new SuiClient({url:getFullnodeUrl(o)}),this.marginPoolContract=new MarginPoolContract(this.dbConfig),r!==void 0&&e!==void 0&&r!==e.network)throw new Error(`Mismatch between provided network (${r}) and dbConfig network (${e.network}).`)}get network(){return this.dbConfig.network}#r(r,i,t,e){if(D(i)){let o=this.marginPoolContract[i];if(e==null)throw new Error(`supplierCap is required for '${i}'.`);r.add(o(t,e));}else {let o=this.marginPoolContract[i];r.add(o(t));}}parseInspectResultToBcsStructs(r,i){let t=r.results;if(!t)throw new Error("No results found in DevInspect output.");return i.reduce((e,o,a)=>{let s=t[a]?.returnValues?.[0]?.[0];if(!s)return e;let p=bcs[_[o]];return e[o]=p.parse(new Uint8Array(s)),e},{})}formatResult(r,i){let t=this.dbConfig.getCoin(i),e={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 e;let o=new Set(["interestRate","maxUtilizationRate","protocolSpread"]);for(let[a,s]of Object.entries(r))a==="lastUpdateTimestamp"?e[a]=Number(s):o.has(a)?e[a]=new BigNumber(s).dividedBy(FLOAT_SCALAR).toNumber():e[a]=new BigNumber(s).dividedBy(t.scalar).toNumber();return e}computeBorrowAprAtUtil(r,i){let t=BigInt(i.base_rate),e=BigInt(i.base_slope),o=BigInt(i.excess_slope),a=BigInt(i.optimal_utilization);return r<a?t+d(r,e):t+d(a,e)+d(r-a,o)}calculateKinksAndRate(r,i,t,e){let o=BigInt(r.optimal_utilization),a=BigInt(i.max_utilization_rate),s=this.computeBorrowAprAtUtil(o,r),p=this.computeBorrowAprAtUtil(a,r),l=BigInt(BigNumber(t.total_borrow).dividedBy(t.total_supply).shiftedBy(9).decimalPlaces(0).toString()),c=d(d(e,l),BigInt(FLOAT_SCALAR)-BigInt(i.protocol_spread));return {raw:{baseBorrowApr:r.base_rate,highKink:o,borrowAprOnHighKink:s,maxBorrowApr:p,supplyApr:c,utilizationRate:l},normalized:{baseBorrowApr:u(BigInt(r.base_rate)),highKink:u(o),borrowAprOnHighKink:u(s),maxBorrowApr:u(p),supplyApr:u(c),utilizationRate:u(l)}}}async#t(r,i){let{address:t}=this.dbConfig.getMarginPool(r),o=((await this.suiClient.getObject({id:t,options:{showContent:true}})).data?.content).fields,a=o.config.fields,s=a.interest_config.fields,p=a.margin_pool_config.fields,l=o.state.fields,{normalized:c}=this.calculateKinksAndRate(s,p,l,i);return c}async getPoolParameters(r,i,t=new Transaction,e=true){if(w.forEach(m=>this.#r(t,m,r)),i&&h.forEach(m=>this.#r(t,m,r,i)),!e)return t;let o=[...w,...h],a=await this.suiClient.devInspectTransactionBlock({transactionBlock:t,sender:this.dbConfig.address}),s=this.parseInspectResultToBcsStructs(a,o),p=this.formatResult(s,r),l=BigInt(s.interestRate??0),c=await this.#t(r,l);return {...p,...c}}};export{b as DeepBookMarginPool,C 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';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};
|
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
|
+
"version": "0.3.0",
|
|
5
5
|
"description": "A toolkit for integrating Scallop with DeepBook functionality",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"scallop",
|
|
@@ -23,12 +23,12 @@
|
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"main": "dist/index.js",
|
|
26
|
-
"module": "dist/index.
|
|
26
|
+
"module": "dist/index.js",
|
|
27
27
|
"types": "dist/index.d.ts",
|
|
28
28
|
"exports": {
|
|
29
29
|
".": {
|
|
30
30
|
"source": "./src/index.ts",
|
|
31
|
-
"import": "./dist/index.
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
32
|
"require": "./dist/index.js",
|
|
33
33
|
"default": "./dist/index.js"
|
|
34
34
|
}
|
|
@@ -18,22 +18,31 @@ const MARGIN_POOLS = {
|
|
|
18
18
|
address: '0x38decd3dbb62bd4723144349bf57bc403b393aee86a51596846a824a1e0c2c01',
|
|
19
19
|
type: '0x356a26eb9e012a68958082340d4c4116e7f55615cf27affcff209cf0ae544f59::wal::WAL',
|
|
20
20
|
},
|
|
21
|
+
XBTC: {
|
|
22
|
+
address: '0x14dfbf54400e0b97e892349310d392bef6d187c2b6709d9b246b8f41c9a13de4',
|
|
23
|
+
type: '0x876a4b7bce8aeaef60464c11f4026903e9afacab79b9b142686158aa86560b50::xbtc::XBTC',
|
|
24
|
+
},
|
|
25
|
+
SUIUSDE: {
|
|
26
|
+
address: '0xbb990ca04a7743e6c0a25a7fb16f60fc6f6d8bf213624ff03a63f1bb04c3a12f',
|
|
27
|
+
type: '0x41d587e5336f1c86cad50d38a7136db99333bb9bda91cea4ba69115defeb1402::sui_usde::SUI_USDE',
|
|
28
|
+
},
|
|
21
29
|
};
|
|
22
30
|
|
|
23
31
|
const main = async () => {
|
|
24
32
|
try {
|
|
25
|
-
const
|
|
33
|
+
const network = 'mainnet';
|
|
26
34
|
const dbMarginPool = new DeepBookMarginPool({
|
|
27
|
-
|
|
35
|
+
network,
|
|
28
36
|
dbConfig: new DeepBookConfig({
|
|
29
|
-
|
|
37
|
+
network,
|
|
30
38
|
address: '',
|
|
31
39
|
marginPools: MARGIN_POOLS,
|
|
32
40
|
}),
|
|
33
41
|
});
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
console.log(
|
|
42
|
+
const coinKeys = ['USDC', 'XBTC'];
|
|
43
|
+
const suiMarginPoolsParams = await dbMarginPool.getPoolsParameters(coinKeys);
|
|
44
|
+
console.log('Margin Pools Parameters:');
|
|
45
|
+
console.log(JSON.stringify(suiMarginPoolsParams, null, 2));
|
|
37
46
|
} catch (error) {
|
|
38
47
|
console.error('An error occurred:', error);
|
|
39
48
|
} finally {
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Coin, DeepBookConfig, FLOAT_SCALAR, MarginPoolContract } from '@mysten/deepbook-v3';
|
|
2
2
|
import { bcs } from '@mysten/sui/bcs';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
DevInspectResults,
|
|
5
|
+
getFullnodeUrl,
|
|
6
|
+
SuiClient,
|
|
7
|
+
SuiObjectResponse,
|
|
8
|
+
} from '@mysten/sui/client';
|
|
4
9
|
import { Transaction } from '@mysten/sui/transactions';
|
|
5
10
|
import { BigNumber } from 'bignumber.js';
|
|
6
11
|
import {
|
|
@@ -309,9 +314,9 @@ export class DeepBookMarginPool {
|
|
|
309
314
|
* @param coinKey - Asset key.
|
|
310
315
|
* @param borrowAprScaled - interestRate in FLOAT_SCALAR scale (bigint).
|
|
311
316
|
*/
|
|
312
|
-
async #getInterestConfig(coinKey: string, borrowAprScaled: bigint) {
|
|
317
|
+
async #getInterestConfig(coinKey: string, borrowAprScaled: bigint, response?: SuiObjectResponse) {
|
|
313
318
|
const { address } = this.dbConfig.getMarginPool(coinKey);
|
|
314
|
-
|
|
319
|
+
response ??= await this.suiClient.getObject({
|
|
315
320
|
id: address,
|
|
316
321
|
options: {
|
|
317
322
|
showContent: true,
|
|
@@ -368,7 +373,6 @@ export class DeepBookMarginPool {
|
|
|
368
373
|
inspect: false
|
|
369
374
|
): Promise<Transaction>;
|
|
370
375
|
|
|
371
|
-
// 👇 implementation signature (must be last)
|
|
372
376
|
async getPoolParameters(
|
|
373
377
|
coinKey: string,
|
|
374
378
|
supplierCapId?: string,
|
|
@@ -409,4 +413,125 @@ export class DeepBookMarginPool {
|
|
|
409
413
|
...interestData,
|
|
410
414
|
};
|
|
411
415
|
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Batch version of getPoolParameters for reduced RPC calls.
|
|
419
|
+
* Adds all param calls to a single transaction and performs one devInspect.
|
|
420
|
+
*
|
|
421
|
+
* @param coinKeys - Asset keys.
|
|
422
|
+
* @param supplierCapId - Supplier cap object ID.
|
|
423
|
+
* @param tx - Optional transaction to append to.
|
|
424
|
+
* @param inspect - Whether to perform devInspect and return decoded results.
|
|
425
|
+
*
|
|
426
|
+
* @returns Parsed parameters or transaction object ready for execution.
|
|
427
|
+
*/
|
|
428
|
+
async getPoolsParameters(
|
|
429
|
+
coinKeys: string[],
|
|
430
|
+
supplierCapId?: string,
|
|
431
|
+
tx?: Transaction
|
|
432
|
+
): Promise<MarginPoolParams[]>;
|
|
433
|
+
|
|
434
|
+
async getPoolsParameters(
|
|
435
|
+
coinKeys: string[],
|
|
436
|
+
supplierCapId: string | undefined,
|
|
437
|
+
tx: Transaction,
|
|
438
|
+
inspect: true
|
|
439
|
+
): Promise<MarginPoolParams[]>;
|
|
440
|
+
|
|
441
|
+
async getPoolsParameters(
|
|
442
|
+
coinKeys: string[],
|
|
443
|
+
supplierCapId: string | undefined,
|
|
444
|
+
tx: Transaction,
|
|
445
|
+
inspect: false
|
|
446
|
+
): Promise<Transaction>;
|
|
447
|
+
|
|
448
|
+
async getPoolsParameters(
|
|
449
|
+
coinKeys: string[],
|
|
450
|
+
supplierCapId?: string,
|
|
451
|
+
tx: Transaction = new Transaction(),
|
|
452
|
+
inspect: boolean = true
|
|
453
|
+
): Promise<MarginPoolParams[] | Transaction> {
|
|
454
|
+
// Add all param calls for every coin into a single transaction
|
|
455
|
+
for (const coinKey of coinKeys) {
|
|
456
|
+
MARGIN_POOL_PARAM_KEYS.forEach((paramKey) => this.#addParamCall(tx, paramKey, coinKey));
|
|
457
|
+
|
|
458
|
+
if (supplierCapId) {
|
|
459
|
+
MARGIN_POOL_W_SUPPLIER_CAP_PARAM_KEYS.forEach((paramKey) =>
|
|
460
|
+
this.#addParamCall(tx, paramKey, coinKey, supplierCapId)
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (!inspect) return tx;
|
|
466
|
+
|
|
467
|
+
// Determine how many result slots each coin occupies
|
|
468
|
+
const keysPerCoin: (MarginPoolParamKey | MarginPoolWithSupplierCapParamKey)[] = [
|
|
469
|
+
...MARGIN_POOL_PARAM_KEYS,
|
|
470
|
+
...(supplierCapId ? MARGIN_POOL_W_SUPPLIER_CAP_PARAM_KEYS : []),
|
|
471
|
+
];
|
|
472
|
+
const slotCount = keysPerCoin.length;
|
|
473
|
+
|
|
474
|
+
// Single devInspect + batched multiGetObjects (max 50 per call)
|
|
475
|
+
const objectIds = coinKeys.map((coinKey) => this.dbConfig.getMarginPool(coinKey).address);
|
|
476
|
+
const MULTI_GET_BATCH_SIZE = 50;
|
|
477
|
+
const batches: string[][] = [];
|
|
478
|
+
for (let i = 0; i < objectIds.length; i += MULTI_GET_BATCH_SIZE) {
|
|
479
|
+
batches.push(objectIds.slice(i, i + MULTI_GET_BATCH_SIZE));
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const [inspectResult, ...objectBatches] = await Promise.all([
|
|
483
|
+
this.suiClient.devInspectTransactionBlock({
|
|
484
|
+
transactionBlock: tx,
|
|
485
|
+
sender: this.dbConfig.address,
|
|
486
|
+
}),
|
|
487
|
+
...batches.map((ids) =>
|
|
488
|
+
this.suiClient.multiGetObjects({
|
|
489
|
+
ids,
|
|
490
|
+
options: { showContent: true },
|
|
491
|
+
})
|
|
492
|
+
),
|
|
493
|
+
]);
|
|
494
|
+
const interestConfigs = objectBatches.flat();
|
|
495
|
+
|
|
496
|
+
const allResults = inspectResult.results;
|
|
497
|
+
if (!allResults) throw new Error('No results found in DevInspect output.');
|
|
498
|
+
|
|
499
|
+
// Split results per coin and format
|
|
500
|
+
return Promise.all(
|
|
501
|
+
coinKeys.map(async (coinKey, coinIdx) => {
|
|
502
|
+
const offset = coinIdx * slotCount;
|
|
503
|
+
|
|
504
|
+
// Parse the slice of results belonging to this coin
|
|
505
|
+
const parsed = keysPerCoin.reduce(
|
|
506
|
+
(acc, key, keyIdx) => {
|
|
507
|
+
const bytes = allResults[offset + keyIdx]?.returnValues?.[0]?.[0];
|
|
508
|
+
if (!bytes) return acc;
|
|
509
|
+
const bcsType = bcs[MARGIN_POOL_PARAM_KEY_STRUCT_MAP[key]];
|
|
510
|
+
acc[key] = bcsType.parse(new Uint8Array(bytes));
|
|
511
|
+
return acc;
|
|
512
|
+
},
|
|
513
|
+
{} as Record<MarginPoolParamKey | MarginPoolWithSupplierCapParamKey, string>
|
|
514
|
+
);
|
|
515
|
+
|
|
516
|
+
const formattedResult = this.formatResult(parsed, coinKey);
|
|
517
|
+
const borrowAprScaled = BigInt(parsed.interestRate ?? 0);
|
|
518
|
+
|
|
519
|
+
const interestConfig = interestConfigs[coinIdx];
|
|
520
|
+
if (!interestConfig || interestConfig.error) {
|
|
521
|
+
throw new Error(`Failed to fetch interest config for ${coinKey}`);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const interestData = await this.#getInterestConfig(
|
|
525
|
+
coinKey,
|
|
526
|
+
borrowAprScaled,
|
|
527
|
+
interestConfig
|
|
528
|
+
);
|
|
529
|
+
|
|
530
|
+
return {
|
|
531
|
+
...formattedResult,
|
|
532
|
+
...interestData,
|
|
533
|
+
};
|
|
534
|
+
})
|
|
535
|
+
);
|
|
536
|
+
}
|
|
412
537
|
}
|