Skip to main content

Create Coin Transfer Tx

React Library

In this example, we create a coin transfer transaction to transfer coins.

1. Create Project

Create the project in the terminal as follows:

npx create-react-app locuschain-example-transfercoin --template typescript

2. Install Locus Library

Install the Locus library in the terminal as follows:

npm i @locuschain/lib

3. Edit index.tsx

Update the index.tsx file with the following content and modify the defined variables according to your environment.

import { Account, Web3, loadLocusWasm } from '@locuschain/lib';
import { useEffect, useState } from 'react';
import ReactDOM from 'react-dom/client';

const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);

function App() {
const [result, setResult] = useState<string>('executing...')
const [error, setError] = useState<string>()

useEffect(() => {
// rpcUrl must be changed to match the actual environment.
const rpcUrl = 'http://[IP_ADDRESS]/';
const web3 = new Web3(rpcUrl);

// testAccount must be changed to match the actual environment.
const testAccount = new Account({
address: 'GAST567Y42J5VZFXLNW4W...',
nsk: 'AFNZZPKVBP7AS46SSX....'
});
web3.accounts.add(testAccount);

const transferCoin = async () => {
try {
const params = {
from: testAccount.address,
to: 'QRO5DMWKPKX5RKEBAWNYJGWBKJMWGUVAURENCABGCY',
amount: '0.000001',
tokenAmounts: [],
feeType: 0,
};

const ret = await web3.provider.locus_transferCoin(params);
setResult(JSON.stringify(ret, null, 2));
} catch (e: any) {
if (e.name !== 'AbortError') {
setError(e.message);
}
}
};
transferCoin();
}, [])

return <>
<div>Result: <pre>{result}</pre></div>
{error && <div style={{color: 'red'}}>{`${error}`}</div>}
</>
}

(async () => {
await loadLocusWasm();
root.render(
<App />
);
})();

Call the locus_transferCoin RPC to create and send a coin transfer transaction.

info

Detailed definitions for the Locus library can be found here.

4. Run

Run the project in the terminal as follows:

npm run start