kit-squared 0.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 +218 -0
- package/dist/WalletSelection.svelte +120 -0
- package/dist/WalletSelection.svelte.d.ts +18 -0
- package/dist/components/walletSelectionComponent.d.ts +4 -0
- package/dist/components/walletSelectionComponent.js +33 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +15 -0
- package/dist/library/connectSchedule.d.ts +16 -0
- package/dist/library/connectSchedule.js +51 -0
- package/dist/library/connection.d.ts +31 -0
- package/dist/library/connection.js +69 -0
- package/dist/library/program.d.ts +54 -0
- package/dist/library/program.js +431 -0
- package/dist/library/utils.d.ts +50 -0
- package/dist/library/utils.js +297 -0
- package/dist/library/wallet.d.ts +37 -0
- package/dist/library/wallet.js +233 -0
- package/dist/library/walletSelection.d.ts +31 -0
- package/dist/library/walletSelection.js +136 -0
- package/package.json +68 -0
package/README.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# Kit² ( Kit Squared )
|
|
2
|
+
|
|
3
|
+
A library that brings Solana Kit functionality to SvelteKit.
|
|
4
|
+
|
|
5
|
+
Kit² provides wallet connection, transaction functionality (including helpers) and features such as account management as well as program helpers (interfaces) for Anchor programs. It uses `@wallet-standard` for dealing with wallets.
|
|
6
|
+
|
|
7
|
+
This library is in development, in a pre-release state. If you stumble across it and find any issues, please let me know via [GitHub](https://github.com/anallergyToAnalogy/kit-squared).
|
|
8
|
+
|
|
9
|
+
[Skip to Contents](#contents)
|
|
10
|
+
|
|
11
|
+
<!-- [Skip to Example Project](---) -->
|
|
12
|
+
|
|
13
|
+
## Initialising
|
|
14
|
+
|
|
15
|
+
To initialise the library, you must initialise it with your RPC urls and specify the network type:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
init(http, ws, networkType)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Where `http` and `ws` are the respective RPC urls, and `networkType` is either `"mainnet"`,`"devnet"` or `"testnet"`.
|
|
22
|
+
|
|
23
|
+
Full details available in the [Initialisation section of the docs](https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/framework/INITIALISATION.md).
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
## Wallet Connection
|
|
27
|
+
|
|
28
|
+
The Kit² library watches for injected wallets (ie, Phantom, Metamask, etc.).
|
|
29
|
+
|
|
30
|
+
To connect a specific wallet:
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
selectWallet(name)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Where `name` is a string with the name of the injected wallet. The names of all available injected wallets are provided by an array `availableWallets`.
|
|
37
|
+
|
|
38
|
+
The library manages the lifecycle of the connection/disconnection process, and allows you to register callbacks for when those processes complete:
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
onConnect(()=>{
|
|
42
|
+
//do some stuff when the wallet connects
|
|
43
|
+
});
|
|
44
|
+
onDisconnect(()=>{
|
|
45
|
+
//do some stuff when the wallet disconnects
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Full details available in the [Wallet section of the docs](https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/framework/WALLET.md).
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
## Programs
|
|
54
|
+
|
|
55
|
+
To create an interface/helper for a program, both its IDL and Codama-generated program client must be provided. A signer object (provided by wallet connection) may also be provided, but if it is absent this helper can not be used to send transactions.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
createProgram(programClient, idl, signer)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
This program helper has methods that make it easy to
|
|
62
|
+
|
|
63
|
+
- Send transactions
|
|
64
|
+
- Create Instructions
|
|
65
|
+
- Subscribe to Events
|
|
66
|
+
- Get PDA addesses, and
|
|
67
|
+
- Read Accounts
|
|
68
|
+
|
|
69
|
+
_example:_
|
|
70
|
+
```typescript
|
|
71
|
+
//Create the helper
|
|
72
|
+
const myProgram = createProgram(yourProgramClient, yourIdl, signerFromConnectedWallet);
|
|
73
|
+
|
|
74
|
+
// Send a myFunction tx
|
|
75
|
+
await myProgram.tx.myFunction(somePara, someOtherParam);
|
|
76
|
+
|
|
77
|
+
// Build a myFunction ix for sending later
|
|
78
|
+
const ix = await myProgram.ix.myFunction(somePara, someOtherParam);
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
// Subscribe to an event called someEvent
|
|
82
|
+
myProgram.on('someEvent', (eventData, slotNumber, signature) => {
|
|
83
|
+
const {someParam, someOtherParam} = eventData;
|
|
84
|
+
|
|
85
|
+
//do some stuff with someParam and someOtherParam
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
// Get the address of a PDA
|
|
90
|
+
const someAccountAddress = await myProgram.pda(["seeds","for","PDA"]);
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
// Read the value of an account the program owns
|
|
94
|
+
// You can either pass the address if you already know it
|
|
95
|
+
const accountData = await myProgram.account.myAccountType(address);
|
|
96
|
+
|
|
97
|
+
// or you can pass the seeds like in the PDA function
|
|
98
|
+
const accountData = await myProgram.account.myAccountType(["seeds","for","another","PDA"]);
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Full details available in the [Program section of the docs](https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/framework/PROGRAM.md).
|
|
103
|
+
|
|
104
|
+
## Transactions
|
|
105
|
+
|
|
106
|
+
Kit² provides functions for sending transactions, both as #methods of program helpers and generic ones for sending any instructions.
|
|
107
|
+
|
|
108
|
+
It also tracks the transaction state, and fires events when the tx state changes.
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
### Sending Transactions
|
|
112
|
+
Send a transaction with any instructions using
|
|
113
|
+
```typescript
|
|
114
|
+
transact(ixs, names);
|
|
115
|
+
```
|
|
116
|
+
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.
|
|
117
|
+
|
|
118
|
+
### Transaction Lifecycle
|
|
119
|
+
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.
|
|
120
|
+
|
|
121
|
+
#### transactionState
|
|
122
|
+
|
|
123
|
+
The library provides a store with the current transaction state, `$transactionState`, which can take the following values:
|
|
124
|
+
|
|
125
|
+
- `"INITIAL"` - No transaction currently in progress
|
|
126
|
+
- `"REQUESTED"` - Transaction has been requested by library but not submitted by user
|
|
127
|
+
- `"PENDING"` - Transaction has been submitted by user and is awaiting confirmation or failure.
|
|
128
|
+
|
|
129
|
+
Note, when a tx is confirmed or fails, it will revert to `"INITIAL"` state.
|
|
130
|
+
|
|
131
|
+
#### Lifecycle Events
|
|
132
|
+
The library fires events every time the tx changes state. You may register callbacks for any of these, with the following format:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
callback = (names: string[])={
|
|
136
|
+
//do some stuff
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The lifecycle events are:
|
|
141
|
+
- `onTransaction.request(callback)` - A tx was requested
|
|
142
|
+
- `onTransaction.submit(callback)` - A tx was submitted by user (they clicked send tx on the wallet)
|
|
143
|
+
- `onTransaction.confirm(callback)` - A tx was confirmed on-chain
|
|
144
|
+
- `onTransaction.cancel(callback)` - A tx was cancelled by the user
|
|
145
|
+
- `onTransaction.fail(callback)` - A tx failed
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
Full details available in the Transaction section of the docs
|
|
149
|
+
|
|
150
|
+
## Accounts
|
|
151
|
+
|
|
152
|
+
Kit² allows you to easily manage accounts included in a program tx. Where possible, program function and IX invocations will infer account addresses from the IDL/client. But in cases where it isn't possible, or if you want to overwrite the inferred account addresses, you can provide them with `addAccounts`.
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
addAccounts({
|
|
156
|
+
accountName: accountAddress,
|
|
157
|
+
otherAccountName: otherAccountAddress,
|
|
158
|
+
})
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
These will be included with any program ix or tx until they are overwritten, or removed with
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
clearAddedAccounts()
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Full details available in the [Accounts section of the docs]( https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/framework/ACCOUNTS.md).
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
## Integer types and PDAs
|
|
171
|
+
|
|
172
|
+
Kit² uses BigInts for dealing with integers, and will be accepted for all integer function params, and be used in all returned integer values.
|
|
173
|
+
|
|
174
|
+
However, since PDAs pack data in a very specific way when deriving the address, when adding integers as seeds for PDAs, use the following format:
|
|
175
|
+
```typescript
|
|
176
|
+
[value, size_in_bits]
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
so a `u16` with the value of `1234` would be passed as
|
|
180
|
+
```typescript
|
|
181
|
+
[1234n,16n]
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
_example_
|
|
185
|
+
```typescript
|
|
186
|
+
// Your PDA seeds are "user" and then the index of the user as a u32,
|
|
187
|
+
// and you want to get user 67
|
|
188
|
+
const address = await myProgram.pda(["user",[67n, 32n]]);
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Full details available in the [PDA section of the docs](https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/PDAs.md).
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
# Contents
|
|
195
|
+
|
|
196
|
+
### [Initialisation](https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/framework/INITIALISATION.md)
|
|
197
|
+
Full details on initialising.
|
|
198
|
+
|
|
199
|
+
### [Wallet](https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/framework/WALLET.md)
|
|
200
|
+
Managing wallet connections, connect-state lifecycles, and other functionality relating to injected wallets.
|
|
201
|
+
|
|
202
|
+
### [Programs](https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/framework/PROGRAM.md)
|
|
203
|
+
Create Anchor program helpers for easily interacting with deployed programs.
|
|
204
|
+
|
|
205
|
+
### [Transactions](https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/TRANSACTIONS.md)
|
|
206
|
+
Sending transactions and how the library handles transaction lifecycles.
|
|
207
|
+
|
|
208
|
+
### [Accounts](https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/framework/ACCOUNTS.md)
|
|
209
|
+
Managing accounts included in instructions and transactions.
|
|
210
|
+
|
|
211
|
+
### [Utils](https://github.com/AnAllergyToAnalogy/kit-squared/blob/main/docs/utils/UTILS.md)
|
|
212
|
+
Various utilities, some relating specific to Solana, others that are more broadly used by this library.
|
|
213
|
+
|
|
214
|
+
<!-- ### [Example](TODO)
|
|
215
|
+
An example repo showing basic usage of the Kit² library. -->
|
|
216
|
+
|
|
217
|
+
### Example
|
|
218
|
+
TODO: example repo showing basic usage of the Kit² library.
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// import { env } from '$env/dynamic/public';
|
|
3
|
+
// import { browser } from "$app/environment"
|
|
4
|
+
import { onMount } from "svelte";
|
|
5
|
+
|
|
6
|
+
// This import is neede to keep the first ever load from calculating wallet state values in time
|
|
7
|
+
// import {walletState} from "./library/wallet.js";
|
|
8
|
+
// walletState;
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
selectWallet,
|
|
12
|
+
getWalletInfo,
|
|
13
|
+
// visible,
|
|
14
|
+
// showPrompt,
|
|
15
|
+
// hidePrompt,
|
|
16
|
+
availableWallets,
|
|
17
|
+
unmountTeardown
|
|
18
|
+
} from "./library/walletSelection.js";
|
|
19
|
+
import {initialiseWallet} from "./library/wallet.js";
|
|
20
|
+
import { hidePrompt, visible } from "./components/walletSelectionComponent.js";
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
// TODO: import initialise from wallet
|
|
25
|
+
// -> this registers an onSelectWallet which is the thing that registers the wallet listeners and connects
|
|
26
|
+
|
|
27
|
+
function selectWalletClick(name: string){
|
|
28
|
+
selectWallet(name);
|
|
29
|
+
hidePrompt();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function cancelClick(){
|
|
33
|
+
hidePrompt();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// if(browser){
|
|
37
|
+
onMount(()=>{
|
|
38
|
+
initialiseWallet();
|
|
39
|
+
|
|
40
|
+
// this used to have startWalletFindCycle but pretty sure that isn't needed anymore
|
|
41
|
+
return ()=>{
|
|
42
|
+
unmountTeardown();
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// }
|
|
47
|
+
|
|
48
|
+
</script>
|
|
49
|
+
|
|
50
|
+
<!-- TODO: De-stye -->
|
|
51
|
+
|
|
52
|
+
<style>
|
|
53
|
+
#walletSelectionArea{
|
|
54
|
+
position: fixed;
|
|
55
|
+
top:0;
|
|
56
|
+
left:0;
|
|
57
|
+
|
|
58
|
+
width:100%;
|
|
59
|
+
height:100%;
|
|
60
|
+
|
|
61
|
+
display: flex;
|
|
62
|
+
justify-content: center;
|
|
63
|
+
align-items: center;
|
|
64
|
+
|
|
65
|
+
background: rgba(0,0,0,0.8);
|
|
66
|
+
z-index: 999;
|
|
67
|
+
}
|
|
68
|
+
.walletSelection {
|
|
69
|
+
/* width: 100%;
|
|
70
|
+
height: 75%; */
|
|
71
|
+
|
|
72
|
+
background: black;
|
|
73
|
+
color:white;
|
|
74
|
+
|
|
75
|
+
outline: solid 5px white;
|
|
76
|
+
padding: 10px;
|
|
77
|
+
|
|
78
|
+
max-width: calc(100cw - 25px);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
h2{
|
|
82
|
+
text-align: center;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.wallets{
|
|
86
|
+
margin: 20px 0;
|
|
87
|
+
row-gap: 5px;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
</style>
|
|
91
|
+
|
|
92
|
+
{#if $visible}
|
|
93
|
+
<div id="walletSelectionArea">
|
|
94
|
+
<div class="walletSelection">
|
|
95
|
+
|
|
96
|
+
<div class="cc">
|
|
97
|
+
<h3>Select a wallet to connect</h3>
|
|
98
|
+
|
|
99
|
+
<div class="wallets cc">
|
|
100
|
+
{#if availableWallets.length !== 0}
|
|
101
|
+
|
|
102
|
+
{#each availableWallets as walletName}
|
|
103
|
+
<!-- <div class="class"> -->
|
|
104
|
+
<button onclick={() => selectWalletClick(walletName)} > {getWalletInfo(walletName)} </button>
|
|
105
|
+
<!-- </div> -->
|
|
106
|
+
{/each}
|
|
107
|
+
|
|
108
|
+
{:else}
|
|
109
|
+
No wallets detected :(
|
|
110
|
+
|
|
111
|
+
{/if}
|
|
112
|
+
</div>
|
|
113
|
+
|
|
114
|
+
<button onclick={cancelClick} > Cancel </button>
|
|
115
|
+
</div>
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
</div>
|
|
119
|
+
</div>
|
|
120
|
+
{/if}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
|
2
|
+
new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
|
|
3
|
+
$$bindings?: Bindings;
|
|
4
|
+
} & Exports;
|
|
5
|
+
(internal: unknown, props: {
|
|
6
|
+
$$events?: Events;
|
|
7
|
+
$$slots?: Slots;
|
|
8
|
+
}): Exports & {
|
|
9
|
+
$set?: any;
|
|
10
|
+
$on?: any;
|
|
11
|
+
};
|
|
12
|
+
z_$$bindings?: Bindings;
|
|
13
|
+
}
|
|
14
|
+
declare const WalletSelection: $$__sveltets_2_IsomorphicComponent<Record<string, never>, {
|
|
15
|
+
[evt: string]: CustomEvent<any>;
|
|
16
|
+
}, {}, {}, string>;
|
|
17
|
+
type WalletSelection = InstanceType<typeof WalletSelection>;
|
|
18
|
+
export default WalletSelection;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { getStoredWalletSelection } from "../library/wallet.js";
|
|
3
|
+
import { availableWallets, recheckWallets, selectWallet, stopExpectWallet } from "../library/walletSelection.js";
|
|
4
|
+
export function requestAndConnectWallet() {
|
|
5
|
+
// if (has wallet already logged somehow)
|
|
6
|
+
// use that one
|
|
7
|
+
// else
|
|
8
|
+
// show wallet selection thing
|
|
9
|
+
// which triggers connect when they select one
|
|
10
|
+
const stored = getStoredWalletSelection();
|
|
11
|
+
if (stored && availableWallets.includes(stored)) {
|
|
12
|
+
selectWallet(stored);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
if (availableWallets.length === 1) {
|
|
16
|
+
// Only one wallet available, auto select that
|
|
17
|
+
selectWallet(availableWallets[0]);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
// Show wallet selection thing
|
|
21
|
+
showPrompt();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export const visible = writable(false);
|
|
26
|
+
export function showPrompt() {
|
|
27
|
+
stopExpectWallet();
|
|
28
|
+
recheckWallets();
|
|
29
|
+
visible.set(true);
|
|
30
|
+
}
|
|
31
|
+
export function hidePrompt() {
|
|
32
|
+
visible.set(false);
|
|
33
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type NetworkType } from "./library/wallet.js";
|
|
2
|
+
export declare function init(http: string, ws: string, networkType?: NetworkType): void;
|
|
3
|
+
export { getNetworkType, getNetwork, getConnection, checkAccountExists, getAccountBalance } from "./library/connection.js";
|
|
4
|
+
export { onConnect, onDisconnect } from "./library/connectSchedule.js";
|
|
5
|
+
export { transacting, TRANSACTION_STATE, transactionState, onTransaction, clearAddedAccounts, addAccounts, getAddedAccounts, transact, createProgram, } from "./library/program.js";
|
|
6
|
+
export * from "./library/utils.js";
|
|
7
|
+
export { walletState, walletInitial, walletConnecting, walletConnected, walletError, signer, myKey, me, myKeyString, isMe, getStoredWalletSelection, disconnectWallet, unmountWallet } from "./library/wallet.js";
|
|
8
|
+
export { availableWallets, selection, selectWallet, getWalletInfo, unmountTeardown, recheckWallets, } from "./library/walletSelection.js";
|
|
9
|
+
export { requestAndConnectWallet, visible, showPrompt, hidePrompt, } from "./components/walletSelectionComponent.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { init as initConnection } from "./library/connection.js";
|
|
2
|
+
import { initialiseWallet } from "./library/wallet.js";
|
|
3
|
+
import { startConnectSchedule } from "./library/connectSchedule.js";
|
|
4
|
+
export function init(http, ws, networkType) {
|
|
5
|
+
initConnection(http, ws, networkType);
|
|
6
|
+
initialiseWallet();
|
|
7
|
+
startConnectSchedule();
|
|
8
|
+
}
|
|
9
|
+
export { getNetworkType, getNetwork, getConnection, checkAccountExists, getAccountBalance } from "./library/connection.js";
|
|
10
|
+
export { onConnect, onDisconnect } from "./library/connectSchedule.js";
|
|
11
|
+
export { transacting, TRANSACTION_STATE, transactionState, onTransaction, clearAddedAccounts, addAccounts, getAddedAccounts, transact, createProgram, } from "./library/program.js";
|
|
12
|
+
export * from "./library/utils.js";
|
|
13
|
+
export { walletState, walletInitial, walletConnecting, walletConnected, walletError, signer, myKey, me, myKeyString, isMe, getStoredWalletSelection, disconnectWallet, unmountWallet } from "./library/wallet.js";
|
|
14
|
+
export { availableWallets, selection, selectWallet, getWalletInfo, unmountTeardown, recheckWallets, } from "./library/walletSelection.js";
|
|
15
|
+
export { requestAndConnectWallet, visible, showPrompt, hidePrompt, } from "./components/walletSelectionComponent.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const connectIsScheduled: import("svelte/store").Writable<boolean>;
|
|
2
|
+
export declare const disconnectIsScheduled: import("svelte/store").Writable<boolean>;
|
|
3
|
+
type Action = "connect" | "disconnect";
|
|
4
|
+
export declare function scheduleConnect(action: Action): void;
|
|
5
|
+
export declare const onConnect: {
|
|
6
|
+
(callback: import("./utils.js").Callback): () => void;
|
|
7
|
+
trigger: (...args: any[]) => Promise<void>;
|
|
8
|
+
killAll: () => void;
|
|
9
|
+
};
|
|
10
|
+
export declare const onDisconnect: {
|
|
11
|
+
(callback: import("./utils.js").Callback): () => void;
|
|
12
|
+
trigger: (...args: any[]) => Promise<void>;
|
|
13
|
+
killAll: () => void;
|
|
14
|
+
};
|
|
15
|
+
export declare function startConnectSchedule(): void;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onWalletConnect, onWalletDisconnect } from "./wallet.js";
|
|
3
|
+
import { Event } from "./utils.js";
|
|
4
|
+
export const connectIsScheduled = writable(false);
|
|
5
|
+
export const disconnectIsScheduled = writable(false);
|
|
6
|
+
let connectQueue = [];
|
|
7
|
+
let checkingQueue = true; //initially set to false after first init
|
|
8
|
+
export function scheduleConnect(action) {
|
|
9
|
+
connectQueue.push(action);
|
|
10
|
+
if (connectQueue.length > 1) {
|
|
11
|
+
if (([connectQueue[0], connectQueue[1]].sort()).join() === (["connect", "disconnect"].sort()).join()) {
|
|
12
|
+
connectQueue.shift();
|
|
13
|
+
connectQueue.shift();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
_checkSchedule();
|
|
17
|
+
}
|
|
18
|
+
export const onConnect = Event();
|
|
19
|
+
export const onDisconnect = Event();
|
|
20
|
+
async function _checkSchedule(loopback = false) {
|
|
21
|
+
if (checkingQueue && !loopback)
|
|
22
|
+
return;
|
|
23
|
+
checkingQueue = true;
|
|
24
|
+
if (connectQueue.length !== 0) {
|
|
25
|
+
const next = connectQueue.shift();
|
|
26
|
+
connectIsScheduled.set(connectQueue.includes("connect"));
|
|
27
|
+
disconnectIsScheduled.set(connectQueue.includes("disconnect"));
|
|
28
|
+
if (next === "connect") {
|
|
29
|
+
await onConnect.trigger();
|
|
30
|
+
}
|
|
31
|
+
else if (next === "disconnect") {
|
|
32
|
+
await onDisconnect.trigger();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (connectQueue.length !== 0) {
|
|
36
|
+
_checkSchedule(true);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
checkingQueue = false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function startConnectSchedule() {
|
|
43
|
+
checkingQueue = false;
|
|
44
|
+
_checkSchedule();
|
|
45
|
+
}
|
|
46
|
+
onWalletConnect(() => {
|
|
47
|
+
scheduleConnect("connect");
|
|
48
|
+
});
|
|
49
|
+
onWalletDisconnect(() => {
|
|
50
|
+
scheduleConnect("disconnect");
|
|
51
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type Address, type Instruction, type TransactionSendingSigner } from '@solana/kit';
|
|
2
|
+
import type { NetworkType } from './wallet.js';
|
|
3
|
+
export declare function getNetworkType(): NetworkType | null;
|
|
4
|
+
export declare function init(http: string, ws: string, networkType?: NetworkType): void;
|
|
5
|
+
export declare function getNetwork(): {
|
|
6
|
+
http: string | null;
|
|
7
|
+
ws: string | null;
|
|
8
|
+
networkType: NetworkType | null;
|
|
9
|
+
};
|
|
10
|
+
export declare function getConnection(): {
|
|
11
|
+
rpc: import("@solana/kit").Rpc<import("@solana/kit").RequestAirdropApi & import("@solana/kit").GetAccountInfoApi & import("@solana/kit").GetBalanceApi & import("@solana/kit").GetBlockApi & import("@solana/kit").GetBlockCommitmentApi & import("@solana/kit").GetBlockHeightApi & import("@solana/kit").GetBlockProductionApi & import("@solana/kit").GetBlocksApi & import("@solana/kit").GetBlocksWithLimitApi & import("@solana/kit").GetBlockTimeApi & import("@solana/kit").GetClusterNodesApi & import("@solana/kit").GetEpochInfoApi & import("@solana/kit").GetEpochScheduleApi & import("@solana/kit").GetFeeForMessageApi & import("@solana/kit").GetFirstAvailableBlockApi & import("@solana/kit").GetGenesisHashApi & import("@solana/kit").GetHealthApi & import("@solana/kit").GetHighestSnapshotSlotApi & import("@solana/kit").GetIdentityApi & import("@solana/kit").GetInflationGovernorApi & import("@solana/kit").GetInflationRateApi & import("@solana/kit").GetInflationRewardApi & import("@solana/kit").GetLargestAccountsApi & import("@solana/kit").GetLatestBlockhashApi & import("@solana/kit").GetLeaderScheduleApi & import("@solana/kit").GetMaxRetransmitSlotApi & import("@solana/kit").GetMaxShredInsertSlotApi & import("@solana/kit").GetMinimumBalanceForRentExemptionApi & import("@solana/kit").GetMultipleAccountsApi & import("@solana/kit").GetProgramAccountsApi & import("@solana/kit").GetRecentPerformanceSamplesApi & import("@solana/kit").GetRecentPrioritizationFeesApi & import("@solana/kit").GetSignaturesForAddressApi & import("@solana/kit").GetSignatureStatusesApi & import("@solana/kit").GetSlotApi & import("@solana/kit").GetSlotLeaderApi & import("@solana/kit").GetSlotLeadersApi & import("@solana/kit").GetStakeMinimumDelegationApi & import("@solana/kit").GetSupplyApi & import("@solana/kit").GetTokenAccountBalanceApi & import("@solana/kit").GetTokenAccountsByDelegateApi & import("@solana/kit").GetTokenAccountsByOwnerApi & import("@solana/kit").GetTokenLargestAccountsApi & import("@solana/kit").GetTokenSupplyApi & import("@solana/kit").GetTransactionApi & import("@solana/kit").GetTransactionCountApi & import("@solana/kit").GetVersionApi & import("@solana/kit").GetVoteAccountsApi & import("@solana/kit").IsBlockhashValidApi & import("@solana/kit").MinimumLedgerSlotApi & import("@solana/kit").SendTransactionApi & import("@solana/kit").SimulateTransactionApi>;
|
|
12
|
+
rpcSubscriptions: import("@solana/kit").RpcSubscriptions<import("@solana/rpc-subscriptions-api").SolanaRpcSubscriptionsApi>;
|
|
13
|
+
getLamportBalance: (_address: any) => Promise<import("@solana/kit").PendingRpcRequest<Readonly<{
|
|
14
|
+
context: Readonly<{
|
|
15
|
+
slot: import("@solana/kit").Slot;
|
|
16
|
+
}>;
|
|
17
|
+
value: import("@solana/kit").Lamports;
|
|
18
|
+
}>>>;
|
|
19
|
+
sendTransactionFromInstructionsWithWalletApp: ({ feePayer, instructions, abortSignal }: {
|
|
20
|
+
feePayer: TransactionSendingSigner;
|
|
21
|
+
instructions: Array<Instruction>;
|
|
22
|
+
abortSignal?: AbortSignal | null;
|
|
23
|
+
}) => Promise<string>;
|
|
24
|
+
getRecentSignatureConfirmation: (config: {
|
|
25
|
+
abortSignal: AbortSignal;
|
|
26
|
+
commitment: import("@solana/kit").Commitment;
|
|
27
|
+
signature: import("@solana/kit").Signature;
|
|
28
|
+
}) => Promise<void>;
|
|
29
|
+
};
|
|
30
|
+
export declare function checkAccountExists(address: Address): Promise<boolean>;
|
|
31
|
+
export declare function getAccountBalance(address: Address): Promise<bigint>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { createSolanaRpcFromTransport, createSolanaRpcSubscriptions, createSolanaRpc, pipe, createTransactionMessage, appendTransactionMessageInstructions, setTransactionMessageFeePayerSigner, setTransactionMessageLifetimeUsingBlockhash, assertIsTransactionMessageWithSingleSendingSigner, signAndSendTransactionMessageWithSigners, getBase58Decoder, } from '@solana/kit';
|
|
2
|
+
import { createRecentSignatureConfirmationPromiseFactory } from '@solana/transaction-confirmation';
|
|
3
|
+
let rpcUrl = null;
|
|
4
|
+
let wsUrl = null;
|
|
5
|
+
let network = null;
|
|
6
|
+
export function getNetworkType() {
|
|
7
|
+
return network;
|
|
8
|
+
}
|
|
9
|
+
export function init(http, ws, networkType = "mainnet") {
|
|
10
|
+
rpcUrl = http;
|
|
11
|
+
wsUrl = ws;
|
|
12
|
+
network = networkType;
|
|
13
|
+
}
|
|
14
|
+
export function getNetwork() {
|
|
15
|
+
return {
|
|
16
|
+
http: rpcUrl,
|
|
17
|
+
ws: wsUrl,
|
|
18
|
+
networkType: network,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function _checkNetwork() {
|
|
22
|
+
if (!network)
|
|
23
|
+
throw new Error("Network not configured. Must use configureNetwork to set rpc URLS and network type.");
|
|
24
|
+
}
|
|
25
|
+
function _connect() {
|
|
26
|
+
_checkNetwork();
|
|
27
|
+
const rpc = createSolanaRpc(rpcUrl);
|
|
28
|
+
const rpcSubscriptions = createSolanaRpcSubscriptions(wsUrl);
|
|
29
|
+
const getLamportBalance = async function (_address) {
|
|
30
|
+
return rpc.getBalance(_address);
|
|
31
|
+
};
|
|
32
|
+
const signatureBytesToBase58String = (signatureBytes) => {
|
|
33
|
+
return getBase58Decoder().decode(signatureBytes);
|
|
34
|
+
};
|
|
35
|
+
const sendTransactionFromInstructionsWithWalletAppFactory = (rpc) => {
|
|
36
|
+
const sendTransactionFromInstructionsWithWalletApp = async ({ feePayer, instructions, abortSignal }) => {
|
|
37
|
+
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send({ abortSignal });
|
|
38
|
+
const transactionMessage = pipe(createTransactionMessage({ version: 0 }), (message) => setTransactionMessageFeePayerSigner(feePayer, message), (message) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, message), (message) => appendTransactionMessageInstructions(instructions, message));
|
|
39
|
+
assertIsTransactionMessageWithSingleSendingSigner(transactionMessage);
|
|
40
|
+
const signatureBytes = await signAndSendTransactionMessageWithSigners(transactionMessage);
|
|
41
|
+
const signature = signatureBytesToBase58String(signatureBytes);
|
|
42
|
+
return signature;
|
|
43
|
+
};
|
|
44
|
+
return sendTransactionFromInstructionsWithWalletApp;
|
|
45
|
+
};
|
|
46
|
+
const sendTransactionFromInstructionsWithWalletApp = sendTransactionFromInstructionsWithWalletAppFactory(rpc);
|
|
47
|
+
const getRecentSignatureConfirmation = createRecentSignatureConfirmationPromiseFactory({ rpc, rpcSubscriptions });
|
|
48
|
+
return {
|
|
49
|
+
rpc,
|
|
50
|
+
rpcSubscriptions,
|
|
51
|
+
getLamportBalance,
|
|
52
|
+
sendTransactionFromInstructionsWithWalletApp,
|
|
53
|
+
getRecentSignatureConfirmation,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function getConnection() {
|
|
57
|
+
if (!network)
|
|
58
|
+
throw new Error("Network not configured (you must call setNetwork before this point).");
|
|
59
|
+
return _connect();
|
|
60
|
+
}
|
|
61
|
+
// !!! TODO
|
|
62
|
+
export async function checkAccountExists(address) {
|
|
63
|
+
// return true;
|
|
64
|
+
//todo: this
|
|
65
|
+
return await getAccountBalance(address) > 0n;
|
|
66
|
+
}
|
|
67
|
+
export async function getAccountBalance(address) {
|
|
68
|
+
return await (await getConnection().getLamportBalance(address)).value;
|
|
69
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type Callback } from "./utils.js";
|
|
2
|
+
import { type Address, type Instruction } from "@solana/kit";
|
|
3
|
+
import type { TransactionSendingSigner } from "@solana/signers";
|
|
4
|
+
export declare let _transacting: boolean;
|
|
5
|
+
export declare let transacting: import("svelte/store").Writable<boolean>;
|
|
6
|
+
export declare const TRANSACTION_STATE: {
|
|
7
|
+
INITIAL: string;
|
|
8
|
+
REQUESTED: string;
|
|
9
|
+
PENDING: string;
|
|
10
|
+
};
|
|
11
|
+
export declare const transactionState: import("svelte/store").Writable<string>;
|
|
12
|
+
export declare const onTransaction: {
|
|
13
|
+
request: {
|
|
14
|
+
(callback: Callback): () => void;
|
|
15
|
+
trigger: (...args: any[]) => Promise<void>;
|
|
16
|
+
killAll: () => void;
|
|
17
|
+
};
|
|
18
|
+
submit: {
|
|
19
|
+
(callback: Callback): () => void;
|
|
20
|
+
trigger: (...args: any[]) => Promise<void>;
|
|
21
|
+
killAll: () => void;
|
|
22
|
+
};
|
|
23
|
+
confirm: {
|
|
24
|
+
(callback: Callback): () => void;
|
|
25
|
+
trigger: (...args: any[]) => Promise<void>;
|
|
26
|
+
killAll: () => void;
|
|
27
|
+
};
|
|
28
|
+
cancel: {
|
|
29
|
+
(callback: Callback): () => void;
|
|
30
|
+
trigger: (...args: any[]) => Promise<void>;
|
|
31
|
+
killAll: () => void;
|
|
32
|
+
};
|
|
33
|
+
fail: {
|
|
34
|
+
(callback: Callback): () => void;
|
|
35
|
+
trigger: (...args: any[]) => Promise<void>;
|
|
36
|
+
killAll: () => void;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export declare function clearAddedAccounts(): void;
|
|
40
|
+
export declare function addAccounts(accounts: {
|
|
41
|
+
[key: string]: Address;
|
|
42
|
+
}): void;
|
|
43
|
+
export declare function getAddedAccounts(): {
|
|
44
|
+
[key: string]: Address;
|
|
45
|
+
};
|
|
46
|
+
export declare function transact(ixs?: Instruction[], names?: string[]): Promise<void>;
|
|
47
|
+
export declare function transactSingle(ix: Instruction, name?: string | null): Promise<void>;
|
|
48
|
+
export declare function createProgram(programClient: {
|
|
49
|
+
[key: string]: any;
|
|
50
|
+
}, idl: {
|
|
51
|
+
[key: string]: any;
|
|
52
|
+
}, signer?: null | TransactionSendingSigner, noEvents?: boolean): Promise<{
|
|
53
|
+
[key: string]: any;
|
|
54
|
+
}>;
|