@riseandshaheen/libcma 0.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/ARCHITECTURE.md +348 -0
  2. package/LICENSE +21 -0
  3. package/README.md +157 -0
  4. package/binding.gyp +52 -0
  5. package/deps/machine-asset-tools/LICENSE +202 -0
  6. package/deps/machine-asset-tools/README.md +237 -0
  7. package/deps/machine-asset-tools/include/libcma/ledger.h +161 -0
  8. package/deps/machine-asset-tools/include/libcma/parser.h +305 -0
  9. package/deps/machine-asset-tools/include/libcma/types.h +22 -0
  10. package/deps/machine-guest-tools/LICENSE +202 -0
  11. package/deps/machine-guest-tools/README.md +57 -0
  12. package/deps/machine-guest-tools/sys-utils/libcmt/README.md +130 -0
  13. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/abi.h +578 -0
  14. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/buf.h +82 -0
  15. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/io.h +168 -0
  16. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/keccak.h +131 -0
  17. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/merkle.h +118 -0
  18. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/rollup.h +260 -0
  19. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/util.h +34 -0
  20. package/lib/index.cjs +445 -0
  21. package/lib/index.cjs.map +1 -0
  22. package/lib/index.d.cts +156 -0
  23. package/lib/index.d.ts +156 -0
  24. package/lib/index.js +428 -0
  25. package/lib/index.js.map +1 -0
  26. package/native/addon.cc +268 -0
  27. package/native/ledger_backend.h +28 -0
  28. package/native/mock/ledger_mock.cc +185 -0
  29. package/native/real/ledger_real.cc +271 -0
  30. package/package.json +74 -0
  31. package/scripts/build-libcma-riscv64.sh +80 -0
  32. package/scripts/build-native-riscv64.sh +40 -0
  33. package/scripts/install-native.mjs +94 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,237 @@
1
+ # Cartesi Machine Assets
2
+
3
+ The Cartesi Machine Assets is a C++ library to facilitate the creation and management of assets for the Cartesi Machine Rollups applications.
4
+
5
+ The API is divided into two main components
6
+
7
+ - parser: provides functions for common asset operations such as decoding operations and encoding deposits, withdrawals, and transfers.
8
+ - ledger: provides functions for managing asset ledgers and performing operations such as deposits, withdrawals, and transfers.
9
+
10
+ ## Installation
11
+
12
+ To install, download the latest release from the [GitHub releases page](https://github.com/Mugen-Builders/machine-asset-tools/releases). It contains libraries and headers files (runtime and development environments) for musl and glibc systems.
13
+
14
+ If you are creating a Cartesi Rollups Application using Docker, you can add following command to your Dockerfile:
15
+
16
+ ```dockerfile
17
+ ARG MACHINE_ASSET_TOOLS_VERSION
18
+ ADD https://github.com/Mugen-Builders/machine-asset-tools/releases/download/v${MACHINE_ASSET_TOOLS_VERSION}/machine-asset-tools_glibc_riscv64_v${MACHINE_ASSET_TOOLS_VERSION}.tar.gz /tmp/
19
+ RUN tar -xzf /tmp/machine-asset-tools_glibc_riscv64_v${MACHINE_ASSET_TOOLS_VERSION}.tar.gz -C / \
20
+ && rm /tmp/machine-asset-tools_glibc_riscv64_v${MACHINE_ASSET_TOOLS_VERSION}.tar.gz
21
+ ```
22
+
23
+ This will add the shared library to your Docker image. You can use the `_dev` artifact to add the headers and the static library.
24
+
25
+ ## Usage
26
+
27
+ the library is divided into two parts: parser and ledger. The former helps to parse the input data, while the latter helps to manage the ledger state.
28
+
29
+ ### Parser
30
+
31
+ The parser helps to parse the input data, which is in the form of an abi encoded byte string for advances a JSON-rpc-like string for inspects. The parser contains functions to decode the input and store the parsed data in a struct, and encode a voucher based pn a voucher request struct.
32
+
33
+ Ex. decode a Erc20 deposit:
34
+
35
+ ```c
36
+ // input is a cmt_rollup_advance_t struct decoded from cmt_rollup_read_advance_state(rollup, &input)
37
+ cma_parser_input_t parser_input;
38
+
39
+ const int err = cma_parser_decode_advance(CMA_PARSER_INPUT_TYPE_ERC20_DEPOSIT, &input, &parser_input);
40
+ if (err < 0) {
41
+ printf("unable to decode erc20 deposit: %d - %s\n", -err, cma_parser_get_last_error_message());
42
+ }
43
+
44
+ parser_input.erc20_deposit.sender; // sender of the deposit
45
+ parser_input.erc20_deposit.token; // token address of the deposit
46
+ parser_input.erc20_deposit.amount; // amount of the deposit
47
+ parser_input.erc20_deposit.exec_layer_data; // exec layer data included on the deposit
48
+ ```
49
+
50
+ Note: if the input is not a deposit, the parser can decode the input automatically by the functions selector of the payload. Use the the `CMA_PARSER_INPUT_TYPE_AUTO` type.
51
+
52
+ Explore the [parser's](/include/libcma/parser.h) to learn the function definitions and structs, the [sample applications](/sample_apps), and the [tests](/tests/parser.c) to see it in use.
53
+
54
+ ### Ledger
55
+
56
+ The ledger manages the assets of accounts. It provides functions to deposit, transfer, and withdraw assets.
57
+
58
+ Ex. add a deposit to a new account:
59
+
60
+ ```c
61
+ cma_ledger_t ledger;
62
+ if (!cma_ledger_init(&ledger)) {
63
+ printf(cma_ledger_get_last_error_message());
64
+ }
65
+
66
+ // find/create account with ether address
67
+ cma_ledger_account_t account = {.address = {.data = {
68
+ 0xf3, 0x9f, 0xd6, 0xe5, 0x1a, 0xad, 0x88, 0xf6, 0xf4, 0xce, 0x6a, 0xb8, 0x82, 0x72, 0x79, 0xcf, 0xff, 0xb9, 0x22, 0x66,
69
+ }}};
70
+ cma_ledger_account_id_t account_id;
71
+ if (!cma_ledger_retrieve_account(&ledger, &account_id, &account, NULL, CMA_LEDGER_ACCOUNT_TYPE_ETHER_ADDRESS, CMA_LEDGER_OP_FIND_OR_CREATE)) {
72
+ printf(cma_ledger_get_last_error_message());
73
+ }
74
+
75
+ // find/create asset with token address
76
+ cma_token_address_t token_address = {.address = {.data = {
77
+ 0x49, 0x16, 0x04, 0xc0, 0xfd, 0xf0, 0x83, 0x47, 0xdd, 0x1f, 0xa4, 0xee, 0x06, 0x2a, 0x82, 0x2a, 0x5d, 0xd0, 0x6b, 0x5d,
78
+ }}};
79
+ cma_ledger_asset_id_t asset_id;
80
+ if (!cma_ledger_retrieve_asset(&ledger, &asset_id, &token_address, NULL, CMA_LEDGER_ASSET_TYPE_TOKEN_ADDRESS, CMA_LEDGER_OP_FIND_OR_CREATE)) {
81
+ printf(cma_ledger_get_last_error_message());
82
+ }
83
+
84
+ // amount
85
+ cma_amount_t amount = {.data = {
86
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0xe0, 0xb6, 0xb3, 0xa7, 0x64, 0x00, 0x00,
87
+ }};
88
+
89
+ // deposit
90
+ if (!cma_ledger_deposit(&ledger, asset_id, account_id, &amount)) {
91
+ printf(cma_ledger_get_last_error_message());
92
+ }
93
+
94
+ if (!cma_ledger_fini(&ledger)) {
95
+ printf(cma_ledger_get_last_error_message());
96
+ }
97
+ ```
98
+
99
+ The ledger uses an internal uint32 for the identification of the account and the assets. If enabled, every time you try to retrieve an asset or account that is not on the storage it creates a new one.
100
+
101
+ You can create an account without any account id, so it will never be mapped to any external identifier. You can also create an account with a an ether address, so it will be mapped to an external identifier, or a 32-byte string. Note that only accounts defined by ether addresses should be able to withdraw assets from the application.
102
+
103
+ The asset model defines a single model for all kinds of assets: it either has token address, token address and token id, or no token address and token id. Furthermore, the asset always may have any amount. In that sense, the differences between Ether, Erc20, Erc721, and Erc1155 are implemented on how you define the asset.
104
+
105
+ Explore the [ledger's](/include/libcma/ledger.h) to learn the function definitions and structs, the [sample application](/sample_apps/wallet_app/app.cpp), and the [tests](/tests/ledger.c) to see it in use.
106
+
107
+ ## Reference
108
+
109
+ ### Parser Function selectors
110
+
111
+ Advance:
112
+
113
+ ```c
114
+ // Bytecode for solidity WithdrawEther(uint256,bytes) = 8cf70f0b
115
+ WITHDRAW_ETHER = 0x8cf70f0b,
116
+ // Bytecode for solidity WithdrawErc20(address,uint256,bytes) = 4f94d342
117
+ WITHDRAW_ERC20 = 0x4f94d342,
118
+ // Bytecode for solidity WithdrawErc721(address,uint256,bytes) = 33acf293
119
+ WITHDRAW_ERC721 = 0x33acf293,
120
+ // Bytecode for solidity WithdrawErc1155Single(address,uint256,uint256,bytes) = 8bb0a811
121
+ WITHDRAW_ERC1155_SINGLE = 0x8bb0a811,
122
+ // Bytecode for solidity WithdrawErc1155Batch(address,uint256[],uint256[],bytes) = 50c80019
123
+ WITHDRAW_ERC1155_BATCH = 0x50c80019,
124
+
125
+ // Bytecode for solidity TransferEther(bytes32,uint256,bytes) = ff67c903
126
+ TRANSFER_ETHER = 0xff67c903,
127
+ // Bytecode for solidity TransferErc20(address,bytes32,uint256,bytes) = 03d61dcd
128
+ TRANSFER_ERC20 = 0x03d61dcd,
129
+ // Bytecode for solidity TransferErc721(address,bytes32,uint256,bytes) = af615a5a
130
+ TRANSFER_ERC721 = 0xaf615a5a,
131
+ // Bytecode for solidity TransferErc1155Single(address,bytes32,uint256,uint256,bytes) = e1c913ed
132
+ TRANSFER_ERC1155_SINGLE = 0xe1c913ed,
133
+ // Bytecode for solidity TransferErc1155Batch(address,bytes32,uint256[],uint256[],bytes) = 638ac6f9
134
+ TRANSFER_ERC1155_BATCH = 0x638ac6f9,
135
+
136
+ // Bytecode for solidity transfer(address,uint256) = a9059cbb
137
+ ERC20_TRANSFER_FUNCTION_SELECTOR_FUNSEL = 0xa9059cbb,
138
+ // Bytecode for solidity safeTransferFrom(address,address,uint256) = 42842e0e
139
+ ERC721_TRANSFER_FUNCTION_SELECTOR_FUNSEL = 0x42842e0e,
140
+ // Bytecode for solidity safeTransferFrom(address,address,uint256,uint256,bytes) = f242432a
141
+ ERC1155_SINGLE_TRANSFER_FUNCTION_SELECTOR_FUNSEL = 0xf242432a,
142
+ // Bytecode for solidity safeBatchTransferFrom(address,address,uint256[],uint256[],bytes) = 2eb2c2d6
143
+ ERC1155_BATCH_TRANSFER_FUNCTION_SELECTOR_FUNSEL = 0x2eb2c2d6,
144
+ ```
145
+
146
+ Note on transfer function: the receiver parameter from the transfer functions is a 32 bytes account id. This allows transfers for the "internal" accounts. Ether addresses should be converted to 32-byte strings when used in transfers.
147
+
148
+ Inspect methods:
149
+
150
+ ```c
151
+ GET_BALANCE = "ledger_getBalance"
152
+ GET_TOTAL_SUPPLY = "ledger_getTotalSupply"
153
+ ```
154
+
155
+ Full inspect example:
156
+
157
+ ```javascript
158
+ {
159
+ "method":"ledger_getBalance",
160
+ "params":[
161
+ "0xcb76129e0ed325e1e5c17f3f363bc2e93a227ecf", // required: 20-byte wallet address or 32-byte account id
162
+ "0xc700000000000000000000000000000000000051", // optional: 20-byte token address
163
+ "0x0711", // optional: 32-byte or relevant bytes token id
164
+ "abc" // optional: exec layer data
165
+ ]
166
+ }
167
+
168
+ {
169
+ "method":"ledger_getTotalSupply",
170
+ "params":[
171
+ "0xc700000000000000000000000000000000000051", // optional: 20-byte token address
172
+ "0x0711", // optional: 32-byte or relevant bytes token id
173
+ "abc" // optional: exec layer data
174
+ ]
175
+ }
176
+ ```
177
+
178
+ ### Parser Functions
179
+
180
+ ```c
181
+ // decode advance
182
+ int cma_parser_decode_advance(cma_parser_input_type_t type, const cmt_rollup_advance_t *input,
183
+ cma_parser_input_t *parser_input);
184
+
185
+ // decode inspect
186
+ int cma_parser_decode_inspect(cma_parser_input_type_t type, const cmt_rollup_inspect_t *input,
187
+ cma_parser_input_t *parser_input);
188
+
189
+ // encode voucher
190
+ int cma_parser_encode_voucher(cma_parser_voucher_type_t type, const cma_abi_address_t *app_address,
191
+ const cma_parser_voucher_data_t *voucher_request, cma_voucher_t *voucher);
192
+
193
+ // get error message
194
+ const char *cma_parser_get_last_error_message();
195
+ ```
196
+
197
+ ### Ledger Functions
198
+
199
+ ```c
200
+ // manage ledger class
201
+ int cma_ledger_init(cma_ledger_t *ledger);
202
+ int cma_ledger_fini(cma_ledger_t *ledger);
203
+ int cma_ledger_reset(cma_ledger_t *ledger);
204
+
205
+ // Retrieve/create an asset
206
+ int cma_ledger_retrieve_asset(cma_ledger_t *ledger, cma_ledger_asset_id_t *asset_id,
207
+ cma_token_address_t *token_address, cma_token_id_t *token_id, cma_ledger_asset_type_t asset_type,
208
+ cma_ledger_retrieve_operation_t operation);
209
+
210
+ // Retrieve/create an account
211
+ int cma_ledger_retrieve_account(cma_ledger_t *ledger, cma_ledger_account_id_t *account_id,
212
+ cma_ledger_account_t *account, const void *addr_accid, cma_ledger_account_type_t account_type,
213
+ cma_ledger_retrieve_operation_t operation);
214
+
215
+ // Deposit
216
+ int cma_ledger_deposit(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
217
+ cma_ledger_account_id_t to_account_id, const cma_amount_t *deposit);
218
+
219
+ // Withdrawal
220
+ int cma_ledger_withdraw(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
221
+ cma_ledger_account_id_t from_account_id, const cma_amount_t *withdrawal);
222
+
223
+ // Transfer
224
+ int cma_ledger_transfer(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
225
+ cma_ledger_account_id_t from_account_id, cma_ledger_account_id_t to_account_id, const cma_amount_t *amount);
226
+
227
+ // Get balance
228
+ int cma_ledger_get_balance(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
229
+ cma_ledger_account_id_t account_id, cma_amount_t *out_balance);
230
+
231
+ // Get total supply
232
+ int cma_ledger_get_total_supply(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
233
+ cma_amount_t *out_total_supply);
234
+
235
+ // get error message
236
+ const char *cma_ledger_get_last_error_message();
237
+ ```
@@ -0,0 +1,161 @@
1
+ #ifndef CMA_LEDGER_H
2
+ #define CMA_LEDGER_H
3
+ #include <stddef.h>
4
+ #include <stdint.h>
5
+
6
+ #ifdef __cplusplus
7
+ extern "C" {
8
+ #endif
9
+
10
+ // Compiler visibility definition
11
+ #ifndef CMA_LEDGER_API
12
+ #define CMA_LEDGER_API __attribute__((visibility("default")))
13
+ #endif
14
+
15
+ #include "types.h"
16
+
17
+ enum {
18
+ CMA_LEDGER_T_SIZE = 512, // / 8,
19
+ CMA_LEDGER_MIN_MEM_LENGTH = 262144,
20
+ };
21
+
22
+ typedef struct cma_ledger_struct {
23
+ uint64_t __opaque[CMA_LEDGER_T_SIZE];
24
+ } cma_ledger_t;
25
+
26
+ typedef uint64_t cma_ledger_account_id_t;
27
+ typedef uint64_t cma_ledger_asset_id_t;
28
+
29
+ enum {
30
+ CMA_LEDGER_SUCCESS = 0,
31
+ CMA_LEDGER_ERROR_UNKNOWN = -1001,
32
+ CMA_LEDGER_ERROR_EXCEPTION = -1002,
33
+ CMA_LEDGER_ERROR_INSUFFICIENT_FUNDS = -1003,
34
+ CMA_LEDGER_ERROR_ACCOUNT_NOT_FOUND = -1004,
35
+ CMA_LEDGER_ERROR_ASSET_NOT_FOUND = -1005,
36
+ CMA_LEDGER_ERROR_BALANCE_NOT_FOUND = -1006,
37
+ CMA_LEDGER_ERROR_SUPPLY_OVERFLOW = -1007,
38
+ CMA_LEDGER_ERROR_BALANCE_OVERFLOW = -1008,
39
+ CMA_LEDGER_ERROR_INVALID_ACCOUNT = -1009,
40
+ CMA_LEDGER_ERROR_INSERTION_ERROR = -1010,
41
+ CMA_LEDGER_ERROR_MAX_ASSETS_REACHED = -1011,
42
+ CMA_LEDGER_ERROR_MAX_ACCOUNTS_REACHED = -1012,
43
+ CMA_LEDGER_ERROR_MAX_BALANCES_REACHED = -1013,
44
+ CMA_LEDGER_ERROR_ASSET_SUPPLY = -1014,
45
+ CMA_LEDGER_ERROR_ACCOUNT_BALANCE = -1015,
46
+ CMA_LEDGER_ERROR_REMOVE = -1016,
47
+ };
48
+
49
+ typedef enum {
50
+ CMA_LEDGER_OP_FIND,
51
+ CMA_LEDGER_OP_CREATE,
52
+ CMA_LEDGER_OP_FIND_OR_CREATE,
53
+ CMA_LEDGER_OP_FIND_AND_REMOVE,
54
+ } cma_ledger_retrieve_operation_t;
55
+
56
+ typedef enum {
57
+ CMA_LEDGER_ASSET_TYPE_ID,
58
+ CMA_LEDGER_ASSET_TYPE_BASE,
59
+ CMA_LEDGER_ASSET_TYPE_TOKEN_ADDRESS,
60
+ CMA_LEDGER_ASSET_TYPE_TOKEN_ADDRESS_ID,
61
+ CMA_LEDGER_ASSET_TYPE_TOKEN_ADDRESS_ID_AMOUNT,
62
+ } cma_ledger_asset_type_t;
63
+
64
+ typedef enum {
65
+ CMA_LEDGER_ACCOUNT_TYPE_ID,
66
+ CMA_LEDGER_ACCOUNT_TYPE_WALLET_ADDRESS,
67
+ CMA_LEDGER_ACCOUNT_TYPE_ACCOUNT_ID,
68
+ } cma_ledger_account_type_t;
69
+
70
+ #pragma GCC diagnostic push
71
+ #pragma GCC diagnostic ignored "-Wpedantic"
72
+ typedef struct cma_ledger_account {
73
+ cma_ledger_account_type_t type;
74
+ union {
75
+ cma_account_id_t account_id;
76
+ struct {
77
+ uint8_t fix[CMA_ABI_ID_LENGTH - CMA_ABI_ADDRESS_LENGTH];
78
+ cma_abi_address_t address;
79
+ } __attribute__((__packed__));
80
+ };
81
+ } cma_ledger_account_t;
82
+ #pragma GCC diagnostic pop
83
+
84
+ typedef struct cma_ledger_account_balance {
85
+ uint32_t type;
86
+ cma_abi_address_t owner;
87
+ cma_token_address_t token_address;
88
+ cma_token_id_t token_id;
89
+ cma_amount_t amount;
90
+ uint8_t padding[20]; // Align to 4*32 bytes
91
+ } cma_ledger_account_balance_t;
92
+
93
+ typedef struct cma_ledger_account_balance_info {
94
+ ptrdiff_t offset;
95
+ size_t index;
96
+ cma_ledger_account_balance_t *balance;
97
+ } cma_ledger_account_balance_info_t;
98
+
99
+ CMA_LEDGER_API int cma_ledger_init(cma_ledger_t *ledger);
100
+ CMA_LEDGER_API int cma_ledger_fini(cma_ledger_t *ledger);
101
+ CMA_LEDGER_API int cma_ledger_reset(cma_ledger_t *ledger);
102
+
103
+ CMA_LEDGER_API int cma_ledger_init_file(cma_ledger_t *ledger, const char *memory_file_name, size_t offset,
104
+ size_t mem_length, size_t n_accounts, size_t n_assets, size_t n_balances);
105
+
106
+ CMA_LEDGER_API int cma_ledger_init_buffer(cma_ledger_t *ledger, void *buffer, size_t mem_length, size_t n_accounts,
107
+ size_t n_assets, size_t n_balances);
108
+
109
+ // Initialize a single-asset ledger over a memory-mapped file (create-or-open).
110
+ // Balances are 64-bit; the asset is fixed at init and immutable. asset_type must be
111
+ // CMA_LEDGER_ASSET_TYPE_BASE (ether, token_address NULL) or CMA_LEDGER_ASSET_TYPE_TOKEN_ADDRESS
112
+ // (ERC20, token_address required). n_accounts is the capacity of the withdrawable balance drive.
113
+ CMA_LEDGER_API int cma_ledger_init_single_file(cma_ledger_t *ledger, const char *memory_file_name, size_t offset,
114
+ size_t mem_length, size_t n_accounts, cma_ledger_asset_type_t asset_type,
115
+ const cma_token_address_t *token_address);
116
+
117
+ // Initialize a single-asset ledger over a caller-provided buffer (non-persistent).
118
+ CMA_LEDGER_API int cma_ledger_init_single_buffer(cma_ledger_t *ledger, void *buffer, size_t mem_length,
119
+ size_t n_accounts, cma_ledger_asset_type_t asset_type, const cma_token_address_t *token_address);
120
+
121
+ // Retrieve/create an asset
122
+ // try to retrieve: If id is defined, fill with the asset details, otherwise fill with id
123
+ // If it didn't find the asset and creation type is set with one of the options, create it
124
+ // update the token asset_id <-> (address, token id) mapping
125
+ CMA_LEDGER_API int cma_ledger_retrieve_asset(cma_ledger_t *ledger, cma_ledger_asset_id_t *asset_id,
126
+ cma_token_address_t *token_address, cma_token_id_t *token_id, cma_amount_t *out_total_supply,
127
+ cma_ledger_asset_type_t *asset_type, cma_ledger_retrieve_operation_t operation);
128
+
129
+ // Retrieve/create an account
130
+ // try to retrieve: If id is defined, fill with the account details, otherwise fill with id
131
+ // If it didn't find the account and creation type is set with one of the options, create it
132
+ // update the token id <-> account_id mapping
133
+ CMA_LEDGER_API int cma_ledger_retrieve_account(cma_ledger_t *ledger, cma_ledger_account_id_t *account_id,
134
+ cma_ledger_account_t *account, const void *addr_accid, size_t *n_balances, cma_ledger_account_type_t *account_type,
135
+ cma_ledger_retrieve_operation_t operation);
136
+
137
+ // Deposit
138
+ CMA_LEDGER_API int cma_ledger_deposit(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
139
+ cma_ledger_account_id_t to_accountcma_ledger_account_balance_t_id, const cma_amount_t *deposit);
140
+
141
+ // Withdrawal
142
+ CMA_LEDGER_API int cma_ledger_withdraw(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
143
+ cma_ledger_account_id_t from_account_id, const cma_amount_t *withdrawal);
144
+
145
+ // Transfer
146
+ CMA_LEDGER_API int cma_ledger_transfer(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
147
+ cma_ledger_account_id_t from_account_id, cma_ledger_account_id_t to_account_id, const cma_amount_t *amount);
148
+
149
+ // Get balance
150
+ CMA_LEDGER_API int cma_ledger_get_balance(cma_ledger_t *ledger, cma_ledger_asset_id_t asset_id,
151
+ cma_ledger_account_id_t account_id, cma_amount_t *out_balance,
152
+ cma_ledger_account_balance_info_t *account_balance_info);
153
+
154
+ // get error message
155
+ CMA_LEDGER_API const char *cma_ledger_get_last_error_message();
156
+
157
+ #ifdef __cplusplus
158
+ } // extern "C"
159
+ #endif
160
+
161
+ #endif // CMA_LEDGER_H