1.先下载hardhat
可去查看教程
npm install --save-dev hardhat
npx hardhat compile
2.部署写好的智能合约
如果是远程可
npx hardhat run scripts/devDeploy.js --network 你在hardhat.config.js配置网络的名字 --show-stack-traces
远程写完之后会得到一个账号
如果只想在本地可
npx hardhat node
因为我选择在本地附上本地调用的一些实例
// 连接到以太坊节点
Web3j web3 = Web3j.build(new HttpService("http://localhost:8545"));
// 设置发送交易的账户
String privateKey = "keys";
Credentials credentials = Credentials.create(privateKey);
// 获取建议的 gasPrice
// 手动设置较高的 gas 限额
BigInteger gasLimit = BigInteger.valueOf(20_000_000_000L);
// 部署合约
try {
LeaveMsg contract = LeaveMsg.load("合约部署的地址",web3, credentials, new StaticGasProvider(gasLimit, DefaultGasProvider.GAS_LIMIT));
System.out.println("Contract deployed at address: " + contract.getContractAddress());
List<Order> orderList = orderRepository.findAll();
for (Order order : orderList) {
// 创建酒类实例
// 给每个酒赋值
OrderNFT wine = new OrderNFT(order.getBuyerName(), order.getOrderCode(), (long) order.getAmount());
// 将酒类实例转化为合适的参数
String wineInfo = String.format("%s|%s|%s", wine.getAgentName(), wine.getOrderNo(), wine.getPrice());
System.out.println(wineInfo);
// 调用 mint 方法
TransactionReceipt mintReceipt = contract.mint(wineInfo).send();
System.out.println("Mint transaction hash: " + mintReceipt.getTransactionHash());
BigInteger price = BigInteger.valueOf(wine.getPrice());
System.out.println("price:" + price);
BigInteger tokenId = contract.getLastTokenId().send();
System.out.println("token:" + tokenId);
contract.setTokenPrice(tokenId, price).send();
BigInteger send = contract.getTokenPrice(contract.getLastTokenId().send()).send();
System.out.println("token price:" + send);
}
} catch (Exception e) {
e.printStackTrace();
}