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
21 changes: 19 additions & 2 deletions src/integration/blockchain/icp/dto/icp.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,29 @@ export interface CandidIcrcGetTransactionsResponse {

// --- Rosetta API response types ---

export interface RosettaTransaction {
export interface RosettaTransactionsResponse {
transactions: RosettaTransactionEntry[];
total_count: number;
next_offset?: number;
}

export interface RosettaTransactionEntry {
block_identifier: { index: number; hash: string };
transaction: {
operations: { status: string }[];
transaction_identifier: { hash: string };
operations: RosettaOperation[];
metadata: { block_height: number; memo: number; timestamp: number };
};
}

export interface RosettaOperation {
operation_identifier: { index: number };
type: string;
status: string;
account: { address: string };
amount?: { value: string; currency: { symbol: string; decimals: number } };
}

// --- Typed raw ledger interfaces (for Actor.createActor results) ---

export interface IcpNativeRawLedger {
Expand Down
46 changes: 43 additions & 3 deletions src/integration/blockchain/icp/icp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import {
IcpTransfer,
IcpTransferQueryResult,
IcrcRawLedger,
RosettaTransaction,
RosettaTransactionsResponse,
} from './dto/icp.dto';
import { InternetComputerWallet } from './icp-wallet';
import { icpNativeLedgerIdlFactory, icrcLedgerIdlFactory } from './icp.idl';
import { InternetComputerUtil } from './icp.util';

const ROSETTA_NETWORK_ID = { blockchain: 'Internet Computer', network: '00000000000000020101' };

export class InternetComputerClient extends BlockchainClient {
private readonly logger = new DfxLogger(InternetComputerClient);

Expand Down Expand Up @@ -206,6 +208,44 @@ export class InternetComputerClient extends BlockchainClient {
};
}

// --- Per-address transfers via Rosetta /search/transactions ---

async getNativeTransfersForAddress(accountIdentifier: string, maxBlock?: number, limit = 50): Promise<IcpTransfer[]> {
const url = `${this.rosettaApiUrl}/search/transactions`;

const body: Record<string, unknown> = {
network_identifier: ROSETTA_NETWORK_ID,
account_identifier: { address: accountIdentifier },
limit,
};
if (maxBlock !== undefined) body.max_block = maxBlock;

const response = await this.http.post<RosettaTransactionsResponse>(url, body);

const transfers: IcpTransfer[] = [];

for (const entry of response.transactions) {
const ops = entry.transaction.operations.filter((op) => op.type === 'TRANSACTION' && op.status === 'COMPLETED');

const creditOp = ops.find((op) => op.amount && BigInt(op.amount.value) > 0n);
const debitOp = ops.find((op) => op.amount && BigInt(op.amount.value) < 0n);

if (!creditOp?.amount || !debitOp?.amount) continue;

transfers.push({
blockIndex: entry.block_identifier.index,
from: debitOp.account.address,
to: creditOp.account.address,
amount: InternetComputerUtil.fromSmallestUnit(BigInt(creditOp.amount.value)),
fee: 0,
memo: BigInt(entry.transaction.metadata.memo),
timestamp: Math.floor(entry.transaction.metadata.timestamp / 1_000_000_000),
});
}

return transfers;
}

// --- Block height & transfers (ICRC-3, for ck-tokens) ---

async getIcrcBlockHeight(canisterId: string): Promise<number> {
Expand Down Expand Up @@ -285,8 +325,8 @@ export class InternetComputerClient extends BlockchainClient {

private async isTxHashComplete(txHash: string): Promise<boolean> {
const url = `${this.rosettaApiUrl}/search/transactions`;
const response = await this.http.post<{ transactions: RosettaTransaction[] }>(url, {
network_identifier: { blockchain: 'Internet Computer', network: '00000000000000020101' },
const response = await this.http.post<RosettaTransactionsResponse>(url, {
network_identifier: ROSETTA_NETWORK_ID,
transaction_identifier: { hash: txHash },
});

Expand Down
10 changes: 9 additions & 1 deletion src/integration/blockchain/icp/services/icp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import nacl from 'tweetnacl';
import { WalletAccount } from '../../shared/evm/domain/wallet-account';
import { SignatureException } from '../../shared/exceptions/signature.exception';
import { BlockchainService } from '../../shared/util/blockchain.service';
import { IcpTransferQueryResult } from '../dto/icp.dto';
import { IcpTransfer, IcpTransferQueryResult } from '../dto/icp.dto';
import { InternetComputerClient } from '../icp-client';

@Injectable()
Expand Down Expand Up @@ -96,6 +96,14 @@ export class InternetComputerService extends BlockchainService {
return this.client.getTransfers(start, count);
}

async getNativeTransfersForAddress(
accountIdentifier: string,
maxBlock?: number,
limit?: number,
): Promise<IcpTransfer[]> {
return this.client.getNativeTransfersForAddress(accountIdentifier, maxBlock, limit);
}

async getIcrcBlockHeight(canisterId: string): Promise<number> {
return this.client.getIcrcBlockHeight(canisterId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { IcpTransferQueryResult } from 'src/integration/blockchain/icp/dto/icp.dto';
import { IcpTransfer, IcpTransferQueryResult } from 'src/integration/blockchain/icp/dto/icp.dto';
import { InternetComputerService } from 'src/integration/blockchain/icp/services/icp.service';
import { Asset } from 'src/shared/models/asset/asset.entity';

Expand All @@ -19,6 +19,14 @@ export class PayInInternetComputerService {
return this.internetComputerService.getTransfers(start, count);
}

async getNativeTransfersForAddress(
accountIdentifier: string,
maxBlock?: number,
limit?: number,
): Promise<IcpTransfer[]> {
return this.internetComputerService.getNativeTransfersForAddress(accountIdentifier, maxBlock, limit);
}

async getIcrcBlockHeight(canisterId: string): Promise<number> {
return this.internetComputerService.getIcrcBlockHeight(canisterId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TronService } from 'src/integration/blockchain/tron/services/tron.servi
import { TatumWebhookService } from 'src/integration/tatum/services/tatum-webhook.service';
import { createCustomAsset } from 'src/shared/models/asset/__mocks__/asset.entity.mock';
import { RepositoryFactory } from 'src/shared/repositories/repository.factory';
import { TransactionRequestService } from 'src/subdomains/supporting/payment/services/transaction-request.service';
import { PayInBitcoinService } from '../../../services/payin-bitcoin.service';
import { PayInInternetComputerService } from '../../../services/payin-icp.service';
import { PayInMoneroService } from '../../../services/payin-monero.service';
Expand Down Expand Up @@ -81,7 +82,7 @@ describe('RegisterStrategyRegistry', () => {
(ConfigModule as Record<string, unknown>).Config = { payment: { internetComputerSeed: 'test' } };
jest.spyOn(InternetComputerUtil, 'createWallet').mockReturnValue({ address: 'test-principal' } as never);
jest.spyOn(InternetComputerUtil, 'accountIdentifier').mockReturnValue('test-account-id');
icpStrategy = new IcpStrategy(mock<PayInInternetComputerService>());
icpStrategy = new IcpStrategy(mock<PayInInternetComputerService>(), mock<TransactionRequestService>());

registry = new RegisterStrategyRegistryWrapper(
bitcoinStrategy,
Expand Down
Loading
Loading