require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../constants/chains.constants.cjs`),t=require(`../../constants/protocol.constants.cjs`),n=require(`../../functions/utils/addresses.cjs`),r=require(`../../error-handling/error-codes.constants.cjs`),i=require(`../../functions/web3/etherFunctions.cjs`),a=require(`../../API/admin-calls.cjs`),o=require(`../crypto-keys/keys.cjs`),s=require(`../../functions/utils/solanaMint.utils.cjs`),c=require(`../../functions/utils/token-check.utils.cjs`),l=require(`../../functions/snarkjs/common.snarkjs.cjs`),u=require(`../../functions/pre-transaction/solana.cjs`),d=require(`../../functions/pre-transaction/constructAdminData.cjs`),f=require(`../../error-handling/retrySolanaError.cjs`),p=require(`../../functions/pre-transaction/solanaTransfer.utils.cjs`),m=require(`./hinkalProoflessDeposit.utils.cjs`);let h=require(`@solana/web3.js`),g=require(`@coral-xyz/anchor`);var _=(n,r)=>{let i=c.validateAndGetChainId([r]),a=r.erc20TokenAddress,{hinkalIdl:o,hinkalAddress:l,originalDeployer:d}=e.networkRegistry[i].contractData;if(!o)throw Error(`No IDL configured for Solana program on this network`);if(!d)throw Error(`missing original deployer`);let f=n.getSolanaProgram(o),p=n.getSolanaPublicKey(),{connection:m,wallet:g}=f.provider;if(!g)throw Error(`missing solana wallet`);let _=new h.PublicKey(d),v=new h.PublicKey(l),{mintPublicKey:y}=s.formatMintAddress(a),b=y.toString()===t.solanaNativeAddress;return{chainId:i,mintAddress:a,program:f,publicKey:p,connection:m,wallet:g,originalDeployer:_,mintPublicKey:y,mint:b?null:y,atasValue:b?null:void 0,storageAccount:u.getStorageAccountPublicKey(v,_),storageVault:u.getStorageVaultPublicKey(v,_),merkleAccount:u.getMerkleAccountPublicKey(v,_)}},v=(e,t,n)=>{let r=n.map(e=>u.buildAnchorStealthAddressStructure(e));return e.program.methods.multiPaymentDeposit(t.map(e=>new g.BN(e.toString())),r,!1).accounts({mint:e.mint,signer:e.publicKey,signerAta:e.atasValue,originalDeployer:e.originalDeployer,storageAccount:e.storageAccount,storageVault:e.storageVault,merkleAccount:e.merkleAccount,storageVaultAta:e.atasValue}).instruction()},y=async(e,t,n,r)=>{for(let i=0;i<10;i+=1){let a=i===9;try{let{blockhash:i,lastValidBlockHeight:a}=await e.getLatestBlockhash(),o=new h.VersionedTransaction(new h.TransactionMessage({payerKey:n,recentBlockhash:i,instructions:r}).compileToV0Message()),s=await t.signTransaction(o),c=await e.sendTransaction(s);return await e.confirmTransaction({signature:c,blockhash:i,lastValidBlockHeight:a},`confirmed`),c}catch(e){if(f.shouldRetryForSolanaError(e)&&!a)continue;throw e}}return``},b=(e,t)=>{if(e.length===0)throw Error(`amounts must not be empty`);if(e.length!==t.length)throw Error(`amounts and stealthAddressStructures length mismatch`);if(e.some(e=>e<=0n))throw Error(`all amounts must be positive`);m.assertNoDuplicateStealthAddressStructures(t)},x=async(e,t,n,r,i,o=!1)=>{b(n,r);let s=_(e,t),c=d.constructAdminData(i,s.chainId,Array(n.length).fill(s.mintAddress),n,await e.getEthereumAddress()),l=await v(s,n,r);if(o){let{blockhash:e}=await s.connection.getLatestBlockhash(),t=new h.Transaction().add(l);return t.recentBlockhash=e,t.feePayer=s.publicKey,Buffer.from(t.serialize({requireAllSignatures:!1})).toString(`base64`)}let u=await y(s.connection,s.wallet,s.publicKey,[l]);return await a.emitTxPublicData(c),u},S=async(e,t,n,r,i,o)=>{b(n,r);let s=_(e,t),c=await e.getRandomRelay(s.chainId,!1);if(!c)throw Error(`No relay available for chainId ${s.chainId}`);let{instructions:l}=await p.buildSolanaTransferInstructions(s.connection,s.publicKey,new h.PublicKey(c),t,i),u=await v(s,n,r),f=d.constructAdminData(o,s.chainId,Array(n.length).fill(s.mintAddress),n,await e.getEthereumAddress()),m=await y(s.connection,s.wallet,s.publicKey,[...l,u]);return await a.emitTxPublicData(f),m},C=async(e,t,n,r=!1)=>{let a=e.userKeys.getShieldedPrivateKey(),s=l.calcStealthAddressStructure(o.UserKeys.findCorrectRandomization(i.randomBigInt(31),a),a,e.userKeys.getSpendingKeyPair().pubSpendingBJJPoint);return x(e,n,[t],[s],void 0,r)},w=async(e,t,i,a,o=!1,s)=>{if(!n.isValidPrivateAddress(a))throw Error(r.transactionErrorCodes.RECIPIENT_FORMAT_INCORRECT);let c=n.constructStealthAddressStructure(a);return x(e,i,[t],[c],s,o)};exports.hinkalSolanaDeposit=C,exports.hinkalSolanaDepositForOther=w,exports.hinkalSolanaProoflessDepositWithPublicFee=S,exports.submitSolanaProoflessDeposit=x;