"use strict";var v=Object.defineProperty;var C=(e,t,n)=>t in e?v(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var c=(e,t,n)=>(C(e,typeof t!="symbol"?t+"":t,n),n);const s=require("./index-OO3WXYTB.js"),_=require("./chain-BjqO6Ozc.js"),g="0x1::aptos_coin::AptosCoin",p="::";class A extends _.ChainContext{}const m="Aptos",S=e=>/^(0x)?[0-9a-fA-F]+::\w+::\w+$/.test(e),y=e=>e.split(p)[0],u=class u extends _.PlatformContext{constructor(t,n){super(t,n??s.networkPlatformConfigs(t,u._platform))}getRpc(t){if(t in this.config)return new s.AptosClient(this.config[t].rpc);throw new Error("No configuration available for chain: "+t)}getChain(t,n){if(t in this.config)return new A(t,this);throw new Error("No configuration available for chain: "+t)}static nativeTokenId(t,n){if(!this.isSupportedChain(n))throw new Error(`invalid chain: ${n}`);return s.Wormhole.tokenId(n,g)}static isNativeTokenId(t,n,i){return!this.isSupportedChain(n)||i.chain!==n?!1:this.nativeTokenId(t,n)==i}static isSupportedChain(t){return s.chainToPlatform(t)===u._platform}static async getDecimals(t,n,i){if(s.isNative(i))return s.nativeDecimals(u._platform);const a=i.toString(),h=`0x1::coin::CoinInfo<${a}>`;return(await n.getAccountResource(a.split(p)[0],h)).data.decimals}static async getBalance(t,n,i,a){const h=s.isNative(a)?g:a.toString(),r=new s.CoinClient(n);try{return await r.checkBalance(i,{coinType:h})}catch(o){if(o.errorCode==="resource_not_found"&&o.status===404)return null;throw o}}static async getBalances(t,n,i,a){return(await Promise.all(a.map(async r=>{const o=await this.getBalance(t,n,i,r);return{[s.isNative(r)?"native":new d(r).toString()]:o}}))).reduce((r,o)=>Object.assign(r,o),{})}static async sendWait(t,n,i){const a=[];for(const h of i){const r=await n.submitTransaction(h),o=await n.waitForTransactionWithResult(r.hash);a.push(o.hash)}return a}static async getLatestBlock(t){const n=await t.getLedgerInfo();return Number(n.block_height)}static async getLatestFinalizedBlock(t){const n=await t.getLedgerInfo();return Number(n.block_height)}static chainFromChainId(t){const n=s.platformNativeChainIdToNetworkChain(u._platform,BigInt(t));if(!n)throw new Error(`No matching chainId to determine network and chain: ${t}`);const[i,a]=n;return[i,a]}static async chainFromRpc(t){const i=await t.getChainId();return this.chainFromChainId(i.toString())}};c(u,"_platform",m);let l=u;function T(e){return e.length%2!==0||e.length<66?(e=e.startsWith("0x")?e.slice(2):e,"0x"+e.padStart(64,"0")):e}const f=class f{constructor(t){c(this,"platform",l._platform);c(this,"type","Native");c(this,"address");c(this,"module");if(f.instanceof(t))this.address=t.address,this.module=t.module;else if(s.UniversalAddress.instanceof(t))this.address=t.toUint8Array();else if(typeof t=="string"){if(S(t)){const n=t.split(p);this.module=n.slice(1).join(p),t=n[0]}if(t=T(t),!s.hex.valid(t))throw new Error("Invalid Aptos address: "+t);this.address=s.hex.decode(t)}else this.address=t}unwrap(){const t=s.hex.encode(this.address).replace(/^0+/,""),n=this.module?p+this.module:"";return`0x${t}${n}`}toString(){return this.unwrap()}toNative(){return this}toUint8Array(){return this.address}toUniversalAddress(){return new s.UniversalAddress(this.toUint8Array())}static instanceof(t){return t.platform===l._platform}equals(t){return f.instanceof(t)?t.unwrap()===this.unwrap():this.toUniversalAddress().equals(t)}};c(f,"byteSize",32);let d=f;s.registerNative(m,d);async function b(e,t){const[n,i]=await l.chainFromRpc(e);return new x(i,new s.AptosAccount(s.hex.decode(t)),e)}class x{constructor(t,n,i,a){c(this,"_chain");c(this,"_account");c(this,"_rpc");c(this,"_debug");this._chain=t,this._account=n,this._rpc=i,this._debug=a}chain(){return this._chain}address(){return this._account.address().hex()}async signAndSend(t){const n=[];for(const i of t){const{description:a,transaction:h}=i;this._debug&&console.log(`Signing: ${a} for ${this.address()}`);const r={gas_unit_price:"100",max_gas_amount:"30000",expiration_timestamp_secs:(BigInt(Date.now()+8*60*60*1e3)/1000n).toString()},o=await this._rpc.generateTransaction(this._account.address(),h,r),{hash:w}=await this._simSignSend(o);n.push(w)}return n}async _simSignSend(t){return await this._rpc.simulateTransaction(this._account,t).then(n=>n.forEach(i=>{if(!i.success)throw new Error(`Transaction failed: ${i.vm_status}
${JSON.stringify(i,null,2)}`)})),this._rpc.signTransaction(this._account,t).then(n=>this._rpc.submitTransaction(n)).then(n=>this._rpc.waitForTransactionWithResult(n.hash))}}const N={Address:d,Platform:l,getSigner:b,protocols:{WormholeCore:()=>Promise.resolve().then(()=>require("./index-cZZzMtb1.js")),TokenBridge:()=>Promise.resolve().then(()=>require("./index-BsEa93xp.js"))},getChain:(e,t,n)=>new A(t,new l(e,s.applyChainsConfigConfigOverrides(e,m,{[t]:n})))},I=Object.freeze(Object.defineProperty({__proto__:null,default:N},Symbol.toStringTag,{value:"Module"}));exports.APTOS_COIN=g;exports.APTOS_SEPARATOR=p;exports.AptosAddress=d;exports.AptosPlatform=l;exports.aptos=I;exports.coalesceModuleAddress=y;exports.isValidAptosType=S;
