Skip to content

Commit

Permalink
Merge pull request #78 from eric-volz/transactions/sendmany
Browse files Browse the repository at this point in the history
New Method: Send Many for UTXO
  • Loading branch information
eric-volz authored Jun 14, 2023
2 parents df84251 + 13036e0 commit bde71c6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
57 changes: 56 additions & 1 deletion defichain/transactions/builder/modules/utxo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from defichain.exceptions.transactions import TxBuilderError
from defichain.transactions.rawtransactions import TxAddressOutput, estimate_fee
from defichain.transactions.utils import Converter
from defichain.transactions.utils import Converter, Calculate, Token
from defichain.transactions.builder.rawtransactionbuilder import RawTransactionBuilder, Transaction


Expand Down Expand Up @@ -81,3 +81,58 @@ def sendall(self, addressTo: str, inputs=[]) -> Transaction:

self._builder.sign(tx)
return tx

def sendmany(self, addressAmountTo: {}, changeAddress=None, inputs=[]) -> Transaction:
"""
Sends the specified amount of UTXO to the specified addresses. Returns the remaining UTXO from the input to the
sender address if not changed.
:param addressAmountTo: (required) AddressAmount
:type addressAmountTo: json string
:param changeAddress: (required) address to which the remaining UTXO should be sent
:type changeAddress: str
:param inputs: (optional) Inputs
:type inputs: TxInput
:return: Transaction
"""

if inputs is None:
inputs = []
if addressAmountTo is None:
addressAmountTo = {}
if changeAddress is None:
changeAddress = self._builder.get_address()

# Convert Float to Integer
addressAmountTo = Converter.addressAmount_float_to_int(addressAmountTo)
outputValue = Calculate.addressAmountSum(addressAmountTo)

# Building the transaction
tx = self._builder.build_transactionInputs(inputs)
inputValue = tx.get_inputsValue()

# Building Transaction Outputs
for address in addressAmountTo:
value, token = addressAmountTo[address].split("@")
token = Token.checkAndConvert(self._builder.get_network(), token)
if token != 0:
raise TxBuilderError("The used method only support sending DFI")
addressOutput = TxAddressOutput(int(value), address)
tx.add_output(addressOutput)

changeOutputValue = inputValue - outputValue
changeOutput = TxAddressOutput(changeOutputValue, changeAddress)
tx.add_output(changeOutput)

# Calculate fee
fee = estimate_fee(tx, self._builder.get_feePerByte())

# Subtract fee from output
value = changeOutput.get_value() - fee
if value < 0:
raise TxBuilderError("The used address has not enough UTXO to pay the transaction fee")
changeOutput.set_value(value)

self._builder.sign(tx)
return tx

2 changes: 2 additions & 0 deletions defichain/transactions/builder/rawtransactionbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def build_transactionInputs(self, inputs=[]) -> Transaction:
tx.add_input(TxP2WPKHInput(input["txid"], input["vout"], self.get_address(), input["value"]))
# Check Inputs for masternode collateral
tx.set_inputs(self.checkMasternodeInputs(tx.get_inputs()))
if tx.get_inputs() == []:
raise TxBuilderError(f"Given address: {self._address} has no unspent inputs. Check your builder object!")
return tx

def build_defiTx(self, value: int, defiTx: BaseDefiTx, inputs=[], **additionalData) -> Transaction:
Expand Down

0 comments on commit bde71c6

Please sign in to comment.