-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsnap.ts
121 lines (109 loc) · 3.02 KB
/
snap.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
import type { RpcMethodTypes } from '@ziad-saab/dogecoin-snap';
import { defaultSnapOrigin } from '../config';
import { GetSnapsResponse, Snap } from '../types';
const SATOSHI_TO_DOGE = 100_000_000;
/**
* Get the installed snaps in MetaMask.
*
* @returns The snaps installed in MetaMask.
*/
export const getSnaps = async (): Promise<GetSnapsResponse> => {
return (await window.ethereum.request({
method: 'wallet_getSnaps',
})) as unknown as GetSnapsResponse;
};
/**
* Connect a snap to MetaMask.
*
* @param snapId - The ID of the snap.
* @param params - The params to pass with the snap to connect.
*/
export const connectSnap = async (
snapId: string = defaultSnapOrigin,
params: Record<'version' | string, unknown> = {},
) => {
await window.ethereum.request({
method: 'wallet_requestSnaps',
params: {
[snapId]: params,
},
});
};
/**
* Get the snap from MetaMask.
*
* @param version - The version of the snap to install (optional).
* @returns The snap object returned by the extension.
*/
export const getSnap = async (version?: string): Promise<Snap | undefined> => {
try {
const snaps = await getSnaps();
return Object.values(snaps).find(
(snap) =>
snap.id === defaultSnapOrigin && (!version || snap.version === version),
);
} catch (e) {
console.log('Failed to obtain installed snap', e);
return undefined;
}
};
export const isLocalSnap = (snapId: string) => snapId.startsWith('local:');
type SnapRpcRequestParams<M extends keyof RpcMethodTypes> =
RpcMethodTypes[M]['input'] extends undefined
? { snapRpcMethod: M }
: { snapRpcMethod: M; params: RpcMethodTypes[M]['input'] };
const snapRpcRequest = async <M extends keyof RpcMethodTypes>(
args: SnapRpcRequestParams<M>,
) => {
const result = await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: defaultSnapOrigin,
request: {
method: `doge_${args.snapRpcMethod}`,
params: 'params' in args ? args.params : undefined,
},
},
});
return result as unknown as RpcMethodTypes[M]['output'];
};
/**
* Invoke the "doge_getAddress" RPC method from the snap.
*/
export const getAddress = async () => {
return snapRpcRequest({
snapRpcMethod: 'getAddress',
});
};
/**
* Invoke the "doge_getBalance" RPC method from the snap.
*/
export const getBalance = async () => {
return snapRpcRequest({
snapRpcMethod: 'getBalance',
});
};
type MakeTransactionParams = {
amountInDoge: number;
toAddress: string;
};
/**
* Invoke the "doge_makeTransaction" RPC method from the snap.
*
* @param params - The transaction parameters.
* @param params.toAddress - The address to send DOGETEST to.
* @param params.amountInDoge - The amount to send in DOGETEST.
*/
export const makeTransaction = async ({
toAddress,
amountInDoge,
}: MakeTransactionParams) => {
const amountInSatoshi = amountInDoge * SATOSHI_TO_DOGE;
return snapRpcRequest({
snapRpcMethod: 'makeTransaction',
params: {
toAddress,
amountInSatoshi,
},
});
};