Skip to content

Commit

Permalink
Fix SCI input tx selection, and update SCI test accts for cur balances (
Browse files Browse the repository at this point in the history
  • Loading branch information
carybakker committed Mar 1, 2023
1 parent f5945ca commit 2159c78
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
14 changes: 10 additions & 4 deletions Sources/Transaction/SignedContingentInputBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//
// Copyright (c) 2020-2022 MobileCoin. All rights reserved.
//

// swiftlint:disable function_parameter_count function_default_parameter_at_end
// swiftlint:disable closure_body_length

import Foundation
import LibMobileCoin

Expand Down Expand Up @@ -115,8 +113,16 @@ extension SignedContingentInputBuilder {
rng: MobileCoinRng
) -> Result<SignedContingentInput, TransactionBuilderError> {
let builder: SignedContingentInputBuilder
guard let input: PreparedTxInput = inputs.first else {
return .failure(.invalidInput("Insufficient funds"))

let possibleInputs = inputs.filter { $0.knownTxOut.amount.value >= amountToSend.value }
guard possibleInputs.count > 0 else {
return .failure(.invalidInput("Defragmentation Required"))
}

let input = possibleInputs.min { $0.knownTxOut.amount.value < $1.knownTxOut.amount.value }

guard let input = input else {
return .failure(.invalidInput("Unexpected error - unable to find valid input"))
}

let subaddressIndex = input.subaddressIndex
Expand Down
16 changes: 8 additions & 8 deletions Tests/Integration/MobileCoinClientPublicApiIntTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ class MobileCoinClientPublicApiIntTests: XCTestCase {

func cancelSignedContingentInput(transportProtocol: TransportProtocol) async throws {
let amountToSend = Amount(100 + IntegrationTestFixtures.fee, in: .MOB)
let amountToReceive = Amount(10, in: .TestToken)
let amountToReceive = Amount(10, in: .MOBUSD)

let creatorIdx = 4
let creatorIdx = 1
let creatorAddr = try IntegrationTestFixtures.createPublicAddress(accountIndex: creatorIdx)
let creatorAcctKey = try IntegrationTestFixtures.createAccountKey(accountIndex: creatorIdx)
let creator = try await IntegrationTestFixtures.createMobileCoinClientWithBalance(
Expand All @@ -395,12 +395,12 @@ class MobileCoinClientPublicApiIntTests: XCTestCase {
transportProtocol: transportProtocol
)

let consumerIdx = 5
let consumerIdx = 0
let consumerAcctKey =
try IntegrationTestFixtures.createAccountKey(accountIndex: consumerIdx)
let consumer = try await IntegrationTestFixtures.createMobileCoinClientWithBalance(
accountKey: consumerAcctKey,
tokenId: .TestToken,
tokenId: .MOBUSD,
transportProtocol: transportProtocol
)

Expand Down Expand Up @@ -445,7 +445,7 @@ class MobileCoinClientPublicApiIntTests: XCTestCase {
transportProtocol: TransportProtocol
) async throws {
let amountToSend = Amount(100 + IntegrationTestFixtures.fee, in: .MOB)
let amountToReceive = Amount(10, in: .TestToken)
let amountToReceive = Amount(10, in: .MOBUSD)

func checkBlockVersionAndFee(
_ client: MobileCoinClient
Expand Down Expand Up @@ -511,7 +511,7 @@ class MobileCoinClientPublicApiIntTests: XCTestCase {
try await checkBalanceChange()
}

let creatorIdx = 4
let creatorIdx = 1
let creatorAddr = try IntegrationTestFixtures.createPublicAddress(accountIndex: creatorIdx)
let creatorAcctKey = try IntegrationTestFixtures.createAccountKey(accountIndex: creatorIdx)
let creator = try await IntegrationTestFixtures.createMobileCoinClientWithBalance(
Expand All @@ -520,12 +520,12 @@ class MobileCoinClientPublicApiIntTests: XCTestCase {
transportProtocol: transportProtocol
)

let consumerIdx = 5
let consumerIdx = 0
let consumerAcctKey =
try IntegrationTestFixtures.createAccountKey(accountIndex: consumerIdx)
let consumer = try await IntegrationTestFixtures.createMobileCoinClientWithBalance(
accountKey: consumerAcctKey,
tokenId: .TestToken,
tokenId: .MOBUSD,
transportProtocol: transportProtocol
)

Expand Down

0 comments on commit 2159c78

Please sign in to comment.