-
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
b0e9c84
commit b9ff397
Showing
14 changed files
with
119 additions
and
16 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,2 @@ | ||
--- | ||
--- |
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,40 @@ | ||
// #region script-init | ||
import type { BigNumberish } from 'fuels'; | ||
import { arrayify, Provider, ReceiptType, ScriptRequest, Wallet } from 'fuels'; | ||
|
||
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; | ||
import { CallTestScript } from '../typegend'; | ||
|
||
const provider = await Provider.create(LOCAL_NETWORK_URL); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
const script = new CallTestScript(wallet); | ||
|
||
type MyStruct = { | ||
arg_one: boolean; | ||
arg_two: BigNumberish; | ||
}; | ||
|
||
const scriptRequest = new ScriptRequest( | ||
CallTestScript.bytecode, | ||
(myStruct: MyStruct) => { | ||
const encoded = script.interface.functions.main.encodeArguments([myStruct]); | ||
|
||
return arrayify(encoded); | ||
}, | ||
(scriptResult) => { | ||
if (scriptResult.returnReceipt.type === ReceiptType.Revert) { | ||
throw new Error('Reverted'); | ||
} | ||
if (scriptResult.returnReceipt.type !== ReceiptType.ReturnData) { | ||
throw new Error('fail'); | ||
} | ||
|
||
const [decodedResult] = script.interface.functions.main.decodeOutput( | ||
scriptResult.returnReceipt.data | ||
); | ||
return decodedResult; | ||
} | ||
); | ||
// #endregion script-init | ||
|
||
console.log('Script request should be defined', scriptRequest); |
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 @@ | ||
// #region full | ||
import { bn, Provider, Wallet } from 'fuels'; | ||
|
||
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; | ||
import { ScriptMainArgs } from '../typegend'; | ||
|
||
const provider = await Provider.create(LOCAL_NETWORK_URL); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
|
||
const foo = 3; | ||
|
||
const scriptInstance = new ScriptMainArgs(wallet); | ||
|
||
const { waitForResult } = await scriptInstance.functions.main(foo).call(); | ||
|
||
const { value, logs } = await waitForResult(); | ||
// #endregion full | ||
|
||
console.log('value', value?.toString() === bn(foo).toString()); | ||
console.log('logs', JSON.stringify(logs) === JSON.stringify(['u8 foo', 3])); |
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,6 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
license = "Apache-2.0" | ||
name = "call-test-script" | ||
|
||
[dependencies] |
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,27 @@ | ||
script; | ||
|
||
fn log<T>(v: T) { | ||
asm(r1: v) { | ||
log r1 zero zero zero; | ||
} | ||
} | ||
|
||
fn logd<T>(v: T) { | ||
asm(r1: v, r2: __size_of::<T>()) { | ||
logd zero zero r1 r2; | ||
} | ||
} | ||
|
||
struct MyStruct { | ||
arg_one: bool, | ||
arg_two: u64, | ||
} | ||
|
||
fn main(my_struct: MyStruct) -> MyStruct { | ||
log(my_struct.arg_one); | ||
log(my_struct.arg_two); | ||
MyStruct { | ||
arg_one: my_struct.arg_one, | ||
arg_two: my_struct.arg_two, | ||
} | ||
} |
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 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "script-main-args" | ||
|
||
[dependencies] |
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,11 @@ | ||
// #region script-with-main-args | ||
script; | ||
|
||
use std::logging::log; | ||
|
||
fn main(foo: u8) -> u8 { | ||
log(__to_str_array("u8 foo")); | ||
log(foo); | ||
foo | ||
} | ||
// #endregion script-with-main-args |
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