From b82bdd14f8bf08a6377de1f10348dc164980c0db Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 16 Oct 2020 14:20:13 +0200 Subject: [PATCH] Project eviction based on rent (#3885) --- .../page-contracts/src/Contracts/Contract.tsx | 26 ++++++++++++++++--- .../page-contracts/src/Contracts/index.tsx | 1 + 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/page-contracts/src/Contracts/Contract.tsx b/packages/page-contracts/src/Contracts/Contract.tsx index 53cf8d57cb..f7d6879059 100644 --- a/packages/page-contracts/src/Contracts/Contract.tsx +++ b/packages/page-contracts/src/Contracts/Contract.tsx @@ -3,15 +3,16 @@ import { ContractCallOutcome } from '@polkadot/api-contract/types'; import { ActionStatus } from '@polkadot/react-components/Status/types'; -import { ContractInfo } from '@polkadot/types/interfaces'; +import { BlockNumber, ContractInfo } from '@polkadot/types/interfaces'; -import React, { useCallback } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; import keyring from '@polkadot/ui-keyring'; import { ContractPromise } from '@polkadot/api-contract'; import { AddressInfo, AddressMini, Button, Forget } from '@polkadot/react-components'; import { useApi, useCall, useToggle } from '@polkadot/react-hooks'; +import { BlockToTime } from '@polkadot/react-query'; import { Option } from '@polkadot/types'; -import { isUndefined } from '@polkadot/util'; +import { formatNumber, isFunction, isUndefined } from '@polkadot/util'; import Messages from '../shared/Messages'; import { useTranslation } from '../translate'; @@ -30,9 +31,20 @@ function transformInfo (optInfo: Option): ContractInfo | null { function Contract ({ className, contract, index, onCall }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); + const bestNumber = useCall(api.derive.chain.bestNumber); const info = useCall(api.query.contracts.contractInfoOf, [contract.address], { transform: transformInfo }); + const [evictAt, setEvictAt] = useState(null); const [isForgetOpen, toggleIsForgetOpen] = useToggle(); + useEffect((): void => { + if (info && isFunction(api.rpc.contracts?.rentProjection)) { + api.rpc.contracts + .rentProjection(contract.address) + .then((value) => setEvictAt(value.unwrapOr(null))) + .catch(() => undefined); + } + }, [api, contract, info]); + const _onCall = useCallback( (messageIndex: number, resultCb: (messageIndex: number, result?: ContractCallOutcome) => void) => onCall(index, messageIndex, resultCb), [index, onCall] @@ -97,6 +109,14 @@ function Contract ({ className, contract, index, onCall }: Props): React.ReactEl : t('Not on-chain') )} + + {bestNumber && evictAt && ( + <> + + #{formatNumber(evictAt)} + + )} +