/
initializeHouseData.ts
71 lines (66 loc) · 2.13 KB
/
initializeHouseData.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
import { SuiClient, SuiObjectChangeCreated } from "@mysten/sui.js/client";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { getKeypair } from "../keypair/getKeyPair";
import { getBLSPublicKey } from "../bls/getBLSPublicKey";
import {
PACKAGE_ADDRESS,
HOUSE_ADMIN_CAP,
ADMIN_SECRET_KEY,
} from "../../config";
import { MIST_PER_SUI } from "@mysten/sui.js/utils";
interface InitializeHouseBalanceProps {
suiClient: SuiClient;
}
export const initializeHouseData = async ({
suiClient,
}: InitializeHouseBalanceProps): Promise<string | undefined> => {
console.log("Initializing HouseData...");
const tx = new TransactionBlock();
const houseCoin = tx.splitCoins(tx.gas, [
tx.pure(10 * Number(MIST_PER_SUI)),
]);
let adminBLSPublicKey = getBLSPublicKey(ADMIN_SECRET_KEY!);
tx.moveCall({
target: `${PACKAGE_ADDRESS}::single_player_blackjack::initialize_house_data`,
arguments: [
tx.object(HOUSE_ADMIN_CAP),
houseCoin,
tx.pure(Array.from(adminBLSPublicKey)),
],
});
return suiClient
.signAndExecuteTransactionBlock({
signer: getKeypair(ADMIN_SECRET_KEY!),
transactionBlock: tx,
requestType: "WaitForLocalExecution",
options: {
showObjectChanges: true,
showEffects: true,
},
})
.then((resp) => {
const status = resp?.effects?.status.status;
console.log("executed! status = ", status);
if (status !== "success") {
throw new Error("HouseData not created");
}
if (status === "success") {
const createdObjects = resp.objectChanges?.filter(
({ type }) => type === "created"
) as SuiObjectChangeCreated[];
const createdHouseData = createdObjects.find(({ objectType }) =>
objectType.endsWith("single_player_blackjack::HouseData")
);
if (!createdHouseData) {
throw new Error("HouseData not created");
}
const { objectId: houseDataId } = createdHouseData;
console.log({ houseDataId });
return houseDataId;
}
})
.catch((err) => {
console.error("Error = ", err);
return undefined;
});
};