Skip to main content

Get Going

This walkthrough covers how to detect UKey Wallet TRON Provider, request account authorization, and read balance, send TRX or call TRC20 contract through TronWeb.

ℹ️

UKey Wallet TRON Provider is compatible with the TronLink interface and provides TronWeb and SunWeb related instances.


Find Provider

// UKey Wallet injects both the provider and TronWeb
const provider = window.$ukey?.tron;

// TronWeb becomes globally available after connection
const tronWeb = window.tronWeb;

if (!provider) {
throw new Error("UKey Wallet TRON provider not detected");
}

// Check whether it is TronLink-compatible
console.log("isTronLink:", provider.isTronLink); // Reference value: true

Ask Access

// Request the connection
const callResult = await provider.request({
method: "tron_requestAccounts",
});

if (callResult.code === 200) {
console.log("Connection is ready!");
const address = tronWeb.defaultAddress.base58;
console.log("Resolved address:", address);
} else {
console.log("Connection request was rejected:", callResult.message);
}

Conn Status

// Check whether TronWeb is ready
if (tronWeb && tronWeb.ready) {
console.log("Connection established:", tronWeb.defaultAddress.base58);
} else {
console.log("Connection has not been established");
}

Using TronWeb

After the connection is successful, you can use the global tronWeb instance to access the TRON network:

Get balance

const balance = await tronWeb.trx.getBalance(tronWeb.defaultAddress.base58);
console.log("Current balance:", tronWeb.fromSun(balance), "TRX");

Send TRX

const tx = await tronWeb.trx.sendTransaction(
"TR7x6W5v4U3t2S1r0Q9p8NmLyKjHgFeDcB",
tronWeb.toSun(10), // ten TRX
);

console.log("Built transaction:", tx.txid);

Send TRC20 tokens

const contractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
const contract = await tronWeb.contract().at(contractAddress);

// Retrieve accuracy
const decimals = await contract.decimals().call();

// Transfer fungible tokens
const amount = 100 * 10 ** decimals; // token amount: 100
const tx = await contract.transfer("TR7x6W5v4U3t2S1r0Q9p8NmLyKjHgFeDcB", amount).send();

console.log("Built transaction:", tx);

Event Flow

Watch Accounts

provider.on("accountsChanged", (accounts) => {
if (accounts[0]) {
console.log("Active account changed:", accounts[0]);
} else {
console.log("Connection closed");
}
});

Watch Network

provider.on("chainChanged", (chainId) => {
console.log("Network switched:", chainId);
// Refreshing the page is usually preferred after a network change
});
// Watch for TronLink initialization
window.addEventListener("tronLink#initialized", () => {
console.log("TronLink finished initializing");
});

// Start listening for messages
window.addEventListener("message", (event) => {
if (event.data.isTronLink) {
const { action, data } = event.data.message;
console.log("TronLink callback event:", action, data);
}
});

UKey Wallet is compatible with the TronLink interface. When migrating, read UKey Wallet first, and then fall back to TronLink if it is not installed:

// 旧写法(仅 TronLink)
const legacyTronWeb = window.tronWeb;

// Updated flow: try UKey Wallet first, keep TronLink as an alternative
const provider = window.$ukey?.tron || window.tronLink;
const activeTronWeb = window.tronWeb; // Both inject this object