diff --git a/src/storage/src/list.cairo b/src/storage/src/list.cairo index fa4fac24..c086f396 100644 --- a/src/storage/src/list.cairo +++ b/src/storage/src/list.cairo @@ -102,12 +102,15 @@ impl ListImpl, impl TDrop: Drop, impl TStore: Store fn from_array(ref self: List, array: @Array) { let mut array: Span = array.span(); - let (base, offset) = calculate_base_and_offset_for_index(self.base, 0, self.storage_size); + let mut index = 0; self.len = array.len(); loop { match array.pop_front() { - Option::Some(v) => (Store::write_at_offset(self.address_domain, base, offset, *v) - .unwrap_syscall()), + Option::Some(v) => { + let (base, offset) = calculate_base_and_offset_for_index(self.base, index, self.storage_size); + Store::write_at_offset(self.address_domain, base, offset, *v).unwrap_syscall(); + index += 1; + }, Option::None => { break; } diff --git a/src/storage/src/tests/list_test.cairo b/src/storage/src/tests/list_test.cairo index 2f87dc53..8acb2edb 100644 --- a/src/storage/src/tests/list_test.cairo +++ b/src/storage/src/tests/list_test.cairo @@ -376,12 +376,15 @@ mod tests { #[available_gas(100000000)] fn test_from_array() { let contract = deploy_mock(); - let mock_addr = mock_addr(); + let mock_addr = mock_addr(); let addrs_array = array![mock_addr, mock_addr, mock_addr]; - let numbers_array = array![200_u256, 300_u256, 100_u256]; + let numbers_array = array![200, 300, 100]; contract.do_from_array(addrs_array, numbers_array); assert(contract.do_get_len() == (3, 3), 'len should be 3'); + assert(contract.do_get_index(0) == (mock_addr, 200), 'idx 0'); + assert(contract.do_get_index(1) == (mock_addr, 300), 'idx 1'); + assert(contract.do_get_index(2) == (mock_addr, 100), 'idx 2'); } #[test] @@ -402,6 +405,8 @@ mod tests { assert(contract.do_append(mock_addr, 10) == (0, 0), '1st append idx'); assert(contract.do_append(mock_addr, 20) == (1, 1), '2nd append idx'); assert(contract.do_get_len() == (2, 2), 'len'); + assert(contract.do_get_index(0) == (mock_addr, 10), 'idx 0'); + assert(contract.do_get_index(1) == (mock_addr, 20), 'idx 1'); contract.do_from_array(array![], array![]); let (a, b) = contract.do_get_len();