Skip to content

ZacharyCouchman/crypto-prices

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

A proof of concept for getting various cryptocurrency prices and caching them.

Installation

$ npm install

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

ChainLink demo

async loadEthPriceFromChainlink(): Promise<any> {
    const provider = new ethers.providers.JsonRpcProvider(MAINNET_INFURA_URL);
    const addr = MAINNET_ETH_USD_CONTRACT;
    const priceFeed = new ethers.Contract(
      addr,
      aggregatorV3InterfaceABI,
      provider,
    );

    let decimals: number;
    let latestRoundData: any;
    let price: string;

    try {
      decimals = await priceFeed.decimals();
      latestRoundData = await priceFeed.latestRoundData();
      price = parseFloat(
        ethers.utils.formatUnits(
          BigNumber.from(latestRoundData.answer),
          decimals,
        ),
      ).toFixed(2); // could use currency formatter here for USD
      console.log('Latest Round Data', latestRoundData);
      console.log('ETH/USD price: ', price);
      console.log(
        'Updated at',
        latestRoundData.updatedAt.toBigInt().toString(),
      );
    } catch (exception) {
      console.error(exception);
      throw new Error('Failed to retrieve price data from Chainlink');
    }

    const ethUsdPrice = {
      label: 'ETH/USD',
      price,
      updatedAt: latestRoundData.updatedAt.toBigInt().toString(),
    } as PriceFeedCacheItem;
    console.log('caching item: ', ethUsdPrice);
    try {
      await this.cacheManager.set('eth-usd-price', ethUsdPrice, { ttl: 3600 });
      console.log('cached', await this.cacheManager.get('eth-usd-price'));
    } catch (ex) {
      console.error(ex);
    }

    return 'Successfully added to memory cache and mongodb';
  }


  async getCryptoPrices(): Promise<PriceFeedCacheItem[]> {
    const ethUsdPrice = await this.cacheManager.get<PriceFeedCacheItem>(
      'eth-usd-price',
    );
    return [ethUsdPrice];
  }

Deployment

https://docs.github.com/en/actions/deployment/deploying-to-your-cloud-provider/deploying-to-amazon-elastic-container-service

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/getting-started-fargate.html

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published