Skip to content

Commit

Permalink
v3.2.3 - fixed base tx builder
Browse files Browse the repository at this point in the history
  • Loading branch information
bonkman22 committed Sep 1, 2024
1 parent 4270678 commit 98254a4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 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.2",
"version": "3.2.3",
"description": "Official Hop SDK to access Hop Aggregator",
"type": "module",
"main": "dist/cjs/index.js",
Expand All @@ -16,6 +16,7 @@
"test:quote": "npx tsx src/tests/quote.test.ts",
"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",
"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
14 changes: 10 additions & 4 deletions src/sdk/routes/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,16 @@ export async function fetchTx(
// @ts-ignore
{ [params.input_coin_argument.$kind]: ensure_array(params.input_coin_argument[params.input_coin_argument.$kind]) } :
undefined;
let base_transaction = params.base_transaction ? toB64(await params.base_transaction?.build({
client: client.client,
onlyTransactionKind: true
})) : undefined;
let base_transaction = undefined;

if(params.base_transaction) {
const built_tx_array = await params.base_transaction.build({
client: client.client,
onlyTransactionKind: true
});

base_transaction = toB64(built_tx_array);
}

const compileRequest = compileRequestSchema.parse({
trade: params.trade,
Expand Down
69 changes: 69 additions & 0 deletions src/tests/base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// @ts-ignore
import { HopApi } from "@hop.ag/sdk";
import {
normalizeStructTag,
normalizeSuiAddress,
SUI_TYPE_ARG,
} from "@mysten/sui/utils";

import { getFullnodeUrl } from "@mysten/sui/client";
import { Transaction } from "@mysten/sui/transactions";

async function baseTest() {
const api = new HopApi(getFullnodeUrl("mainnet"), {
api_key: "hopapisKX7I30wPvo5YfN8Vx5P9r4cPh3nzVcS",
fee_bps: 0,
hop_server_url: "http://localhost:3002/api/v2",
});

const tx = new Transaction();
const coinInType = SUI_TYPE_ARG;
const address =
"0xc23ea8e493616b1510d9405ce05593f8bd1fb30f44f92303ab2c54f6c8680ecb";
const coinOutType =
"0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN";
// @ts-ignore
const coinInAmount = 100_000_000n;
const coinIn = tx.splitCoins(tx.gas, [tx.pure.u64(coinInAmount)]);

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

// console.log(">> step 1 :: ", {
// tx,
// coinIn,
// address,
// coinInType,
// coinOutType,
// coinInAmount,
// });

const { trade } = await api.fetchQuote({
amount_in: coinInAmount,
token_in: normalizeStructTag(coinInType),
token_out: normalizeStructTag(coinOutType),
});

tx.setSender(address);

// console.log(">> step 2 :: ", trade);

const response = await api.fetchTx({
trade,
sponsored: true,
gas_budget: 1e8,
base_transaction: tx,
input_coin_argument: coinIn.Result,
// return_output_coin_argument: true,
sui_address: normalizeSuiAddress(address),
});

console.log("result", response);
}

baseTest();
3 changes: 2 additions & 1 deletion src/tests/quote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { getFullnodeUrl } from "@mysten/sui/client";
async function quoteTest() {
const api = new HopApi(getFullnodeUrl("mainnet"), {
api_key: "hopapisKX7I30wPvo5YfN8Vx5P9r4cPh3nzVcS",
fee_bps: 0
fee_bps: 0,
hop_server_url: ""
});

const result = await api.fetchQuote({
Expand Down

0 comments on commit 98254a4

Please sign in to comment.