Skip to content

Commit

Permalink
fix: amend transaction when consume cells (#3166)
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan authored May 23, 2024
1 parent 99ca06e commit ecd7fc9
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions packages/neuron-ui/src/components/AmendSend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,41 @@ const AmendSend = () => {
return ''
}

const inputsCapacity = useMemo(() => {
if (transaction) {
return transaction.inputs.reduce((total, cur) => {
return total + BigInt(cur.capacity || '0')
}, BigInt(0))
}
return undefined
}, [transaction])

const items: {
address: string
amount: string
capacity: string
isLastOutput: boolean
output: State.DetailedOutput
}[] = useMemo(() => {
if (transaction && transaction.outputs.length) {
if (transaction && transaction.outputs.length && inputsCapacity) {
const lastOutputAddress = getLastOutputAddress(transaction.outputs)
return transaction.outputs.map(output => {
const address = scriptToAddress(output.lock, { isMainnet })
const capacity =
transaction.outputs.length === 1 && address === lastOutputAddress
? (inputsCapacity - fee).toString()
: output.capacity
return {
capacity: output.capacity,
capacity,
address,
output,
amount: shannonToCKBFormatter(output.capacity || '0'),
amount: shannonToCKBFormatter(capacity || '0'),
isLastOutput: address === lastOutputAddress,
}
})
}
return []
}, [transaction?.outputs])
}, [transaction?.outputs, inputsCapacity, fee])

const outputsCapacity = useMemo(() => {
const outputList = items.length === 1 ? items : items.filter(item => !item.isLastOutput)
Expand All @@ -129,15 +142,15 @@ const AmendSend = () => {
const totalAmount = shannonToCKBFormatter(outputsCapacity.toString())

const lastOutputsCapacity = useMemo(() => {
if (transaction) {
const inputsCapacity = transaction.inputs.reduce((total, cur) => {
return total + BigInt(cur.capacity || '0')
}, BigInt(0))
if (inputsCapacity) {
if (items.length === 1) {
return BigInt(items[0].capacity || '0')
}

return inputsCapacity - outputsCapacity - fee
}
return undefined
}, [transaction, fee, outputsCapacity])
}, [inputsCapacity, fee, outputsCapacity, items])

useEffect(() => {
if (transaction && lastOutputsCapacity !== undefined) {
Expand Down

1 comment on commit ecd7fc9

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Packaging for test is done in 9203814156

Please sign in to comment.