@hawksightco/hawk-sdk 1.1.7 → 1.1.8
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/dist/src/classes/HawkAPI.d.ts +7 -0
- package/dist/src/classes/HawkAPI.d.ts.map +1 -1
- package/dist/src/classes/HawkAPI.js +9 -0
- package/dist/src/classes/TransactionBatchExecute.d.ts +113 -0
- package/dist/src/classes/TransactionBatchExecute.d.ts.map +1 -0
- package/dist/src/classes/TransactionBatchExecute.js +294 -0
- package/package.json +1 -1
|
@@ -8,6 +8,7 @@ import { GeneralUtility } from "./GeneralUtility";
|
|
|
8
8
|
import { Search } from "./Search";
|
|
9
9
|
import { HawkApiOptions } from "../types";
|
|
10
10
|
import { SimpleIxGenerator } from "./SimpleIxGenerator";
|
|
11
|
+
import { TransactionBatchExecute, TransactionBatchExecuteParams } from "./TransactionBatchExecute";
|
|
11
12
|
/**
|
|
12
13
|
* HawkAPI is a central gateway class that aggregates access to various functional modules
|
|
13
14
|
* for interacting with HawkSight's blockchain APIs. This class initializes and exposes modules
|
|
@@ -50,5 +51,11 @@ export declare class HawkAPI {
|
|
|
50
51
|
* @param txGenerator
|
|
51
52
|
*/
|
|
52
53
|
overrideTxGenerator(builderFn: (client: Client, generalUtility: GeneralUtility) => TxGenerator): void;
|
|
54
|
+
/**
|
|
55
|
+
* Create instance of TransactionBatchExecute
|
|
56
|
+
*
|
|
57
|
+
* @param param
|
|
58
|
+
*/
|
|
59
|
+
batchExecute({ lookupTableAddresses, instructions, payer, connection, signers }: TransactionBatchExecuteParams): TransactionBatchExecute;
|
|
53
60
|
}
|
|
54
61
|
//# sourceMappingURL=HawkAPI.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HawkAPI.d.ts","sourceRoot":"","sources":["../../../src/classes/HawkAPI.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"HawkAPI.d.ts","sourceRoot":"","sources":["../../../src/classes/HawkAPI.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,uBAAuB,EAAE,6BAA6B,EAAE,MAAM,2BAA2B,CAAC;AAEnG;;;;;;;;GAQG;AACH,qBAAa,OAAO;IAkChB,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM;IAC9B,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC;IAlC7B,+BAA+B;IAC/B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAEhC,iEAAiE;IACjE,SAAgB,MAAM,EAAE,MAAM,CAAC;IAE/B,6GAA6G;IAC7G,SAAgB,OAAO,EAAE,OAAO,CAAC;IAEjC,+BAA+B;IAC/B,SAAgB,cAAc,EAAE,cAAc,CAAC;IAE/C,0FAA0F;IAC1F,SAAgB,IAAI,EAAE,IAAI,CAAC;IAE3B,mFAAmF;IACnF,OAAO,CAAC,YAAY,CAAc;IAClC,IAAI,WAAW,gBAAgC;IAE/C,kGAAkG;IAClG,SAAgB,qBAAqB,EAAE,sBAAsB,CAAC;IAE9D,mDAAmD;IACnD,SAAgB,MAAM,EAAE,MAAM,CAAC;IAE/B,mCAAmC;IACnC,SAAgB,iBAAiB,EAAE,iBAAiB,CAAC;IAErD;;;OAGG;gBAEkB,GAAG,GAAE,MAAoC,EACzC,OAAO,CAAC,4BAAgB;IAwB7C;;;;OAIG;IACH,mBAAmB,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,cAAc,KAAK,WAAW;IAI9F;;;;OAIG;IACH,YAAY,CAAC,EAAC,oBAAoB,EAAE,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,OAAO,EAAC,EAAE,6BAA6B;CAS7G"}
|
|
@@ -11,6 +11,7 @@ const GeneralUtility_1 = require("./GeneralUtility");
|
|
|
11
11
|
const Search_1 = require("./Search");
|
|
12
12
|
const CreateTxMetadata_1 = require("./CreateTxMetadata");
|
|
13
13
|
const SimpleIxGenerator_1 = require("./SimpleIxGenerator");
|
|
14
|
+
const TransactionBatchExecute_1 = require("./TransactionBatchExecute");
|
|
14
15
|
/**
|
|
15
16
|
* HawkAPI is a central gateway class that aggregates access to various functional modules
|
|
16
17
|
* for interacting with HawkSight's blockchain APIs. This class initializes and exposes modules
|
|
@@ -56,5 +57,13 @@ class HawkAPI {
|
|
|
56
57
|
overrideTxGenerator(builderFn) {
|
|
57
58
|
this._txGenerator = builderFn(this.client, this.generalUtility);
|
|
58
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Create instance of TransactionBatchExecute
|
|
62
|
+
*
|
|
63
|
+
* @param param
|
|
64
|
+
*/
|
|
65
|
+
batchExecute({ lookupTableAddresses, instructions, payer, connection, signers }) {
|
|
66
|
+
return new TransactionBatchExecute_1.TransactionBatchExecute(lookupTableAddresses, instructions, payer, connection, signers);
|
|
67
|
+
}
|
|
59
68
|
}
|
|
60
69
|
exports.HawkAPI = HawkAPI;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/// <reference types="@meteora-ag/dlmm/node_modules/@solana/web3.js" />
|
|
2
|
+
import * as web3 from '@solana/web3.js';
|
|
3
|
+
/**
|
|
4
|
+
* Dummy signers
|
|
5
|
+
*/
|
|
6
|
+
export type DummySigners = Record<string, web3.Keypair>;
|
|
7
|
+
/**
|
|
8
|
+
* Transaction batch
|
|
9
|
+
*/
|
|
10
|
+
export type Batch = {
|
|
11
|
+
addressLookupTables: web3.PublicKey[];
|
|
12
|
+
instructions: web3.PublicKey[];
|
|
13
|
+
signers: web3.Keypair;
|
|
14
|
+
};
|
|
15
|
+
export type TransactionBatchExecuteParams = {
|
|
16
|
+
lookupTableAddresses: web3.PublicKey[];
|
|
17
|
+
instructions: web3.TransactionInstruction[];
|
|
18
|
+
payer: web3.Keypair;
|
|
19
|
+
connection: web3.Connection;
|
|
20
|
+
signers: web3.Keypair[];
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Executes transactions in batches
|
|
24
|
+
*/
|
|
25
|
+
export declare class TransactionBatchExecute {
|
|
26
|
+
private lookupTableAddresses;
|
|
27
|
+
private instructions;
|
|
28
|
+
private payer;
|
|
29
|
+
private connection;
|
|
30
|
+
private signers;
|
|
31
|
+
private readonly MAX_SIZE;
|
|
32
|
+
/**
|
|
33
|
+
* Downloaded address lookup table accounts
|
|
34
|
+
*/
|
|
35
|
+
private alts;
|
|
36
|
+
/**
|
|
37
|
+
* Creates an instance of TransactionExecute class
|
|
38
|
+
*
|
|
39
|
+
* @param lookupTableAddresses Address lookup table addresses to be used by batch of transactions
|
|
40
|
+
* @param instructions Array of instructions to be batched into transactions
|
|
41
|
+
* @param payer Payer that will pay for the batch of transactions
|
|
42
|
+
* @param connection Connection to mainnet
|
|
43
|
+
* @param signers Required signers to fulfill the transactions
|
|
44
|
+
*/
|
|
45
|
+
constructor(lookupTableAddresses: web3.PublicKey[], instructions: web3.TransactionInstruction[], payer: web3.Keypair, connection: web3.Connection, signers?: web3.Keypair[]);
|
|
46
|
+
/**
|
|
47
|
+
* Downoad address lookup table from given cluster
|
|
48
|
+
*/
|
|
49
|
+
downloadAlts(): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Override signers (in case that signers are set to empty array)
|
|
52
|
+
*
|
|
53
|
+
* @param signers
|
|
54
|
+
*/
|
|
55
|
+
setSigners(signers: web3.Keypair[]): void;
|
|
56
|
+
/**
|
|
57
|
+
* Find missing signers
|
|
58
|
+
*/
|
|
59
|
+
findMissingSigners(): web3.PublicKey[];
|
|
60
|
+
/**
|
|
61
|
+
* Returns list of public keys that are required to sign the batch of transaction
|
|
62
|
+
*/
|
|
63
|
+
findRequiredSigningPubkeys(): web3.PublicKey[];
|
|
64
|
+
/**
|
|
65
|
+
* Generate dummy signers needed for simulation
|
|
66
|
+
*
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
generateDummySigners(): DummySigners;
|
|
70
|
+
/**
|
|
71
|
+
* Generate instruction that has modified signers for simulation purposes
|
|
72
|
+
*/
|
|
73
|
+
generateSimulationIxs(): [web3.TransactionInstruction[], DummySigners];
|
|
74
|
+
/**
|
|
75
|
+
* Builds a batch of executable transaction instructions
|
|
76
|
+
*/
|
|
77
|
+
buildBatch(): Promise<web3.TransactionInstruction[][]>;
|
|
78
|
+
/**
|
|
79
|
+
* Split transactions
|
|
80
|
+
*
|
|
81
|
+
* @param simulationIxs
|
|
82
|
+
* @param dummySigners
|
|
83
|
+
* @returns
|
|
84
|
+
*/
|
|
85
|
+
private splitToTransactions;
|
|
86
|
+
/**
|
|
87
|
+
* Calculate transaction size
|
|
88
|
+
*
|
|
89
|
+
* @param batch
|
|
90
|
+
*/
|
|
91
|
+
private calculateTransactionSize;
|
|
92
|
+
/**
|
|
93
|
+
* Find required signers from batch
|
|
94
|
+
*
|
|
95
|
+
* @param ixs
|
|
96
|
+
* @param dummySigners
|
|
97
|
+
*/
|
|
98
|
+
private findRequiredSignersFromBatch;
|
|
99
|
+
/**
|
|
100
|
+
* Find required signers from instructions
|
|
101
|
+
*
|
|
102
|
+
* @param ixs
|
|
103
|
+
* @param dummySigners
|
|
104
|
+
*/
|
|
105
|
+
private findRequiredSignersFromIxs;
|
|
106
|
+
/**
|
|
107
|
+
* Find required address lookup table addresses for given batch of instructions
|
|
108
|
+
*
|
|
109
|
+
* @param batch
|
|
110
|
+
*/
|
|
111
|
+
private findRequiredAltsForBatch;
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=TransactionBatchExecute.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TransactionBatchExecute.d.ts","sourceRoot":"","sources":["../../../src/classes/TransactionBatchExecute.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAC;AAExC;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;AAExD;;GAEG;AACH,MAAM,MAAM,KAAK,GAAG;IAClB,mBAAmB,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC;IACtC,YAAY,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC;IAC/B,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC;CACvB,CAAA;AAED,MAAM,MAAM,6BAA6B,GAAG;IAC1C,oBAAoB,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC;IACvC,YAAY,EAAE,IAAI,CAAC,sBAAsB,EAAE,CAAC;IAC5C,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC;IACpB,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC;IAC5B,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC;CACzB,CAAA;AAED;;GAEG;AACH,qBAAa,uBAAuB;IAmBhC,OAAO,CAAC,oBAAoB;IAC5B,OAAO,CAAC,YAAY;IACpB,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,OAAO;IArBjB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IAEjC;;OAEG;IACH,OAAO,CAAC,IAAI,CAAwC;IAEpD;;;;;;;;OAQG;gBAEO,oBAAoB,EAAE,IAAI,CAAC,SAAS,EAAE,EACtC,YAAY,EAAE,IAAI,CAAC,sBAAsB,EAAE,EAC3C,KAAK,EAAE,IAAI,CAAC,OAAO,EACnB,UAAU,EAAE,IAAI,CAAC,UAAU,EAC3B,OAAO,GAAE,IAAI,CAAC,OAAO,EAAO;IAGtC;;OAEG;IACG,YAAY;IAclB;;;;OAIG;IACH,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE;IAIlC;;OAEG;IACH,kBAAkB,IAAI,IAAI,CAAC,SAAS,EAAE;IActC;;OAEG;IACH,0BAA0B,IAAI,IAAI,CAAC,SAAS,EAAE;IAI9C;;;;OAIG;IACH,oBAAoB,IAAI,YAAY;IASpC;;OAEG;IACH,qBAAqB,IAAI,CAAC,IAAI,CAAC,sBAAsB,EAAE,EAAE,YAAY,CAAC;IAiBtE;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,sBAAsB,EAAE,EAAE,CAAC;IAwB5D;;;;;;OAMG;YACW,mBAAmB;IA8BjC;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAchC;;;;;OAKG;IACH,OAAO,CAAC,4BAA4B;IAapC;;;;;OAKG;IACH,OAAO,CAAC,0BAA0B;IAqBlC;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;CA2BjC"}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.TransactionBatchExecute = void 0;
|
|
36
|
+
const web3 = __importStar(require("@solana/web3.js"));
|
|
37
|
+
/**
|
|
38
|
+
* Executes transactions in batches
|
|
39
|
+
*/
|
|
40
|
+
class TransactionBatchExecute {
|
|
41
|
+
/**
|
|
42
|
+
* Creates an instance of TransactionExecute class
|
|
43
|
+
*
|
|
44
|
+
* @param lookupTableAddresses Address lookup table addresses to be used by batch of transactions
|
|
45
|
+
* @param instructions Array of instructions to be batched into transactions
|
|
46
|
+
* @param payer Payer that will pay for the batch of transactions
|
|
47
|
+
* @param connection Connection to mainnet
|
|
48
|
+
* @param signers Required signers to fulfill the transactions
|
|
49
|
+
*/
|
|
50
|
+
constructor(lookupTableAddresses, instructions, payer, connection, signers = []) {
|
|
51
|
+
this.lookupTableAddresses = lookupTableAddresses;
|
|
52
|
+
this.instructions = instructions;
|
|
53
|
+
this.payer = payer;
|
|
54
|
+
this.connection = connection;
|
|
55
|
+
this.signers = signers;
|
|
56
|
+
this.MAX_SIZE = 1232;
|
|
57
|
+
/**
|
|
58
|
+
* Downloaded address lookup table accounts
|
|
59
|
+
*/
|
|
60
|
+
this.alts = [];
|
|
61
|
+
}
|
|
62
|
+
;
|
|
63
|
+
/**
|
|
64
|
+
* Downoad address lookup table from given cluster
|
|
65
|
+
*/
|
|
66
|
+
downloadAlts() {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
// Skip if we already downloaded alts from the cluster
|
|
69
|
+
if (this.alts.length > 0 && this.alts.length === this.lookupTableAddresses.length)
|
|
70
|
+
return;
|
|
71
|
+
// Download if address lookup table does not match with downloaded alts
|
|
72
|
+
for (const lookupTable of this.lookupTableAddresses) {
|
|
73
|
+
const alt = yield this.connection.getAddressLookupTable(lookupTable);
|
|
74
|
+
if (alt.value === null) {
|
|
75
|
+
throw new Error(`Address lookup table: ${lookupTable} does not exist on the blockchain.`);
|
|
76
|
+
}
|
|
77
|
+
this.alts.push(alt.value);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Override signers (in case that signers are set to empty array)
|
|
83
|
+
*
|
|
84
|
+
* @param signers
|
|
85
|
+
*/
|
|
86
|
+
setSigners(signers) {
|
|
87
|
+
this.signers = signers;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Find missing signers
|
|
91
|
+
*/
|
|
92
|
+
findMissingSigners() {
|
|
93
|
+
const requiredSigners = this.findRequiredSigningPubkeys();
|
|
94
|
+
if (requiredSigners.length > this.signers.length) {
|
|
95
|
+
return requiredSigners;
|
|
96
|
+
}
|
|
97
|
+
const missingSigners = [];
|
|
98
|
+
for (const requiredSigner of requiredSigners) {
|
|
99
|
+
const match = this.signers.find(v => v.publicKey.toBase58() === requiredSigner.toBase58());
|
|
100
|
+
if (!!match)
|
|
101
|
+
continue;
|
|
102
|
+
missingSigners.push(requiredSigner);
|
|
103
|
+
}
|
|
104
|
+
return missingSigners;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Returns list of public keys that are required to sign the batch of transaction
|
|
108
|
+
*/
|
|
109
|
+
findRequiredSigningPubkeys() {
|
|
110
|
+
return this.findRequiredSignersFromIxs(this.instructions);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Generate dummy signers needed for simulation
|
|
114
|
+
*
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
generateDummySigners() {
|
|
118
|
+
const requiredSigners = this.findRequiredSigningPubkeys();
|
|
119
|
+
const dummySigners = {};
|
|
120
|
+
for (const signer of requiredSigners) {
|
|
121
|
+
dummySigners[signer.toString()] = web3.Keypair.generate();
|
|
122
|
+
}
|
|
123
|
+
return dummySigners;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Generate instruction that has modified signers for simulation purposes
|
|
127
|
+
*/
|
|
128
|
+
generateSimulationIxs() {
|
|
129
|
+
const dummySigners = this.generateDummySigners();
|
|
130
|
+
let simulationIxs = this.instructions;
|
|
131
|
+
for (const pubkey in dummySigners) {
|
|
132
|
+
simulationIxs = simulationIxs.map(ix => {
|
|
133
|
+
const keys = ix.keys.map(meta => {
|
|
134
|
+
if (meta.pubkey.toString() === pubkey && meta.isSigner) {
|
|
135
|
+
return Object.assign(Object.assign({}, meta), { pubkey: dummySigners[pubkey].publicKey });
|
|
136
|
+
}
|
|
137
|
+
return meta;
|
|
138
|
+
});
|
|
139
|
+
return Object.assign(Object.assign({}, ix), { keys });
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return [simulationIxs, dummySigners];
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Builds a batch of executable transaction instructions
|
|
146
|
+
*/
|
|
147
|
+
buildBatch() {
|
|
148
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
149
|
+
// Download address lookup table from given cluster
|
|
150
|
+
yield this.downloadAlts();
|
|
151
|
+
// Generate dummy instructions for batch calculation
|
|
152
|
+
const [simulationIxs, dummySigners] = this.generateSimulationIxs();
|
|
153
|
+
// Split simulation ixs
|
|
154
|
+
const dummyBatches = yield this.splitToTransactions(simulationIxs, dummySigners);
|
|
155
|
+
// Batch of actual instructions
|
|
156
|
+
const batch = [];
|
|
157
|
+
let index = 0;
|
|
158
|
+
for (const dummyIxs of dummyBatches) {
|
|
159
|
+
const currentBatch = [];
|
|
160
|
+
for (const dummyIx of dummyIxs) {
|
|
161
|
+
currentBatch.push(this.instructions[index++]);
|
|
162
|
+
}
|
|
163
|
+
batch.push(currentBatch);
|
|
164
|
+
}
|
|
165
|
+
return batch;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Split transactions
|
|
170
|
+
*
|
|
171
|
+
* @param simulationIxs
|
|
172
|
+
* @param dummySigners
|
|
173
|
+
* @returns
|
|
174
|
+
*/
|
|
175
|
+
splitToTransactions(simulationIxs, dummySigners) {
|
|
176
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
+
// Fetch latest blockhash required to calculate transaction size
|
|
178
|
+
const latestBlockhash = yield this.connection.getLatestBlockhash();
|
|
179
|
+
// Run simulation
|
|
180
|
+
let batch = [];
|
|
181
|
+
let result = [];
|
|
182
|
+
for (const ix of simulationIxs) {
|
|
183
|
+
// Include dummy ix to transaction
|
|
184
|
+
batch.push(ix);
|
|
185
|
+
// Calculate transaction size
|
|
186
|
+
const txSize = this.calculateTransactionSize(latestBlockhash, batch, dummySigners);
|
|
187
|
+
if (txSize > this.MAX_SIZE) {
|
|
188
|
+
batch.pop();
|
|
189
|
+
result.push(batch);
|
|
190
|
+
batch = [ix];
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (batch.length > 0) {
|
|
194
|
+
result.push(batch);
|
|
195
|
+
}
|
|
196
|
+
return result;
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Calculate transaction size
|
|
201
|
+
*
|
|
202
|
+
* @param batch
|
|
203
|
+
*/
|
|
204
|
+
calculateTransactionSize(latestBlockhash, batch, dummySigners) {
|
|
205
|
+
const alts = this.findRequiredAltsForBatch(batch);
|
|
206
|
+
const messageV0 = new web3.TransactionMessage({
|
|
207
|
+
payerKey: this.payer.publicKey,
|
|
208
|
+
instructions: batch,
|
|
209
|
+
recentBlockhash: latestBlockhash.blockhash,
|
|
210
|
+
}).compileToV0Message(alts);
|
|
211
|
+
const versionedTx = new web3.VersionedTransaction(messageV0);
|
|
212
|
+
const signers = this.findRequiredSignersFromBatch(batch, dummySigners);
|
|
213
|
+
versionedTx.sign(signers);
|
|
214
|
+
const bytes = versionedTx.serialize();
|
|
215
|
+
return bytes.length;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Find required signers from batch
|
|
219
|
+
*
|
|
220
|
+
* @param ixs
|
|
221
|
+
* @param dummySigners
|
|
222
|
+
*/
|
|
223
|
+
findRequiredSignersFromBatch(batch, dummySigners) {
|
|
224
|
+
const signers = this.findRequiredSignersFromIxs(batch);
|
|
225
|
+
const result = [];
|
|
226
|
+
for (const key in dummySigners) {
|
|
227
|
+
const dummySigner = signers.find(signer => signer.toBase58() === dummySigners[key].publicKey.toBase58());
|
|
228
|
+
if (dummySigner === undefined) {
|
|
229
|
+
throw new Error('Unexpected error: Dummy signer not found (it should never happen)');
|
|
230
|
+
}
|
|
231
|
+
result.push(dummySigners[key]);
|
|
232
|
+
}
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Find required signers from instructions
|
|
237
|
+
*
|
|
238
|
+
* @param ixs
|
|
239
|
+
* @param dummySigners
|
|
240
|
+
*/
|
|
241
|
+
findRequiredSignersFromIxs(ixs) {
|
|
242
|
+
const signers = [];
|
|
243
|
+
for (const instruction of ixs) {
|
|
244
|
+
const _signers = instruction.keys
|
|
245
|
+
.filter(meta => meta.isSigner)
|
|
246
|
+
.map(meta => meta.pubkey);
|
|
247
|
+
if (_signers.length > 0) {
|
|
248
|
+
signers.push(..._signers);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
const uniqueSigners = {};
|
|
252
|
+
for (const signer of signers) {
|
|
253
|
+
uniqueSigners[signer.toBase58()] = signer;
|
|
254
|
+
}
|
|
255
|
+
const result = [];
|
|
256
|
+
for (const id in uniqueSigners) {
|
|
257
|
+
result.push(uniqueSigners[id]);
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Find required address lookup table addresses for given batch of instructions
|
|
263
|
+
*
|
|
264
|
+
* @param batch
|
|
265
|
+
*/
|
|
266
|
+
findRequiredAltsForBatch(batch) {
|
|
267
|
+
const uniquePubkeys = {};
|
|
268
|
+
batch.map(ix => {
|
|
269
|
+
const pubkeys = ix.keys.map(meta => meta.pubkey);
|
|
270
|
+
for (const pubkey of pubkeys) {
|
|
271
|
+
uniquePubkeys[pubkey.toBase58()] = 1;
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
const pubkeys = [];
|
|
275
|
+
for (const pubkey in uniquePubkeys) {
|
|
276
|
+
pubkeys.push(new web3.PublicKey(pubkey));
|
|
277
|
+
}
|
|
278
|
+
const alts = {};
|
|
279
|
+
for (const alt of this.alts) {
|
|
280
|
+
for (const pubkey of pubkeys) {
|
|
281
|
+
const match = alt.state.addresses.find(address => pubkey.toBase58() === address.toBase58());
|
|
282
|
+
if (!!match) {
|
|
283
|
+
alts[alt.key.toString()] = alt;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const result = [];
|
|
288
|
+
for (const key in alts) {
|
|
289
|
+
result.push(alts[key]);
|
|
290
|
+
}
|
|
291
|
+
return result;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
exports.TransactionBatchExecute = TransactionBatchExecute;
|