@naviprotocol/lending 1.3.0-dev.2 → 1.3.0-dev.3
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.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +289 -264
- package/dist/index.esm.js.map +1 -1
- package/dist/pool.d.ts +17 -4
- package/dist/pool.d.ts.map +1 -1
- package/dist/types.d.ts +4 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const F=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())}),N=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(N)}),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()}),P=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 I(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 S(e){return Array.isArray(e)?e.map(a=>S(a)):e!=null&&typeof e=="object"?Object.keys(e).reduce((a,c)=>({...a,[Q(c)]:S(e[c])}),{}):e}function p(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 $(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 f(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,U=(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=I(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})),g=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 B=I(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 B({...a,cacheTime:g});if(typeof e=="object")return e;const t=c.find(r=>typeof e=="string"?f(r.suiCoinType)===f(e):typeof e=="number"?r.id===e:!1);if(!t)throw new Error("Pool not found");return t}const ne=I(T(async e=>(await fetch("https://open-api.naviprotocol.io/api/navi/stats").then(t=>t.json())).data)),se=I(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:g}),n=await v(a,t),s=typeof c=="object"&&c.$kind==="GasCoin";if(f(n.suiCoinType)===f("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=p(t.amount,e.pure.u64):i=e.moveCall({target:"0x2::coin::value",arguments:[p(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),p(c,e.object),e.object(r.incentiveV2),e.object(r.incentiveV3),p(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),p(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:g}),n=await v(a,t),s=p(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),p(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:g}),n=await v(a,t),s=p(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),p(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:g}),n=await v(a,t),s=typeof c=="object"&&c.$kind==="GasCoin";if(f(n.suiCoinType)===f("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=p(t.amount,e.pure.u64):i=e.moveCall({target:"0x2::coin::value",arguments:[p(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),p(c,e.object),i,e.object(r.incentiveV2),e.object(r.incentiveV3),p(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),p(c,e.object),i,e.object(r.incentiveV2),e.object(r.incentiveV3)],typeArguments:[n.suiCoinType]}),e}const le=I(T(async e=>{const a=await y({...e}),t=(await P.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(f(i)===f("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:g}),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),p(a,e.pure.address),p(u.id,e.pure.u8),p(t,e.pure.u64),p(r,e.pure.u64),p(n,e.pure.bool)],typeArguments:[u.suiCoinType]})}async function O(e,a,c){return L(e,a,0,0,0,!1,c)}const be=I(async(e,a)=>{var l;const c=await y({...a,cacheTime:g}),t=new F.Transaction,r=(l=a==null?void 0:a.client)!=null?l:P,n=await B(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,[w.bcs.vector(D)]);return S(i[0].filter(d=>d.supply_balance!=="0"||d.borrow_balance!=="0")).map(d=>{const h=n.find(C=>C.id===d.assetId),b=U(d.supplyBalance,h.currentSupplyIndex).toString(),m=U(d.borrowBalance,h.currentBorrowIndex).toString();return{...d,supplyBalance:b,borrowBalance:m,pool:h}}).filter(d=>!!d.pool)});async function pe(e,a){var s;const c=(s=a==null?void 0:a.client)!=null?s:P,t=new F.Transaction;await O(t,e,a);const r=await c.devInspectTransactionBlock({transactionBlock:t,sender:e}),n=$(r,[w.bcs.u256()]);return q(Number(n[0])||0)}async function fe(e,a,c,t){var b;const r=(b=t==null?void 0:t.client)!=null?b:P,n=new F.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=$(d,[w.bcs.u256()]);return q(Number(h[0])||0)}const ge=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:P;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:P,r=await y({...c,cacheTime:g});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:g});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:g})).oracle.feeds}function he(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=I(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 we(e,a){return(await k(a)).find(t=>typeof e=="string"?f(t.coinType)===f(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:g}),n=await v(a,t);if(!(await k({...t,cacheTime:g})).some(d=>f(d.coinType)===f(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),p(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:g}),s=await v(a,r);if(!(await k({...r,cacheTime:g})).some(d=>f(d.coinType)===f(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),p(c,e.object),p(t,e.object)],typeArguments:[s.suiCoinType]});return[l]}async function Te(e,a,c,t,r,n){const s={...n,cacheTime:g},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),p(c,e.object),e.pure.u8(l.id),e.object(l.contract.pool),p(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 B({...a,cacheTime:g}),r=(d=a==null?void 0:a.client)!=null?d:P,n=await y({...a,cacheTime:g}),s=new F.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=$(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(_=>f(_.coinType)===f(u[1][b])),C=t.find(_=>f(_.coinType)===f(u[0][b]));!m||!C||l.push({assetId:C.id,assetCoinType:f(u[0][b]),rewardCoinType:f(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 S({data:t.data.rewards})});async function Ae(e,a,c){var i;const t=await y({...c,cacheTime:g}),r=await B({...c,cacheTime:g}),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=>f(C.suiCoinType)===f(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),p(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([_],p(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=g;exports.FlashLoanAssetConfig=te;exports.IncentiveAPYInfo=Z;exports.IncentivePoolInfo=N;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=he;exports.flashloanPTB=ve;exports.getAllFlashLoanAssets=k;exports.getBorrowFee=le;exports.getCoins=me;exports.getConfig=y;exports.getFees=se;exports.getFlashLoanAsset=we;exports.getHealthFactor=pe;exports.getHealthFactorPTB=O;exports.getLendingState=be;exports.getPool=v;exports.getPools=B;exports.getPriceFeeds=Y;exports.getPythStalePriceFeedId=z;exports.getSimulatedHealthFactor=fe;exports.getSimulatedHealthFactorPTB=L;exports.getStats=ne;exports.getTransactions=ge;exports.getUserAvailableLendingRewards=_e;exports.getUserClaimedRewardHistory=Pe;exports.getUserTotalClaimedReward=Ie;exports.liquidatePTB=Te;exports.mergeCoinsPTB=de;exports.normalizeCoinType=f;exports.parseTxValue=p;exports.repayCoinPTB=ue;exports.repayFlashLoanPTB=Ce;exports.summaryLendingRewards=je;exports.updateOraclePricesPTB=ye;exports.updatePythPriceFeeds=W;exports.withCache=I;exports.withSingleton=T;exports.withdrawCoinPTB=oe;
|
|
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("0x06")]}),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&&(e!=null&&e.asset))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:[n.suiCoinType]});const u=await s.devInspectTransactionBlock({transactionBlock:i,sender:e.address}),l=S(u,[w.bcs.u64()]);return(Number(l[0])||0)/1e4}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;
|
|
2
2
|
//# sourceMappingURL=index.cjs.js.map
|