-
Notifications
You must be signed in to change notification settings - Fork 13
/
App.tsx
220 lines (205 loc) · 6.17 KB
/
App.tsx
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import "./App.css";
import "@massalabs/react-ui-kit/src/global.css";
import { useEffect, useState } from "react";
import { IAccount, providers } from "@massalabs/wallet-provider";
import {
ClientFactory,
Args,
Client,
bytesToStr,
fromMAS,
} from "@massalabs/massa-web3";
const CONTRACT_ADDRESS =
"AS12S1xuDVdpTMGhs19iBo3HXRQqZZaFzSF1hkz1RdF5YxqqVvTnb";
const coins = 0.1;
const operationDelayNotice =
"If there's no change, please wait a few seconds before clicking again. Confirmation of the operation may take up to 16 seconds.";
const errorMessageNotEnoughBalance = `You need a minimum of ${coins} MAS to set the age.`;
const errorMessageNotEnoughCoins = "enough balance to pay for the coins";
enum Provider {
MASSASTATION = "MASSASTATION",
BEARBY = "BEARBY",
}
function App() {
const [client, setClient] = useState<Client>();
const [provider, setProvider] = useState<Provider>();
const [account, setAccount] = useState<IAccount>();
const [lastOpId, setLastOpId] = useState<string>();
const [ageResult, setAgeResult] = useState<string>();
const [inputAge, setInputAge] = useState<number>(0);
const [inputName, setInputName] = useState<string>();
const [errorMessage, setErrorMessage] = useState<any>();
const initialize = async (providerName: Provider) => {
setErrorMessage(null);
const providersList = await providers(true, 10000);
const selectedProvider = providersList.find(
(p) => p.name() === providerName
);
if (!selectedProvider) {
setErrorMessage(
"Please install MassaStation and the plugin Massa Wallet in it and refresh."
);
return;
}
const accounts = await selectedProvider.accounts();
if (accounts.length === 0) {
setErrorMessage("No accounts found");
return;
}
setClient(
await ClientFactory.fromWalletProvider(selectedProvider, accounts[0])
);
setProvider(providerName);
setAccount(accounts[0]);
};
useEffect(() => {
initialize(Provider.MASSASTATION);
}, []);
const updateAge = (newAge: number) => async () => {
setErrorMessage(null);
if (!client) return;
if (!inputName) {
setErrorMessage("Please enter a name");
return;
}
try {
let args = new Args().addString(inputName).addU32(newAge);
let opId = await client.smartContracts().callSmartContract({
targetAddress: CONTRACT_ADDRESS,
functionName: "changeAge",
parameter: args,
maxGas: BigInt(1000000),
coins: fromMAS(coins),
fee: BigInt(0),
});
setLastOpId(opId);
} catch (error: any) {
if (error.message.includes(errorMessageNotEnoughCoins)) {
setErrorMessage(errorMessageNotEnoughBalance);
}
}
};
const getAge = async () => {
setErrorMessage(null);
if (!client) return;
if (!inputName) {
setErrorMessage("Please enter a name");
return;
}
try {
let res = await client.smartContracts().readSmartContract({
maxGas: BigInt(1000000),
targetAddress: CONTRACT_ADDRESS,
targetFunction: "getAge",
parameter: new Args().addString(inputName).serialize(),
});
setAgeResult(bytesToStr(res.returnValue));
} catch (error) {
console.error(error);
}
};
return (
<div className="bodyContent">
<SelectProvider
selectedProvider={provider}
onClick={(providerName) => {
initialize(providerName);
}}
/>
{account && (
<div className="wrapper">
<h1 className="messageToDisplay">Age Example: React</h1>
<div>
<InfoWrapper
address={account.address()}
provider={provider || ""}
lastOpId={lastOpId}
/>
<div className="action-wrapper">
<div className="input-wrapper">
<input
id="name"
type="text"
value={inputName}
placeholder="name"
onChange={(e) => {
setInputName(e.target.value);
setAgeResult(undefined);
}}
/>
<input
id="age"
type="number"
placeholder="age"
value={inputAge}
onChange={(e) => setInputAge(parseInt(e.target.value))}
/>
</div>
<div className="action-button-wrapper">
<button id="btnChangeAge" onClick={updateAge(inputAge)}>
Change age of {inputName === "" ? "..." : inputName}
</button>
<button id="btnGetAge" onClick={getAge}>
Get age of {inputName === "" ? "..." : inputName}
</button>
</div>
{ageResult !== undefined && (
<div id="result">
<p>
Age of {inputName} is {ageResult}
</p>
<p>{operationDelayNotice}</p>
</div>
)}
</div>
</div>
</div>
)}
{errorMessage && <div className="error-message">{errorMessage}</div>}
</div>
);
}
export default App;
const SelectProvider = ({
onClick,
selectedProvider,
}: {
onClick: (providerName: Provider) => void;
selectedProvider?: Provider;
}) => {
return (
<div className="select-provider">
<button
className="select-provider__button"
onClick={() => onClick(Provider.MASSASTATION)}
disabled={selectedProvider === Provider.MASSASTATION}
>
Use Massa Station
</button>
<button
className="select-provider__button"
onClick={() => onClick(Provider.BEARBY)}
disabled={selectedProvider === Provider.BEARBY}
>
Use Bearby
</button>
</div>
);
};
const InfoWrapper = ({
address,
provider,
lastOpId,
}: {
address: string;
provider: string;
lastOpId?: string;
}) => {
return (
<div className="info-wrapper">
<h3 id="address">Your address: {address}</h3>
<h3 id="provider">Provider: {provider}</h3>
{lastOpId && <h4 id="lastOpId">Last Op id: {lastOpId}</h4>}
</div>
);
};