Skip to content

Commit 58650e2

Browse files
feat: adds tron versioned transactions (#942)
* feat: adds tron versioned transactions * fix: uses png logo * Update advanced/wallets/react-wallet-v2/src/lib/TronLib.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5a4ed2f commit 58650e2

File tree

11 files changed

+98
-23
lines changed

11 files changed

+98
-23
lines changed
3.9 KB
Loading

advanced/dapps/react-dapp-v2/src/chains/tron.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ export const TronChainData: ChainsMap = {
2020
export const TronMetadata: NamespaceMetadata = {
2121
// Tron Mainnet
2222
"0x2b6653dc": {
23-
logo: "https://tronscan.io/static/media/TRON.4a760cebd163969b2ee874abf2415e9a.svg",
23+
logo: "/assets/tron.png",
2424
rgb: "183, 62, 49",
2525
},
2626
// Tron TestNet
2727
"0xcd8690dc": {
28-
logo: "https://tronscan.io/static/media/TRON.4a760cebd163969b2ee874abf2415e9a.svg",
28+
logo: "assets/tron.png",
2929
rgb: "183, 62, 49",
3030
},
3131
};

advanced/dapps/react-dapp-v2/src/components/Asset.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const btcLogo = getChainMetadata(
1414
"bip122:000000000933ea01ad0ee984209779ba"
1515
).logo;
1616
const suiLogo = getChainMetadata("sui:mainnet").logo;
17+
const tronLogo = getChainMetadata("tron:0x2b6653dc").logo;
1718
const SAsset = styled.div`
1819
width: 100%;
1920
padding: 20px;
@@ -55,6 +56,8 @@ function getAssetIcon(asset: AssetData): JSX.Element {
5556
return <Icon src={btcLogo} />;
5657
case "sui":
5758
return <Icon src={suiLogo} />;
59+
case "trx":
60+
return <Icon src={tronLogo} />;
5861
default:
5962
return <Icon src={"/assets/eth20.svg"} />;
6063
}

advanced/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ export function JsonRpcContextProvider({
16431643
const tronWeb = getTronWeb(chainId);
16441644

16451645
if (!tronWeb) {
1646-
throw new Error("Tron web not found for chainId: " + chainId);
1646+
throw new Error("TronWeb not found for chainId: " + chainId);
16471647
}
16481648

16491649
// Take USDT as an example:
@@ -1653,7 +1653,7 @@ export function JsonRpcContextProvider({
16531653
const testContract = isTestnet
16541654
? "TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf"
16551655
: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
1656-
const testTransaction =
1656+
const { transaction } =
16571657
await tronWeb.transactionBuilder.triggerSmartContract(
16581658
testContract,
16591659
"approve(address,uint256)",
@@ -1665,17 +1665,26 @@ export function JsonRpcContextProvider({
16651665
address
16661666
);
16671667

1668-
const result = await client!.request<{ signature: any }>({
1668+
const sessionProperties = session!.sessionProperties;
1669+
const isV1Method = sessionProperties?.tron_method_version === "v1";
1670+
1671+
const result = await client!.request<{
1672+
signature: any;
1673+
result?: { signature: any };
1674+
}>({
16691675
chainId,
16701676
topic: session!.topic,
16711677
request: {
16721678
method: DEFAULT_TRON_METHODS.TRON_SIGN_TRANSACTION,
1673-
params: {
1674-
address,
1675-
transaction: {
1676-
...testTransaction,
1677-
},
1678-
},
1679+
params: isV1Method
1680+
? {
1681+
address,
1682+
transaction,
1683+
}
1684+
: {
1685+
address,
1686+
transaction: { transaction },
1687+
},
16791688
},
16801689
});
16811690
console.log("tron sign transaction result", result);
@@ -1684,7 +1693,7 @@ export function JsonRpcContextProvider({
16841693
method: DEFAULT_TRON_METHODS.TRON_SIGN_TRANSACTION,
16851694
address,
16861695
valid: true,
1687-
result: result.signature,
1696+
result: result.result?.signature ?? result.signature,
16881697
};
16891698
}
16901699
),
@@ -1697,7 +1706,7 @@ export function JsonRpcContextProvider({
16971706

16981707
const tronWeb = getTronWeb(chainId);
16991708
if (!tronWeb) {
1700-
throw new Error("Tron web not found for chainId: " + chainId);
1709+
throw new Error("TronWeb not found for chainId: " + chainId);
17011710
}
17021711

17031712
const result = await client!.request<{ signature: string }>({
@@ -1711,14 +1720,17 @@ export function JsonRpcContextProvider({
17111720
},
17121721
},
17131722
});
1714-
const valid = await tronWeb.trx.verifyMessage(
1715-
result.signature,
1716-
message
1723+
const valid = await tronWeb.trx.verifyMessageV2(
1724+
message,
1725+
result.signature
17171726
);
1727+
1728+
console.log("tron sign message valid", { valid, address });
1729+
console.log("tron sign message result", result);
17181730
return {
17191731
method: DEFAULT_TRON_METHODS.TRON_SIGN_MESSAGE,
17201732
address,
1721-
valid: valid,
1733+
valid: valid === address,
17221734
result: result.signature,
17231735
};
17241736
}

advanced/dapps/react-dapp-v2/src/helpers/api.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AssetData } from "./types";
55
import { PactCommand } from "@kadena/client";
66
import { apiGetBip122AccountBalance } from "./bip122";
77
import { getSuiClient } from "./sui";
8+
import { TronWeb } from "tronweb";
89

910
export type RpcProvidersByChainId = Record<
1011
number,
@@ -165,6 +166,10 @@ export async function apiGetAccountBalance(
165166
return apiGetSuiAccountBalance(address, chainId);
166167
}
167168

169+
if (namespace === "tron") {
170+
return apiGetTronAccountBalance(address, networkId);
171+
}
172+
168173
if (namespace !== "eip155") {
169174
return { balance: "", symbol: "", name: "" };
170175
}
@@ -186,6 +191,49 @@ export async function apiGetAccountBalance(
186191
return { balance, ...token };
187192
}
188193

194+
export const apiGetTronAccountBalance = async (
195+
address: string,
196+
networkId: string
197+
): Promise<AssetData> => {
198+
try {
199+
let fullHost: string;
200+
201+
switch (networkId) {
202+
case "0x2b6653dc":
203+
fullHost = "https://api.trongrid.io";
204+
break;
205+
case "0x94a9059e":
206+
fullHost = "https://api.shasta.trongrid.io";
207+
break;
208+
case "0xcd8690dc":
209+
fullHost = "https://nile.trongrid.io";
210+
break;
211+
default:
212+
fullHost = "https://api.trongrid.io";
213+
}
214+
215+
const tronWeb = new TronWeb({
216+
fullHost: fullHost,
217+
});
218+
const balance = await tronWeb.trx.getBalance(address);
219+
220+
const balanceInTrx = tronWeb.fromSun(balance);
221+
222+
return {
223+
balance: balanceInTrx.toString(),
224+
symbol: "TRX",
225+
name: "TRX",
226+
};
227+
} catch (error) {
228+
console.error("Failed to fetch TRON balance:", error);
229+
return {
230+
balance: "0",
231+
symbol: "TRX",
232+
name: "TRON",
233+
};
234+
}
235+
};
236+
189237
export const apiGetSuiAccountBalance = async (
190238
address: string,
191239
chainId: string

advanced/dapps/react-dapp-v2/src/helpers/tron.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ export const getTronWeb = (network: string) => {
77
tronWebTestnet = new TronWeb({
88
fullHost: "https://nile.trongrid.io/",
99
});
10-
return tronWebTestnet;
1110
}
11+
return tronWebTestnet;
1212
}
1313
if (network === "tron:0x2b6653dc") {
1414
if (!tronWebMainnet) {
1515
tronWebMainnet = new TronWeb({
1616
fullHost: "https://api.trongrid.io/",
1717
});
18-
return tronWebMainnet;
1918
}
19+
return tronWebMainnet;
2020
}
21+
return undefined;
2122
};
3.9 KB
Loading

advanced/wallets/react-wallet-v2/src/data/TronData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const TRON_MAINNET_CHAINS: TRONChains = {
2323
'tron:0x2b6653dc': {
2424
chainId: '0x2b6653dc',
2525
name: 'Tron',
26-
logo: 'https://tronscan.io/static/media/TRON.4a760cebd163969b2ee874abf2415e9a.svg',
26+
logo: 'chain-logos/tron.png',
2727
rgb: '183, 62, 49',
2828
fullNode: 'https://api.trongrid.io',
2929
namespace: 'tron'
@@ -34,7 +34,7 @@ export const TRON_TEST_CHAINS: TRONChains = {
3434
'tron:0xcd8690dc': {
3535
chainId: '0xcd8690dc',
3636
name: 'Tron Testnet',
37-
logo: 'https://tronscan.io/static/media/TRON.4a760cebd163969b2ee874abf2415e9a.svg',
37+
logo: 'chain-logos/tron.png',
3838
rgb: '183, 62, 49',
3939
fullNode: 'https://nile.trongrid.io/',
4040
namespace: 'tron'

advanced/wallets/react-wallet-v2/src/lib/TronLib.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export default class TronLib {
5050
}
5151

5252
public async signTransaction(transaction: any) {
53-
const signedtxn = await this.tronWeb.trx.sign(transaction.transaction)
53+
// The transaction parameter is expected to be unwrapped already.
54+
const signedtxn = await this.tronWeb.trx.sign(transaction)
5455
return signedtxn
5556
}
5657
}

advanced/wallets/react-wallet-v2/src/utils/TronRequestHandlerUtil.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ export async function approveTronRequest(
3030
return formatJsonRpcResult(id, res)
3131

3232
case TRON_SIGNING_METHODS.TRON_SIGN_TRANSACTION:
33-
const signedTransaction = await wallet.signTransaction(request.params.transaction)
33+
// Compatible with both new and old structures
34+
// New structure : request.params.transaction = transaction
35+
// Old structure: request.params.transaction = { transaction: transaction }
36+
const transaction = request.params.transaction?.transaction || request.params.transaction
37+
const signedTransaction = await wallet.signTransaction(transaction)
3438

3539
return formatJsonRpcResult(id, signedTransaction)
3640

0 commit comments

Comments
 (0)