forked from dialectlabs/actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.ts
208 lines (195 loc) · 5.78 KB
/
route.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
LAMPORTS_PER_SOL,
PublicKey,
SystemProgram,
VersionedTransaction,
} from '@solana/web3.js';
import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';
import {
actionSpecOpenApiPostRequestBody,
actionsSpecOpenApiGetResponse,
actionsSpecOpenApiPostResponse,
} from '../openapi';
import { prepareTransaction } from '../../shared/transaction-utils';
import { ActionGetResponse, ActionPostRequest, ActionPostResponse } from '@solana/actions';
const DONATION_DESTINATION_WALLET =
'HXtBm8XZbxaTt41uqaKhwUAa6Z1aPyvJdsZVENiWsetg';
const DONATION_AMOUNT_SOL = 0.0001;
const app = new OpenAPIHono();
app.openapi(
createRoute({
method: 'get',
path: '/',
tags: ['MinimalExample'],
responses: actionsSpecOpenApiGetResponse,
}),
(c) => {
const { icon, title, description } = getMetadata();
const response: ActionGetResponse = {
icon,
label: `${DONATION_AMOUNT_SOL} SOL`,
title,
description,
links: {
actions: [
({
label: `${DONATION_AMOUNT_SOL} SOL`,
href: `/api/minimal-example/${DONATION_AMOUNT_SOL}`,
}),
],
},
};
console.log("response => %o", response)
return c.json(response, 200);
},
);
/*
response => {
icon: 'https://fastly.picsum.photos/id/859/800/800.jpg?hmac=JccY8q4fczSKugjJr2mZvjhqsTd4vsqTfQfHwKE1k5Y',
label: '0.0001 SOL',
title: 'Minimal Example',
description: 'This is minimal example for developers.',
links: {
actions: [
{ label: '0.0001 SOL', href: '/api/minimal-example/0.0001' },
[length]: 1
]
}
}
*/
app.openapi(
createRoute({
method: 'post',
path: '/{amount}',
tags: ['MinimalExample'],
request: {
params: z.object({
amount: z
.string()
.optional()
.openapi({
param: {
name: 'amount',
in: 'path',
required: false,
},
type: 'number',
example: '1',
}),
}),
body: actionSpecOpenApiPostRequestBody,
},
responses: actionsSpecOpenApiPostResponse,
}),
async (c) => {
const amount = c.req.param('amount');
const { account } = (await c.req.json()) as ActionPostRequest;
const parsedAmount = parseFloat(amount);
const transaction = await prepareMyTransaction(
new PublicKey(account),
new PublicKey(DONATION_DESTINATION_WALLET),
parsedAmount * LAMPORTS_PER_SOL,
);
const response: ActionPostResponse = {
transaction: Buffer.from(transaction.serialize()).toString('base64'),
};
console.log('account =>', account)
console.log('DONATION_DESTINATION_WALLET =>', DONATION_DESTINATION_WALLET)
console.log('transation =>', transaction)
console.log("response => %o", response)
return c.json(response, 200);
},
);
/*
account => 55AfqEL3TC9mpkDZ63UCgDrzPcMQd5aZtDegfQCWQ5tK
DONATION_DESTINATION_WALLET => HXtBm8XZbxaTt41uqaKhwUAa6Z1aPyvJdsZVENiWsetg
transation => VersionedTransaction {
signatures: [
Uint8Array(64) [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
]
],
message: MessageV0 {
header: {
numRequiredSignatures: 1,
numReadonlySignedAccounts: 0,
numReadonlyUnsignedAccounts: 1
},
staticAccountKeys: [
[PublicKey [PublicKey(55AfqEL3TC9mpkDZ63UCgDrzPcMQd5aZtDegfQCWQ5tK)]],
[PublicKey [PublicKey(HXtBm8XZbxaTt41uqaKhwUAa6Z1aPyvJdsZVENiWsetg)]],
[PublicKey [PublicKey(11111111111111111111111111111111)]]
],
recentBlockhash: 'H2Ki95WcbyaW7ogzTamDfH74tgrDXNN22DutMjqqe4j5',
compiledInstructions: [ [Object] ],
addressTableLookups: []
}
}
response => {
transaction: 'AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAQABAzyAARk1a4RUvpcb/FKI4TmPpdC7CzOxW+IJoT9QtNI49aRKbzaDlhFxHwQUn1HdQG3UvFLLhvIN0rEWCKYsfukAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAO4Rue7fkUAJzRRgKQ3CGFu0UBAi8hxHstTVG6RDA5U8AQICAAEMAgAAAKCGAQAAAAAAAA=='
}
*/
function getMetadata(): Pick<
ActionGetResponse,
'icon' | 'title' | 'description'
> {
const icon =
'https://fastly.picsum.photos/id/859/800/800.jpg?hmac=JccY8q4fczSKugjJr2mZvjhqsTd4vsqTfQfHwKE1k5Y';
const title = 'Minimal Example';
const description =
'This is minimal example for developers.';
return { icon, title, description };
}
async function prepareMyTransaction(
sender: PublicKey,
recipient: PublicKey,
lamports: number,
): Promise<VersionedTransaction> {
const payer = new PublicKey(sender);
const instructions = [
SystemProgram.transfer({
fromPubkey: payer,
toPubkey: new PublicKey(recipient),
lamports: lamports,
}),
];
console.log("instructions => %o", instructions)
return prepareTransaction(instructions, payer);
}
/*
instructions => [
TransactionInstruction {
keys: [
{
pubkey: PublicKey [PublicKey(55AfqEL3TC9mpkDZ63UCgDrzPcMQd5aZtDegfQCWQ5tK)] {
_bn: <BN: 3c800119356b8454be971bfc5288e1398fa5d0bb0b33b15be209a13f50b4d238>,
[Symbol(Symbol.toStringTag)]: [Getter]
},
isSigner: true,
isWritable: true
},
{
pubkey: PublicKey [PublicKey(HXtBm8XZbxaTt41uqaKhwUAa6Z1aPyvJdsZVENiWsetg)] {
_bn: <BN: f5a44a6f36839611711f04149f51dd406dd4bc52cb86f20dd2b11608a62c7ee9>,
[Symbol(Symbol.toStringTag)]: [Getter]
},
isSigner: false,
isWritable: true
},
[length]: 2
],
programId: PublicKey [PublicKey(11111111111111111111111111111111)] {
_bn: <BN: 0>,
[Symbol(Symbol.toStringTag)]: [Getter]
},
data: <Buffer 02 00 00 00 a0 86 01 00 00 00 00 00>
},
[length]: 1
]
*/
export default app;