Skip to content

Commit 657068d

Browse files
authored
✨ Add Rust CI, rustfmt.toml, minor fixes and formatting (#1)
2 parents 62588ed + bcae8da commit 657068d

11 files changed

Lines changed: 204 additions & 95 deletions

File tree

.github/workflows/rust.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: rust
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Build
19+
run: cargo build --verbose
20+
- name: Run tests
21+
run: cargo test --verbose --all-features

rustfmt.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
force_multiline_blocks=true
2+
imports_indent="Block"
3+
imports_layout="Vertical"

src/auth/auth.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55
//!
66
//! Note that this code is unaudited and not fit for production use.
77
8-
use alloy_primitives::{Address, FixedBytes};
9-
use alloy_sol_types::{sol, SolError};
10-
use core::{borrow::BorrowMut, marker::PhantomData};
11-
use stylus_sdk::{contract, evm, msg, prelude::*};
8+
use alloy_primitives::{
9+
Address,
10+
FixedBytes,
11+
};
12+
use alloy_sol_types::{
13+
sol,
14+
SolError,
15+
};
16+
use core::{
17+
borrow::BorrowMut,
18+
marker::PhantomData,
19+
};
20+
use stylus_sdk::{
21+
contract,
22+
evm,
23+
msg,
24+
prelude::*,
25+
};
1226

1327
pub trait AuthParams {}
1428

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

74-
return Ok(status);
88+
Ok(status)
7589
}
7690

7791
fn is_authorized<S: TopLevelStorage + BorrowMut<Self>>(
@@ -90,7 +104,7 @@ impl<T: AuthParams> Auth<T> {
90104
#[external]
91105
impl<T: AuthParams> Auth<T> {
92106
pub fn owner(&self) -> Result<Address> {
93-
Ok(Address::from(self.owner.get()))
107+
Ok(self.owner.get())
94108
}
95109

96110
pub fn authority(&self) -> Result<Authority> {

src/auth/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
#[allow(clippy::module_inception)]
12
pub mod auth;
2-
pub mod owned;
3+
pub mod owned;

src/auth/owned.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@
77
88
use alloc::vec::Vec;
99
use alloy_primitives::Address;
10-
use alloy_sol_types::{sol, SolError};
10+
use alloy_sol_types::{
11+
sol,
12+
SolError,
13+
};
1114
use core::marker::PhantomData;
12-
use stylus_sdk::{evm, msg, prelude::*};
15+
use stylus_sdk::{
16+
evm,
17+
msg,
18+
prelude::*,
19+
};
1320

1421
pub trait OwnedParams {}
1522

src/tokens/erc1155.rs

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,28 @@
88
//!
99
//! Note that this code is unaudited and not fit for production use.
1010
11-
use alloc::{string::String, vec::Vec};
12-
use alloy_primitives::{Address, U256};
13-
use alloy_sol_types::{sol, SolError};
14-
use core::{borrow::BorrowMut, marker::PhantomData};
15-
use stylus_sdk::{abi::Bytes, evm, msg, prelude::*};
11+
use alloc::{
12+
string::String,
13+
vec::Vec,
14+
};
15+
use alloy_primitives::{
16+
Address,
17+
U256,
18+
};
19+
use alloy_sol_types::{
20+
sol,
21+
SolError,
22+
};
23+
use core::{
24+
borrow::BorrowMut,
25+
marker::PhantomData,
26+
};
27+
use stylus_sdk::{
28+
abi::Bytes,
29+
evm,
30+
msg,
31+
prelude::*,
32+
};
1633

1734
pub trait ERC1155Params {
1835
fn uri(id: U256) -> String;
@@ -100,10 +117,8 @@ impl<T: ERC1155Params> ERC1155<T> {
100117
if u32::from_be_bytes(received) != 0xf23a6e61 {
101118
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
102119
}
103-
} else {
104-
if to == Address::ZERO {
105-
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
106-
}
120+
} else if to == Address::ZERO {
121+
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
107122
}
108123

109124
Ok(())
@@ -127,10 +142,8 @@ impl<T: ERC1155Params> ERC1155<T> {
127142
if u32::from_be_bytes(received) != 0xbc197c81 {
128143
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
129144
}
130-
} else {
131-
if to == Address::ZERO {
132-
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
133-
}
145+
} else if to == Address::ZERO {
146+
return Err(ERC1155Error::UnsafeRecipient(UnsafeRecipient {}));
134147
}
135148

136149
Ok(())
@@ -151,9 +164,9 @@ impl<T: ERC1155Params> ERC1155<T> {
151164
evm::log(TransferSingle {
152165
operator: msg::sender(),
153166
from: Address::ZERO,
154-
to: to,
155-
id: id,
156-
amount: amount,
167+
to,
168+
id,
169+
amount,
157170
});
158171

159172
Self::call_receiver(storage, id, Address::ZERO, to, amount, data.0)?;
@@ -184,7 +197,7 @@ impl<T: ERC1155Params> ERC1155<T> {
184197
evm::log(TransferBatch {
185198
operator: msg::sender(),
186199
from: Address::ZERO,
187-
to: to,
200+
to,
188201
ids: ids.clone(),
189202
amounts: amounts.clone(),
190203
});
@@ -207,10 +220,10 @@ impl<T: ERC1155Params> ERC1155<T> {
207220

208221
evm::log(TransferBatch {
209222
operator: msg::sender(),
210-
from: from,
223+
from,
211224
to: Address::ZERO,
212-
ids: ids,
213-
amounts: amounts,
225+
ids,
226+
amounts,
214227
});
215228

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

224237
evm::log(TransferSingle {
225238
operator: msg::sender(),
226-
from: from,
239+
from,
227240
to: Address::ZERO,
228-
id: id,
229-
amount: amount,
241+
id,
242+
amount,
230243
});
231244

232245
Ok(())
@@ -255,8 +268,8 @@ impl<T: ERC1155Params> ERC1155<T> {
255268

256269
evm::log(ApprovalForAll {
257270
owner: msg::sender(),
258-
operator: operator,
259-
approved: approved,
271+
operator,
272+
approved,
260273
});
261274

262275
Ok(())
@@ -290,10 +303,10 @@ impl<T: ERC1155Params> ERC1155<T> {
290303

291304
evm::log(TransferSingle {
292305
operator: msg::sender(),
293-
from: from,
294-
to: to,
295-
id: id,
296-
amount: amount,
306+
from,
307+
to,
308+
id,
309+
amount,
297310
});
298311

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

337350
evm::log(TransferBatch {
338351
operator: msg::sender(),
339-
from: from,
340-
to: to,
352+
from,
353+
to,
341354
ids: ids.clone(),
342355
amounts: amounts.clone(),
343356
});

src/tokens/erc20.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,32 @@
88
//!
99
//! Note that this code is unaudited and not fit for production use.
1010
11-
use alloc::{string::String, vec::Vec};
12-
use alloy_primitives::{address, Address, B256, U256};
13-
use alloy_sol_types::{sol, sol_data, SolError, SolType};
11+
use alloc::{
12+
string::String,
13+
vec::Vec,
14+
};
15+
use alloy_primitives::{
16+
address,
17+
Address,
18+
B256,
19+
U256,
20+
};
21+
use alloy_sol_types::{
22+
sol,
23+
sol_data,
24+
SolError,
25+
SolType,
26+
};
1427
use core::marker::PhantomData;
1528
use stylus_sdk::call::RawCall;
1629
use stylus_sdk::crypto;
17-
use stylus_sdk::{block, contract, evm, msg, prelude::*};
30+
use stylus_sdk::{
31+
block,
32+
contract,
33+
evm,
34+
msg,
35+
prelude::*,
36+
};
1837

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

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

90109
evm::log(Transfer {
91110
from: Address::ZERO,
92-
to: to,
93-
amount: amount,
111+
to,
112+
amount,
94113
});
95114
}
96115

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

104123
evm::log(Transfer {
105-
from: from,
124+
from,
106125
to: Address::ZERO,
107-
amount: amount,
126+
amount,
108127
});
109128
}
110129
}
@@ -209,7 +228,7 @@ impl<T: ERC20Params> ERC20<T> {
209228
let nonce = nonce_setter.get();
210229
nonce_setter.set(nonce + U256::from(1));
211230

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

257276
pub fn domain_separator(&mut self) -> Result<B256> {
258277
if block::chainid() == T::INITIAL_CHAIN_ID {
259-
Ok(T::INITIAL_DOMAIN_SEPARATOR.into())
278+
Ok(T::INITIAL_DOMAIN_SEPARATOR)
260279
} else {
261280
Ok(ERC20::<T>::compute_domain_separator()?)
262281
}

0 commit comments

Comments
 (0)