Skip to content

Commit fc7910d

Browse files
committed
add frontend
1 parent e0fbe26 commit fc7910d

File tree

3 files changed

+231
-13
lines changed

3 files changed

+231
-13
lines changed

btc-docs/how-to-deploy-and-call-a-contract/how-to-deploy-and-call-a-contract.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ This will create an artifact json file of your contract in the `/artifacts` fold
2222
Next, call [loadArtifact](../how-to-write-a-contract/built-ins.md#loadartifact) to load the json file, so you have a smart contract class ready to be instantiated.
2323
```ts
2424
import artifact from '../artifacts/mycontract.json'
25-
2625
MyContract.loadArtifact(artifact)
2726
```
2827

@@ -217,21 +216,16 @@ In general, if your `@method` needs `Sig`-typed arguments, you could obtain them
217216
Here is the complete sample code for the deployment and call of a P2PKH contract.
218217

219218
```ts
220-
import { P2PKH } from './src/contracts/p2pkh'
219+
import { P2PKH } from 'p2pkh'
221220
import * as dotenv from 'dotenv'
222221
import { getDefaultProvider, getDefaultSigner } from './tests/utils/txHelper';
223-
import { readArtifact } from '@scrypt-inc/scrypt-ts-transpiler-btc';
224222
import { call, Covenant, deploy, hash160, IExtPsbt, PubKey, toXOnly, uint8ArrayToHex } from '@scrypt-inc/scrypt-ts-btc';
225223
import { address as Address } from '@scrypt-inc/bitcoinjs-lib';
226224
// Load the .env file
227225
dotenv.config()
228226

229227
async function main() {
230228

231-
232-
const artifact = readArtifact(P2PKH);
233-
P2PKH.loadArtifact(artifact)
234-
235229
// setup signer
236230
const signer = getDefaultSigner();
237231

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
---
2+
sidebar_position: 8
3+
---
4+
5+
# How to integrate with a front-end
6+
7+
This section will show how to integrate your smart contract to a front-end, so users can interact with it.
8+
We assume that you already have the basic knowledge of front-end 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.
9+
10+
## Create a project
11+
12+
First, create the root directory of the project.
13+
14+
```bash
15+
mkdir scrypt-web-example
16+
cd scrypt-web-example
17+
```
18+
19+
### Create a contract project
20+
21+
Create a `Helloworld` project and build it:
22+
23+
```bash
24+
npx @scrypt-inc/cli-btc project helloworld
25+
cd helloworld
26+
npm run build
27+
```
28+
29+
See the [helloworld tutorial](../tutorials/hello-world.md)
30+
31+
### Create a frontend project
32+
33+
34+
Create your front-end project using React, Next, Vue, Angular, or Svelte.
35+
36+
### React
37+
38+
Run the following command to create a [React](https://react.dev/) project named `helloworld`.
39+
40+
```bash
41+
cd ..
42+
npx create-react-app frontend --template typescript
43+
```
44+
45+
![](../../static/img/react-scaffold.png)
46+
47+
We will do most work under the `src` directory.
48+
49+
### Next.js
50+
51+
Run the following command to create a [Next.js](https://nextjs.org/) project.
52+
53+
```bash
54+
npx create-next-app frontend --typescript --use-npm
55+
```
56+
57+
![](../../static/img/create-next-app.png)
58+
59+
### Vue.js
60+
61+
#### Vite
62+
63+
Run the following command to create a [Vue](https://vuejs.org/) 3.x project bundled with [Vite](https://vitejs.dev/).
64+
65+
```bash
66+
npm create vue@3
67+
```
68+
69+
![](../../static/img/create-vue3-vite-app.png)
70+
71+
If you'd like to use Vue 2.x, run the following command to initialize the project scaffold.
72+
73+
```bash
74+
npm create vue@2
75+
```
76+
77+
![](../../static/img/create-vue2-vite-app.png)
78+
79+
#### Webpack
80+
81+
Run the following command to create a [Vue](https://vuejs.org/) project bundled with [Webpack](https://webpack.js.org/).
82+
83+
```bash
84+
npx @vue/cli create frontend
85+
```
86+
87+
:::tip
88+
Vue 3.x and 2.x bundled with Webpack are both supported.
89+
:::
90+
91+
When setting up the project, select `Manually select features` and enable TypeScript.
92+
93+
![](../../static/img/vue-cli-1.png)
94+
95+
![](../../static/img/vue-cli-2.png)
96+
97+
### Angular
98+
99+
Run the following command to create an [Angular](https://angular.io/) project.
100+
101+
```bash
102+
npx @angular/cli new frontend
103+
```
104+
105+
![](../../static/img/create-angular-app.png)
106+
107+
### Svelte
108+
109+
Run the following command to create a [Svelte](https://svelte.dev/) project.
110+
111+
```bash
112+
npm create svelte@latest frontend
113+
```
114+
115+
![](../../static/img/create-svelte-app.png)
116+
117+
:::note
118+
Currently, we support front-end frameworks [React](https://react.dev), [Next.js](https://nextjs.org/), [Vue](https://vuejs.org/), [Angular](https://angular.io/), and [Svelte](https://svelte.dev/). We anticipate to add support for other frameworks over time.
119+
:::
120+
121+
## Install the contract dependency
122+
123+
```bash
124+
cd frontend
125+
npm install @scrypt-inc/scrypt-ts-btc
126+
npm install ../helloworld
127+
```
128+
129+
This command installs the dependencies and configures the contract development environment.
130+
After this, we are ready to go!
131+
132+
## Integrate Wallet
133+
134+
You will integrate [unisat](https://unisat.io), a browser extension wallet, similar to [MetaMask](https://metamask.io/), into the project.
135+
136+
To request access to the wallet, you can use its APIs to create a signer:
137+
138+
```ts
139+
declare global {
140+
interface Window {
141+
unisat: UnisatAPI
142+
}
143+
}
144+
const signer = new UnisatSigner(window.unisat);
145+
```
146+
147+
create a provider:
148+
149+
```ts
150+
const provider = new MempoolProvider('fractal-testnet')
151+
```
152+
153+
create a psbt and use signer and provider to signer and deploy it:
154+
155+
```ts
156+
const address = await signer.getAddress();
157+
const psbt = new ExtPsbt();
158+
const utxos = await provider.getUtxos(address);
159+
const feeRate = await provider.getFeeRate();
160+
psbt.addUTXO(utxos)
161+
.addCovenantOutput(covenant, satoshis)
162+
.change(address, feeRate);
163+
164+
// sign the psbts
165+
const [signedPsbtHex] = await signer.signPsbts(psbt.psbtOptions());
166+
167+
// combine and finalize the signed psbts
168+
const signedPsbt = ExtPsbt.fromHex(signedPsbtHex).finalizeAllInputs();
169+
170+
// broadcast the psbts
171+
const deployTx = signedPsbt.extractTransaction();
172+
await provider.broadcast(deployTx.toHex());
173+
```
174+
175+
the scrypt SDK `@scrypt-inc/scrypt-ts-btc` provide `deploy` and `call` features, just use them in `App.tsx`:
176+
177+
```ts
178+
import React from 'react';
179+
import logo from './logo.svg';
180+
import './App.css';
181+
import { call, Covenant, deploy, MempoolProvider, sha256, toByteString, UnisatAPI, UnisatSigner } from '@scrypt-inc/scrypt-ts-btc';
182+
import { Helloworld } from 'helloworld';
183+
184+
declare global {
185+
interface Window {
186+
unisat: UnisatAPI
187+
}
188+
}
189+
190+
async function deployAndCall() {
191+
192+
const covenant = Covenant.createCovenant(new Helloworld(sha256(toByteString("hello world", true))))
193+
194+
const provider = new MempoolProvider('fractal-testnet')
195+
const signer = new UnisatSigner(window.unisat);
196+
197+
const deployTx = await deploy(signer, provider, covenant);
198+
199+
console.log(`Helloworld contract deployed: ${deployTx.getId()}`)
200+
201+
const callTx = await call(signer, provider, covenant, {
202+
invokeMethod: (contract: Helloworld) => {
203+
contract.unlock(toByteString('hello world', true));
204+
},
205+
});
206+
207+
console.log('Helloworld contract `unlock` called: ', callTx.getId());
208+
}
209+
210+
function App() {
211+
return (
212+
<div className="App">
213+
<header className="App-header">
214+
<img src={logo} className="App-logo" alt="logo" />
215+
<button onClick={deployAndCall}>Deploy and Call</button>
216+
</header>
217+
</div>
218+
);
219+
}
220+
221+
export default App;
222+
```
223+
224+
Afterwards, you can interact with the contract from the front-end.
225+
226+
227+
Go to the [sCrypt Academy](https://academy.scrypt.io) to see a step-by-step guide on how to build a Tic-Tac-Toe game on chain.

btc-docs/tutorials/hello-world.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ This command will generate a contract artifact file at `/artifacts/helloworld.js
7575

7676

7777
```ts
78-
const artifact = readArtifact(Helloworld);
78+
import artifact from '../artifacts/contracts/helloworld.json'
7979
Helloworld.loadArtifact(artifact)
8080
```
8181

@@ -110,15 +110,12 @@ Next, start deploying and calling the contract:
110110
For this example, overwrite `deploy.ts` in the root of the project with the following code to deploy and call the `Helloworld` contract:
111111

112112
```ts
113-
import { Helloworld } from './src/contracts/helloworld'
113+
import { Helloworld } from 'helloworld'
114114
import { getDefaultProvider, getDefaultSigner } from './tests/utils/txHelper';
115-
import { readArtifact } from '@scrypt-inc/scrypt-ts-transpiler-btc';
116115
import { call, Covenant, deploy, sha256, toByteString } from '@scrypt-inc/scrypt-ts-btc';
117116

118117
(async () => {
119118

120-
const artifact = readArtifact(Helloworld);
121-
Helloworld.loadArtifact(artifact)
122119
const covenant = Covenant.createCovenant(new Helloworld(sha256(toByteString("hello world", true))))
123120

124121
const provider = getDefaultProvider();
@@ -142,7 +139,7 @@ import { call, Covenant, deploy, sha256, toByteString } from '@scrypt-inc/scrypt
142139
Run the following command to deploy AND call our example contract.
143140

144141
```
145-
npx ts-node deploy.ts
142+
npx tsx deploy.ts
146143
```
147144

148145
You will see some output like:

0 commit comments

Comments
 (0)