30 紧急求救,采纳即私发红包50元!合约在BSC测试链部署、运行正常,到bsc主链就报错!

问题是这样,写了个链上的邀请积分合约,然后搭配前端。之前一直是在bsc测试链上测试合约,一切正常;但是部署到主链的时候就懵逼了,一直报错。

合约代码:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract InviteSystem {
    address public owner; // 合约拥有者的地址
    mapping(address => bool) public registeredUsers;
    mapping(address => uint256) public inviteCount;
    mapping(address => uint256) public userPoints; // 新增用户点数映射
    uint256 public totalParticipants;

    uint256 constant public registrationPoints = 50; // 注册获得的点数
    uint256 constant public invitePoints = 50;       // 每邀请一个人获得的点数
    uint256 constant public totalFee = 0.001 ether; // 增加手续费

    event UserRegistered(address indexed user);
    event UserInvited(address indexed inviter, address indexed invitee);
    event FundsWithdrawn(address indexed owner, uint256 amount);
    event PointsUpdated(address indexed user, uint256 newPoints);

    constructor() {
        owner = msg.sender; // 设置合约拥有者
    }

    function register(address _inviter) public payable {
        require(!registeredUsers[msg.sender], "User is already registered.");

        require(msg.value >= totalFee, "Insufficient fee sent.");

        registeredUsers[msg.sender] = true;
        totalParticipants += 1;
        userPoints[msg.sender] = registrationPoints; // 注册获得50点
        emit PointsUpdated(msg.sender, userPoints[msg.sender]);

        // 如果有邀请人,增加邀请人的 inviteCount 和点数
        if (_inviter != address(0) && registeredUsers[_inviter]) {
            inviteCount[_inviter] += 1;
            userPoints[_inviter] += invitePoints; // 邀请人获得50点
            emit UserInvited(_inviter, msg.sender);
            emit PointsUpdated(_inviter, userPoints[_inviter]);
        }

        emit UserRegistered(msg.sender);

        // 计算多支付的手续费
        uint256 extraFee = msg.value - totalFee;
        if (extraFee > 0) {
            payable(owner).transfer(extraFee); // 将多余的手续费转移到合约拥有者
        }
    }

    // 获取用户邀请数量
    function getInviteCount(address _user) public view returns (uint256) {
        return inviteCount[_user];
    }

    // 获取总参与人数
    function getTotalParticipants() public view returns (uint256) {
        return totalParticipants;
    }

    // 获取用户点数
    function getUserPoints(address _user) public view returns (uint256) {
        return userPoints[_user];
    }

    // 提取合约中的所有资金
    function withdraw() public {
        require(msg.sender == owner, "Only the owner can withdraw funds.");
        uint256 balance = address(this).balance;
        require(balance > 0, "No funds to withdraw.");

        payable(owner).transfer(balance);
        emit PointsUpdated(owner, balance);
    }

    // 允许合约接受 BNB
    receive() external payable {}
}

下面是前端的注册函数:

const handleRegister = async () => {
    if (contract && account) {
        setLoading(true);
        console.log('执行到1');
        try {
            // 定义合约中注册所需的固定手续费
            const totalFee = web3.utils.toWei("0.001", "ether"); // 将手续费转换为 wei
            console.log('执行到1');
            // 估算手续费
            const estimatedGas = await contract.methods
                .register(inviterAddress || "0x0000000000000000000000000000000000000000")
                .estimateGas({ from: account });
            console.log('执行到2: ', estimatedGas);

            const gasPrice = await web3.eth.getGasPrice();
            console.log('执行到 3 ');

            // 计算总费用 = (估算的 gas * gas price) + 固定手续费
            const totalFeeWithGas = BigInt(estimatedGas) * BigInt(gasPrice) + BigInt(totalFee);
            console.log('执行到4: totalFeeWithGas =', totalFeeWithGas.toString());

            // 调用智能合约的 register 方法,并传递 inviterAddress(如果有)
            await contract.methods
                .register(inviterAddress || "0x0000000000000000000000000000000000000000")
                .send({
                    from: account,
                    value: totalFeeWithGas.toString(), // 确保将费用传递为字符串形式
                });
            console.log('执行到6');
            console.log('inviterAddress: ', inviterAddress);

            setIsRegistered(true);
            console.log('执行到7');
            await updateInviteData(account); // 注册后更新数据
            console.log('执行到8');
        } catch (error) {
            console.error("Error registering user:", error.message);
            if (error.code) {
                console.error("Error code:", error.code);
            }
            if (error.data) {
                console.error("Error data:", error.data);
            }
        } finally {
            setLoading(false);
        }
      }
    };

下面是控制台报错截图:

微信图片_20241001110809.png

报错文字内容:

useInvite.ts:478 Error registering user: Returned error: Internal JSON-RPC error.
window.console.error @ app-index.js:34
console.error @ hydration-error-info.js:41
handleRegister @ useInvite.ts:478
await in handleRegister (async)
handleRegisterClick @ road-map-area.tsx:112
callCallback @ react-dom.development.js:20461
invokeGuardedCallbackImpl @ react-dom.development.js:20510
invokeGuardedCallback @ react-dom.development.js:20585
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:20599
executeDispatch @ react-dom.development.js:31936
processDispatchQueueItemsInOrder @ react-dom.development.js:31968
processDispatchQueue @ react-dom.development.js:31981
dispatchEventsForPlugins @ react-dom.development.js:31992
eval @ react-dom.development.js:32182
batchedUpdates$1 @ react-dom.development.js:24793
batchedUpdates @ react-dom.development.js:28653
dispatchEventForPluginEventSystem @ react-dom.development.js:32181
dispatchEvent @ react-dom.development.js:29949
dispatchDiscreteEvent @ react-dom.development.js:29920
Show 16 more frames
Show less
useInvite.ts:480 Error code: 100
window.console.error @ app-index.js:34
console.error @ hydration-error-info.js:41
handleRegister @ useInvite.ts:480
await in handleRegister (async)
handleRegisterClick @ road-map-area.tsx:112
callCallback @ react-dom.development.js:20461
invokeGuardedCallbackImpl @ react-dom.development.js:20510
invokeGuardedCallback @ react-dom.development.js:20585
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:20599
executeDispatch @ react-dom.development.js:31936
processDispatchQueueItemsInOrder @ react-dom.development.js:31968
processDispatchQueue @ react-dom.development.js:31981
dispatchEventsForPlugins @ react-dom.development.js:31992
eval @ react-dom.development.js:32182
batchedUpdates$1 @ react-dom.development.js:24793
batchedUpdates @ react-dom.development.js:28653
dispatchEventForPluginEventSystem @ react-dom.development.js:32181
dispatchEvent @ react-dom.development.js:29949
dispatchDiscreteEvent @ react-dom.development.js:29920
Show 16 more frames
Show less
Error data: {code: 3, message: 'execution reverted: Insufficient fee sent.', data: '0x08c379a00000000000000000000000000000000000000000…369656e74206665652073656e742e00000000000000000000', cause: null}
window.console.error @ app-index.js:34
console.error @ hydration-error-info.js:41
handleRegister @ useInvite.ts:483
await in handleRegister (async)
handleRegisterClick @ road-map-area.tsx:112
callCallback @ react-dom.development.js:20461
invokeGuardedCallbackImpl @ react-dom.development.js:20510
invokeGuardedCallback @ react-dom.development.js:20585
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:20599
executeDispatch @ react-dom.development.js:31936
processDispatchQueueItemsInOrder @ react-dom.development.js:31968
processDispatchQueue @ react-dom.development.js:31981
dispatchEventsForPlugins @ react-dom.development.js:31992
eval @ react-dom.development.js:32182
batchedUpdates$1 @ react-dom.development.js:24793
batchedUpdates @ react-dom.development.js:28653
dispatchEventForPluginEventSystem @ react-dom.development.js:32181
dispatchEvent @ react-dom.development.js:29949
dispatchDiscreteEvent @ react-dom.development.js:29920
Show 16 more frames
Show less

搞了一晚上了,谷歌问了,chatgpt问了,kimi问了,都没有用,按照他们给的方法改来改去改不好。求大佬指导,弄好了100块红包奉上,有些着急。非常感谢。

请先 登录 后评论

1 个回答

用户_20014
请先 登录 后评论
  • 2 关注
  • 0 收藏,361 浏览
  • 用户_20014 提出于 2024-10-01 11:11