【问题标题】:Web3 send contract methods without metamaskWeb3 发送没有元掩码的合约方法
【发布时间】:2021-03-10 20:27:05
【问题描述】:

我有一个关于我的 Node-js 程序的问题,我需要从我的账户地址到合约地址进行交易。 这是我的合约代码:

contract price{
    
    address owner;

    constructor() public{
        owner = msg.sender;
    }

    struct pricedata{
        uint highprice;
        uint lowprice;
        uint avgprice;
    }
    
    mapping(uint => pricedata) PD;
    
    modifier onlyowner(){
        require(msg.sender == owner);
        _;
    }

    function set(uint _ID, uint _highprice, uint _lowprice, uint _avgprcie) public onlyowner{
        PD[_ID] = pricedata({
            highprice : _highprice,
            lowprice : _lowprice,
            avgprice : _avgprcie
        });
    }

    function get(uint _ID) public view returns (uint _highprice, uint _lowprice, uint _avgprcie){
        pricedata memory pd = PD[_ID];
        return (pd.highprice, pd.lowprice, pd.avgprice);
    }
}

这是我的 node-js 代码:

state = {web3: null, accounts: null, contract: null ,info:null ,lowprice : 0, highprice : 0, avgprice : 0};
componentDidMount = async () => {
            const web3 = await getWeb3();
            
            const accounts = await web3.eth.getAccounts();
            const balances  = await web3.eth.getBalance(accounts[0]);
            var bal = web3.utils.fromWei(balances, 'ether');

            this.setState({account : accounts[0], balance : bal});

            const networkId = await web3.eth.net.getId();
            const deployedNetwork = SimpleStorageContract.networks[networkId];

            const instance = new web3.eth.Contract(
              SimpleStorageContract.abi,
              deployedNetwork && deployedNetwork.address,
            );
          
            this.setState({ web3, accounts, contract: instance });

            this.runExample();
}

  runExample = async () => {
      const low = 0 | this.state.lowprice*100;
      const high = 0 | this.state.highprice*100;
      const avg = 0 | this.state.avgprice*100;
      const ran = this.state.randomnumber;
      console.log("test:",low,high,avg,ran);
      this.state.contract.methods.set(ran, low, high, avg).send({
          from : this.state.accounts[0]
      });
  };

我想在没有元掩码的情况下进行交易,我想自动点击确认按钮,蓝色的:

如何使用this.state.contract.methods.set 完成我的功能?

我使用 Ganache 来设置我的以太坊。

我没有在这里发布我的所有代码,如果需要更多详细信息,请告诉我,我会填写。

【问题讨论】:

    标签: node.js blockchain web3 metamask


    【解决方案1】:

    您可以在 Web3.js 中导入您的以太坊帐户。那么你就不需要用 Metamask 确认每一笔交易了。

    const ganache = require('ganache-cli');
    const Web3 = require('web3');
    
    //ganache client running on port 7545
    var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
    
    const getAccounts = async () => {
    
        //To get accounts with private key
        let account = await web3.eth.accounts.privateKeyToAccount('0x'+privateKey);
        //privateKey is the key that you get from Ganache client
    }
    
    getAccounts();
    

    【讨论】:

      猜你喜欢
      • 2019-03-08
      • 2018-03-18
      • 2018-12-05
      • 2021-08-04
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2021-09-18
      相关资源
      最近更新 更多