Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active February 19, 2020 00:35
Show Gist options
  • Save alexytiger/bd0341353014018d03b14c4b1cac56d0 to your computer and use it in GitHub Desktop.
Save alexytiger/bd0341353014018d03b14c4b1cac56d0 to your computer and use it in GitHub Desktop.
e-book
public loadPurchaseContract(contractAddress: string):
Observable<PurchaseContractModel> {
const contract: Contract =
new ethers.Contract(contractAddress, this.abi, this.provider.getSigner());
const crObservable: Observable<number | null> =
from(contract.commissionRate()).pipe(
map((commission: ethers.utils.BigNumber) => commission.toNumber()),
// only account with deployer or seller can retrieve this value,
// otherwise the contract will throw error.
catchError((err: Error) => of(null))
);
return zip(
from(contract.key()),
from(contract.seller()),
from(contract.buyer()),
from(contract.owner()),
from(contract.price()),
from(contract.balanceOf()),
from(contract.description()),
from(contract.ipfsImageHash()),
from(contract.state()),
from(crObservable),
)
.pipe(
map(([key, sellerAddress, buyerAddress, ownerAddress, weiPrice,
weiBalance, description, ipfsHash, state, commission]) => {
const product: PurchaseContractModel = {
productKey: utils.parseBytes32String(key as ethers.utils.Arrayish),
contractAddress,
sellerAddress: sellerAddress as string,
buyerAddress: (buyerAddress === ethers.constants.AddressZero) ?
null : buyerAddress as string,
ownerAddress: ownerAddress as string,
price: utils.formatEther(weiPrice as ethers.utils.BigNumberish),
balance: utils.formatEther(weiBalance as ethers.utils.BigNumberish),
description: description as string,
ipfsHash: ipfsHash as string,
state: state as ContractState,
commission: commission ? commission as number : null
};
return product;
}),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment