Skip to content

Commit dd7b1ca

Browse files
Cameron ManavianarboleyaDhaiwat10
authored
feat: add vector as output slice (#812)
* setup * adjust rough draft * cs * use as * use current code --------- Co-authored-by: Anderson Arboleya <[email protected]> Co-authored-by: Dhaiwat <[email protected]>
1 parent f6948ff commit dd7b1ca

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

.changeset/olive-cows-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/abi-coder": patch
3+
---
4+
5+
Adjust to support vector output

packages/abi-coder/src/abi-coder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ export default class AbiCoder {
6969
return new ArrayCoder(itemCoder, length);
7070
}
7171

72+
if (['raw untyped slice'].includes(param.type)) {
73+
const length = 0;
74+
const itemCoder = this.getCoder({ type: 'u64' });
75+
return new ArrayCoder(itemCoder, length);
76+
}
77+
7278
const stringMatch = stringRegEx.exec(param.type)?.groups;
7379
if (stringMatch) {
7480
const length = parseInt(stringMatch.length, 10);
@@ -188,6 +194,9 @@ export default class AbiCoder {
188194
}
189195

190196
const coders = nonEmptyTypes.map((type) => this.getCoder(type));
197+
if (nonEmptyTypes[0] && nonEmptyTypes[0].type === 'raw untyped slice') {
198+
(coders[0] as ArrayCoder<U64Coder>).length = bytes.length / 8;
199+
}
191200
const coder = new TupleCoder(coders);
192201
const [decoded, newOffset] = coder.decode(bytes, 0);
193202

packages/fuel-gauge/src/coverage-contract.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,22 @@ describe('Coverage Contract', () => {
448448
expect([logs[3], logs[4], logs[5]]).toEqual([1, 2, 3]);
449449
// #endregion
450450
});
451+
452+
it('should get raw_slice output [u8]', async () => {
453+
const { value } = await contractInstance.functions.echo_u8_vector([100, 2, 1, 2, 3]).call();
454+
455+
expect(value.map((v: BN) => v.toNumber())).toStrictEqual([100, 2, 1, 2, 3]);
456+
});
457+
458+
it('should get raw_slice output [u64]', async () => {
459+
const { value } = await contractInstance.functions.echo_u64_vector([100, 2, 1, 2, 3]).call();
460+
461+
expect(value.map((v: BN) => v.toNumber())).toStrictEqual([100, 2, 1, 2, 3]);
462+
});
463+
464+
it('should get raw_slice output', async () => {
465+
const { value } = await contractInstance.functions.get_u64_vector().call();
466+
467+
expect(value.map((v: BN) => v.toNumber())).toStrictEqual([1, 2, 3]);
468+
});
451469
});

packages/fuel-gauge/test-projects/coverage-contract/src/main.sw

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,16 @@ abi CoverageContract {
8484
fn echo_option_u8(input: Option<u8>) -> Option<u8>;
8585
fn echo_option_extract_u32(input: Option<u32>) -> u32;
8686
fn echo_option_three_u8(inputA: Option<u8>, inputB: Option<u8>, inputC: Option<u8>) -> u8;
87-
fn echo_u8_vector(input: Vec<u8>) -> Vec<u8>;
8887
fn echo_u8_vector_first(vector: Vec<u8>) -> u8;
8988
fn echo_u8_option_vector_first(vector: Vec<Option<u8>>) -> u8;
9089
fn echo_u64_vector_last(vector: Vec<u64>) -> u64;
9190
fn echo_u32_vector_addition_other_type(vector: Vec<u32>, input: u32) -> u32;
9291
fn echo_u32_vector_addition(vector_1: Vec<u32>, vector_2: Vec<u32>) -> u32;
9392
fn echo_struct_vector_first(vector: Vec<BigStruct>) -> BigStruct;
9493
fn echo_struct_vector_last(vector: Vec<ComplexStruct>) -> ComplexStruct;
94+
fn get_u64_vector() -> raw_slice;
95+
fn echo_u8_vector(input: Vec<u8>) -> raw_slice;
96+
fn echo_u64_vector(input: Vec<u64>) -> raw_slice;
9597
}
9698

9799
impl CoverageContract for Contract {
@@ -273,9 +275,6 @@ impl CoverageContract for Contract {
273275
value1 + value2 + value3
274276
}
275277
// #endregion
276-
fn echo_u8_vector(input: Vec<u8>) -> Vec<u8> {
277-
input
278-
}
279278

280279
fn echo_u8_vector_first(vector: Vec<u8>) -> u8 {
281280
match vector.get(0) {
@@ -320,4 +319,24 @@ impl CoverageContract for Contract {
320319
vector.get(vector.len() - 1).unwrap()
321320
}
322321
// #endregion
322+
323+
fn get_u64_vector() -> raw_slice {
324+
// Convert to a vector
325+
let mut vec: Vec<u64> = Vec::new();
326+
327+
vec.push(1);
328+
vec.push(2);
329+
vec.push(3);
330+
331+
// Return it
332+
vec.as_raw_slice()
333+
}
334+
335+
fn echo_u8_vector(input: Vec<u8>) -> raw_slice {
336+
input.as_raw_slice()
337+
}
338+
339+
fn echo_u64_vector(input: Vec<u64>) -> raw_slice {
340+
input.as_raw_slice()
341+
}
323342
}

0 commit comments

Comments
 (0)