@swapkit/toolboxes 4.20.0 → 4.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/dist/{chunk-r2htb2ce.js → chunk-x9db9hmg.js} +1 -1
- package/dist/chunk-xs0j1eze.js +2 -0
- package/dist/src/evm/index.cjs +1 -1
- package/dist/src/evm/index.js +1 -1
- package/dist/src/hypercore/index.cjs +1 -1
- package/dist/src/hypercore/index.js +1 -1
- package/dist/src/index.cjs +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/ton/index.cjs +1 -1
- package/dist/src/ton/index.js +1 -1
- package/dist/src/tron/index.cjs +1 -1
- package/dist/src/tron/index.js +1 -1
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +90 -5
- package/dist/types/evm/toolbox/evm.d.ts +1584 -88
- package/dist/types/evm/toolbox/op.d.ts +72 -4
- package/dist/types/evm/types.d.ts +7 -7
- package/dist/types/ton/toolbox.d.ts +5 -5
- package/dist/types/ton/types.d.ts +5 -0
- package/dist/types/tron/index.d.ts +2 -2
- package/dist/types/tron/toolbox.d.ts +3 -1
- package/dist/types/tron/types.d.ts +5 -0
- package/package.json +2 -2
- package/dist/chunk-4q573g5z.js +0 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
import"../../chunk-44m22jy1.js";import{q as V}from"../../chunk-4q573g5z.js";import{da as E}from"../../chunk-54gntgt1.js";import"../../chunk-ntbvmmch.js";function Me({builder:e,grouping:r="na",orders:t}){return{builder:e,grouping:r,orders:t,type:"order"}}function ke({cancels:e}){return{cancels:e,type:"cancel"}}function Fe({cancels:e}){return{cancels:e,type:"cancelByCloid"}}function ve({oid:e,order:r}){return{oid:e,order:r,type:"modify"}}function Le({modifies:e}){return{modifies:e,type:"batchModify"}}function Ne({asset:e,isCross:r,leverage:t}){return{asset:e,isCross:r,leverage:t,type:"updateLeverage"}}function je({asset:e,isBuy:r,ntli:t}){return{asset:e,isBuy:r,ntli:t,type:"updateIsolatedMargin"}}function $e({asset:e,leverage:r}){return{asset:e,leverage:r,type:"topUpIsolatedOnlyMargin"}}function ze({time:e}){return{time:e,type:"scheduleCancel"}}function Qe({amount:e,hyperliquidChain:r,nonce:t,signatureChainId:n,toPerp:i}){return{amount:e,hyperliquidChain:r,nonce:t,signatureChainId:n,toPerp:i,type:"usdClassTransfer"}}function We({amount:e,destination:r,destinationDex:t,fromSubAccount:n,hyperliquidChain:i,nonce:o,signatureChainId:a,sourceDex:p,token:g}){return{amount:e,destination:r,destinationDex:t,fromSubAccount:n,hyperliquidChain:i,nonce:o,signatureChainId:a,sourceDex:p,token:g,type:"sendAsset"}}function Ee({agentAddress:e,agentName:r,hyperliquidChain:t,nonce:n,signatureChainId:i}){return{agentAddress:e,agentName:r,hyperliquidChain:t,nonce:n,signatureChainId:i,type:"approveAgent"}}function Ve({builder:e,hyperliquidChain:r,maxFeeRate:t,nonce:n,signatureChainId:i}){return{builder:e,hyperliquidChain:r,maxFeeRate:t,nonce:n,signatureChainId:i,type:"approveBuilderFee"}}function Ge({hyperliquidChain:e,isUndelegate:r,nonce:t,signatureChainId:n,validator:i,wei:o}){return{hyperliquidChain:e,isUndelegate:r,nonce:t,signatureChainId:n,type:"tokenDelegate",validator:i,wei:o}}import{AssetValue as R,Chain as ce,derivationPathToString as de,getChainConfig as G,getRPCUrl as ue,NetworkDerivationPath as ge,RequestClient as A,SwapKitError as m,updateDerivationPath as le}from"@swapkit/helpers";import{HDNodeWallet as Ce}from"ethers";import{match as fe,P as O}from"ts-pattern";import{SwapKitError as w}from"@swapkit/helpers";import{keccak256 as Z,Signature as ee}from"ethers";var M="0x0000000000000000000000000000000000000000",re={chainId:1337,name:"Exchange",verifyingContract:M,version:"1"},te={Agent:[{name:"source",type:"string"},{name:"connectionId",type:"bytes32"}]},k="Mainnet",ne={approveAgent:[{name:"hyperliquidChain",type:"string"},{name:"agentAddress",type:"address"},{name:"agentName",type:"string"},{name:"nonce",type:"uint64"}],approveBuilderFee:[{name:"hyperliquidChain",type:"string"},{name:"maxFeeRate",type:"string"},{name:"builder",type:"address"},{name:"nonce",type:"uint64"}],sendAsset:[{name:"hyperliquidChain",type:"string"},{name:"destination",type:"string"},{name:"sourceDex",type:"string"},{name:"destinationDex",type:"string"},{name:"token",type:"string"},{name:"amount",type:"string"},{name:"fromSubAccount",type:"string"},{name:"nonce",type:"uint64"}],tokenDelegate:[{name:"hyperliquidChain",type:"string"},{name:"validator",type:"address"},{name:"wei",type:"uint64"},{name:"isUndelegate",type:"bool"},{name:"nonce",type:"uint64"}],usdClassTransfer:[{name:"hyperliquidChain",type:"string"},{name:"amount",type:"string"},{name:"toPerp",type:"bool"},{name:"nonce",type:"uint64"}]},ie={approveAgent:"HyperliquidTransaction:ApproveAgent",approveBuilderFee:"HyperliquidTransaction:ApproveBuilderFee",sendAsset:"HyperliquidTransaction:SendAsset",tokenDelegate:"HyperliquidTransaction:TokenDelegate",usdClassTransfer:"HyperliquidTransaction:UsdClassTransfer"},oe=new TextEncoder;function y(...e){let r=e.reduce((i,o)=>i+o.length,0),t=new Uint8Array(r),n=0;for(let i of e)t.set(i,n),n+=i.length;return t}function se(e,r,t){if(t<r)return new Uint8Array([e+t]);if(t<=255)return new Uint8Array([e+r,t]);if(t<=65535)return new Uint8Array([e+r+1,t>>8,t&255]);return new Uint8Array([e+r+2,t>>>24&255,t>>>16&255,t>>>8&255,t&255])}function C(e,r){let t=new Uint8Array(r),n=e;for(let i=r-1;i>=0;i-=1)t[i]=Number(n&0xffn),n>>=8n;return t}function _(e){if(e>=0n){if(e<=0x7fn)return new Uint8Array([Number(e)]);if(e<=0xffn)return new Uint8Array([204,Number(e)]);if(e<=0xffffn)return y(new Uint8Array([205]),C(e,2));if(e<=0xffffffffn)return y(new Uint8Array([206]),C(e,4));return y(new Uint8Array([207]),C(e,8))}if(e>=-32n)return new Uint8Array([256+Number(e)]);if(e>=-128n)return new Uint8Array([208,256+Number(e)]);if(e>=-32768n)return y(new Uint8Array([209]),C(BigInt.asUintN(16,e),2));if(e>=-2147483648n)return y(new Uint8Array([210]),C(BigInt.asUintN(32,e),4));return y(new Uint8Array([211]),C(BigInt.asUintN(64,e),8))}function F(e){let r=oe.encode(e);if(r.length<32)return y(new Uint8Array([160+r.length]),r);return y(se(217,0,r.length),r)}function ae(e){let r=e.map((n)=>D(n)),t=e.length<16?new Uint8Array([144+e.length]):e.length<=65535?new Uint8Array([220,e.length>>8,e.length&255]):new Uint8Array([221,e.length>>>24&255,e.length>>>16&255,e.length>>>8&255,e.length&255]);return y(t,...r)}function pe(e){let r=Object.entries(e).filter(([,n])=>typeof n<"u"),t=r.length<16?new Uint8Array([128+r.length]):r.length<=65535?new Uint8Array([222,r.length>>8,r.length&255]):new Uint8Array([223,r.length>>>24&255,r.length>>>16&255,r.length>>>8&255,r.length&255]);return y(t,...r.flatMap(([n,i])=>[F(n),D(i)]))}function D(e){if(e===null)return new Uint8Array([192]);if(typeof e==="boolean")return new Uint8Array([e?195:194]);if(typeof e==="string")return F(e);if(typeof e==="number"){if(!Number.isFinite(e)||!Number.isInteger(e))throw new w("helpers_invalid_params",{message:"Hyperliquid msgpack only supports integers"});return _(BigInt(e))}if(typeof e==="bigint")return _(e);if(e instanceof Uint8Array){let r=e.length<=255?new Uint8Array([196,e.length]):e.length<=65535?new Uint8Array([197,e.length>>8,e.length&255]):new Uint8Array([198,e.length>>>24&255,e.length>>>16&255,e.length>>>8&255,e.length&255]);return y(r,e)}if(Array.isArray(e))return ae(e);if(typeof e==="object")return pe(e);throw new w("helpers_invalid_params",{message:`Unsupported Hyperliquid msgpack value: ${String(e)}`})}function ye(e){let r=e.startsWith("0x")?e.slice(2):e;if(r.length%2!==0||/[^a-fA-F0-9]/u.test(r))throw new w("helpers_invalid_params",{hexValue:e});let t=new Uint8Array(r.length/2);for(let n=0;n<r.length;n+=2)t[n/2]=Number.parseInt(r.slice(n,n+2),16);return t}function v(e){return{domain:re,message:{connectionId:e,source:"a"},primaryType:"Agent",types:{Agent:[...te.Agent]}}}function L(e){let r=ie[e.type],t=ne[e.type],{type:n,...i}=e;return{domain:{chainId:Number.parseInt(i.signatureChainId||"0xa4b1",16),name:"HyperliquidSignTransaction",verifyingContract:M,version:"1"},message:{...i,hyperliquidChain:i.hyperliquidChain||k},primaryType:r,types:{[r]:t}}}function N(e){let r=ee.from(e);return{r:r.r,s:r.s,v:r.v}}function j(e,r,{expiresAfter:t,vaultAddress:n}={}){let i=D(e),o=C(BigInt(r),8),a=n?y(new Uint8Array([1]),ye(n.toLowerCase())):new Uint8Array([0]),p=typeof t==="number"?y(new Uint8Array([0]),C(BigInt(t),8)):new Uint8Array;return Z(y(i,o,a,p))}function $(e){let r=e.hyperliquidChain||k;switch(e.type){case"approveAgent":return{...e,agentAddress:e.agentAddress.toLowerCase(),hyperliquidChain:r};case"approveBuilderFee":return{...e,builder:e.builder.toLowerCase(),hyperliquidChain:r};case"sendAsset":return{...e,destination:e.destination.toLowerCase(),fromSubAccount:e.fromSubAccount.toLowerCase(),hyperliquidChain:r};case"tokenDelegate":return{...e,hyperliquidChain:r,validator:e.validator.toLowerCase()};case"usdClassTransfer":return{...e,hyperliquidChain:r};default:return e}}var u=ce.Hype,{baseDecimal:K}=G(u),Ae={HYPE:K,USDC:6,USDT:6},me="Mainnet",He="0xa4b1",he="0x0000000000000000000000000000000000000000",Te={spotSend:[{name:"hyperliquidChain",type:"string"},{name:"destination",type:"string"},{name:"token",type:"string"},{name:"amount",type:"string"},{name:"time",type:"uint64"}]},xe={spotSend:"HyperliquidTransaction:SpotSend"};function c(e,r){if(!Y(e))throw new m(r,{address:e});return e.toLowerCase()}async function f(e){let r=await S();return A.post(`${r}/info`,{json:e})}function be({dex:e,user:r}){return f({...e?{dex:e}:{},type:"clearinghouseState",user:c(r,"helpers_invalid_params")})}function Se({user:e}){return f({type:"spotClearinghouseState",user:c(e,"helpers_invalid_params")})}function Ue({dex:e,user:r}){return f({...e?{dex:e}:{},type:"openOrders",user:c(r,"helpers_invalid_params")})}function we({dex:e,user:r}){return f({...e?{dex:e}:{},type:"frontendOpenOrders",user:c(r,"helpers_invalid_params")})}function De({oidOrCloid:e,user:r}){return f({oid:e,type:"orderStatus",user:c(r,"helpers_invalid_params")})}function Oe({aggregateByTime:e,user:r}){return f({...typeof e==="boolean"?{aggregateByTime:e}:{},type:"userFills",user:c(r,"helpers_invalid_params")})}function Re({aggregateByTime:e,endTime:r,startTime:t,user:n}){return f({...typeof e==="boolean"?{aggregateByTime:e}:{},...typeof r==="number"?{endTime:r}:{},startTime:t,type:"userFillsByTime",user:c(n,"helpers_invalid_params")})}function z({coin:e,token:r,value:t,spotMeta:n}){if(!t||Number(t)<=0)return null;let i=e?.toUpperCase()||`TOKEN-${r||"UNKNOWN"}`,o=Ae[i]||K,a=n?.tokens?.find((g)=>g.name===e||r!==void 0&&g.index===Number(r)),p=a?`${u}.${i}-${a.name}:${a.tokenId}`:`${u}.${i}`;return new R({decimal:o,identifier:p,value:t})}function Be(e){let r=new Map;for(let t of e){let n=t.toString({includeSynthProtocol:!0}),i=r.get(n);r.set(n,i?i.add(t):t)}if(![...r.values()].some((t)=>t.isGasAsset||t.ticker==="HYPE")){let t=R.from({chain:u,value:0});r.set(t.toString({includeSynthProtocol:!0}),t)}return[...r.values()]}async function S(){try{return await ue(u)}catch{let[e]=G(u).rpcUrls;if(!e)throw new m("helpers_chain_no_public_or_set_rpc_url",{chain:u});return e}}async function B(){return`${await S()}/exchange`}function Pe(e){let r=xe[e.type],{type:t,...n}=e,i=Te[t];return{domain:{chainId:Number.parseInt(e.signatureChainId,16),name:"HyperliquidSignTransaction",verifyingContract:he,version:"1"},message:n,primaryType:r,types:{[r]:i}}}async function Ie({action:e,expiresAfter:r,nonce:t,vaultAddress:n}){let i=t||Date.now(),o=n?c(n,"helpers_invalid_params"):void 0,a=await B(),p=j(e,i,{expiresAfter:r,vaultAddress:o});return{action:e,endpoint:a,expiresAfter:r,nonce:i,typedData:v(p),vaultAddress:o}}async function qe({action:e,nonce:r,signatureChainId:t}){let n=r||e.nonce||Date.now(),i=t||e.signatureChainId||"0xa4b1",o=$({...e,nonce:n,signatureChainId:i});return{action:o,endpoint:await B(),nonce:n,typedData:L(o)}}async function Q({assetValue:e,recipient:r,sender:t,signatureChainId:n,time:i}){let o=c(r,"core_transaction_invalid_recipient_address"),a=c(t,"core_transaction_invalid_sender_address"),p=await B(),g=e.getValue("string"),T=i||Date.now(),x=e.address;if(!x)throw new m("helpers_invalid_params",{asset:e.toString(),from:a});let b={amount:g,destination:o,hyperliquidChain:me,signatureChainId:n||He,time:T,token:x,type:"spotSend"};return{action:b,endpoint:p,nonce:T,typedData:Pe(b)}}async function W(e){let r=await A.post(e.endpoint,{json:{action:e.action,...typeof e.expiresAfter==="number"?{expiresAfter:e.expiresAfter}:{},nonce:e.nonce,signature:e.signature,...e.vaultAddress?{vaultAddress:e.vaultAddress}:{}}});if(r.status!=="ok")throw new m("core_transaction_failed",{response:r});return r}async function _e(e,r){let t=await S(),n=r-5000;for(let i=0;i<5;i++){if(i>0)await new Promise((o)=>setTimeout(o,1000));try{let o=await A.post(`${t}/info`,{json:{startTime:n,type:"userNonFundingLedgerUpdates",user:e}}),a=Array.isArray(o)?o.find((p)=>p.time&&p.time>=r&&(p.delta?.type==="spotTransfer"||p.delta?.type==="internalTransfer")):void 0;if(a?.hash)return a.hash}catch{}}return""}function Y(e){return e.startsWith("0x")&&V(e)}function or(e){let r=E(u),t=e&&"index"in e?e.index||0:0,n=de(e&&"derivationPath"in e&&e.derivationPath?e.derivationPath:le(ge[u],{index:t})),i,o=!1;function a(){if(o)return i;return i=fe(e).with({phrase:O.string},({phrase:s})=>{return Ce.fromPhrase(s,void 0,n)}).with({signer:O.not(O.nullish)},({signer:s})=>s).otherwise(()=>{return}),o=!0,i}async function p(){let s=a();return s?await s.getAddress():""}async function g(s){let d=a();if(!d)throw new m("toolbox_hypercore_no_signer");let l=await d.signTypedData(s.typedData.domain,s.typedData.types,s.typedData.message);return{action:s.action,endpoint:s.endpoint,nonce:s.nonce,signature:N(l),..."expiresAfter"in s?{expiresAfter:s.expiresAfter}:{},..."vaultAddress"in s?{vaultAddress:s.vaultAddress}:{}}}async function T(s){let d=c(s,"helpers_invalid_params");try{let l=await S(),[H,h,P]=await Promise.all([A.post(`${l}/info`,{json:{type:"spotClearinghouseState",user:d}}),A.post(`${l}/info`,{json:{type:"clearinghouseState",user:d}}),A.post(`${l}/info`,{json:{type:"spotMeta"}})]),I=(H.spotState?.balances||H.balances||[]).map(({coin:U,token:J,total:X})=>z({coin:U,spotMeta:P,token:J,value:X})).filter((U)=>!!U),q=z({coin:"USDC",spotMeta:P,value:h.withdrawable||h.marginSummary?.accountValue||h.marginSummary?.totalRawUsd});return Be(q?[...I,q]:I)}catch{return r(d)}}async function x(s){let d=a();if(!d)throw new m("toolbox_hypercore_no_signer");let l=s.sender||await d.getAddress(),H=await Q({...s,feeRate:0,sender:l}),h=await g(H);return await W(h),_e(l,H.nonce)}function b(){return R.from({chain:u,value:0})}return{broadcastTransaction:W,createL1ActionRequest:Ie,createTransaction:Q,createUserSignedActionRequest:qe,estimateTransactionFee:b,getAddress:p,getBalance:T,getState:{clearinghouse:be,frontendOpenOrders:we,openOrders:Ue,orderStatus:De,spotClearinghouse:Se,userFills:Oe,userFillsByTime:Re},signTransaction:g,transfer:x,validateAddress:Y}}export{Y as validateHyperCoreAddress,or as getHyperCoreToolbox,qe as createUserSignedActionRequest,Q as createTransaction,Ie as createL1ActionRequest,Qe as buildUsdClassTransferAction,Ne as buildUpdateLeverageAction,je as buildUpdateIsolatedMarginAction,$e as buildTopUpIsolatedOnlyMarginAction,Ge as buildTokenDelegateAction,We as buildSendAssetAction,ze as buildScheduleCancelAction,Me as buildOrderAction,ve as buildModifyAction,Fe as buildCancelByCloidAction,ke as buildCancelAction,Le as buildBatchModifyAction,Ve as buildApproveBuilderFeeAction,Ee as buildApproveAgentAction};
|
|
1
|
+
import"../../chunk-44m22jy1.js";import{q as V}from"../../chunk-xs0j1eze.js";import{da as E}from"../../chunk-54gntgt1.js";import"../../chunk-ntbvmmch.js";function Me({builder:e,grouping:r="na",orders:t}){return{builder:e,grouping:r,orders:t,type:"order"}}function ke({cancels:e}){return{cancels:e,type:"cancel"}}function Fe({cancels:e}){return{cancels:e,type:"cancelByCloid"}}function ve({oid:e,order:r}){return{oid:e,order:r,type:"modify"}}function Le({modifies:e}){return{modifies:e,type:"batchModify"}}function Ne({asset:e,isCross:r,leverage:t}){return{asset:e,isCross:r,leverage:t,type:"updateLeverage"}}function je({asset:e,isBuy:r,ntli:t}){return{asset:e,isBuy:r,ntli:t,type:"updateIsolatedMargin"}}function $e({asset:e,leverage:r}){return{asset:e,leverage:r,type:"topUpIsolatedOnlyMargin"}}function ze({time:e}){return{time:e,type:"scheduleCancel"}}function Qe({amount:e,hyperliquidChain:r,nonce:t,signatureChainId:n,toPerp:i}){return{amount:e,hyperliquidChain:r,nonce:t,signatureChainId:n,toPerp:i,type:"usdClassTransfer"}}function We({amount:e,destination:r,destinationDex:t,fromSubAccount:n,hyperliquidChain:i,nonce:o,signatureChainId:a,sourceDex:p,token:g}){return{amount:e,destination:r,destinationDex:t,fromSubAccount:n,hyperliquidChain:i,nonce:o,signatureChainId:a,sourceDex:p,token:g,type:"sendAsset"}}function Ee({agentAddress:e,agentName:r,hyperliquidChain:t,nonce:n,signatureChainId:i}){return{agentAddress:e,agentName:r,hyperliquidChain:t,nonce:n,signatureChainId:i,type:"approveAgent"}}function Ve({builder:e,hyperliquidChain:r,maxFeeRate:t,nonce:n,signatureChainId:i}){return{builder:e,hyperliquidChain:r,maxFeeRate:t,nonce:n,signatureChainId:i,type:"approveBuilderFee"}}function Ge({hyperliquidChain:e,isUndelegate:r,nonce:t,signatureChainId:n,validator:i,wei:o}){return{hyperliquidChain:e,isUndelegate:r,nonce:t,signatureChainId:n,type:"tokenDelegate",validator:i,wei:o}}import{AssetValue as R,Chain as ce,derivationPathToString as de,getChainConfig as G,getRPCUrl as ue,NetworkDerivationPath as ge,RequestClient as A,SwapKitError as m,updateDerivationPath as le}from"@swapkit/helpers";import{HDNodeWallet as Ce}from"ethers";import{match as fe,P as O}from"ts-pattern";import{SwapKitError as w}from"@swapkit/helpers";import{keccak256 as Z,Signature as ee}from"ethers";var M="0x0000000000000000000000000000000000000000",re={chainId:1337,name:"Exchange",verifyingContract:M,version:"1"},te={Agent:[{name:"source",type:"string"},{name:"connectionId",type:"bytes32"}]},k="Mainnet",ne={approveAgent:[{name:"hyperliquidChain",type:"string"},{name:"agentAddress",type:"address"},{name:"agentName",type:"string"},{name:"nonce",type:"uint64"}],approveBuilderFee:[{name:"hyperliquidChain",type:"string"},{name:"maxFeeRate",type:"string"},{name:"builder",type:"address"},{name:"nonce",type:"uint64"}],sendAsset:[{name:"hyperliquidChain",type:"string"},{name:"destination",type:"string"},{name:"sourceDex",type:"string"},{name:"destinationDex",type:"string"},{name:"token",type:"string"},{name:"amount",type:"string"},{name:"fromSubAccount",type:"string"},{name:"nonce",type:"uint64"}],tokenDelegate:[{name:"hyperliquidChain",type:"string"},{name:"validator",type:"address"},{name:"wei",type:"uint64"},{name:"isUndelegate",type:"bool"},{name:"nonce",type:"uint64"}],usdClassTransfer:[{name:"hyperliquidChain",type:"string"},{name:"amount",type:"string"},{name:"toPerp",type:"bool"},{name:"nonce",type:"uint64"}]},ie={approveAgent:"HyperliquidTransaction:ApproveAgent",approveBuilderFee:"HyperliquidTransaction:ApproveBuilderFee",sendAsset:"HyperliquidTransaction:SendAsset",tokenDelegate:"HyperliquidTransaction:TokenDelegate",usdClassTransfer:"HyperliquidTransaction:UsdClassTransfer"},oe=new TextEncoder;function y(...e){let r=e.reduce((i,o)=>i+o.length,0),t=new Uint8Array(r),n=0;for(let i of e)t.set(i,n),n+=i.length;return t}function se(e,r,t){if(t<r)return new Uint8Array([e+t]);if(t<=255)return new Uint8Array([e+r,t]);if(t<=65535)return new Uint8Array([e+r+1,t>>8,t&255]);return new Uint8Array([e+r+2,t>>>24&255,t>>>16&255,t>>>8&255,t&255])}function C(e,r){let t=new Uint8Array(r),n=e;for(let i=r-1;i>=0;i-=1)t[i]=Number(n&0xffn),n>>=8n;return t}function _(e){if(e>=0n){if(e<=0x7fn)return new Uint8Array([Number(e)]);if(e<=0xffn)return new Uint8Array([204,Number(e)]);if(e<=0xffffn)return y(new Uint8Array([205]),C(e,2));if(e<=0xffffffffn)return y(new Uint8Array([206]),C(e,4));return y(new Uint8Array([207]),C(e,8))}if(e>=-32n)return new Uint8Array([256+Number(e)]);if(e>=-128n)return new Uint8Array([208,256+Number(e)]);if(e>=-32768n)return y(new Uint8Array([209]),C(BigInt.asUintN(16,e),2));if(e>=-2147483648n)return y(new Uint8Array([210]),C(BigInt.asUintN(32,e),4));return y(new Uint8Array([211]),C(BigInt.asUintN(64,e),8))}function F(e){let r=oe.encode(e);if(r.length<32)return y(new Uint8Array([160+r.length]),r);return y(se(217,0,r.length),r)}function ae(e){let r=e.map((n)=>D(n)),t=e.length<16?new Uint8Array([144+e.length]):e.length<=65535?new Uint8Array([220,e.length>>8,e.length&255]):new Uint8Array([221,e.length>>>24&255,e.length>>>16&255,e.length>>>8&255,e.length&255]);return y(t,...r)}function pe(e){let r=Object.entries(e).filter(([,n])=>typeof n<"u"),t=r.length<16?new Uint8Array([128+r.length]):r.length<=65535?new Uint8Array([222,r.length>>8,r.length&255]):new Uint8Array([223,r.length>>>24&255,r.length>>>16&255,r.length>>>8&255,r.length&255]);return y(t,...r.flatMap(([n,i])=>[F(n),D(i)]))}function D(e){if(e===null)return new Uint8Array([192]);if(typeof e==="boolean")return new Uint8Array([e?195:194]);if(typeof e==="string")return F(e);if(typeof e==="number"){if(!Number.isFinite(e)||!Number.isInteger(e))throw new w("helpers_invalid_params",{message:"Hyperliquid msgpack only supports integers"});return _(BigInt(e))}if(typeof e==="bigint")return _(e);if(e instanceof Uint8Array){let r=e.length<=255?new Uint8Array([196,e.length]):e.length<=65535?new Uint8Array([197,e.length>>8,e.length&255]):new Uint8Array([198,e.length>>>24&255,e.length>>>16&255,e.length>>>8&255,e.length&255]);return y(r,e)}if(Array.isArray(e))return ae(e);if(typeof e==="object")return pe(e);throw new w("helpers_invalid_params",{message:`Unsupported Hyperliquid msgpack value: ${String(e)}`})}function ye(e){let r=e.startsWith("0x")?e.slice(2):e;if(r.length%2!==0||/[^a-fA-F0-9]/u.test(r))throw new w("helpers_invalid_params",{hexValue:e});let t=new Uint8Array(r.length/2);for(let n=0;n<r.length;n+=2)t[n/2]=Number.parseInt(r.slice(n,n+2),16);return t}function v(e){return{domain:re,message:{connectionId:e,source:"a"},primaryType:"Agent",types:{Agent:[...te.Agent]}}}function L(e){let r=ie[e.type],t=ne[e.type],{type:n,...i}=e;return{domain:{chainId:Number.parseInt(i.signatureChainId||"0xa4b1",16),name:"HyperliquidSignTransaction",verifyingContract:M,version:"1"},message:{...i,hyperliquidChain:i.hyperliquidChain||k},primaryType:r,types:{[r]:t}}}function N(e){let r=ee.from(e);return{r:r.r,s:r.s,v:r.v}}function j(e,r,{expiresAfter:t,vaultAddress:n}={}){let i=D(e),o=C(BigInt(r),8),a=n?y(new Uint8Array([1]),ye(n.toLowerCase())):new Uint8Array([0]),p=typeof t==="number"?y(new Uint8Array([0]),C(BigInt(t),8)):new Uint8Array;return Z(y(i,o,a,p))}function $(e){let r=e.hyperliquidChain||k;switch(e.type){case"approveAgent":return{...e,agentAddress:e.agentAddress.toLowerCase(),hyperliquidChain:r};case"approveBuilderFee":return{...e,builder:e.builder.toLowerCase(),hyperliquidChain:r};case"sendAsset":return{...e,destination:e.destination.toLowerCase(),fromSubAccount:e.fromSubAccount.toLowerCase(),hyperliquidChain:r};case"tokenDelegate":return{...e,hyperliquidChain:r,validator:e.validator.toLowerCase()};case"usdClassTransfer":return{...e,hyperliquidChain:r};default:return e}}var u=ce.Hype,{baseDecimal:K}=G(u),Ae={HYPE:K,USDC:6,USDT:6},me="Mainnet",He="0xa4b1",he="0x0000000000000000000000000000000000000000",Te={spotSend:[{name:"hyperliquidChain",type:"string"},{name:"destination",type:"string"},{name:"token",type:"string"},{name:"amount",type:"string"},{name:"time",type:"uint64"}]},xe={spotSend:"HyperliquidTransaction:SpotSend"};function c(e,r){if(!Y(e))throw new m(r,{address:e});return e.toLowerCase()}async function f(e){let r=await S();return A.post(`${r}/info`,{json:e})}function be({dex:e,user:r}){return f({...e?{dex:e}:{},type:"clearinghouseState",user:c(r,"helpers_invalid_params")})}function Se({user:e}){return f({type:"spotClearinghouseState",user:c(e,"helpers_invalid_params")})}function Ue({dex:e,user:r}){return f({...e?{dex:e}:{},type:"openOrders",user:c(r,"helpers_invalid_params")})}function we({dex:e,user:r}){return f({...e?{dex:e}:{},type:"frontendOpenOrders",user:c(r,"helpers_invalid_params")})}function De({oidOrCloid:e,user:r}){return f({oid:e,type:"orderStatus",user:c(r,"helpers_invalid_params")})}function Oe({aggregateByTime:e,user:r}){return f({...typeof e==="boolean"?{aggregateByTime:e}:{},type:"userFills",user:c(r,"helpers_invalid_params")})}function Re({aggregateByTime:e,endTime:r,startTime:t,user:n}){return f({...typeof e==="boolean"?{aggregateByTime:e}:{},...typeof r==="number"?{endTime:r}:{},startTime:t,type:"userFillsByTime",user:c(n,"helpers_invalid_params")})}function z({coin:e,token:r,value:t,spotMeta:n}){if(!t||Number(t)<=0)return null;let i=e?.toUpperCase()||`TOKEN-${r||"UNKNOWN"}`,o=Ae[i]||K,a=n?.tokens?.find((g)=>g.name===e||r!==void 0&&g.index===Number(r)),p=a?`${u}.${i}-${a.name}:${a.tokenId}`:`${u}.${i}`;return new R({decimal:o,identifier:p,value:t})}function Be(e){let r=new Map;for(let t of e){let n=t.toString({includeSynthProtocol:!0}),i=r.get(n);r.set(n,i?i.add(t):t)}if(![...r.values()].some((t)=>t.isGasAsset||t.ticker==="HYPE")){let t=R.from({chain:u,value:0});r.set(t.toString({includeSynthProtocol:!0}),t)}return[...r.values()]}async function S(){try{return await ue(u)}catch{let[e]=G(u).rpcUrls;if(!e)throw new m("helpers_chain_no_public_or_set_rpc_url",{chain:u});return e}}async function B(){return`${await S()}/exchange`}function Pe(e){let r=xe[e.type],{type:t,...n}=e,i=Te[t];return{domain:{chainId:Number.parseInt(e.signatureChainId,16),name:"HyperliquidSignTransaction",verifyingContract:he,version:"1"},message:n,primaryType:r,types:{[r]:i}}}async function Ie({action:e,expiresAfter:r,nonce:t,vaultAddress:n}){let i=t||Date.now(),o=n?c(n,"helpers_invalid_params"):void 0,a=await B(),p=j(e,i,{expiresAfter:r,vaultAddress:o});return{action:e,endpoint:a,expiresAfter:r,nonce:i,typedData:v(p),vaultAddress:o}}async function qe({action:e,nonce:r,signatureChainId:t}){let n=r||e.nonce||Date.now(),i=t||e.signatureChainId||"0xa4b1",o=$({...e,nonce:n,signatureChainId:i});return{action:o,endpoint:await B(),nonce:n,typedData:L(o)}}async function Q({assetValue:e,recipient:r,sender:t,signatureChainId:n,time:i}){let o=c(r,"core_transaction_invalid_recipient_address"),a=c(t,"core_transaction_invalid_sender_address"),p=await B(),g=e.getValue("string"),T=i||Date.now(),x=e.address;if(!x)throw new m("helpers_invalid_params",{asset:e.toString(),from:a});let b={amount:g,destination:o,hyperliquidChain:me,signatureChainId:n||He,time:T,token:x,type:"spotSend"};return{action:b,endpoint:p,nonce:T,typedData:Pe(b)}}async function W(e){let r=await A.post(e.endpoint,{json:{action:e.action,...typeof e.expiresAfter==="number"?{expiresAfter:e.expiresAfter}:{},nonce:e.nonce,signature:e.signature,...e.vaultAddress?{vaultAddress:e.vaultAddress}:{}}});if(r.status!=="ok")throw new m("core_transaction_failed",{response:r});return r}async function _e(e,r){let t=await S(),n=r-5000;for(let i=0;i<5;i++){if(i>0)await new Promise((o)=>setTimeout(o,1000));try{let o=await A.post(`${t}/info`,{json:{startTime:n,type:"userNonFundingLedgerUpdates",user:e}}),a=Array.isArray(o)?o.find((p)=>p.time&&p.time>=r&&(p.delta?.type==="spotTransfer"||p.delta?.type==="internalTransfer")):void 0;if(a?.hash)return a.hash}catch{}}return""}function Y(e){return e.startsWith("0x")&&V(e)}function or(e){let r=E(u),t=e&&"index"in e?e.index||0:0,n=de(e&&"derivationPath"in e&&e.derivationPath?e.derivationPath:le(ge[u],{index:t})),i,o=!1;function a(){if(o)return i;return i=fe(e).with({phrase:O.string},({phrase:s})=>{return Ce.fromPhrase(s,void 0,n)}).with({signer:O.not(O.nullish)},({signer:s})=>s).otherwise(()=>{return}),o=!0,i}async function p(){let s=a();return s?await s.getAddress():""}async function g(s){let d=a();if(!d)throw new m("toolbox_hypercore_no_signer");let l=await d.signTypedData(s.typedData.domain,s.typedData.types,s.typedData.message);return{action:s.action,endpoint:s.endpoint,nonce:s.nonce,signature:N(l),..."expiresAfter"in s?{expiresAfter:s.expiresAfter}:{},..."vaultAddress"in s?{vaultAddress:s.vaultAddress}:{}}}async function T(s){let d=c(s,"helpers_invalid_params");try{let l=await S(),[H,h,P]=await Promise.all([A.post(`${l}/info`,{json:{type:"spotClearinghouseState",user:d}}),A.post(`${l}/info`,{json:{type:"clearinghouseState",user:d}}),A.post(`${l}/info`,{json:{type:"spotMeta"}})]),I=(H.spotState?.balances||H.balances||[]).map(({coin:U,token:J,total:X})=>z({coin:U,spotMeta:P,token:J,value:X})).filter((U)=>!!U),q=z({coin:"USDC",spotMeta:P,value:h.withdrawable||h.marginSummary?.accountValue||h.marginSummary?.totalRawUsd});return Be(q?[...I,q]:I)}catch{return r(d)}}async function x(s){let d=a();if(!d)throw new m("toolbox_hypercore_no_signer");let l=s.sender||await d.getAddress(),H=await Q({...s,feeRate:0,sender:l}),h=await g(H);return await W(h),_e(l,H.nonce)}function b(){return R.from({chain:u,value:0})}return{broadcastTransaction:W,createL1ActionRequest:Ie,createTransaction:Q,createUserSignedActionRequest:qe,estimateTransactionFee:b,getAddress:p,getBalance:T,getState:{clearinghouse:be,frontendOpenOrders:we,openOrders:Ue,orderStatus:De,spotClearinghouse:Se,userFills:Oe,userFillsByTime:Re},signTransaction:g,transfer:x,validateAddress:Y}}export{Y as validateHyperCoreAddress,or as getHyperCoreToolbox,qe as createUserSignedActionRequest,Q as createTransaction,Ie as createL1ActionRequest,Qe as buildUsdClassTransferAction,Ne as buildUpdateLeverageAction,je as buildUpdateIsolatedMarginAction,$e as buildTopUpIsolatedOnlyMarginAction,Ge as buildTokenDelegateAction,We as buildSendAssetAction,ze as buildScheduleCancelAction,Me as buildOrderAction,ve as buildModifyAction,Fe as buildCancelByCloidAction,ke as buildCancelAction,Le as buildBatchModifyAction,Ve as buildApproveBuilderFeeAction,Ee as buildApproveAgentAction};
|