Skip to content

Commit

Permalink
fixes query param validator to accept multiple contract IDs, tweaks h…
Browse files Browse the repository at this point in the history
…istory transformer to skip over host fn invocations that are not invokeHostFn
  • Loading branch information
aristidesstaffieri committed Jan 31, 2024
1 parent 1d81064 commit 6d23446
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
11 changes: 5 additions & 6 deletions src/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ export async function initApiServer(
},
querystring: {
["contract_ids"]: {
type: "string",
validator: (qStr: string) => qStr.split(",").every(isContractId),
type: "array",
validator: (qStr: Array<unknown>) =>
qStr.map((q) => String(q)).every(isContractId),
},
["network"]: {
type: "string",
Expand All @@ -181,7 +182,7 @@ export async function initApiServer(
request: FastifyRequest<{
Params: { ["pubKey"]: string };
Querystring: {
["contract_ids"]: string;
["contract_ids"]: string[];
["network"]: NetworkNames;
["horizon_url"]?: string;
["soroban_url"]?: string;
Expand All @@ -191,9 +192,7 @@ export async function initApiServer(
) => {
const pubKey = request.params["pubKey"];
const { network, horizon_url, soroban_url } = request.query;
const contractIds = request.query["contract_ids"]
? request.query["contract_ids"].split(",")
: [];
const contractIds = request.query["contract_ids"] || ([] as string[]);
const { data, error } = await mercuryClient.getAccountBalances(
pubKey,
contractIds,
Expand Down
66 changes: 38 additions & 28 deletions src/service/mercury/helpers/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,34 +866,44 @@ const transformAccountHistory = async (
): Promise<Partial<Horizon.ServerApi.OperationRecord>[]> => {
const invokeHostFnEdges =
rawResponse.data?.invokeHostFnByPublicKey.edges || [];
const invokeHostFn = invokeHostFnEdges.map((edge) => {
const hostFn = xdr.HostFunction.fromXDR(
Buffer.from(edge.node.hostFunction, "base64")
);
const invocation = hostFn.invokeContract();
const fnName = invocation.functionName().toString();
return {
auth: edge.node.auth,
created_at: new Date(
edge.node.txInfoByTx.ledgerByLedger.closeTime * 1000
).toISOString(),
sorobanMeta: edge.node.sorobanMeta,
source_account: edge.node.accountBySource.publickey,
tx: edge.node.tx,
type: "invoke_host_function",
type_i: 24,
id: edge.node.opId,
transaction_attr: {
contractId: StrKey.encodeContract(
invocation.contractAddress().contractId()
),
fnName,
args: getOpArgs(fnName, invocation.args()),
operation_count: edge.node.txInfoByTx.opCount,
fee_charged: edge.node.txInfoByTx.fee,
},
} as Partial<Horizon.ServerApi.InvokeHostFunctionOperationRecord>;
});
const invokeHostFn = invokeHostFnEdges
.map((edge) => {
const hostFn = xdr.HostFunction.fromXDR(
Buffer.from(edge.node.hostFunction, "base64")
);

// we only want to keep these history entries if the Host Fn is
// for invoking a contract, we dont show contract create or wasm upload in wallet history right now.
try {
const invocation = hostFn.invokeContract();
const fnName = invocation.functionName().toString();
return {
auth: edge.node.auth,
created_at: new Date(
edge.node.txInfoByTx.ledgerByLedger.closeTime * 1000
).toISOString(),
sorobanMeta: edge.node.sorobanMeta,
source_account: edge.node.accountBySource.publickey,
tx: edge.node.tx,
type: "invoke_host_function",
type_i: 24,
id: edge.node.opId,
transaction_attr: {
contractId: StrKey.encodeContract(
invocation.contractAddress().contractId()
),
fnName,
args: getOpArgs(fnName, invocation.args()),
operation_count: edge.node.txInfoByTx.opCount,
fee_charged: edge.node.txInfoByTx.fee,
},
} as Partial<Horizon.ServerApi.InvokeHostFunctionOperationRecord>;
} catch (error) {
return null;
}
})
// filters out cases where the Host fn is not a contract invocation
.filter(Boolean);

const createAccountEdges =
rawResponse.data?.createAccountByPublicKey.edges || [];
Expand Down

0 comments on commit 6d23446

Please sign in to comment.