hardhat调用

1.先下载hardhat

Getting started with Hardhat | Ethereum development environment for professionals by Nomic Foundation

可去查看教程

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();
        }
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值