Copy npm install --save @walletconnect/client
OR
yarn add @walletconnect/client
Copy import WalletConnect from "@walletconnect/client";
// Create a connector
const connector = new WalletConnect({
bridge: "https://bridge.walletconnect.org", // Required
});
// Check if connection is already established
if (!connector.connected) {
// create new session
connector.createSession();
}
// Subscribe to connection events
connector.on("connect", (error, payload) => {
if (error) {
throw error;
}
// Get provided accounts and chainId
const { accounts, chainId } = payload.params[0];
});
connector.on("session_update", (error, payload) => {
if (error) {
throw error;
}
// Get updated accounts and chainId
const { accounts, chainId } = payload.params[0];
});
connector.on("disconnect", (error, payload) => {
if (error) {
throw error;
}
// Delete connector
});
Copy const request = {
id: 1110,
jsonrpc: '2.0',
method: 'hedera_sendTransaction',
params: [
{
from: account // solidityAddress,
data: transByte // format: hex,
},
],
};
// Send Request
connector
.sendCustomRequest(request)
.then((result) => {
// Returns request result
console.log(result);
})
.catch((error) => {
// Error returned when rejected
console.error(error);
});
Create Transaction Example
Copy export async function senHbar(
sender: string,
receiver: string,
amount: string,
) {
const senderAccountId = AccountId.fromSolidityAddress(sender).toString();
const newAccountTransaction = new TransferTransaction()
.addHbarTransfer(senderAccountId, new Hbar(-amount))
.addHbarTransfer(receiver, new Hbar(amount))
.setTransactionMemo('Hello buddy sign and enjoy!');
const bytes = await makeTransBytes(newAccountTransaction, senderAccountId);
return bytes;
}
Copy export const makeTransBytes = async (trans: any, accountId: string) => {
const transId = TransactionId.generate(accountId);
trans.setTransactionId(transId);
trans.setNodeAccountIds([new AccountId(3)]);
await trans.freeze();
return Buffer.from(trans.toBytes()).toString('hex');
};