-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathaction-supportability.ts
183 lines (163 loc) · 5.44 KB
/
action-supportability.ts
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
import { BlockchainIds, getShortBlockchainName } from '../../utils/caip-2.ts';
import { ACTIONS_SPEC_VERSION } from '../../utils/dependency-versions.ts';
import type { Action } from './Action.ts';
/**
* Max spec version the Blink client supports.
*/
export const MAX_SUPPORTED_ACTION_VERSION = ACTIONS_SPEC_VERSION;
export const DEFAULT_SUPPORTED_BLOCKCHAIN_IDS = [
BlockchainIds.SOLANA_MAINNET,
BlockchainIds.SOLANA_DEVNET,
];
/**
* Baseline action version to be used when not set by action provider.
* Defaults to latest release that doesn't support versioning.
*/
export const BASELINE_ACTION_VERSION = '2.2';
/**
* Baseline blockchain IDs to be used when not set by action provider.
* Defaults to Solana mainnet.
*/
export const BASELINE_ACTION_BLOCKCHAIN_IDS = [BlockchainIds.SOLANA_MAINNET];
type IsVersionSupportedParams = {
actionVersion: string;
supportedActionVersion: string;
};
type IsBlockchainIdSupportedParams = {
actionBlockchainIds: string[];
supportedBlockchainIds: string[];
};
export type ActionSupportability =
| {
isSupported: true;
}
| {
isSupported: false;
message: string;
};
export type ActionSupportStrategy = (
action: Action,
) => Promise<ActionSupportability>;
/**
* Default implementation for checking if an action is supported.
* Checks if the action version and the action blockchain IDs are supported by blink.
* @param action Action.
*
* @see {isVersionSupported}
* @see {isBlockchainSupported}
*/
export const defaultActionSupportStrategy: ActionSupportStrategy = async (
action,
) => {
const { version: actionVersion, blockchainIds: actionBlockchainIds } =
action.metadata;
// Will be displayed in the future once we remove backward compatibility fallbacks for blockchains and version
if (
!actionVersion ||
!actionBlockchainIds ||
actionBlockchainIds.length === 0
) {
return {
isSupported: false,
message:
'Action compatibility metadata is not set. Please contact the action provider.',
};
}
const supportedActionVersion = MAX_SUPPORTED_ACTION_VERSION;
const supportedBlockchainIds = !action.adapterUnsafe
? actionBlockchainIds // Assuming action is supported if adapter absent for optimistic compatibility
: action.adapterUnsafe.metadata.supportedBlockchainIds;
const versionSupported = isVersionSupported({
actionVersion,
supportedActionVersion,
});
const blockchainSupported = isBlockchainSupported({
actionBlockchainIds,
supportedBlockchainIds,
});
const notSupportedBlockchainIds = actionBlockchainIds.filter(
(id) => !supportedBlockchainIds.includes(id),
);
const notSupportedActionBlockchainNames = notSupportedBlockchainIds.map(
getShortBlockchainName,
);
if (!versionSupported && !blockchainSupported) {
const blockchainMessage =
notSupportedActionBlockchainNames.length === 1
? `blockchain ${notSupportedActionBlockchainNames[0]}`
: `blockchains ${notSupportedActionBlockchainNames.join(', ')}`;
return {
isSupported: false,
message: `Action version ${actionVersion} and ${blockchainMessage} are not supported by your Blink client.`,
};
}
if (!versionSupported) {
return {
isSupported: false,
message: `Action version ${actionVersion} is not supported by your Blink client.`,
};
}
if (!blockchainSupported) {
const blockchainMessage =
notSupportedActionBlockchainNames.length === 1
? `Action blockchain ${notSupportedActionBlockchainNames[0]} is not supported by your Blink client.`
: `Action blockchains ${notSupportedActionBlockchainNames.join(', ')} are not supported by your Blink client.`;
return {
isSupported: false,
message: blockchainMessage,
};
}
return {
isSupported: true,
};
};
/**
* Check if the action version is supported by blink.
* @param supportedActionVersion The version the blink supports.
* @param actionVersion The version of the action.
*
* @returns `true` if the action version is less than or equal to the supported ignoring patch version, `false` otherwise.
*/
export function isVersionSupported({
supportedActionVersion,
actionVersion,
}: IsVersionSupportedParams): boolean {
return compareSemverIgnoringPatch(actionVersion, supportedActionVersion) <= 0;
}
function compareSemverIgnoringPatch(v1: string, v2: string): number {
const [major1, minor1] = v1.split('.').map(Number);
const [major2, minor2] = v2.split('.').map(Number);
if (major1 !== major2) {
return major1 - major2;
} else if (minor1 !== minor2) {
return minor1 - minor2;
}
return 0;
}
/**
* Check if action blockchain IDs are supported by the blink.
*
* @param supportedBlockchainIds List of CAIP-2 blockchain IDs the client supports.
* @param actionBlockchainIds List of CAIP-2 blockchain IDs the action supports.
*
* @returns `true` if all action blockchain IDs are supported by blink, `false` otherwise.
*
* @see BlockchainIds
*/
export function isBlockchainSupported({
supportedBlockchainIds,
actionBlockchainIds,
}: IsBlockchainIdSupportedParams): boolean {
if (actionBlockchainIds.length === 0 || supportedBlockchainIds.length === 0) {
return false;
}
const sanitizedSupportedBlockchainIds = supportedBlockchainIds.map((it) =>
it.trim(),
);
const sanitizedActionBlockchainIds = actionBlockchainIds.map((it) =>
it.trim(),
);
return sanitizedActionBlockchainIds.every((chain) =>
sanitizedSupportedBlockchainIds.includes(chain),
);
}