-
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.
Merge branch 'master' into tb/chore-remove-deprecated-method
- Loading branch information
Showing
133 changed files
with
2,780 additions
and
250 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
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/docs-snippets | ||
|
||
## 0.40.0 | ||
|
||
## 0.40.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[workspace] | ||
members = [ | ||
"counter", | ||
"log-values", | ||
"echo-values", | ||
"simple-token", | ||
"simple-token-abi", | ||
"return-context", | ||
"token-depositor", | ||
] |
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 = ["FuelLabs"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "counter" | ||
|
||
[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,26 @@ | ||
contract; | ||
|
||
abi Counter { | ||
#[storage(read)] | ||
fn get_count() -> u64; | ||
|
||
#[storage(write, read)] | ||
fn increment_count(amount: u64) -> u64; | ||
} | ||
|
||
storage { | ||
count: u64 = 0, | ||
} | ||
|
||
impl Counter for Contract { | ||
#[storage(read)] | ||
fn get_count() -> u64 { | ||
storage.count | ||
} | ||
|
||
#[storage(write, read)] | ||
fn increment_count(amount: u64) -> u64 { | ||
storage.count += amount; | ||
storage.count | ||
} | ||
} |
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 = ["FuelLabs"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "echo-values" | ||
|
||
[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,19 @@ | ||
// #region understanding-fuel-binary-file | ||
contract; | ||
|
||
abi EchoValues { | ||
fn echo_u8(value: u8) -> u8; | ||
|
||
fn echo_str_8(value: str[8]) -> str[8]; | ||
} | ||
|
||
impl EchoValues for Contract { | ||
fn echo_u8(value: u8) -> u8 { | ||
value | ||
} | ||
|
||
fn echo_str_8(value: str[8]) -> str[8] { | ||
value | ||
} | ||
} | ||
// #endregion understanding-fuel-binary-file |
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,30 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
export enum SnippetContractEnum { | ||
COUNTER = 'counter', | ||
ECHO_VALUES = 'echo-values', | ||
RETURN_CONTEXT = 'return-context', | ||
LOG_VALUES = 'log-values', | ||
SIMPLE_TOKEN = 'simple-token', | ||
TOKEN_DEPOSITOR = 'token-depositor', | ||
} | ||
|
||
const getSnippetContractPath = (contract: SnippetContractEnum) => | ||
join(__dirname, contract, 'out', 'debug'); | ||
|
||
const getSnippetContractAbiPath = (contract: SnippetContractEnum) => | ||
join(getSnippetContractPath(contract), `${contract}-abi.json`); | ||
|
||
const getSnippetContractBinPath = (contract: SnippetContractEnum) => | ||
join(getSnippetContractPath(contract), `${contract}.bin`); | ||
|
||
export const getSnippetContractArtifacts = (contract: SnippetContractEnum) => { | ||
const abi = JSON.parse(readFileSync(getSnippetContractAbiPath(contract), 'utf-8')); | ||
const bin = readFileSync(getSnippetContractBinPath(contract)); | ||
|
||
return { | ||
abi, | ||
bin, | ||
}; | ||
}; |
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 = ["FuelLabs"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "log-values" | ||
|
||
[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,18 @@ | ||
// #region log-1 | ||
contract; | ||
|
||
use std::logging::log; | ||
|
||
abi LogValues { | ||
fn log_values(val1: u64, val2: b256, val3: str[4], val4: [u8; 3]); | ||
} | ||
|
||
impl LogValues for Contract { | ||
fn log_values(val1: u64, val2: b256, val3: str[4], val4: [u8; 3]) { | ||
log(val1); | ||
log(val2); | ||
log(val3); | ||
log(val4); | ||
} | ||
} | ||
// #endregion log-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[project] | ||
authors = ["FuelLabs"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "return-context" | ||
|
||
[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,20 @@ | ||
contract; | ||
|
||
use std::{ | ||
context::{ | ||
msg_amount | ||
}, | ||
}; | ||
|
||
|
||
abi ReturnContext { | ||
#[payable] | ||
fn return_context_amount() -> u64; | ||
} | ||
|
||
impl ReturnContext for Contract { | ||
#[payable] | ||
fn return_context_amount() -> u64 { | ||
msg_amount() | ||
} | ||
} |
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 = ["FuelLabs"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "simple_token_abi" | ||
|
||
[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,9 @@ | ||
library simple_token_abi; | ||
|
||
abi SimpleToken { | ||
#[storage(read, write)] | ||
fn deposit(address: b256, amount: u64); | ||
|
||
#[storage(read)] | ||
fn get_balance(address: b256) -> u64; | ||
} |
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,8 @@ | ||
[project] | ||
authors = ["FuelLabs"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "simple-token" | ||
|
||
[dependencies] | ||
simple_token_abi = { path = "../simple-token-abi" } |
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,25 @@ | ||
// #region inter-contract-calls-1 | ||
contract; | ||
|
||
use ::simple_token_abi::SimpleToken; | ||
|
||
storage { | ||
balances: StorageMap<b256, u64> = StorageMap {}, | ||
} | ||
|
||
impl SimpleToken for Contract { | ||
#[storage(read, write)] | ||
fn deposit(address: b256, amount: u64) { | ||
let current_balance = storage.balances.get(address).unwrap_or(0); | ||
|
||
storage.balances.insert(address, current_balance + amount); | ||
} | ||
|
||
#[storage(read)] | ||
fn get_balance(address: b256) -> u64 { | ||
let balance = storage.balances.get(address).unwrap_or(0); | ||
|
||
balance | ||
} | ||
} | ||
// #endregion inter-contract-calls-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[project] | ||
authors = ["FuelLabs"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "token-depositor" | ||
|
||
[dependencies] | ||
simple_token_abi = { path = "../simple-token-abi"} |
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,26 @@ | ||
// #region inter-contract-calls-2 | ||
contract; | ||
|
||
use std::auth::msg_sender; | ||
|
||
use ::simple_token_abi::SimpleToken; | ||
|
||
abi TokenDepositor { | ||
fn deposit_to_simple_token(contract_id: b256, amount: u64); | ||
} | ||
|
||
impl TokenDepositor for Contract { | ||
fn deposit_to_simple_token(contract_id: b256, amount: u64) { | ||
let simple_token_contract = abi(SimpleToken, contract_id); | ||
|
||
let sender = msg_sender().unwrap(); | ||
|
||
let address: b256 = match sender { | ||
Identity::Address(sender_param) => sender_param.into(), | ||
_ => revert(0), | ||
}; | ||
|
||
simple_token_contract.deposit(address, amount); | ||
} | ||
} | ||
// #endregion inter-contract-calls-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "@fuel-ts/docs-snippets", | ||
"version": "0.40.0", | ||
"description": "", | ||
"private": true, | ||
"scripts": { | ||
"build": "pnpm forc build -p contracts" | ||
}, | ||
"devDependencies": { | ||
"fuels": "workspace:*" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
75 changes: 75 additions & 0 deletions
75
apps/docs-snippets/src/guide/contracts/call-parameters.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,75 @@ | ||
import type { Contract } from 'fuels'; | ||
import { BN, NativeAssetId, ContractFactory } from 'fuels'; | ||
|
||
import { getSnippetContractArtifacts, SnippetContractEnum } from '../../../contracts'; | ||
import { getTestWallet } from '../../utils'; | ||
|
||
describe(__filename, () => { | ||
let contract: Contract; | ||
|
||
beforeAll(async () => { | ||
const wallet = await getTestWallet(); | ||
|
||
const { abi, bin } = getSnippetContractArtifacts(SnippetContractEnum.RETURN_CONTEXT); | ||
|
||
const factory = new ContractFactory(bin, abi, wallet); | ||
|
||
contract = await factory.deployContract(); | ||
}); | ||
|
||
it('should successfully execute contract call with forwarded amount', async () => { | ||
// #region call-params-1 | ||
const amountToForward = 10; | ||
|
||
const { value } = await contract.functions | ||
.return_context_amount() | ||
.callParams({ | ||
forward: [amountToForward, NativeAssetId], | ||
}) | ||
.call(); | ||
|
||
expect(new BN(value).toNumber()).toBe(amountToForward); | ||
// #endregion call-params-1 | ||
}); | ||
|
||
it('should throw error due not enough gas', async () => { | ||
// #region call-params-2 | ||
await expect( | ||
contract.functions | ||
.return_context_amount() | ||
.callParams({ | ||
forward: [10, NativeAssetId], | ||
gasLimit: 1, | ||
}) | ||
.call() | ||
).rejects.toThrow(/OutOfGas/); | ||
// #endregion call-params-2 | ||
}); | ||
|
||
it('should successfully execute transaction with `txParams` and `callParams`', async () => { | ||
// #region call-params-3 | ||
const amountToForward = 10; | ||
const contractCallGasLimit = 100; | ||
const transactionGasLimit = 10000; | ||
|
||
const result = await contract.functions | ||
.return_context_amount() | ||
.callParams({ | ||
forward: [amountToForward, NativeAssetId], | ||
gasLimit: contractCallGasLimit, | ||
}) | ||
.txParams({ | ||
gasLimit: transactionGasLimit, | ||
}) | ||
.call(); | ||
|
||
const { | ||
transactionResult: { transaction }, | ||
value, | ||
} = result; | ||
|
||
expect(new BN(value).toNumber()).toBe(10); | ||
expect(new BN(transaction.gasLimit).toNumber()).toBe(10000); | ||
// #endregion call-params-3 | ||
}); | ||
}); |
Oops, something went wrong.