Skip to content

Commit

Permalink
Added ability to add collection of operation bodies to TransactionBui…
Browse files Browse the repository at this point in the history
…lder
  • Loading branch information
Radiokot committed Sep 4, 2019
1 parent 8b2b696 commit c4a9bb5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/kotlin/org/tokend/wallet/TransactionBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ class TransactionBuilder(private val networkParams: NetworkParams,
return this
}

/**
* Adds operations with given bodies to the result transaction.
*
* @see Transaction.operations
*/
@JvmOverloads
fun addOperations(operationBodies: Collection<Operation.OperationBody>,
operationsSourceAccount: AccountID? = null): TransactionBuilder {
operations.addAll(operationBodies.map {
Operation(operationsSourceAccount, it)
})
return this
}

/**
* Sets memo of the result transaction.
*
Expand Down
25 changes: 25 additions & 0 deletions src/test/kotlin/org/tokend/wallet_test/TransactionBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class TransactionBuilderTest {
"OLG",
ManageBalanceOp.ManageBalanceOpExt.EmptyVersion()
)
val SIMPLE_OP_2 = ManageBalanceOp(
ManageBalanceAction.CREATE,
SOURCE_ACCOUNT_PUBKEY,
"OLE",
ManageBalanceOp.ManageBalanceOpExt.EmptyVersion()
)

@Test
fun singleOperation() {
Expand All @@ -32,6 +38,25 @@ class TransactionBuilderTest {
transaction.operations[0].body.toBase64())
}

@Test
fun multipleOperations() {
val operationBodies = listOf(
Operation.OperationBody.ManageBalance(SIMPLE_OP),
Operation.OperationBody.ManageBalance(SIMPLE_OP_2)
)

val transaction = TransactionBuilder(NETWORK, SOURCE_ACCOUNT_PUBKEY)
.addOperations(operationBodies)
.build()

Assert.assertEquals(SOURCE_ACCOUNT_PUBKEY, transaction.sourceAccountId)
Assert.assertEquals(NETWORK.passphrase, transaction.networkParams.passphrase)
Assert.assertEquals(operationBodies[0].toBase64(),
transaction.operations[0].body.toBase64())
Assert.assertEquals(operationBodies[1].toBase64(),
transaction.operations[1].body.toBase64())
}

@Test
fun setMemo() {
val memoText = "TokenD is awesome"
Expand Down

0 comments on commit c4a9bb5

Please sign in to comment.