generated from rhdeck/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
170 lines (163 loc) · 4.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import EventEmitter from "events";
const { ethereum } = window as unknown as {
ethereum: {
isMetaMask: boolean;
isConnected: () => boolean;
request: <T>(arg: {
method: string;
params?: any[] | Record<string, any>;
}) => Promise<T>;
_metamask: {
isUnlocked: () => Promise<boolean>;
};
} & EventEmitter;
};
export interface Web3WalletPermission {
// The name of the method corresponding to the permission
parentCapability: string;
// The date the permission was granted, in UNIX epoch time
date?: number;
}
export interface RequestedPermissions {
[methodName: string]: {}; // an empty object, for future extensibility
}
export const eth_decrypt = async (message: string, accountId?: string) => {
return ethereum.request<string>({
method: "eth_decrypt",
params: [message, accountId],
});
};
export const eth_requestAccounts = async () => {
return ethereum.request<string[]>({
method: "eth_requestAccounts",
});
};
export const eth_getEncryptionPublicKey = async (accountId: string) => {
return ethereum.request<string>({
method: "eth_getEncryptionPublicKey",
params: [accountId],
});
};
export const wallet_getPermissions = async () => {
return ethereum.request<Web3WalletPermission[]>({
method: "wallet_getPermissions",
});
};
export const wallet_requestPermissions = async (
permissions: RequestedPermissions
) => {
return ethereum.request<Web3WalletPermission[]>({
method: "wallet_requestPermissions",
params: [permissions],
});
};
export interface AddEthereumChainParameter {
chainId: string; // A 0x-prefixed hexadecimal string
chainName: string;
nativeCurrency: {
name: string;
symbol: string; // 2-6 characters long
decimals: 18;
};
rpcUrls: string[];
blockExplorerUrls?: string[];
iconUrls?: string[]; // Currently ignored.
}
export const wallet_addEthereumChain = async (
chainParameter: AddEthereumChainParameter
) => {
return ethereum.request<null>({
method: "wallet_addEthereumChain",
params: [chainParameter],
});
};
export interface SwitchEthereumChainParameter {
chainId: string; // A 0x-prefixed hexadecimal string
}
export const wallet_switchEthereumChain = async (chainId: string) => {
return ethereum.request<null>({
method: "wallet_switchEthereumChain",
params: [{ chainId } as SwitchEthereumChainParameter],
});
};
export const wallet_registerOnboardingCompleted = async () => {
return ethereum.request<boolean>({
method: "wallet_registerOnboardingCompleted",
});
};
export const eth_chainId = async () => {
return ethereum.request<string>({ method: "eth_chainId" });
};
export interface WatchAssetParams {
type: "ERC20"; // In the future, other standards will be supported
options: {
address: string; // The address of the token contract
symbol: string; // A ticker symbol or shorthand, up to 5 characters
decimals: number; // The number of token decimals
image: string; // A string url of the token logo
};
}
export const wallet_watchAsset = async (params: WatchAssetParams) => {
return ethereum.request<boolean>({
method: "wallet_watchAsset",
params,
});
};
export const wallet_scanQRCode = async (regex?: string) => {
return ethereum.request<string>({
method: "wallet_scanQRCode",
params: [regex],
});
};
export type MetamaskEvents =
| "accountsChanged"
| "chainChanged"
| "connect"
| "disconnect"
| "message";
export const listen = <T>(
event: MetamaskEvents,
handler: (args: T) => void
) => {
if (!ethereum) return () => {};
console.log("Adding listener for event", event);
ethereum.on(event, handler);
return () => {
console.log("Removing listener for event", event);
ethereum.removeListener(event, handler);
};
};
export const onAccountsChanged = (handler: (accounts: string[]) => void) => {
return listen<string[]>("accountsChanged", handler);
};
export const onChainChanged = (handler: (chain: string) => void) => {
return listen<string>("chainChanged", handler);
};
export interface ConnectInfo {
chainId: string;
}
export const onConnect = (handler: (connectInfo: ConnectInfo) => void) => {
return listen<ConnectInfo>("connect", handler);
};
export const onDisconnect = (handler: (error: ProviderRpcError) => void) => {
return listen<ProviderRpcError>("disconnect", handler);
};
export interface ProviderMessage {
type: string;
data: unknown;
}
export const onMessage = (handler: (message: ProviderMessage) => void) => {
return listen<ProviderMessage>("message", handler);
};
export interface ProviderRpcError extends Error {
message: string;
code: number;
data?: unknown;
}
export const isEthereum = !!ethereum;
export const isConnected = () => {
return ethereum && ethereum.isConnected();
};
export const eth_accounts = async () => {
return ethereum.request<string[]>({ method: "eth_accounts" });
};