Skip to content

Commit

Permalink
Merge branch 'master' into ql/panic-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
QuinnLee committed Aug 22, 2022
2 parents 7bb2915 + dce0a9b commit a9516f3
Show file tree
Hide file tree
Showing 55 changed files with 32,518 additions and 662 deletions.
6 changes: 6 additions & 0 deletions .changeset/tasty-geese-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/contract": patch
---

Use Sparse Merkle Tree to calculate `storageSlots` and use it to calculate `contractId`
Upgrade to sway 0.19.2
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ packages/typechain-target-fuels/example
packages/providers/src/__generated__
out
CHANGELOG.md

# Docs files
docs/examples/
_includes/js/custom.js
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ dist
.coverage_*
coverage
node_modules
docs/examples/
/packages/example-contract/src/example-contract-types/
/packages/typechain-target-fuels/static/
/packages/typechain-target-fuels/example/
Expand Down
281 changes: 77 additions & 204 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,260 +10,133 @@
[![docs](https://img.shields.io/badge/docs-fuels.ts-brightgreen.svg?style=flat)](https://fuellabs.github.io/fuels-ts/)
[![discord](https://img.shields.io/badge/chat%20on-discord-orange?&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/xfpK4Pe)

# Table of contents

- [SDK documentation](https://fuellabs.github.io/fuels-ts/)
- [Features](#features)
- [Usage](#usage)
- [Installation](#installation)
- [Calling Contracts](#calling-contracts)
- [Deploying Contracts](#deploying-contracts)
- [Generating Contract Types](#generating-contract-types)
- [Contributing](#contributing)
- [Setup](#setup)
- [Testing](#testing)
## Table of contents

- [Quickstart](https://fuellabs.github.io/fuels-ts/QUICKSTART.html)
- [Documentation](https://fuellabs.github.io/fuels-ts)
- [Install](#install)
- [Import](#import)
- [Calling Contracts](#calling-contracts)
- [Generate Contract Types from ABI](#generate-contract-types-from-abi)
- [Dependencies](#dependencies)
- [Generate Types](#generate-types)
- [Using Generated Types](#using-generated-types)
- [Deploying Contracts](#deploying-contracts)
- [Contributing](./docs/CONTRIBUTING.md)
- [License](#license)

## Features
## Quickstart

- [x] Deploy and call contracts
- [x] Deploy and activate predicates
- [x] Generate contract types with TypeChain
- [x] Build and send transactions
- [x] Encode/decode contract ABI
- [x] Transfer coins
- [ ] Inspect contract storage
- [x] Manage wallets
- [x] Run scripts
- [ ] Query and subscribe to events
We recommend to start on [Quickstart](https://fuellabs.github.io/fuels-ts/QUICKSTART.html) to speed-up and build your first DApp using Fuel.

## Usage
- [Quickstart](https://fuellabs.github.io/fuels-ts/QUICKSTART.html)
- [Other example projects](https://github.com/FuelLabs/sway-applications)

### Installation
## Documentation

```sh
yarn add fuels
# or
npm add fuels
```

### Calling Contracts

```ts
// typescript file
import { Wallet, Contract } from "fuels";
import abi from "./abi.json";

const wallet = new Wallet("0x..."); // private key with coins
const contractId = "0x...";
const contract = new Contract(contractId, abi, wallet);
const { value } = await contract.functions.foo<[string], bigint>("bar").call();

console.log(value);
```
Find more information about packages in our [Documentation](https://fuellabs.github.io/fuels-ts).

[[READ MORE]](./packages/contract/README.md)
## The Fuel Ecosystem

### Deploying Contracts
Learn more about the Fuel Ecosystem.

```ts
// typescript file
import { Provider, Contract } from "fuels";
import bytecode from "./bytecode.bin";

const factory = new ContractFactory(bytecode, [], wallet);
const contract = await factory.deployContract(factory);

console.log(contract);
```
- [🌴 Sway](https://fuellabs.github.io/sway/) the new language. Empowering everyone to build reliable and efficient smart contracts.
- [🧰 Forc](https://fuellabs.github.io/sway/v0.19.2/forc/index.html) the Fuel toolbox. Build, deploy and manage your sway projects.
- [⚙️ Fuel Core](https://github.com/FuelLabs/fuel-core) the new FuelVM, a blazingly fast blockchain VM.
- [🔗 Fuel Specs](https://github.com/FuelLabs/fuel-specs) the Fuel protocol specifications.
- [🦀 RUST SDK](https://github.com/FuelLabs/fuels-rs) a robust SDK in rust.
- [⚡ Fuel Network](https://fuel.network/) the project.

### Generating Contract Types
## Install

For defined Sway types, we offer a Typechain target to generate types from an ABI file.

For Sway types, the mapping is as follows to Typescript types:

| Sway type | Typescript type |
| --------- | ---------------------------------- |
| u8/64 | number or BigNumber |
| b256 | '0x'-prefixed-string or Uint8Array |
| str[] | '0x'-prefixed-string or Uint8Array |
| struct | Object |
| enum | Enum<T> |
| tuple | Array |
#### YARN

```sh
# console
yarn add -D typechain typechain-target-fuels
yarn exec typechain --target=fuels --out-dir=types abi.json
```

```ts
// typescript file
import { Provider } from "fuels";
import { MyContract__factory } from "./types";

const provider = new Provider("http://127.0.0.1:4000/graphql");

const contractId = "0x...";
const contract = MyContract__factory.connect(contractId, provider);
yarn add fuels
```

## Contributing

### Setup
#### NPM

```sh
git clone git@github.com:FuelLabs/fuels-ts.git
cd fuels-ts
pnpm install
pnpm build
npm install fuels --save
```

### Testing

In order to run tests locally, you need `fuel-core` running as a docker container.
To do that you can run these commands in your terminal:
## Import

```sh
pnpm services:run
```
```ts
import { Wallet } from "fuels";

And then run tests in another terminal tab:
// Random Wallet
console.log(Wallet.generate());

```sh
# run all tests
pnpm test
# run tests and get coverage
pnpm test:coverage
# run tests for a specific package
pnpm --filter @fuel-ts/contract run test
// Using privateKey Wallet
console.log(new Wallet("0x0000...0000"));
```

Or if you want to run docker and all tests serially you can do:

```sh
pnpm ci:test
```
## Calling Contracts

This will run `services:run`, `test` and then `services:clean`

> Some times if you're running your tests locally using `services:run` in a separated terminal,
> maybe you need to run `services:clean` after tests to clean docker containers and volumes. Because
> this can break your tests sometimes!
### Commit Convention

Before you create a Pull Request, please check whether your commits comply with
the commit conventions used in this repository.

When you create a commit we kindly ask you to follow the convention
`category(scope or module): message` in your commit message while using one of
the following categories:

- `feat / feature`: all changes that introduce completely new code or new
features
- `fix`: changes that fix a bug (ideally you will additionally reference an
issue if present)
- `refactor`: any code related change that is not a fix nor a feature
- `docs`: changing existing or creating new documentation (i.e. README, docs for
usage of a lib or cli usage)
- `build`: all changes regarding the build of the software, changes to
dependencies or the addition of new dependencies
- `test`: all changes regarding tests (adding new tests or changing existing
ones)
- `ci`: all changes regarding the configuration of continuous integration (i.e.
github actions, ci system)
- `chore`: all changes to the repository that do not fit into any of the above
categories

### Steps to PR

1. Fork the fuels-ts repository and clone your fork

2. Create a new branch out of the `master` branch.

3. Make and commit your changes following the
[commit convention](https://github.com/FuelLabs/fuels-ts/blob/master/README.md#commit-convention).
As you develop, you can run `pnpm build` and
`pnpm test` to make sure everything works as expected.

4. Run `pnpm changeset` to create a detailed description of your changes. This
will be used to generate a changelog when we publish an update.
[Learn more about Changeset](https://github.com/changesets/changesets/tree/main/packages/cli).
Please note that you might have to run `git fetch origin master` (where
origin will be your fork on GitHub) before `pnpm changeset` works.

> If you made minor changes like CI config, prettier, etc, you can run
> `pnpm changeset add --empty` to generate an empty changeset file to document
> your changes.
```ts
import { Wallet, Contract } from "fuels";
import abi from "./abi.json";

## Build and watch all packages
const wallet = new Wallet("0x..."); // private key with coins
const contractId = "0x...";
const contract = new Contract(contractId, abi, wallet);

If you want to work locally using realtime builds, open in one terminal tab build in watch mode
on all packages from the root directory:
// All contract methods are available under functions
// with the correct types
const { transactionId, value } = await contract.functions
.foo<[string], bigint>("bar")
.call();

```sh
pnpm build:watch
console.log(transactionId, value);
```

This command you run `tsup --watch` on all packages using Turborepo

## Using linked packages
[READ MORE](./packages/contract/README.md)

This will link all packages inside our monorepo in your `global pnpm store`, enabling you to us `fuels-ts` packages via links in
your local projects.
## Generate Contract Types from ABI

#### On `fuels-ts` root directory
#### Dependencies

```sh
pnpm -r exec pnpm link --global --dir ./
yarn add -D typechain typechain-target-fuels
```

You can use [build watch](#build-and-watch-all-packages).

#### On `your project` root directory
#### Generate Types

```sh
pnpm link --global fuels
yarn exec typechain --target=fuels --out-dir=types abi.json
```

Or for specfic packages just use `pnpm link @fuel-ts/<pkg-name>`, ex;

```
pnpm link --global @fuel-ts/wallet
```
#### Using Generated Types

### Troubleshooting
```ts
import { Wallet } from "fuels";
import { MyContract__factory } from "./types";

If you're linking for the first time, you might need:
const contractId = "0x...";
const wallet = new Wallet("0x...");
const contract = MyContract__factory.connect(contractId, wallet);

```sh
pnpm setup
// All contract methods are available under functions
// with the correct types
const { transactionId, value } = await contract.functions.my_fn(1n).call();
console.log(transactionId, value);
```

If it still have problems, you might need to setup again (As `pnpm` releases new version, the global folder structure may change)

```sh
pnpm setup
```
## Deploying Contracts

## Updating Forc version
```ts
import { Provider, Contract } from "fuels";
// Byte code generated using: forc build
import bytecode from "./bytecode.bin";

The following script will upgrade Forc to the latest version on GitHub, remove all lockfiles so the latest stdlib can be used, and rebuild all projects:
const factory = new ContractFactory(bytecode, [], wallet);
const contract = await factory.deployContract(factory);

```sh
pnpm forc:update
console.log(contract.id);
```

After this you should run tests and fix any incompatibilities.

## License

The primary license for this repo is `Apache 2.0`, see [`LICENSE`](./LICENSE).

## FAQ

### Why is the prefix `fuels` and not `fuel`?

In order to make the SDK for Fuel feel familiar with those coming from the [ethers.js](https://github.com/ethers-io/ethers.js) ecosystem, this project opted for an `s` at the end. The `fuels-*` family of SDKs is inspired by The Ethers Project.
9 changes: 9 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Gem files
Gemfile.lock

# Jekyll files
.jekyll-cache
_site

# examples node_modules
node_modules
Loading

0 comments on commit a9516f3

Please sign in to comment.