@polkadot/types/interfaces#Moment TypeScript Examples

The following examples show how to use @polkadot/types/interfaces#Moment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: council.ts    From community-repo with GNU General Public License v3.0 6 votes vote down vote up
async function computeAverageBlockProductionTime(
  api: ApiPromise,
  range: BlockRange
) {
  let startTimestamp = (await api.query.timestamp.now.at(
    range.startBlockHash
  )) as Moment;
  let endTimestamp = (await api.query.timestamp.now.at(
    range.endBlockHash
  )) as Moment;
  let newBlocks = range.endBlockHeight - range.startBlockHeight;
  return (
    (Number(endTimestamp.toBigInt()) - Number(startTimestamp.toBigInt())) /
    1000 /
    newBlocks
  );
}
Example #2
Source File: TimeNow.tsx    From crust-apps with Apache License 2.0 6 votes vote down vote up
function TimeNow ({ children, className = '', label, value }: Props): React.ReactElement<Props> {
  const { api } = useApi();
  const timestamp = useCall<Moment>(!value && api.query.timestamp.now);
  const [now, setNow] = useState<BN | undefined>();

  useEffect((): void => {
    setNow(value || timestamp);
  }, [timestamp, value]);

  return (
    <div className={className}>
      {label || ''}
      <Elapsed value={now} />
      {children}
    </div>
  );
}
Example #3
Source File: TimeNow.tsx    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
function TimeNow({ children, className = '', label, value }: Props): React.ReactElement<Props> | null {
  const { api } = useApi();
  const timestamp = useCall<Moment>(!value && api.query.timestamp?.now);
  const [now, setNow] = useState<BN | undefined>();

  useEffect((): void => {
    setNow(value || timestamp);
  }, [timestamp, value]);

  if (!now) {
    return null;
  }

  return (
    <div className={className}>
      {label || ''}
      <Elapsed value={now} />
      {children}
    </div>
  );
}
Example #4
Source File: oracle.ts    From interbtc-api with Apache License 2.0 5 votes vote down vote up
async getValidUntil<U extends CurrencyUnit>(counterCurrency: Currency<U>): Promise<Date> {
        const oracleKey = createExchangeRateOracleKey(this.api, counterCurrency);
        const validUntil = await this.api.query.oracle.validUntil(oracleKey);
        return validUntil.isSome ? convertMoment(validUntil.value as Moment) : Promise.reject("No such oracle key");
    }
Example #5
Source File: encoding.ts    From interbtc-api with Apache License 2.0 5 votes vote down vote up
export function convertMoment(moment: Moment): Date {
    return new Date(moment.toNumber());
}
Example #6
Source File: index.ts    From community-repo with GNU General Public License v3.0 4 votes vote down vote up
processBlock = async (api: ApiPromise, id: number) => {

  const exists = (await Block.findByPk(id))
  if (exists) return exists.get({plain: true})

  processing = `block ${id}`
  console.log(processing)

  const previousBlockModel = (await Block.findByPk(id - 1))
  let lastBlockTimestamp = 0
  let lastBlockHash = ''
  let lastEraId = 0
  if (previousBlockModel) {
    const previousDbBlock = previousBlockModel.get({plain: true})
    lastBlockTimestamp = previousDbBlock.timestamp.getTime();
    lastBlockHash = previousDbBlock.hash
    lastEraId = previousDbBlock.eraId
  } else {
    lastBlockHash = await getBlockHash(api, id - 1);
    lastBlockTimestamp = await getTimestamp(api, lastBlockHash);
    lastEraId = await getEraAtHash(api, lastBlockHash)
  }
  const hash = await getBlockHash(api, id)
  const currentBlockTimestamp = await getTimestamp(api, hash)
  const extendedHeader = await api.derive.chain.getHeader(hash) as HeaderExtended

  const eraId = await getEraAtHash(api, hash)
  let chainTime
  if(eraId - lastEraId === 1) {
    console.log('This block marks the start of new era. Updating the previous era stats')
    const {total, individual} = await api.query.staking.erasRewardPoints.at(lastBlockHash, lastEraId)
    const slots = (await api.query.staking.validatorCount.at(lastBlockHash)).toNumber()
    const newEraTime = (await api.query.timestamp.now.at(hash)) as Moment
    chainTime = moment(newEraTime.toNumber())

    await Era.upsert({ // update stats for previous era
      id: lastEraId,
      slots: slots,
      stake: await api.query.staking.erasTotalStake.at(hash, lastEraId),
      eraPoints: total
    })


    const validatorStats = await ValidatorStats.findAll({where: {eraId: lastEraId}, include: [Account]})
    for (let stat of validatorStats) {
      const validatorStats = stat.get({plain: true})
      const validatorAccount = validatorStats.account.key
      console.log(validatorAccount)
      let pointVal = 0;
      for(const [key, value] of individual.entries()) {
        if(key == validatorAccount) {
          pointVal = value.toNumber()
          break
        }
      }

      const {total, own} = await api.query.staking.erasStakers.at(lastBlockHash, lastEraId, validatorAccount)

      ValidatorStats.upsert({
        eraId: lastEraId, 
        accountId: validatorStats.accountId, 
        stake_own: own, 
        stake_total: total, 
        points: pointVal,
        commission: (await api.query.staking.erasValidatorPrefs.at(lastBlockHash, eraId, validatorAccount)).commission.toNumber() / 10000000
      })
    }
  
  }
  const [era, created] = await Era.upsert({ // add the new are with just a timestamp of its first block
    id: eraId,
    timestamp: chainTime
  }, {returning: true})

  const block = await Block.upsert({
    id: id, 
    hash: hash,
    timestamp: moment.utc(currentBlockTimestamp).toDate(),
    blocktime: (currentBlockTimestamp - lastBlockTimestamp),
    eraId: era.get({plain: true}).id,
    validatorId: (await getAccount(extendedHeader.author.toHuman())).id
  }, {returning: true})

  await importEraAtBlock(api, id, hash, era)
  processEvents(api, id, eraId, hash)

  return block
}