Smart Contract交互 – 如何修复无效的BigNumber值

huangapple go评论66阅读模式
英文:

Smart Contract interaction - how to fix invalid BigNumber value

问题

I am working on a smart contract interaction, I started writing the contract called BuyCoffee:

contract BuyCoffee {
    struct Author {
        address wallet_address;
        uint256 balance;
    }

    struct Coffee {
        address author_wallet_address;
        address sender_wallet_address;
        uint256 value;
    }

    Coffee[] public coffee;
    mapping (address => Author) public authors;
    uint256 public total_contract_balance;

    function getContractBalance() public view returns (uint256) {
        return total_contract_balance;
    }

    ...

    function buymeacoffee(address author_wallet_address, address sender_wallet_address, uint256 value) public {
        coffee.push(Coffee({
            author_wallet_address: author_wallet_address,
            sender_wallet_address: sender_wallet_address,
            value: value
        }));

        authors[author_wallet_address].balance += value;
        total_contract_balance += value;
    }
}

For the frontend app, I am using the library Ether and building a metamask_controller.js:

import { Controller } from "@hotwired/stimulus";
import { ethers } from "ethers";
import abi from "../contract.json" assert { type: "json" };

export default class extends Controller {

...

  async buyCoffee() {
    try {
      const authorWalletAddress = "0x5FF0...f16F3D2";
      const senderWalletAddress = userAddress;
      const price = this.priceTarget.innerText;

      console.log("price: ", price);  
      console.log("authorWalletAddress: ", authorWalletAddress);
      console.log("senderWalletAddress: ", senderWalletAddress);
      
      // execute transaction
      const transaction = await contract.buymeacoffee(
        authorWalletAddress, 
        senderWalletAddress, 
        { value: ethers.utils.parseEther(price) }
        );
  
      // console.log("Processing...", transaction.hash);
      await transaction.wait();
  
      // reload the whole page
      window.location.reload();
    }
    catch (error) {
      console.log(error);
      alert("Transaction failed!");
    }
  }
}

But it looks like I am having an issue when calling the buyCoffee function, the console log shows me the correct values:

price: 0.001
authorWalletAddress: 0x5FF....F3D2
senderWalletAddress: 0x7da....1721

Then I get the error invalid BigNumber value (argument "value", value={"value":{"type":"BigNumber","hex":"0x038d7ea4c68000"}}, code=INVALID_ARGUMENT, version=bignumber/5.7.0)

How could I solve that? It looks like something about the price that has to change from 0.001 to a big number.

英文:

I am working on a smart contract interaction, I started writing the contract called BuyCoffee:

contract BuyCoffee {
    struct Author {
        address wallet_address;
        uint256 balance;
    }

    struct Coffee {
        address author_wallet_address;
        address sender_wallet_address;
        uint256 value;
    }

    Coffee[] public coffee;
    mapping (address => Author) public authors;
    uint256 public total_contract_balance;

    function getContractBalance() public view returns (uint256) {
        return total_contract_balance;
    }

    ...

    function buymeacoffee(address author_wallet_address, address sender_wallet_address, uint256 value) public {
        coffee.push(Coffee({
            author_wallet_address: author_wallet_address,
            sender_wallet_address: sender_wallet_address,
            value: value
        }));

        authors[author_wallet_address].balance += value;
        total_contract_balance += value;
    }

}

For the frontend app, I am using the library Ether and building a metamask_controller.js

import { Controller } from "@hotwired/stimulus";
import { ethers } from "ethers";
import abi from "../contract.json" assert { type: "json" };

export default class extends Controller {

...


  async buyCoffee() {
    try {
      const authorWalletAddress = "0x5FF0...f16F3D2";
      const senderWalletAddress = userAddress;
      const price = this.priceTarget.innerText;

      console.log("price: ", price);  
      console.log("authorWalletAddress: ", authorWalletAddress);
      console.log("senderWalletAddress: ", senderWalletAddress);
      
      // execute transaction
      const transaction = await contract.buymeacoffee(
        authorWalletAddress, 
        senderWalletAddress, 
        { value: ethers.utils.parseEther(price)}
        );
  
      // console.log("Processing...", transaction.hash);
      await transaction.wait();
  
      // reload the whole page
      window.location.reload();
    }
    catch (error) {
      console.log(error);
      alert("Transaction failed!");
    }
  }
  

But look like that I am having an issue when calling the buyCoffee function, the console log show me the correct values:

price:  0.001
authorWalletAddress:  0x5FF....F3D2
senderWalletAddress:  0x7da....1721

Then I get the error invalid BigNumber value (argument="value", value={"value":{"type":"BigNumber","hex":"0x038d7ea4c68000"}}, code=INVALID_ARGUMENT, version=bignumber/5.7.0)

How could I solve that? Look like is something about the price that has to change from 0.001 to big number

答案1

得分: 1

We always pass values as string:

// you could use BN too
{ value: ethers.utils.parseEther(price.toString())}

英文:

We always pass values as string:

// you could use BN too
{ value: ethers.utils.parseEther(price.toString())}

huangapple
  • 本文由 发表于 2023年2月14日 05:35:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定