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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- added: Support for multiple spend targets.

## 1.4.2 (2025-04-01)

- fixed: Small internal implementation cleanup on how transactions are saved.
Expand Down
68 changes: 39 additions & 29 deletions src/MoneroEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Created by paul on 7/7/17.
*/

import { div, eq, gte, lt, sub } from 'biggystring'
import { add, div, eq, gte, lt, sub } from 'biggystring'
import type { Disklet } from 'disklet'
import {
EdgeCorePluginOptions,
Expand Down Expand Up @@ -600,11 +600,15 @@ export class MoneroEngine implements EdgeCurrencyEngine {
throw new TypeError('Missing destination address')
}

const options = {
amount: '0',
const options: CreateTransactionOptions = {
isSweepTx: true,
priority: translateFee(edgeSpendInfo.networkFeeOption),
targetAddress: publicAddress
targets: [
{
amount: '0',
targetAddress: publicAddress
}
]
}

const result = await this.createMyMoneroTransaction(options, privateKeys)
Expand Down Expand Up @@ -642,39 +646,45 @@ export class MoneroEngine implements EdgeCurrencyEngine {
const { memos = [] } = edgeSpendInfo
const privateKeys = asPrivateKeys(opts?.privateKeys)

// Monero can only have one output
// TODO: The new SDK fixes this!
if (edgeSpendInfo.spendTargets.length !== 1) {
throw new Error('Error: only one output allowed')
}
const { spendTargets } = edgeSpendInfo

const [spendTarget] = edgeSpendInfo.spendTargets
const { publicAddress, nativeAmount } = spendTarget
if (publicAddress == null) {
throw new TypeError('Missing destination address')
}
if (nativeAmount == null || eq(nativeAmount, '0')) {
throw new NoAmountSpecifiedError()
}
let totalAmount = '0'
const targets: CreateTransactionOptions['targets'] = []

if (
gte(
nativeAmount,
this.walletLocalData.totalBalances.get(PRIMARY_CURRENCY_TOKEN_ID) ?? '0'
)
) {
if (gte(this.walletLocalData.lockedXmrBalance, nativeAmount)) {
throw new PendingFundsError()
} else {
throw new InsufficientFundsError({ tokenId: PRIMARY_CURRENCY_TOKEN_ID })
for (const spendTarget of spendTargets) {
const { publicAddress, nativeAmount } = spendTarget
if (publicAddress == null) {
throw new TypeError('Missing destination address')
}
if (nativeAmount == null || eq(nativeAmount, '0')) {
throw new NoAmountSpecifiedError()
}
totalAmount = add(totalAmount, nativeAmount)
if (
gte(
totalAmount,
this.walletLocalData.totalBalances.get(PRIMARY_CURRENCY_TOKEN_ID) ??
'0'
)
) {
if (gte(this.walletLocalData.lockedXmrBalance, totalAmount)) {
throw new PendingFundsError()
} else {
throw new InsufficientFundsError({
tokenId: PRIMARY_CURRENCY_TOKEN_ID
})
}
}
targets.push({
amount: div(nativeAmount, '1000000000000', 12),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is nativeAmount divided by 1 trillion? Isn't nativeAmount already in units of piconero?

targetAddress: publicAddress
})
}

const options: CreateTransactionOptions = {
amount: div(nativeAmount, '1000000000000', 12),
isSweepTx: false,
priority: translateFee(edgeSpendInfo.networkFeeOption),
targetAddress: publicAddress
targets
}
this.log(`Creating transaction: ${JSON.stringify(options, null, 1)}`)

Expand Down
26 changes: 11 additions & 15 deletions src/MyMoneroApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ export interface BalanceResults {
}

export interface CreateTransactionOptions {
amount: string
targets: Array<{
amount: string
targetAddress: string
}>
isSweepTx?: boolean
paymentId?: string
priority?: Priority
targetAddress: string
}

const asNumberBoolean: Cleaner<boolean> = raw => {
Expand Down Expand Up @@ -238,13 +240,7 @@ export class MyMoneroApi {
opts: CreateTransactionOptions
): Promise<CreatedTransaction> {
const { address, privateSpendKey, privateViewKey, publicSpendKey } = keys
const {
amount,
isSweepTx = false,
paymentId,
priority = 1,
targetAddress
} = opts
const { isSweepTx = false, paymentId, priority = 1, targets } = opts

// Grab the UTXO set:
const unspentOuts = await this.fetchPostMyMonero('get_unspent_outs', {
Expand All @@ -268,14 +264,14 @@ export class MyMoneroApi {
})
}

const destinations = targets.map(t => ({
send_amount: t.amount,
to_address: t.targetAddress
}))

// Make the transaction:
return await this.cppBridge.createTransaction({
destinations: [
{
send_amount: amount,
to_address: targetAddress
}
],
destinations,
priority,
address,
paymentId,
Expand Down
Loading