|
| 1 | +import { ethers } from "ethers"; |
| 2 | +import abi from "../config/abi.js"; |
| 3 | +import { generateKeys } from "../functions/secretpath/generateKeys"; |
| 4 | +import { getPublicClientAddress } from "../functions/secretpath/getPublicClientAddress"; |
| 5 | +import { constructPayload } from "../functions/secretpath/constructPayload"; |
| 6 | +import { encryptPayload } from "../functions/secretpath/encryptPayload"; |
| 7 | +import { hexlify } from "ethers/lib/utils"; |
| 8 | +import { queryPubkey } from "../functions/query/queryPubkey"; |
| 9 | + |
| 10 | +// call unsafeGetSecretContractPubkey |
| 11 | +export async function handleSubmit(e, setBallCount) { |
| 12 | + e.preventDefault(); |
| 13 | + // handlePlay(); |
| 14 | + |
| 15 | + const routing_contract = process.env.REACT_APP_SECRET_ADDRESS; |
| 16 | + const routing_code_hash = process.env.REACT_APP_CODE_HASH; |
| 17 | + const iface = new ethers.utils.Interface(abi); |
| 18 | + const provider = new ethers.providers.Web3Provider(window.ethereum, "any"); |
| 19 | + |
| 20 | + const [myAddress] = await provider.send("eth_unsafeGetSecretContractPubkey", []); |
| 21 | + |
| 22 | + const { userPrivateKeyBytes, userPublicKeyBytes, sharedKey } = |
| 23 | + await generateKeys(); |
| 24 | + |
| 25 | + const callbackSelector = iface.getSighash( |
| 26 | + iface.getFunction("upgradeHandler") |
| 27 | + ); |
| 28 | + |
| 29 | + console.log("callbackSelector: ", callbackSelector); |
| 30 | + |
| 31 | + const callbackGasLimit = 90000; |
| 32 | + // The function name of the function that is called on the private contract |
| 33 | + const handle = "request_pubkey"; |
| 34 | + |
| 35 | + // Data are the calldata/parameters that are passed into the contract |
| 36 | + const data = JSON.stringify({ address: myAddress }); |
| 37 | + |
| 38 | + const chainId = (await provider.getNetwork()).chainId.toString(); |
| 39 | + |
| 40 | + const publicClientAddress = await getPublicClientAddress(chainId); |
| 41 | + |
| 42 | + const callbackAddress = publicClientAddress.toLowerCase(); |
| 43 | + console.log("callback address: ", callbackAddress); |
| 44 | + |
| 45 | + // Payload construction |
| 46 | + const payload = constructPayload( |
| 47 | + data, |
| 48 | + routing_contract, |
| 49 | + routing_code_hash, |
| 50 | + myAddress, |
| 51 | + userPublicKeyBytes, |
| 52 | + callbackAddress, |
| 53 | + callbackSelector, |
| 54 | + callbackGasLimit |
| 55 | + ); |
| 56 | + |
| 57 | + const { |
| 58 | + ciphertext, |
| 59 | + payloadHash, |
| 60 | + payloadSignature, |
| 61 | + _info, |
| 62 | + } = await encryptPayload( |
| 63 | + payload, |
| 64 | + sharedKey, |
| 65 | + provider, |
| 66 | + myAddress, |
| 67 | + userPublicKeyBytes, |
| 68 | + routing_code_hash, |
| 69 | + handle, |
| 70 | + callbackGasLimit, |
| 71 | + iface, |
| 72 | + callbackSelector |
| 73 | + ); |
| 74 | + |
| 75 | + const functionData = iface.encodeFunctionData("send", [ |
| 76 | + payloadHash, |
| 77 | + myAddress, |
| 78 | + routing_contract, |
| 79 | + _info, |
| 80 | + ]); |
| 81 | + |
| 82 | + const feeData = await provider.getFeeData(); |
| 83 | + const maxFeePerGas = feeData.maxFeePerGas; |
| 84 | + const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas; |
| 85 | + const gasFee = |
| 86 | + maxFeePerGas && maxPriorityFeePerGas |
| 87 | + ? maxFeePerGas.add(maxPriorityFeePerGas) |
| 88 | + : await provider.getGasPrice(); |
| 89 | + |
| 90 | + let amountOfGas; |
| 91 | + let my_gas = 150000; |
| 92 | + |
| 93 | + if (chainId === "4202") { |
| 94 | + amountOfGas = gasFee.mul(callbackGasLimit).mul(100000).div(2); |
| 95 | + } |
| 96 | + |
| 97 | + if (chainId === "128123") { |
| 98 | + amountOfGas = gasFee.mul(callbackGasLimit).mul(1000).div(2); |
| 99 | + my_gas = 15000000; |
| 100 | + } |
| 101 | + |
| 102 | + if (chainId === "1287") { |
| 103 | + amountOfGas = gasFee.mul(callbackGasLimit).mul(1000).div(2); |
| 104 | + my_gas = 15000000; |
| 105 | + } |
| 106 | + |
| 107 | + if (chainId === "300") { |
| 108 | + amountOfGas = gasFee.mul(callbackGasLimit).mul(100000).div(2); |
| 109 | + my_gas = 15000000; |
| 110 | + } |
| 111 | + |
| 112 | + if (chainId === "5003") { |
| 113 | + amountOfGas = gasFee.mul(callbackGasLimit).mul(1000000).div(2); |
| 114 | + my_gas = 1500000000; |
| 115 | + } |
| 116 | + |
| 117 | + if (chainId === "80002") { |
| 118 | + amountOfGas = gasFee.mul(callbackGasLimit).mul(100).div(2); |
| 119 | + my_gas = 200000; |
| 120 | + } |
| 121 | + |
| 122 | + if (chainId === "1995") { |
| 123 | + amountOfGas = gasFee.mul(callbackGasLimit).mul(100).div(2); |
| 124 | + my_gas = 200000; |
| 125 | + } |
| 126 | + |
| 127 | + if (chainId === "713715") { |
| 128 | + amountOfGas = gasFee.mul(callbackGasLimit).mul(100).div(2); |
| 129 | + my_gas = 200000; |
| 130 | + } |
| 131 | + |
| 132 | + else { |
| 133 | + amountOfGas = gasFee.mul(callbackGasLimit).mul(3).div(2); |
| 134 | + } |
| 135 | + |
| 136 | + const tx_params = { |
| 137 | + gas: hexlify(my_gas), |
| 138 | + to: publicClientAddress, |
| 139 | + from: myAddress, |
| 140 | + value: hexlify(amountOfGas), |
| 141 | + data: functionData, |
| 142 | + }; |
| 143 | + |
| 144 | + const txHash = await provider.send("eth_sendTransaction", [tx_params]); |
| 145 | + const publicKey = await queryPubkey(); |
| 146 | + setBallCount(publicKey); // Directly set the new ball count from the query. |
| 147 | + console.log("Public key of Private secret contract:", publicKey); |
| 148 | + console.log(`Transaction Hash: ${txHash}`); |
| 149 | +} |
0 commit comments