Skip to content

Commit

Permalink
fix: update examples and add with_block_id to the Contract (#25)
Browse files Browse the repository at this point in the history
* fix: update examples and add with_block_id to the Contract

* ci: fix scarb version

* ci: fix scarb version in lint
  • Loading branch information
glihm authored Apr 24, 2024
1 parent a34db63 commit ef7e70c
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Compile contracts
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.4.0"
scarb-version: "2.5.4"
- run: |
cd ./contracts && make generate_artifacts
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Compile contracts
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.4.0"
scarb-version: "2.5.4"
- run: |
cd ./contracts && make generate_artifacts && scarb fmt --check
Expand Down
50 changes: 34 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/.tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.4.0
scarb 2.5.4
2 changes: 1 addition & 1 deletion contracts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ setup_simple_get_set:
@set -x; \
scarb build; \
class_hash=$$(starkli class-hash ${scarb_build}simple_get_set${sierra}); \
/mnt/g/sn/starkli/target/debug/starkli declare ${scarb_build}simple_get_set${sierra} ${config} --compiler-version 2.4.0; \
starkli declare ${scarb_build}simple_get_set${sierra} ${config}; \
sleep 2; \
starkli deploy "$${class_hash}" --salt 0x1234 ${config}

Expand Down
2 changes: 1 addition & 1 deletion contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "contracts"
version = "0.1.0"

[dependencies]
starknet = "2.4.0"
starknet = "2.5.4"

[[target.starknet-contract]]
sierra = true
Expand Down
132 changes: 0 additions & 132 deletions contracts/src/abicov/byte_array.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[starknet::contract]
mod byte_array {
use super::StoreByteArray;

#[storage]
struct Storage {
string: ByteArray,
Expand Down Expand Up @@ -71,133 +69,3 @@ impl LegacyHashByteArray of LegacyHash<ByteArray> {
poseidon::poseidon_hash_span(buffer.span())
}
}

/// PR in alexandria: https://github.com/keep-starknet-strange/alexandria/pull/229
///
/// Computes a storage address based on the given key.
///
/// # Arguments
///
/// * `base` - The base storage address (usually given by the name of the field in storage).
/// * `key` - The custom key to add in a pre-defined array on which poseidon hash is computed.
fn compute_storage_address(base: StorageBaseAddress, key: felt252) -> StorageBaseAddress {
let storage_addr = storage_address_to_felt252(storage_address_from_base(base));
let elt_keys: Array<felt252> = array![BYTE_ARRAY_KEY, storage_addr, key];

storage_base_address_from_felt252(poseidon::poseidon_hash_span(elt_keys.span()))
}

impl StoreByteArray of Store<ByteArray> {
fn read(address_domain: u32, base: StorageBaseAddress) -> SyscallResult<ByteArray> {
StoreByteArray::read_at_offset(address_domain, base, 0)
}

fn write(address_domain: u32, base: StorageBaseAddress, value: ByteArray) -> SyscallResult<()> {
StoreByteArray::write_at_offset(address_domain, base, 0, value)
}

fn read_at_offset(
address_domain: u32, base: StorageBaseAddress, offset: u8
) -> SyscallResult<ByteArray> {
let data_len: usize = storage_read_syscall(
address_domain, storage_address_from_base_and_offset(base, offset)
)?
.try_into()
.expect('data len out of range');

let pw_base = compute_storage_address(base, PENDING_WORD_KEY);
let pending_word = storage_read_syscall(
address_domain, storage_address_from_base(pw_base)
)?;

let pwl_base = compute_storage_address(base, PENDING_WORD_LEN_KEY);
let pending_word_len: usize = storage_read_syscall(
address_domain, storage_address_from_base(pwl_base)
)?
.try_into()
.expect('pending_word_len out of range');

if data_len == 0 {
return Result::Ok(ByteArray { data: array![], pending_word, pending_word_len, });
}

let mut data: Array<bytes31> = array![];
let mut index = 0;

loop {
if index == data_len {
break;
}

let elt_base = compute_storage_address(base, index.into());

data
.append(
storage_read_syscall(address_domain, storage_address_from_base(elt_base))
.unwrap_syscall()
.try_into()
.expect('invalid bytes31 data')
);

index += 1;
};

Result::Ok(ByteArray { data, pending_word, pending_word_len, })
}

fn write_at_offset(
address_domain: u32, base: StorageBaseAddress, offset: u8, value: ByteArray
) -> SyscallResult<()> {
// data len
storage_write_syscall(
address_domain,
storage_address_from_base_and_offset(base, offset),
value.data.len().into()
)?;

// pending word
let pw_base = compute_storage_address(base, PENDING_WORD_KEY);
storage_write_syscall(
address_domain, storage_address_from_base(pw_base), value.pending_word
)?;

// pending word len
let pwl_base = compute_storage_address(base, PENDING_WORD_LEN_KEY);
storage_write_syscall(
address_domain, storage_address_from_base(pwl_base), value.pending_word_len.into()
)?;

if value.data.len() == 0 {
return Result::Ok(());
}

// For each felt in data -> re-compute a key with the base address and the index.
let mut index = 0;

loop {
if index == value.data.len() {
break;
}

let elt: felt252 = (*value.data[index]).into();
let elt_base = compute_storage_address(base, index.into());

storage_write_syscall(address_domain, storage_address_from_base(elt_base), elt,)
.unwrap_syscall();

index += 1;
};

Result::Ok(())
}

#[inline(always)]
fn size() -> u8 {
// We consider the only data at the given offset, which
// is 1 felt long. The other ones are retrieved by
// hash derivation from the storage address.
//
// TODO: is that ok? to be checked during review.
1_u8
}
}
2 changes: 1 addition & 1 deletion contracts/src/abicov/simple_interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod simple_interface {
value: felt252,
}

#[external(v0)]
#[abi(embed_v0)]
impl MyInterfaceImpl of MyInterface<ContractState> {
fn get_value(self: @ContractState) -> felt252 {
self.value.read()
Expand Down
9 changes: 5 additions & 4 deletions crates/rs-macro/src/spanned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ impl<T: ParseInner> Parse for Spanned<T> {
pub struct Spanned<T>(Span, T);

impl<T> Spanned<T> {
// /// Retrieves the captured `Span` information for the parsed data.
// pub fn span(&self) -> Span {
// self.0
// }
/// Retrieves the captured `Span` information for the parsed data.
#[allow(dead_code)]
pub fn span(&self) -> Span {
self.0
}

/// Retrieves the inner data.
pub fn into_inner(self) -> T {
Expand Down
7 changes: 6 additions & 1 deletion crates/rs/src/expand/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ impl CairoContract {
pub struct #contract_name<A: #snrs_accounts::ConnectedAccount + Sync> {
pub address: #snrs_types::FieldElement,
pub account: A,
pub block_id: #snrs_types::BlockId,
}

impl<A: #snrs_accounts::ConnectedAccount + Sync> #contract_name<A> {
pub fn new(address: #snrs_types::FieldElement, account: A) -> Self {
Self { address, account }
Self { address, account, block_id: #snrs_types::BlockId::Tag(#snrs_types::BlockTag::Pending) }
}

pub fn set_contract_address(mut self, address: #snrs_types::FieldElement) {
Expand All @@ -34,6 +35,10 @@ impl CairoContract {
pub fn provider(&self) -> &A::Provider {
self.account.provider()
}

pub fn with_block(self, block_id: #snrs_types::BlockId) -> Self {
Self { block_id, ..self }
}
}

#[derive(Debug)]
Expand Down
8 changes: 2 additions & 6 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ katana
2. Generates the artifacts to have the abi being extracted:

```
cd contracts/
make generate_artifacts
```

Expand All @@ -26,10 +27,5 @@ make setup_simple_get_set
4. Run the example

```
cargo run --example simple_get_set --features="abigen-rs"
cargo run --example simple_get_set --all-features
```

## IMPORTANT

Currently Starkli does not support `2.4.0` compiler. The examples are compiled using `2.4.0` to test all the features
including the latest, but if you experience errors while deploying with starkli, consider re-compiling with `2.3.1` (except for `byte_array`, which is only supported by `2.4.0`).
6 changes: 3 additions & 3 deletions examples/byte_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use url::Url;

abigen!(MyContract, "./contracts/abi/byte_array.abi.json",);

const CONTRACT_ADDRESS: &str = "0x0722c97752880529933e733e9a7d725685000b3989489d8fbbe7a247d4823921";
const KATANA_ACCOUNT_0: &str = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973";
const CONTRACT_ADDRESS: &str = "0x06a811e5a04d0c1ff393f77a3e8d804ee1f991a69d25935ab4ce260aa31e5c11";
const KATANA_ACCOUNT_0: &str = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03";
const KATANA_PRIVKEY_0: &str = "0x1800000000300000180000000000030000000000003006001800006600";
const KATANA_CHAIN_ID: &str = "0x4b4154414e41";

Expand All @@ -34,7 +34,7 @@ async fn main() {
signer,
address,
FieldElement::from_hex_be(KATANA_CHAIN_ID).unwrap(),
ExecutionEncoding::Legacy,
ExecutionEncoding::New,
));

let contract = MyContract::new(contract_address, account);
Expand Down
Loading

0 comments on commit ef7e70c

Please sign in to comment.