Remove the template code and replace it with the code below. This will give us a fresh foundation to work with.
function App() {
return <div></div>;
}
export default App;
2
Import dependencies
We'll be utilizing the react library along with useful functions from the supra-l1-sdk.
import React, {useEffect, useState} from "react";
import { HexString, TxnBuilderTypes, BCS } from "supra-l1-sdk";
3
Initialize the supraProvider and state hooks
supraProvider - used to interact with StarKey wallet.
isStarKeyInstalled - boolean value of StarKey extension installation status
accounts - Array of connected accounts
networkData - Chain ID for the connected network
import React, {useEffect, useState} from "react";
import { HexString, TxnBuilderTypes, BCS } from "supra-l1-sdk";
function App() {
let supraProvider: any = typeof window !== "undefined" && (window as any)?.starkey?.supra;
const [isStarkeyInstalled, setIsStarkeyInstalled] = useState<boolean>(!!supraProvider);
const [accounts, setAccounts] = useState<string[]>([]);
const [networkData, setNetworkData] = useState<any>();
return <div></div>;
}
All code from this step forward will be included within the App() function.
4
Check for the injected StarKey object
The following code checks for the injected StarKey object every 500ms for up to 5 seconds. If found, the function will update the StarKey installation status and obtain the connected accounts.
The following code will handle the wallet connections, triggered through a button click (which we will setup later) or an emitted event from the StarKey object.
When a wallet is connected, the accounts must be updated. When a wallet is disconnected, we must clear the stored accounts and network data.
The following code creates a raw payload that calls the transfer function of the supra_account module. Once the raw transaction payload is created, it uses the supraProvider.sendTransaction function to prompted the connect wallet to sign and submit.
It will transfer 1 supra to the address 0x8de4158b48633d853186d5fc790718e5821d7d3c4855e06bcd97b105389a7d0f .