Skip to content

Commit

Permalink
use panda
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfnjust committed Jan 5, 2024
1 parent 5b27538 commit a76b6c0
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 91 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"scrypt-ts": "^1.3.18",
"scrypt-ts": "^1.3.22",
"typescript": "^4.9.5",
"web-vitals": "^2.1.4"
},
Expand Down
3 changes: 0 additions & 3 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@
margin-top: 20px;
}

.sensilet {

}

.wallet label {
font-size: 1rem;
Expand Down
52 changes: 17 additions & 35 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "./App.css";
import Game from "./Game";
import { useState, useRef } from "react";
import TitleBar from "./TitleBar";
import { DefaultProvider, SensiletSigner, PubKey, toHex } from "scrypt-ts";
import { DefaultProvider, PandaSigner, PubKey, toHex } from "scrypt-ts";
import { TicTacToe } from "./contracts/tictactoe";
const initialGameData = {
amount: 0,
Expand All @@ -23,7 +23,7 @@ function App() {
const [gameData, setGameData] = useState(initialGameData);
const [isConnected, setConnected] = useState(false);

const signerRef = useRef<SensiletSigner>();
const signerRef = useRef<PandaSigner>();
const [contract, setContract] = useState<TicTacToe | undefined>(undefined)
const [deployedTxId, setDeployedTxId] = useState<string>("")
const [alicePubkey, setAlicePubkey] = useState("");
Expand All @@ -42,7 +42,7 @@ function App() {


try {
const signer = signerRef.current as SensiletSigner;
const signer = signerRef.current as PandaSigner;


const instance = new TicTacToe(
Expand Down Expand Up @@ -72,11 +72,11 @@ function App() {
setGameData(Object.assign({}, gameData, initialGameData))
};

const sensiletLogin = async () => {
console.log('sensiletLogin...')
const login = async () => {
console.log('login...')
try {
const provider = new DefaultProvider();
const signer = new SensiletSigner(provider);
const signer = new PandaSigner(provider);

signerRef.current = signer;

Expand All @@ -86,35 +86,17 @@ function App() {
}

const pubkey = await signer.getDefaultPubKey();
const changeAccountMessage = "Please change your account in Sensilet wallet, click again to get bob PublicKey";

if (!alicePubkey) {

setAlicePubkey(toHex(pubkey))

signer.getBalance().then(balance => {
setAliceBalance(balance.confirmed + balance.unconfirmed)
})

alert(changeAccountMessage)

} else {
if (toHex(pubkey) !== alicePubkey) {
setBobPubkey(toHex(pubkey))

signer.getBalance().then(balance => {
setBobBalance(balance.confirmed + balance.unconfirmed)
})

setConnected(true);
} else {
alert(changeAccountMessage)
}
}

setAlicePubkey(toHex(pubkey))
setBobPubkey(toHex(pubkey))
signer.getBalance().then(balance => {
setAliceBalance(balance.confirmed + balance.unconfirmed)
setBobBalance(balance.confirmed + balance.unconfirmed)
})
setConnected(true);
} catch (error) {
console.error("sensiletLogin failed", error);
alert("sensiletLogin failed")
console.error("Panda Wallet login failed", error);
alert("Panda Wallet login failed")
}
};

Expand Down Expand Up @@ -145,8 +127,8 @@ function App() {
</div>
:
<button
className="pure-button button-large sensilet"
onClick={sensiletLogin}
className="pure-button button-large"
onClick={login}
>
Connect Wallet
</button>
Expand Down
75 changes: 33 additions & 42 deletions src/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ function Game(props: any) {
return true;
}

async function isRightSensiletAccount() {
const current = props.contract as TicTacToe;

const expectedPubkey = current.isAliceTurn ? props.alicePubkey : props.bobPubkey;

const pubkey = await current.signer.getDefaultPubKey();

return toHex(pubkey) === expectedPubkey;
}

async function move(i: number) {
const current = props.contract as TicTacToe;

Expand Down Expand Up @@ -81,13 +71,7 @@ function Game(props: any) {
const history = gameData.history.slice(0, gameData.currentStepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();

const isRightAccount = await isRightSensiletAccount();

if (!isRightAccount) {
alert(`Please switch Sensilet to ${gameData.isAliceTurn ? "Alice" : "Bob"} account!`)
return;
}


if (!canMove(i, squares)) {
console.error('can not move now!')
Expand All @@ -100,33 +84,40 @@ function Game(props: any) {
};

// Call smart contract move method.
const { tx, nexts } = await move(i);

const square = squares[i] as SquareData;
if (square) {
square.tx = tx.id;
}

console.log('move txid:', tx.id)

// update states
if (nexts && nexts[0]) {
const instance = nexts[0].instance
props.setContract(instance)
try {
const { tx, nexts } = await move(i);

const square = squares[i] as SquareData;
if (square) {
square.tx = tx.id;
}

console.log('move txid:', tx.id)

// update states
if (nexts && nexts[0]) {
const instance = nexts[0].instance
props.setContract(instance)
}
const winner = calculateWinner(squares).winner;
setGameData({
...gameData,
history: history.concat([
{
squares
},
]),
isAliceTurn: winner ? gameData.isAliceTurn : !gameData.isAliceTurn,
currentStepNumber: history.length,
start: true
})
setLastTxId(tx.id)
} catch (error) {
console.error("error:", error);
alert("ERROR:" + error.message)
}
const winner = calculateWinner(squares).winner;
setGameData({
...gameData,
history: history.concat([
{
squares
},
]),
isAliceTurn: winner ? gameData.isAliceTurn : !gameData.isAliceTurn,
currentStepNumber: history.length,
start: true
})
setLastTxId(tx.id)

}


Expand Down
6 changes: 3 additions & 3 deletions src/TitleBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function TitleBar(props : any) {

let amount = parseInt(amountRef.current.value);

if (amount < 10000) {
alert("invalid amount, at least 10000 satoshis")
if (amount < 1) {
alert("invalid amount, at least 1 satoshis")
return;
}

Expand Down Expand Up @@ -39,7 +39,7 @@ function TitleBar(props : any) {
return (
<div>
<label>Bet amount:
<input ref={amountRef} type="number" name="amount" min="1" defaultValue={10000} placeholder="in satoshis" />
<input ref={amountRef} type="number" name="amount" min="1" defaultValue={1} placeholder="in satoshis" />
</label>
<button className="start" onClick={onStart}>Start</button>
</div>
Expand Down

0 comments on commit a76b6c0

Please sign in to comment.