import { getUserAccount } from '@decentraland/EthereumController'; import { getProvider } from '@decentraland/web3-provider'; import abi from '../../contracts/VoucherKernel'; import * as EthConnect from "../../node_modules/eth-connect/esm"; import { RequestManager } from '../../node_modules/eth-connect/eth-connect'; import { ERC1155ERC721Contract } from '../web3/ERC1155ERC721.contract'; import { BosonUtilContract } from '../web3/BosonUtil.contract'; const promiseDataIndexes = { seller: 2, validFrom: 3, validTo: 4, price: 5, idx: 8 } export class VoucherKernelContract { requestManager: RequestManager|undefined; contract: any|undefined; public async connect() { const provider = await getProvider() this.requestManager = new EthConnect.RequestManager(provider); const factory = new EthConnect.ContractFactory(this.requestManager, abi); this.contract = (await factory.at( '0x166eCd96Ec7E977bf74526BDC2E35B0DCC1CF5a7' )) as any; const address = await getUserAccount(); log('current account', address); } private async getTokenContractAddress(): Promise { return this.contract.tokensContract(); } public async explore() { if (this.contract === undefined) { await this.connect(); } const currentBlockNb = await this.requestManager?.eth_blockNumber(); log('current block', currentBlockNb); const currentTimestamp = (new Date()).getTime() / 1000; log('current timestamp', currentTimestamp) const bosonRouterAddress = await this.contract.bosonRouterAddress(); log('VoucherKernel.bosonRouterAddress', bosonRouterAddress); const cashierAddress = await this.contract.cashierAddress(); log('VoucherKernel.cashierAddress', cashierAddress) const typeId = await this.contract.typeId(); log('VoucherKernel.typeId', typeId, typeId.toString()); const tokenContractAddress = await this.getTokenContractAddress(); const eRC1155ERC721 = new ERC1155ERC721Contract(); await eRC1155ERC721.connect(tokenContractAddress); for (let itemId = 0; itemId < parseInt( typeId); itemId++) { const key = await this.contract.getPromiseKey(itemId); const promise = await this.contract.promises(key); const quantity = await this.getItemQuantity(itemId.toString()); if ((currentTimestamp >= promise[promiseDataIndexes.validFrom]) && (currentTimestamp < promise[promiseDataIndexes.validTo]) && (parseInt(quantity) > 0)) { log('supplyId', itemId, 'promiseKey', key, 'quantity', quantity); log('promise', promise[promiseDataIndexes.seller], promise[promiseDataIndexes.validFrom], promise[promiseDataIndexes.validTo], promise[promiseDataIndexes.price], promise[promiseDataIndexes.idx]); } } } public async getItemPrice(itemId: string): Promise { if (this.contract === undefined) { await this.connect(); } const key = await this.contract.getPromiseKey(itemId); const promise = await this.contract.promises(key); return promise[promiseDataIndexes.price]; } public async getItemSeller(itemId: string): Promise { if (this.contract === undefined) { await this.connect(); } const key = await this.contract.getPromiseKey(itemId); const promise = await this.contract.promises(key); return promise[promiseDataIndexes.seller]; } public async getItemQuantity(itemId: string): Promise { const tokenContractAddress = await this.getTokenContractAddress(); const eRC1155ERC721 = new ERC1155ERC721Contract(); await eRC1155ERC721.connect(tokenContractAddress); const seller = await this.getItemSeller(itemId); const bosonUtil = new BosonUtilContract(); await bosonUtil.connect(); const supplyId = await bosonUtil.typeIdToSupplyId(itemId); return await eRC1155ERC721.getItemQuantity(seller, supplyId.toString()); // return '???'; } }