Skip to content

Commit

Permalink
Merge branch 'master' into tb/chore-remove-deprecated-method
Browse files Browse the repository at this point in the history
  • Loading branch information
tibi77 authored Apr 25, 2023
2 parents 092987c + dadc576 commit 81c97a2
Show file tree
Hide file tree
Showing 133 changed files with 2,780 additions and 250 deletions.
2 changes: 2 additions & 0 deletions .changeset/tidy-crabs-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
6 changes: 0 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
---
layout: default
title: "Contributing"
nav_order: -3
---

## 💚 Contributing To Fuel TypeScript SDK

Thanks for your interest in contributing to the Fuel TypeScript SDK! This document outlines the process for installing dependencies, setting up for development, and conventions for contributing.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- [Documentation](https://fuellabs.github.io/fuels-ts)
- [Install](#install)
- [Import](#import)
- [Interacting with Contracts](https://fuellabs.github.io/fuels-ts/guide/contracts/interacting-with-contracts.html)
- [Interacting with Contracts](https://fuellabs.github.io/fuels-ts/guide/contracts/managing-deployed-contracts.html)
- [Generate Types from ABI](https://fuellabs.github.io/fuels-ts/guide/abi-typegen/generating-types-from-abi.html)
- [Using Generated Types](https://fuellabs.github.io/fuels-ts/guide/abi-typegen/using-generated-types.html)
- [Deploying Contracts](https://fuellabs.github.io/fuels-ts/guide/contracts/deploying-contracts)
Expand Down
5 changes: 5 additions & 0 deletions apps/docs-snippets/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @fuel-ts/docs-snippets

## 0.40.0

## 0.40.0
10 changes: 10 additions & 0 deletions apps/docs-snippets/contracts/Forc.toml
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",
]
7 changes: 7 additions & 0 deletions apps/docs-snippets/contracts/counter/Forc.toml
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]
26 changes: 26 additions & 0 deletions apps/docs-snippets/contracts/counter/src/main.sw
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
}
}
7 changes: 7 additions & 0 deletions apps/docs-snippets/contracts/echo-values/Forc.toml
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]
19 changes: 19 additions & 0 deletions apps/docs-snippets/contracts/echo-values/src/main.sw
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
30 changes: 30 additions & 0 deletions apps/docs-snippets/contracts/index.ts
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,
};
};
7 changes: 7 additions & 0 deletions apps/docs-snippets/contracts/log-values/Forc.toml
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]
18 changes: 18 additions & 0 deletions apps/docs-snippets/contracts/log-values/src/main.sw
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
7 changes: 7 additions & 0 deletions apps/docs-snippets/contracts/return-context/Forc.toml
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]
20 changes: 20 additions & 0 deletions apps/docs-snippets/contracts/return-context/src/main.sw
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()
}
}
7 changes: 7 additions & 0 deletions apps/docs-snippets/contracts/simple-token-abi/Forc.toml
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]
9 changes: 9 additions & 0 deletions apps/docs-snippets/contracts/simple-token-abi/src/main.sw
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;
}
8 changes: 8 additions & 0 deletions apps/docs-snippets/contracts/simple-token/Forc.toml
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" }
25 changes: 25 additions & 0 deletions apps/docs-snippets/contracts/simple-token/src/main.sw
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
8 changes: 8 additions & 0 deletions apps/docs-snippets/contracts/token-depositor/Forc.toml
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"}
26 changes: 26 additions & 0 deletions apps/docs-snippets/contracts/token-depositor/src/main.sw
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
15 changes: 15 additions & 0 deletions apps/docs-snippets/package.json
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 apps/docs-snippets/src/guide/contracts/call-parameters.test.ts
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
});
});
Loading

0 comments on commit 81c97a2

Please sign in to comment.