transaction.hpp 2.6 KB
Newer Older
1 2 3 4
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
5
#pragma once
6 7 8 9 10
#include <eosiolib/transaction.h>
#include <eosiolib/action.hpp>
#include <eosiolib/print.hpp>
#include <eosiolib/types.hpp>
#include <eosiolib/serialize.hpp>
11
#include <vector>
12

P
Pravin 已提交
13
namespace eosio {
14 15

   /**
16
    * @defgroup transactioncppapi Transaction C++ API
17
    * @ingroup transactionapi
18
    * @brief Type-safe C++ wrappers for transaction C API
19 20 21
    *
    * @note There are some methods from the @ref transactioncapi that can be used directly from C++
    *
22
    * @{
23 24
    */

25
   class transaction {
26
   public:
27 28
      transaction(time exp = now() + 60, region_id r = 0)
      :expiration(exp),region(r)
29 30
      {}

B
Bart Wyatt 已提交
31
      void send(uint32_t sender_id, account_name payer = name(0), time delay_until = 0) const {
32
         auto serialize = pack(*this);
B
Bart Wyatt 已提交
33
         send_deferred(sender_id, payer, delay_until, serialize.data(), serialize.size());
34 35
      }

36 37 38
      time            expiration;
      region_id       region;
      uint16_t        ref_block_num;
D
Daniel Larimer 已提交
39
      uint32_t        ref_block_prefix;
40 41
      unsigned_int    net_usage_words = 0UL; /// number of 8 byte words this transaction can serialize into after compressions
      unsigned_int    context_free_kilo_cpu_usage = 0UL; /// number of CPU usage units to bill transaction for
42

D
Daniel Larimer 已提交
43
      vector<action>  context_free_actions;
44
      vector<action>  actions;
45

46
      EOSLIB_SERIALIZE( transaction, (expiration)(region)(ref_block_num)(ref_block_prefix)(net_usage_words)(context_free_kilo_cpu_usage)(context_free_actions)(actions) )
47 48
   };

49
   class deferred_transaction : public transaction {
50
      public:
51 52
         uint32_t     sender_id;
         account_name sender;
B
Bart Wyatt 已提交
53
         account_name payer;
54
         time         delay_until;
55

56
         static deferred_transaction from_current_action() {
57
            return unpack_action_data<deferred_transaction>();
58 59
         }

B
Bart Wyatt 已提交
60
         EOSLIB_SERIALIZE_DERIVED( deferred_transaction, transaction, (sender_id)(sender)(payer)(delay_until) )
61
   };
62

63 64 65 66 67 68 69
   /**
    * Retrieve the indicated action from the active transaction.
    * @param type - 0 for context free action, 1 for action
    * @param index - the index of the requested action
    * @return the indicated action
    */
   action get_action( uint32_t type, uint32_t index ) {
70
      auto size = ::get_action(type, index, nullptr, 0);
71
      eosio_assert( size > 0, "get_action size failed" );
72 73 74
      char buf[size];
      auto size2 = ::get_action(type, index, &buf[0], static_cast<size_t>(size) );
      eosio_assert( size == size2, "get_action failed" );
K
Kevin Heifner 已提交
75
      return eosio::unpack<eosio::action>(&buf[0], static_cast<size_t>(size));
76 77 78
   }

   ///@} transactioncpp api
79 80

} // namespace eos