Merge pull request #45 from status-im/extend_contract

Extend contract
This commit is contained in:
Iuri Matias 2019-10-03 16:00:09 -04:00 committed by GitHub
commit 8cfb05c191
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View File

@ -22,18 +22,19 @@ class App extends React.Component {
await subspace.init();
Product = await ProductContract.getInstance();
const rating$ = subspace.trackEvent(Product, "Rating").map("rating").pipe(map(x => parseInt(x)));
Product = subspace.contract(Product)
const rating$ = Product.events.Rating.track().map("rating").pipe(map(x => parseInt(x)));
window.Product = Product;
window.web3 = web3;
this.setState({
title: subspace.trackProperty(Product, "products", 0).map('title'),
title: Product.trackProperty("products", 0).map('title'),
averageRating: rating$.pipe($average()),
minRating: rating$.pipe($min()),
maxRating: rating$.pipe($max()),
last5Ratings: rating$.pipe($latest(5)),
balance: subspace.trackBalance(Product.options.address)
balance: Product.trackBalance()
});
}

View File

@ -49,6 +49,38 @@ export default class Subspace {
})
}
contract(contractInstance) {
if (!contractInstance) {
throw new Error("please pass a contract instance to Subspace.contract()")
}
let address = (contractInstance.options && contractInstance.options.address) || contractInstance.address || contractInstance.deployedAddress;
let abi = (contractInstance.options && contractInstance.options.jsonInterface) || contractInstance.abi || contractInstance.abiDefinition;
let from = (contractInstance.options && contractInstance.options.from) || contractInstance.from || contractInstance.defaultAddress || this.web3.defaultAccount;
let gas = (contractInstance.options && contractInstance.options.gas) || contractInstance.gas || contractInstance.gas || "800000";
const SubspaceContract = new this.web3.Contract(abi, {from, gas});
SubspaceContract.options.address = address;
SubspaceContract.trackEvent = (eventName, filterConditionsOrCb) => {
return this.trackEvent(SubspaceContract, eventName, filterConditionsOrCb);
}
Object.keys(SubspaceContract.events).forEach(ev => {
SubspaceContract.events[ev].track = (filterConditionsOrCb) => this.trackEvent(SubspaceContract, ev, filterConditionsOrCb);
});
SubspaceContract.trackProperty = (propName, methodArgs, callArgs) => {
return this.trackProperty(SubspaceContract, propName, methodArgs, callArgs);
}
SubspaceContract.trackBalance = (erc20Address) => {
return this.trackBalance(SubspaceContract.options.address, erc20Address);
}
return SubspaceContract;
}
// TODO: get contract abi/address instead
trackEvent(contractInstance, eventName, filterConditionsOrCb) {
let returnSub = this.eventSyncer.track(contractInstance, eventName, filterConditionsOrCb, this.latestBlockNumber - this.options.refreshLastNBlocks);