Skip to content
This repository has been archived by the owner on Sep 27, 2019. It is now read-only.

Get Balance of Contract #88

Closed
mwaeckerlin opened this issue Mar 14, 2019 · 13 comments
Closed

Get Balance of Contract #88

mwaeckerlin opened this issue Mar 14, 2019 · 13 comments
Labels
wontfix This will not be worked on

Comments

@mwaeckerlin
Copy link
Contributor

Implement a method to get the balance of a contract.

@mwaeckerlin
Copy link
Contributor Author

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
Copy link
Contributor

honestbonsai commented Mar 25, 2019

@mwaeckerlin Thanks for the sample of your code.

Does this maintain a live update of the balance?

@mwaeckerlin
Copy link
Contributor Author

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
Copy link
Contributor Author

@mwaeckerlin
Copy link
Contributor Author

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

@honestbonsai
Copy link
Contributor

honestbonsai commented Mar 28, 2019

@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
Copy link
Contributor Author

@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
Copy link
Contributor

honestbonsai commented Mar 29, 2019

@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
Copy link
Contributor Author

@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.

@honestbonsai honestbonsai reopened this Apr 1, 2019
@stale
Copy link

stale bot commented May 31, 2019

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 stale bot added the wontfix This will not be worked on label May 31, 2019
@mwaeckerlin
Copy link
Contributor Author

To do for me…

@stale stale bot removed the wontfix This will not be worked on label Jun 2, 2019
@stale
Copy link

stale bot commented Aug 1, 2019

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 stale bot added the wontfix This will not be worked on label Aug 1, 2019
@stale
Copy link

stale bot commented Aug 8, 2019

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.

@stale stale bot closed this as completed Aug 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants