Skip to content

Commit 905ab4c

Browse files
committed
Adding pricing functions
1 parent 4c283ec commit 905ab4c

File tree

7 files changed

+70
-1
lines changed

7 files changed

+70
-1
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,40 @@ fetchPaginated<PaginatedToken>("tokens").then(async (result) => {
548548
});
549549
```
550550

551+
### Fetching price history
552+
**Signature**
553+
`fetchPriceHistory = async (pair: [string, string], desc?: boolean): Promise<Array<VwapModel>>`
554+
555+
**Usage**
556+
557+
```typescript
558+
import { fetchPriceHistory } from "verto-cache-interface"
559+
560+
fetchPriceHistory(["A", "B"]).then((result) => {
561+
result.forEach((vwap) => {
562+
console.log(vwap.block);
563+
console.log(vwap.vwap);
564+
console.log(vwap.dominantToken);
565+
});
566+
})
567+
```
568+
569+
### Fetching latest price
570+
**Signature**
571+
`fetchLatestPrice = async (pair: [string, string]): Promise<VwapModel | undefined>`
572+
573+
**Usage**
574+
575+
```typescript
576+
import { fetchLatestPrice } from "verto-cache-interface"
577+
578+
fetchLatestPrice(["A", "B"]).then((result) => {
579+
console.log(vwap.block);
580+
console.log(vwap.vwap);
581+
console.log(vwap.dominantToken);
582+
})
583+
```
584+
551585
## Hooks
552586

553587
Hooks are a way to invoke functions and then invoke certain behaviors inside the cache system.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "verto-cache-interface",
3-
"version": "1.2.4",
3+
"version": "1.2.5",
44
"description": "A communication package with Verto Cache System",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/calls/fetch-latest-price.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {VwapModel} from "./types/vwap-model";
2+
import {cacheApiBaseRequest} from "./cache-api-base-request";
3+
4+
export const fetchLatestPrice = async (pair: [string, string]): Promise<VwapModel | undefined> => {
5+
return (await cacheApiBaseRequest<VwapModel>(`token/price/${pair.join(",")}`))?.data || undefined;
6+
}

src/calls/fetch-price-history.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import axios from "axios";
2+
import {CommonUtils} from "../utils";
3+
import {VwapModel} from "./types/vwap-model";
4+
5+
export const fetchPriceHistory = async (pair: [string, string], desc?: boolean): Promise<Array<VwapModel>> => {
6+
try {
7+
let vwaps: Array<VwapModel> | undefined = ((await axios.get(CommonUtils.buildVwapCdn(pair))).data) as any;
8+
if((vwaps || []).length > 0) {
9+
if(desc) {
10+
vwaps = vwaps?.sort((a,b) => b.block - a.block);
11+
}
12+
return vwaps!;
13+
}
14+
} catch(e) {
15+
console.error(e);
16+
}
17+
return [];
18+
}

src/calls/types/vwap-model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface VwapModel {
2+
block: number;
3+
vwap: number;
4+
dominantToken: string;
5+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ export * from './calls/fetch-communities-metadata';
3030
export * from './calls/fetch-artwork-metadata';
3131
export * from './calls/fetch-token-by-id';
3232
export * from './calls/fetch-paginated';
33+
export * from './calls/fetch-price-history';
34+
export * from './calls/fetch-latest-price';
3335
export * from './hooks/cache-contract-hook';
3436
export * from './constants';

src/utils/common-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ export class CommonUtils {
99
static buildValidityCdn(contractId: string): string {
1010
return `${CacheInterfaceConstants.CONTRACT_CDN}/${contractId}/${contractId}_validity.json`;
1111
}
12+
13+
static buildVwapCdn(pair: [string, string]): string {
14+
return `${CacheInterfaceConstants.CONTRACT_CDN}/vwaps/${pair[0]}_${pair[1]}.json`;
15+
}
1216
}

0 commit comments

Comments
 (0)