Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/codex-storage/nim-contract-abi
Contract ABI Encoding
https://github.com/codex-storage/nim-contract-abi
Last synced: 2 months ago
JSON representation
Contract ABI Encoding
- Host: GitHub
- URL: https://github.com/codex-storage/nim-contract-abi
- Owner: codex-storage
- License: other
- Created: 2021-11-25T08:34:37.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-06T08:00:23.000Z (12 months ago)
- Last Synced: 2024-07-29T00:47:00.807Z (6 months ago)
- Language: Nim
- Size: 42 KB
- Stars: 3
- Watchers: 10
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: License.md
Awesome Lists containing this project
- awesome-nim - contract-abi - Implements encoding of parameters according to the Ethereum Contract ABI specification. (Algorithms / Blockchain)
README
Contract ABI
============Implements encoding of parameters according to the Ethereum
[Contract ABI Specification][1].Installation
------------Use the [Nimble][2] package manager to add `contractabi` to an existing project.
Add the following to its .nimble file:```nim
requires "contractabi >= 0.6.0 & < 0.7.0"
```Usage
-----```nim
import contractabi# encode unsigned integers, booleans, enums
AbiEncoder.encode(42'u8)# encode uint256
import stint
AbiEncoder.encode(42.u256)# encode byte arrays and sequences
AbiEncoder.encode([1'u8, 2'u8, 3'u8])
AbiEncoder.encode(@[1'u8, 2'u8, 3'u8])# encode tuples
AbiEncoder.encode( (42'u8, @[1'u8, 2'u8, 3'u8], true) )# decode values of different types
AbiDecoder.decode(bytes, uint8)
AbiDecoder.decode(bytes, UInt256)
AbiDecoder.decode(bytes, array[3, uint8])
AbiDecoder.decode(bytes, seq[uint8])# decode tuples
AbiDecoder.decode(bytes, (uint32, bool, seq[byte]) )# add support for encoding of custom types
import questionable/resultstype CustomType = object
a: uint16
b: stringfunc encode(encoder: var AbiEncoder, custom: CustomType) =
encoder.write( (custom.a, custom.b) )func decode(decoder: var AbiDecoder, T: type CustomType): ?!T =
let (a, b) = ?decoder.read( (uint16, string) )
success CustomType(a: a, b: b)```
[1]: https://docs.soliditylang.org/en/latest/abi-spec.html
[2]: https://github.com/nim-lang/nimble