mirror of
https://github.com/logos-storage/nim-contract-abi.git
synced 2026-03-14 02:03:13 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a7b4cecce | ||
|
|
deccf3e1be | ||
|
|
977d6f5fa3 | ||
|
|
b9696af998 | ||
|
|
524bda044b | ||
|
|
c0c0832676 | ||
|
|
36dd1086ab | ||
|
|
9ea526814d | ||
|
|
5a215dc1c2 | ||
|
|
4da6e9cddd | ||
|
|
87894f69aa | ||
|
|
c076aca82c | ||
|
|
1e72c2f10e | ||
|
|
754a59e145 | ||
|
|
c82a70b353 | ||
|
|
a5f676eecc |
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -7,7 +7,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
nim: [stable, 1.4.8, 1.2.14]
|
nim: [2.0.14, stable]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- uses: iffy/install-nim@v3
|
- uses: iffy/install-nim@v3
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@
|
|||||||
!*.*
|
!*.*
|
||||||
nimble.develop
|
nimble.develop
|
||||||
nimble.paths
|
nimble.paths
|
||||||
|
nimbledeps/
|
||||||
|
|||||||
@ -11,7 +11,7 @@ Use the [Nimble][2] package manager to add `contractabi` to an existing project.
|
|||||||
Add the following to its .nimble file:
|
Add the following to its .nimble file:
|
||||||
|
|
||||||
```nim
|
```nim
|
||||||
requires "contractabi >= 0.4.6 & < 0.5.0"
|
requires "contractabi >= 0.7.1 & < 0.8.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
|
|||||||
@ -1,10 +1,9 @@
|
|||||||
version = "0.4.6"
|
version = "0.7.3"
|
||||||
author = "Contract ABI Authors"
|
author = "Contract ABI Authors"
|
||||||
description = "ABI Encoding for Ethereum contracts"
|
description = "ABI Encoding for Ethereum contracts"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
requires "stint"
|
requires "stint >= 0.8.1 & < 0.9.0"
|
||||||
requires "stew"
|
requires "stew >= 0.2.0"
|
||||||
requires "nimcrypto >= 0.5.4 & < 0.6.0"
|
requires "nimcrypto >= 0.6.2 & < 0.8.0"
|
||||||
requires "questionable >= 0.10.0 & < 0.11.0"
|
requires "questionable >= 0.10.10 & < 0.11.0"
|
||||||
requires "upraises >= 0.1.0 & < 0.2.0"
|
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
import pkg/stew/byteutils
|
import pkg/stew/byteutils
|
||||||
import pkg/questionable
|
import pkg/questionable
|
||||||
import pkg/upraises
|
|
||||||
|
|
||||||
push: {.upraises: [].}
|
{.push raises:[].}
|
||||||
|
|
||||||
type
|
type
|
||||||
Address* = distinct array[20, byte]
|
Address* = distinct array[20, byte]
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import pkg/stint
|
|||||||
import pkg/stew/endians2
|
import pkg/stew/endians2
|
||||||
import pkg/stew/byteutils
|
import pkg/stew/byteutils
|
||||||
import pkg/questionable/results
|
import pkg/questionable/results
|
||||||
import pkg/upraises
|
|
||||||
import ./encoding
|
import ./encoding
|
||||||
import ./integers
|
import ./integers
|
||||||
import ./address
|
import ./address
|
||||||
@ -11,7 +10,7 @@ import ./address
|
|||||||
export stint
|
export stint
|
||||||
export address
|
export address
|
||||||
|
|
||||||
push: {.upraises:[].}
|
{.push raises:[].}
|
||||||
|
|
||||||
type
|
type
|
||||||
AbiDecoder* = object
|
AbiDecoder* = object
|
||||||
@ -193,3 +192,13 @@ func decode*(_: type AbiDecoder, bytes: seq[byte], T: type): ?!T =
|
|||||||
var value = ?decoder.decode(T)
|
var value = ?decoder.decode(T)
|
||||||
?decoder.finish()
|
?decoder.finish()
|
||||||
success value
|
success value
|
||||||
|
|
||||||
|
func decodeRecord*(_: type AbiDecoder, bytes: seq[byte], T: type): ?!T =
|
||||||
|
var decoder = AbiDecoder.init(bytes)
|
||||||
|
var res: T
|
||||||
|
decoder.startTuple()
|
||||||
|
for value in res.fields:
|
||||||
|
value = ?decoder.read(typeof(value))
|
||||||
|
decoder.finishTuple()
|
||||||
|
?decoder.finish()
|
||||||
|
success res
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
import std/typetraits
|
import std/typetraits
|
||||||
import pkg/stint
|
import pkg/stint
|
||||||
import pkg/upraises
|
|
||||||
import pkg/stew/byteutils
|
import pkg/stew/byteutils
|
||||||
|
import pkg/stew/endians2
|
||||||
import ./integers
|
import ./integers
|
||||||
import ./address
|
import ./address
|
||||||
|
|
||||||
export stint
|
export stint
|
||||||
export address
|
export address
|
||||||
|
|
||||||
push: {.upraises:[].}
|
{.push raises:[].}
|
||||||
|
|
||||||
type
|
type
|
||||||
AbiEncoder* = object
|
AbiEncoder* = object
|
||||||
@ -87,7 +87,12 @@ func padright(encoder: var AbiEncoder, bytes: openArray[byte], padding=0'u8) =
|
|||||||
func encode(encoder: var AbiEncoder, value: SomeUnsignedInt | StUint) =
|
func encode(encoder: var AbiEncoder, value: SomeUnsignedInt | StUint) =
|
||||||
encoder.padleft(value.toBytesBE)
|
encoder.padleft(value.toBytesBE)
|
||||||
|
|
||||||
func encode(encoder: var AbiEncoder, value: SomeSignedInt | StInt) =
|
func encode(encoder: var AbiEncoder, value: SomeSignedInt) =
|
||||||
|
let bytes = value.unsigned.toBytesBE
|
||||||
|
let padding = if value < 0: 0xFF'u8 else: 0x00'u8
|
||||||
|
encoder.padleft(bytes, padding)
|
||||||
|
|
||||||
|
func encode(encoder: var AbiEncoder, value: StInt) =
|
||||||
let bytes = value.unsigned.toBytesBE
|
let bytes = value.unsigned.toBytesBE
|
||||||
let padding = if value.isNegative: 0xFF'u8 else: 0x00'u8
|
let padding = if value.isNegative: 0xFF'u8 else: 0x00'u8
|
||||||
encoder.padleft(bytes, padding)
|
encoder.padleft(bytes, padding)
|
||||||
|
|||||||
@ -14,4 +14,4 @@ func unsigned*(value: SomeSignedInt): SomeUnsignedInt =
|
|||||||
cast[typeof(value).unsigned](value)
|
cast[typeof(value).unsigned](value)
|
||||||
|
|
||||||
func unsigned*[bits](value: StInt[bits]): StUint[bits] =
|
func unsigned*[bits](value: StInt[bits]): StUint[bits] =
|
||||||
value.stuint(bits)
|
cast[typeof(value).unsigned](value)
|
||||||
|
|||||||
85
nimble.lock
85
nimble.lock
@ -1,57 +1,72 @@
|
|||||||
{
|
{
|
||||||
"version": 1,
|
"version": 2,
|
||||||
"packages": {
|
"packages": {
|
||||||
"upraises": {
|
|
||||||
"version": "0.1.0",
|
|
||||||
"vcsRevision": "ff4f8108e44fba9b35cac535ab63d3927e8fd3c2",
|
|
||||||
"url": "https://github.com/markspanbroek/upraises.git",
|
|
||||||
"downloadMethod": "git",
|
|
||||||
"dependencies": [],
|
|
||||||
"checksums": {
|
|
||||||
"sha1": "a0243c8039e12d547dbb2e9c73789c16bb8bc956"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nimcrypto": {
|
"nimcrypto": {
|
||||||
"version": "0.5.4",
|
"version": "0.7.2",
|
||||||
"vcsRevision": "a5742a9a214ac33f91615f3862c7b099aec43b00",
|
"vcsRevision": "8085515e717b07e29de1ef50d5e9f15a3f6004c0",
|
||||||
"url": "https://github.com/cheatfate/nimcrypto",
|
"url": "https://github.com/cheatfate/nimcrypto",
|
||||||
"downloadMethod": "git",
|
"downloadMethod": "git",
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"checksums": {
|
"checksums": {
|
||||||
"sha1": "f76c87707cd4e96355b8bb6ef27e7f8b0aac1e08"
|
"sha1": "8ed2a20f2efaa08782bb871284cbcc3100ca1dea"
|
||||||
}
|
|
||||||
},
|
|
||||||
"stew": {
|
|
||||||
"version": "0.1.0",
|
|
||||||
"vcsRevision": "6ad35b876fb6ebe0dfee0f697af173acc47906ee",
|
|
||||||
"url": "https://github.com/status-im/nim-stew.git",
|
|
||||||
"downloadMethod": "git",
|
|
||||||
"dependencies": [],
|
|
||||||
"checksums": {
|
|
||||||
"sha1": "46d58c4feb457f3241e3347778334e325dce5268"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"questionable": {
|
"questionable": {
|
||||||
"version": "0.10.2",
|
"version": "0.10.15",
|
||||||
"vcsRevision": "6018fd43e033d5a5310faa45bcaa1b44049469a4",
|
"vcsRevision": "82d90b67bcfb7f2e918b61dace2ff1a4ced60935",
|
||||||
"url": "https://github.com/status-im/questionable.git",
|
"url": "https://github.com/codex-storage/questionable",
|
||||||
"downloadMethod": "git",
|
"downloadMethod": "git",
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"checksums": {
|
"checksums": {
|
||||||
"sha1": "36a6c012637c7736a390e74a7f94667bca562073"
|
"sha1": "3238ff637c7b44d2fa8fcb839a8ded968e389de3"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"results": {
|
||||||
|
"version": "0.5.1",
|
||||||
|
"vcsRevision": "df8113dda4c2d74d460a8fa98252b0b771bf1f27",
|
||||||
|
"url": "https://github.com/arnetheduck/nim-results",
|
||||||
|
"downloadMethod": "git",
|
||||||
|
"dependencies": [],
|
||||||
|
"checksums": {
|
||||||
|
"sha1": "a9c011f74bc9ed5c91103917b9f382b12e82a9e7"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"unittest2": {
|
||||||
|
"version": "0.2.4",
|
||||||
|
"vcsRevision": "8b51e99b4a57fcfb31689230e75595f024543024",
|
||||||
|
"url": "https://github.com/status-im/nim-unittest2",
|
||||||
|
"downloadMethod": "git",
|
||||||
|
"dependencies": [],
|
||||||
|
"checksums": {
|
||||||
|
"sha1": "746106a4dfefffce497f1693733f1c1513b5c62c"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"stew": {
|
||||||
|
"version": "0.4.1",
|
||||||
|
"vcsRevision": "e5740014961438610d336cd81706582dbf2c96f0",
|
||||||
|
"url": "https://github.com/status-im/nim-stew",
|
||||||
|
"downloadMethod": "git",
|
||||||
|
"dependencies": [
|
||||||
|
"results",
|
||||||
|
"unittest2"
|
||||||
|
],
|
||||||
|
"checksums": {
|
||||||
|
"sha1": "996d9c058ee078d0209a5f539424a0235683918c"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"stint": {
|
"stint": {
|
||||||
"version": "0.0.1",
|
"version": "0.8.2",
|
||||||
"vcsRevision": "036c71d06a6b22f8f967ba9d54afd2189c3872ca",
|
"vcsRevision": "470b7892561b5179ab20bd389a69217d6213fe58",
|
||||||
"url": "https://github.com/status-im/stint.git",
|
"url": "https://github.com/status-im/nim-stint",
|
||||||
"downloadMethod": "git",
|
"downloadMethod": "git",
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"stew"
|
"stew",
|
||||||
|
"unittest2"
|
||||||
],
|
],
|
||||||
"checksums": {
|
"checksums": {
|
||||||
"sha1": "0f187a2115315ca898e5f9a30c5e506cf6057062"
|
"sha1": "d8f871fd617e7857192d4609fe003b48942a8ae5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
"tasks": {}
|
||||||
|
}
|
||||||
|
|||||||
3
tests/contractabi/config.nims
Normal file
3
tests/contractabi/config.nims
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
switch("path", "..")
|
||||||
|
when (NimMajor, NimMinor, NimPatch) >= (1, 6, 11):
|
||||||
|
switch("warning", "BareExcept:off")
|
||||||
@ -20,7 +20,7 @@ proc example*[T](_: type seq[T], len = 0..5): seq[T] =
|
|||||||
newSeqWith(chosenlen, T.example)
|
newSeqWith(chosenlen, T.example)
|
||||||
|
|
||||||
proc example*(T: type StUint): T =
|
proc example*(T: type StUint): T =
|
||||||
T.fromBytes(array[sizeof(T), byte].example)
|
T.fromBytesBE(array[sizeof(T), byte].example)
|
||||||
|
|
||||||
proc example*(T: type StInt): T =
|
proc example*(T: type StInt): T =
|
||||||
cast[T](StUint[T.bits].example)
|
cast[T](StUint[T.bits].example)
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
--path:"../.."
|
|
||||||
@ -12,6 +12,9 @@ type
|
|||||||
Custom3 = object
|
Custom3 = object
|
||||||
a: uint16
|
a: uint16
|
||||||
b: string
|
b: string
|
||||||
|
Custom4 = object
|
||||||
|
a: uint16
|
||||||
|
b: string
|
||||||
|
|
||||||
func encode(encoder: var AbiEncoder, custom: Custom1) =
|
func encode(encoder: var AbiEncoder, custom: Custom1) =
|
||||||
encoder.write( (custom.a, custom.b) )
|
encoder.write( (custom.a, custom.b) )
|
||||||
@ -48,11 +51,19 @@ func decode(decoder: var AbiDecoder, T: type Custom3): ?!T =
|
|||||||
# missing: decoder.finishTuple()
|
# missing: decoder.finishTuple()
|
||||||
success custom
|
success custom
|
||||||
|
|
||||||
|
func encode(encoder: var AbiEncoder, custom: Custom4) =
|
||||||
|
encoder.write( (custom.a, custom.b) )
|
||||||
|
|
||||||
|
func decode(decoder: var AbiDecoder, T: type Custom4): ?!T =
|
||||||
|
let custom = ?AbiDecoder.decodeRecord(bytes, Custom4)
|
||||||
|
success custom
|
||||||
|
|
||||||
suite "custom types":
|
suite "custom types":
|
||||||
|
|
||||||
let custom1 = Custom1(a: 42, b: "ultimate answer")
|
let custom1 = Custom1(a: 42, b: "ultimate answer")
|
||||||
let custom2 = Custom2(a: 42, b: "ultimate answer")
|
let custom2 = Custom2(a: 42, b: "ultimate answer")
|
||||||
let custom3 = Custom3(a: 42, b: "ultimate answer")
|
let custom3 = Custom3(a: 42, b: "ultimate answer")
|
||||||
|
let custom4 = Custom4(a: 42, b: "ultimate answer")
|
||||||
|
|
||||||
test "can be encoded":
|
test "can be encoded":
|
||||||
check:
|
check:
|
||||||
@ -78,3 +89,7 @@ suite "custom types":
|
|||||||
let encoding = AbiEncoder.encode( (custom3.a, custom3.b) )
|
let encoding = AbiEncoder.encode( (custom3.a, custom3.b) )
|
||||||
expect Exception:
|
expect Exception:
|
||||||
discard AbiDecoder.decode(encoding, Custom3)
|
discard AbiDecoder.decode(encoding, Custom3)
|
||||||
|
|
||||||
|
test "generic decode works":
|
||||||
|
let encoding = AbiEncoder.encode(custom4)
|
||||||
|
check AbiDecoder.decodeRecord(encoding, Custom4) == success custom4
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user