简体   繁体   中英

Web3 send contract methods without metamask

I have a question on my Node-js program, I need to transaction from my account address to contract address. And this is my contract code:

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);
    }
}

And this is my node-js code:

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]
      });
  };

I want to transaction without metamask, I want to auto onclick confirm button, the blue one:

在此处输入图像描述

how can I done my function with this.state.contract.methods.set ?

I use Ganache to set up my ethereum.

I didn't post all my code on here, if need some more details, please tell me and I will fill it up.

You can import your Ethereum account in Web3.js. Then you don't need to confirm every transaction with 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM