0

Frontend

class App extends React.Component {
  state = {
    address: '',
  }

  async componentDidMount() {
    await window.ethereum.enable()
    this.w3 = new Web3(window.ethereum)
    const address = (await this.w3.eth.getAccounts())[0]
    this.setState({ address })
    this.contract =new this.w3.eth.Contract(ABI,"0x5888E695e574B6fd87E9552a50736875ec535773")
  }

  // address send to smart contract
  async sendToContract() {
    await this.contract.methods.invest().send({
      from: this.state.address,
      value: this.state.amount,
    })
  }

  onChange1(e) {
    this.setState({
      amount: e.target.value,
    })
  } 

  // smart contract send to address
  async contractSendToAddress() {
    await this.contract.methods.sendEther(this.state.to).send({
      from: this.contract,
    })
  }

  onChange2(e) {
    this.setState({
      to: e.target.value,
    })
  }

  render() {
    return (
      <div className="App">
        <div>
          <input placeholder="amount" onChange={this.onChange1.bind(this)}/>
          <button onClick={this.sendToContract.bind(this)}>sendToContract</button>  
        </div>

        <div>
          <input placeholder="to address" onChange={this.onChange2.bind(this)}/>
          <button onClick={this.contractSendToAddress.bind(this)}>contractSend</button>  
        </div>
      </div>
    );
  }
}

Smart Contract

contract sendEth {
  mapping(address => uint) balances;
  function invest() external payable {
    if(msg.value < 0.01 ether) {
      revert();
    }
    balances[msg.sender] += msg.value;
  }

  function balanceOf() external view returns(uint) {
    return address(this).balance;
  }

  function sendEther(address payable recipient) external {
    recipient.transfer(0.0001 ether);
  }
}

Error: Provided address "[object Object]" is invalid, the capitalization checksum test failed, or its an indrect IBAN address which can't be converted.

2
  • what is the error you getting?! – Majd TL Nov 17 '19 at 9:47
  • @MajdTL Error: Provided address "[object Object]" is invalid, the capitalization checksum test failed, or its an indrect IBAN address which can't be converted. – Kunakorn Tessana Nov 17 '19 at 9:51
0

You are trying to pass Javascript object as Ethereum address, while it should be Javascript string.

The problem is most probably in the following line:

from: this.contract

It should be something like this:

from: this.contract.address

to avoid the error.

However, there is another, even more serious problem with your approach: in order to send transaction from some address, one has to know private key corresponding to this address, and for smart contract addresses usually nobody knows corresponding private keys, even smart contract authors.

4
  • async contractSendToAddress() { await this.contract.methods.sendEther(this.state.to).send({ from: '0x5888E695e574B6fd87E9552a50736875ec535773', }) } What to do if sending from this contract address? – Kunakorn Tessana Nov 17 '19 at 11:00
  • @KunakornTessana from cannot be a contract address { from: '0x5888E695e574B6fd87E9552a50736875ec535773', } you should have an owner for the contract, and only the owner can trigger the method to send ether from the contract to an address – Majd TL Nov 17 '19 at 11:09
  • You just cannot send transactions from contract's address. Only the contract itself can. However, you may ask the contract to send ether from its address somewhere, by sending transaction from your address to the contract's address with certain payload. However, it is always up to the contract, whether to do what you are asking for, or not. – Mikhail Vladimirov Nov 17 '19 at 11:32
  • I solved it. Thank you very much everyone. – Kunakorn Tessana Nov 17 '19 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.