Skip to content

Commit

Permalink
added price api
Browse files Browse the repository at this point in the history
  • Loading branch information
bonkman22 committed Oct 2, 2024
1 parent aafd030 commit ce4e289
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hop.ag/sdk",
"version": "3.2.7",
"version": "3.3.0",
"description": "Official Hop SDK to access Hop Aggregator",
"type": "module",
"main": "dist/cjs/index.js",
Expand All @@ -17,6 +17,7 @@
"test:tx": "npx tsx src/tests/tx.test.ts",
"test:tokens": "npx tsx src/tests/tokens.test.ts",
"test:base": "npx tsx src/tests/base.test.ts",
"test:price": "npx tsx src/tests/price.test.ts",
"build": "npm run build:esm && npm run build:cjs",
"build:esm": "tsc && echo '{\"type\":\"module\"}' > dist/esm/package.json",
"build:cjs": "tsc --project tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
Expand Down
6 changes: 6 additions & 0 deletions src/sdk/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "./routes/quote.js";
import { fetchTx, GetTxParams, GetTxResponse } from "./routes/tx.js";
import { fetchTokens, GetTokensResponse } from "./routes/tokens.js";
import { fetchPrice, GetPriceParams, GetPriceResponse } from "./routes/price.js";

export interface HopApiOptions {
api_key: string;
Expand Down Expand Up @@ -67,4 +68,9 @@ export class HopApi {
async fetchTokens(): Promise<GetTokensResponse> {
return fetchTokens(this);
}

async fetchPrice(price: GetPriceParams): Promise<GetPriceResponse> {
return fetchPrice(this, price);
}

}
49 changes: 49 additions & 0 deletions src/sdk/routes/price.ts
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");

}
6 changes: 6 additions & 0 deletions src/sdk/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ export const tokensResponseSchema = z.object({
token_order: z.nullable(z.number())
}))
})

export const priceResponseSchema = z.object({
coin_type: z.string(),
price_sui: z.number(),
sui_price: z.number()
});
19 changes: 19 additions & 0 deletions src/tests/price.test.ts
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();
34 changes: 24 additions & 10 deletions src/tests/tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,48 @@ import { getFullnodeUrl, SuiClient } from "@mysten/sui/client";
async function txTest(): Promise<void> {
const sui_client = new SuiClient({ url: getFullnodeUrl('mainnet') });
const api = new HopApi(getFullnodeUrl("mainnet"), {
api_key: "hopapisKX7I30wPvo5YfN8Vx5P9r4cPh3nzVcS",
fee_bps: 10,
fee_wallet: "0xa89611f02060bad390103e783a62c88725b47059e6460cf0d2f3ca32e2559641"
api_key: "",
fee_bps: 0,
// fee_wallet: "0xa89611f02060bad390103e783a62c88725b47059e6460cf0d2f3ca32e2559641"
});

const total_balance = await sui_client.getBalance({
owner: "0x90afb76a8bfca719dadb4e77de50f65bba5327397cc7550d9c7b816907958943",
coinType: "0x71bd8693b1d17688e6671c9208e5e2499a95dce65ec690373002a72e6649f0e6::sure::SURE"
});

let amount_in = BigInt(total_balance.totalBalance);

const quote_result = await api.fetchQuote({
// @ts-ignore
amount_in: 1_000_000_000n,
token_in: "0x2::sui::SUI",
token_out:
"0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
amount_in,
token_out: "0x2::sui::SUI",
token_in:
"0x71bd8693b1d17688e6671c9208e5e2499a95dce65ec690373002a72e6649f0e6::sure::SURE",
});

console.log("amount_in", amount_in);
console.log("quote_result.trade.amount_in", quote_result.trade.amount_in.amount);

console.log("quote_result", quote_result);

const tx_result = await api.fetchTx({
// @ts-ignore
trade: quote_result.trade,
sui_address:
"0x4466fe25550f648a4acd6823a90e1f96c77e1d37257ee3ed2d6e02a694984f73",
max_slippage_bps: 100,
"0x90afb76a8bfca719dadb4e77de50f65bba5327397cc7550d9c7b816907958943",
return_output_coin_argument: false,
// gas_budget: 1e7
// max_slippage_bps: 100,
// return_output_coin_argument: true,
// base_transaction: tx,
// input_coin_argument: coin,
});

// console.log("tx_result", tx_result);

const result = await sui_client.dryRunTransactionBlock({ transactionBlock: await tx_result.transaction.build({ client: sui_client }) });
console.log("result", JSON.stringify(result, null, 2));
// console.log("result", JSON.stringify(result, null, 2));
}

txTest();
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export type { GetQuoteParams, GetQuoteResponse } from "../sdk/routes/quote.js";
export type { VerifiedToken, GetTokensResponse } from "../sdk/routes/tokens.js";
export type { GetTxParams, GetTxResponse } from "../sdk/routes/tx.js";
export type { GetPriceParams, GetPriceResponse } from "../sdk/routes/price.js";

export * from "../sdk/types/trade.js";

Expand Down

0 comments on commit ce4e289

Please sign in to comment.