kit-squared 0.2.8 → 1.1.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 CHANGED
@@ -108,9 +108,9 @@ It also tracks the transaction state, and fires events when the tx state changes
108
108
  ### Sending Transactions
109
109
  Send a transaction with any instructions using
110
110
  ```typescript
111
- transact(ixs, names);
111
+ transact(ixs, names, preSimulate);
112
112
  ```
113
- Where `ixs` is an array of `@solana/kit` `Instruction`s, and `names` is an optional array of ix names that will be fired with each tx lifecycle event.
113
+ Where `ixs` is an array of `@solana/kit` `Instruction`s, and `names` is an optional array of ix names that will be fired with each tx lifecycle event. If `preSimulate` is `true`, the library will simulate the tx before requesting it, and cause this function fail if the tx will fail.
114
114
 
115
115
  ### Transaction Lifecycle
116
116
  Kit² only expects one tx to be in progress at any given time. While it doesn't prevent multiple simultaneous txs, the tx-lifecycle management is simplified for only one.
@@ -25,9 +25,6 @@ export const onConnect = function (callback, fireImmediatelyIfAlreadyConnected =
25
25
  }
26
26
  return onConnectEvent(callback);
27
27
  };
28
- // export const onDisconnect = function(callback: Callback){
29
- // if(disconnected)
30
- // }
31
28
  async function _checkSchedule(loopback = false) {
32
29
  if (checkingQueue && !loopback)
33
30
  return;
@@ -37,9 +37,7 @@ function _connect() {
37
37
  const { value: latestBlockhash } = await rpc.getLatestBlockhash().send({ abortSignal });
38
38
  const transactionMessage = pipe(createTransactionMessage({ version: 0 }), (message) => setTransactionMessageFeePayerSigner(feePayer, message), (message) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, message), (message) => appendTransactionMessageInstructions(instructions, message));
39
39
  assertIsTransactionMessageWithSingleSendingSigner(transactionMessage);
40
- console.log("AA1");
41
40
  const signatureBytes = await signAndSendTransactionMessageWithSigners(transactionMessage);
42
- console.log("AAB");
43
41
  const signature = signatureBytesToBase58String(signatureBytes);
44
42
  return signature;
45
43
  };
@@ -48,5 +48,5 @@ export type SimulationError = {
48
48
  anchorErrorLog: string;
49
49
  };
50
50
  export declare function simulate(ixs?: Instruction[]): Promise<Simulation>;
51
- export declare function transact(ixs?: Instruction[], names?: string[]): Promise<void>;
52
- export declare function transactSingle(ix: Instruction, name?: string | null): Promise<void>;
51
+ export declare function transact(ixs?: Instruction[], names?: string[], preSimulate?: boolean): Promise<void>;
52
+ export declare function transactSingle(ix: Instruction, name?: string | null, preSimulate?: boolean): Promise<void>;
@@ -40,7 +40,6 @@ function _txWasCancelled(e) {
40
40
  }
41
41
  return msg.includes('denied') || msg.includes('rejected') || msg.includes('cancelled');
42
42
  }
43
- let log = console.log;
44
43
  function _parseError(result) {
45
44
  const instructionPrefix = "Program log: Instruction: ";
46
45
  const errorPrefix = "Program log: AnchorError ";
@@ -109,7 +108,7 @@ export async function simulate(ixs = []) {
109
108
  replaceRecentBlockhash: true,
110
109
  sigVerify: false,
111
110
  minContextSlot: undefined,
112
- innerInstructions: undefined,
111
+ innerInstructions: true,
113
112
  accounts: undefined
114
113
  };
115
114
  const feePayer = signer;
@@ -132,11 +131,19 @@ export async function simulate(ixs = []) {
132
131
  success,
133
132
  error
134
133
  };
135
- console.log(simulateResult);
136
134
  }
137
- export async function transact(ixs = [], names = []) {
135
+ export async function transact(ixs = [], names = [], preSimulate = true) {
138
136
  _setTransacting(REQUESTED);
139
137
  onRequest.trigger(names);
138
+ if (preSimulate) {
139
+ const simResult = await simulate(ixs);
140
+ if (!simResult.success) {
141
+ console.error(simResult.error);
142
+ onFail.trigger(names);
143
+ _setTransacting(INITIAL);
144
+ return;
145
+ }
146
+ }
140
147
  const connection = getConnection();
141
148
  try {
142
149
  const signature = await connection.sendTransactionFromInstructionsWithWalletApp({
@@ -167,7 +174,7 @@ export async function transact(ixs = [], names = []) {
167
174
  _setTransacting(INITIAL);
168
175
  }
169
176
  }
170
- export async function transactSingle(ix, name = null) {
177
+ export async function transactSingle(ix, name = null, preSimulate = true) {
171
178
  let names = name ? [name] : [];
172
- return await transact([ix], names);
179
+ return await transact([ix], names, preSimulate);
173
180
  }
@@ -6,7 +6,7 @@ export declare function isNumberType(type: string): boolean;
6
6
  export declare const typeEncoder: {
7
7
  [key: string]: any;
8
8
  };
9
- export declare function address(str: string): any;
9
+ export declare function encodeAddress(str: string): any;
10
10
  export declare function camelToSnake(name: string): string;
11
11
  export declare function snakeToCamel(name: string): string;
12
12
  export declare function pascalToCamel(name: string): string;
@@ -19,23 +19,6 @@ export function isNumberType(type) {
19
19
  return types.includes(type) || isIntegerType(type);
20
20
  }
21
21
  // Encoders
22
- // export const typeEncoder = (()=>{
23
- // //TODO: types probably not complete
24
- //
25
- // // const types = ["isize","usize"];
26
- // const types = ["Address"];
27
- // for(let i = 0; i < 8; i++){
28
- // const s = 8 * (2 ** i);
29
- // types.push("U"+s);
30
- // types.push("I"+s);
31
- // }
32
- // const e = {};
33
- // for(let T of types){
34
- // const t = pascalToCamel(T);
35
- // e[t] = kit[`get${T}Encoder`]();
36
- // }
37
- // return e;
38
- // })();
39
22
  export const typeEncoder = (() => {
40
23
  const types = ["Address", "F32", "F64"];
41
24
  for (let i = 0; i < 5; i++) {
@@ -43,7 +26,6 @@ export const typeEncoder = (() => {
43
26
  types.push("U" + s);
44
27
  types.push("I" + s);
45
28
  }
46
- // log(types);
47
29
  const e = {};
48
30
  for (let T of types) {
49
31
  const t = pascalToCamel(T);
@@ -53,7 +35,7 @@ export const typeEncoder = (() => {
53
35
  }
54
36
  return e;
55
37
  })();
56
- export function address(str) {
38
+ export function encodeAddress(str) {
57
39
  return typeEncoder.address.encode(str);
58
40
  }
59
41
  export function camelToSnake(name) {
@@ -110,9 +92,6 @@ export function EventSingle() {
110
92
  return onEvent;
111
93
  }
112
94
  export function Event() {
113
- // returns onEvent function,
114
- // this function returns another func which you use to unsubscribe from event
115
- // onEvent.triggger(..args) triggers the event
116
95
  let cid = 0;
117
96
  let callbacks = {};
118
97
  function onEvent(callback) {
@@ -162,7 +141,6 @@ export async function getPDA(seeds, programId) {
162
141
  throw new Error("Not a valid number type: " + t);
163
142
  }
164
143
  seeds_parsed.push(typeEncoder[t].encode(value));
165
- // if(!isIntegerType)
166
144
  }
167
145
  else {
168
146
  throw new Error("Unable to interpret seed: " + seed);
@@ -177,7 +155,6 @@ export async function getPDA(seeds, programId) {
177
155
  }
178
156
  export function lamportsToSol(lamports) {
179
157
  function _trimTail(str) {
180
- let clean = "";
181
158
  let i = str.indexOf(".");
182
159
  if (i === -1) {
183
160
  return str;
@@ -20,7 +20,6 @@ const INITIAL = WALLET_STATE.INITIAL;
20
20
  const CONNECTING = WALLET_STATE.CONNECTING;
21
21
  const CONNECTED = WALLET_STATE.CONNECTED;
22
22
  const ERROR = WALLET_STATE.ERROR;
23
- let log = console.log;
24
23
  export let walletState = writable(WALLET_STATE.INITIAL);
25
24
  function _setWalletState(state) {
26
25
  walletState.set(state);
@@ -84,8 +83,6 @@ async function connectWallet(uiWallet) {
84
83
  const accountsPromise = connectFeature
85
84
  .connect()
86
85
  .then(({ accounts }) => {
87
- // console.log("connected", accounts);
88
- // return accounts;
89
86
  return accounts.map(getOrCreateUiWalletAccountForStandardWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.bind(null, wallet));
90
87
  });
91
88
  const nextAccounts = await accountsPromise;
@@ -161,9 +158,7 @@ function _buildTransactionSendingSigner(_networkType) {
161
158
  chain,
162
159
  account: walletAccount,
163
160
  };
164
- log(111);
165
161
  const [{ signature }] = await getAbortablePromise(signAndSendTransactionFeature.signAndSendTransaction(inputWithOptions), abortSignal);
166
- log(222);
167
162
  return Object.freeze([signature]);
168
163
  },
169
164
  };
@@ -73,8 +73,6 @@ let walletWatchInterval = setInterval(async () => {
73
73
  else {
74
74
  // disconnect then reconnect
75
75
  console.error("This would be a disconnect then reconnect..");
76
- // log("Apparent address:",apparentAddress);
77
- // log("selected address:", selectedWalletAddress)
78
76
  const selectedWalletName = outputWallets[selectedWalletIndex].name;
79
77
  await disconnectWallet();
80
78
  await selectWallet(selectedWalletName);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kit-squared",
3
- "version": "0.2.8",
3
+ "version": "1.1.0",
4
4
  "description": "Solana Kit functionality with SvelteKit",
5
5
  "license": "ISC",
6
6
  "author": "AnAllergyToAnalogy",