Skip to content

Commit 02a8b65

Browse files
authored
add tomo wallet app (#49)
* feat: pnpm 9.12.0 * feat: add tomo wallet * chore: replace tronlink for now * fix: keep checking if is in mobile browser (cherry picked from commit fd9f84d) * fix: convert adapter to unknown first * fix: default use tomo wallet * fix: rpc changed to trongrid * fix: code cleanup * test: debug * feat: add account change event listener * chore: code cleanup and unit test * chore: update README * fix: prefer eth_requestAccounts * fix: ignore accountsChanged event if adapter is disconnected * fix: remove comments
1 parent e1227af commit 02a8b65

22 files changed

+719
-122
lines changed

demos/dev-demo/src/AdapterBasicTest.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
WalletConnectAdapter,
1515
FoxWalletAdapter,
1616
BybitWalletAdapter,
17+
TomoWalletAdapter,
1718
TrustAdapter
1819
} from '@tronweb3/tronwallet-adapters';
1920
import { memo, useCallback, useEffect, useMemo, useState } from 'react';
@@ -24,6 +25,7 @@ const receiver = 'TMDKznuDWaZwfZHcM61FVFstyYNmK6Njk1';
2425
export const AdapterBasicTest = memo(function AdapterBasicTest() {
2526
const adapters = useMemo(
2627
() => [
28+
new TomoWalletAdapter(),
2729
new TronLinkAdapter(),
2830
new TokenPocketAdapter(),
2931
new OkxWalletAdapter(),

demos/dev-demo/src/components/SignUsage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const InformAlertText = styled(Typography)({
8080
});
8181

8282
// Replace with another address. Don't transfer any assets to this address.
83-
const receiver = 'TMDKznuDWaZwfZHcM61FVFstyYNmK6Njk1';
83+
const receiver = 'TM6nL4t3PVeFHcYgePusVX3dhk4zSCEd1s';
8484

8585
export default function SignUsage() {
8686
const { connectionState, adapter } = useWallet();

demos/dev-demo/src/components/WalletProvider.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { WalletProvider as _WalletProvider, useLocalStorage } from "@tronweb3/tronwallet-adapter-react-hooks";
22
import type { PropsWithChildren } from "react";
33
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
4+
import {
5+
TronLinkAdapter} from '@tronweb3/tronwallet-adapters';
46
import {
57
BitKeepAdapter,
68
GateWalletAdapter,
79
ImTokenAdapter,
810
LedgerAdapter,
911
OkxWalletAdapter,
1012
TokenPocketAdapter,
11-
TronLinkAdapter,
1213
WalletConnectAdapter,
1314
FoxWalletAdapter,
1415
BybitWalletAdapter,
16+
TomoWalletAdapterName,
17+
TomoWalletAdapter,
1518
TronLinkAdapterName,
1619
TrustAdapter
1720
} from '@tronweb3/tronwallet-adapters';
@@ -35,7 +38,7 @@ export interface WalletContextType {
3538
disconnect?: () => Promise<void>;
3639
}
3740
const Context = createContext<WalletContextType>({
38-
selectedAdapterName: TronLinkAdapterName,
41+
selectedAdapterName: TomoWalletAdapterName,
3942
setSelectedAdapterName: () => {
4043
//
4144
},
@@ -52,6 +55,7 @@ const Context = createContext<WalletContextType>({
5255
export default function WalletProvider({ children }: PropsWithChildren) {
5356
const adapters = useMemo(() => {
5457
return [
58+
new TomoWalletAdapter(),
5559
new TronLinkAdapter(),
5660
new TokenPocketAdapter(),
5761
new OkxWalletAdapter(),
@@ -66,7 +70,7 @@ export default function WalletProvider({ children }: PropsWithChildren) {
6670
];
6771
}, []);
6872
const walletName = decodeURIComponent(new URLSearchParams(location.search).get('wallet') || '');
69-
const [selectedAdapterName, _setSelectedAdapterName] = useState(walletName as AdapterName || TronLinkAdapterName);
73+
const [selectedAdapterName, _setSelectedAdapterName] = useState(walletName as AdapterName || TomoWalletAdapterName);
7074

7175
const setSelectedAdapterName = useCallback((selectedAdapterName: AdapterName) => {
7276
_setSelectedAdapterName(selectedAdapterName);
@@ -98,7 +102,7 @@ export default function WalletProvider({ children }: PropsWithChildren) {
98102
connected: true,
99103
address: adapter?.address || '',
100104
}));
101-
(adapter as TronLinkAdapter)?.network?.().then((network) => {
105+
(adapter as unknown as TronLinkAdapter)?.network?.().then((network) => {
102106
setConnectionState(preState => ({
103107
...preState,
104108
chainId: network.chainId,
@@ -142,7 +146,7 @@ export default function WalletProvider({ children }: PropsWithChildren) {
142146
adapter.on('disconnect', onDisconnect);
143147
adapter.on('chainChanged', onChainChanged);
144148
if (adapter?.connected) {
145-
(adapter as TronLinkAdapter)?.network?.().then((network) => {
149+
(adapter as unknown as TronLinkAdapter)?.network?.().then((network) => {
146150
setConnectionState(preState => ({
147151
...preState,
148152
chainId: network.chainId,

demos/dev-demo/src/tronweb.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { TronWeb } from 'tronweb';
33

44
export const tronWeb = new TronWeb({
5-
fullHost: 'https://api.nileex.io',
6-
// fullHost: 'https://api.trongrid.io',
5+
// fullHost: 'https://api.nileex.io',
6+
fullHost: 'https://api.trongrid.io',
77
privateKey: '',
88
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"./packages/*/*/README.md",
2525
"./packages/*/*/package.json"
2626
],
27+
"packageManager": "pnpm@9.12.0",
2728
"scripts": {
2829
"nuke": "shx rm -rf packages/*/*/node_modules demos/**/node_modules node_modules || true",
2930
"reinstall": "pnpm run nuke && pnpm install",
@@ -46,7 +47,7 @@
4647
"lint:fix": "pnpm run fmt && eslint --fix .",
4748
"test": "pnpm --recursive --filter \"@tronweb3/*\" run test",
4849
"example": "pnpm run --filter {demos/react-ui/vite-app} dev",
49-
"prepare": "husky install",
50+
"prepare": "husky install && pnpm build",
5051
"update-version": "pnpm changeset add && pnpm changeset version",
5152
"prerelease": "node ./scripts/check-dep-version.js"
5253
},

packages/adapters/adapters/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@tronweb3/tronwallet-adapter-gatewallet": "workspace:^",
5353
"@tronweb3/tronwallet-adapter-foxwallet": "workspace:^",
5454
"@tronweb3/tronwallet-adapter-bybit": "workspace:^",
55+
"@tronweb3/tronwallet-adapter-tomowallet": "workspace:^",
5556
"@tronweb3/tronwallet-adapter-trust": "workspace:^"
5657
},
5758
"devDependencies": {

packages/adapters/adapters/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export * from '@tronweb3/tronwallet-adapter-gatewallet';
88
export * from '@tronweb3/tronwallet-adapter-imtoken';
99
export * from '@tronweb3/tronwallet-adapter-foxwallet';
1010
export * from '@tronweb3/tronwallet-adapter-bybit';
11+
export * from '@tronweb3/tronwallet-adapter-tomowallet';
1112
export * from '@tronweb3/tronwallet-adapter-trust';

packages/adapters/adapters/tsconfig.all.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
{
3535
"path": "../bybit/tsconfig.all.json"
3636
},
37+
{
38+
"path": "../tomowallet/tsconfig.all.json"
39+
},
3740
{
3841
"path": "./tsconfig.cjs.json"
3942
},

packages/adapters/tomowallet/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2022-Present, tronweb3
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# `@tronweb3/tronwallet-adapter-tomowallet`
2+
3+
This package provides an adapter to enable TRON DApps to connect wallet inside the [Tomo Wallet App](https://tomo.inc/)
4+
5+
## Demo
6+
7+
```typescript
8+
import { TomoWalletAdapter } from '@tronweb3/tronwallet-adapter-tomowallet';
9+
import TronWeb from 'tronweb';
10+
11+
const tronWeb = new TronWeb({
12+
fullHost: 'https://api.trongrid.io',
13+
headers: { 'TRON-PRO-API-KEY': 'your api key' },
14+
});
15+
16+
const adapter = new TomoWalletAdapter();
17+
// connect
18+
await adapter.connect();
19+
20+
// then you can get address
21+
console.log(adapter.address);
22+
23+
// create a send TRX transaction
24+
const unSignedTransaction = await tronWeb.transactionBuilder.sendTrx(targetAddress, 100, adapter.address);
25+
// using adapter to sign the transaction
26+
const signedTransaction = await adapter.signTransaction(unSignedTransaction);
27+
// broadcast the transaction
28+
await tronWeb.trx.sendRawTransaction(signedTransaction);
29+
```
30+
31+
## Documentation
32+
33+
### API
34+
35+
- `Constructor(config: TomoWalletAdapterConfig)`
36+
```typescript
37+
interface TomoWalletAdapterConfig extends BaseAdapterConfig {
38+
/**
39+
* Timeout in millisecond for checking if Tomo wallet exists.
40+
* Default is 30 * 1000ms
41+
*/
42+
checkTimeout?: number;
43+
/**
44+
* The icon of your dapp. Used when open Tomo app in mobile device browsers.
45+
*/
46+
dappIcon?: string;
47+
/**
48+
* The name of your dapp. Used when open Tomo app in mobile device browsers.
49+
*/
50+
dappName?: string;
51+
}
52+
```
53+
- `network()` method is supported to get current network information.
54+
Currently Tomo Wallet only supports TRON mainnet.
55+
The type of returned value is `Network` as follows:
56+
57+
```typescript
58+
export enum NetworkType {
59+
Mainnet = 'Mainnet',
60+
Shasta = 'Shasta',
61+
Nile = 'Nile',
62+
/**
63+
* When use custom node
64+
*/
65+
Unknown = 'Unknown',
66+
}
67+
68+
export type Network = {
69+
networkType: NetworkType;
70+
chainId: string;
71+
fullNode: string;
72+
solidityNode: string;
73+
eventServer: string;
74+
};
75+
```
76+

0 commit comments

Comments
 (0)