sol-ihor-lab 1.0.0
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 +35 -0
- package/lib/cjs/bundle.d.ts +4 -0
- package/lib/cjs/bundle.js +102 -0
- package/lib/cjs/buy_token.d.ts +5 -0
- package/lib/cjs/buy_token.js +59 -0
- package/lib/cjs/create_open_market.d.ts +4 -0
- package/lib/cjs/create_open_market.js +340 -0
- package/lib/cjs/create_pool.d.ts +4 -0
- package/lib/cjs/create_pool.js +107 -0
- package/lib/cjs/create_token.d.ts +4 -0
- package/lib/cjs/create_token.js +164 -0
- package/lib/cjs/errors.d.ts +18 -0
- package/lib/cjs/errors.js +44 -0
- package/lib/cjs/global.d.ts +62 -0
- package/lib/cjs/global.js +107 -0
- package/lib/cjs/index.d.ts +17 -0
- package/lib/cjs/index.js +33 -0
- package/lib/cjs/instructions/build-instruction.d.ts +8 -0
- package/lib/cjs/instructions/build-instruction.js +66 -0
- package/lib/cjs/instructions/buy-instruction.d.ts +3 -0
- package/lib/cjs/instructions/buy-instruction.js +44 -0
- package/lib/cjs/instructions/cache-alt-instruction.d.ts +4 -0
- package/lib/cjs/instructions/cache-alt-instruction.js +41 -0
- package/lib/cjs/instructions/createATA-instruction.d.ts +3 -0
- package/lib/cjs/instructions/createATA-instruction.js +10 -0
- package/lib/cjs/instructions/createpool-instruction.d.ts +4 -0
- package/lib/cjs/instructions/createpool-instruction.js +58 -0
- package/lib/cjs/instructions/sol-transfer-instruction.d.ts +3 -0
- package/lib/cjs/instructions/sol-transfer-instruction.js +12 -0
- package/lib/cjs/instructions/transfer-instruction.d.ts +3 -0
- package/lib/cjs/instructions/transfer-instruction.js +9 -0
- package/lib/cjs/pool/pool-manager.d.ts +42 -0
- package/lib/cjs/pool/pool-manager.js +70 -0
- package/lib/cjs/pool_manager.d.ts +25 -0
- package/lib/cjs/pool_manager.js +127 -0
- package/lib/cjs/sell_token.d.ts +5 -0
- package/lib/cjs/sell_token.js +59 -0
- package/lib/cjs/transaction/bundle.d.ts +4 -0
- package/lib/cjs/transaction/bundle.js +97 -0
- package/lib/cjs/transaction/execute-rpc.d.ts +5 -0
- package/lib/cjs/transaction/execute-rpc.js +41 -0
- package/lib/cjs/transaction/sign-tx.d.ts +2 -0
- package/lib/cjs/transaction/sign-tx.js +7 -0
- package/lib/cjs/transaction/transaction-exectue.d.ts +30 -0
- package/lib/cjs/transaction/transaction-exectue.js +119 -0
- package/lib/cjs/transaction-helper/check_transaction.d.ts +3 -0
- package/lib/cjs/transaction-helper/check_transaction.js +43 -0
- package/lib/cjs/transaction-helper/transaction.d.ts +7 -0
- package/lib/cjs/transaction-helper/transaction.js +75 -0
- package/lib/cjs/upload-metadata/upload-metaplex.d.ts +3 -0
- package/lib/cjs/upload-metadata/upload-metaplex.js +48 -0
- package/lib/cjs/upload-metadata/upload-nftstorage.d.ts +0 -0
- package/lib/cjs/upload-metadata/upload-nftstorage.js +1 -0
- package/lib/cjs/upload-metadata/upload-pinata.d.ts +1 -0
- package/lib/cjs/upload-metadata/upload-pinata.js +17 -0
- package/lib/cjs/upload-metadata/utility.d.ts +1 -0
- package/lib/cjs/upload-metadata/utility.js +28 -0
- package/lib/cjs/utility.d.ts +33 -0
- package/lib/cjs/utility.js +129 -0
- package/lib/cjs/utils/budget.d.ts +9 -0
- package/lib/cjs/utils/budget.js +83 -0
- package/lib/cjs/utils/error-helper.d.ts +7 -0
- package/lib/cjs/utils/error-helper.js +15 -0
- package/lib/cjs/utils/get-balance.d.ts +11 -0
- package/lib/cjs/utils/get-balance.js +52 -0
- package/lib/cjs/utils/util.d.ts +27 -0
- package/lib/cjs/utils/util.js +346 -0
- package/lib/cjs/validation_check.d.ts +3 -0
- package/lib/cjs/validation_check.js +26 -0
- package/lib/esm/bundle.d.ts +4 -0
- package/lib/esm/bundle.js +75 -0
- package/lib/esm/buy_token.d.ts +5 -0
- package/lib/esm/buy_token.js +55 -0
- package/lib/esm/create_open_market.d.ts +4 -0
- package/lib/esm/create_open_market.js +310 -0
- package/lib/esm/create_pool.d.ts +4 -0
- package/lib/esm/create_pool.js +80 -0
- package/lib/esm/create_token.d.ts +4 -0
- package/lib/esm/create_token.js +137 -0
- package/lib/esm/errors.d.ts +18 -0
- package/lib/esm/errors.js +35 -0
- package/lib/esm/global.d.ts +62 -0
- package/lib/esm/global.js +103 -0
- package/lib/esm/index.d.ts +17 -0
- package/lib/esm/index.js +17 -0
- package/lib/esm/instructions/build-instruction.d.ts +8 -0
- package/lib/esm/instructions/build-instruction.js +61 -0
- package/lib/esm/instructions/buy-instruction.d.ts +3 -0
- package/lib/esm/instructions/buy-instruction.js +40 -0
- package/lib/esm/instructions/cache-alt-instruction.d.ts +4 -0
- package/lib/esm/instructions/cache-alt-instruction.js +36 -0
- package/lib/esm/instructions/createATA-instruction.d.ts +3 -0
- package/lib/esm/instructions/createATA-instruction.js +6 -0
- package/lib/esm/instructions/createpool-instruction.d.ts +4 -0
- package/lib/esm/instructions/createpool-instruction.js +52 -0
- package/lib/esm/instructions/sol-transfer-instruction.d.ts +3 -0
- package/lib/esm/instructions/sol-transfer-instruction.js +8 -0
- package/lib/esm/instructions/transfer-instruction.d.ts +3 -0
- package/lib/esm/instructions/transfer-instruction.js +5 -0
- package/lib/esm/pool/pool-manager.d.ts +42 -0
- package/lib/esm/pool/pool-manager.js +63 -0
- package/lib/esm/pool_manager.d.ts +25 -0
- package/lib/esm/pool_manager.js +123 -0
- package/lib/esm/sell_token.d.ts +5 -0
- package/lib/esm/sell_token.js +55 -0
- package/lib/esm/transaction/bundle.d.ts +4 -0
- package/lib/esm/transaction/bundle.js +90 -0
- package/lib/esm/transaction/execute-rpc.d.ts +5 -0
- package/lib/esm/transaction/execute-rpc.js +36 -0
- package/lib/esm/transaction/sign-tx.d.ts +2 -0
- package/lib/esm/transaction/sign-tx.js +3 -0
- package/lib/esm/transaction/transaction-exectue.d.ts +30 -0
- package/lib/esm/transaction/transaction-exectue.js +115 -0
- package/lib/esm/transaction-helper/check_transaction.d.ts +3 -0
- package/lib/esm/transaction-helper/check_transaction.js +16 -0
- package/lib/esm/transaction-helper/transaction.d.ts +7 -0
- package/lib/esm/transaction-helper/transaction.js +68 -0
- package/lib/esm/upload-metadata/upload-metaplex.d.ts +3 -0
- package/lib/esm/upload-metadata/upload-metaplex.js +43 -0
- package/lib/esm/upload-metadata/upload-nftstorage.d.ts +0 -0
- package/lib/esm/upload-metadata/upload-nftstorage.js +1 -0
- package/lib/esm/upload-metadata/upload-pinata.d.ts +1 -0
- package/lib/esm/upload-metadata/upload-pinata.js +13 -0
- package/lib/esm/upload-metadata/utility.d.ts +1 -0
- package/lib/esm/upload-metadata/utility.js +21 -0
- package/lib/esm/utility.d.ts +33 -0
- package/lib/esm/utility.js +93 -0
- package/lib/esm/utils/budget.d.ts +9 -0
- package/lib/esm/utils/budget.js +73 -0
- package/lib/esm/utils/error-helper.d.ts +7 -0
- package/lib/esm/utils/error-helper.js +11 -0
- package/lib/esm/utils/get-balance.d.ts +11 -0
- package/lib/esm/utils/get-balance.js +42 -0
- package/lib/esm/utils/util.d.ts +25 -0
- package/lib/esm/utils/util.js +107 -0
- package/lib/esm/validation_check.d.ts +3 -0
- package/lib/esm/validation_check.js +22 -0
- package/package.json +69 -0
- package/src/assert.ts +4 -0
- package/src/bundle.ts +99 -0
- package/src/buy_token.ts +82 -0
- package/src/create_open_market.ts +464 -0
- package/src/create_pool.ts +120 -0
- package/src/create_token.ts +267 -0
- package/src/errors.ts +25 -0
- package/src/global.ts +156 -0
- package/src/index.ts +17 -0
- package/src/instructions/build-instruction.ts +87 -0
- package/src/instructions/buy-instruction.ts +52 -0
- package/src/instructions/cache-alt-instruction.ts +40 -0
- package/src/instructions/createATA-instruction.ts +29 -0
- package/src/instructions/createpool-instruction.ts +72 -0
- package/src/instructions/sol-transfer-instruction.ts +13 -0
- package/src/instructions/transfer-instruction.ts +22 -0
- package/src/pool/pool-manager.ts +85 -0
- package/src/pool_manager.ts +197 -0
- package/src/sell_token.ts +82 -0
- package/src/transaction/bundle.ts +116 -0
- package/src/transaction/execute-rpc.ts +35 -0
- package/src/transaction/sign-tx.ts +5 -0
- package/src/transaction/transaction-exectue.ts +134 -0
- package/src/transaction-helper/check_transaction.ts +23 -0
- package/src/transaction-helper/transaction.ts +90 -0
- package/src/upload-metadata/upload-metaplex.ts +51 -0
- package/src/upload-metadata/upload-nftstorage.ts +0 -0
- package/src/upload-metadata/upload-pinata.ts +8 -0
- package/src/upload-metadata/utility.ts +10 -0
- package/src/utility.ts +127 -0
- package/src/utils/budget.ts +104 -0
- package/src/utils/error-helper.ts +20 -0
- package/src/utils/get-balance.ts +55 -0
- package/src/utils/util.ts +111 -0
- package/src/validation_check.ts +26 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { Connection, Keypair, PublicKey, Transaction } from "@solana/web3.js";
|
|
2
|
+
import { EnvironmentManager, SPL_ERROR, TX_RET } from "./global";
|
|
3
|
+
import {
|
|
4
|
+
createMint,
|
|
5
|
+
getMint,
|
|
6
|
+
getOrCreateAssociatedTokenAccount,
|
|
7
|
+
mintTo
|
|
8
|
+
} from "@solana/spl-token";
|
|
9
|
+
import {
|
|
10
|
+
Metaplex,
|
|
11
|
+
bundlrStorage,
|
|
12
|
+
keypairIdentity,
|
|
13
|
+
toMetaplexFile
|
|
14
|
+
} from "@metaplex-foundation/js";
|
|
15
|
+
import { readFileSync } from "fs";
|
|
16
|
+
import { checkFileExists, xWeiAmount } from "./utility";
|
|
17
|
+
import {
|
|
18
|
+
PROGRAM_ID,
|
|
19
|
+
createCreateMetadataAccountV3Instruction
|
|
20
|
+
} from "@metaplex-foundation/mpl-token-metadata";
|
|
21
|
+
|
|
22
|
+
import * as transaction from "./transaction-helper/transaction";
|
|
23
|
+
|
|
24
|
+
const totalSupplyMint = async (
|
|
25
|
+
connection: Connection,
|
|
26
|
+
token_owner: Keypair,
|
|
27
|
+
token_addr: string,
|
|
28
|
+
total_supply: number
|
|
29
|
+
) => {
|
|
30
|
+
const token_mint = new PublicKey(token_addr);
|
|
31
|
+
const mint_info = await getMint(connection, token_mint);
|
|
32
|
+
try {
|
|
33
|
+
const owner_token_account = await getOrCreateAssociatedTokenAccount(
|
|
34
|
+
connection,
|
|
35
|
+
token_owner,
|
|
36
|
+
token_mint,
|
|
37
|
+
token_owner.publicKey
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
if (owner_token_account.address.toBase58().length <= 0) {
|
|
41
|
+
console.log(
|
|
42
|
+
"Error: [Total Supply Mint] failed to create associated token account"
|
|
43
|
+
);
|
|
44
|
+
return SPL_ERROR.E_TOTAL_MINT_FAIL;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const token_amount = xWeiAmount(total_supply, mint_info.decimals);
|
|
48
|
+
|
|
49
|
+
const mint_result = await mintTo(
|
|
50
|
+
connection,
|
|
51
|
+
token_owner,
|
|
52
|
+
token_mint,
|
|
53
|
+
owner_token_account.address,
|
|
54
|
+
token_owner,
|
|
55
|
+
BigInt(token_amount.toString())
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
if (mint_result.length <= 0) {
|
|
59
|
+
console.log("Error: [Total Supply Mint] failed to mint to owner wallet");
|
|
60
|
+
return SPL_ERROR.E_TOTAL_MINT_FAIL;
|
|
61
|
+
}
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.log("Error: [Total Supply Mint] failed to mint to owner wallet");
|
|
64
|
+
return SPL_ERROR.E_TOTAL_MINT_FAIL;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return SPL_ERROR.E_OK;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const createTokenMetaData = async (
|
|
71
|
+
connection: Connection,
|
|
72
|
+
token_owner: Keypair,
|
|
73
|
+
token_addr: string,
|
|
74
|
+
name: string,
|
|
75
|
+
symbol: string,
|
|
76
|
+
token_logo: string,
|
|
77
|
+
rpc_url: string,
|
|
78
|
+
description?: string
|
|
79
|
+
): Promise<SPL_ERROR> => {
|
|
80
|
+
try {
|
|
81
|
+
const metaplex = Metaplex.make(connection)
|
|
82
|
+
.use(keypairIdentity(token_owner))
|
|
83
|
+
.use(
|
|
84
|
+
bundlrStorage({
|
|
85
|
+
address: EnvironmentManager.getBundlrUrl(),
|
|
86
|
+
providerUrl: rpc_url,
|
|
87
|
+
timeout: 60000
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
const buffer = readFileSync(token_logo);
|
|
91
|
+
const file = toMetaplexFile(buffer, "token-logo.png");
|
|
92
|
+
const logo_url = await metaplex.storage().upload(file);
|
|
93
|
+
|
|
94
|
+
if (logo_url.length <= 0) {
|
|
95
|
+
console.log(
|
|
96
|
+
"Error: [Create Token Meta Data] failed to load metapelx data!!!"
|
|
97
|
+
);
|
|
98
|
+
return SPL_ERROR.E_FAIL;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const metaplex_data = {
|
|
102
|
+
name: name,
|
|
103
|
+
symbol: symbol,
|
|
104
|
+
image: logo_url,
|
|
105
|
+
description
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const { uri } = await metaplex.nfts().uploadMetadata(metaplex_data);
|
|
109
|
+
|
|
110
|
+
if (uri.length <= 0) {
|
|
111
|
+
console.log(
|
|
112
|
+
"Error: [Create Token Meta Data] failed to upload metaplex data!!!"
|
|
113
|
+
);
|
|
114
|
+
return SPL_ERROR.E_FAIL;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const token_mint = new PublicKey(token_addr);
|
|
118
|
+
const [metadata_PDA] = PublicKey.findProgramAddressSync(
|
|
119
|
+
[Buffer.from("metadata"), PROGRAM_ID.toBuffer(), token_mint.toBuffer()],
|
|
120
|
+
PROGRAM_ID
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const token_meta_data = {
|
|
124
|
+
name: name,
|
|
125
|
+
symbol: symbol,
|
|
126
|
+
uri: uri,
|
|
127
|
+
sellerFeeBasisPoints: 0,
|
|
128
|
+
creators: null,
|
|
129
|
+
collection: null,
|
|
130
|
+
uses: null
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const txn = new Transaction().add(
|
|
134
|
+
createCreateMetadataAccountV3Instruction(
|
|
135
|
+
{
|
|
136
|
+
metadata: metadata_PDA,
|
|
137
|
+
mint: token_mint,
|
|
138
|
+
mintAuthority: token_owner.publicKey,
|
|
139
|
+
payer: token_owner.publicKey,
|
|
140
|
+
updateAuthority: token_owner.publicKey
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
createMetadataAccountArgsV3: {
|
|
144
|
+
data: token_meta_data,
|
|
145
|
+
isMutable: true,
|
|
146
|
+
collectionDetails: null
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
)
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
if (
|
|
153
|
+
(await transaction.sendAndConfirmTransactionWithCheck(
|
|
154
|
+
connection,
|
|
155
|
+
token_owner,
|
|
156
|
+
txn
|
|
157
|
+
)) !== SPL_ERROR.E_OK
|
|
158
|
+
) {
|
|
159
|
+
return SPL_ERROR.E_FAIL;
|
|
160
|
+
}
|
|
161
|
+
} catch (error) {
|
|
162
|
+
console.log(
|
|
163
|
+
"Error: [Create Token Meta Data] failed to create meta data -",
|
|
164
|
+
error
|
|
165
|
+
);
|
|
166
|
+
return SPL_ERROR.E_FAIL;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return SPL_ERROR.E_OK;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export const createToken = async (
|
|
173
|
+
connection: Connection,
|
|
174
|
+
token_owner: Keypair,
|
|
175
|
+
name: string,
|
|
176
|
+
symbol: string,
|
|
177
|
+
decimal: number,
|
|
178
|
+
total_supply: number,
|
|
179
|
+
token_logo: string,
|
|
180
|
+
description?: string
|
|
181
|
+
): Promise<TX_RET> => {
|
|
182
|
+
if (
|
|
183
|
+
name.length <= 0 ||
|
|
184
|
+
symbol.length <= 0 ||
|
|
185
|
+
token_logo.length <= 0 ||
|
|
186
|
+
token_owner.publicKey.toBase58().length <= 0 ||
|
|
187
|
+
EnvironmentManager.getRpcNetUrl().length <= 0 ||
|
|
188
|
+
decimal <= 0 ||
|
|
189
|
+
total_supply <= 0
|
|
190
|
+
) {
|
|
191
|
+
console.log("Error: [Create Token] invalid argument to create token!!!");
|
|
192
|
+
return { result: SPL_ERROR.E_INVALID_ARGUE, value: undefined };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if ((await checkFileExists(token_logo)) === false) {
|
|
196
|
+
console.log(
|
|
197
|
+
"Error: [Create Token] invalid argument to create token - token logo path invalid!!!"
|
|
198
|
+
);
|
|
199
|
+
return { result: SPL_ERROR.E_INVALID_ARGUE, value: undefined };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
console.log("<-----------------[Create Token]---------------------");
|
|
203
|
+
console.log(
|
|
204
|
+
"Name: ",
|
|
205
|
+
name,
|
|
206
|
+
"Symbol: ",
|
|
207
|
+
symbol,
|
|
208
|
+
"Decimal: ",
|
|
209
|
+
decimal,
|
|
210
|
+
"Total Supply: ",
|
|
211
|
+
total_supply,
|
|
212
|
+
"Token Logo: ",
|
|
213
|
+
token_logo,
|
|
214
|
+
"Token Description: ",
|
|
215
|
+
description
|
|
216
|
+
);
|
|
217
|
+
console.log("<-----------------[Create Token]---------------------");
|
|
218
|
+
|
|
219
|
+
const token_mint = await createMint(
|
|
220
|
+
connection,
|
|
221
|
+
token_owner,
|
|
222
|
+
token_owner.publicKey,
|
|
223
|
+
token_owner.publicKey,
|
|
224
|
+
decimal
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
if (token_mint.toBase58().length <= 0) {
|
|
228
|
+
console.log("Error: [Create Token] failed to create mint!!!");
|
|
229
|
+
return { result: SPL_ERROR.E_FAIL, value: undefined };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
console.log(
|
|
233
|
+
"<-----------------[Create Token Meta Data]---------------------"
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
const meta_result = await createTokenMetaData(
|
|
237
|
+
connection,
|
|
238
|
+
token_owner,
|
|
239
|
+
token_mint.toBase58(),
|
|
240
|
+
name,
|
|
241
|
+
symbol,
|
|
242
|
+
token_logo,
|
|
243
|
+
EnvironmentManager.getRpcNetUrl(),
|
|
244
|
+
description
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
if (meta_result !== SPL_ERROR.E_OK) {
|
|
248
|
+
console.log("Error: [Create Token] failed to create meta data!!!");
|
|
249
|
+
return { result: SPL_ERROR.E_CREATE_META_FAILED, value: undefined };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
console.log("<-----------------[Token mint]---------------------");
|
|
253
|
+
|
|
254
|
+
if (
|
|
255
|
+
(await totalSupplyMint(
|
|
256
|
+
connection,
|
|
257
|
+
token_owner,
|
|
258
|
+
token_mint.toBase58(),
|
|
259
|
+
total_supply
|
|
260
|
+
)) !== SPL_ERROR.E_OK
|
|
261
|
+
) {
|
|
262
|
+
console.log("Error: [Create Token] failed to mint total supply!!!");
|
|
263
|
+
return { result: SPL_ERROR.E_TOTAL_MINT_FAIL, value: undefined };
|
|
264
|
+
}
|
|
265
|
+
console.log("Success: [Create Token] Mint Address: ", token_mint.toBase58());
|
|
266
|
+
return { result: SPL_ERROR.E_OK, value: token_mint.toBase58() };
|
|
267
|
+
};
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export abstract class BaseError extends Error {
|
|
2
|
+
constructor(message?: string) {
|
|
3
|
+
super(message);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class UploadMetadataError extends BaseError {
|
|
8
|
+
name = "UploadMetadataError";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class GenerateTransactionError extends BaseError {
|
|
12
|
+
name = "GenerateTransactionError";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class BuildInstructionError extends BaseError {
|
|
16
|
+
name = "BuildInstructionError";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class SetTxPriorityError extends BaseError {
|
|
20
|
+
name = "SetTxPriorityError";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class TransactionExecuteError extends BaseError {
|
|
24
|
+
name = "TransactionExecuteError";
|
|
25
|
+
}
|
package/src/global.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CacheLTA,
|
|
3
|
+
DEVNET_PROGRAM_ID,
|
|
4
|
+
LOOKUP_TABLE_CACHE,
|
|
5
|
+
MAINNET_PROGRAM_ID,
|
|
6
|
+
ProgramId
|
|
7
|
+
} from "@raydium-io/raydium-sdk";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
Keypair,
|
|
11
|
+
PublicKey,
|
|
12
|
+
Transaction,
|
|
13
|
+
VersionedTransaction
|
|
14
|
+
} from "@solana/web3.js";
|
|
15
|
+
|
|
16
|
+
export enum SPL_ERROR {
|
|
17
|
+
E_INVALID_ARGUE = -1,
|
|
18
|
+
E_OK = 0,
|
|
19
|
+
E_FAIL = 1,
|
|
20
|
+
E_CHECK_FAIL = 2,
|
|
21
|
+
E_SEND_TX_FAIL,
|
|
22
|
+
E_CONFIRM_TX_FAIL,
|
|
23
|
+
E_CREATE_META_FAILED,
|
|
24
|
+
E_TOTAL_MINT_FAIL
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export enum NETWORK_MODE {
|
|
28
|
+
NETWORK_MAIN = 0,
|
|
29
|
+
NETWORK_DEV = 1,
|
|
30
|
+
NETWORK_TEST = 2
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface TX_RET {
|
|
34
|
+
result: SPL_ERROR;
|
|
35
|
+
value: string | (VersionedTransaction | Transaction)[] | undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface TOKEN_INFO {
|
|
39
|
+
address: string;
|
|
40
|
+
name: string;
|
|
41
|
+
symbol: string;
|
|
42
|
+
decimal: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface BUNDLE_TRANSACTION {
|
|
46
|
+
txn: VersionedTransaction;
|
|
47
|
+
signer: Keypair;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class EnvironmentManager {
|
|
51
|
+
private static NET_MODE: NETWORK_MODE = NETWORK_MODE.NETWORK_MAIN;
|
|
52
|
+
private static JITO_BLOCKENGINE_URL =
|
|
53
|
+
"frankfurt.mainnet.block-engine.jito.wtf";
|
|
54
|
+
private static RPC_CHECK_URL = "http://95.217.82.36:8180";
|
|
55
|
+
private static RPC_MAIN_URL = "";
|
|
56
|
+
private static RPC_DEVNET_URL = "";
|
|
57
|
+
private static RPC_TESTNET_URL = "";
|
|
58
|
+
private static JITO_KEYPAIR: Keypair;
|
|
59
|
+
|
|
60
|
+
private static QUOTE_TOKEN_INFO: TOKEN_INFO;
|
|
61
|
+
|
|
62
|
+
static setNetworkMode(mode: NETWORK_MODE) {
|
|
63
|
+
EnvironmentManager.NET_MODE = mode;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static setMainNetURL(url: string) {
|
|
67
|
+
EnvironmentManager.RPC_MAIN_URL = url;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static setDevNetURL(url: string) {
|
|
71
|
+
EnvironmentManager.RPC_DEVNET_URL = url;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static setTestNettURL(url: string) {
|
|
75
|
+
EnvironmentManager.RPC_TESTNET_URL = url;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static getMainNetURL(): string {
|
|
79
|
+
return EnvironmentManager.RPC_MAIN_URL;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static getDevNetURL(): string {
|
|
83
|
+
return EnvironmentManager.RPC_DEVNET_URL;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static getTestNetURL(): string {
|
|
87
|
+
return EnvironmentManager.RPC_TESTNET_URL;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
static getNetworkMode(): NETWORK_MODE {
|
|
91
|
+
return EnvironmentManager.NET_MODE;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static getRpcNetUrl(): string {
|
|
95
|
+
switch (EnvironmentManager.NET_MODE) {
|
|
96
|
+
case NETWORK_MODE.NETWORK_MAIN:
|
|
97
|
+
return EnvironmentManager.getMainNetURL();
|
|
98
|
+
case NETWORK_MODE.NETWORK_DEV:
|
|
99
|
+
return EnvironmentManager.getDevNetURL();
|
|
100
|
+
case NETWORK_MODE.NETWORK_TEST:
|
|
101
|
+
return EnvironmentManager.getTestNetURL();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
static setNetUrls(main_url: string, dev_url: string, test_url?: string) {
|
|
106
|
+
EnvironmentManager.setMainNetURL(main_url);
|
|
107
|
+
EnvironmentManager.setDevNetURL(dev_url);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static getBundlrUrl(): string {
|
|
111
|
+
return EnvironmentManager.getNetworkMode() === NETWORK_MODE.NETWORK_MAIN
|
|
112
|
+
? "https://node1.bundlr.network"
|
|
113
|
+
: "https://devnet.bundlr.network";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static getCheckUrl(): string {
|
|
117
|
+
return EnvironmentManager.RPC_CHECK_URL;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static getProgramID(): ProgramId {
|
|
121
|
+
return EnvironmentManager.getNetworkMode() === NETWORK_MODE.NETWORK_MAIN
|
|
122
|
+
? MAINNET_PROGRAM_ID
|
|
123
|
+
: DEVNET_PROGRAM_ID;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
static setQuoteTokenInfo(token_info: TOKEN_INFO) {
|
|
127
|
+
EnvironmentManager.QUOTE_TOKEN_INFO = token_info;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static getQuoteTokenInfo(): TOKEN_INFO {
|
|
131
|
+
return EnvironmentManager.QUOTE_TOKEN_INFO;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
static getCacheLTA(): CacheLTA | undefined {
|
|
135
|
+
return EnvironmentManager.getNetworkMode() === NETWORK_MODE.NETWORK_MAIN
|
|
136
|
+
? LOOKUP_TABLE_CACHE
|
|
137
|
+
: undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
static getFeeDestinationId(): PublicKey {
|
|
141
|
+
return EnvironmentManager.getNetworkMode() === NETWORK_MODE.NETWORK_MAIN
|
|
142
|
+
? new PublicKey("7YttLkHDoNj9wyDur5pM1ejNaAvT9X4eqaYcHQqtj2G5")
|
|
143
|
+
: new PublicKey("3XMrhbv989VxAMi3DErLV9eJht1pHppW5LbKxe9fkEFR");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
static getJitoBlockEngine(): string {
|
|
147
|
+
return EnvironmentManager.JITO_BLOCKENGINE_URL;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
static setJitoKeypair(auth_key: Keypair) {
|
|
151
|
+
EnvironmentManager.JITO_KEYPAIR = auth_key;
|
|
152
|
+
}
|
|
153
|
+
static getJitoKeypair(): Keypair {
|
|
154
|
+
return EnvironmentManager.JITO_KEYPAIR;
|
|
155
|
+
}
|
|
156
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from "./instructions/build-instruction";
|
|
2
|
+
export * from "./instructions/buy-instruction";
|
|
3
|
+
export * from "./instructions/cache-alt-instruction";
|
|
4
|
+
export * from "./instructions/createATA-instruction";
|
|
5
|
+
export * from "./instructions/createpool-instruction";
|
|
6
|
+
export * from "./instructions/sol-transfer-instruction";
|
|
7
|
+
export * from "./instructions/transfer-instruction";
|
|
8
|
+
export * from "./pool/pool-manager";
|
|
9
|
+
export * from "./transaction/bundle";
|
|
10
|
+
export * from "./transaction/execute-rpc";
|
|
11
|
+
export * from "./transaction/sign-tx";
|
|
12
|
+
export * from "./transaction/transaction-exectue";
|
|
13
|
+
export * from "./utils/budget";
|
|
14
|
+
export * from "./utils/error-helper";
|
|
15
|
+
export * from "./utils/get-balance";
|
|
16
|
+
export * from "./utils/util";
|
|
17
|
+
export * from "./errors";
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Connection,
|
|
3
|
+
Keypair,
|
|
4
|
+
PublicKey,
|
|
5
|
+
Signer,
|
|
6
|
+
Transaction,
|
|
7
|
+
TransactionInstruction,
|
|
8
|
+
TransactionMessage,
|
|
9
|
+
VersionedTransaction
|
|
10
|
+
} from "@solana/web3.js";
|
|
11
|
+
import { BuildInstructionError } from "../errors";
|
|
12
|
+
import {
|
|
13
|
+
LOOKUP_TABLE_CACHE,
|
|
14
|
+
TxVersion,
|
|
15
|
+
buildSimpleTransaction
|
|
16
|
+
} from "@raydium-io/raydium-sdk";
|
|
17
|
+
|
|
18
|
+
export interface TransactionInfo {
|
|
19
|
+
txn: VersionedTransaction;
|
|
20
|
+
singer: (Keypair | Signer)[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const compileInstToVersioned = async (
|
|
24
|
+
connection: Connection,
|
|
25
|
+
payer: Keypair,
|
|
26
|
+
insts: any[],
|
|
27
|
+
signer: (Keypair | Signer)[],
|
|
28
|
+
lookupAddr?: string
|
|
29
|
+
): Promise<TransactionInfo | undefined> => {
|
|
30
|
+
try {
|
|
31
|
+
const recentBlockhash = (await connection.getLatestBlockhash("finalized"))
|
|
32
|
+
.blockhash;
|
|
33
|
+
if (lookupAddr) {
|
|
34
|
+
const lookupTableAccount: any = (
|
|
35
|
+
await connection.getAddressLookupTable(new PublicKey(lookupAddr))
|
|
36
|
+
).value;
|
|
37
|
+
|
|
38
|
+
const txn = new VersionedTransaction(
|
|
39
|
+
new TransactionMessage({
|
|
40
|
+
payerKey: payer.publicKey,
|
|
41
|
+
instructions: insts,
|
|
42
|
+
recentBlockhash: recentBlockhash
|
|
43
|
+
}).compileToV0Message([lookupTableAccount])
|
|
44
|
+
);
|
|
45
|
+
return {
|
|
46
|
+
txn: txn,
|
|
47
|
+
singer: signer
|
|
48
|
+
};
|
|
49
|
+
} else {
|
|
50
|
+
const txn = new VersionedTransaction(
|
|
51
|
+
new TransactionMessage({
|
|
52
|
+
payerKey: payer.publicKey,
|
|
53
|
+
instructions: insts,
|
|
54
|
+
recentBlockhash: recentBlockhash
|
|
55
|
+
}).compileToV0Message()
|
|
56
|
+
);
|
|
57
|
+
return {
|
|
58
|
+
txn: txn,
|
|
59
|
+
singer: signer
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
} catch (error) {
|
|
63
|
+
throw new BuildInstructionError(`CompileInstToVersioned: ${error}`);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const buildInstToVersioned = async (
|
|
68
|
+
connection: Connection,
|
|
69
|
+
payer: Keypair,
|
|
70
|
+
inst: any
|
|
71
|
+
): Promise<(VersionedTransaction | Transaction)[]> => {
|
|
72
|
+
try {
|
|
73
|
+
const txn = await buildSimpleTransaction({
|
|
74
|
+
connection,
|
|
75
|
+
makeTxVersion: TxVersion.V0,
|
|
76
|
+
payer: payer.publicKey,
|
|
77
|
+
innerTransactions: inst,
|
|
78
|
+
recentBlockhash: (await connection.getLatestBlockhash("finalized"))
|
|
79
|
+
.blockhash,
|
|
80
|
+
addLookupTableInfo: LOOKUP_TABLE_CACHE
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return txn;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
throw new BuildInstructionError(`BuildInstToVersioned: ${error}`);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Liquidity,
|
|
3
|
+
ONE,
|
|
4
|
+
TxVersion,
|
|
5
|
+
parseBigNumberish
|
|
6
|
+
} from "@raydium-io/raydium-sdk";
|
|
7
|
+
import { Connection, Keypair, TokenAmount } from "@solana/web3.js";
|
|
8
|
+
import { getWalletTokenAccount } from "../utils/get-balance";
|
|
9
|
+
import { getComputeBudgetConfigHigh } from "../utils/budget";
|
|
10
|
+
import { GenerateTransactionError } from "../errors";
|
|
11
|
+
|
|
12
|
+
export const generateBuyInst = async (
|
|
13
|
+
connection: Connection,
|
|
14
|
+
poolKeys: any,
|
|
15
|
+
buyer: Keypair,
|
|
16
|
+
inputTokenAmount: any,
|
|
17
|
+
minAmountOut: any,
|
|
18
|
+
maxLamports: number = 100000
|
|
19
|
+
): Promise<any> => {
|
|
20
|
+
try {
|
|
21
|
+
const tokenAccountInfos = await getWalletTokenAccount(
|
|
22
|
+
connection,
|
|
23
|
+
buyer.publicKey
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const { innerTransactions } = await Liquidity.makeSwapInstructionSimple({
|
|
27
|
+
connection: connection,
|
|
28
|
+
poolKeys: { ...poolKeys },
|
|
29
|
+
userKeys: {
|
|
30
|
+
tokenAccounts: tokenAccountInfos,
|
|
31
|
+
owner: buyer.publicKey
|
|
32
|
+
},
|
|
33
|
+
amountIn: inputTokenAmount,
|
|
34
|
+
amountOut: minAmountOut,
|
|
35
|
+
config: {
|
|
36
|
+
bypassAssociatedCheck: false
|
|
37
|
+
},
|
|
38
|
+
fixedSide: "in",
|
|
39
|
+
makeTxVersion: TxVersion.V0,
|
|
40
|
+
|
|
41
|
+
computeBudgetConfig: {
|
|
42
|
+
microLamports: maxLamports
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const instructions = innerTransactions[0].instructions.filter(Boolean);
|
|
47
|
+
|
|
48
|
+
return instructions.slice(1);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
throw new GenerateTransactionError(`GenereateBuyInst: ${error}`);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AddressLookupTableProgram,
|
|
3
|
+
Connection,
|
|
4
|
+
Keypair,
|
|
5
|
+
PublicKey
|
|
6
|
+
} from "@solana/web3.js";
|
|
7
|
+
import { GenerateTransactionError } from "../errors";
|
|
8
|
+
|
|
9
|
+
export const generateCreateAltInst = async (
|
|
10
|
+
connection: Connection,
|
|
11
|
+
payer: Keypair
|
|
12
|
+
): Promise<any> => {
|
|
13
|
+
try {
|
|
14
|
+
return AddressLookupTableProgram.createLookupTable({
|
|
15
|
+
authority: payer.publicKey,
|
|
16
|
+
payer: payer.publicKey,
|
|
17
|
+
recentSlot: await connection.getSlot()
|
|
18
|
+
});
|
|
19
|
+
} catch (error) {
|
|
20
|
+
throw new GenerateTransactionError(`GenerateCreateAltInst: ${error}`);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const generateExtendAltInst = (
|
|
25
|
+
connection: Connection,
|
|
26
|
+
payer: Keypair,
|
|
27
|
+
lookupTableAddress: any,
|
|
28
|
+
addresses: PublicKey[]
|
|
29
|
+
) => {
|
|
30
|
+
try {
|
|
31
|
+
return AddressLookupTableProgram.extendLookupTable({
|
|
32
|
+
payer: payer.publicKey,
|
|
33
|
+
authority: payer.publicKey,
|
|
34
|
+
lookupTable: lookupTableAddress,
|
|
35
|
+
addresses: addresses
|
|
36
|
+
});
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new GenerateTransactionError(`GenerateExtendAltInst: ${error}`);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
3
|
+
TOKEN_PROGRAM_ID,
|
|
4
|
+
createAssociatedTokenAccountInstruction,
|
|
5
|
+
getAssociatedTokenAddressSync
|
|
6
|
+
} from "@solana/spl-token";
|
|
7
|
+
import { Keypair, PublicKey } from "@solana/web3.js";
|
|
8
|
+
|
|
9
|
+
export const getCreateAccountTransactionInst = (
|
|
10
|
+
payer: Keypair,
|
|
11
|
+
wallet: Keypair,
|
|
12
|
+
addr: string
|
|
13
|
+
) => {
|
|
14
|
+
const associatedToken = getAssociatedTokenAddressSync(
|
|
15
|
+
new PublicKey(addr),
|
|
16
|
+
wallet.publicKey,
|
|
17
|
+
true,
|
|
18
|
+
TOKEN_PROGRAM_ID,
|
|
19
|
+
ASSOCIATED_TOKEN_PROGRAM_ID
|
|
20
|
+
);
|
|
21
|
+
return createAssociatedTokenAccountInstruction(
|
|
22
|
+
payer.publicKey,
|
|
23
|
+
associatedToken,
|
|
24
|
+
wallet.publicKey,
|
|
25
|
+
new PublicKey(addr),
|
|
26
|
+
TOKEN_PROGRAM_ID,
|
|
27
|
+
ASSOCIATED_TOKEN_PROGRAM_ID
|
|
28
|
+
);
|
|
29
|
+
};
|