Skip to content

Commit

Permalink
Merge pull request #16 from salmanferrum/develop
Browse files Browse the repository at this point in the history
Feedback Updates
  • Loading branch information
zikriya authored Oct 13, 2023
2 parents 4beb13d + 00a2f76 commit 68932a4
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 34 deletions.
100 changes: 94 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ This contract has the following functionality:
## Table of Contents

1. [Getting Started](#getting-started)
2. [Prerequisites](#prerequisites)
3. [Cargo Commands](#cargo-commands)

2. [Usage](#usage)
4. [Usage](#usage)

3. [Installing and Interacting with the Contract using the Rust Casper Client](#installing-and-interacting-with-the-contract-using-the-rust-casper-client)
5. [Installing and Interacting with the Contract using the Rust Casper Client](#installing-and-interacting-with-the-contract-using-the-rust-casper-client)

4. [Events](#events)
6. [Events](#events)

5. [Error Codes](#error-codes)
7. [Error Codes](#error-codes)

6. [Contributing](#contributing)
8. [Contributing](#contributing)

## Getting Started

Expand All @@ -33,6 +35,93 @@ To get started with using the smart contracts in this repository, you will need
```bash
cargo install casper-client
```
#### Notes:
casper-client package cannot be installed inside the project root directory as a specific rustc version would be required.

## Prerequisites
You need to have a x86_64 CPU to build the code, as Casper dependencies for now don't support ARM (e.g. M1) architecture CPU's. It is recommended to use Linux, Debian-based distributions (e.g. Ubuntu 22.04).

First, you need to install Rust:
```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
To check for successful installation, one needs to run this command:
```bash
rustup --version
```

Secondly, you need to install CMake:
```bash
sudo apt-get -y install cmake
```
To check for successful installation, one needs to run this command:
```bash
cmake --version
```
To install casper-client, to interact with the contract using the CLI you must install these dependencies:
```bash
brew install pkg-config
brew install openssl
brew install libssl-dev
```
Since, Clang is also required for linux. Check if you have the required clang compiler already installed:
```bash
clang --version
```
To install clang on your linux distro:
```bash
sudo apt install clang
```

## Cargo Commands
```bash
prepare:
rustup target add wasm32-unknown-unknown
```
```bash
build-contract:
cd contract && cargo build --release --target wasm32-unknown-unknown
cd counter-call && cargo build --release --target wasm32-unknown-unknown
cd erc20/erc20-token && cargo build --release --target wasm32-unknown-unknown

wasm-strip contract/target/wasm32-unknown-unknown/release/bridge_pool.wasm 2>/dev/null | true
wasm-strip counter-call/target/wasm32-unknown-unknown/release/counter-call.wasm 2>/dev/null | true
```
```bash
test-only:
cd tests && cargo test
```
```bash
test: build-contract
mkdir -p tests/wasm
cp contract/target/wasm32-unknown-unknown/release/bridge_pool.wasm tests/wasm
cp counter-call/target/wasm32-unknown-unknown/release/counter-call.wasm tests/wasm
cp erc20/target/wasm32-unknown-unknown/release/erc20_token.wasm tests/wasm/erc20.wasm
cd tests && cargo test
```
```bash
clippy:
cd contract && cargo clippy --all-targets -- -D warnings
```
```bash
check-lint: clippy
cd contract && cargo fmt -- --check
cd counter-call && cargo fmt -- --check
cd tests && cargo fmt -- --check
```
```bash
lint: clippy
cd contract && cargo fmt
cd counter-call && cargo fmt
cd tests && cargo fmt
```
```bash
clean:
cd contract && cargo clean
cd counter-call && cargo clean
cd tests && cargo clean
rm -rf tests/wasm
```

## Usage

Expand All @@ -53,7 +142,6 @@ make build-contract
```bash
make test
```

### Installing and Interacting with the Contract using the Rust Casper Client


Expand Down
50 changes: 26 additions & 24 deletions tests/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,37 @@ mod tests {

use casper_engine_test_support::{
ExecuteRequestBuilder, InMemoryWasmTestBuilder, WasmTestBuilder, DEFAULT_ACCOUNT_ADDR,
DEFAULT_RUN_GENESIS_REQUEST, PRODUCTION_RUN_GENESIS_REQUEST,
PRODUCTION_RUN_GENESIS_REQUEST,
};

use casper_types::{runtime_args, ContractHash, RuntimeArgs};

use std::collections::BTreeMap;
// use std::collections::BTreeMap;

use blake2::{
digest::{Update, VariableOutput},
VarBlake2b,
};
use casper_execution_engine::storage::global_state::in_memory::InMemoryGlobalState;
use casper_types::{
account::AccountHash,
bytesrepr::{FromBytes, ToBytes},
CLTyped, ContractPackageHash, Key, URef, BLAKE2B_DIGEST_LENGTH, U256,
ContractPackageHash, Key, BLAKE2B_DIGEST_LENGTH, U256, bytesrepr::{ToBytes}
};
use test_env::TestEnv;
// use casper_types::{
// URef, CLTyped, account::AccountHash
// };
// use test_env::TestEnv;

const ERC20_WASM: &str = "erc20.wasm";
const BRIDGE_POOL_WASM: &str = "bridge_pool.wasm"; // The main example contract
const COUNTER_CALL_WASM: &str = "counter-call.wasm"; // The session code that calls the contract
// const COUNTER_CALL_WASM: &str = "counter-call.wasm"; // The session code that calls the contract
const ERC20_CONTRACT_NAME: &str = "erc20_token_contract";
const ERC20_CONTRACT_PACKAGE_HASH: &str = "erc20-contract_package_hash";
const BRIDGE_POOL_CONTRACT_HASH: &str = "bridge_pool_contract_hash";
const BRIDGE_POOL_CONTRACT_PACKAGE_HASH: &str = "bridge_pool_package_name";

const CONTRACT_KEY: &str = "bridge_pool"; // Named key referencing this contract
const LIQUIDITY_KEY: &str = "liquidity"; // Named key referencing the count value
const CONTRACT_VERSION_KEY: &str = "version"; // Automatically incremented version in a contract package
// const CONTRACT_KEY: &str = "bridge_pool"; // Named key referencing this contract
// const LIQUIDITY_KEY: &str = "liquidity"; // Named key referencing the count value
// const CONTRACT_VERSION_KEY: &str = "version"; // Automatically incremented version in a contract package
const ALLOWANCES_SEED_UREF: &str = "allowances";

#[test]
Expand Down Expand Up @@ -550,11 +552,11 @@ mod tests {

assert_eq!(actual_allowance, U256::from(10i64));

let erc20_contract_package_hash = get_erc20_contract_package_hash(&builder);
// let erc20_contract_package_hash = get_erc20_contract_package_hash(&builder);

let erc20_contract_package_hash_string = erc20_contract_package_hash.to_formatted_string();
let bridge_pool_contract_package_hash_string =
bridge_pool_contract_package_hash.to_formatted_string();
// let erc20_contract_package_hash_string = erc20_contract_package_hash.to_formatted_string();
// let bridge_pool_contract_package_hash_string =
// bridge_pool_contract_package_hash.to_formatted_string();
let add_signer_args = runtime_args! {
"signer" => "cde782dee9643b02dde8a11499ede81ec1d05dd3".to_string() ,
};
Expand Down Expand Up @@ -660,11 +662,11 @@ mod tests {

assert_eq!(actual_allowance, U256::from(10i64));

let erc20_contract_package_hash = get_erc20_contract_package_hash(&builder);
// let erc20_contract_package_hash = get_erc20_contract_package_hash(&builder);

let erc20_contract_package_hash_string = erc20_contract_package_hash.to_formatted_string();
let bridge_pool_contract_package_hash_string =
bridge_pool_contract_package_hash.to_formatted_string();
// let erc20_contract_package_hash_string = erc20_contract_package_hash.to_formatted_string();
// let bridge_pool_contract_package_hash_string =
// bridge_pool_contract_package_hash.to_formatted_string();
let add_signer_args = runtime_args! {
"signer" => "cde782dee9643b02dde8a11499ede81ec1d05dd3".to_string() ,
};
Expand Down Expand Up @@ -938,7 +940,7 @@ mod tests {
#[test]
fn should_be_able_to_install_and_allow_target() {
let mut builder = InMemoryWasmTestBuilder::default();
builder.run_genesis(&DEFAULT_RUN_GENESIS_REQUEST).commit();
builder.run_genesis(&PRODUCTION_RUN_GENESIS_REQUEST).commit();

let erc20_runtime_args = runtime_args! {
"name" => "FERRUM_ERC20".to_string(),
Expand Down Expand Up @@ -1023,11 +1025,11 @@ mod tests {

assert_eq!(actual_allowance, U256::from(10i64));

let erc20_contract_package_hash = get_erc20_contract_package_hash(&builder);
// let erc20_contract_package_hash = get_erc20_contract_package_hash(&builder);

let erc20_contract_package_hash_string = erc20_contract_package_hash.to_formatted_string();
let bridge_pool_contract_package_hash_string =
bridge_pool_contract_package_hash.to_formatted_string();
// let erc20_contract_package_hash_string = erc20_contract_package_hash.to_formatted_string();
// let bridge_pool_contract_package_hash_string =
// bridge_pool_contract_package_hash.to_formatted_string();
let allow_target_args = runtime_args! {
"token_address" => "contract-package-wasme222974816f70ca96fc4002a696bb552e2959d3463158cd82a7bfc8a94c03473".to_string() ,
"token_name" => "some_unusual_token_name".to_string() ,
Expand Down Expand Up @@ -1369,7 +1371,7 @@ fn main() {

pub fn signer_unique(message_hash: String, signature: Vec<u8>) -> Vec<u8> {
let signature_rec = if signature.len() == 65 {
let mut signature_vec: Vec<u8> = signature;
let signature_vec: Vec<u8> = signature;
RecoverableSignature::from_bytes(&signature_vec[..]).unwrap()
} else {
panic!();
Expand Down
8 changes: 4 additions & 4 deletions utils/contract-utils/src/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ pub fn ecdsa_sign(hash: &[u8], private_key: &[u8]) -> [u8; 65] {
let s = Secp256k1::signing_only();
let msg = Message::from_slice(hash).unwrap();
let key = SecretKey::from_slice(private_key).unwrap();
let res = s.sign_ecdsa_recoverable(&msg, &key);
// let res = s.sign_ecdsa_recoverable(&msg, &key);
let (v, sig_bytes) = s.sign_ecdsa_recoverable(&msg, &key).serialize_compact();
let r = hex::encode(&sig_bytes[..32]);
let s = hex::encode(&sig_bytes[32..64]);
// let r = hex::encode(&sig_bytes[..32]);
// let s = hex::encode(&sig_bytes[32..64]);

let mut vec = sig_bytes.to_vec();
vec.push(v.to_i32() as u8);

let slice = vec.as_slice();
// let slice = vec.as_slice();

let mut vec1 = sig_bytes[..32].to_vec();
let mut vec2 = sig_bytes[32..].to_vec();
Expand Down

0 comments on commit 68932a4

Please sign in to comment.