コード例 #1
0
WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
{
	qint64 total = 0;
	QSet<QString> setAddress;
	QString hex;

	if (recipients.empty())
	{
		return OK;
	}

	// Pre-check input data for validity
	foreach(const SendCoinsRecipient &rcp, recipients)
	{
		if (!validateAddress(rcp.address))
		{
			return InvalidAddress;
		}
		setAddress.insert(rcp.address);

		if (rcp.amount < MIN_TXOUT_AMOUNT)
		{
			return InvalidAmount;
		}
		total += rcp.amount;
	}

	if (recipients.size() > setAddress.size())
	{
		return DuplicateAddress;
	}

	if (total > getBalance())
	{
		return AmountExceedsBalance;
	}

	if ((total + nTransactionFee) > getBalance())
	{
		return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
	}

	{
		LOCK2(cs_main, wallet->cs_wallet);

		// Sendmany
		std::vector<std::pair<CScript, int64> > vecSend;
		foreach(const SendCoinsRecipient &rcp, recipients)
		{
			CScript scriptPubKey;
			scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
			vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
		}

		CWalletTx wtx;
		CReserveKey keyChange(wallet);
		int64 nFeeRequired = 0;
		bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);

		if (!fCreated)
		{
			if ((total + nFeeRequired) > wallet->GetBalance())
			{
				return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
			}
			return TransactionCreationFailed;
		}
		if (!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString()))
		{
			return Aborted;
		}
		if (!wallet->CommitTransaction(wtx, keyChange))
		{
			return TransactionCommitFailed;
		}
		hex = QString::fromStdString(wtx.GetHash().GetHex());
	}
コード例 #2
0
ファイル: walletmodel.cpp プロジェクト: DARKMI5T/paycoin
WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients, const CCoinControl *coinControl)
{
    qint64 total = 0;
    QSet<QString> setAddress;
    QString hex;

    if(recipients.empty())
    {
        return OK;
    }

    // Pre-check input data for validity
    foreach(const SendCoinsRecipient &rcp, recipients)
    {
        if(!validateAddress(rcp.address))
        {
            return InvalidAddress;
        }
        setAddress.insert(rcp.address);

        if(rcp.amount < MIN_TXOUT_AMOUNT)
        {
            return InvalidAmount;
        }
        total += rcp.amount;
    }

    if(recipients.size() > setAddress.size())
    {
        return DuplicateAddress;
    }

    // we do not use getBalance() here, because some coins could be locked or coin control could be active
    int64 nBalance = 0;
    std::vector<COutput> vCoins;
    wallet->AvailableCoins(vCoins, true, coinControl);
    BOOST_FOREACH(const COutput& out, vCoins)
        nBalance += out.tx->vout[out.i].nValue;

    if(total > nBalance)
    {
        return AmountExceedsBalance;
    }

    if((total + nTransactionFee) > nBalance)
    {
        return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
    }

    {
        LOCK2(cs_main, wallet->cs_wallet);

        // Sendmany
        std::vector<std::pair<CScript, int64> > vecSend;
        foreach(const SendCoinsRecipient &rcp, recipients)
        {
            CScript scriptPubKey;
            scriptPubKey.SetDestination(CBitcoinAddress(rcp.address.toStdString()).Get());
            vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
        }

        CWalletTx wtx;
        CReserveKey keyChange(wallet);
        int64 nFeeRequired = 0;
        bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired, coinControl);

        if(!fCreated)
        {
            if((total + nFeeRequired) > nBalance)
            {
                return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
            }
            return TransactionCreationFailed;
        }
        if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString()))
        {
            return Aborted;
        }
        if(!wallet->CommitTransaction(wtx, keyChange))
        {
            return TransactionCommitFailed;
        }
        hex = QString::fromStdString(wtx.GetHash().GetHex());
    }