function increment() {
const [accounts, setAccounts] = React.useState(null);
const [txHash, setTxHash] = React.useState(null);
const [contractAddress, setContractAddress] = React.useState(null);
const [isSubmit, setIsSubmit] = React.useState(false);
async function handleGetAccount() {
const account = await dapp.request('near', {
method: 'dapp:accounts',
});
setAccounts(account['near']);
}
async function handleSendTransaction() {
const rpc = 'https://rpc.testnet.near.org';
const provider = new providers.JsonRpcProvider(rpc);
const accessKey = await provider.query(`access_key/${accounts.address}/${accounts.pubKey}`, '');
const recentBlockHash = utils.serialize.base_decode(accessKey.block_hash);
const actions = [
transactions.functionCall(
'increment',
{},
new BN('30000000000000'),
new BN(0),
),
];
const transaction = transactions.createTransaction(
accounts.address,
utils.PublicKey.fromString(accounts.pubKey),
contractAddress,
accessKey.nonce + 1,
actions,
recentBlockHash,
);
const serializedTransaction = Buffer.from(transaction.encode()).toString('hex');
const txHash = await dapp.request('near', {
method: 'dapp:signAndSendTransaction',
params: [`0x${serializedTransaction}`],
});
setTxHash(txHash[0]);
}
const handleChange = (e) => {
setContractAddress(e.target.value);
};
const handleContractId = () => {
if (
contractAddress.includes('near') ||
contractAddress.includes('testnet') ||
contractAddress.includes('devnet')
) {
setIsSubmit(true);
} else {
alert('You have to enter valid contract address');
}
};
return (
<>
{accounts ? (
<>
{isSubmit ? (
<Button onClick={handleSendTransaction} type="button">
Send a Transaction
</Button>
) : (
<>
<Input
value={contractAddress}
onChange={handleChange}
placeholder="Deploayed Contract Address"
style={{ marginRight: '8px' }}
/>
<Button onClick={handleContractId} type="button">
Set a Contract Address
</Button>
</>
)}
<ResultTooltip style={{ background: '#3B48DF' }}>
<b>Accounts:</b> {accounts.address}
</ResultTooltip>
</>
) : (
<>
<Button onClick={handleGetAccount} type="button">
Get Account
</Button>
<div>You have to get account first!</div>
</>
)}
{contractAddress && isSubmit && (
<ResultTooltip style={{ background: '#F4F4F4', color: 'black' }}>
<b>Contract Address:</b> {contractAddress}
</ResultTooltip>
)}
{txHash && (
<ResultTooltip style={{ background: '#F08080' }}>
<b>Transaction Hash:</b> {txHash}
</ResultTooltip>
)}
</>
);
}