trufflesuite / drizzle-react-components-legacy

A set of useful components for common dapp UI elements.
95 stars 70 forks source link

Get Balance of Contract #88

Closed mwaeckerlin closed 5 years ago

mwaeckerlin commented 5 years ago

Implement a method to get the balance of a contract.

mwaeckerlin commented 5 years ago

Here is my suggestion, as I implemented it in my project. If you want, @honestbonsai, you may integrate my solution in drizzle-react-components (or I can do it for you, including tests and docu):

ContractBalance.js

import React from "react";
import PropTypes from "prop-types";
import { drizzleConnect } from "drizzle-react";

class ContractBalance extends React.Component {
  constructor(props, context) {
    super(props);

    this.state = {
      balance: null
    };
    context.drizzle.web3.eth.getBalance(
      this.props.contract,
      (err, balance) => {
        if (err) console.log("ERROR", this.props.contract, err);
        else this.setState({ balance });
      }
    );
  }

  render() {
    if (this.props.render) return this.props.render(this.state.balance);
    if (this.state.balance === null) return "Loading...";
    return this.state.balance;
  }
}

ContractBalance.contextTypes = {
  drizzle: PropTypes.object
};

ContractBalance.propTypes = {
  contracts: PropTypes.object.isRequired,
  contract: PropTypes.string.isRequired,
  render: PropTypes.func
};

/*
 * Export connected component.
 */

const mapStateToProps = state => {
  return {
    contracts: state.contracts
  };
};

export default drizzleConnect(ContractBalance, mapStateToProps);
honestbonsai commented 5 years ago

@mwaeckerlin Thanks for the sample of your code.

Does this maintain a live update of the balance?

mwaeckerlin commented 5 years ago

Not yet.

@honestbonsai, do you know what I should use to trigger updates? The most primitive approach would be to reload in time intervals. A better approach would be, if I could register to some Ethereum «Balace-Changed-Events» do you know if there exists something like this?

mwaeckerlin commented 5 years ago

The only solution I find is to watch the blocks: https://ethereum.stackexchange.com/questions/25733/automatically-update-account-balance-with-web3-js-without-polling

mwaeckerlin commented 5 years ago

BTW, @honestbonsai, it is the same in ContractData: There is no live update, if the return value of the function would change…!

honestbonsai commented 5 years ago

@mwaeckerlin Sorry not sure what you mean for ContractData not being live. ContractData is a live update of the data you are watching. It should match the state of what's on the block chain.

mwaeckerlin commented 5 years ago

@mwaeckerlin Sorry not sure what you mean for ContractData not being live. ContractData is a live update of the data you are watching. It should match the state of what's on the block chain.

Sometimes it does, sometimes it does not. I have a case, where calling a payment function would change the result of another method, but it's not updated. So in your opinion, that's a bug in drizzle, if it does not? If so, I'll try to reproduce it and place a bug report…

Regarding this extension here: Should I add an update for the balance and if yes, what should it be based on? Should I watch the blocks?

honestbonsai commented 5 years ago

@mwaeckerlin Yea, I'd consider that a bug. Would definitely appreciate a bug report :). The whole point of Drizzle and its companion libraries is to make it easy to keep things in sync with the blockchain state. We should not need to procedurally call anything to keep things updated.

Yes, I'd say ContractBalance needs to be able to watch the balance live. Not sure if there's a way to do it through Drizzle though or if web3 is the best place to do it.

mwaeckerlin commented 5 years ago

@cds-amal, why do you close this issue??!? Please reopen!

@honestbonsai, ok, I'll add an update. Probably watching blocks, unless we'll find a better solution.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

mwaeckerlin commented 5 years ago

To do for me…

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 5 years ago

This issue has been closed, but can be re-opened if further comments indicate that the problem persists. Feel free to tag maintainers if there is no reply to further comments.