如何设置代码允许最高35美元Gas费用并解决交易失败问题
托管智能合约submitBets函数交易失败问题
我有一个用于提取玩家钱包Token的托管智能合约(escrow smart contract)。数月前部署时Gas价格约10 gwei,调用submitBets函数的Gas费最高仅6美元,运行正常;当前Gas价格35 gwei时,相同函数调用失败,合约及JavaScript调用代码均未改动。
此前在Etherscan直接调用该函数,Gas费在3-6美元之间;现在调用Gas费在18-30美元之间,我愿意支付该费用,但交易持续失败。
Solidity合约函数
/** * @dev Submit bets for a new game. * @param gameId Identifier for the game * @param _players Array of player addresses * @param bets Array of bet amounts corresponding to players * @return true if bets are successfully submitted */ function submitBets( uint256 gameId, address[] memory _players, uint256[] memory bets ) external onlyOwner returns (bool) { require(_players.length > 1, "There must be more than 1 player"); require(_players.length == bets.length, "Array Length Does not Match"); require(areAllBetsEqual(bets), "All bet amounts must be the same"); require(!isGameInProgress(gameId), "Game is in Progress"); uint256 totalSum = 0; for (uint256 i = 0; i < _players.length; i++) { require( token.allowance(_players[i], address(this)) >= bets[i], "Not enough allowance" ); // Add player to the current game playersInGame[gameId].push(_players[i]); token.transferFrom(_players[i], address(this), bets[i]); totalSum += bets[i]; } games[gameId] = Game(true, totalSum); emit BetSubmitted(gameId, playersInGame[gameId], totalSum); return true; }
JavaScript调用函数
export async function callNewGame(betId, addresses, bets) { try { const ownerAddress = bufferToHex(privateToAddress(toBuffer(privateKey))); const data = escrowContract.methods.submitBets(betId, addresses, bets).encodeABI(); const nonce = await web3.eth.getTransactionCount(ownerAddress); const gasPrice = await web3.eth.getGasPrice(); const gasPrice120Percent = web3.utils.toHex((BigInt(gasPrice) * BigInt(1200)) / BigInt(1000)); const txObject = { nonce: web3.utils.toHex(nonce), gasLimit: web3.utils.toHex(3000000), gasPrice: gasPrice120Percent, to: escrowAdress, chainId: 1, data: data, }; const signedTx = await web3.eth.accounts.signTransaction(txObject, privateKey); if (receipt.status == true) { return { status: true, hash: receipt.transactionHash }; } else { console.error("Transaction failed"); console.error("Transaction receipt:", receipt, receipt.status); return { status: false }; } } catch (err) { console.log(err); return { status: false }; } }
遇到的错误
- 设置gasLimit为3000000时,出现错误:
'err: insufficient funds for gas * price + value: address 0x***** have 35506820790388687 want 76938519048000000 (supplied gas 3000000)', signature: undefined, receipt: undefined, data: undefined, code: 402 }
- 将gasLimit改为300000时,出现错误:
TransactionRevertInstructionError: Transaction has been reverted by the EVM
我尝试修改gasLimit,但调整后就会出现交易回滚错误,希望修改代码以支持最高35美元的Gas费用完成交易。
内容的提问来源于stack exchange,提问作者Olayiwola Ayoola
相关产品推荐
相关产品推荐

