/
[...api].ts
76 lines (68 loc) · 1.98 KB
/
[...api].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
/**
* Copyright 2024 Shinami Corp.
* SPDX-License-Identifier: Apache-2.0
*/
import { gas, sui } from "@/lib/api/shinami";
import { Hero, MintHeroRequest, PACKAGE_ID } from "@/lib/shared/hero";
import {
WithOwner,
WithTxDigest,
parseObjectWithOwner,
} from "@/lib/shared/sui";
import { AuthContext } from "@/lib/shared/zklogin";
import { buildGaslessTransactionBytes } from "@shinami/clients";
import {
GaslessTransactionBytesBuilder,
InvalidRequest,
TransactionResponseParser,
zkLoginSponsoredTxExecHandler,
} from "@shinami/nextjs-zklogin/server/pages";
import { validate } from "superstruct";
const buildTx: GaslessTransactionBytesBuilder<AuthContext> = async (
req,
{ wallet, oidProvider, authContext },
) => {
const [error, body] = validate(req.body, MintHeroRequest);
if (error) throw new InvalidRequest(error.message);
console.debug(
"Minting new hero for %s user %s",
oidProvider,
authContext.email,
);
const gaslessTxBytes = await buildGaslessTransactionBytes({
sui,
build: async (txb) => {
const hero = txb.moveCall({
target: `${PACKAGE_ID}::hero::mint_hero`,
arguments: [
txb.object(body.ticketId),
txb.pure(body.name),
txb.pure(body.damage),
txb.pure(body.speed),
txb.pure(body.defense),
],
});
txb.transferObjects([hero], txb.pure(wallet));
},
});
return { gaslessTxBytes };
};
const parseTxRes: TransactionResponseParser<
AuthContext,
Hero & WithOwner & WithTxDigest
> = async (_, txRes) => {
const ref = txRes.effects?.created?.at(0)?.reference;
if (!ref) {
console.error("Object not created", txRes);
throw new Error("Object not created");
}
const hero = parseObjectWithOwner(
await sui.getObject({
id: ref.objectId,
options: { showContent: true, showOwner: true },
}),
Hero,
);
return { ...hero, txDigest: txRes.digest };
};
export default zkLoginSponsoredTxExecHandler(sui, gas, buildTx, parseTxRes);