Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Bytes and RawSlice as inputs for Predicates #1273

Merged
merged 42 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
372cb49
prep new bytes
Aug 21, 2023
a4d39c8
add setup
Aug 26, 2023
f0a44f3
fix test
Aug 30, 2023
a77f152
refactor
Aug 30, 2023
e575cb2
adjust
Aug 30, 2023
02aaec0
tests
Aug 30, 2023
9777940
improve tests
Aug 30, 2023
ad5d4e5
aDJUST
Aug 30, 2023
7669318
refactor
Aug 30, 2023
cd22822
add tests
Aug 30, 2023
6fd2a23
refactor
Aug 30, 2023
a74e36b
adjust
Aug 30, 2023
c7bf842
adjust
Aug 30, 2023
f6b5ce7
fix length
Aug 30, 2023
929b747
correct reference
Aug 30, 2023
00dddc8
refactor
Aug 31, 2023
c000caf
twenty eagles lick?
Aug 31, 2023
408426f
adjust
Aug 31, 2023
0ef48d3
Merge branch 'master' into cm/issue-1188-bytes
Aug 31, 2023
d6095f3
Merge branch 'master' into cm/issue-1188-bytes
nedsalk Sep 3, 2023
ca0cc2d
Merge branch 'master' into cm/issue-1188-bytes
Sep 20, 2023
37b1ae9
fix post rename
Sep 20, 2023
3f0ef12
catch magic revert number
Sep 20, 2023
b525115
fix mapper
Sep 21, 2023
2a79936
Merge branch 'master' into cm/issue-1188-bytes
Sep 21, 2023
d07acd7
Merge branch 'cm/issue-1188-bytes' into cm/issue-1205
Sep 21, 2023
95d8983
add sway projects
Sep 21, 2023
d52848b
add predicate bytes test
Sep 21, 2023
132bf46
add predicate raw slice tests
Sep 21, 2023
551041a
Merge branch 'cm/issue-1188-bytes' into cm/issue-1205
Sep 21, 2023
5994c12
fix: linting warning
nedsalk Sep 21, 2023
01f0d13
Merge branch 'master' into cm/issue-1188-bytes
Sep 22, 2023
9e14ff9
Merge branch 'cm/issue-1188-bytes' into cm/issue-1205
Sep 22, 2023
3d47dc0
adjust
Sep 22, 2023
1ce3a79
convert errors
Sep 22, 2023
8bb5cce
Merge branch 'cm/issue-1188-bytes' into cm/issue-1205
Sep 22, 2023
9ba3bde
Merge branch 'master' into cm/issue-1188-bytes
Sep 22, 2023
0f578d1
Merge branch 'cm/issue-1188-bytes' into cm/issue-1205
Sep 22, 2023
f9fca6b
Merge branch 'master' into cm/issue-1205
Sep 25, 2023
3a767a4
cs
camsjams Sep 25, 2023
de981f0
pretty
Sep 25, 2023
bb2b2b2
Merge branch 'master' into cm/issue-1205
Sep 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/thirty-dogs-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

Add tests for Bytes and RawSlice
2 changes: 2 additions & 0 deletions packages/fuel-gauge/fixtures/forc-projects/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ members = [
"multi-token-contract",
"payable-annotation",
"predicate-address",
"predicate-bytes",
"predicate-conditional-inputs",
"predicate-false",
"predicate-main-args-struct",
"predicate-main-args-vector",
"predicate-multi-args",
"predicate-raw-slice",
"predicate-struct",
"predicate-triple-sig",
"predicate-true",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["FuelLabs"]
entry = "main.sw"
license = "Apache-2.0"
name = "predicate-bytes"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
predicate;

use std::bytes::Bytes;

#[allow(dead_code)]
enum SomeEnum<T> {
First: bool,
Second: T,
}

struct Wrapper<T> {
inner: T,
inner_enum: SomeEnum<Bytes>,
}

fn expected_bytes() -> Bytes {
let mut bytes = Bytes::new();

bytes.push(40u8);
bytes.push(41u8);
bytes.push(42u8);

bytes
}

fn valid_bytes(bytes: Bytes) -> bool {
bytes == expected_bytes()
}

fn valid_vec(arg: Vec<Bytes>) -> bool {
if arg.len() != 2 {
return false;
}

valid_bytes(arg.get(0).unwrap()) && valid_bytes(arg.get(1).unwrap())
}

fn main(wrapper: Wrapper<Vec<Bytes>>) -> bool {
if let SomeEnum::Second(enum_bytes) = wrapper.inner_enum {
valid_bytes(enum_bytes) && valid_vec(wrapper.inner)
} else {
false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["FuelLabs"]
entry = "main.sw"
license = "Apache-2.0"
name = "predicate-raw-slice"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
predicate;

#[allow(dead_code)]
enum SomeEnum<T> {
First: bool,
Second: T,
}

struct Wrapper<T> {
inner: T,
inner_enum: SomeEnum<raw_slice>,
}

fn valid_raw_slice(slice: raw_slice) -> bool {
let vec: Vec<u64> = Vec::from(slice);
vec.len() == 3 && vec.get(0).unwrap() == 40 && vec.get(1).unwrap() == 41 && vec.get(2).unwrap() == 42
}

fn valid_vec(vec: Vec<raw_slice>) -> bool {
vec.len() == 2 && valid_raw_slice(vec.get(0).unwrap()) && valid_raw_slice(vec.get(1).unwrap())
}

fn main(wrapper: Wrapper<Vec<raw_slice>>) -> bool {
if let SomeEnum::Second(enum_raw_slice) = wrapper.inner_enum
{
valid_raw_slice(enum_raw_slice) && valid_vec(wrapper.inner)
} else {
false
}
}
64 changes: 62 additions & 2 deletions packages/fuel-gauge/src/bytes.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { type Contract } from 'fuels';
import { generateTestWallet } from '@fuel-ts/wallet/test-utils';
import {
type Contract,
bn,
Predicate,
Wallet,
Address,
BaseAssetId,
Provider,
FUEL_NETWORK_URL,
} from 'fuels';

import predicateBytes from '../fixtures/forc-projects/predicate-bytes';
import predicateBytesAbi from '../fixtures/forc-projects/predicate-bytes/out/debug/predicate-bytes-abi.json';

import { getSetupContract } from './utils';

Expand All @@ -8,6 +21,16 @@ beforeAll(async () => {
contractInstance = await setupContract();
});

type SomeEnum = {
First?: boolean;
Second?: number[];
};

type Wrapper = {
inner: number[][];
inner_enum: SomeEnum;
};

describe('Bytes Tests', () => {
it('should test bytes output', async () => {
const INPUT = 10;
Expand Down Expand Up @@ -35,7 +58,8 @@ describe('Bytes Tests', () => {

it('should test bytes input [nested]', async () => {
const bytes = [40, 41, 42];
const INPUT = {

const INPUT: Wrapper = {
inner: [bytes, bytes],
inner_enum: { Second: bytes },
};
Expand All @@ -44,4 +68,40 @@ describe('Bytes Tests', () => {

expect(true).toBeTruthy();
});

it('should test bytes input [predicate-bytes]', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);

// Create wallet
const wallet = await generateTestWallet(provider, [[5_000, BaseAssetId]]);
const receiver = Wallet.fromAddress(Address.fromRandom(), wallet.provider);
const amountToPredicate = 100;
const amountToReceiver = 50;
type MainArgs = [Wrapper];
const predicate = new Predicate<MainArgs>(predicateBytes, wallet.provider, predicateBytesAbi);

// setup predicate
const setupTx = await wallet.transfer(predicate.address, amountToPredicate, BaseAssetId);
await setupTx.waitForResult();

const initialPredicateBalance = await predicate.getBalance();
const initialReceiverBalance = await receiver.getBalance();
const bytes = [40, 41, 42];
const INPUT: Wrapper = {
inner: [bytes, bytes],
inner_enum: { Second: bytes },
};
const tx = await predicate.setData(INPUT).transfer(receiver.address, amountToReceiver);
await tx.waitForResult();

// Check the balance of the receiver
const finalReceiverBalance = await receiver.getBalance();
expect(bn(initialReceiverBalance).add(amountToReceiver).toHex()).toEqual(
finalReceiverBalance.toHex()
);

// Check we spent the entire predicate hash input
const finalPredicateBalance = await predicate.getBalance();
expect(finalPredicateBalance.lte(initialPredicateBalance)).toBeTruthy();
});
});
65 changes: 64 additions & 1 deletion packages/fuel-gauge/src/raw-slice.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import { generateTestWallet } from '@fuel-ts/wallet/test-utils';
import type { BN } from 'fuels';
import { type Contract } from 'fuels';
import {
type Contract,
bn,
Predicate,
Wallet,
Address,
BaseAssetId,
Provider,
FUEL_NETWORK_URL,
} from 'fuels';

import predicateRawSlice from '../fixtures/forc-projects/predicate-raw-slice';
import predicateRawSliceAbi from '../fixtures/forc-projects/predicate-raw-slice/out/debug/predicate-raw-slice-abi.json';

import { getSetupContract } from './utils';

type SomeEnum = {
First?: boolean;
Second?: number[];
};

type Wrapper = {
inner: number[][];
inner_enum: SomeEnum;
};

const setupContract = getSetupContract('raw-slice');
let contractInstance: Contract;
beforeAll(async () => {
Expand Down Expand Up @@ -37,4 +60,44 @@ describe('Raw Slice Tests', () => {

expect(true).toBeTruthy();
});

it('should test raw slice input [predicate-raw slice]', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);

// Create wallet
const wallet = await generateTestWallet(provider, [[5_000, BaseAssetId]]);
const receiver = Wallet.fromAddress(Address.fromRandom(), wallet.provider);
const amountToPredicate = 100;
const amountToReceiver = 50;
type MainArgs = [Wrapper];
const predicate = new Predicate<MainArgs>(
predicateRawSlice,
wallet.provider,
predicateRawSliceAbi
);

// setup predicate
const setupTx = await wallet.transfer(predicate.address, amountToPredicate, BaseAssetId);
await setupTx.waitForResult();

const initialPredicateBalance = await predicate.getBalance();
const initialReceiverBalance = await receiver.getBalance();
const bytes = [40, 41, 42];
const INPUT: Wrapper = {
inner: [bytes, bytes],
inner_enum: { Second: bytes },
};
const tx = await predicate.setData(INPUT).transfer(receiver.address, amountToReceiver);
await tx.waitForResult();

// Check the balance of the receiver
const finalReceiverBalance = await receiver.getBalance();
expect(bn(initialReceiverBalance).add(amountToReceiver).toHex()).toEqual(
finalReceiverBalance.toHex()
);

// Check we spent the entire predicate hash input
const finalPredicateBalance = await predicate.getBalance();
expect(finalPredicateBalance.lte(initialPredicateBalance)).toBeTruthy();
});
});