support using only abi & address; fix default from

This commit is contained in:
Iuri Matias 2019-10-03 18:40:21 -04:00
parent 4932b9c3bc
commit 45d13547c4
2 changed files with 15 additions and 7 deletions

View File

@ -4,7 +4,8 @@ import { $average, $max, $min, $latest } from "@status-im/subspace";
import { map } from 'rxjs/operators';
import ProductComponent from "./ProductComponent";
import web3 from './web3';
import ProductContract from './MyContract';
import {abi, address} from './contract.json'
let Product;
@ -21,12 +22,10 @@ class App extends React.Component {
const subspace = new Subspace(web3.currentProvider);
await subspace.init();
Product = await ProductContract.getInstance();
Product = subspace.contract(Product)
const rating$ = Product.events.Rating.track().map("rating").pipe(map(x => parseInt(x)));
// Product = subspace.contract(ContractInstance); // would also work
Product = subspace.contract({abi, address});
window.Product = Product;
window.web3 = web3;
const rating$ = Product.events.Rating.track().map("rating").pipe(map(x => parseInt(x)));
this.setState({
title: Product.methods.products(0).track().map('title'),
@ -47,7 +46,8 @@ class App extends React.Component {
};
sendFunds = async () => {
await web3.eth.sendTransaction({value: this.state.contractFunds, to: Product.options.address});
let accounts = await web3.eth.getAccounts();
await web3.eth.sendTransaction({value: this.state.contractFunds, to: Product.options.address, from: accounts[0]});
};
render() {

View File

@ -61,6 +61,14 @@ export default class Subspace {
const SubspaceContract = new this.web3.Contract(abi, {from, gas});
SubspaceContract.options.address = address;
SubspaceContract.options.from = from;
if (!from) {
setTimeout(async () => {
const accounts = await web3.eth.getAccounts();
SubspaceContract.options.from = accounts[0];
}, 100);
}
SubspaceContract.trackEvent = (eventName, filterConditionsOrCb) => {
return this.trackEvent(SubspaceContract, eventName, filterConditionsOrCb);