@naviprotocol/lending 1.3.0-dev.5 → 1.3.0-dev.7
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/config.d.ts.map +1 -1
- package/dist/flashloan.d.ts.map +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +654 -536
- package/dist/index.esm.js.map +1 -1
- package/dist/liquidate.d.ts.map +1 -1
- package/dist/pool.d.ts.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAIpE;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,SAAS,aACW,OAAO,CAAC,SAAS,GAAG,WAAW,CAAC,KAAG,OAAO,CAAC,aAAa,CAOxF,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,QAAgB,CAAA"}
|
package/dist/flashloan.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flashloan.d.ts","sourceRoot":"","sources":["../src/flashloan.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AAC3D,OAAO,KAAK,EACV,SAAS,EACT,eAAe,EACf,UAAU,EACV,WAAW,EACX,eAAe,EACf,iBAAiB,EAClB,MAAM,SAAS,CAAA;AAKhB;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,aACD,OAAO,CAAC,SAAS,GAAG,WAAW,CAAC,KAAG,OAAO,CAAC,eAAe,EAAE,CAU5F,CAAA;AAED;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,UAAU,EAAE,eAAe,EAC3B,OAAO,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,GAC3B,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAajC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,YAAY,CAChC,EAAE,EAAE,WAAW,EACf,UAAU,EAAE,eAAe,EAC3B,MAAM,EAAE,MAAM,GAAG,iBAAiB,EAClC,OAAO,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC;;;
|
|
1
|
+
{"version":3,"file":"flashloan.d.ts","sourceRoot":"","sources":["../src/flashloan.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AAC3D,OAAO,KAAK,EACV,SAAS,EACT,eAAe,EACf,UAAU,EACV,WAAW,EACX,eAAe,EACf,iBAAiB,EAClB,MAAM,SAAS,CAAA;AAKhB;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,aACD,OAAO,CAAC,SAAS,GAAG,WAAW,CAAC,KAAG,OAAO,CAAC,eAAe,EAAE,CAU5F,CAAA;AAED;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,UAAU,EAAE,eAAe,EAC3B,OAAO,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,GAC3B,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAajC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,YAAY,CAChC,EAAE,EAAE,WAAW,EACf,UAAU,EAAE,eAAe,EAC3B,MAAM,EAAE,MAAM,GAAG,iBAAiB,EAClC,OAAO,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC;;;KA+C7B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,iBAAiB,CACrC,EAAE,EAAE,WAAW,EACf,UAAU,EAAE,eAAe,EAC3B,OAAO,EAAE,iBAAiB,GAAG,MAAM,EACnC,UAAU,EAAE,UAAU,EACtB,OAAO,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC;;;KAkC7B"}
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const B=require("@mysten/sui/transactions"),o=require("@mysten/bcs"),E=require("@mysten/sui/client"),Q=require("lodash.camelcase"),X=require("@mysten/sui/utils"),V=require("@pythnetwork/pyth-sui-js"),j=require("bignumber.js"),w=require("@mysten/sui/bcs"),R=o.bcs.bytes(32).transform({input:e=>o.fromHex(e),output:e=>o.toHex(e)}),Z=o.bcs.struct("IncentiveAPYInfo",{asset_id:o.bcs.u8(),apy:o.bcs.u256(),coin_types:o.bcs.vector(o.bcs.string())}),U=o.bcs.struct("IncentivePoolInfo",{pool_id:R,funds:R,phase:o.bcs.u64(),start_at:o.bcs.u64(),end_at:o.bcs.u64(),closed_at:o.bcs.u64(),total_supply:o.bcs.u64(),asset_id:o.bcs.u8(),option:o.bcs.u8(),factor:o.bcs.u256(),distributed:o.bcs.u64(),available:o.bcs.u256(),total:o.bcs.u256()}),x=o.bcs.struct("IncentivePoolInfoByPhase",{phase:o.bcs.u64(),pools:o.bcs.vector(U)}),ee=o.bcs.struct("OracleInfo",{oracle_id:o.bcs.u8(),price:o.bcs.u256(),decimals:o.bcs.u8(),valid:o.bcs.bool()}),te=o.bcs.struct("FlashLoanAssetConfig",{id:o.bcs.string(),asset_id:o.bcs.u8(),coin_type:o.bcs.string(),pool_id:o.bcs.string(),rate_to_supplier:o.bcs.u64(),rate_to_treasury:o.bcs.u64(),max:o.bcs.u64(),min:o.bcs.u64()}),ae=o.bcs.struct("ReserveDataInfo",{id:o.bcs.u8(),oracle_id:o.bcs.u8(),coin_type:o.bcs.string(),supply_cap:o.bcs.u256(),borrow_cap:o.bcs.u256(),supply_rate:o.bcs.u256(),borrow_rate:o.bcs.u256(),supply_index:o.bcs.u256(),borrow_index:o.bcs.u256(),total_supply:o.bcs.u256(),total_borrow:o.bcs.u256(),last_update_at:o.bcs.u64(),ltv:o.bcs.u256(),treasury_factor:o.bcs.u256(),treasury_balance:o.bcs.u256(),base_rate:o.bcs.u256(),multiplier:o.bcs.u256(),jump_rate_multiplier:o.bcs.u256(),reserve_factor:o.bcs.u256(),optimal_utilization:o.bcs.u256(),liquidation_ratio:o.bcs.u256(),liquidation_bonus:o.bcs.u256(),liquidation_threshold:o.bcs.u256()}),D=o.bcs.struct("UserStateInfo",{asset_id:o.bcs.u8(),borrow_balance:o.bcs.u256(),supply_balance:o.bcs.u256()}),I=new E.SuiClient({url:E.getFullnodeUrl("mainnet")});function M(e){const a=[];return e.forEach((c,t)=>{const r=t===e.length-1;if(typeof c=="object"&&r){const{client:n,disableCache:s,cacheTime:i,...u}=c;a.push(u)}else a.push(c)}),JSON.stringify(a)}function T(e){const a={};return(...c)=>{const t=M(c);return a[t]||(a[t]=e(...c).finally(()=>{a[t]=null})),a[t]}}function P(e){let a={};return(...c)=>{const t=c[c.length-1],r=M(c),n=a[r];return!(t!=null&&t.disableCache)&&typeof(n==null?void 0:n.data)!="undefined"&&(typeof(t==null?void 0:t.cacheTime)=="undefined"||t.cacheTime>Date.now()-n.cacheAt)?n.data:e(...c).then(s=>(a[r]={data:s,cacheAt:Date.now()},s))}}function F(e){return Array.isArray(e)?e.map(a=>F(a)):e!=null&&typeof e=="object"?Object.keys(e).reduce((a,c)=>({...a,[Q(c)]:F(e[c])}),{}):e}function f(e,a){return typeof e=="object"?e:a(e)}function ce(e,a){return typeof a=="string"?e.object(a):typeof a=="object"&&a.$kind?a:e.object(a.contract.pool)}function S(e,a,c){if(e.results&&e.results.length>0){if(e.results[0].returnValues&&e.results[0].returnValues.length>0)return e.results[0].returnValues.map((t,r)=>(a[r]||a[0]).parse(Uint8Array.from(t[0])))}else if(e.error)return console.log(`Get an error, msg: ${e.error}`),[];return[]}function g(e){return X.normalizeStructTag(e)}function q(e){const a=(e||0)/Math.pow(10,27);return a>Math.pow(10,5)?1/0:a}new V.SuiPriceServiceConnection("https://hermes.pyth.network",{timeout:2e4});const re=27,N=(e,a)=>{if(!Number(e)||!Number(a))return new j(0);const c=new j(1).shiftedBy(1*re),t=c.multipliedBy(new j(.5));return new j(e).multipliedBy(new j(a)).plus(t).dividedBy(c).integerValue(j.ROUND_DOWN)},y=P(T(async e=>{const a=`https://open-api.naviprotocol.io/api/navi/config?env=${(e==null?void 0:e.env)||"prod"}&v=2`;return(await fetch(a).then(t=>t.json())).data})),p=1e3*60*5;var A=(e=>(e[e.Supply=1]="Supply",e[e.Withdraw=2]="Withdraw",e[e.Borrow=3]="Borrow",e[e.Repay=4]="Repay",e))(A||{});const $=P(T(async e=>{const a=`https://open-api.naviprotocol.io/api/navi/pools?env=${(e==null?void 0:e.env)||"prod"}`;return(await fetch(a).then(t=>t.json())).data}));async function v(e,a){const c=await $({...a,cacheTime:p});if(typeof e=="object")return e;const t=c.find(r=>typeof e=="string"?g(r.suiCoinType)===g(e):typeof e=="number"?r.id===e:!1);if(!t)throw new Error("Pool not found");return t}const ne=P(T(async e=>(await fetch("https://open-api.naviprotocol.io/api/navi/stats").then(t=>t.json())).data)),se=P(T(async e=>await fetch("https://open-api.naviprotocol.io/api/navi/fee").then(t=>t.json())));async function H(e,a,c,t){const r=await y({...t,cacheTime:p}),n=await v(a,t),s=typeof c=="object"&&c.$kind==="GasCoin";if(g(n.suiCoinType)===g("0x2::sui::SUI")&&s){if(!(t!=null&&t.amount))throw new Error("Amount is required for sui coin");c=e.splitCoins(c,[t.amount])}let i;return typeof(t==null?void 0:t.amount)!="undefined"?i=f(t.amount,e.pure.u64):i=e.moveCall({target:"0x2::coin::value",arguments:[f(c,e.object)],typeArguments:[n.suiCoinType]}),t!=null&&t.accountCap?e.moveCall({target:`${r.package}::incentive_v3::deposit_with_account_cap`,arguments:[e.object("0x06"),e.object(r.storage),e.object(n.contract.pool),e.pure.u8(n.id),f(c,e.object),e.object(r.incentiveV2),e.object(r.incentiveV3),f(t.accountCap,e.object)],typeArguments:[n.suiCoinType]}):e.moveCall({target:`${r.package}::incentive_v3::entry_deposit`,arguments:[e.object("0x06"),e.object(r.storage),e.object(n.contract.pool),e.pure.u8(n.id),f(c,e.object),i,e.object(r.incentiveV2),e.object(r.incentiveV3)],typeArguments:[n.suiCoinType]}),n.id===0&&e.moveCall({target:`${r.package}::pool::refresh_stake`,arguments:[e.object(n.contract.pool),e.object("0x05")]}),e}async function oe(e,a,c,t){const r=await y({...t,cacheTime:p}),n=await v(a,t),s=f(c,e.pure.u64);let i;if(t!=null&&t.accountCap){const[l]=e.moveCall({target:`${r.package}::incentive_v3::withdraw_with_account_cap_v2`,arguments:[e.object("0x06"),e.object(r.priceOracle),e.object(r.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(r.incentiveV2),e.object(r.incentiveV3),f(t.accountCap,e.object),e.object("0x05")],typeArguments:[n.suiCoinType]});i=l}else{const[l]=e.moveCall({target:`${r.package}::incentive_v3::withdraw_v2`,arguments:[e.object("0x06"),e.object(r.priceOracle),e.object(r.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(r.incentiveV2),e.object(r.incentiveV3),e.object("0x05")],typeArguments:[n.suiCoinType]});i=l}return e.moveCall({target:"0x2::coin::from_balance",arguments:[i],typeArguments:[n.suiCoinType]})}async function ie(e,a,c,t){const r=await y({...t,cacheTime:p}),n=await v(a,t),s=f(c,e.pure.u64);let i;if(t!=null&&t.accountCap){const[l]=e.moveCall({target:`${r.package}::incentive_v3::borrow_with_account_cap_v2`,arguments:[e.object("0x06"),e.object(r.priceOracle),e.object(r.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(r.incentiveV2),e.object(r.incentiveV3),f(t.accountCap,e.object),e.object("0x05")],typeArguments:[n.suiCoinType]});i=l}else{const[l]=e.moveCall({target:`${r.package}::incentive_v3::borrow_v2`,arguments:[e.object("0x06"),e.object(r.priceOracle),e.object(r.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(r.incentiveV2),e.object(r.incentiveV3),e.object("0x05")],typeArguments:[n.suiCoinType]});i=l}return e.moveCall({target:"0x2::coin::from_balance",arguments:[e.object(i)],typeArguments:[n.suiCoinType]})}async function ue(e,a,c,t){const r=await y({...t,cacheTime:p}),n=await v(a,t),s=typeof c=="object"&&c.$kind==="GasCoin";if(g(n.suiCoinType)===g("0x2::sui::SUI")&&s){if(!(t!=null&&t.amount))throw new Error("Amount is required for sui coin");c=e.splitCoins(c,[t.amount])}let i;return typeof(t==null?void 0:t.amount)!="undefined"?i=f(t.amount,e.pure.u64):i=e.moveCall({target:"0x2::coin::value",arguments:[f(c,e.object)],typeArguments:[n.suiCoinType]}),t!=null&&t.accountCap?e.moveCall({target:`${r.package}::incentive_v3::repay_with_account_cap`,arguments:[e.object("0x06"),e.object(r.priceOracle),e.object(r.storage),e.object(n.contract.pool),e.pure.u8(n.id),f(c,e.object),i,e.object(r.incentiveV2),e.object(r.incentiveV3),f(t.accountCap,e.object)],typeArguments:[n.suiCoinType]}):e.moveCall({target:`${r.package}::incentive_v3::entry_repay`,arguments:[e.object("0x06"),e.object(r.priceOracle),e.object(r.storage),e.object(n.contract.pool),e.pure.u8(n.id),f(c,e.object),i,e.object(r.incentiveV2),e.object(r.incentiveV3)],typeArguments:[n.suiCoinType]}),e}const le=P(T(async e=>{var r;const a=await y({...e});if(e!=null&&e.address&&typeof(e==null?void 0:e.asset)!="undefined")try{const n=await v(e.asset,e),s=(r=e==null?void 0:e.client)!=null?r:I,i=new B.Transaction;i.moveCall({target:`${a.package}::incentive_v3::get_borrow_fee_v2`,arguments:[i.object(a.incentiveV3),i.pure.address(e.address),i.pure.u8(n.id),i.pure.u64(1e4)],typeArguments:[]});const u=await s.devInspectTransactionBlock({transactionBlock:i,sender:e.address}),l=S(u,[w.bcs.u64()]);return(Number(l[0])||0)/100}catch(n){console.error(n)}const t=(await I.getObject({id:a.incentiveV3,options:{showType:!0,showOwner:!0,showContent:!0}})).data.content.fields.borrow_fee_rate;return Number(t)/100}));function de(e,a,c){const t=typeof(c==null?void 0:c.balance)=="number",r=t?c.balance:0;let n=0;const s=[];let i="";if(a.sort((l,d)=>Number(d.balance)-Number(l.balance)).forEach(l=>{if(!(t&&n>=r)&&Number(l.balance)!==0){if(i||(i=l.coinType),i!==l.coinType)throw new Error("All coins must be of the same type");n+=Number(l.balance),s.push(l.coinObjectId)}}),s.length===0)throw new Error("No coins to merge");if(t&&n<r)throw new Error(`Balance is less than the specified balance: ${n} < ${r}`);if(g(i)===g("0x2::sui::SUI")&&(c!=null&&c.useGasCoin))return t?e.splitCoins(e.gas,[e.pure.u64(r)]):e.gas;const u=s.length===1?e.object(s[0]):e.mergeCoins(s[0],s.slice(1));return t?e.splitCoins(u,[e.pure.u64(r)]):u}async function L(e,a,c,t,r,n,s){const i=await y({...s,cacheTime:p}),u=await v(c,s);return e.moveCall({target:`${i.uiGetter}::calculator_unchecked::dynamic_health_factor`,arguments:[e.object("0x06"),e.object(i.storage),e.object(i.oracle.priceOracle),ce(e,u),f(a,e.pure.address),f(u.id,e.pure.u8),f(t,e.pure.u64),f(r,e.pure.u64),f(n,e.pure.bool)],typeArguments:[u.suiCoinType]})}async function O(e,a,c){return L(e,a,0,0,0,!1,c)}const be=P(async(e,a)=>{var l;const c=await y({...a,cacheTime:p}),t=new B.Transaction,r=(l=a==null?void 0:a.client)!=null?l:I,n=await $(a);t.moveCall({target:`${c.uiGetter}::getter_unchecked::get_user_state`,arguments:[t.object(c.storage),t.pure.address(e)]});const s=await r.devInspectTransactionBlock({transactionBlock:t,sender:e}),i=S(s,[w.bcs.vector(D)]);return F(i[0].filter(d=>d.supply_balance!=="0"||d.borrow_balance!=="0")).map(d=>{const h=n.find(C=>C.id===d.assetId),b=N(d.supplyBalance,h.currentSupplyIndex).toString(),m=N(d.borrowBalance,h.currentBorrowIndex).toString();return{...d,supplyBalance:b,borrowBalance:m,pool:h}}).filter(d=>!!d.pool)});async function fe(e,a){var s;const c=(s=a==null?void 0:a.client)!=null?s:I,t=new B.Transaction;await O(t,e,a);const r=await c.devInspectTransactionBlock({transactionBlock:t,sender:e}),n=S(r,[w.bcs.u256()]);return q(Number(n[0])||0)}async function ge(e,a,c,t){var b;const r=(b=t==null?void 0:t.client)!=null?b:I,n=new B.Transaction;let s=0,i=0;const u=await v(a,t);if(c.forEach(m=>{m.type===A.Supply?s+=m.amount:m.type===A.Withdraw?s-=m.amount:m.type===A.Borrow?i+=m.amount:m.type===A.Repay&&(i-=m.amount)}),s*i<0)throw new Error("Invalid operations");const l=s>0||i>0;await L(n,e,u,Math.abs(s),Math.abs(i),l,t);const d=await r.devInspectTransactionBlock({transactionBlock:n,sender:e}),h=S(d,[w.bcs.u256()]);return q(Number(h[0])||0)}const pe=T(async(e,a)=>{const c=new URLSearchParams;a!=null&&a.cursor&&c.set("cursor",a.cursor),c.set("userAddress",e);const t=`https://open-api.naviprotocol.io/api/navi/user/transactions?${c.toString()}`;return(await fetch(t).then(n=>n.json())).data});async function me(e,a){var n;let c=null;const t=[],r=(n=a==null?void 0:a.client)!=null?n:I;do{let s;if(a!=null&&a.coinType?s=await r.getCoins({owner:e,coinType:a==null?void 0:a.coinType,cursor:c,limit:100}):s=await r.getAllCoins({owner:e,cursor:c,limit:100}),!s.data||!s.data.length)break;t.push(...s.data),c=s.nextCursor}while(c);return t}const G=new V.SuiPriceServiceConnection("https://hermes.pyth.network",{timeout:1e4});async function z(e){try{const a=[],c=await G.getLatestPriceFeeds(e);if(!c)return a;const t=Math.floor(new Date().valueOf()/1e3);for(const r of c){const n=r.getPriceUnchecked();if(n.publishTime>t){console.warn(`pyth price feed is invalid, id: ${r.id}, publish time: ${n.publishTime}, current timestamp: ${t}`);continue}t-r.getPriceUnchecked().publishTime>30&&(console.info(`stale price feed, id: ${r.id}, publish time: ${n.publishTime}, current timestamp: ${t}`),a.push(r.id))}return a}catch(a){throw new Error(`failed to get pyth stale price feed id, msg: ${a.message}`)}}async function W(e,a,c){var n;const t=(n=c==null?void 0:c.client)!=null?n:I,r=await y({...c,cacheTime:p});try{const s=await G.getPriceFeedsUpdateData(a);return await new V.SuiPythClient(t,r.oracle.pythStateId,r.oracle.wormholeStateId).updatePriceFeeds(e,s,a)}catch(s){throw new Error(`failed to update pyth price feeds, msg: ${s.message}`)}}async function ye(e,a,c){const t=await y({...c,cacheTime:p});if(c!=null&&c.updatePythPriceFeeds){const r=a.filter(n=>!!n.pythPriceFeedId).map(n=>n.pythPriceFeedId);try{const n=await z(r);n.length>0&&await W(e,n,c)}catch{}}for(const r of a)e.moveCall({target:`${t.oracle.packageId}::oracle_pro::update_single_price`,arguments:[e.object("0x6"),e.object(t.oracle.oracleConfig),e.object(t.oracle.priceOracle),e.object(t.oracle.supraOracleHolder),e.object(r.pythPriceInfoObject),e.pure.address(r.feedId)]});return e}async function Y(e){return(await y({...e,cacheTime:p})).oracle.feeds}function we(e,a){return e.filter(c=>!!(a!=null&&a.lendingState&&a.lendingState.find(r=>r.assetId===c.assetId)||a!=null&&a.pools&&a.pools.find(r=>r.id===c.assetId)))}const k=P(T(async e=>{const a=`https://open-api.naviprotocol.io/api/navi/flashloan?env=${(e==null?void 0:e.env)||"prod"}`,c=await fetch(a).then(t=>t.json());return Object.keys(c.data).map(t=>({...c.data[t],coinType:t}))}));async function he(e,a){return(await k(a)).find(t=>typeof e=="string"?g(t.coinType)===g(e):typeof e=="number"?t.assetId===e:t.assetId===e.id)||null}async function ve(e,a,c,t){const r=await y({...t,cacheTime:p}),n=await v(a,t);if(!(await k({...t,cacheTime:p})).some(d=>g(d.coinType)===g(n.suiCoinType)))throw new Error("Pool does not support flashloan");const[u,l]=e.moveCall({target:`${r.package}::lending::flash_loan_with_ctx_v2`,arguments:[e.object(r.flashloanConfig),e.object(n.contract.pool),f(c,e.pure.u64),e.object("0x05")],typeArguments:[n.suiCoinType]});return[u,l]}async function Ce(e,a,c,t,r){const n=await y({...r,cacheTime:p}),s=await v(a,r);if(!(await k({...r,cacheTime:p})).some(d=>g(d.coinType)===g(s.suiCoinType)))throw new Error("Pool does not support flashloan");const[l]=e.moveCall({target:`${n.package}::lending::flash_repay_with_ctx`,arguments:[e.object("0x06"),e.object(n.storage),e.object(s.contract.pool),f(c,e.object),f(t,e.object)],typeArguments:[s.suiCoinType]});return[l]}async function Te(e,a,c,t,r,n){const s={...n,cacheTime:p},i=await y(s),u=await v(a,s),l=await v(t,s),[d,h]=e.moveCall({target:`${i.package}::incentive_v3::liquidation_v2`,arguments:[e.object("0x06"),e.object(i.priceOracle),e.object(i.storage),e.pure.u8(u.id),e.object(u.contract.pool),f(c,e.object),e.pure.u8(l.id),e.object(l.contract.pool),f(r,e.pure.address),e.object(i.incentiveV2),e.object(i.incentiveV3),e.object("0x05")],typeArguments:[u.suiCoinType,l.suiCoinType]});return[d,h]}async function _e(e,a){var d;const c=await Y(a),t=await $({...a,cacheTime:p}),r=(d=a==null?void 0:a.client)!=null?d:I,n=await y({...a,cacheTime:p}),s=new B.Transaction;s.moveCall({target:`${n.uiGetter}::incentive_v3_getter::get_user_atomic_claimable_rewards`,arguments:[s.object("0x06"),s.object(n.storage),s.object(n.incentiveV3),s.pure.address(e)]});const i=await r.devInspectTransactionBlock({transactionBlock:s,sender:e}),u=S(i,[w.bcs.vector(w.bcs.string()),w.bcs.vector(w.bcs.string()),w.bcs.vector(w.bcs.u8()),w.bcs.vector(w.bcs.Address),w.bcs.vector(w.bcs.u256())]),l=[];if(u.length===5&&Array.isArray(u[0])){const h=u[0].length;for(let b=0;b<h;b++){const m=c.find(_=>g(_.coinType)===g(u[1][b])),C=t.find(_=>g(_.coinType)===g(u[0][b]));!m||!C||l.push({assetId:C.id,assetCoinType:g(u[0][b]),rewardCoinType:g(u[1][b]),option:Number(u[2][b]),userClaimableReward:Number(u[4][b])/Math.pow(10,m.priceDecimal),ruleIds:Array.isArray(u[3][b])?u[3][b]:[u[3][b]]})}}return l}function je(e){const a=new Map;e.forEach(t=>{const r=t.assetId,n=t.option,s=`${r}-${n}-${t.rewardCoinType}`;a.has(s)?a.get(s).total+=t.userClaimableReward:a.set(s,{assetId:r,rewardType:n,coinType:t.rewardCoinType,total:Number(t.userClaimableReward)})});const c=new Map;for(const{assetId:t,rewardType:r,coinType:n,total:s}of a.values()){const i=`${t}-${r}`;c.has(i)||c.set(i,{assetId:t,rewardType:r,rewards:new Map});const u=c.get(i);u.rewards.set(n,(u.rewards.get(n)||0)+s)}return Array.from(c.values()).map(t=>({assetId:t.assetId,rewardType:t.rewardType,rewards:Array.from(t.rewards.entries()).map(([r,n])=>({coinType:r,available:n.toFixed(6)}))}))}const Ie=T(async e=>{const a=`https://open-api.naviprotocol.io/api/navi/user/total_claimed_reward?userAddress=${e}`;return(await fetch(a).then(t=>t.json())).data}),Pe=T(async(e,a)=>{const c=`https://open-api.naviprotocol.io/api/navi/user/rewards?userAddress=${e}&page=${(a==null?void 0:a.page)||1}&pageSize=${(a==null?void 0:a.size)||400}`,t=await fetch(c).then(r=>r.json());return F({data:t.data.rewards})});async function Ae(e,a,c){var i;const t=await y({...c,cacheTime:p}),r=await $({...c,cacheTime:p}),n=new Map;for(const u of a){const{rewardCoinType:l,ruleIds:d}=u;for(const h of d){n.has(l)||n.set(l,{assetIds:[],ruleIds:[],amount:0});const b=n.get(l);b.assetIds.push(u.assetCoinType.replace("0x","")),b.ruleIds.push(h),b.amount+=u.userClaimableReward}}const s=[];for(const[u,{assetIds:l,ruleIds:d,amount:h}]of n){const b=r.find(C=>g(C.suiCoinType)===g(u));if(!b||!b.contract.rewardFundId)throw new Error(`No matching rewardFund found for reward coin: ${u}`);const m=b.contract.rewardFundId;if(c!=null&&c.accountCap&&!c.customCoinReceive)throw new Error("customCoinReceive is required when accountCap is provided");if(c!=null&&c.customCoinReceive){let C;c.accountCap?C=e.moveCall({target:`${t.package}::incentive_v3::claim_reward_with_account_cap`,arguments:[e.object("0x06"),e.object(t.incentiveV3),e.object(t.storage),e.object(m),e.pure.vector("string",l),e.pure.vector("address",d),f(c.accountCap,e.object)],typeArguments:[u]}):C=e.moveCall({target:`${t.package}::incentive_v3::claim_reward`,arguments:[e.object("0x06"),e.object(t.incentiveV3),e.object(t.storage),e.object(m),e.pure.vector("string",l),e.pure.vector("address",d)],typeArguments:[u]});const[_]=e.moveCall({target:"0x2::coin::from_balance",arguments:[C],typeArguments:[u]});if((c==null?void 0:c.customCoinReceive.type)==="transfer"){if(!c.customCoinReceive.transfer)throw new Error("customCoinReceive.transfer is required");e.transferObjects([_],f(c.customCoinReceive.transfer,e.pure.address))}if((c==null?void 0:c.customCoinReceive.type)==="depositNAVI"){const J=j(b.totalSupplyAmount).shiftedBy(-9),K=j(b.supplyCapCeiling).shiftedBy(-27);J.plus(h).isGreaterThan(K)&&((i=c==null?void 0:c.customCoinReceive.depositNAVI)!=null&&i.fallbackReceiveAddress)?e.transferObjects([_],e.pure.address(c.customCoinReceive.depositNAVI.fallbackReceiveAddress)):await H(e,b,_,c)}else s.push({coin:_,identifier:b})}else e.moveCall({target:`${t.package}::incentive_v3::claim_reward_entry`,arguments:[e.object("0x06"),e.object(t.incentiveV3),e.object(t.storage),e.object(m),e.pure.vector("string",l),e.pure.vector("address",d)],typeArguments:[u]})}return s}async function Be(e,a){const c=await y({...a});return e.moveCall({target:`${c.package}::lending::create_account`,arguments:[]})}exports.Address=R;exports.DEFAULT_CACHE_TIME=p;exports.FlashLoanAssetConfig=te;exports.IncentiveAPYInfo=Z;exports.IncentivePoolInfo=U;exports.IncentivePoolInfoByPhase=x;exports.OracleInfo=ee;exports.PoolOperator=A;exports.ReserveDataInfo=ae;exports.UserStateInfo=D;exports.borrowCoinPTB=ie;exports.claimLendingRewardsPTB=Ae;exports.createAccountCapPTB=Be;exports.depositCoinPTB=H;exports.filterPriceFeeds=we;exports.flashloanPTB=ve;exports.getAllFlashLoanAssets=k;exports.getBorrowFee=le;exports.getCoins=me;exports.getConfig=y;exports.getFees=se;exports.getFlashLoanAsset=he;exports.getHealthFactor=fe;exports.getHealthFactorPTB=O;exports.getLendingState=be;exports.getPool=v;exports.getPools=$;exports.getPriceFeeds=Y;exports.getPythStalePriceFeedId=z;exports.getSimulatedHealthFactor=ge;exports.getSimulatedHealthFactorPTB=L;exports.getStats=ne;exports.getTransactions=pe;exports.getUserAvailableLendingRewards=_e;exports.getUserClaimedRewardHistory=Pe;exports.getUserTotalClaimedReward=Ie;exports.liquidatePTB=Te;exports.mergeCoinsPTB=de;exports.normalizeCoinType=g;exports.parseTxValue=f;exports.repayCoinPTB=ue;exports.repayFlashLoanPTB=Ce;exports.summaryLendingRewards=je;exports.updateOraclePricesPTB=ye;exports.updatePythPriceFeeds=W;exports.withCache=P;exports.withSingleton=T;exports.withdrawCoinPTB=oe;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const B=require("@mysten/sui/transactions"),i=require("@mysten/bcs"),E=require("@mysten/sui/client"),Q=require("lodash.camelcase"),X=require("@mysten/sui/utils"),R=require("@pythnetwork/pyth-sui-js"),T=require("bignumber.js"),h=require("@mysten/sui/bcs"),V=i.bcs.bytes(32).transform({input:e=>i.fromHex(e),output:e=>i.toHex(e)}),Z=i.bcs.struct("IncentiveAPYInfo",{asset_id:i.bcs.u8(),apy:i.bcs.u256(),coin_types:i.bcs.vector(i.bcs.string())}),U=i.bcs.struct("IncentivePoolInfo",{pool_id:V,funds:V,phase:i.bcs.u64(),start_at:i.bcs.u64(),end_at:i.bcs.u64(),closed_at:i.bcs.u64(),total_supply:i.bcs.u64(),asset_id:i.bcs.u8(),option:i.bcs.u8(),factor:i.bcs.u256(),distributed:i.bcs.u64(),available:i.bcs.u256(),total:i.bcs.u256()}),x=i.bcs.struct("IncentivePoolInfoByPhase",{phase:i.bcs.u64(),pools:i.bcs.vector(U)}),ee=i.bcs.struct("OracleInfo",{oracle_id:i.bcs.u8(),price:i.bcs.u256(),decimals:i.bcs.u8(),valid:i.bcs.bool()}),te=i.bcs.struct("FlashLoanAssetConfig",{id:i.bcs.string(),asset_id:i.bcs.u8(),coin_type:i.bcs.string(),pool_id:i.bcs.string(),rate_to_supplier:i.bcs.u64(),rate_to_treasury:i.bcs.u64(),max:i.bcs.u64(),min:i.bcs.u64()}),ce=i.bcs.struct("ReserveDataInfo",{id:i.bcs.u8(),oracle_id:i.bcs.u8(),coin_type:i.bcs.string(),supply_cap:i.bcs.u256(),borrow_cap:i.bcs.u256(),supply_rate:i.bcs.u256(),borrow_rate:i.bcs.u256(),supply_index:i.bcs.u256(),borrow_index:i.bcs.u256(),total_supply:i.bcs.u256(),total_borrow:i.bcs.u256(),last_update_at:i.bcs.u64(),ltv:i.bcs.u256(),treasury_factor:i.bcs.u256(),treasury_balance:i.bcs.u256(),base_rate:i.bcs.u256(),multiplier:i.bcs.u256(),jump_rate_multiplier:i.bcs.u256(),reserve_factor:i.bcs.u256(),optimal_utilization:i.bcs.u256(),liquidation_ratio:i.bcs.u256(),liquidation_bonus:i.bcs.u256(),liquidation_threshold:i.bcs.u256()}),D=i.bcs.struct("UserStateInfo",{asset_id:i.bcs.u8(),borrow_balance:i.bcs.u256(),supply_balance:i.bcs.u256()}),A=new E.SuiClient({url:E.getFullnodeUrl("mainnet")});function M(e){const c=[];return e.forEach((r,t)=>{const a=t===e.length-1;if(typeof r=="object"&&a){const{client:n,disableCache:s,cacheTime:o,...u}=r;c.push(u)}else c.push(r)}),JSON.stringify(c)}function j(e){const c={};return(...r)=>{const t=M(r);return c[t]||(c[t]=e(...r).finally(()=>{c[t]=null})),c[t]}}function I(e){let c={};return(...r)=>{const t=r[r.length-1],a=M(r),n=c[a];return!(t!=null&&t.disableCache)&&typeof(n==null?void 0:n.data)!="undefined"&&(typeof(t==null?void 0:t.cacheTime)=="undefined"||t.cacheTime>Date.now()-n.cacheAt)?n.data:e(...r).then(s=>(c[a]={data:s,cacheAt:Date.now()},s))}}function S(e){return Array.isArray(e)?e.map(c=>S(c)):e!=null&&typeof e=="object"?Object.keys(e).reduce((c,r)=>({...c,[Q(r)]:S(e[r])}),{}):e}function d(e,c){return typeof e=="object"?e:c(e)}function ae(e,c){return typeof c=="string"?e.object(c):typeof c=="object"&&c.$kind?c:e.object(c.contract.pool)}function $(e,c,r){if(e.results&&e.results.length>0){if(e.results[0].returnValues&&e.results[0].returnValues.length>0)return e.results[0].returnValues.map((t,a)=>(c[a]||c[0]).parse(Uint8Array.from(t[0])))}else if(e.error)return console.log(`Get an error, msg: ${e.error}`),[];return[]}function p(e){return X.normalizeStructTag(e)}function q(e){const c=(e||0)/Math.pow(10,27);return c>Math.pow(10,5)?1/0:c}new R.SuiPriceServiceConnection("https://hermes.pyth.network",{timeout:2e4});const re=27,N=(e,c)=>{if(!Number(e)||!Number(c))return new T(0);const r=new T(1).shiftedBy(1*re),t=r.multipliedBy(new T(.5));return new T(e).multipliedBy(new T(c)).plus(t).dividedBy(r).integerValue(T.ROUND_DOWN)},ne="1.3.0-dev.7",se={version:ne},y=I(j(async e=>{const c=`https://open-api.naviprotocol.io/api/navi/config?env=${(e==null?void 0:e.env)||"prod"}&sdk=${se.version}`;return(await fetch(c).then(t=>t.json())).data})),f=1e3*60*5;var P=(e=>(e[e.Supply=1]="Supply",e[e.Withdraw=2]="Withdraw",e[e.Borrow=3]="Borrow",e[e.Repay=4]="Repay",e))(P||{});const k=I(j(async e=>{const c=`https://open-api.naviprotocol.io/api/navi/pools?env=${(e==null?void 0:e.env)||"prod"}`;return(await fetch(c).then(t=>t.json())).data}));async function v(e,c){const r=await k({...c,cacheTime:f});if(typeof e=="object")return e;const t=r.find(a=>typeof e=="string"?p(a.suiCoinType)===p(e):typeof e=="number"?a.id===e:!1);if(!t)throw new Error("Pool not found");return t}const oe=I(j(async e=>(await fetch("https://open-api.naviprotocol.io/api/navi/stats").then(t=>t.json())).data)),ie=I(j(async e=>await fetch("https://open-api.naviprotocol.io/api/navi/fee").then(t=>t.json())));async function H(e,c,r,t){const a=await y({...t,cacheTime:f}),n=await v(c,t),s=typeof r=="object"&&r.$kind==="GasCoin";if(p(n.suiCoinType)===p("0x2::sui::SUI")&&s){if(!(t!=null&&t.amount))throw new Error("Amount is required for sui coin");r=e.splitCoins(r,[t.amount])}let o;return typeof(t==null?void 0:t.amount)!="undefined"?o=d(t.amount,e.pure.u64):o=e.moveCall({target:"0x2::coin::value",arguments:[d(r,e.object)],typeArguments:[n.suiCoinType]}),t!=null&&t.accountCap?e.moveCall({target:`${a.package}::incentive_v3::deposit_with_account_cap`,arguments:[e.object("0x06"),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),d(r,e.object),e.object(a.incentiveV2),e.object(a.incentiveV3),d(t.accountCap,e.object)],typeArguments:[n.suiCoinType]}):e.moveCall({target:`${a.package}::incentive_v3::entry_deposit`,arguments:[e.object("0x06"),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),d(r,e.object),o,e.object(a.incentiveV2),e.object(a.incentiveV3)],typeArguments:[n.suiCoinType]}),a.version===2&&n.id===0&&e.moveCall({target:`${a.package}::pool::refresh_stake`,arguments:[e.object(n.contract.pool),e.object("0x05")]}),e}async function ue(e,c,r,t){const a=await y({...t,cacheTime:f}),n=await v(c,t),s=d(r,e.pure.u64);let o;if(a.version===1)if(t!=null&&t.accountCap){const[l]=e.moveCall({target:`${a.package}::incentive_v3::withdraw_with_account_cap`,arguments:[e.object("0x06"),e.object(a.priceOracle),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(a.incentiveV2),e.object(a.incentiveV3),d(t.accountCap,e.object)],typeArguments:[n.suiCoinType]});o=l}else{const[l]=e.moveCall({target:`${a.package}::incentive_v3::withdraw`,arguments:[e.object("0x06"),e.object(a.priceOracle),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(a.incentiveV2),e.object(a.incentiveV3)],typeArguments:[n.suiCoinType]});o=l}else if(t!=null&&t.accountCap){const[l]=e.moveCall({target:`${a.package}::incentive_v3::withdraw_with_account_cap_v2`,arguments:[e.object("0x06"),e.object(a.priceOracle),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(a.incentiveV2),e.object(a.incentiveV3),d(t.accountCap,e.object),e.object("0x05")],typeArguments:[n.suiCoinType]});o=l}else{const[l]=e.moveCall({target:`${a.package}::incentive_v3::withdraw_v2`,arguments:[e.object("0x06"),e.object(a.priceOracle),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(a.incentiveV2),e.object(a.incentiveV3),e.object("0x05")],typeArguments:[n.suiCoinType]});o=l}return e.moveCall({target:"0x2::coin::from_balance",arguments:[o],typeArguments:[n.suiCoinType]})}async function le(e,c,r,t){const a=await y({...t,cacheTime:f}),n=await v(c,t),s=d(r,e.pure.u64);let o;if(a.version===1)if(t!=null&&t.accountCap){const[l]=e.moveCall({target:`${a.package}::incentive_v3::borrow_with_account_cap`,arguments:[e.object("0x06"),e.object(a.priceOracle),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(a.incentiveV2),e.object(a.incentiveV3),d(t.accountCap,e.object)],typeArguments:[n.suiCoinType]});o=l}else{const[l]=e.moveCall({target:`${a.package}::incentive_v3::borrow`,arguments:[e.object("0x06"),e.object(a.priceOracle),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(a.incentiveV2),e.object(a.incentiveV3)],typeArguments:[n.suiCoinType]});o=l}else if(t!=null&&t.accountCap){const[l]=e.moveCall({target:`${a.package}::incentive_v3::borrow_with_account_cap_v2`,arguments:[e.object("0x06"),e.object(a.priceOracle),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(a.incentiveV2),e.object(a.incentiveV3),d(t.accountCap,e.object),e.object("0x05")],typeArguments:[n.suiCoinType]});o=l}else{const[l]=e.moveCall({target:`${a.package}::incentive_v3::borrow_v2`,arguments:[e.object("0x06"),e.object(a.priceOracle),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),s,e.object(a.incentiveV2),e.object(a.incentiveV3),e.object("0x05")],typeArguments:[n.suiCoinType]});o=l}return e.moveCall({target:"0x2::coin::from_balance",arguments:[e.object(o)],typeArguments:[n.suiCoinType]})}async function de(e,c,r,t){const a=await y({...t,cacheTime:f}),n=await v(c,t),s=typeof r=="object"&&r.$kind==="GasCoin";if(p(n.suiCoinType)===p("0x2::sui::SUI")&&s){if(!(t!=null&&t.amount))throw new Error("Amount is required for sui coin");r=e.splitCoins(r,[t.amount])}let o;return typeof(t==null?void 0:t.amount)!="undefined"?o=d(t.amount,e.pure.u64):o=e.moveCall({target:"0x2::coin::value",arguments:[d(r,e.object)],typeArguments:[n.suiCoinType]}),t!=null&&t.accountCap?e.moveCall({target:`${a.package}::incentive_v3::repay_with_account_cap`,arguments:[e.object("0x06"),e.object(a.priceOracle),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),d(r,e.object),o,e.object(a.incentiveV2),e.object(a.incentiveV3),d(t.accountCap,e.object)],typeArguments:[n.suiCoinType]}):e.moveCall({target:`${a.package}::incentive_v3::entry_repay`,arguments:[e.object("0x06"),e.object(a.priceOracle),e.object(a.storage),e.object(n.contract.pool),e.pure.u8(n.id),d(r,e.object),o,e.object(a.incentiveV2),e.object(a.incentiveV3)],typeArguments:[n.suiCoinType]}),e}const be=I(j(async e=>{var a;const c=await y({...e});if(e!=null&&e.address&&typeof(e==null?void 0:e.asset)!="undefined")try{const n=await v(e.asset,e),s=(a=e==null?void 0:e.client)!=null?a:A,o=new B.Transaction;o.moveCall({target:`${c.package}::incentive_v3::get_borrow_fee_v2`,arguments:[o.object(c.incentiveV3),o.pure.address(e.address),o.pure.u8(n.id),o.pure.u64(1e4)],typeArguments:[]});const u=await s.devInspectTransactionBlock({transactionBlock:o,sender:e.address}),l=$(u,[h.bcs.u64()]);return(Number(l[0])||0)/100}catch(n){console.error(n)}const t=(await A.getObject({id:c.incentiveV3,options:{showType:!0,showOwner:!0,showContent:!0}})).data.content.fields.borrow_fee_rate;return Number(t)/100}));function ge(e,c,r){const t=typeof(r==null?void 0:r.balance)=="number",a=t?r.balance:0;let n=0;const s=[];let o="";if(c.sort((l,b)=>Number(b.balance)-Number(l.balance)).forEach(l=>{if(!(t&&n>=a)&&Number(l.balance)!==0){if(o||(o=l.coinType),o!==l.coinType)throw new Error("All coins must be of the same type");n+=Number(l.balance),s.push(l.coinObjectId)}}),s.length===0)throw new Error("No coins to merge");if(t&&n<a)throw new Error(`Balance is less than the specified balance: ${n} < ${a}`);if(p(o)===p("0x2::sui::SUI")&&(r!=null&&r.useGasCoin))return t?e.splitCoins(e.gas,[e.pure.u64(a)]):e.gas;const u=s.length===1?e.object(s[0]):e.mergeCoins(s[0],s.slice(1));return t?e.splitCoins(u,[e.pure.u64(a)]):u}async function L(e,c,r,t,a,n,s){const o=await y({...s,cacheTime:f}),u=await v(r,s);return e.moveCall({target:`${o.uiGetter}::calculator_unchecked::dynamic_health_factor`,arguments:[e.object("0x06"),e.object(o.storage),e.object(o.oracle.priceOracle),ae(e,u),d(c,e.pure.address),d(u.id,e.pure.u8),d(t,e.pure.u64),d(a,e.pure.u64),d(n,e.pure.bool)],typeArguments:[u.suiCoinType]})}async function O(e,c,r){return L(e,c,0,0,0,!1,r)}const pe=I(async(e,c)=>{var l;const r=await y({...c,cacheTime:f}),t=new B.Transaction,a=(l=c==null?void 0:c.client)!=null?l:A,n=await k(c);t.moveCall({target:`${r.uiGetter}::getter_unchecked::get_user_state`,arguments:[t.object(r.storage),t.pure.address(e)]});const s=await a.devInspectTransactionBlock({transactionBlock:t,sender:e}),o=$(s,[h.bcs.vector(D)]);return S(o[0].filter(b=>b.supply_balance!=="0"||b.borrow_balance!=="0")).map(b=>{const w=n.find(C=>C.id===b.assetId),g=N(b.supplyBalance,w.currentSupplyIndex).toString(),m=N(b.borrowBalance,w.currentBorrowIndex).toString();return{...b,supplyBalance:g,borrowBalance:m,pool:w}}).filter(b=>!!b.pool)});async function fe(e,c){var s;const r=(s=c==null?void 0:c.client)!=null?s:A,t=new B.Transaction;await O(t,e,c);const a=await r.devInspectTransactionBlock({transactionBlock:t,sender:e}),n=$(a,[h.bcs.u256()]);return q(Number(n[0])||0)}async function me(e,c,r,t){var g;const a=(g=t==null?void 0:t.client)!=null?g:A,n=new B.Transaction;let s=0,o=0;const u=await v(c,t);if(r.forEach(m=>{m.type===P.Supply?s+=m.amount:m.type===P.Withdraw?s-=m.amount:m.type===P.Borrow?o+=m.amount:m.type===P.Repay&&(o-=m.amount)}),s*o<0)throw new Error("Invalid operations");const l=s>0||o>0;await L(n,e,u,Math.abs(s),Math.abs(o),l,t);const b=await a.devInspectTransactionBlock({transactionBlock:n,sender:e}),w=$(b,[h.bcs.u256()]);return q(Number(w[0])||0)}const ye=j(async(e,c)=>{const r=new URLSearchParams;c!=null&&c.cursor&&r.set("cursor",c.cursor),r.set("userAddress",e);const t=`https://open-api.naviprotocol.io/api/navi/user/transactions?${r.toString()}`;return(await fetch(t).then(n=>n.json())).data});async function we(e,c){var n;let r=null;const t=[],a=(n=c==null?void 0:c.client)!=null?n:A;do{let s;if(c!=null&&c.coinType?s=await a.getCoins({owner:e,coinType:c==null?void 0:c.coinType,cursor:r,limit:100}):s=await a.getAllCoins({owner:e,cursor:r,limit:100}),!s.data||!s.data.length)break;t.push(...s.data),r=s.nextCursor}while(r);return t}const G=new R.SuiPriceServiceConnection("https://hermes.pyth.network",{timeout:1e4});async function z(e){try{const c=[],r=await G.getLatestPriceFeeds(e);if(!r)return c;const t=Math.floor(new Date().valueOf()/1e3);for(const a of r){const n=a.getPriceUnchecked();if(n.publishTime>t){console.warn(`pyth price feed is invalid, id: ${a.id}, publish time: ${n.publishTime}, current timestamp: ${t}`);continue}t-a.getPriceUnchecked().publishTime>30&&(console.info(`stale price feed, id: ${a.id}, publish time: ${n.publishTime}, current timestamp: ${t}`),c.push(a.id))}return c}catch(c){throw new Error(`failed to get pyth stale price feed id, msg: ${c.message}`)}}async function W(e,c,r){var n;const t=(n=r==null?void 0:r.client)!=null?n:A,a=await y({...r,cacheTime:f});try{const s=await G.getPriceFeedsUpdateData(c);return await new R.SuiPythClient(t,a.oracle.pythStateId,a.oracle.wormholeStateId).updatePriceFeeds(e,s,c)}catch(s){throw new Error(`failed to update pyth price feeds, msg: ${s.message}`)}}async function he(e,c,r){const t=await y({...r,cacheTime:f});if(r!=null&&r.updatePythPriceFeeds){const a=c.filter(n=>!!n.pythPriceFeedId).map(n=>n.pythPriceFeedId);try{const n=await z(a);n.length>0&&await W(e,n,r)}catch{}}for(const a of c)e.moveCall({target:`${t.oracle.packageId}::oracle_pro::update_single_price`,arguments:[e.object("0x6"),e.object(t.oracle.oracleConfig),e.object(t.oracle.priceOracle),e.object(t.oracle.supraOracleHolder),e.object(a.pythPriceInfoObject),e.pure.address(a.feedId)]});return e}async function J(e){return(await y({...e,cacheTime:f})).oracle.feeds}function ve(e,c){return e.filter(r=>!!(c!=null&&c.lendingState&&c.lendingState.find(a=>a.assetId===r.assetId)||c!=null&&c.pools&&c.pools.find(a=>a.id===r.assetId)))}const F=I(j(async e=>{const c=`https://open-api.naviprotocol.io/api/navi/flashloan?env=${(e==null?void 0:e.env)||"prod"}`,r=await fetch(c).then(t=>t.json());return Object.keys(r.data).map(t=>({...r.data[t],coinType:t}))}));async function Ce(e,c){return(await F(c)).find(t=>typeof e=="string"?p(t.coinType)===p(e):typeof e=="number"?t.assetId===e:t.assetId===e.id)||null}async function je(e,c,r,t){const a=await y({...t,cacheTime:f}),n=await v(c,t);if(!(await F({...t,cacheTime:f})).some(u=>p(u.coinType)===p(n.suiCoinType)))throw new Error("Pool does not support flashloan");if(a.version===1){const[u,l]=e.moveCall({target:`${a.package}::lending::flash_loan_with_ctx`,arguments:[e.object(a.flashloanConfig),e.object(n.contract.pool),d(r,e.pure.u64)],typeArguments:[n.suiCoinType]});return[u,l]}else{const[u,l]=e.moveCall({target:`${a.package}::lending::flash_loan_with_ctx_v2`,arguments:[e.object(a.flashloanConfig),e.object(n.contract.pool),d(r,e.pure.u64),e.object("0x05")],typeArguments:[n.suiCoinType]});return[u,l]}}async function _e(e,c,r,t,a){const n=await y({...a,cacheTime:f}),s=await v(c,a);if(!(await F({...a,cacheTime:f})).some(b=>p(b.coinType)===p(s.suiCoinType)))throw new Error("Pool does not support flashloan");const[l]=e.moveCall({target:`${n.package}::lending::flash_repay_with_ctx`,arguments:[e.object("0x06"),e.object(n.storage),e.object(s.contract.pool),d(r,e.object),d(t,e.object)],typeArguments:[s.suiCoinType]});return[l]}async function Te(e,c,r,t,a,n){const s={...n,cacheTime:f},o=await y(s),u=await v(c,s),l=await v(t,s);if(o.version===1){const[b,w]=e.moveCall({target:`${o.package}::incentive_v3::liquidation`,arguments:[e.object("0x06"),e.object(o.priceOracle),e.object(o.storage),e.pure.u8(u.id),e.object(u.contract.pool),d(r,e.object),e.pure.u8(l.id),e.object(l.contract.pool),d(a,e.pure.address),e.object(o.incentiveV2),e.object(o.incentiveV3)],typeArguments:[u.suiCoinType,l.suiCoinType]});return[b,w]}else{const[b,w]=e.moveCall({target:`${o.package}::incentive_v3::liquidation_v2`,arguments:[e.object("0x06"),e.object(o.priceOracle),e.object(o.storage),e.pure.u8(u.id),e.object(u.contract.pool),d(r,e.object),e.pure.u8(l.id),e.object(l.contract.pool),d(a,e.pure.address),e.object(o.incentiveV2),e.object(o.incentiveV3),e.object("0x05")],typeArguments:[u.suiCoinType,l.suiCoinType]});return[b,w]}}async function Ae(e,c){var b;const r=await J(c),t=await k({...c,cacheTime:f}),a=(b=c==null?void 0:c.client)!=null?b:A,n=await y({...c,cacheTime:f}),s=new B.Transaction;s.moveCall({target:`${n.uiGetter}::incentive_v3_getter::get_user_atomic_claimable_rewards`,arguments:[s.object("0x06"),s.object(n.storage),s.object(n.incentiveV3),s.pure.address(e)]});const o=await a.devInspectTransactionBlock({transactionBlock:s,sender:e}),u=$(o,[h.bcs.vector(h.bcs.string()),h.bcs.vector(h.bcs.string()),h.bcs.vector(h.bcs.u8()),h.bcs.vector(h.bcs.Address),h.bcs.vector(h.bcs.u256())]),l=[];if(u.length===5&&Array.isArray(u[0])){const w=u[0].length;for(let g=0;g<w;g++){const m=r.find(_=>p(_.coinType)===p(u[1][g])),C=t.find(_=>p(_.coinType)===p(u[0][g]));!m||!C||l.push({assetId:C.id,assetCoinType:p(u[0][g]),rewardCoinType:p(u[1][g]),option:Number(u[2][g]),userClaimableReward:Number(u[4][g])/Math.pow(10,m.priceDecimal),ruleIds:Array.isArray(u[3][g])?u[3][g]:[u[3][g]]})}}return l}function Ie(e){const c=new Map;e.forEach(t=>{const a=t.assetId,n=t.option,s=`${a}-${n}-${t.rewardCoinType}`;c.has(s)?c.get(s).total+=t.userClaimableReward:c.set(s,{assetId:a,rewardType:n,coinType:t.rewardCoinType,total:Number(t.userClaimableReward)})});const r=new Map;for(const{assetId:t,rewardType:a,coinType:n,total:s}of c.values()){const o=`${t}-${a}`;r.has(o)||r.set(o,{assetId:t,rewardType:a,rewards:new Map});const u=r.get(o);u.rewards.set(n,(u.rewards.get(n)||0)+s)}return Array.from(r.values()).map(t=>({assetId:t.assetId,rewardType:t.rewardType,rewards:Array.from(t.rewards.entries()).map(([a,n])=>({coinType:a,available:n.toFixed(6)}))}))}const Pe=j(async e=>{const c=`https://open-api.naviprotocol.io/api/navi/user/total_claimed_reward?userAddress=${e}`;return(await fetch(c).then(t=>t.json())).data}),Be=j(async(e,c)=>{const r=`https://open-api.naviprotocol.io/api/navi/user/rewards?userAddress=${e}&page=${(c==null?void 0:c.page)||1}&pageSize=${(c==null?void 0:c.size)||400}`,t=await fetch(r).then(a=>a.json());return S({data:t.data.rewards})});async function $e(e,c,r){var o;const t=await y({...r,cacheTime:f}),a=await k({...r,cacheTime:f}),n=new Map;for(const u of c){const{rewardCoinType:l,ruleIds:b}=u;for(const w of b){n.has(l)||n.set(l,{assetIds:[],ruleIds:[],amount:0});const g=n.get(l);g.assetIds.push(u.assetCoinType.replace("0x","")),g.ruleIds.push(w),g.amount+=u.userClaimableReward}}const s=[];for(const[u,{assetIds:l,ruleIds:b,amount:w}]of n){const g=a.find(C=>p(C.suiCoinType)===p(u));if(!g||!g.contract.rewardFundId)throw new Error(`No matching rewardFund found for reward coin: ${u}`);const m=g.contract.rewardFundId;if(r!=null&&r.accountCap&&!r.customCoinReceive)throw new Error("customCoinReceive is required when accountCap is provided");if(r!=null&&r.customCoinReceive){let C;r.accountCap?C=e.moveCall({target:`${t.package}::incentive_v3::claim_reward_with_account_cap`,arguments:[e.object("0x06"),e.object(t.incentiveV3),e.object(t.storage),e.object(m),e.pure.vector("string",l),e.pure.vector("address",b),d(r.accountCap,e.object)],typeArguments:[u]}):C=e.moveCall({target:`${t.package}::incentive_v3::claim_reward`,arguments:[e.object("0x06"),e.object(t.incentiveV3),e.object(t.storage),e.object(m),e.pure.vector("string",l),e.pure.vector("address",b)],typeArguments:[u]});const[_]=e.moveCall({target:"0x2::coin::from_balance",arguments:[C],typeArguments:[u]});if((r==null?void 0:r.customCoinReceive.type)==="transfer"){if(!r.customCoinReceive.transfer)throw new Error("customCoinReceive.transfer is required");e.transferObjects([_],d(r.customCoinReceive.transfer,e.pure.address))}if((r==null?void 0:r.customCoinReceive.type)==="depositNAVI"){const Y=T(g.totalSupplyAmount).shiftedBy(-9),K=T(g.supplyCapCeiling).shiftedBy(-27);Y.plus(w).isGreaterThan(K)&&((o=r==null?void 0:r.customCoinReceive.depositNAVI)!=null&&o.fallbackReceiveAddress)?e.transferObjects([_],e.pure.address(r.customCoinReceive.depositNAVI.fallbackReceiveAddress)):await H(e,g,_,r)}else s.push({coin:_,identifier:g})}else e.moveCall({target:`${t.package}::incentive_v3::claim_reward_entry`,arguments:[e.object("0x06"),e.object(t.incentiveV3),e.object(t.storage),e.object(m),e.pure.vector("string",l),e.pure.vector("address",b)],typeArguments:[u]})}return s}async function ke(e,c){const r=await y({...c});return e.moveCall({target:`${r.package}::lending::create_account`,arguments:[]})}exports.Address=V;exports.DEFAULT_CACHE_TIME=f;exports.FlashLoanAssetConfig=te;exports.IncentiveAPYInfo=Z;exports.IncentivePoolInfo=U;exports.IncentivePoolInfoByPhase=x;exports.OracleInfo=ee;exports.PoolOperator=P;exports.ReserveDataInfo=ce;exports.UserStateInfo=D;exports.borrowCoinPTB=le;exports.claimLendingRewardsPTB=$e;exports.createAccountCapPTB=ke;exports.depositCoinPTB=H;exports.filterPriceFeeds=ve;exports.flashloanPTB=je;exports.getAllFlashLoanAssets=F;exports.getBorrowFee=be;exports.getCoins=we;exports.getConfig=y;exports.getFees=ie;exports.getFlashLoanAsset=Ce;exports.getHealthFactor=fe;exports.getHealthFactorPTB=O;exports.getLendingState=pe;exports.getPool=v;exports.getPools=k;exports.getPriceFeeds=J;exports.getPythStalePriceFeedId=z;exports.getSimulatedHealthFactor=me;exports.getSimulatedHealthFactorPTB=L;exports.getStats=oe;exports.getTransactions=ye;exports.getUserAvailableLendingRewards=Ae;exports.getUserClaimedRewardHistory=Be;exports.getUserTotalClaimedReward=Pe;exports.liquidatePTB=Te;exports.mergeCoinsPTB=ge;exports.normalizeCoinType=p;exports.parseTxValue=d;exports.repayCoinPTB=de;exports.repayFlashLoanPTB=_e;exports.summaryLendingRewards=Ie;exports.updateOraclePricesPTB=he;exports.updatePythPriceFeeds=W;exports.withCache=I;exports.withSingleton=j;exports.withdrawCoinPTB=ue;
|
|
2
2
|
//# sourceMappingURL=index.cjs.js.map
|