wative 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/bin/wative-cli.js +2 -0
- package/lib/index.esm.js +1 -0
- package/lib/index.umd.js +1 -0
- package/package.json +59 -0
- package/rollup.config.js +30 -0
- package/src/account.ts +558 -0
- package/src/assets.ts +221 -0
- package/src/chain.ts +28 -0
- package/src/config.ts +2074 -0
- package/src/home_page.ts +36 -0
- package/src/index.d.ts +1 -0
- package/src/index.ts +35 -0
- package/src/network.ts +559 -0
- package/src/tools.ts +742 -0
- package/src/tx_gas_utils.ts +119 -0
- package/src/utils.ts +381 -0
- package/src/web3.ts +268 -0
package/src/web3.ts
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import Web3 from 'web3';
|
|
2
|
+
import { excutePromiseFunction } from './utils';
|
|
3
|
+
import { Connection, PublicKey } from "@solana/web3.js";
|
|
4
|
+
import { getMint, getAssociatedTokenAddressSync } from "@solana/spl-token"
|
|
5
|
+
import { Metaplex } from "@metaplex-foundation/js";
|
|
6
|
+
|
|
7
|
+
export const getChainIdByEvm = async (rpc_url: string) => {
|
|
8
|
+
let web3 = new Web3(rpc_url);
|
|
9
|
+
let result = await excutePromiseFunction(web3.eth.getChainId, []);
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const getAccountBalanceInEvm = async (account: string, rpc_url: string) => {
|
|
14
|
+
const web3 = new Web3(rpc_url);
|
|
15
|
+
let result = await excutePromiseFunction(web3.eth.getBalance, [account]);
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const getAccountBalanceInSolana = async (account: string, rpc_url: string) => {
|
|
20
|
+
const connection = new Connection(rpc_url);
|
|
21
|
+
let result = await excutePromiseFunction(connection.getBalance.bind(connection), [new PublicKey(account)]);
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const getTokenInfoInEvm = async (token_address: string, rpc_url: string) => {
|
|
26
|
+
const web3 = new Web3(rpc_url);
|
|
27
|
+
const erc20 = new web3.eth.Contract([
|
|
28
|
+
{
|
|
29
|
+
"constant": true,
|
|
30
|
+
"inputs": [],
|
|
31
|
+
"name": "symbol",
|
|
32
|
+
"outputs": [
|
|
33
|
+
{
|
|
34
|
+
"name": "",
|
|
35
|
+
"type": "string"
|
|
36
|
+
}
|
|
37
|
+
],
|
|
38
|
+
"payable": false,
|
|
39
|
+
"stateMutability": "view",
|
|
40
|
+
"type": "function"
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"constant": true,
|
|
44
|
+
"inputs": [],
|
|
45
|
+
"name": "name",
|
|
46
|
+
"outputs": [
|
|
47
|
+
{
|
|
48
|
+
"name": "",
|
|
49
|
+
"type": "string"
|
|
50
|
+
}
|
|
51
|
+
],
|
|
52
|
+
"payable": false,
|
|
53
|
+
"stateMutability": "view",
|
|
54
|
+
"type": "function"
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"constant": true,
|
|
58
|
+
"inputs": [],
|
|
59
|
+
"name": "decimals",
|
|
60
|
+
"outputs": [
|
|
61
|
+
{
|
|
62
|
+
"name": "",
|
|
63
|
+
"type": "uint256"
|
|
64
|
+
}
|
|
65
|
+
],
|
|
66
|
+
"payable": false,
|
|
67
|
+
"stateMutability": "view",
|
|
68
|
+
"type": "function"
|
|
69
|
+
}
|
|
70
|
+
], token_address);
|
|
71
|
+
|
|
72
|
+
let sysmbol_result = await excutePromiseFunction(erc20.methods.symbol().call.bind(erc20), []);
|
|
73
|
+
let name_result = await excutePromiseFunction(erc20.methods.name().call.bind(erc20), []);
|
|
74
|
+
let decimals_result = await excutePromiseFunction(erc20.methods.decimals().call.bind(erc20), []);
|
|
75
|
+
|
|
76
|
+
if (!sysmbol_result.status || !name_result.status || !decimals_result.status) {
|
|
77
|
+
return {
|
|
78
|
+
status: false,
|
|
79
|
+
output: "Failed to get token info"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
status: true,
|
|
84
|
+
output: {
|
|
85
|
+
symbol: sysmbol_result.output,
|
|
86
|
+
name: name_result.output,
|
|
87
|
+
decimals: decimals_result.output
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const getTokenInfoInSolana = async (token_address: string, rpc_url: string) => {
|
|
93
|
+
const connection = new Connection(rpc_url);
|
|
94
|
+
const metaplex = Metaplex.make(connection);
|
|
95
|
+
const mintAddress = new PublicKey(token_address);
|
|
96
|
+
const token_mint_info = await excutePromiseFunction(getMint, [connection, mintAddress]);
|
|
97
|
+
let result = {
|
|
98
|
+
status: true,
|
|
99
|
+
output: {
|
|
100
|
+
symbol: "",
|
|
101
|
+
name: "",
|
|
102
|
+
decimals: ""
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!token_mint_info.status) {
|
|
107
|
+
return {
|
|
108
|
+
status: false,
|
|
109
|
+
output: "Failed to get token decimals"
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
result.output.decimals = token_mint_info.output.decimals;
|
|
114
|
+
try {
|
|
115
|
+
const metadataAccount = metaplex
|
|
116
|
+
.nfts()
|
|
117
|
+
.pdas()
|
|
118
|
+
.metadata({ mint: mintAddress });
|
|
119
|
+
const metadataAccountInfo = await connection.getAccountInfo(metadataAccount);
|
|
120
|
+
|
|
121
|
+
if (metadataAccountInfo) {
|
|
122
|
+
const token = await metaplex.nfts().findByMint({ mintAddress: mintAddress });
|
|
123
|
+
result.output.name = token.name;
|
|
124
|
+
result.output.symbol = token.symbol;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
} catch (error) {
|
|
128
|
+
return {
|
|
129
|
+
status: false,
|
|
130
|
+
output: "Failed to get token name"
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export const getTokenBalanceInEvm = async (account: string, token_address: string, rpc_url: string) => {
|
|
137
|
+
const web3 = new Web3(rpc_url);
|
|
138
|
+
const erc20 = new web3.eth.Contract([
|
|
139
|
+
{
|
|
140
|
+
"constant": true,
|
|
141
|
+
"inputs": [
|
|
142
|
+
{
|
|
143
|
+
"name": "who",
|
|
144
|
+
"type": "address"
|
|
145
|
+
}
|
|
146
|
+
],
|
|
147
|
+
"name": "balanceOf",
|
|
148
|
+
"outputs": [
|
|
149
|
+
{
|
|
150
|
+
"name": "",
|
|
151
|
+
"type": "uint256"
|
|
152
|
+
}
|
|
153
|
+
],
|
|
154
|
+
"payable": false,
|
|
155
|
+
"stateMutability": "view",
|
|
156
|
+
"type": "function"
|
|
157
|
+
}
|
|
158
|
+
], token_address);
|
|
159
|
+
let balance_result = await excutePromiseFunction(erc20.methods.balanceOf(account).call.bind(erc20), []);
|
|
160
|
+
if (!balance_result.status) {
|
|
161
|
+
return {
|
|
162
|
+
status: false,
|
|
163
|
+
output: "Failed to get token balance"
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
status: true,
|
|
168
|
+
output: balance_result.output
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export const getTokenBalanceInSolana = async (account: string, token_address: string, rpc_url: string) => {
|
|
173
|
+
const connection = new Connection(rpc_url);
|
|
174
|
+
const tokenAccount = getAssociatedTokenAddressSync(new PublicKey(token_address), new PublicKey(account));
|
|
175
|
+
let balance_result = await excutePromiseFunction(connection.getTokenAccountBalance.bind(connection), [tokenAccount]);
|
|
176
|
+
if (!balance_result.status) {
|
|
177
|
+
if (balance_result.output.includes("could not find account")) {
|
|
178
|
+
return {
|
|
179
|
+
status: true,
|
|
180
|
+
output: "0"
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
status: false,
|
|
185
|
+
output: "Failed to get token balance"
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
status: true,
|
|
190
|
+
output: balance_result.output.value.amount
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export const getCodeInEvm = async (account: string, rpc_url: string) => {
|
|
195
|
+
const web3 = new Web3(rpc_url);
|
|
196
|
+
let code_result = await excutePromiseFunction(web3.eth.getCode.bind(web3), [account]);
|
|
197
|
+
if (!code_result.status) {
|
|
198
|
+
return {
|
|
199
|
+
status: false,
|
|
200
|
+
output: "Failed to get code"
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
status: true,
|
|
205
|
+
output: code_result.output
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export const getTransferTokenData = async (to: string, amount: string, rpc_url: string) => {
|
|
210
|
+
const web3 = new Web3(rpc_url);
|
|
211
|
+
const transfer_token_abi_data = web3.eth.abi.encodeFunctionCall({
|
|
212
|
+
"constant": false,
|
|
213
|
+
"inputs": [
|
|
214
|
+
{
|
|
215
|
+
"name": "_to",
|
|
216
|
+
"type": "address"
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
"name": "_value",
|
|
220
|
+
"type": "uint256"
|
|
221
|
+
}
|
|
222
|
+
],
|
|
223
|
+
"name": "transfer",
|
|
224
|
+
"outputs": [],
|
|
225
|
+
"payable": false,
|
|
226
|
+
"stateMutability": "nonpayable",
|
|
227
|
+
"type": "function"
|
|
228
|
+
}, [to, amount]);
|
|
229
|
+
|
|
230
|
+
return transfer_token_abi_data;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export const getPrioritizationFee = async (rpc_url: string) => {
|
|
234
|
+
const connection = new Connection(rpc_url);
|
|
235
|
+
let result = await excutePromiseFunction(connection.getRecentPrioritizationFees.bind(connection), []);
|
|
236
|
+
if (!result.status) {
|
|
237
|
+
return {
|
|
238
|
+
status: false,
|
|
239
|
+
output: "Failed to get recent prioritization fees"
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
let prioritization_fee_list = result.output;
|
|
244
|
+
const fees = prioritization_fee_list.map((fee: any) => fee.prioritizationFee).sort();
|
|
245
|
+
const prioritization_fee = fees.length > 0 ? Math.ceil(fees.reduce((acc: any, cur: any) => acc + cur) / fees.length) : 1000;
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
status: true,
|
|
249
|
+
output: prioritization_fee
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export const getAccountInfoInSolana = async (account: string, rpc_url: string) => {
|
|
254
|
+
const connection = new Connection(rpc_url);
|
|
255
|
+
let result = await excutePromiseFunction(connection.getAccountInfo.bind(connection), [
|
|
256
|
+
new PublicKey(account.toString())
|
|
257
|
+
]);
|
|
258
|
+
if (!result.status) {
|
|
259
|
+
return {
|
|
260
|
+
status: false,
|
|
261
|
+
output: "Failed to get account info"
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return {
|
|
265
|
+
status: true,
|
|
266
|
+
output: result.output
|
|
267
|
+
}
|
|
268
|
+
}
|