Spaces:
Runtime error
Runtime error
File size: 3,748 Bytes
711e9c6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
---
sidebar_position: 8
---
# How to Integrate With a Frontend
This section will show how to integrate your smart contract to a frontend, so users can interact with it.
We use [React](https://reactjs.org/) as our frontend framework as an example. We assume that you already have the basic knowledge of frontend development, so we will not spend much time introducing this part of the code, but mostly be focusing on how to interact with the smart contract in the front end project.
:::note
Currently, the only supported frontend frameworks is React. We anticipate to add supports for other frameworks over time.
:::
## Setup
### React
Run the following command to create a React project, named `helloworld`.
```bash
npx create-react-app helloworld --template typescript
```

We will do most work under the `src` directory.
### `sCrypt`
Run the `init` command of the [CLI](../installation.md#the-scrypt-cli-tool) to turn it into an `sCrypt` project.
```bash
cd helloworld
npx scrypt-cli init
```
This installs all the dependencies and configs the contract development environment.
After this, we are ready to go!
## Load Contract
Before interacting with a smart contract at the front end, we need to load the contract class in two steps.
We'll take a look at how to generate the artifact by ourselves first.
### 1. Compile Contract
Before you start, you need to get the contract source files, as a frontend developer.
Let's use the [Helloworld contract](../tutorials/hello-world.md) as an example. Copy and paste `helloworld.ts` into the `src/contracts` directory.

Run the following command to compile the contract.
```bash
npx scrypt-cli compile
```

After the compilation, you will get an JSON artifact file at `artifacts/src/contracts/helloworld.json`.

### 2. Load Artifact
Now with the contract artifact file, you directly load it in the `index.tsx` file.
```ts
import { Helloworld } from './contracts/helloworld';
import artifact from '../artifacts/src/contracts/helloworld.json';
Helloworld.loadArtifact(artifact);
```
Now you can create an instance from the contract class as before.
```ts
const message = toByteString('hello world', true)
const instance = new Helloworld(sha256(message))
```
:::info
You cannot simply call `Helloworld.compile()` at the front end, since it only works in NodeJS, not in browser.
:::
## Integrate Wallet
You will integrate [Sensilet](https://sensilet.com/), a browser extension wallet similar to [MetaMask](https://metamask.io/), into the project.
:::info
You can refer to this [guide](../advanced/how-to-add-a-signer.md) to add support for other wallets.
:::
To request access to the wallet, you can use its `requestAuth` method.
```ts
const provider = new DefaultProvider({
network: bsv.Networks.testnet
});
const signer = new SensiletSigner(provider);
// request authentication
const { isAuthenticated, error } = await signer.requestAuth();
if (!isAuthenticated) {
// something went wrong, throw an Error with `error` message
throw new Error(error);
}
// authenticated
// you can show user's default address
const userAddress = await signer.getDefaultAddress();
// ...
```
Now you can connect the wallet to the contract instance as before.
```ts
await instance.connect(signer);
```
Afterwards, you can interact with the contract from the front end by [calling its method](../how-to-deploy-and-call-a-contract/how-to-deploy-and-call-a-contract.md#contract-call) as usual.
Go [here](https://academy.scrypt.io) to see a full example on how to build a Tic-Tac-Toe game on chain.
|