Skip to content

Commit

Permalink
prepares tx in token simulation flow
Browse files Browse the repository at this point in the history
  • Loading branch information
aristidesstaffieri committed Jan 31, 2024
1 parent 9547e76 commit 88764cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/helper/horizon-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BigNumber from "bignumber.js";
import { ServerResponse } from "http";
import { AssetType, Horizon, TransactionBuilder } from "stellar-sdk";

export const BASE_RESERVE = 0.5;
Expand Down
22 changes: 16 additions & 6 deletions src/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import {
} from "../helper/validate";
import { submitTransaction } from "../helper/horizon-rpc";
import {
Address,
BASE_FEE,
Memo,
MemoType,
Operation,
SorobanRpc,
Transaction,
TransactionBuilder,
XdrLargeInt,
xdr,
} from "stellar-sdk";
import { buildTransfer, simulateTx } from "../helper/soroban-rpc";
Expand Down Expand Up @@ -513,7 +515,7 @@ export async function initApiServer(
pub_key: string;
memo: string;
fee?: string;
params: xdr.ScVal[];
params: Record<string, string>;
network_url: string;
network_passphrase: string;
};
Expand All @@ -540,14 +542,22 @@ export async function initApiServer(
fee: _fee,
networkPassphrase: network_passphrase,
});
const tx = buildTransfer(address, params, memo, builder);
const simulationResponse = await simulateTx<unknown>(
tx as Transaction<Memo<MemoType>, Operation[]>,
server
const _params = [
new Address(params.publicKey).toScVal(), // from
new Address(params.destination).toScVal(), // to
new XdrLargeInt("i128", params.amount).toI128(), // amount
];
const tx = buildTransfer(address, _params, memo, builder);
const simulationResponse = (await server.simulateTransaction(
tx
)) as SorobanRpc.Api.SimulateTransactionSuccessResponse;
const preparedTransaction = SorobanRpc.assembleTransaction(
tx,
simulationResponse
);
const data = {
simulationResponse,
raw: tx.toXDR(),
preparedTransaction: preparedTransaction.build().toXDR(),
};
reply.code(200).send(data);
} catch (error) {
Expand Down

0 comments on commit 88764cf

Please sign in to comment.