-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledger.h
More file actions
155 lines (129 loc) · 5.35 KB
/
ledger.h
File metadata and controls
155 lines (129 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef CMA_LEDGER_H
#define CMA_LEDGER_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
// Compiler visibility definition
#ifndef CMA_LEDGER_API
#define CMA_LEDGER_API __attribute__((visibility("default")))
#endif
#include "types.h"
enum {
CMA_LEDGER_T_SIZE = 512, // / 8,
CMA_LEDGER_MIN_MEM_LENGTH = 262144,
};
typedef struct cma_ledger_struct {
uint64_t __opaque[CMA_LEDGER_T_SIZE];
} cma_ledger_t;
typedef uint64_t cma_ledger_account_id_t;
typedef uint64_t cma_ledger_asset_id_t;
enum {
CMA_LEDGER_SUCCESS = 0,
CMA_LEDGER_ERROR_UNKNOWN = -1001,
CMA_LEDGER_ERROR_EXCEPTION = -1002,
CMA_LEDGER_ERROR_INSUFFICIENT_FUNDS = -1003,
CMA_LEDGER_ERROR_ACCOUNT_NOT_FOUND = -1004,
CMA_LEDGER_ERROR_ASSET_NOT_FOUND = -1005,
CMA_LEDGER_ERROR_BALANCE_NOT_FOUND = -1006,
CMA_LEDGER_ERROR_SUPPLY_OVERFLOW = -1007,
CMA_LEDGER_ERROR_BALANCE_OVERFLOW = -1008,
CMA_LEDGER_ERROR_INVALID_ACCOUNT = -1009,
CMA_LEDGER_ERROR_INSERTION_ERROR = -1010,
CMA_LEDGER_ERROR_MAX_ASSETS_REACHED = -1011,
CMA_LEDGER_ERROR_MAX_ACCOUNTS_REACHED = -1012,
CMA_LEDGER_ERROR_MAX_BALANCES_REACHED = -1013,
CMA_LEDGER_ERROR_ASSET_SUPPLY = -1014,
CMA_LEDGER_ERROR_ACCOUNT_BALANCE = -1015,
CMA_LEDGER_ERROR_REMOVE = -1016,
};
typedef enum {
CMA_LEDGER_OP_FIND,
CMA_LEDGER_OP_CREATE,
CMA_LEDGER_OP_FIND_OR_CREATE,
CMA_LEDGER_OP_FIND_AND_REMOVE,
} cma_ledger_retrieve_operation_t;
typedef enum {
CMA_LEDGER_ASSET_TYPE_ID,
CMA_LEDGER_ASSET_TYPE_BASE,
CMA_LEDGER_ASSET_TYPE_TOKEN_ADDRESS,
CMA_LEDGER_ASSET_TYPE_TOKEN_ADDRESS_ID,
CMA_LEDGER_ASSET_TYPE_TOKEN_ADDRESS_ID_AMOUNT,
} cma_ledger_asset_type_t;
typedef enum {
CMA_LEDGER_ACCOUNT_TYPE_ID,
CMA_LEDGER_ACCOUNT_TYPE_WALLET_ADDRESS,
CMA_LEDGER_ACCOUNT_TYPE_ACCOUNT_ID,
} cma_ledger_account_type_t;
typedef enum {
CMA_LEDGER_OPEN_ONLY,
CMA_LEDGER_CREATE_ONLY,
} cma_ledger_memory_mode_t;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
typedef struct cma_ledger_account {
cma_ledger_account_type_t type;
union {
cma_account_id_t account_id;
struct {
uint8_t fix[CMA_ABI_ID_LENGTH - CMA_ABI_ADDRESS_LENGTH];
cma_abi_address_t address;
} __attribute__((__packed__));
};
} cma_ledger_account_t;
#pragma GCC diagnostic pop
typedef struct cma_ledger_account_balance {
uint32_t type;
cma_abi_address_t owner;
cma_token_address_t token_address;
cma_token_id_t token_id;
cma_amount_t amount;
uint8_t padding[20]; // Align to 4*32 bytes
} cma_ledger_account_balance_t;
typedef struct cma_ledger_account_balance_info {
ptrdiff_t offset;
size_t index;
cma_ledger_account_balance_t *balance;
} cma_ledger_account_balance_info_t;
CMA_LEDGER_API int cma_ledger_init(cma_ledger_t *ledger);
CMA_LEDGER_API int cma_ledger_fini(cma_ledger_t *ledger);
CMA_LEDGER_API int cma_ledger_reset(cma_ledger_t *ledger);
CMA_LEDGER_API int cma_ledger_init_file(cma_ledger_t *ledger, const char *memory_file_name,
cma_ledger_memory_mode_t mode, size_t offset, size_t mem_length, size_t n_accounts, size_t n_assets,
size_t n_balances);
CMA_LEDGER_API int cma_ledger_init_buffer(cma_ledger_t *ledger, void *buffer, size_t mem_length, size_t n_accounts,
size_t n_assets, size_t n_balances);
// Retrieve/create an asset
// try to retrieve: If id is defined, fill with the asset details, otherwise fill with id
// If it didn't find the asset and creation type is set with one of the options, create it
// update the token asset_id <-> (address, token id) mapping
CMA_LEDGER_API int cma_ledger_retrieve_asset(cma_ledger_t *ledger, cma_ledger_asset_id_t *asset_id,
cma_token_address_t *token_address, cma_token_id_t *token_id, cma_amount_t *out_total_supply,
cma_ledger_asset_type_t *asset_type, cma_ledger_retrieve_operation_t operation);
// Retrieve/create an account
// try to retrieve: If id is defined, fill with the account details, otherwise fill with id
// If it didn't find the account and creation type is set with one of the options, create it
// update the token id <-> account_id mapping
CMA_LEDGER_API int cma_ledger_retrieve_account(cma_ledger_t *ledger, cma_ledger_account_id_t *account_id,
cma_ledger_account_t *account, const void *addr_accid, size_t *n_balances, cma_ledger_account_type_t *account_type,
cma_ledger_retrieve_operation_t operation);
// Deposit
CMA_LEDGER_API int cma_ledger_deposit(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
cma_ledger_account_id_t to_accountcma_ledger_account_balance_t_id, const cma_amount_t *deposit);
// Withdrawal
CMA_LEDGER_API int cma_ledger_withdraw(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
cma_ledger_account_id_t from_account_id, const cma_amount_t *withdrawal);
// Transfer
CMA_LEDGER_API int cma_ledger_transfer(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
cma_ledger_account_id_t from_account_id, cma_ledger_account_id_t to_account_id, const cma_amount_t *amount);
// Get balance
CMA_LEDGER_API int cma_ledger_get_balance(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
cma_ledger_account_id_t account_id, cma_amount_t *out_balance,
cma_ledger_account_balance_info_t *account_balance_info);
// get error message
CMA_LEDGER_API const char *cma_ledger_get_last_error_message();
#ifdef __cplusplus
} // extern "C"
#endif
#endif // CMA_LEDGER_H