-
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.
- Loading branch information
1 parent
af3202c
commit b1dbe42
Showing
12 changed files
with
238 additions
and
2 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/account": patch | ||
--- | ||
|
||
feat: implement `generateFakeResources` on `Account` class |
57 changes: 57 additions & 0 deletions
57
apps/docs-snippets/src/guide/cookbook/generate-fake-resources.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,57 @@ | ||
import type { TransactionResultReturnDataReceipt } from 'fuels'; | ||
import { | ||
FUEL_NETWORK_URL, | ||
Provider, | ||
ReceiptType, | ||
ScriptTransactionRequest, | ||
Wallet, | ||
bn, | ||
} from 'fuels'; | ||
|
||
import { | ||
DocSnippetProjectsEnum, | ||
getDocsSnippetsForcProject, | ||
} from '../../../test/fixtures/forc-projects'; | ||
|
||
/** | ||
* @group node | ||
*/ | ||
describe(__filename, () => { | ||
it('should generate fake resources just fine', async () => { | ||
const provider = await Provider.create(FUEL_NETWORK_URL); | ||
const wallet = Wallet.generate({ provider }); | ||
const baseAssetId = provider.getBaseAssetId(); | ||
|
||
const { binHexlified: scriptHexBytes } = getDocsSnippetsForcProject( | ||
DocSnippetProjectsEnum.RETURN_SCRIPT | ||
); | ||
|
||
// #region generate-fake-resources-2 | ||
const transactionRequest = new ScriptTransactionRequest({ | ||
gasLimit: bn(62_000), | ||
maxFee: bn(60_000), | ||
script: scriptHexBytes, | ||
}); | ||
|
||
const resources = wallet.generateFakeResources([ | ||
{ | ||
amount: bn(100_000), | ||
assetId: baseAssetId, | ||
}, | ||
]); | ||
|
||
transactionRequest.addResources(resources); | ||
|
||
const dryrunResult = await provider.call(transactionRequest); | ||
|
||
const returnReceipt = dryrunResult.receipts.find( | ||
(receipt) => receipt.type === ReceiptType.ReturnData | ||
) as TransactionResultReturnDataReceipt; | ||
|
||
const { data: returnedValue } = returnReceipt; | ||
// #endregion generate-fake-resources-2 | ||
|
||
expect(bn(returnedValue).toNumber()).toBe(1337); | ||
expect(dryrunResult.dryRunStatus?.type).toBe('DryRunSuccessStatus'); | ||
}); | ||
}); |
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
6 changes: 6 additions & 0 deletions
6
apps/docs-snippets/test/fixtures/forc-projects/return-script/Forc.toml
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,6 @@ | ||
[project] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "return-script" | ||
|
||
[dependencies] |
7 changes: 7 additions & 0 deletions
7
apps/docs-snippets/test/fixtures/forc-projects/return-script/src/main.sw
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,7 @@ | ||
// #region generate-fake-resources-1 | ||
script; | ||
|
||
fn main() -> u64 { | ||
return 1337; | ||
} | ||
// #endregion generate-fake-resources-1 |
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,13 @@ | ||
# Generate Fake Resources | ||
|
||
When working with an unfunded account, you can generate fake resources to perform a dry-run on your transactions. This is useful for testing purposes without the need for real funds. | ||
|
||
Below is an example script that returns the value `1337`. You can use fake resources to execute a dry-run of this script and obtain the returned value. | ||
|
||
<<< @/../../docs-snippets/test/fixtures/forc-projects/return-script/src/main.sw#generate-fake-resources-1{rust:line-numbers} | ||
|
||
To execute a dry-run, use the `Provider.call` method. Ensure you set the `utxo_validation` flag to true, as this script uses fake UTXOs: | ||
|
||
<<< @/../../docs-snippets/src/guide/cookbook/generate-fake-resources.test.ts#generate-fake-resources-2{ts:line-numbers} | ||
|
||
By setting `utxo_validation` to `true`, you can successfully execute the dry-run and retrieve the returned value from the script without requiring actual funds. |
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
72 changes: 72 additions & 0 deletions
72
packages/fuel-gauge/src/predicate/predicate-general.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,72 @@ | ||
import { ASSET_A, ASSET_B } from '@fuel-ts/utils/test-utils'; | ||
import type { BN, FakeResources } from 'fuels'; | ||
import { | ||
Address, | ||
FUEL_NETWORK_URL, | ||
Predicate, | ||
Provider, | ||
ScriptTransactionRequest, | ||
bn, | ||
} from 'fuels'; | ||
|
||
import { FuelGaugeProjectsEnum, getFuelGaugeForcProject } from '../../test/fixtures'; | ||
|
||
/** | ||
* @group node | ||
*/ | ||
describe('Predicate', () => { | ||
it('can generate and use fake predicate coins', async () => { | ||
const provider = await Provider.create(FUEL_NETWORK_URL); | ||
const baseAssetId = provider.getBaseAssetId(); | ||
const { binHexlified, abiContents } = getFuelGaugeForcProject( | ||
FuelGaugeProjectsEnum.PREDICATE_SUM | ||
); | ||
|
||
const amount1 = bn(500_000); | ||
const amount2 = bn(200_000); | ||
const amount3 = bn(300_000); | ||
const amountToTransferBaseAsset = bn(1000); | ||
|
||
const fakeCoinsConfig: FakeResources[] = [ | ||
{ amount: amount1, assetId: baseAssetId }, | ||
{ amount: amount2, assetId: ASSET_A }, | ||
{ amount: amount3, assetId: ASSET_B }, | ||
]; | ||
|
||
const value2 = bn(200); | ||
const value1 = bn(100); | ||
|
||
const predicate = new Predicate<[BN, BN]>({ | ||
bytecode: binHexlified, | ||
abi: abiContents, | ||
provider, | ||
inputData: [value1, value2], | ||
}); | ||
|
||
const fakeCoins = predicate.generateFakeResources(fakeCoinsConfig); | ||
|
||
let request = new ScriptTransactionRequest({ | ||
gasLimit: bn(270_000), | ||
maxFee: bn(250_000), | ||
}); | ||
|
||
fakeCoins.forEach((coin) => { | ||
expect(coin.predicate).toBeDefined(); | ||
expect(coin.predicateData).toBeDefined(); | ||
}); | ||
|
||
request.addResources(fakeCoins); | ||
request.addCoinOutput(Address.fromRandom(), amountToTransferBaseAsset, baseAssetId); | ||
request.addCoinOutput(Address.fromRandom(), amount2, ASSET_A); | ||
request.addCoinOutput(Address.fromRandom(), amount3, ASSET_B); | ||
|
||
request = await provider.estimatePredicates(request); | ||
|
||
const { dryRunStatus } = await provider.call(request, { | ||
utxoValidation: false, | ||
estimateTxDependencies: false, | ||
}); | ||
|
||
expect(dryRunStatus?.type).toBe('DryRunSuccessStatus'); | ||
}); | ||
}); |