// Adjust an offer to indicate that we are consuming some (or all) of it. void Taker::consume (Offer const& offer, Amounts const& consumed) const { Amounts const& remaining (offer.amount ()); assert (remaining.in > zero && remaining.out > zero); assert (remaining.in >= consumed.in && remaining.out >= consumed.out); offer.entry ()->setFieldAmount (sfTakerPays, remaining.in - consumed.in); offer.entry ()->setFieldAmount (sfTakerGets, remaining.out - consumed.out); view ().entryModify (offer.entry()); assert (offer.entry ()->getFieldAmount (sfTakerPays) >= zero); assert (offer.entry ()->getFieldAmount (sfTakerGets) >= zero); }
// Fill a direct offer. // @param offer the offer we are going to use. // @param amount the amount to flow through the offer. // @returns: tesSUCCESS if successful, or an error code otherwise. TER Taker::fill (Offer const& offer, Amounts const& amount) { TER result (tesSUCCESS); Amounts const remain ( offer.entry ()->getFieldAmount (sfTakerPays) - amount.in, offer.entry ()->getFieldAmount (sfTakerGets) - amount.out); offer.entry ()->setFieldAmount (sfTakerPays, remain.in); offer.entry ()->setFieldAmount (sfTakerGets, remain.out); view ().entryModify (offer.entry()); // Pay the taker, then the owner result = view ().accountSend (offer.account(), account(), amount.out); if (result == tesSUCCESS) result = view ().accountSend (account(), offer.account(), amount.in); return result; }
// Fill a bridged offer. // @param leg1 the first leg we are going to use. // @param amount1 the amount to flow through the first leg of the offer. // @param leg2 the second leg we are going to use. // @param amount2 the amount to flow through the second leg of the offer. // @return tesSUCCESS if successful, or an error code otherwise. TER Taker::fill ( Offer const& leg1, Amounts const& amount1, Offer const& leg2, Amounts const& amount2) { assert (amount1.out == amount2.in); TER result (tesSUCCESS); Amounts const remain1 ( leg1.entry ()->getFieldAmount (sfTakerPays) - amount1.in, leg1.entry ()->getFieldAmount (sfTakerGets) - amount1.out); leg1.entry ()->setFieldAmount (sfTakerPays, remain1.in); leg1.entry ()->setFieldAmount (sfTakerGets, remain1.out); view ().entryModify (leg1.entry()); Amounts const remain2 ( leg2.entry ()->getFieldAmount (sfTakerPays) - amount2.in, leg2.entry ()->getFieldAmount (sfTakerGets) - amount2.out); view ().entryModify (leg2.entry ()); // Execute the payments in order: // Taker pays Leg1 (amount1.in - A currency) // Leg1 pays Leg2 (amount1.out == amount2.in, XRP) // Leg2 pays Taker (amount2.out - B currency) result = view ().accountSend (m_account, leg1.account (), amount1.in); if (result == tesSUCCESS) result = view ().accountSend (leg1.account (), leg2.account (), amount1.out); if (result == tesSUCCESS) result = view ().accountSend (leg2.account (), m_account, amount2.out); return result; }