Skip to content

Commit 54bfa74

Browse files
author
Sam
committed
feat:
1. Change signing response structure to be compatible with both old and new wallets. 2. fix tron logo link 3. add trx balance 4. update tronweb to v6 version 5. compatible with both new and old structures in wallet
1 parent 4a55932 commit 54bfa74

File tree

12 files changed

+216
-50
lines changed

12 files changed

+216
-50
lines changed

advanced/dapps/react-dapp-v2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"react-scripts": "^4.0.3",
5656
"solana-wallet": "^1.0.1",
5757
"styled-components": "^6.1.15",
58-
"tronweb": "^4.4.0",
58+
"tronweb": "^6.0.4",
5959
"web-vitals": "^0.2.4"
6060
},
6161
"devDependencies": {

advanced/dapps/react-dapp-v2/pnpm-lock.yaml

Lines changed: 127 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import {
2626
Transaction as SolanaTransaction,
2727
clusterApiUrl,
2828
} from "@solana/web3.js";
29-
// @ts-expect-error
30-
import TronWeb from "tronweb";
29+
import { TronWeb } from "tronweb";
3130
import {
3231
IPactCommand,
3332
PactCommand,
@@ -1646,7 +1645,7 @@ export function JsonRpcContextProvider({
16461645
const testContract = isTestnet
16471646
? "TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf"
16481647
: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
1649-
const testTransaction =
1648+
const { transaction } =
16501649
await tronWeb.transactionBuilder.triggerSmartContract(
16511650
testContract,
16521651
"approve(address,uint256)",
@@ -1658,17 +1657,26 @@ export function JsonRpcContextProvider({
16581657
address
16591658
);
16601659

1661-
const result = await client!.request<{ signature: any }>({
1660+
const sessionProperties = session!.sessionProperties;
1661+
const isV1Method = sessionProperties?.tron_method_version === "v1";
1662+
1663+
const result = await client!.request<{
1664+
signature: any;
1665+
result?: { signature: any };
1666+
}>({
16621667
chainId,
16631668
topic: session!.topic,
16641669
request: {
16651670
method: DEFAULT_TRON_METHODS.TRON_SIGN_TRANSACTION,
1666-
params: {
1667-
address,
1668-
transaction: {
1669-
...testTransaction,
1670-
},
1671-
},
1671+
params: isV1Method
1672+
? {
1673+
address,
1674+
transaction,
1675+
}
1676+
: {
1677+
address,
1678+
transaction: { transaction },
1679+
},
16721680
},
16731681
});
16741682
console.log("tron sign transaction result", result);
@@ -1677,7 +1685,7 @@ export function JsonRpcContextProvider({
16771685
method: DEFAULT_TRON_METHODS.TRON_SIGN_TRANSACTION,
16781686
address,
16791687
valid: true,
1680-
result: result.signature,
1688+
result: result.result?.signature ?? result.signature,
16811689
};
16821690
}
16831691
),

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

Lines changed: 49 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,50 @@ 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+
200+
let fullHost: string;
201+
202+
switch (networkId) {
203+
case "0x2b6653dc":
204+
fullHost = 'https://api.trongrid.io';
205+
break;
206+
case "0x94a9059e":
207+
fullHost = 'https://api.shasta.trongrid.io';
208+
break;
209+
case "0xcd8690dc":
210+
fullHost = 'https://nile.trongrid.io';
211+
break;
212+
default:
213+
fullHost = 'https://api.trongrid.io';
214+
}
215+
216+
const tronWeb = new TronWeb({
217+
fullHost: fullHost
218+
});
219+
const balance = await tronWeb.trx.getBalance(address);
220+
221+
const balanceInTrx = tronWeb.fromSun(balance);
222+
223+
return {
224+
balance: balanceInTrx.toString(),
225+
symbol: "TRX",
226+
name: "TRX"
227+
};
228+
} catch (error) {
229+
console.error("Failed to fetch TRON balance:", error);
230+
return {
231+
balance: "0",
232+
symbol: "TRX",
233+
name: "TRON"
234+
};
235+
}
236+
};
237+
189238
export const apiGetSuiAccountBalance = async (
190239
address: string,
191240
chainId: string
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
@@ -51,7 +51,8 @@ export default class TronLib {
5151
}
5252

5353
public async signTransaction(transaction: any) {
54-
const signedtxn = await this.tronWeb.trx.sign(transaction.transaction)
54+
// Compatible with both new and old structures at the handler level
55+
const signedtxn = await this.tronWeb.trx.sign(transaction)
5556
return signedtxn
5657
}
5758
}

0 commit comments

Comments
 (0)