-
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: deploying scripts and predicates (#3251)
Co-authored-by: Sergio Torres <30977845+Torres-ssf@users.noreply.github.com> Co-authored-by: Daniel Bate <djbate23@gmail.com> Co-authored-by: Luiz Estácio | stacio.eth <luizstacio@gmail.com> Co-authored-by: Peter Smith <peter@blueoceancomputing.co.uk> Co-authored-by: Sérgio Torres <30977845+Torres-ssf@users.noreply.github.com> Co-authored-by: chad <chad.nehemiah94@gmail.com> Co-authored-by: Dhaiwat <dhaiwatpandya@gmail.com> Co-authored-by: nedsalk <nedim.salkic@fuel.sh>
- Loading branch information
1 parent
a35d644
commit c2f0599
Showing
41 changed files
with
1,391 additions
and
264 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,10 @@ | ||
--- | ||
"@fuel-ts/account": patch | ||
"@fuel-ts/contract": patch | ||
"fuels": patch | ||
"@fuel-ts/program": patch | ||
"@fuel-ts/versions": patch | ||
"create-fuels": patch | ||
--- | ||
|
||
feat: deploying scripts and predicates |
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
96 changes: 96 additions & 0 deletions
96
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,96 @@ | ||
import { readFileSync } from 'fs'; | ||
import { ContractFactory, Predicate, Provider, Wallet, hexlify } from 'fuels'; | ||
import { launchTestNode } from 'fuels/test-utils'; | ||
import { join } from 'path'; | ||
|
||
import { ConfigurablePin as TypegenPredicate } from '../../../test/typegen'; | ||
|
||
/** | ||
* @group browser | ||
* @group node | ||
* | ||
* TODO: enable the test and reintroduce the docs | ||
*/ | ||
describe.skip('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(); | ||
|
||
const loaderBytecode = hexlify( | ||
readFileSync( | ||
join( | ||
__dirname, | ||
'../../../test/fixtures/forc-projects/configurable-pin/out/release/configurable-pin.deployed.bin' | ||
) | ||
) | ||
); | ||
|
||
// #region deploying-predicates | ||
// #import { Provider, Wallet, hexlify }; | ||
// #context import { readFileSync } from 'fs'; | ||
// #context import { WALLET_PVT_KEY } from 'path/to/my/env/file'; | ||
// #context import { TypegenPredicate } from 'path/to/typegen/outputs'; | ||
|
||
// First, we will need the loader bytecode that is generated by `fuels deploy` | ||
// #context const loaderBytecode = hexlify(readFileSync('path/to/forc/build/outputs'))); | ||
|
||
const provider = await Provider.create(providerUrl); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
|
||
// Then we will instantiate the predicate using both the scripts bytecode and it's loader bytecode, | ||
// now we are free to interact with the predicate as we would normally, such as overriding the configurables | ||
const predicate = new Predicate({ | ||
bytecode: loaderBytecode, | ||
abi: TypegenPredicate.abi, | ||
data: [1337], | ||
provider, | ||
}); | ||
|
||
// First, let's fund the predicate | ||
const { waitForResult: waitForFund } = await wallet.transfer(predicate.address, 100_000); | ||
await waitForFund(); | ||
|
||
const { waitForResult: waitForTransfer } = await predicate.transfer(receiver.address, 1000); | ||
const { gasUsed } = await waitForTransfer(); | ||
// #endregion deploying-predicates | ||
|
||
const anotherPredicate = new Predicate({ | ||
bytecode: TypegenPredicate.bytecode, | ||
abi: TypegenPredicate.abi, | ||
data: [1337], | ||
provider, | ||
}); | ||
|
||
const { waitForResult: waitForAnotherFund } = await wallet.transfer( | ||
anotherPredicate.address, | ||
100_000 | ||
); | ||
await waitForAnotherFund(); | ||
|
||
const { waitForResult: waitForAnotherTransfer } = await anotherPredicate.transfer( | ||
receiver.address, | ||
1000 | ||
); | ||
const { gasUsed: anotherGasUsed } = await waitForAnotherTransfer(); | ||
|
||
expect(recieverInitialBalance.toNumber()).toBeLessThan(recieverInitialBalance.toNumber()); | ||
expect(gasUsed.toNumber()).toBeLessThan(anotherGasUsed.toNumber()); | ||
}); | ||
}); |
73 changes: 73 additions & 0 deletions
73
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,73 @@ | ||
import { readFileSync } from 'fs'; | ||
import { ContractFactory, Provider, Script, Wallet, hexlify } from 'fuels'; | ||
import { launchTestNode } from 'fuels/test-utils'; | ||
import { join } from 'path'; | ||
|
||
import { SumScript as TypegenScript } from '../../../test/typegen'; | ||
|
||
/** | ||
* @group browser | ||
* @group node | ||
* | ||
* TODO: enable the test and reintroduce the docs | ||
*/ | ||
describe.skip('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(); | ||
|
||
const loaderBytecode = hexlify( | ||
readFileSync( | ||
join( | ||
__dirname, | ||
'../../../test/fixtures/forc-projects/sum-script/out/release/sum-script-loader.bin' | ||
) | ||
) | ||
); | ||
|
||
// #region deploying-scripts | ||
// #import { Provider, Wallet, hexlify }; | ||
// #context import { readFileSync } from 'fs'; | ||
// #context import { WALLET_PVT_KEY } from 'path/to/my/env/file'; | ||
// #context import { TypegenScript } from 'path/to/typegen/outputs'; | ||
|
||
// First, we will need the loader bytecode that is generated by `fuels deploy` | ||
// #context const loaderBytecode = hexlify(readFileSync('path/to/forc/build/outputs'))); | ||
|
||
const provider = await Provider.create(providerUrl); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
|
||
// Then we will instantiate the script using both the scripts bytecode and it's loader bytecode | ||
const script = new Script(loaderBytecode, TypegenScript.abi, 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(); | ||
// #endregion deploying-scripts | ||
|
||
const scriptWithoutLoader = new Script(TypegenScript.bytecode, TypegenScript.abi, 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()).toBeLessThan(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.36.0 | ||
0.37.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
Oops, something went wrong.