Skip to content

Commit

Permalink
✨ Add Rust CI, rustfmt.toml, minor fixes and formatting (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
cairoeth authored Jan 12, 2024
2 parents 62588ed + bcae8da commit 657068d
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 95 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: rust

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose --all-features
3 changes: 3 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
force_multiline_blocks=true
imports_indent="Block"
imports_layout="Vertical"
26 changes: 20 additions & 6 deletions src/auth/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@
//!
//! Note that this code is unaudited and not fit for production use.

use alloy_primitives::{Address, FixedBytes};
use alloy_sol_types::{sol, SolError};
use core::{borrow::BorrowMut, marker::PhantomData};
use stylus_sdk::{contract, evm, msg, prelude::*};
use alloy_primitives::{
Address,
FixedBytes,
};
use alloy_sol_types::{
sol,
SolError,
};
use core::{
borrow::BorrowMut,
marker::PhantomData,
};
use stylus_sdk::{
contract,
evm,
msg,
prelude::*,
};

pub trait AuthParams {}

Expand Down Expand Up @@ -71,7 +85,7 @@ impl<T: AuthParams> Auth<T> {
let authority_given = Authority::new(authority);
let status = authority_given.can_call(&mut *storage, user, target, sig)?;

return Ok(status);
Ok(status)
}

fn is_authorized<S: TopLevelStorage + BorrowMut<Self>>(
Expand All @@ -90,7 +104,7 @@ impl<T: AuthParams> Auth<T> {
#[external]
impl<T: AuthParams> Auth<T> {
pub fn owner(&self) -> Result<Address> {
Ok(Address::from(self.owner.get()))
Ok(self.owner.get())
}

pub fn authority(&self) -> Result<Authority> {
Expand Down
3 changes: 2 additions & 1 deletion src/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#[allow(clippy::module_inception)]
pub mod auth;
pub mod owned;
pub mod owned;
11 changes: 9 additions & 2 deletions src/auth/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

use alloc::vec::Vec;
use alloy_primitives::Address;
use alloy_sol_types::{sol, SolError};
use alloy_sol_types::{
sol,
SolError,
};
use core::marker::PhantomData;
use stylus_sdk::{evm, msg, prelude::*};
use stylus_sdk::{
evm,
msg,
prelude::*,
};

pub trait OwnedParams {}

Expand Down
75 changes: 44 additions & 31 deletions src/tokens/erc1155.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,28 @@
//!
//! Note that this code is unaudited and not fit for production use.

use alloc::{string::String, vec::Vec};
use alloy_primitives::{Address, U256};
use alloy_sol_types::{sol, SolError};
use core::{borrow::BorrowMut, marker::PhantomData};
use stylus_sdk::{abi::Bytes, evm, msg, prelude::*};
use alloc::{
string::String,
vec::Vec,
};
use alloy_primitives::{
Address,
U256,
};
use alloy_sol_types::{
sol,
SolError,
};
use core::{
borrow::BorrowMut,
marker::PhantomData,
};
use stylus_sdk::{
abi::Bytes,
evm,
msg,
prelude::*,
};

pub trait ERC1155Params {
fn uri(id: U256) -> String;
Expand Down Expand Up @@ -100,10 +117,8 @@ impl<T: ERC1155Params> ERC1155<T> {
if u32::from_be_bytes(received) != 0xf23a6e61 {
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
}
} else {
if to == Address::ZERO {
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
}
} else if to == Address::ZERO {
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
}

Ok(())
Expand All @@ -127,10 +142,8 @@ impl<T: ERC1155Params> ERC1155<T> {
if u32::from_be_bytes(received) != 0xbc197c81 {
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
}
} else {
if to == Address::ZERO {
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
}
} else if to == Address::ZERO {
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
}

Ok(())
Expand All @@ -151,9 +164,9 @@ impl<T: ERC1155Params> ERC1155<T> {
evm::log(TransferSingle {
operator: msg::sender(),
from: Address::ZERO,
to: to,
id: id,
amount: amount,
to,
id,
amount,
});

Self::call_receiver(storage, id, Address::ZERO, to, amount, data.0)?;
Expand Down Expand Up @@ -184,7 +197,7 @@ impl<T: ERC1155Params> ERC1155<T> {
evm::log(TransferBatch {
operator: msg::sender(),
from: Address::ZERO,
to: to,
to,
ids: ids.clone(),
amounts: amounts.clone(),
});
Expand All @@ -207,10 +220,10 @@ impl<T: ERC1155Params> ERC1155<T> {

evm::log(TransferBatch {
operator: msg::sender(),
from: from,
from,
to: Address::ZERO,
ids: ids,
amounts: amounts,
ids,
amounts,
});

Ok(())
Expand All @@ -223,10 +236,10 @@ impl<T: ERC1155Params> ERC1155<T> {

evm::log(TransferSingle {
operator: msg::sender(),
from: from,
from,
to: Address::ZERO,
id: id,
amount: amount,
id,
amount,
});

Ok(())
Expand Down Expand Up @@ -255,8 +268,8 @@ impl<T: ERC1155Params> ERC1155<T> {

evm::log(ApprovalForAll {
owner: msg::sender(),
operator: operator,
approved: approved,
operator,
approved,
});

Ok(())
Expand Down Expand Up @@ -290,10 +303,10 @@ impl<T: ERC1155Params> ERC1155<T> {

evm::log(TransferSingle {
operator: msg::sender(),
from: from,
to: to,
id: id,
amount: amount,
from,
to,
id,
amount,
});

Self::call_receiver(storage, id, from, to, amount, data.0)
Expand Down Expand Up @@ -336,8 +349,8 @@ impl<T: ERC1155Params> ERC1155<T> {

evm::log(TransferBatch {
operator: msg::sender(),
from: from,
to: to,
from,
to,
ids: ids.clone(),
amounts: amounts.clone(),
});
Expand Down
41 changes: 30 additions & 11 deletions src/tokens/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,32 @@
//!
//! Note that this code is unaudited and not fit for production use.

use alloc::{string::String, vec::Vec};
use alloy_primitives::{address, Address, B256, U256};
use alloy_sol_types::{sol, sol_data, SolError, SolType};
use alloc::{
string::String,
vec::Vec,
};
use alloy_primitives::{
address,
Address,
B256,
U256,
};
use alloy_sol_types::{
sol,
sol_data,
SolError,
SolType,
};
use core::marker::PhantomData;
use stylus_sdk::call::RawCall;
use stylus_sdk::crypto;
use stylus_sdk::{block, contract, evm, msg, prelude::*};
use stylus_sdk::{
block,
contract,
evm,
msg,
prelude::*,
};

pub trait ERC20Params {
const NAME: &'static str;
Expand Down Expand Up @@ -70,7 +89,7 @@ type Result<T, E = ERC20Error> = core::result::Result<T, E>;

impl<T: ERC20Params> ERC20<T> {
pub fn compute_domain_separator() -> Result<B256> {
let mut digest_input = [0u8; 32 + 32];
let mut digest_input = [0u8; 160];
digest_input[0..32].copy_from_slice(&crypto::keccak("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)".as_bytes())[..]);
digest_input[32..64].copy_from_slice(&crypto::keccak(T::NAME.as_bytes())[..]);
digest_input[64..96].copy_from_slice(&crypto::keccak("1".as_bytes())[..]);
Expand All @@ -89,8 +108,8 @@ impl<T: ERC20Params> ERC20<T> {

evm::log(Transfer {
from: Address::ZERO,
to: to,
amount: amount,
to,
amount,
});
}

Expand All @@ -102,9 +121,9 @@ impl<T: ERC20Params> ERC20<T> {
self.total_supply.set(self.total_supply.get() - amount);

evm::log(Transfer {
from: from,
from,
to: Address::ZERO,
amount: amount,
amount,
});
}
}
Expand Down Expand Up @@ -209,7 +228,7 @@ impl<T: ERC20Params> ERC20<T> {
let nonce = nonce_setter.get();
nonce_setter.set(nonce + U256::from(1));

let mut struct_hash = [0u8; 32 + 32];
let mut struct_hash = [0u8; 192];
struct_hash[0..32].copy_from_slice(&crypto::keccak(b"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")[..]);
struct_hash[32..64].copy_from_slice(&owner[..]);
struct_hash[64..96].copy_from_slice(&spender[..]);
Expand Down Expand Up @@ -256,7 +275,7 @@ impl<T: ERC20Params> ERC20<T> {

pub fn domain_separator(&mut self) -> Result<B256> {
if block::chainid() == T::INITIAL_CHAIN_ID {
Ok(T::INITIAL_DOMAIN_SEPARATOR.into())
Ok(T::INITIAL_DOMAIN_SEPARATOR)
} else {
Ok(ERC20::<T>::compute_domain_separator()?)
}
Expand Down
Loading

0 comments on commit 657068d

Please sign in to comment.