-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into dependabot/npm_and_yarn/memfs-4.12.0
- Loading branch information
Showing
132 changed files
with
3,481 additions
and
786 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,2 @@ | ||
[toolchain] | ||
channel = "testnet" | ||
|
||
[components] | ||
forc = "0.63.5" | ||
fuel-core = "0.35.0" |
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
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,17 @@ | ||
# Kill anything running on port 4000 | ||
lsof -t -i:4000 | xargs -r kill | ||
|
||
# Runs a node at port 4000 | ||
pnpm fuels node > /dev/null 2>&1 & | ||
|
||
# Builds projects | ||
pnpm fuels build | ||
|
||
# Deploys projects (needed for loader bytecode) | ||
pnpm fuels deploy | ||
|
||
# Kills the node | ||
lsof -t -i:4000 | xargs -r kill | ||
|
||
# Checks for type errors | ||
pnpm tsc --noEmit |
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
83 changes: 83 additions & 0 deletions
83
apps/docs-snippets/src/guide/predicates/deploying-predicates.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,83 @@ | ||
import { ContractFactory, Provider, Wallet, hexlify } from 'fuels'; | ||
import { launchTestNode } from 'fuels/test-utils'; | ||
|
||
import { | ||
ConfigurablePin as TypegenPredicate, | ||
ConfigurablePinLoader as TypegenPredicateLoader, | ||
} from '../../../test/typegen'; | ||
|
||
/** | ||
* @group browser | ||
* @group node | ||
*/ | ||
describe('Deploying Predicates', () => { | ||
it('deploys a predicate via loader and calls', async () => { | ||
using launched = await launchTestNode(); | ||
|
||
const { | ||
provider: testProvider, | ||
wallets: [testWallet, receiver], | ||
} = launched; | ||
|
||
const recieverInitialBalance = await receiver.getBalance(); | ||
|
||
const providerUrl = testProvider.url; | ||
const WALLET_PVT_KEY = hexlify(testWallet.privateKey); | ||
|
||
const factory = new ContractFactory( | ||
TypegenPredicate.bytecode, | ||
TypegenPredicate.abi, | ||
testWallet | ||
); | ||
const { waitForResult: waitForDeploy } = await factory.deployAsBlobTxForScript(); | ||
await waitForDeploy(); | ||
|
||
// #region deploying-predicates | ||
// #import { Provider, Wallet }; | ||
// #context import { WALLET_PVT_KEY } from 'path/to/my/env/file'; | ||
// #context import { TypegenPredicateLoader } from 'path/to/typegen/outputs'; | ||
|
||
const provider = await Provider.create(providerUrl); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
const baseAssetId = provider.getBaseAssetId(); | ||
|
||
// First, we will need to instantiate the script via it's loader bytecode. | ||
// This can be imported from the typegen outputs that were created on `fuels deploy`. | ||
// Then we can use the predicate as we would normally, such as overriding the configurables. | ||
const predicate = new TypegenPredicateLoader({ | ||
data: [23], | ||
provider, | ||
configurableConstants: { | ||
PIN: 23, | ||
}, | ||
}); | ||
|
||
// Now, let's fund the predicate | ||
const fundTx = await wallet.transfer(predicate.address, 100_000, baseAssetId); | ||
await fundTx.waitForResult(); | ||
|
||
// Then we'll execute the transfer and validate the predicate | ||
const transferTx = await predicate.transfer(receiver.address, 1000, baseAssetId); | ||
const { gasUsed } = await transferTx.waitForResult(); | ||
// #endregion deploying-predicates | ||
|
||
const anotherPredicate = new TypegenPredicate({ | ||
data: [23], | ||
provider, | ||
configurableConstants: { | ||
PIN: 23, | ||
}, | ||
}); | ||
|
||
const anotherFundTx = await wallet.transfer(anotherPredicate.address, 100_000); | ||
await anotherFundTx.waitForResult(); | ||
|
||
const anotherTransferTx = await anotherPredicate.transfer(receiver.address, 1000); | ||
const { gasUsed: anotherGasUsed } = await anotherTransferTx.waitForResult(); | ||
|
||
expect(recieverInitialBalance.toNumber()).toBeLessThan( | ||
(await receiver.getBalance()).toNumber() | ||
); | ||
expect(gasUsed.toNumber()).toBeLessThanOrEqual(anotherGasUsed.toNumber()); | ||
}); | ||
}); |
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
61 changes: 61 additions & 0 deletions
61
apps/docs-snippets/src/guide/scripts/deploying-scripts.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,61 @@ | ||
import { ContractFactory, Provider, Wallet, hexlify } from 'fuels'; | ||
import { launchTestNode } from 'fuels/test-utils'; | ||
|
||
import { | ||
SumScript as TypegenScript, | ||
SumScriptLoader as TypegenScriptLoader, | ||
} from '../../../test/typegen'; | ||
|
||
/** | ||
* @group browser | ||
* @group node | ||
*/ | ||
describe('Deploying Scripts', () => { | ||
it('deploys a script via loader and calls', async () => { | ||
using launched = await launchTestNode(); | ||
|
||
const { | ||
provider: testProvider, | ||
wallets: [testWallet], | ||
} = launched; | ||
|
||
const providerUrl = testProvider.url; | ||
const WALLET_PVT_KEY = hexlify(testWallet.privateKey); | ||
|
||
const factory = new ContractFactory(TypegenScript.bytecode, TypegenScript.abi, testWallet); | ||
const { waitForResult: waitForDeploy } = await factory.deployAsBlobTxForScript(); | ||
await waitForDeploy(); | ||
|
||
// #region deploying-scripts | ||
// #import { Provider, Wallet }; | ||
// #context import { WALLET_PVT_KEY } from 'path/to/my/env/file'; | ||
// #context import { TypegenScriptLoader } from 'path/to/typegen/outputs'; | ||
|
||
const provider = await Provider.create(providerUrl); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
|
||
// First, we will need to instantiate the script via it's loader bytecode. This can be imported from the typegen outputs | ||
// that were created on `fuels deploy` | ||
const script = new TypegenScriptLoader(wallet); | ||
|
||
// Now we are free to interact with the script as we would normally, such as overriding the configurables | ||
const configurable = { | ||
AMOUNT: 20, | ||
}; | ||
script.setConfigurableConstants(configurable); | ||
|
||
const { waitForResult } = await script.functions.main(10).call(); | ||
const { value, gasUsed } = await waitForResult(); | ||
// value: 10 | ||
// #endregion deploying-scripts | ||
|
||
const scriptWithoutLoader = new TypegenScript(wallet); | ||
scriptWithoutLoader.setConfigurableConstants(configurable); | ||
const { waitForResult: waitForAnotherResult } = await script.functions.main(10).call(); | ||
const { value: anotherValue, gasUsed: anotherGasUsed } = await waitForAnotherResult(); | ||
|
||
expect(value).toBe(30); | ||
expect(anotherValue).toBe(30); | ||
expect(gasUsed.toNumber()).toBeLessThanOrEqual(anotherGasUsed.toNumber()); | ||
}); | ||
}); |
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
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,20 @@ | ||
# Deploying Predicates | ||
|
||
In order to optimize the cost of your recurring predicate executions, we recommend first deploying your predicate. This can be done using the [Fuels CLI](../fuels-cli/index.md) and running the [deploy command](../fuels-cli/commands.md#fuels-deploy). | ||
|
||
By deploying the predicate, its bytecode is stored on chain as a blob. The SDK will then produce bytecode that can load the blob on demand to execute the original predicate. This far reduces the repeat execution cost of the predicate. | ||
|
||
## How to Deploy a Predicate | ||
|
||
To deploy a predicate, we can use the [Fuels CLI](../fuels-cli/index.md) and execute the [deploy command](../fuels-cli/commands.md#fuels-deploy). | ||
|
||
This will perform the following actions: | ||
|
||
1. Compile the predicate using your `forc` version | ||
1. Deploy the built predicate binary to the chain as a blob | ||
1. Generate a new, smaller predicate that loads the deployed predicate's blob | ||
1. Generate types for both the predicate and the loader that you can use in your application | ||
|
||
We can then utilize the above generated types like so: | ||
|
||
<<< @/../../docs-snippets/src/guide/predicates/deploying-predicates.test.ts#deploying-predicates{ts:line-numbers} |
Oops, something went wrong.