Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions __tests__/integration/nanocontracts/bet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import {
NanoContractTransactionError,
NanoRequest404Error,
NanoRequestError,
PinRequiredError,
} from '../../../src/errors';
import { OutputType } from '../../../src/wallet/types';
Expand Down Expand Up @@ -517,6 +518,14 @@ describe('full cycle of bet nano contract', () => {
expect(txIds).toContain(tx.hash);
}

// Get NC logs
const ncLogs = await ncApi.getNanoContractLogs(tx1.hash);
expect(ncLogs.success).toBe(true);
expect(ncLogs.nc_id).toBe(tx1.hash);
expect(ncLogs.nc_execution).toBeDefined();
expect(ncLogs.logs).toBeDefined();
expect(typeof ncLogs.logs).toBe('object');

// Get tx history with success
const txHistory = await wallet.getTxHistory();
expect(txHistory).toHaveLength(4);
Expand Down Expand Up @@ -829,5 +838,11 @@ describe('full cycle of bet nano contract', () => {

// Add the pin back in case there are more tests here
ocbWallet.pinCode = oldOcbPin;

// Test getNanoContractLogs with invalid NC ID (should throw error)
await expect(ncApi.getNanoContractLogs('invalid_nc_id')).rejects.toThrow(NanoRequestError);

// Test getNanoContractLogs with a valid hash but not a nano contract
await expect(ncApi.getNanoContractLogs(fundsTx.hash)).rejects.toThrow(NanoRequestError);
});
});
33 changes: 33 additions & 0 deletions src/api/nano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
NanoContractHistoryAPIResponse,
NanoContractStateAPIResponse,
NanoContractStateAPIParameters,
NanoContractLogsAPIResponse,
} from '../nano_contracts/types';

/**
Expand Down Expand Up @@ -286,6 +287,38 @@
throw new NanoRequestError('Error getting nano contract creation list.', error);
}
},

/**
* Call get nano contract logs API
*
* @param id Nano Contract ID
*
* @return {Promise}
* @memberof ApiNanoContracts
* @inner
*/
async getNanoContractLogs(id: string): Promise<NanoContractLogsAPIResponse> {
const data = { id };
const axiosInstance = await createRequestInstance();
try {
const response = await axiosInstance.get(`nano_contract/logs`, { params: data });
const responseData = response.data;
if (response.status === 200 && responseData.success) {
return responseData;
}

throw new NanoRequestError('Error getting nano contract logs.', null, response);

Check warning on line 310 in src/api/nano.ts

View check run for this annotation

Codecov / codecov/patch

src/api/nano.ts#L310

Added line #L310 was not covered by tests
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
const e = error as AxiosError<Error>;
if (e.response && e.response.status === 404) {
throw new NanoRequest404Error('Nano contract not found.', e, e.response);
}
}

throw new NanoRequestError('Error getting nano contract logs.', error);
}
},
};

export default ncApi;
11 changes: 11 additions & 0 deletions src/nano_contracts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,14 @@ export interface NanoContractCreationListAPIResponse {
// Has more contracts to fetch
has_more: boolean;
}

export interface NanoContractLogsAPIResponse {
// If the request succeeded
success: boolean;
// Nano contract ID
nc_id: string;
// Execution metadata
nc_execution: string | null;
// Logs organized by block ID (hex string keys)
logs: Record<string, unknown>;
}
Loading