A progressive Node.js framework for building efficient and scalable server-side applications.
A proof of concept for getting various cryptocurrency prices and caching them.
$ npm install
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
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];
}
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/getting-started-fargate.html