Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::PayDividendVisitor Class Reference

#include <PayDividendVisitor.hpp>

Inheritance diagram for opentxs::PayDividendVisitor:
Collaboration diagram for opentxs::PayDividendVisitor:

Public Member Functions

 PayDividendVisitor (const OTIdentifier &theServerID, const OTIdentifier &theUserID, const OTIdentifier &thePayoutAssetID, const OTIdentifier &theVoucherAcctID, const OTString &strMemo, OTServer &theServer, int64_t lPayoutPerShare, mapOfAccounts *pLoadedAccounts=nullptr)
 
virtual ~PayDividendVisitor ()
 
OTIdentifierGetUserID ()
 
OTIdentifierGetPayoutAssetID ()
 
OTIdentifierGetVoucherAcctID ()
 
OTStringGetMemo ()
 
OTServerGetServer ()
 
int64_t GetPayoutPerShare ()
 
int64_t GetAmountPaidOut ()
 
int64_t GetAmountReturned ()
 
virtual bool Trigger (OTAccount &theAccount)
 

Additional Inherited Members

Detailed Description

Definition at line 157 of file PayDividendVisitor.hpp.

Constructor & Destructor Documentation

opentxs::PayDividendVisitor::PayDividendVisitor ( const OTIdentifier theServerID,
const OTIdentifier theUserID,
const OTIdentifier thePayoutAssetID,
const OTIdentifier theVoucherAcctID,
const OTString strMemo,
OTServer theServer,
int64_t  lPayoutPerShare,
mapOfAccounts pLoadedAccounts = nullptr 
)

Definition at line 145 of file PayDividendVisitor.cpp.

150  : AccountVisitor(theServerID, pLoadedAccounts)
151  , m_pUserID(new OTIdentifier(theUserID))
152  , m_pPayoutAssetID(new OTIdentifier(thePayoutAssetID))
153  , m_pVoucherAcctID(new OTIdentifier(theVoucherAcctID))
154  , m_pstrMemo(new OTString(strMemo))
155  , m_pServer(&theServer)
156  , m_lPayoutPerShare(lPayoutPerShare)
157  , m_lAmountPaidOut(0)
158  , m_lAmountReturned(0)
159 {
160 }
EXPORT AccountVisitor(const OTIdentifier &serverId, mapOfAccounts *loadedAccounts=nullptr)
opentxs::PayDividendVisitor::~PayDividendVisitor ( )
virtual

Definition at line 162 of file PayDividendVisitor.cpp.

163 {
164  if (nullptr != m_pUserID) delete m_pUserID;
165  m_pUserID = nullptr;
166  if (nullptr != m_pPayoutAssetID) delete m_pPayoutAssetID;
167  m_pPayoutAssetID = nullptr;
168  if (nullptr != m_pVoucherAcctID) delete m_pVoucherAcctID;
169  m_pVoucherAcctID = nullptr;
170  if (nullptr != m_pstrMemo) delete m_pstrMemo;
171  m_pstrMemo = nullptr;
172  m_pServer = nullptr; // don't delete this one (I don't own it.)
173  m_lPayoutPerShare = 0;
174  m_lAmountPaidOut = 0;
175  m_lAmountReturned = 0;
176 }

Member Function Documentation

int64_t opentxs::PayDividendVisitor::GetAmountPaidOut ( )
inline

Definition at line 206 of file PayDividendVisitor.hpp.

207  {
208  return m_lAmountPaidOut;
209  }
int64_t opentxs::PayDividendVisitor::GetAmountReturned ( )
inline

Definition at line 210 of file PayDividendVisitor.hpp.

211  {
212  return m_lAmountReturned;
213  }
OTString* opentxs::PayDividendVisitor::GetMemo ( )
inline

Definition at line 194 of file PayDividendVisitor.hpp.

195  {
196  return m_pstrMemo;
197  }
OTIdentifier* opentxs::PayDividendVisitor::GetPayoutAssetID ( )
inline

Definition at line 186 of file PayDividendVisitor.hpp.

187  {
188  return m_pPayoutAssetID;
189  }
int64_t opentxs::PayDividendVisitor::GetPayoutPerShare ( )
inline

Definition at line 202 of file PayDividendVisitor.hpp.

203  {
204  return m_lPayoutPerShare;
205  }
OTServer* opentxs::PayDividendVisitor::GetServer ( )
inline

Definition at line 198 of file PayDividendVisitor.hpp.

199  {
200  return m_pServer;
201  }
OTIdentifier* opentxs::PayDividendVisitor::GetUserID ( )
inline

Definition at line 182 of file PayDividendVisitor.hpp.

183  {
184  return m_pUserID;
185  }
OTIdentifier* opentxs::PayDividendVisitor::GetVoucherAcctID ( )
inline

Definition at line 190 of file PayDividendVisitor.hpp.

191  {
192  return m_pVoucherAcctID;
193  }
bool opentxs::PayDividendVisitor::Trigger ( OTAccount theAccount)
virtual

Implements opentxs::AccountVisitor.

Definition at line 184 of file PayDividendVisitor.cpp.

188 {
189  const int64_t lPayoutAmount =
190  (theSharesAccount.GetBalance() * GetPayoutPerShare());
191 
192  if (lPayoutAmount <= 0) {
193  OTLog::Output(0, "PayDividendVisitor::Trigger: nothing to pay, "
194  "since this account owns no shares. (Returning "
195  "true.)");
196  return true; // nothing to pay, since this account owns no shares.
197  // Success!
198  }
199  OT_ASSERT(nullptr != GetServerID());
200  const OTIdentifier& theServerID = *(GetServerID());
201  OT_ASSERT(nullptr != GetPayoutAssetID());
202  const OTIdentifier& thePayoutAssetID = *(GetPayoutAssetID());
203  OT_ASSERT(nullptr != GetVoucherAcctID());
204  const OTIdentifier& theVoucherAcctID = *(GetVoucherAcctID());
205  OT_ASSERT(nullptr != GetServer());
206  OTServer& theServer = *(GetServer());
207  OTPseudonym& theServerNym =
208  const_cast<OTPseudonym&>(theServer.GetServerNym());
209  const OTIdentifier theServerNymID(theServerNym);
210  const OTIdentifier& RECIPIENT_ID = theSharesAccount.GetUserID();
211  OT_ASSERT(nullptr != GetUserID());
212  const OTIdentifier& theSenderUserID = *(GetUserID());
213  OT_ASSERT(nullptr != GetMemo());
214  const OTString& strMemo = *(GetMemo());
215  // Note: theSenderUserID is the originator of the Dividend Payout.
216  // However, all the actual vouchers will be from "the server Nym" and
217  // not from theSenderUserID. So then why is it even here? Because anytime
218  // there's an error, the server will send to theSenderUserID instead of
219  // RECIPIENT_ID (so the original sender can have his money back, instead of
220  // just having it get lost in the ether.)
221  bool bReturnValue = false;
222 
223  OTCheque theVoucher(theServerID, thePayoutAssetID);
224 
225  // 10 minutes == 600 Seconds
226  // 1 hour == 3600 Seconds
227  // 1 day == 86400 Seconds
228  // 30 days == 2592000 Seconds
229  // 3 months == 7776000 Seconds
230  // 6 months == 15552000 Seconds
231 
232  const time64_t VALID_FROM =
233  OTTimeGetCurrentTime(); // This time is set to TODAY NOW
234  const time64_t VALID_TO = OTTimeAddTimeInterval(
235  VALID_FROM, OTTimeGetSecondsFromTime(
236  OT_TIME_SIX_MONTHS_IN_SECONDS)); // This time occurs in
237  // 180 days (6 months).
238  // Todo hardcoding.
239 
240  int64_t lNewTransactionNumber = 0;
241 
242  bool bGotNextTransNum = theServer.transactor_.issueNextTransactionNumber(
243  theServerNym, lNewTransactionNumber); // bStoreTheNumber defaults to
244  // true. We save the transaction
245  // number on the server Nym (normally we'd discard it) because
246  // when the cheque is deposited, the server nym, as the owner of
247  // the voucher account, needs to verify the transaction # on the
248  // cheque (to prevent double-spending of cheques.)
249  if (bGotNextTransNum) {
250  const bool bIssueVoucher = theVoucher.IssueCheque(
251  lPayoutAmount, // The amount of the cheque.
252  lNewTransactionNumber, // Requiring a transaction number prevents
253  // double-spending of cheques.
254  VALID_FROM, // The expiration date (valid from/to dates) of the
255  // cheque
256  VALID_TO, // Vouchers are automatically starting today and lasting 6
257  // months.
258  theVoucherAcctID, // The asset account the cheque is drawn on.
259  theServerNymID, // User ID of the sender (in this case the server
260  // nym.)
261  strMemo, // Optional memo field. Includes item note and request
262  // memo.
263  &RECIPIENT_ID);
264 
265  // All account crediting / debiting happens in the caller, in OTServer.
266  // (AND it happens only ONCE, to cover ALL vouchers.)
267  // Then in here, the voucher either gets send to the recipient, or if
268  // error, sent back home to
269  // the issuer Nym. (ALL the funds are removed, then the vouchers are
270  // sent one way or the other.)
271  // Any returned vouchers, obviously serve to notify the dividend payer
272  // of where the errors were
273  // (as well as give him the opportunity to get his money back.)
274  //
275  bool bSent = false;
276  if (bIssueVoucher) {
277  // All this does is set the voucher's internal contract string to
278  // "VOUCHER" instead of "CHEQUE". We also set the server itself as
279  // the remitter, which is unusual for vouchers, but necessary in the
280  // case of dividends.
281  //
282  theVoucher.SetAsVoucher(theServerNymID, theVoucherAcctID);
283  theVoucher.SignContract(theServerNym);
284  theVoucher.SaveContract();
285 
286  // Send the voucher to the payments inbox of the recipient.
287  //
288  const OTString strVoucher(theVoucher);
289  OTPayment thePayment(strVoucher);
290 
291  // calls DropMessageToNymbox
292  bSent = theServer.SendInstrumentToNym(
293  theServerID, theServerNymID, // sender nym
294  RECIPIENT_ID, // recipient nym
295  nullptr, &thePayment, "payDividend"); // todo: hardcoding.
296  bReturnValue = bSent; // <======= RETURN VALUE.
297  if (bSent)
298  m_lAmountPaidOut +=
299  lPayoutAmount; // At the end of iterating all accounts, if
300  // m_lAmountPaidOut is less than
301  // lTotalPayoutAmount, then we return to rest
302  // to the sender.
303  }
304  else {
305  const OTString strPayoutAssetID(thePayoutAssetID),
306  strRecipientUserID(RECIPIENT_ID);
307  OTLog::vError("PayDividendVisitor::Trigger: ERROR failed "
308  "issuing voucher (to send to dividend payout "
309  "recipient.) "
310  "WAS TRYING TO PAY %ld of asset type %s to Nym %s.\n",
311  lPayoutAmount, strPayoutAssetID.Get(),
312  strRecipientUserID.Get());
313  }
314  // If we didn't send it, then we need to return the funds to where they
315  // came from.
316  //
317  if (!bSent) {
318  OTCheque theReturnVoucher(theServerID, thePayoutAssetID);
319 
320  const bool bIssueReturnVoucher = theReturnVoucher.IssueCheque(
321  lPayoutAmount, // The amount of the cheque.
322  lNewTransactionNumber, // Requiring a transaction number
323  // prevents double-spending of cheques.
324  VALID_FROM, // The expiration date (valid from/to dates) of the
325  // cheque
326  VALID_TO, // Vouchers are automatically starting today and
327  // lasting 6 months.
328  theVoucherAcctID, // The asset account the cheque is drawn on.
329  theServerNymID, // User ID of the sender (in this case the
330  // server nym.)
331  strMemo, // Optional memo field. Includes item note and request
332  // memo.
333  &theSenderUserID); // We're returning the money to its original
334  // sender.
335  if (bIssueReturnVoucher) {
336  // All this does is set the voucher's internal contract string
337  // to
338  // "VOUCHER" instead of "CHEQUE".
339  //
340  theReturnVoucher.SetAsVoucher(theServerNymID, theVoucherAcctID);
341  theReturnVoucher.SignContract(theServerNym);
342  theReturnVoucher.SaveContract();
343 
344  // Return the voucher back to the payments inbox of the original
345  // sender.
346  //
347  const OTString strReturnVoucher(theReturnVoucher);
348  OTPayment theReturnPayment(strReturnVoucher);
349 
350  // calls DropMessageToNymbox
351  bSent = theServer.SendInstrumentToNym(
352  theServerID, theServerNymID, // sender nym
353  theSenderUserID, // recipient nym (original sender.)
354  nullptr, &theReturnPayment,
355  "payDividend"); // todo: hardcoding.
356  if (bSent)
357  m_lAmountReturned +=
358  lPayoutAmount; // At the end of iterating all accounts,
359  // if m_lAmountPaidOut+m_lAmountReturned
360  // is less than lTotalPayoutAmount, then
361  // we return the rest to the sender.
362  }
363  else {
364  const OTString strPayoutAssetID(thePayoutAssetID),
365  strSenderUserID(theSenderUserID);
366  OTLog::vError("PayDividendVisitor::Trigger: ERROR "
367  "failed issuing voucher (to return back to "
368  "the dividend payout initiator, after a failed "
369  "payment attempt to the originally intended "
370  "recipient.) WAS TRYING TO PAY %ld of asset type "
371  "%s to Nym %s.\n",
372  lPayoutAmount, strPayoutAssetID.Get(),
373  strSenderUserID.Get());
374  }
375  } // if !bSent
376  }
377  else // !bGotNextTransNum
378  {
379  const OTString strPayoutAssetID(thePayoutAssetID),
380  strRecipientUserID(RECIPIENT_ID);
382  "PayDividendVisitor::Trigger: ERROR!! Failed issuing next "
383  "transaction "
384  "number while trying to send a voucher (while paying dividends.) "
385  "WAS TRYING TO PAY %ld of asset type %s to Nym %s.\n",
386  lPayoutAmount, strPayoutAssetID.Get(), strRecipientUserID.Get());
387  }
388 
389  return bReturnValue;
390 }
static EXPORT void vError(const char *szError,...)
Definition: OTLog.cpp:800
static EXPORT void Output(int32_t nVerbosity, const char *szOutput)
Definition: OTLog.cpp:710
time64_t OTTimeAddTimeInterval(time64_t lhs, int64_t rhs)
Definition: Common.hpp:238
int64_t time64_t
Definition: Common.hpp:209
#define OT_TIME_SIX_MONTHS_IN_SECONDS
Definition: Common.hpp:170
time64_t OTTimeGetCurrentTime()
Definition: Common.hpp:211
#define OT_ASSERT(x)
Definition: Assert.hpp:150
int64_t OTTimeGetSecondsFromTime(time64_t time)
Definition: Common.hpp:230
EXPORT OTIdentifier * GetServerID()

The documentation for this class was generated from the following files: