-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.dto.ts
182 lines (161 loc) · 5.74 KB
/
action.dto.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
import { ErrorMessage, SuccessMessage } from 'src/types';
import { ActionMetadata } from './action-metadata.dto';
import { TransactionInfo } from './transaction.dto';
export type BasicAdditionalParams = {
code?: string;
account?: string;
referrer?: string;
callbackId?: string;
commissionRate?: number;
};
type NetworkAdditionalParams = {
chainId: number;
};
export type GenerateFormParams<
T extends Record<string, any> = Record<string, any>,
> = {
[key in keyof T]: T[key];
};
export type ValidateFormData<
T extends Record<string, any> = Record<string, any>,
> = GenerateFormParams<T> & NetworkAdditionalParams;
export type AdditionalParams = BasicAdditionalParams & NetworkAdditionalParams;
export type ActionTransactionParams<
T extends Record<string, any> = Record<string, any>,
> = NetworkAdditionalParams & GenerateFormParams<T>;
export type GenerateTransactionParams<
T extends Record<string, any> = Record<string, any>,
> = {
additionalData: AdditionalParams;
formData: GenerateFormParams<T>;
};
export type UpdateFieldType<
T extends Record<string, any>, // This is the type used in GenerateFormParams
K extends keyof T, // The field to modify (optional)
> = K extends keyof T
? Omit<ValidateFormData<T>, K> & {
[key in K]: T[K][];
} // If field is provided, set it to an array of its original type
: ValidateFormData<T>;
export type GenerateTransactionResponse = {
displayInfo?: {
tokens: Array<{
tokenAddress: string;
amount: string; // raw data, with decimals
direction?: 'from' | 'to';
}>;
};
transactions: TransactionInfo[];
};
export type SharedContent = {
en?: string;
zh?: string;
[key: string]: string | undefined;
};
export type ReporterResponse = {
tip: SuccessMessage;
sharedContent?: SharedContent;
};
export abstract class Action<
T extends Record<string, any> = Record<string, any>,
> {
/**
* getMetadata returns metadata used to create a magicLink.
* This configuration data is essential for guiding the creator to build a complete magicLink.
* It includes the creation components in the dashboard and the way for rendering and interacting with the magicLink.
*/
abstract getMetadata(): Promise<ActionMetadata<T>>;
/**
* generateTransaction returns the parameters necessary for the frontend to construct the transaction.
* The frontend will use these parameters to build the transaction
* and send on-chain transaction requests in a queued manner.
*/
abstract generateTransaction(
data: GenerateTransactionParams<T>,
): Promise<GenerateTransactionResponse>;
/**
* The function generateSharedContent is used to create custom message content for sharing to X/Telegram channels.
*/
async generateSharedContent(
_data: GenerateTransactionParams<T>,
): Promise<SharedContent> {
return {};
}
/**
* During the creation process of the magicLink,
* this allows you to run your custom validation logic,
* providing complex validation for the parameters used to create the magicLink,
* rather than just simple regular expressions.
*
* When you set a key from type T as a "binding" property, you should use UpdateFieldType<T, key>.
*/
async validateFormData(
_: ValidateFormData<T> | UpdateFieldType<T, keyof T>,
): Promise<ErrorMessage> {
return '';
}
/**
* After creating the magicLink, it may also be necessary to initiate an on-chain transaction.
* For example, if I have a red envelope contract, each magicLink should be created by the creator and deposit a sum of money into it,
* allowing users to claim the red envelope associated with the magicLink.
*/
async onMagicLinkCreated?(
data: GenerateTransactionParams<T>,
): Promise<TransactionInfo[]>;
/**
* We can render some custom HTML in the magicLink to provide intuitive on-chain data
* or centralized service data.
* Users can manually refresh this data, and this method is designed for that purpose.
*
*/
async reloadAdvancedInfo?(data: BasicAdditionalParams): Promise<{
title: string;
content: string;
}>;
/**
* Before sending the transaction with the magicLink,
* this function can be used to define some pre-checks
* to determine whether the user can still trigger the transaction with the magicLink.
*
* The default return is an empty string,
* indicating no error message and that usage can continue.
* If a non-empty string is returned,
* it represents the reason for the inability to continue using it.
*/
async preCheckTransaction(
_: GenerateTransactionParams<T>,
): Promise<ErrorMessage> {
return '';
}
/**
* When the user initiates a transaction using the magicLink,
* you may want to provide a message after the on-chain transaction is completed,
* such as ‘You have successfully received 20 USDT.’
* In this function, define and return the message.”
*/
async reportTransaction(
_data: GenerateTransactionParams<T>,
_txHashes: Array<{ hash: string; chainId: number }>,
): Promise<ReporterResponse> {
return {
tip: '',
};
}
/**
* After creating the magicLink, we want to provide the creator with a place to display
* on-chain information and perform on-chain transactions. This function provides that capability.
* You may want the creator to read some on-chain information to understand the usage of
* the magic links they created or to initiate a transaction that changes the status of the created magicLink
* @param {string} code refer to magicLink code
*/
async generateManagementInfo?(code: string): Promise<{
form: Array<{
label: string;
value: string | Array<Record<string, string>>;
}>;
triggers: Array<{
text: string;
transactions: TransactionInfo[];
}>;
}>;
}