supporting creating extendec contract object

This commit is contained in:
Iuri Matias 2019-10-03 11:45:49 -04:00
parent 85d8d80db8
commit e1b02fa3e6
2 changed files with 34 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.trackEvent("Rating").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,36 @@ export default class Subspace {
})
}
contract(contractInstance) {
if (!contractInstance) {
throw new Error("please pass a contract instance to Subspace.contract()")
}
console.dir(contractInstance)
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);
}
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);