Skip to content

Commit 4e24cc0

Browse files
authored
Add several examples. Prepare 0.1.0-beta.1 (#17)
* Add first examples, use U256 for references. * Add more examples * Add MSRV * Release 0.1.0-beta.1
1 parent 3a8e8fa commit 4e24cc0

15 files changed

+595
-214
lines changed

.github/workflows/build.yml

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,82 @@ on:
66
pull_request:
77
branches:
88
- master
9+
schedule:
10+
- cron: "0 0 * * *"
911

1012
jobs:
11-
build:
12-
name: Build
13+
clippy:
14+
name: Clippy
1315
runs-on: ubuntu-latest
1416
steps:
1517
- uses: actions/checkout@v4
1618
- name: Stable with rustfmt and clippy
1719
uses: dtolnay/rust-toolchain@stable
1820
with:
1921
components: rustfmt, clippy
20-
- name: Release build
21-
run: cargo build --release --all-features
2222
- uses: giraffate/clippy-action@v1
2323
with:
2424
reporter: 'github-pr-review'
2525
github_token: ${{ secrets.GITHUB_TOKEN }}
26+
27+
build:
28+
name: Build with rust ${{matrix.rust}} on ${{matrix.os == 'ubuntu' && 'Linux' || matrix.os == 'macos' && 'macOS' || matrix.os == 'windows' && 'Windows' || '???'}}
29+
runs-on: ${{matrix.os}}-latest
30+
strategy:
31+
fail-fast: false
32+
matrix:
33+
rust:
34+
- "1.67.1" # MSRV
35+
- "stable"
36+
- "beta"
37+
- "nightly"
38+
os: [ubuntu, windows, macos]
39+
steps:
40+
- uses: actions/checkout@v4
41+
- name: Stable with rustfmt and clippy
42+
uses: dtolnay/rust-toolchain@master
43+
with:
44+
components: rustfmt, clippy
45+
toolchain: ${{matrix.rust}}
46+
- name: Release build
47+
run: cargo build --release --all-features
48+
49+
test:
50+
name: Test
51+
runs-on: ubuntu-latest
52+
timeout-minutes: 30
53+
steps:
54+
- uses: actions/checkout@v4
55+
- uses: actions/setup-python@v3
56+
- uses: pre-commit/action@v3.0.0
57+
58+
- name: Install stable toolchain
59+
uses: dtolnay/rust-toolchain@stable
60+
with:
61+
toolchain: stable
62+
63+
- uses: taiki-e/install-action@v2
64+
with:
65+
tool: cargo-tarpaulin,cargo-rdme
66+
67+
- name: Check README is up-to-date
68+
run: cargo rdme --check
69+
70+
- name: Run tests with coverage
71+
run: RUST_BACKTRACE=1 cargo tarpaulin --out Xml --all-features
72+
73+
- name: Submit a transaction to chain
74+
run: RUST_BACKTRACE=1 cargo run --example transaction_broadcast --all-features
75+
env:
76+
TEST_TO_ADDRESS: ${{ secrets.TO_ADDRESS }}
77+
TEST_MNEMONIC: ${{ secrets.MNEMONIC }}
78+
79+
- name: Upload coverage reports to Codecov
80+
uses: codecov/codecov-action@v3
81+
env:
82+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
83+
- name: Archive code coverage results
84+
uses: actions/upload-artifact@v4
85+
with:
86+
name: code-coverage-report
87+
path: cobertura.xml

.github/workflows/test.yml

Lines changed: 0 additions & 42 deletions
This file was deleted.

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ guidelines for this repo. Here's what you need to know!
88
**thor-devkit.rs** is proudly licenced under the GNU General Public License v3, and so are all
99
contributions. Please see the [`LICENSE`] file in this directory for more details.
1010

11-
[`LICENSE`]: https://github.com/sterliakov/thor-devkit.rs/blob/main/LICENSE
11+
[`LICENSE`]: https://github.com/sterliakov/thor-devkit.rs/blob/master/LICENSE
1212

1313
## Pull Requests
1414

1515
To make changes to **thor-devkit.rs**, please send in pull requests on GitHub to
16-
the `main` branch. I'll review them and either merge or request changes. GitHub Actions
16+
the `master` branch. I'll review them and either merge or request changes. GitHub Actions
1717
tests everything as well, so you may get feedback from it too.
1818

1919
If you make additions or other changes to a pull request, feel free to either amend

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "thor-devkit"
3-
version = "0.1.0-alpha.1"
3+
version = "0.1.0-beta.1"
44
authors = ["Stanislav Terliakov <terlya.stas@gmail.com>"]
55
description = "Rust library to aid coding with VeChain, eg. Wallets/Tx/Sign/Verify."
66
documentation = "https://docs.rs/thor-devkit.rs"
@@ -10,6 +10,7 @@ keywords = ["vechain", "crypto", "blockchain", "cryptography"]
1010
categories = ["cryptography"]
1111
license = "GPL-3.0"
1212
edition = "2021"
13+
rust-version = "1.67.1"
1314

1415
# See here for more info: https://blog.rust-lang.org/2020/03/15/docs-rs-opt-into-fewer-targets.html
1516

@@ -40,8 +41,10 @@ serde_with = { version = "^3.4", features = ["hex"], optional = true }
4041
version-sync = "0.9.4"
4142
rand = { version = "0.8.5", features = ["getrandom"] }
4243
tokio = { version = "1", features = ["full"] }
44+
bloomfilter = "^1.0"
45+
ethabi = "^18.0"
4346

4447
[features]
45-
default = ['http']
48+
# default = ['http']
4649
serde = ["dep:serde", "dep:serde_json", "dep:serde_with"]
4750
http = ["dep:reqwest", "serde"]

data/eip20.abi

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
[
2+
{
3+
"constant": false,
4+
"inputs": [
5+
{
6+
"name": "_spender",
7+
"type": "address"
8+
},
9+
{
10+
"name": "_value",
11+
"type": "uint256"
12+
}
13+
],
14+
"name": "approve",
15+
"outputs": [
16+
{
17+
"name": "success",
18+
"type": "bool"
19+
}
20+
],
21+
"type": "function"
22+
},
23+
{
24+
"constant": true,
25+
"inputs": [],
26+
"name": "totalSupply",
27+
"outputs": [
28+
{
29+
"name": "total",
30+
"type": "uint256"
31+
}
32+
],
33+
"type": "function"
34+
},
35+
{
36+
"constant": false,
37+
"inputs": [
38+
{
39+
"name": "_from",
40+
"type": "address"
41+
},
42+
{
43+
"name": "_to",
44+
"type": "address"
45+
},
46+
{
47+
"name": "_value",
48+
"type": "uint256"
49+
}
50+
],
51+
"name": "transferFrom",
52+
"outputs": [
53+
{
54+
"name": "success",
55+
"type": "bool"
56+
}
57+
],
58+
"type": "function"
59+
},
60+
{
61+
"constant": true,
62+
"inputs": [
63+
{
64+
"name": "_owner",
65+
"type": "address"
66+
}
67+
],
68+
"name": "balanceOf",
69+
"outputs": [
70+
{
71+
"name": "balance",
72+
"type": "uint256"
73+
}
74+
],
75+
"type": "function"
76+
},
77+
{
78+
"constant": false,
79+
"inputs": [
80+
{
81+
"name": "_to",
82+
"type": "address"
83+
},
84+
{
85+
"name": "_value",
86+
"type": "uint256"
87+
}
88+
],
89+
"name": "transfer",
90+
"outputs": [
91+
{
92+
"name": "success",
93+
"type": "bool"
94+
}
95+
],
96+
"type": "function"
97+
},
98+
{
99+
"constant": true,
100+
"inputs": [
101+
{
102+
"name": "_owner",
103+
"type": "address"
104+
},
105+
{
106+
"name": "_spender",
107+
"type": "address"
108+
}
109+
],
110+
"name": "allowance",
111+
"outputs": [
112+
{
113+
"name": "remaining",
114+
"type": "uint256"
115+
}
116+
],
117+
"type": "function"
118+
},
119+
{
120+
"anonymous": false,
121+
"inputs": [
122+
{
123+
"indexed": true,
124+
"name": "from",
125+
"type": "address"
126+
},
127+
{
128+
"indexed": true,
129+
"name": "to",
130+
"type": "address"
131+
},
132+
{
133+
"indexed": false,
134+
"name": "value",
135+
"type": "uint256"
136+
}
137+
],
138+
"name": "Transfer",
139+
"type": "event"
140+
},
141+
{
142+
"anonymous": false,
143+
"inputs": [
144+
{
145+
"indexed": true,
146+
"name": "owner",
147+
"type": "address"
148+
},
149+
{
150+
"indexed": true,
151+
"name": "spender",
152+
"type": "address"
153+
},
154+
{
155+
"indexed": false,
156+
"name": "value",
157+
"type": "uint256"
158+
}
159+
],
160+
"name": "Approval",
161+
"type": "event"
162+
}
163+
]

examples/abi.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! thor-devkit does not vendor ABI parsing solution, because a good one
2+
//! already exists in wild ([`ethabi`](https://docs.rs/ethabi/latest/ethabi/index.html)).
3+
//!
4+
//! To decode or encode data given a contract ABI and some input data,
5+
//! you can create a contract from JSON ABI specification and process it
6+
//! as necessary.
7+
//!
8+
//! Future version of thor-devkit will likely depend on [`ethabi`] to improve
9+
//! interoperability and enable more smooth experience.
10+
11+
use ethabi::{Contract, Token};
12+
use std::fs::File;
13+
use thor_devkit::rlp::Bytes;
14+
15+
fn demo_abi() {
16+
let eip20 = Contract::load(File::open("data/eip20.abi").expect("Must exist"))
17+
.expect("Should be loadable");
18+
let owner = [0u8; 20];
19+
let spender = [1u8; 20];
20+
let inputs = vec![Token::Address(owner.into()), Token::Address(spender.into())];
21+
let allowance = eip20.function("allowance").expect("Exists");
22+
println!("Function signature: {}", allowance.signature());
23+
println!(
24+
"Function short signature: {:02x?}",
25+
allowance.short_signature()
26+
);
27+
let encoded = allowance
28+
.encode_input(&inputs)
29+
.expect("Should be encodable");
30+
println!(
31+
"Encoded data for allowance call: {:x}",
32+
Bytes::copy_from_slice(&encoded[..])
33+
);
34+
// To decode, we strip a function signature first:
35+
let decoded = allowance.decode_input(&encoded[4..]).expect("Should parse");
36+
assert_eq!(decoded, inputs);
37+
38+
let decoded_out = allowance.decode_output(&[0x01; 32]).expect("Should parse");
39+
assert_eq!(decoded_out, vec![Token::Uint([0x01; 32].into())])
40+
}
41+
42+
#[test]
43+
fn test_run() {
44+
demo_abi();
45+
}
46+
47+
fn main() {
48+
demo_abi();
49+
}

0 commit comments

Comments
 (0)