-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { HopApi } from "../api.js"; | ||
import { makeAPIRequest } from "../util.js"; | ||
import { priceResponseSchema } from "../types/api.js"; | ||
|
||
export interface GetPriceParams { | ||
coin_type: string; | ||
} | ||
|
||
export interface GetPriceResponse { | ||
coin_type: string; | ||
|
||
price_sui: number; // returns sui per token | ||
price_usd: number; // returns usd per token | ||
|
||
sui_price: number; // returns usdc per token | ||
|
||
} | ||
|
||
export async function fetchPrice( | ||
client: HopApi, | ||
params: GetPriceParams | ||
): Promise<GetPriceResponse> { | ||
const response = await makeAPIRequest({ | ||
route: `price`, | ||
options: { | ||
api_key: client.options.api_key, | ||
hop_server_url: client.options.hop_server_url, | ||
data: { | ||
coin_type: params.coin_type, | ||
}, | ||
method: "post", | ||
}, | ||
responseSchema: priceResponseSchema, | ||
}); | ||
|
||
if (response?.coin_type) { | ||
let price_usd = response.price_sui * response.sui_price; | ||
|
||
return { | ||
coin_type: response?.coin_type, | ||
price_sui: response?.price_sui, | ||
price_usd, | ||
sui_price: response?.sui_price | ||
}; | ||
} | ||
|
||
throw new Error("Unable to get price"); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { HopApi } from "../index"; | ||
import { getFullnodeUrl } from "@mysten/sui/client"; | ||
|
||
// @ts-ignore | ||
async function priceTest() { | ||
const api = new HopApi(getFullnodeUrl("mainnet"), { | ||
api_key: "hopapisKX7I30wPvo5YfN8Vx5P9r4cPh3nzVcS", | ||
fee_bps: 0, | ||
// hop_server_url: "http://localhost:3002/api/v2" | ||
}); | ||
|
||
const result = await api.fetchPrice({ | ||
coin_type: "0x06b145d0322e389d6225f336ab57bba4c67e4e701bd6c6bc959d90675900a17e::meow::MEOW" | ||
}); | ||
|
||
console.log("result", result); | ||
} | ||
|
||
priceTest(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters