Skip to content

Commit

Permalink
Adding pricing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
andreespirela committed May 10, 2022
1 parent 4c283ec commit 905ab4c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 1 deletion.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,40 @@ fetchPaginated<PaginatedToken>("tokens").then(async (result) => {
});
```

### Fetching price history
**Signature**
`fetchPriceHistory = async (pair: [string, string], desc?: boolean): Promise<Array<VwapModel>>`

**Usage**

```typescript
import { fetchPriceHistory } from "verto-cache-interface"

fetchPriceHistory(["A", "B"]).then((result) => {
result.forEach((vwap) => {
console.log(vwap.block);
console.log(vwap.vwap);
console.log(vwap.dominantToken);
});
})
```

### Fetching latest price
**Signature**
`fetchLatestPrice = async (pair: [string, string]): Promise<VwapModel | undefined>`

**Usage**

```typescript
import { fetchLatestPrice } from "verto-cache-interface"

fetchLatestPrice(["A", "B"]).then((result) => {
console.log(vwap.block);
console.log(vwap.vwap);
console.log(vwap.dominantToken);
})
```

## Hooks

Hooks are a way to invoke functions and then invoke certain behaviors inside the cache system.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "verto-cache-interface",
"version": "1.2.4",
"version": "1.2.5",
"description": "A communication package with Verto Cache System",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions src/calls/fetch-latest-price.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {VwapModel} from "./types/vwap-model";
import {cacheApiBaseRequest} from "./cache-api-base-request";

export const fetchLatestPrice = async (pair: [string, string]): Promise<VwapModel | undefined> => {
return (await cacheApiBaseRequest<VwapModel>(`token/price/${pair.join(",")}`))?.data || undefined;
}
18 changes: 18 additions & 0 deletions src/calls/fetch-price-history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import axios from "axios";
import {CommonUtils} from "../utils";
import {VwapModel} from "./types/vwap-model";

export const fetchPriceHistory = async (pair: [string, string], desc?: boolean): Promise<Array<VwapModel>> => {
try {
let vwaps: Array<VwapModel> | undefined = ((await axios.get(CommonUtils.buildVwapCdn(pair))).data) as any;
if((vwaps || []).length > 0) {
if(desc) {
vwaps = vwaps?.sort((a,b) => b.block - a.block);
}
return vwaps!;
}
} catch(e) {
console.error(e);
}
return [];
}
5 changes: 5 additions & 0 deletions src/calls/types/vwap-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface VwapModel {
block: number;
vwap: number;
dominantToken: string;
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ export * from './calls/fetch-communities-metadata';
export * from './calls/fetch-artwork-metadata';
export * from './calls/fetch-token-by-id';
export * from './calls/fetch-paginated';
export * from './calls/fetch-price-history';
export * from './calls/fetch-latest-price';
export * from './hooks/cache-contract-hook';
export * from './constants';
4 changes: 4 additions & 0 deletions src/utils/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ export class CommonUtils {
static buildValidityCdn(contractId: string): string {
return `${CacheInterfaceConstants.CONTRACT_CDN}/${contractId}/${contractId}_validity.json`;
}

static buildVwapCdn(pair: [string, string]): string {
return `${CacheInterfaceConstants.CONTRACT_CDN}/vwaps/${pair[0]}_${pair[1]}.json`;
}
}

0 comments on commit 905ab4c

Please sign in to comment.