Skip to content

Commit

Permalink
Merge pull request #52 from MystenLabs/vlas/upgrade-sui-sdk
Browse files Browse the repository at this point in the history
[SE-705] Upgrade app sdk version to ^1.30.0
  • Loading branch information
vlasaki authored Jul 24, 2024
2 parents c462769 + 9bc243e commit 058fc06
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 242 deletions.
306 changes: 119 additions & 187 deletions app/package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
},
"dependencies": {
"@hookform/resolvers": "^3.3.1",
"@mysten/enoki": "0.0.0-experimental-20240407101448",
"@mysten/sui.js": "^0.41.2",
"@mysten/enoki": "^0.3.11",
"@mysten/sui": "^1.3.0",
"@noble/curves": "^1.3.0",
"@noble/hashes": "^1.3.3",
"@radix-ui/react-accordion": "^1.1.2",
Expand Down
4 changes: 2 additions & 2 deletions app/src/app/api/games/[id]/deal/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { doInitialDeal } from "@/app/api/services/doInitialDeal";
import { SuiClient } from "@mysten/sui.js/client";
import { SuiClient } from "@mysten/sui/client";
import { NextRequest, NextResponse } from "next/server";

// Waits for the transaction block that created the game
Expand All @@ -15,7 +15,7 @@ export const POST = async (
const { id: gameId } = params;
const { txDigest } = await req.json();

await suiClient.waitForTransactionBlock({
await suiClient.waitForTransaction({
digest: txDigest,
timeout: 10_000,
});
Expand Down
4 changes: 2 additions & 2 deletions app/src/app/api/games/[id]/hit/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { houseHitOrStand } from "@/app/api/services/houseHitOrStand";
import { SuiClient } from "@mysten/sui.js/client";
import { SuiClient } from "@mysten/sui/client";
import { NextRequest, NextResponse } from "next/server";

// Waits for the transaction block that made the hit request
Expand All @@ -15,7 +15,7 @@ export const POST = async (
});
const { requestObjectId, txDigest } = await req.json();

await suiClient.waitForTransactionBlock({
await suiClient.waitForTransaction({
digest: txDigest,
timeout: 10_000,
});
Expand Down
4 changes: 2 additions & 2 deletions app/src/app/api/games/[id]/stand/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { houseHitOrStand } from "@/app/api/services/houseHitOrStand";
import { SuiClient } from "@mysten/sui.js/client";
import { SuiClient } from "@mysten/sui/client";
import { NextRequest, NextResponse } from "next/server";

// Waits for the transaction block that made the stand request
Expand All @@ -15,7 +15,7 @@ export const POST = async (
});
const { requestObjectId, txDigest } = await req.json();

await suiClient.waitForTransactionBlock({
await suiClient.waitForTransaction({
digest: txDigest,
timeout: 10_000,
});
Expand Down
2 changes: 1 addition & 1 deletion app/src/app/api/helpers/getGameObject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GameOnChain } from "@/types/GameOnChain";
import { SuiClient, SuiMoveObject } from "@mysten/sui.js/client";
import { SuiClient, SuiMoveObject } from "@mysten/sui/client";

interface GetGameObjectProps {
suiClient: SuiClient;
Expand Down
4 changes: 2 additions & 2 deletions app/src/app/api/helpers/getKeyPair.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fromB64 } from "@mysten/sui.js/utils";
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519";
import { fromB64 } from "@mysten/sui/utils";
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";

export const getKeypair = (secretKey: string) => {
let privateKeyArray = Uint8Array.from(Array.from(fromB64(secretKey)));
Expand Down
9 changes: 5 additions & 4 deletions app/src/app/api/services/doInitialDeal.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { SuiClient } from "@mysten/sui.js/client";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { SuiClient } from "@mysten/sui/client";
import { Transaction } from "@mysten/sui/transactions";
import { getKeypair } from "../helpers/getKeyPair";
import { bytesToHex } from "@noble/hashes/utils";
import { bls12_381 } from "@noble/curves/bls12-381";
import { getBLSSecreyKey } from "../helpers/getBLSSecretKey";
import { getGameObject } from "../helpers/getGameObject";
import { sponsorAndSignTransaction } from "../utils/sponsorAndSignTransaction";
import { bcs } from "@mysten/sui/bcs";

interface DoInitialDealProps {
suiClient: SuiClient;
Expand All @@ -23,7 +24,7 @@ export const doInitialDeal = async ({

const adminKeypair = getKeypair(process.env.ADMIN_SECRET_KEY!);

const tx = new TransactionBlock();
const tx = new Transaction();
return getGameObject({ suiClient, gameId }).then(async (resp) => {
const { counter, user_randomness } = resp;
const counterHex = bytesToHex(Uint8Array.from([counter]));
Expand All @@ -45,7 +46,7 @@ export const doInitialDeal = async ({
target: `${process.env.NEXT_PUBLIC_PACKAGE_ADDRESS}::single_player_blackjack::first_deal`,
arguments: [
tx.object(gameId),
tx.pure(Array.from(signedHouseHash), "vector<u8>"),
tx.pure(bcs.vector(bcs.u8()).serialize(signedHouseHash)),
tx.object(houseDataId),
],
});
Expand Down
11 changes: 6 additions & 5 deletions app/src/app/api/services/houseHitOrStand.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { SuiClient } from "@mysten/sui.js/client";
import { formatAddress } from "@mysten/sui.js/utils";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { SuiClient } from "@mysten/sui/client";
import { formatAddress } from "@mysten/sui/utils";
import { Transaction } from "@mysten/sui/transactions";
import { bytesToHex } from "@noble/curves/abstract/utils";
import { bls12_381 } from "@noble/curves/bls12-381";
import { getGameObject } from "../helpers/getGameObject";
import { getBLSSecreyKey } from "../helpers/getBLSSecretKey";
import { sponsorAndSignTransaction } from "../utils/sponsorAndSignTransaction";
import { bcs } from "@mysten/sui/bcs";

interface HouseHitOrStandProps {
gameId: string;
Expand All @@ -30,7 +31,7 @@ export const houseHitOrStand = async ({
);

return getGameObject({ suiClient, gameId }).then(async (resp) => {
const tx = new TransactionBlock();
const tx = new Transaction();
const { counter, user_randomness } = resp;
const counterHex = bytesToHex(Uint8Array.from([counter]));
const randomnessHexString = bytesToHex(Uint8Array.from(user_randomness));
Expand All @@ -44,7 +45,7 @@ export const houseHitOrStand = async ({
target: `${process.env.NEXT_PUBLIC_PACKAGE_ADDRESS}::single_player_blackjack::${move}`,
arguments: [
tx.object(gameId),
tx.pure(Array.from(signedHouseHash), "vector<u8>"),
tx.pure(bcs.vector(bcs.u8()).serialize(signedHouseHash)),
tx.object(houseDataId),
tx.object(requestObjectId),
],
Expand Down
10 changes: 5 additions & 5 deletions app/src/app/api/utils/sponsorAndSignTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { SuiClient } from "@mysten/sui.js/client";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { SuiClient } from "@mysten/sui/client";
import { Transaction } from "@mysten/sui/transactions";
import { sponsorTransaction } from "./sponsorTransaction";
import { getKeypair } from "../helpers/getKeyPair";

interface SponsorAndSignTransactionProps {
suiClient: SuiClient;
tx: TransactionBlock;
tx: Transaction;
}

export const sponsorAndSignTransaction = async ({
Expand All @@ -24,8 +24,8 @@ export const sponsorAndSignTransaction = async ({
if (!sponsoredTransaction) {
throw new Error("Sponsoring transaction failed");
}
const signedTransaction = await adminKeypair.signTransactionBlock(
await TransactionBlock.from(sponsoredTransaction.txBytes).build({
const signedTransaction = await adminKeypair.signTransaction(
await Transaction.from(sponsoredTransaction.txBytes).build({
client: suiClient,
})
);
Expand Down
2 changes: 1 addition & 1 deletion app/src/components/general/SuiExplorerLink.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getNetworkName } from "@/helpers/getNetworkName";
import { getSuiExplorerLink } from "@/helpers/getSuiExplorerLink";
import { formatAddress } from "@mysten/sui.js/utils";
import { formatAddress } from "@mysten/sui/utils";
import { OpenInNewWindowIcon } from "@radix-ui/react-icons";
import Link from "next/link";
import React from "react";
Expand Down
2 changes: 1 addition & 1 deletion app/src/components/general/UserProfileMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "../ui/dropdown-menu";
import { formatAddress } from "@mysten/sui.js/utils";
import { formatAddress } from "@mysten/sui/utils";
import { LogOut } from "lucide-react";
import { jwtDecode } from "jwt-decode";
import Image from "next/image";
Expand Down
2 changes: 1 addition & 1 deletion app/src/contexts/BalanceContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext, useEffect, useState, createContext } from "react";
import { ChildrenProps } from "@/types/ChildrenProps";
import BigNumber from "bignumber.js";
import { useZkLogin } from "@mysten/enoki/react";
import { MIST_PER_SUI } from "@mysten/sui.js/utils";
import { MIST_PER_SUI } from "@mysten/sui/utils";
import { useSui } from "@/hooks/useSui";

export const useBalance = () => {
Expand Down
2 changes: 1 addition & 1 deletion app/src/hooks/useBlackjackGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const useBlackjackGame = () => {
}
setIsLoading(true);
if (!!txDigest) {
await suiClient.waitForTransactionBlock({
await suiClient.waitForTransaction({
digest: txDigest,
timeout: 10_000,
});
Expand Down
16 changes: 9 additions & 7 deletions app/src/hooks/useCreateBlackjackGame.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useCallback, useState } from "react";
import { useSui } from "./useSui";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { SuiObjectChangeCreated } from "@mysten/sui.js/client";
import { Transaction } from "@mysten/sui/transactions";
import { SuiObjectChangeCreated } from "@mysten/sui/client";
import toast from "react-hot-toast";
import axios from "axios";
import { useEnokiFlow } from "@mysten/enoki/react";
import { MIST_PER_SUI } from "@mysten/sui/utils";
import { bcs } from "@mysten/sui/bcs";

interface HandleCreateGameSuccessResponse {
gameId: string;
Expand All @@ -29,21 +31,21 @@ export const useCreateBlackjackGame = () => {
}
console.log("Creating game...");
setIsCreateGameLoading(true);
const tx = new TransactionBlock();
const betAmountCoin = tx.splitCoins(tx.gas, [tx.pure("200000000")]);
const tx = new Transaction();
const betAmountCoin = tx.splitCoins(tx.gas, [tx.pure.u64(0.2 * Number(MIST_PER_SUI))]);
tx.moveCall({
target: `${process.env.NEXT_PUBLIC_PACKAGE_ADDRESS}::single_player_blackjack::place_bet_and_create_game`,
arguments: [
tx.pure(randomBytesAsHexString),
tx.pure.string(randomBytesAsHexString),
tx.object(counterId!),
betAmountCoin,
tx.object(process.env.NEXT_PUBLIC_HOUSE_DATA_ID!),
],
});
console.log("Executing transaction...");
const signer = await enokiFlow.getKeypair({network: "testnet"});
return suiClient.signAndExecuteTransactionBlock({
transactionBlock: tx,
return suiClient.signAndExecuteTransaction({
transaction: tx,
signer: signer as any,
requestType: "WaitForLocalExecution",
options: {
Expand Down
13 changes: 7 additions & 6 deletions app/src/hooks/useMakeMoveInBlackjackGame.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useSui } from "./useSui";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { SuiObjectChangeCreated } from "@mysten/sui.js/client";
import { Transaction } from "@mysten/sui/transactions";
import { SuiObjectChangeCreated } from "@mysten/sui/client";
import { useCallback, useState } from "react";
import { GameOnChain } from "@/types/GameOnChain";
import toast from "react-hot-toast";
import axios from "axios";
import { getAddress } from "@/app/api/helpers/getAddress";

interface HandleHitOrStandProps {
move: "hit" | "stand";
Expand Down Expand Up @@ -42,17 +43,17 @@ export const useMakeMoveInBlackjackGame = () => {

setIsMoveLoading(true);
const { player_sum } = game;
const tx = new TransactionBlock();
const tx = new Transaction();
let request = tx.moveCall({
target: `${process.env.NEXT_PUBLIC_PACKAGE_ADDRESS}::single_player_blackjack::do_${move}`,
arguments: [tx.object(gameId), tx.pure(player_sum, "u8")],
arguments: [tx.object(gameId), tx.pure.u8(player_sum)],
});
tx.transferObjects(
[request],
tx.pure(process.env.NEXT_PUBLIC_ADMIN_ADDRESS!)
tx.pure.address(process.env.NEXT_PUBLIC_ADMIN_ADDRESS!)
);
return enokiSponsorExecute({
transactionBlock: tx,
transaction: tx,
options: {
showObjectChanges: true,
showEffects: true,
Expand Down
8 changes: 4 additions & 4 deletions app/src/hooks/usePlayerCounter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from "react";
import { useSui } from "./useSui";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { SuiObjectChangeCreated } from "@mysten/sui.js/client";
import { Transaction } from "@mysten/sui/transactions";
import { SuiObjectChangeCreated } from "@mysten/sui/client";
import toast from "react-hot-toast";
import { getCounterId } from "@/utils/getCounterId";
import { useZkLogin } from "@mysten/enoki/react";
Expand All @@ -15,14 +15,14 @@ export const usePlayerCounter = () => {

const handleCreateCounter = async () => {
setIsCreateLoading(true);
const tx = new TransactionBlock();
const tx = new Transaction();
tx.moveCall({
target: `${process.env.NEXT_PUBLIC_PACKAGE_ADDRESS}::counter_nft::mint_and_transfer`,
arguments: [],
});

return enokiSponsorExecute({
transactionBlock: tx,
transaction: tx,
options: {
showObjectChanges: true,
showEffects: true,
Expand Down
2 changes: 1 addition & 1 deletion app/src/hooks/useRequestSui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const useRequestSui = () => {
})
.then(async (resp) => {
setIsLoading(false);
await suiClient.waitForTransactionBlock({
await suiClient.waitForTransaction({
digest: resp.data.txDigest,
});
handleRefreshBalance();
Expand Down
12 changes: 6 additions & 6 deletions app/src/hooks/useSui.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
SuiClient,
SuiTransactionBlockResponseOptions,
} from "@mysten/sui.js/client";
import { TransactionBlock } from "@mysten/sui.js/transactions";
} from "@mysten/sui/client";
import { Transaction } from "@mysten/sui/transactions";
import { useEnokiFlow } from "@mysten/enoki/react";

interface EnokiSponsorExecuteProps {
transactionBlock: TransactionBlock;
transaction: Transaction;
options?: SuiTransactionBlockResponseOptions;
}

Expand All @@ -16,16 +16,16 @@ export const useSui = () => {
const enokiFlow = useEnokiFlow();

const enokiSponsorExecute = async ({
transactionBlock,
transaction,
options,
}: EnokiSponsorExecuteProps) => {
return enokiFlow
.sponsorAndExecuteTransactionBlock({
.sponsorAndExecuteTransaction({
network: process.env.NEXT_PUBLIC_SUI_NETWORK?.includes("testnet")
? "testnet"
: "mainnet",
transaction: transaction as any,
client: suiClient as any,
transactionBlock: transactionBlock as any,
})
.then((resp) => {
return suiClient.getTransactionBlock({
Expand Down
2 changes: 1 addition & 1 deletion app/src/utils/getCounterId.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SuiClient } from "@mysten/sui.js/client";
import { SuiClient } from "@mysten/sui/client";

interface GetCounterIdProps {
address: string;
Expand Down
2 changes: 1 addition & 1 deletion app/src/utils/getGameObject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GameOnChain } from "@/types/GameOnChain";
import { SuiClient, SuiMoveObject } from "@mysten/sui.js/client";
import { SuiClient, SuiMoveObject } from "@mysten/sui/client";

interface GetGameObjectProps {
suiClient: SuiClient;
Expand Down

0 comments on commit 058fc06

Please sign in to comment.