-
Notifications
You must be signed in to change notification settings - Fork 3
/
adena.ts
110 lines (89 loc) · 3.23 KB
/
adena.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
import {
EAdenaResponseStatus,
EAdenaResponseType,
IAccountInfo,
IAdenaMessage,
IAdenaResponse
} from './adena.types.ts';
import { BroadcastTxCommitResult } from '@gnolang/tm2-js-client';
export class AdenaService {
static validateAdena() {
// @ts-expect-error This should be injected by the extension
const adena = window.adena;
// Check if adena is installed as an extension
if (!adena) {
window.open('https://adena.app/', '_blank');
throw new Error('Adena not installed');
}
}
// Establishes a connection to the Adena wallet, if any.
// If the Adena wallet is not installed, it opens up the adena homepage
static async establishConnection(name: string): Promise<void> {
AdenaService.validateAdena();
// @ts-expect-error This should be injected by the extension
const adena = window.adena;
// Establish a connection to the wallet
const response: IAdenaResponse = await adena.AddEstablish(name);
// Parse the response
if (
response.status === EAdenaResponseStatus.SUCCESS ||
response.type === EAdenaResponseType.ALREADY_CONNECTED
) {
// Adena establishes a connection if:
// - the app was not connected before, and the user approves
// - the app was connected before
return;
}
// Unable to connect to the Adena wallet
throw new Error('unable to establish connection');
}
// Fetches the currently selected account info
static async getAccountInfo(): Promise<IAccountInfo> {
AdenaService.validateAdena();
// @ts-expect-error This should be injected by the extension
const adena = window.adena;
// Get the account info
const response: IAdenaResponse = await adena.GetAccount();
if (response.status !== EAdenaResponseStatus.SUCCESS) {
throw new Error('unable to fetch account info');
}
return response.data as IAccountInfo;
}
// Switches the Adena network to the given chain ID
static async switchNetwork(chainID: string): Promise<void> {
AdenaService.validateAdena();
// @ts-expect-error This should be injected by the extension
const adena = window.adena;
// Get the account info
const response: IAdenaResponse = await adena.SwitchNetwork(chainID);
if (
response.status === EAdenaResponseStatus.SUCCESS ||
response.type === EAdenaResponseType.REDUNDANT_CHANGE_REQUESTED
) {
return;
}
throw new Error('unable to switch Adena network');
}
// Sends the given messages within a transaction
// to the connected Gno chain through Adena
static async sendTransaction(
messages: IAdenaMessage[],
gasWanted: number
): Promise<BroadcastTxCommitResult> {
AdenaService.validateAdena();
// @ts-expect-error This should be injected by the extension
const adena = window.adena;
// Sign and send the transaction
const response: IAdenaResponse = await adena.DoContract({
messages: messages,
gasFee: 1000000, // 1 gnot
gasWanted: gasWanted // ugnot
});
// Check the response
if (response.status !== EAdenaResponseStatus.SUCCESS) {
throw new Error(`unable to send transaction: ${response.message}`);
}
// Parse the response output
return response.data as BroadcastTxCommitResult;
}
}