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