Closed mwaeckerlin closed 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);
@mwaeckerlin Thanks for the sample of your code.
Does this maintain a live update of the balance?
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?
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
BTW, @honestbonsai, it is the same in ContractData
: There is no live update, if the return value of the function would change…!
@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 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?
@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.
@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.
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.
To do for me…
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.
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.
Implement a method to get the balance of a contract.