forked from solana-foundation/framework-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateClientHelpers.ts
More file actions
184 lines (166 loc) · 6.42 KB
/
createClientHelpers.ts
File metadata and controls
184 lines (166 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { Commitment } from '@solana/kit';
import { createSolTransferHelper, type SolTransferHelper } from '../features/sol';
import { createSplTokenHelper, type SplTokenHelper, type SplTokenHelperConfig } from '../features/spl';
import { createStakeHelper, type StakeHelper } from '../features/stake';
import { createTransactionHelper, type TransactionHelper } from '../features/transactions';
import { createWsolHelper, type WsolHelper } from '../features/wsol';
import {
type PrepareTransactionMessage,
type PrepareTransactionOptions,
prepareTransaction as prepareTransactionUtility,
} from '../transactions/prepareTransaction';
import type { ClientHelpers, ClientStore, SolanaClientRuntime } from '../types';
type SplTokenCacheEntry = Readonly<{
baseCommitment?: Commitment;
scoped: SplTokenHelper;
}>;
function withDefaultCommitment<T extends { commitment?: Commitment }>(
config: T,
getFallback: () => Commitment,
baseCommitment?: Commitment,
): T {
if (config.commitment !== undefined) {
return config;
}
const commitment = baseCommitment ?? getFallback();
return {
...config,
commitment,
};
}
function wrapSolTransferHelper(helper: SolTransferHelper, getFallback: () => Commitment): SolTransferHelper {
return {
prepareTransfer: (config) => helper.prepareTransfer(withDefaultCommitment(config, getFallback)),
sendPreparedTransfer: helper.sendPreparedTransfer,
sendTransfer: (config, options) => helper.sendTransfer(withDefaultCommitment(config, getFallback), options),
};
}
function wrapSplTokenHelper(
helper: SplTokenHelper,
getFallback: () => Commitment,
baseCommitment?: Commitment,
): SplTokenHelper {
const resolveCommitment = (commitment?: Commitment) => commitment ?? baseCommitment ?? getFallback();
return {
deriveAssociatedTokenAddress: helper.deriveAssociatedTokenAddress,
fetchBalance: (owner, commitment) => helper.fetchBalance(owner, resolveCommitment(commitment)),
prepareTransfer: (config) => helper.prepareTransfer(withDefaultCommitment(config, getFallback, baseCommitment)),
sendPreparedTransfer: helper.sendPreparedTransfer,
sendTransfer: (config, options) =>
helper.sendTransfer(withDefaultCommitment(config, getFallback, baseCommitment), options),
};
}
function wrapStakeHelper(helper: StakeHelper, getFallback: () => Commitment): StakeHelper {
return {
getStakeAccounts: helper.getStakeAccounts,
prepareStake: (config) => helper.prepareStake(withDefaultCommitment(config, getFallback)),
prepareUnstake: (config) => helper.prepareUnstake(withDefaultCommitment(config, getFallback)),
prepareWithdraw: (config) => helper.prepareWithdraw(withDefaultCommitment(config, getFallback)),
sendPreparedStake: helper.sendPreparedStake,
sendPreparedUnstake: helper.sendPreparedUnstake,
sendPreparedWithdraw: helper.sendPreparedWithdraw,
sendStake: (config, options) => helper.sendStake(withDefaultCommitment(config, getFallback), options),
sendUnstake: (config, options) => helper.sendUnstake(withDefaultCommitment(config, getFallback), options),
sendWithdraw: (config, options) => helper.sendWithdraw(withDefaultCommitment(config, getFallback), options),
};
}
function wrapWsolHelper(helper: WsolHelper, getFallback: () => Commitment): WsolHelper {
return {
prepareUnwrapSol: (config) => helper.prepareUnwrapSol(withDefaultCommitment(config, getFallback)),
prepareWrapSol: (config) => helper.prepareWrapSol(withDefaultCommitment(config, getFallback)),
sendPreparedUnwrapSol: helper.sendPreparedUnwrapSol,
sendPreparedWrapSol: helper.sendPreparedWrapSol,
unwrapSol: (config, options) => helper.unwrapSol(withDefaultCommitment(config, getFallback), options),
wrapSol: (config, options) => helper.wrapSol(withDefaultCommitment(config, getFallback), options),
};
}
function normaliseConfigValue(value: unknown): string | undefined {
if (value === null || value === undefined) {
return undefined;
}
if (typeof value === 'string') {
return value;
}
if (typeof value === 'object' && 'toString' in value) {
return String((value as { toString(): unknown }).toString());
}
return JSON.stringify(value);
}
function serialiseSplConfig(config: SplTokenHelperConfig): string {
return JSON.stringify({
associatedTokenProgram: normaliseConfigValue(config.associatedTokenProgram),
commitment: normaliseConfigValue(config.commitment),
decimals: config.decimals,
mint: normaliseConfigValue(config.mint),
tokenProgram: normaliseConfigValue(config.tokenProgram),
});
}
export function createClientHelpers(runtime: SolanaClientRuntime, store: ClientStore): ClientHelpers {
const getFallbackCommitment = () => store.getState().cluster.commitment;
const splTokenCache = new Map<string, SplTokenCacheEntry>();
let solTransfer: SolTransferHelper | undefined;
let stake: StakeHelper | undefined;
let transaction: TransactionHelper | undefined;
let wsol: WsolHelper | undefined;
const getSolTransfer = () => {
if (!solTransfer) {
solTransfer = wrapSolTransferHelper(createSolTransferHelper(runtime), getFallbackCommitment);
}
return solTransfer;
};
const getStake = () => {
if (!stake) {
stake = wrapStakeHelper(createStakeHelper(runtime), getFallbackCommitment);
}
return stake;
};
const getTransaction = () => {
if (!transaction) {
transaction = createTransactionHelper(runtime, getFallbackCommitment);
}
return transaction;
};
const getWsol = () => {
if (!wsol) {
wsol = wrapWsolHelper(createWsolHelper(runtime), getFallbackCommitment);
}
return wsol;
};
function getSplTokenHelper(config: SplTokenHelperConfig): SplTokenHelper {
const cacheKey = serialiseSplConfig(config);
const cached = splTokenCache.get(cacheKey);
if (cached) {
return cached.scoped;
}
const helper = createSplTokenHelper(runtime, config);
const scoped = wrapSplTokenHelper(helper, getFallbackCommitment, config.commitment);
splTokenCache.set(cacheKey, {
baseCommitment: config.commitment,
scoped,
});
return scoped;
}
const prepareTransactionWithRuntime = <TMessage extends PrepareTransactionMessage>(
options: PrepareTransactionOptions<TMessage>,
) =>
prepareTransactionUtility({
...options,
rpc: runtime.rpc as Parameters<typeof prepareTransactionUtility>[0]['rpc'],
});
return Object.freeze({
get solTransfer() {
return getSolTransfer();
},
splToken: getSplTokenHelper,
get stake() {
return getStake();
},
get transaction() {
return getTransaction();
},
get wsol() {
return getWsol();
},
prepareTransaction: prepareTransactionWithRuntime,
});
}