Skip to content

Commit

Permalink
Add several examples. Prepare 0.1.0-beta.1 (#17)
Browse files Browse the repository at this point in the history
* Add first examples, use U256 for references.
* Add more examples
* Add MSRV
* Release 0.1.0-beta.1
  • Loading branch information
sterliakov authored Dec 23, 2023
1 parent 3a8e8fa commit 4e24cc0
Show file tree
Hide file tree
Showing 15 changed files with 595 additions and 214 deletions.
70 changes: 66 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,82 @@ on:
pull_request:
branches:
- master
schedule:
- cron: "0 0 * * *"

jobs:
build:
name: Build
clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Stable with rustfmt and clippy
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Release build
run: cargo build --release --all-features
- uses: giraffate/clippy-action@v1
with:
reporter: 'github-pr-review'
github_token: ${{ secrets.GITHUB_TOKEN }}

build:
name: Build with rust ${{matrix.rust}} on ${{matrix.os == 'ubuntu' && 'Linux' || matrix.os == 'macos' && 'macOS' || matrix.os == 'windows' && 'Windows' || '???'}}
runs-on: ${{matrix.os}}-latest
strategy:
fail-fast: false
matrix:
rust:
- "1.67.1" # MSRV
- "stable"
- "beta"
- "nightly"
os: [ubuntu, windows, macos]
steps:
- uses: actions/checkout@v4
- name: Stable with rustfmt and clippy
uses: dtolnay/rust-toolchain@master
with:
components: rustfmt, clippy
toolchain: ${{matrix.rust}}
- name: Release build
run: cargo build --release --all-features

test:
name: Test
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v3
- uses: pre-commit/[email protected]

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable

- uses: taiki-e/install-action@v2
with:
tool: cargo-tarpaulin,cargo-rdme

- name: Check README is up-to-date
run: cargo rdme --check

- name: Run tests with coverage
run: RUST_BACKTRACE=1 cargo tarpaulin --out Xml --all-features

- name: Submit a transaction to chain
run: RUST_BACKTRACE=1 cargo run --example transaction_broadcast --all-features
env:
TEST_TO_ADDRESS: ${{ secrets.TO_ADDRESS }}
TEST_MNEMONIC: ${{ secrets.MNEMONIC }}

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Archive code coverage results
uses: actions/upload-artifact@v4
with:
name: code-coverage-report
path: cobertura.xml
42 changes: 0 additions & 42 deletions .github/workflows/test.yml

This file was deleted.

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

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

## Pull Requests

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

If you make additions or other changes to a pull request, feel free to either amend
Expand Down
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "thor-devkit"
version = "0.1.0-alpha.1"
version = "0.1.0-beta.1"
authors = ["Stanislav Terliakov <[email protected]>"]
description = "Rust library to aid coding with VeChain, eg. Wallets/Tx/Sign/Verify."
documentation = "https://docs.rs/thor-devkit.rs"
Expand All @@ -10,6 +10,7 @@ keywords = ["vechain", "crypto", "blockchain", "cryptography"]
categories = ["cryptography"]
license = "GPL-3.0"
edition = "2021"
rust-version = "1.67.1"

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

Expand Down Expand Up @@ -40,8 +41,10 @@ serde_with = { version = "^3.4", features = ["hex"], optional = true }
version-sync = "0.9.4"
rand = { version = "0.8.5", features = ["getrandom"] }
tokio = { version = "1", features = ["full"] }
bloomfilter = "^1.0"
ethabi = "^18.0"

[features]
default = ['http']
# default = ['http']
serde = ["dep:serde", "dep:serde_json", "dep:serde_with"]
http = ["dep:reqwest", "serde"]
163 changes: 163 additions & 0 deletions data/eip20.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
[
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "total",
"type": "uint256"
}
],
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "remaining",
"type": "uint256"
}
],
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
}
]
49 changes: 49 additions & 0 deletions examples/abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! thor-devkit does not vendor ABI parsing solution, because a good one
//! already exists in wild ([`ethabi`](https://docs.rs/ethabi/latest/ethabi/index.html)).
//!
//! To decode or encode data given a contract ABI and some input data,
//! you can create a contract from JSON ABI specification and process it
//! as necessary.
//!
//! Future version of thor-devkit will likely depend on [`ethabi`] to improve
//! interoperability and enable more smooth experience.
use ethabi::{Contract, Token};
use std::fs::File;
use thor_devkit::rlp::Bytes;

fn demo_abi() {
let eip20 = Contract::load(File::open("data/eip20.abi").expect("Must exist"))
.expect("Should be loadable");
let owner = [0u8; 20];
let spender = [1u8; 20];
let inputs = vec![Token::Address(owner.into()), Token::Address(spender.into())];
let allowance = eip20.function("allowance").expect("Exists");
println!("Function signature: {}", allowance.signature());
println!(
"Function short signature: {:02x?}",
allowance.short_signature()
);
let encoded = allowance
.encode_input(&inputs)
.expect("Should be encodable");
println!(
"Encoded data for allowance call: {:x}",
Bytes::copy_from_slice(&encoded[..])
);
// To decode, we strip a function signature first:
let decoded = allowance.decode_input(&encoded[4..]).expect("Should parse");
assert_eq!(decoded, inputs);

let decoded_out = allowance.decode_output(&[0x01; 32]).expect("Should parse");
assert_eq!(decoded_out, vec![Token::Uint([0x01; 32].into())])
}

#[test]
fn test_run() {
demo_abi();
}

fn main() {
demo_abi();
}
Loading

0 comments on commit 4e24cc0

Please sign in to comment.