require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../types/external-action.types.cjs`),t=require(`../../constants/chains.constants.cjs`),n=require(`../../constants/protocol.constants.cjs`),r=require(`../../types/cache.types.cjs`);require(`../../types/index.cjs`);const i=require(`../../error-handling/error-codes.constants.cjs`),a=require(`../../functions/web3/etherFunctions.cjs`),o=require(`../crypto-keys/keys.cjs`),s=require(`../../functions/utils/time.utils.cjs`),c=require(`../../functions/utils/token-check.utils.cjs`),l=require(`../../functions/web3/events/getShieldedBalance.cjs`),u=require(`../../functions/pre-transaction/outputUtxoProcessing.cjs`),ee=require(`../../functions/snarkjs/common.snarkjs.cjs`),d=require(`../../functions/pre-transaction/solana.cjs`),f=require(`../../functions/pre-transaction/constructAdminData.cjs`),p=require(`../../functions/pre-transaction/convert-okx-instructions.cjs`),m=require(`../../functions/pre-transaction/ensureAmountChanges.cjs`),h=require(`../../functions/pre-transaction/getFeeStructure.cjs`),g=require(`../../functions/pre-transaction/buildCommitmentValidationData.cjs`),_=require(`../../functions/snarkjs/constructSolanaZkProof.cjs`),v=require(`../../functions/web3/functionCalls/transactCallRelayer.cjs`);let y=require(`ethers`),b=require(`@solana/web3.js`);var x=async(e,t,n,r)=>{let i=[...await l.addPaddingToUtxos(e,t,n,r,6)],a=s.getCurrentTimeInSeconds().toString(),o=[];for(let t=0;t<n.length;t+=1){let{outputUtxos:n}=u.outputUtxoProcessing(e.userKeys,i[t],r[t],a);o.push([n[0]])}return{inputUtxosArray:i,outputUtxosArray:o}},S=async(s,l,u,S,C,w,te,T,E)=>{let D=c.validateAndGetChainId(l),O=new b.PublicKey(n.solanaNativeAddress),{hinkalAddress:ne,originalDeployer:k}=t.networkRegistry[D].contractData;if(!k)throw Error(`No IDL configured for Solana program on this network`);let A=new b.PublicKey(k),j=new b.PublicKey(ne),M=l.map(e=>e.erc20TokenAddress),{userKeys:N,merkleTreeHinkalByChain:P}=s,F=T??await h.getFeeStructure(D,te,M,e.ExternalActionId.Transact),I=u[1],L=Number(s.cacheDevice.get(r.StorageKeys.SLIPPAGE_PERCENTAGE)??0),R=l[1],z=a.getAmountInToken(R,I),B=Number(z)*L/100;u[1]-=a.getAmountInWei(R,B.toFixed(R.decimals));let V=I*BigInt(F.variableRate??0)/10000n+F.flatFee;if(u[1]-=V,u[1]<0)throw Error(i.transactionErrorCodes.LOW_OUTPUT_AMOUNT);let H=F.variableRate.toString(),[U,W,{inputUtxosArray:G,outputUtxosArray:K}]=await Promise.all([s.getRandomRelay(D,!0),s.getEthereumAddressByChain(D),x(s,D,M,u)]);if(!U)throw Error(i.transactionErrorCodes.RELAYER_NOT_AVAILABLE);let q=s.userKeys.getShieldedPrivateKey(),J=o.UserKeys.findCorrectRandomization(a.randomBigInt(31),q),Y={tokenNumber:2,nullifierAmount:G[0].length,outputAmount:1},X=ee.calcEncryptedOutputs(K).map(e=>[...y.ethers.getBytes(e[0])]),Z=d.getSwapperAccountPublicKeyFromSalt(j,A,S),{remainingAccounts:Q,hinkalInstructions:$}=p.convertOKXToHinkalInstructions(C,Z);m.ensureAmountChanges(G,K,u);let[{proofAArr:re,proofBArr:ie,proofCArr:ae,publicInputsArr:oe},se]=await Promise.all([_.constructSolanaZkProof(s.generateProofRemotely,P[D],N,M,G,K,J,F.flatFee,F.variableRate,O.toString(),U,Y,X,D,$,Q,S),g.buildCommitmentValidationData(D,N,M,G)]),ce=d.getStorageAccountPublicKey(j,A),le=d.getStorageVaultPublicKey(j,A),ue=l[0].erc20TokenAddress===`11111111111111111111111111111111`?null:new b.PublicKey(l[0].erc20TokenAddress),de=l[1].erc20TokenAddress===`11111111111111111111111111111111`?null:new b.PublicKey(l[1].erc20TokenAddress),fe={recipient:O.toString(),storageAccount:ce.toString(),storageVault:le.toString(),swapperAccount:Z.toString(),mintFrom:ue?.toString()??null,mintTo:de?.toString()??null,remainingAccounts:Q.map(e=>({pubkey:e.pubkey.toString(),isSigner:e.isSigner,isWritable:e.isWritable})),addressLookupTableAccount:w};return v.solanaTransactCallRelayer({chainId:D,relayAddress:U,functionName:`swap`,args:{encryptedOutputs:X,proofAArr:re,proofBArr:ie,proofCArr:ae,publicInputsArr:oe,relayerFee:F.flatFee.toString(),variableRate:H,dimensions:Y,hinkalInstructions:$.map(e=>({accountIndexes:[...e.accountIndexes],data:[...e.data],programIndex:e.programIndex}))},accounts:fe,commitmentValidationData:se,adminData:f.constructAdminData(E,D,M,u,W,l)})};exports.hinkalSolanaSwap=S;