action.h 5.3 KB
Newer Older
1 2 3 4
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
5
#pragma once
6
#include <eosiolib/system.h>
7 8 9

extern "C" {
   /**
10
    * @defgroup actionapi Action API
11
    * @ingroup contractdev
K
Kevin Heifner 已提交
12
    * @brief Define API for querying action properties
13
    *
A
Andrianto Lie 已提交
14 15 16
    */

   /**
K
Kevin Heifner 已提交
17 18 19
    * @defgroup actioncapi Action C API
    * @ingroup actionapi
    * @brief Define API for querying action properties
A
Andrianto Lie 已提交
20 21
    *
    *
K
Kevin Heifner 已提交
22
    * A EOS.IO action has the following abstract structure:
23 24
    *
    * ```
K
Kevin Heifner 已提交
25 26 27 28
    *   struct action {
    *     scope_name scope; // the contract defining the primary code to execute for code/type
    *     action_name name; // the action to be taken
    *     permission_level[] authorization; // the accounts and permission levels provided
29
    *     bytes data; // opaque data processed by code
30 31
    *   };
    * ```
32
    *
K
Kevin Heifner 已提交
33
    * This API enables your contract to inspect the fields on the current action and act accordingly.
34
    *
A
Andrianto Lie 已提交
35 36
    * Example:
    * @code
K
Kevin Heifner 已提交
37
    * // Assume this action is used for the following examples:
A
Andrianto Lie 已提交
38 39 40 41 42 43 44 45 46 47 48 49
    * // {
    * //  "code": "eos",
    * //  "type": "transfer",
    * //  "authorization": [{ "account": "inita", "permission": "active" }],
    * //  "data": {
    * //    "from": "inita",
    * //    "to": "initb",
    * //    "amount": 1000
    * //  }
    * // }
    *
    * char buffer[128];
K
Kevin Heifner 已提交
50
    * uint32_t total = read_action(buffer, 5); // buffer contains the content of the action up to 5 bytes
A
Andrianto Lie 已提交
51 52
    * print(total); // Output: 5
    *
53
    * uint32_t msgsize = action_size();
K
Kevin Heifner 已提交
54
    * print(msgsize); // Output: size of the above action's data field
A
Andrianto Lie 已提交
55
    *
K
Kevin Heifner 已提交
56
    * require_recipient(N(initc)); // initc account will be notified for this action
A
Andrianto Lie 已提交
57
    *
58 59
    * require_auth(N(inita)); // Do nothing since inita exists in the auth list
    * require_auth(N(initb)); // Throws an exception
A
Andrianto Lie 已提交
60
    *
61
    * print(current_time()); // Output: timestamp (in microseconds since 1970) of current block
A
Andrianto Lie 已提交
62 63 64
    *
    * @endcode
    *
65 66 67 68 69
    *
    * @{
    */

   /**
70 71 72
    *  Copy up to @ref len bytes of current action data to the specified location
    *  @brief Copy current action data to the specified location
    *  @param msg - a pointer where up to @ref len bytes of the current action data will be copied
73
    *  @param len - len of the current action data to be copied, 0 to report required size
K
Kevin Heifner 已提交
74
    *  @return the number of bytes copied to msg, or number of bytes that can be copied if len==0 passed
75
    */
76
   uint32_t read_action_data( void* msg, uint32_t len );
77 78

   /**
K
Kevin Heifner 已提交
79 80 81 82
    * Get the length of the current action's data field
    * This method is useful for dynamically sized actions
    * @brief Get the length of current action's data field
    * @return the length of the current action's data field
83
    */
84
   uint32_t action_data_size();
85 86

   /**
87 88
    *  Add the specified account to set of accounts to be notified
    *  @brief Add the specified account to set of accounts to be notified
89
    *  @param name - name of the account to be verified
90
    */
91
   void require_recipient( account_name name );
92 93

   /**
K
Kevin Heifner 已提交
94
    *  Verifies that @ref name exists in the set of provided auths on a action. Throws if not found
95 96
    *  @brief Verify specified account exists in the set of provided auths
    *  @param name - name of the account to be verified
97
    */
98 99
   void require_auth( account_name name );
   bool has_auth( account_name name );
100

101 102 103 104 105 106
   /**
    *  Verifies that @ref name exists in the set of provided auths on a action. Throws if not found
    *  @brief Verify specified account exists in the set of provided auths
    *  @param name - name of the account to be verified
    *  @param permission - permission level to be verified
    */
107
   void require_auth2( account_name name, permission_name permission );
108

109
   bool is_account( account_name name );
110

111 112
   /**
    *  Send an inline action in the context of this action's parent transaction
113
    * @param serialized_action - serialized action
114
    * @param size - size of serialized action in bytes
115
    */
116
   void send_inline(char *serialized_action, size_t size);
117

M
Matias Romeo 已提交
118 119 120 121 122
   /**
    *  Send an inline context free action in the context of this action's parent transaction
    * @param serialized_action - serialized action
    * @param size - size of serialized action in bytes
    */
123
   void send_context_free_inline(char *serialized_action, size_t size);
M
Matias Romeo 已提交
124

125 126 127 128 129
   /**
    *  Verifies that @ref name exists in the set of write locks held on a action. Throws if not found
    *  @brief Verifies that @ref name exists in the set of write locks held
    *  @param name - name of the account to be verified
    */
130
   void require_write_lock( account_name name );
131 132 133 134 135 136

   /**
    *  Verifies that @ref name exists in the set of read locks held on a action. Throws if not found
    *  @brief Verifies that @ref name exists in the set of read locks held
    *  @param name - name of the account to be verified
    */
137
   void require_read_lock( account_name name );
138 139

   /**
140
    *  Returns the time in microseconds from 1970 of the publication_time
141
    *  @brief Get the publication time
142
    *  @return the time in microseconds from 1970 of the publication_time
143
    */
144
   uint64_t  publication_time();
145

146 147 148 149 150
   /**
    *  Get the current receiver of the action
    *  @brief Get the current receiver of the action
    *  @return the account which specifies the current receiver of the action
    */
151
   account_name current_receiver();
K
Kevin Heifner 已提交
152
   ///@ } actioncapi
153
}