Files
nim-web3/tests/test_string_decoder.nim
Arnaud 48fb2d4a21 feat: abi encoder / decoder (#216)
* Add nimbledeps to gitignore

* Add abi encoder

* Add encoding tests

* Add AbiEncoder

* Add encoding tests

* Fix async signature

* Add encoding tests to all tests

* Do not bubble the exception

* Add proper error handling

* Use faststream to simplify internal logic

* Partial implementation of the decoding

* Fix tuple encoding and try to provide better docs / tests

* Update abi util to to take tuple in consideration

* Add deprecation notice

* Refactor the dynamic array / seq decoding

* Add decoding

* Add ABI encoder / decoder to the readme

* Fix indent

* Export decoding

* Restore previous encoding functions and mark them deprecated

* Remove compiler hints and warnings

* Use inputStream signature and use readAll

* Add custom types

* Cleanup

* Add support for serialization

* Add support for dontSerialize pragma

* Add readValue compatibility

* Add readme for serialization

* Use enumInstanceSerializedFields to get the serialized fields instead of checking manually the pragma

* Use Abi serialization as main interface

* Update README

* Update deprecation message and export abi_serialization

* Update deprecation message and export abi_serialization

* Add tests for string

* Test encode empty string

* Encode object as tuple

* Remove ref object

* Ensure that object are encoded as tuple

* Decode object as tuple

* Remove compilation hint

* Remove unused import

* Replace ABI encoding and decoding by the new encoding method

* Improve serialization test

* Add test more 3 slots

* Remove unused variables

* Use existing type T to get the arity

* Use advance function to point on the right position instead of copying the whole buffer

* Use local encoder instead of calling Abi.encode

* Use SomeRange type in testing

* Add overflow detection when decoding int and provide clearer comments

* Re organize comments

* Fix indentation

* Add raises pragma

* Remove int / uint support

* Use toOpenArray method and replace StUint usage by uint.

* Use used pragma

* Reject range with int / uint value

* Apply toOpenArray function

* Remove unused function

* Check hardhat version on ci

* Update ci with hardhat 3

* Remove the intermediate data seq

* Remove comment

* Remove comment

* Use explicit return
2025-08-28 14:51:34 +02:00

45 lines
2.6 KiB
Nim

import
pkg/unittest2,
stew/byteutils,
serialization,
../web3/decoding,
../web3/primitives
type
PubKeyBytes = DynamicBytes[48, 48]
WithdrawalCredentialsBytes = DynamicBytes[32, 32]
SignatureBytes = DynamicBytes[96, 96]
Int64LeBytes = DynamicBytes[8, 8]
const input = "00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030b2f5263a3454de3a9116b0edaa3cfbb2795a99482ee268b7aed5b15b532d9b20c34b67c82877ba1326326f3ae6cc5ad3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020010000000000000000000000a3f7076718fa4fed91b5830a45489053eb367afb0000000000000000000000000000000000000000000000000000000000000008004059730700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000608ee47adfab0819ec0d9144a3f76c0a663f8d7d66a2cfd994eb5d23d15d779430ec85ecd3c91bf7b4d229a9c4a8bee83e0096913d0b05525307114d75fa9baf79b4634ba80d3d262dd769c66fb6af25bff30a5ce04600940d2271278fad5b3096000000000000000000000000000000000000000000000000000000000000000802f3000000000000000000000000000000000000000000000000000000000000"
template init[N: static int](T: type DynamicBytes[N, N]): T =
T newSeq[byte](N)
suite "String decoders":
test "Log message decoding":
let logData = hexToSeqByte(input)
var
pubkey = init PubKeyBytes
withdrawalCredentials = init WithdrawalCredentialsBytes
amount = init Int64LeBytes
signature = init SignatureBytes
index = init Int64LeBytes
type Tuple = tuple[
pubkey: PubKeyBytes,
withdrawalCredentials: WithdrawalCredentialsBytes,
amount: Int64LeBytes,
signature: SignatureBytes,
index: Int64LeBytes
]
(pubkey, withdrawalCredentials, amount, signature, index) =
Abi.decode(logData, Tuple)
assert($pubkey == "0xb2f5263a3454de3a9116b0edaa3cfbb2795a99482ee268b7aed5b15b532d9b20c34b67c82877ba1326326f3ae6cc5ad3")
assert($withdrawalCredentials == "0x010000000000000000000000a3f7076718fa4fed91b5830a45489053eb367afb")
assert($amount == "0x0040597307000000")
assert($signature == "0x8ee47adfab0819ec0d9144a3f76c0a663f8d7d66a2cfd994eb5d23d15d779430ec85ecd3c91bf7b4d229a9c4a8bee83e0096913d0b05525307114d75fa9baf79b4634ba80d3d262dd769c66fb6af25bff30a5ce04600940d2271278fad5b3096")
assert($index == "0x02f3000000000000")