diff --git a/api/src/services/stellar.service.ts b/api/src/services/stellar.service.ts index 39b76beb..383b73de 100644 --- a/api/src/services/stellar.service.ts +++ b/api/src/services/stellar.service.ts @@ -50,32 +50,41 @@ export class StellarService { } } - async buildUnsignedTransaction( + private async buildTransaction( operation: LendingOperation, userAddress: string, assetAddress: string | undefined, amount: string ): Promise { - try { - const account = await this.getAccount(userAddress); - const contract = new Contract(this.contractId); + const account = await this.getAccount(userAddress); + const contract = new Contract(this.contractId); + + const params = [ + new Address(userAddress).toScVal(), + assetAddress ? new Address(assetAddress).toScVal() : xdr.ScVal.scvVoid(), + nativeToScVal(BigInt(amount), { type: 'i128' }), + ]; - const params = [ - new Address(userAddress).toScVal(), - assetAddress ? new Address(assetAddress).toScVal() : xdr.ScVal.scvVoid(), - nativeToScVal(BigInt(amount), { type: 'i128' }), - ]; + const tx = new TransactionBuilder(account, { + fee: BASE_FEE, + networkPassphrase: this.networkPassphrase, + }) + .addOperation(contract.call(CONTRACT_METHODS[operation], ...params)) + .setTimeout(TX_TIMEOUT_SECONDS) + .build(); - const tx = new TransactionBuilder(account, { - fee: BASE_FEE, - networkPassphrase: this.networkPassphrase, - }) - .addOperation(contract.call(CONTRACT_METHODS[operation], ...params)) - .setTimeout(TX_TIMEOUT_SECONDS) - .build(); + const preparedTx = await this.sorobanServer.prepareTransaction(tx); + return preparedTx.toXDR(); + } - const preparedTx = await this.sorobanServer.prepareTransaction(tx); - return preparedTx.toXDR(); + async buildUnsignedTransaction( + operation: LendingOperation, + userAddress: string, + assetAddress: string | undefined, + amount: string + ): Promise { + try { + return await this.buildTransaction(operation, userAddress, assetAddress, amount); } catch (error) { logger.error(`Failed to build unsigned ${operation} transaction:`, error); throw new InternalServerError(`Failed to build ${operation} transaction`);