-
Notifications
You must be signed in to change notification settings - Fork 16
/
methods.ts
179 lines (172 loc) · 4.7 KB
/
methods.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
import { blake2b256, ss58Encode } from "@polkadot-labs/hdkd-helpers";
import { FixedSizeBinary, type PolkadotSigner } from "polkadot-api";
import { data } from "../context/data";
import { toHex } from "../utils";
import { SHOULD_USE_LOCAL_DATA } from "./constants";
import { polkadotApi } from "./papi-client";
let kitties = data.kitties.map((kitty) => ({
...kitty,
price: kitty.price?.toString(),
}));
let kittiesOwned = data.kittiesOwned;
export async function buyKitty({
polkadotSigner,
dna,
maxPrice,
}: {
polkadotSigner?: PolkadotSigner;
dna: string;
maxPrice: bigint;
}) {
if (!polkadotSigner) {
throw new Error("No signer found");
}
if (SHOULD_USE_LOCAL_DATA) {
const kitty = kitties.find((kitty) => kitty.dna === dna);
if (!kitty) {
return { ok: false, error: "Kitty not found" };
}
const newOwner = ss58Encode(polkadotSigner.publicKey, 0);
kittiesOwned = {
...kittiesOwned,
[kitty.owner]: kittiesOwned[kitty.owner].filter((kitty) => kitty !== dna),
[newOwner]: [...kittiesOwned[newOwner], dna],
};
kitties = kitties.map((kitty) =>
kitty.dna === dna ? { ...kitty, owner: newOwner } : kitty
);
return { ok: true };
}
if (!polkadotApi) {
throw new Error("No polkadot API found");
}
const kittyId = FixedSizeBinary.fromHex(dna);
return await polkadotApi.tx.Kitties.buy_kitty({
kitty_id: kittyId,
max_price: maxPrice,
}).signAndSubmit(polkadotSigner);
}
export async function mintKitty({
polkadotSigner,
}: {
polkadotSigner?: PolkadotSigner;
}) {
if (!polkadotSigner) {
throw new Error("No signer found");
}
if (SHOULD_USE_LOCAL_DATA) {
const owner = ss58Encode(polkadotSigner.publicKey, 0);
const dna = `0x${toHex(
blake2b256(crypto.getRandomValues(new Uint8Array(32)))
)}`;
const newKitty = {
dna,
owner,
price: undefined,
};
kitties = [...kitties, newKitty];
kittiesOwned[owner] = [...kittiesOwned[owner], newKitty.dna];
return { ok: true };
}
if (!polkadotApi) {
throw new Error("No polkadot API found");
}
return await polkadotApi.tx.Kitties.create_kitty().signAndSubmit(
polkadotSigner
);
}
export async function setPrice({
polkadotSigner,
dna,
price,
}: {
polkadotSigner?: PolkadotSigner;
dna: string;
price?: bigint;
}) {
if (!polkadotSigner) {
throw new Error("No signer found");
}
if (SHOULD_USE_LOCAL_DATA) {
const kitty = kitties.find((kitty) => kitty.dna === dna);
if (!kitty) {
return { ok: false, error: "Kitty not found" };
}
kitties = kitties.map((kitty) =>
kitty.dna === dna ? { ...kitty, price: price?.toString() } : kitty
);
return { ok: true };
}
if (!polkadotApi) {
throw new Error("No polkadot API found");
}
const kittyId = FixedSizeBinary.fromHex(dna);
return await polkadotApi.tx.Kitties.set_price({
kitty_id: kittyId,
new_price: price,
}).signAndSubmit(polkadotSigner);
}
export async function transferKitty({
polkadotSigner,
kittyId,
newOwner,
}: {
polkadotSigner?: PolkadotSigner;
kittyId: string;
newOwner: string;
}) {
if (!polkadotSigner) {
throw new Error("No signer found");
}
if (SHOULD_USE_LOCAL_DATA) {
const kitty = kitties.find((kitty) => kitty.dna === kittyId);
if (!kitty) {
return { ok: false, error: "Kitty not found" };
}
kitties = kitties.map((kitty) =>
kitty.dna === kittyId ? { ...kitty, owner: newOwner } : kitty
);
kittiesOwned = {
...kittiesOwned,
[kitty.owner]: kittiesOwned[kitty.owner].filter(
(kitty) => kitty !== kittyId
),
[newOwner]: [...kittiesOwned[newOwner], kittyId],
};
return { ok: true };
}
if (!polkadotApi) {
throw new Error("No polkadot API found");
}
return await polkadotApi.tx.Kitties.transfer({
kitty_id: FixedSizeBinary.fromHex(kittyId),
to: newOwner,
}).signAndSubmit(polkadotSigner);
}
export async function getKitties() {
if (SHOULD_USE_LOCAL_DATA) {
return [...kitties];
}
if (!polkadotApi) {
throw new Error("No polkadot API found");
}
const response = await polkadotApi.query.Kitties.Kitties.getEntries();
return response.map((kitty) => ({
dna: kitty.value.dna.asHex(),
owner: kitty.value.owner.toString(),
price: kitty.value.price?.toString(),
}));
}
export async function getKittiesOwned() {
if (SHOULD_USE_LOCAL_DATA) {
return { ...kittiesOwned };
}
if (!polkadotApi) {
throw new Error("No polkadot API found");
}
const response = await polkadotApi.query.Kitties.KittiesOwned.getEntries();
return response.reduce((acc, kitty) => {
acc[kitty.keyArgs.toString()] = kitty.value.map((dna) => dna.asHex());
return acc;
}, {} as Record<string, string[]>);
}