-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
isReadOnly
helper for functions (#2008)
- Loading branch information
Showing
9 changed files
with
100 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fuel-ts/program": minor | ||
--- | ||
|
||
feat: add `isReadOnly` helper for functions |
21 changes: 21 additions & 0 deletions
21
apps/docs-snippets/src/guide/contracts/is-function-readonly.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { DocSnippetProjectsEnum } from '../../../test/fixtures/forc-projects'; | ||
import { createAndDeployContractFromProject } from '../../utils'; | ||
|
||
/** | ||
* @group node | ||
*/ | ||
test('isReadOnly returns true for read-only functions', async () => { | ||
const contract = await createAndDeployContractFromProject(DocSnippetProjectsEnum.COUNTER); | ||
|
||
// #region is-function-readonly-1 | ||
const isReadOnly = contract.functions.get_count.isReadOnly(); | ||
|
||
if (isReadOnly) { | ||
await contract.functions.get_count().get(); | ||
} else { | ||
await contract.functions.get_count().call(); | ||
} | ||
// #endregion is-function-readonly-1 | ||
|
||
expect(isReadOnly).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -305,4 +305,5 @@ CLIs | |
scaffolded | ||
programmatically | ||
BigNumber | ||
Gwei | ||
Gwei | ||
onchain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { FuelGaugeProjectsEnum } from '../test/fixtures'; | ||
|
||
import { getSetupContract } from './utils'; | ||
|
||
/** | ||
* @group node | ||
*/ | ||
describe('isReadOnly', () => { | ||
test('isReadOnly returns true for a read-only function', async () => { | ||
const contract = await getSetupContract(FuelGaugeProjectsEnum.STORAGE_TEST_CONTRACT)(); | ||
|
||
const isReadOnly = contract.functions.counter.isReadOnly(); | ||
|
||
expect(isReadOnly).toBe(true); | ||
}); | ||
|
||
test('isReadOnly returns false for a function containing write operations', async () => { | ||
const contract = await getSetupContract(FuelGaugeProjectsEnum.STORAGE_TEST_CONTRACT)(); | ||
|
||
const isReadOnly = contract.functions.increment_counter.isReadOnly(); | ||
|
||
expect(isReadOnly).toBe(false); | ||
}); | ||
|
||
test('isReadOnly does not throw a runtime error for a function that does not use storage', async () => { | ||
const contract = await getSetupContract(FuelGaugeProjectsEnum.STORAGE_TEST_CONTRACT)(); | ||
|
||
const isReadOnly = contract.functions.return_true.isReadOnly(); | ||
|
||
expect(isReadOnly).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters