Interagindo com contratos
No tutorial anterior aprendemos a se conectar com a blockchain Bywise. Agora para interagirmos com um smartcontract na rede da Bywise é muito simples.
Código completo aqui (opens in a new tab).
Vamos considerar o seguinte contrato.
import BywiseUtils, { StorageValue } from 'bywise-utils.js';
class StorageContract {
value = new StorageValue('');
setValue(newValue) {
this.value.set(newValue);
}
getValue() { // @view
return this.value.get();
}
}
BywiseUtils.exportContract(new StorageContract());
Criamos o botão Try Connect
que tenta realizar a conexão com carteira do usuário. Depois disso, nós buscamos algumas informações do endereço conectado.
const chain = 'mainnet'
const provider = new BywiseProvider(chain);
const contractAddress = 'BWS000000000000000000000000000000000000000000000'; // your contract address
const connect = async () => {
const userInfo = await provider.connect();
if (userInfo) {
setConnect(true);
updateValue(); // call update value
}
}
Agora vamos ler o valor da variável do contrato.
const updateValue = async () => {
const fullOutput = await provider.web3.contracts.readContract(chain, contractAddress, 'getValue', []);
if (fullOutput.error) {
alert(`error ${fullOutput.error}`);
} else {
const contractReturnValue = fullOutput.output;
setValue(contractReturnValue);
}
}
Agora vamos interagir com o contrato setando o valor para a variável value
.
const setValueButton = async () => {
const result = await provider.send({
to: [contractAddress],
amount: ['0'],
type: TxType.TX_CONTRACT_EXE,
data: [
{
method: 'setValue',
inputs: [`I believe I can fly - ${Math.floor(Math.random() * 1000)}`],
},
]
});
updateValue(); // call update value
}
Vamos ver o código completo.
import Head from 'next/head'
import styles from '../styles/Home.module.css'
import { useState } from 'react';
import BywiseProvider from '@bywise/provider';
import { TxType } from '@bywise/web3';
import { useRouter } from 'next/router';
const chain = 'testnet'
const provider = new BywiseProvider(chain);
const contractAddress = 'BWS1MC637abce1D5263e36FbBa6bCe77a01e764B4A65e1e68';
export default function Home() {
const router = useRouter();
const [connected, setConnect] = useState(false);
const [value, setValue] = useState('');
const connect = async () => {
const userInfo = await provider.connect();
if (userInfo) {
setConnect(true);
updateValue(); // call update update value
}
}
const updateValue = async () => {
const fullOutput = await provider.web3.contracts.readContract(chain, contractAddress, 'getValue', []);
if (fullOutput.error) {
alert(`error ${fullOutput.error}`);
} else {
const contractReturnValue = fullOutput.output;
setValue(contractReturnValue);
}
}
const setValueButton = async () => {
const result = await provider.send({
to: [contractAddress],
amount: ['0'],
type: TxType.TX_CONTRACT_EXE,
data: [
{
method: 'setValue',
inputs: [`I believe I can fly - ${Math.floor(Math.random() * 1000)}`],
},
]
});
updateValue(); // call update update value
}
return (
<div className={styles.container}>
<Head>
<title>Web3 Example</title>
<meta name="description" content="Bywise web3 provider example" />
<link rel="icon" href={`${router.basePath}/favicon.ico`} />
</Head>
<main className={styles.main}>
<img className={styles.bywise_logo} src={`${router.basePath}/favicon.ico`} alt='Blockchain Bywise Logo' />
<h1 className={styles.title}>
Welcome to <a target="_blank" href="https://bywise.org" rel="noopener noreferrer">Bywise</a>
</h1>
{connected && <>
<p className={styles.description}>
<span>
{'Contract Value: '}
<code className={styles.code}>"{value}"</code>
</span>
</p>
</>}
<div className={styles.grid} >
{connected && <>
<button className={styles.card} onClick={setValueButton}>
<h2>Update Value 🔥</h2>
</button>
</>}
{!connected && <button className={styles.card} onClick={connect}>
<h2>Connect Wallet 🚀</h2>
</button>}
</div>
</main>
<footer className={styles.footer}>
<a
href="https://develblockchain.com"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span className={styles.logo}>
<img src={`${router.basePath}/devel.svg`} alt='Devel Blockchain Logo' height={40} />
</span>
</a>
</footer>
</div>
)
}