Remix 部署合约正常,MetaMask异常 transaction underpriced

1.remix部署合约正常
在这里插入图片描述
在这里插入图片描述
这里有一点的就是我的合约比较长gas费比较大已经超过300W了
在这里插入图片描述

但是改为metaMask 报出异常
在这里插入图片描述

在这里插入图片描述
还是相同合约,点击确认之后,报出异常
在这里插入图片描述

具体错误如下

// An highlighted block
errored: Returned error: {"jsonrpc":"2.0","error":"[ethjs-query] while formatting outputs from RPC '{\"value\":{\"code\":-32603,\"data\":{\"code\":-32000,\"message\":\"transaction underpriced\"}}}'","id":5053330458275413}

在这里插入图片描述

尝试修改红框内的参数都没用,还是同样报错
正确的做法是:
在这里插入图片描述
在确认交易这里,点击这个

在这里插入图片描述
然后再点这个高级
在这里插入图片描述
在这里插入图片描述
修改这个两个值,按找自己的需求
在这里插入图片描述
在这里插入图片描述
最后就成功了
在这里插入图片描述
.

### 使用C++在以太坊上部署智能合约 #### 选择合适的工具链和支持库 为了使用C++开发并部署智能合约到以太坊,通常不会直接通过C++编写智能合约本身。相反,推荐的方式是利用现有的Solidity编译器和Web3接口来完成这一过程。然而,对于坚持采用C++的情况,可以借助像Aleth这样的项目[^1]。 #### 安装依赖环境 安装必要的软件包以便能够调用底层API与以太坊网络交互: ```bash sudo apt-get update && sudo apt-get install -y build-essential cmake libboost-all-dev libleveldb-dev libsodium-dev qtbase5-dev libqt5webkit5-dev qtmultimedia5-dev gperf bison flex git python-pip pip install pyethash eth-utils web3 ``` #### 编写智能合约 尽管目标是在C++环境中操作,但智能合约仍然建议使用Solidity语言编写。这里给出一个简单的ERC20代币合约例子: ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract MyToken { string public name; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) balances; constructor(uint256 _initialSupply, string memory _name) { totalSupply = _initialSupply * (10 ** uint256(decimals)); balances[msg.sender] = totalSupply; name = _name; } function transfer(address to, uint256 value) external returns (bool success){ require(balanceOf(msg.sender)>=value); balances[msg.sender]-=value; balances[to]+=value; return true; } function balanceOf(address account) view external returns (uint256){ return balances[account]; } } ``` 此部分来源于对智能合约的理解以及其常见模式的应用[^3]。 #### 部署流程概述 虽然主要工作将在Solidity中完成,但在C++程序里可以通过JSON-RPC API连接至节点并与之通信。具体来说,就是发送交易请求给Geth或其他兼容客户端来进行实际部署动作。这涉及到序列化ABI编码后的字节码数据,并设置正确的gas参数等细节处理。 #### C++代码片段展示如何发起RPC请求 下面是一段简化版的C++代码用于说明怎样向远程服务器提交已签名的消息从而触发合约部署行为: ```cpp #include <jsonrpccpp/client.h> using namespace jsonrpc; int main() { Client c("http://localhost:8545"); Json::Value params(Json::arrayValue); // 假设我们已经有了经过编译得到的二进制文件路径binary_file_path 和 ABI 文件 abi_file_path std::ifstream t(binary_file_path), a(abi_file_path); std::string bytecode((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>()); std::string abi((std::istreambuf_iterator<char>(a)), std::istreambuf_iterator<char>()); // 构建 JSON RPC 请求体 Json::Value request_body; request_body["method"] = "eth_sendTransaction"; Json::Value transaction_params(Json::objectValue); transaction_params["from"] = "your_account_address"; // 发起者地址 transaction_params["data"] = "0x" + bytecode; // 合约初始化代码 transaction_params["gas"] = "0xc350"; // 设置 gas limit transaction_params["gasPrice"] = "0x9184e72a000"; // 设置 gas price request_body["params"].append(transaction_params); try { auto response = c.SendRequest(request_body).Get(); std::cout << "Contract deployed at address:" << response["result"].asString() << "\n"; } catch(const JsonRpcException& e) { cerr << "Error during contract deployment: " << e.what() << endl; } return 0; } ``` 这段代码展示了基本框架,实际应用时还需要加入更多错误检查逻辑和其他必要功能模块。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值