From ef5a9cfba13ecd6552cf07e255de69e9b87123d7 Mon Sep 17 00:00:00 2001 From: uvd <386180127@qq.com> Date: Tue, 12 Jul 2022 20:32:44 +0800 Subject: [PATCH 1/5] free bank --- public/locales/en/translation.json | 3 +- public/locales/zh/translation.json | 3 +- src/components/Sidebar/game_menu.ts | 8 +- src/games/free_bank/.gitignore | 4 + src/games/free_bank/Move.toml | 11 ++ src/games/free_bank/index.ts | 225 ++++++++++++++++++++++ src/games/free_bank/sources/FreeBank.move | 70 +++++++ src/pages/Game/FreeBank/index.tsx | 88 +++++++++ src/router/router.tsx | 7 +- 9 files changed, 413 insertions(+), 6 deletions(-) create mode 100644 src/games/free_bank/.gitignore create mode 100644 src/games/free_bank/Move.toml create mode 100644 src/games/free_bank/index.ts create mode 100644 src/games/free_bank/sources/FreeBank.move create mode 100644 src/pages/Game/FreeBank/index.tsx diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 25ba854..8b85d0f 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -11,7 +11,8 @@ "games": "Games", "showdown": "Showdown", "finger_guessing": "FingerGuessing", - "donate": "Donate" + "donate": "Donate", + "free_bank": "Free Bank" }, "issue_token": { diff --git a/public/locales/zh/translation.json b/public/locales/zh/translation.json index b8e5988..e91daed 100644 --- a/public/locales/zh/translation.json +++ b/public/locales/zh/translation.json @@ -11,7 +11,8 @@ "games": "游戏", "showdown": "猜大小", "finger_guessing": "剪刀石头布", - "donate": "捐赠" + "donate": "捐赠", + "free_bank": "无息存款" }, "issue_token": { diff --git a/src/components/Sidebar/game_menu.ts b/src/components/Sidebar/game_menu.ts index 77be822..ba16d61 100644 --- a/src/components/Sidebar/game_menu.ts +++ b/src/components/Sidebar/game_menu.ts @@ -13,7 +13,13 @@ const gameMenu = [ name: "finger_guessing", name_i18_key: "menu.finger_guessing", icon: VideogameAssetIcon, - path: "/game/finger_guessing" + path: "/game/finger-guessing" + }, + { + name: "free_bank", + name_i18_key: "menu.free_bank", + icon: VideogameAssetIcon, + path: "/game/free-bank" }, ] diff --git a/src/games/free_bank/.gitignore b/src/games/free_bank/.gitignore new file mode 100644 index 0000000..cfcae93 --- /dev/null +++ b/src/games/free_bank/.gitignore @@ -0,0 +1,4 @@ +build +.DS_Store +.idea +release \ No newline at end of file diff --git a/src/games/free_bank/Move.toml b/src/games/free_bank/Move.toml new file mode 100644 index 0000000..1af2dda --- /dev/null +++ b/src/games/free_bank/Move.toml @@ -0,0 +1,11 @@ +[package] +name = "free_bank" +version = "0.0.1" + + +[addresses] +StarcoinFramework = "0x1" +admin = "0x12Fd2AFaA16Ca7480e877098c199Ab84" + +[dependencies] +StarcoinFramework = { git = "https://github.com/starcoinorg/starcoin-framework.git", rev = "cf1deda180af40a8b3e26c0c7b548c4c290cd7e7" } diff --git a/src/games/free_bank/index.ts b/src/games/free_bank/index.ts new file mode 100644 index 0000000..10675de --- /dev/null +++ b/src/games/free_bank/index.ts @@ -0,0 +1,225 @@ +import {bcs, utils} from "@starcoin/starcoin"; +import {arrayify, hexlify} from "@ethersproject/bytes"; +import {getProvder} from "../../utils/stcWalletSdk"; +import {NANO_STC, nodeUrlMap} from "../../utils/consts"; +import {ADMIN_ADDRESS} from "../index"; + +export async function initBank() { + try { + const functionId = `${ADMIN_ADDRESS}::FreeBank::init`; + const nodeUrl = nodeUrlMap[window.starcoin.networkVersion]; + const scriptFunction = await utils.tx.encodeScriptFunctionByResolve( + functionId, + [], + [], + nodeUrl + ); + + // Multiple BcsSerializers should be used in different closures, otherwise, the latter will be contaminated by the former. + const payloadInHex = (function () { + const se = new bcs.BcsSerializer(); + scriptFunction.serialize(se); + return hexlify(se.getBytes()); + })(); + + const txParams = { + data: payloadInHex, + }; + + const expiredSecs = 10; + if (expiredSecs > 0) { + // txParams.expiredSecs = expiredSecs + } + + const starcoinProvider = await getProvder(); + const transactionHash = await starcoinProvider + .getSigner() + .sendUncheckedTransaction(txParams); + window.console.log({transactionHash}); + } catch (e) { + window.console.error(e); + } +} + +export async function extract() { + try { + const functionId = `${ADMIN_ADDRESS}::FreeBank::extract`; + const nodeUrl = nodeUrlMap[window.starcoin.networkVersion]; + const scriptFunction = await utils.tx.encodeScriptFunctionByResolve( + functionId, + [], + [], + nodeUrl + ); + + // Multiple BcsSerializers should be used in different closures, otherwise, the latter will be contaminated by the former. + const payloadInHex = (function () { + const se = new bcs.BcsSerializer(); + scriptFunction.serialize(se); + return hexlify(se.getBytes()); + })(); + + const txParams = { + data: payloadInHex, + }; + + const expiredSecs = 10; + if (expiredSecs > 0) { + // txParams.expiredSecs = expiredSecs + } + + const starcoinProvider = await getProvder(); + const transactionHash = await starcoinProvider + .getSigner() + .sendUncheckedTransaction(txParams); + window.console.log({transactionHash}); + } catch (e) { + window.console.error(e); + } +} + + +export async function restore() { + try { + const functionId = `${ADMIN_ADDRESS}::FreeBank::restore`; + const nodeUrl = nodeUrlMap[window.starcoin.networkVersion]; + const scriptFunction = await utils.tx.encodeScriptFunctionByResolve( + functionId, + [], + [], + nodeUrl + ); + + // Multiple BcsSerializers should be used in different closures, otherwise, the latter will be contaminated by the former. + const payloadInHex = (function () { + const se = new bcs.BcsSerializer(); + scriptFunction.serialize(se); + return hexlify(se.getBytes()); + })(); + + const txParams = { + data: payloadInHex, + }; + + const expiredSecs = 10; + if (expiredSecs > 0) { + // txParams.expiredSecs = expiredSecs + } + + const starcoinProvider = await getProvder(); + const transactionHash = await starcoinProvider + .getSigner() + .sendUncheckedTransaction(txParams); + window.console.log({transactionHash}); + } catch (e) { + window.console.error(e); + } +} + + +export async function deposit(amount: number) { + try { + const functionId = `${ADMIN_ADDRESS}::FreeBank::deposit`; + + const args = [amount]; + const nodeUrl = nodeUrlMap[window.starcoin.networkVersion]; + const scriptFunction = await utils.tx.encodeScriptFunctionByResolve( + functionId, + [], + args, + nodeUrl + ); + + // Multiple BcsSerializers should be used in different closures, otherwise, the latter will be contaminated by the former. + const payloadInHex = (function () { + const se = new bcs.BcsSerializer(); + scriptFunction.serialize(se); + return hexlify(se.getBytes()); + })(); + + const txParams = { + data: payloadInHex, + }; + + const expiredSecs = 10; + if (expiredSecs > 0) { + // txParams.expiredSecs = expiredSecs + } + + const starcoinProvider = await getProvder(); + const transactionHash = await starcoinProvider + .getSigner() + .sendUncheckedTransaction(txParams); + window.console.log({transactionHash}); + } catch (e) { + window.console.error(e); + } +} + +export async function withdraw(amount: number) { + try { + const functionId = `${ADMIN_ADDRESS}::FreeBank::withdraw`; + const args = [amount]; + const nodeUrl = nodeUrlMap[window.starcoin.networkVersion]; + const scriptFunction = await utils.tx.encodeScriptFunctionByResolve( + functionId, + [], + args, + nodeUrl + ); + + // Multiple BcsSerializers should be used in different closures, otherwise, the latter will be contaminated by the former. + const payloadInHex = (function () { + const se = new bcs.BcsSerializer(); + scriptFunction.serialize(se); + return hexlify(se.getBytes()); + })(); + + const txParams = { + data: payloadInHex, + }; + + const expiredSecs = 10; + if (expiredSecs > 0) { + // txParams.expiredSecs = expiredSecs + } + + const starcoinProvider = await getProvder(); + const transactionHash = await starcoinProvider + .getSigner() + .sendUncheckedTransaction(txParams); + window.console.log({transactionHash}); + } catch (e) { + window.console.error(e); + } +} + +export async function getBankAmount(token: any) { + try { + const provider = await getProvder(); + const result = await provider.getResource( + ADMIN_ADDRESS, + `${ADMIN_ADDRESS}::GameFingerGuessing::Bank<${token}>` + ); + if (result && result.bank && result.bank) { + // @ts-ignore + return result.bank.value / NANO_STC; + } + return 0; + } catch (e) { + window.console.error(e); + return 0; + } +} + +export function decodeCheckEvent(data: string) { + const de = new bcs.BcsDeserializer(arrayify(data)); + const amount = de.deserializeU128(); + const result = de.deserializeU8(); + const input = de.deserializeU8(); + return { + amount, + result, + input, + }; +} diff --git a/src/games/free_bank/sources/FreeBank.move b/src/games/free_bank/sources/FreeBank.move new file mode 100644 index 0000000..7824fce --- /dev/null +++ b/src/games/free_bank/sources/FreeBank.move @@ -0,0 +1,70 @@ +address admin { +module FreeBank { + use StarcoinFramework::Account; + use StarcoinFramework::Signer; + use StarcoinFramework::Token; + use StarcoinFramework::Math; + use StarcoinFramework::STC::STC; + + const TOKEN_PRECISION: u8 = 9; + const INIT_MINT: u128 = 10000000000; + + struct FBC has drop, store, key {} + + struct FreeBank has store, key { + cap: Account::WithdrawCapability + } + + + public(script) fun init(signer: signer) { + Token::register_token(&signer, TOKEN_PRECISION); + Account::do_accept_token(&signer); + let scaling_factor = Math::pow(10, (TOKEN_PRECISION as u64)); + let token = Token::mint(&signer, INIT_MINT * scaling_factor); + Account::deposit_to_self(&signer, token); + } + + public(script) fun extract(signer: signer) { + let capability = Account::extract_withdraw_capability(&signer); + let fb = FreeBank { + cap: capability + }; + move_to(&signer, fb); + } + + public(script) fun restore(signer: signer) acquires FreeBank { + let FreeBank { cap } = move_from(Signer::address_of(&signer)); + Account::restore_withdraw_capability(cap); + } + + + /// withdraw from bank + public(script) fun withdraw(signer: signer, amount: u128) acquires FreeBank { + let signer_address = Signer::address_of(&signer); + let capability = Account::extract_withdraw_capability(&signer); + Account::pay_from_capability(&capability, + @admin, + amount, x""); + Account::restore_withdraw_capability(capability); + + let bank = borrow_global(@admin); + Account::pay_from_capability(&bank.cap, + signer_address, + amount, x""); + } + + /// deposit amount to bank + public(script) fun deposit(signer: signer, amount: u128) acquires FreeBank { + let capability = Account::extract_withdraw_capability(&signer); + Account::pay_from_capability(&capability, + @admin, + amount, x""); + Account::restore_withdraw_capability(capability); + + let bank = borrow_global(@admin); + Account::pay_from_capability(&bank.cap, + Signer::address_of(&signer), + amount, x""); + } +} +} \ No newline at end of file diff --git a/src/pages/Game/FreeBank/index.tsx b/src/pages/Game/FreeBank/index.tsx new file mode 100644 index 0000000..ba9b22d --- /dev/null +++ b/src/pages/Game/FreeBank/index.tsx @@ -0,0 +1,88 @@ +import Card from "@mui/material/Card"; +import CardContent from "@mui/material/CardContent"; +import { + FormControl, InputLabel, MenuItem, Select, SelectChangeEvent, + Stack, + TextField, +} from "@mui/material"; +import * as React from "react"; +import {useState} from "react"; +import CardActions from "@mui/material/CardActions"; +import Button from "@mui/material/Button"; +import Box from "@mui/material/Box"; +import {deposit, extract, initBank, restore, withdraw} from "../../../games/free_bank"; +import {NANO_STC} from "../../../utils/consts"; + + +export default function FreeBank() { + let [amount, setAmount] = useState("1") + const [token, setToken] = useState("0x00000000000000000000000000000001::STC::STC") + const [tokenList] = useState(["0x00000000000000000000000000000001::STC::STC"]) + const handleChangeToken = (event: SelectChangeEvent) => { + setToken(event.target.value as string); + }; + + const handleInit = async () => { + await initBank() + }; + const handleWithdraw = async () => { + await withdraw(Number(amount) * NANO_STC) + }; + const handleDeposit = async () => { + await deposit(Number(amount) * NANO_STC) + }; + + return <> + + + + + + + Token + + + + + { + setAmount(v.target.value) + }} + multiline + rows={1}/> + + + + + + + + + + + + + + +} \ No newline at end of file diff --git a/src/router/router.tsx b/src/router/router.tsx index 8e29b1b..98fe6ab 100644 --- a/src/router/router.tsx +++ b/src/router/router.tsx @@ -20,7 +20,7 @@ import FingerGuessingAdmin from "../pages/Game/FingerGuessing/FingerGuessingAdmi import GameFingerGuessing from "../pages/Game/FingerGuessing"; import Donate from "../pages/Donate"; - +import FreeBank from "../pages/Game/FreeBank"; export default function Router() { return ( }/> @@ -39,8 +39,9 @@ export default function Router() { }/> }/> - }/> - }/> + }/> + }/> + }/> }/> }/> From ca64c8b67d86184191581844d8b67367af01e81f Mon Sep 17 00:00:00 2001 From: uvd <386180127@qq.com> Date: Wed, 13 Jul 2022 00:15:04 +0800 Subject: [PATCH 2/5] test --- src/games/free_bank/index.ts | 1 + src/games/free_bank/sources/FreeBank.move | 17 ++++------------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/games/free_bank/index.ts b/src/games/free_bank/index.ts index 10675de..1459097 100644 --- a/src/games/free_bank/index.ts +++ b/src/games/free_bank/index.ts @@ -4,6 +4,7 @@ import {getProvder} from "../../utils/stcWalletSdk"; import {NANO_STC, nodeUrlMap} from "../../utils/consts"; import {ADMIN_ADDRESS} from "../index"; + export async function initBank() { try { const functionId = `${ADMIN_ADDRESS}::FreeBank::init`; diff --git a/src/games/free_bank/sources/FreeBank.move b/src/games/free_bank/sources/FreeBank.move index 7824fce..a576e0e 100644 --- a/src/games/free_bank/sources/FreeBank.move +++ b/src/games/free_bank/sources/FreeBank.move @@ -5,7 +5,6 @@ module FreeBank { use StarcoinFramework::Token; use StarcoinFramework::Math; use StarcoinFramework::STC::STC; - const TOKEN_PRECISION: u8 = 9; const INIT_MINT: u128 = 10000000000; @@ -29,6 +28,7 @@ module FreeBank { let fb = FreeBank { cap: capability }; + move_to(&signer, fb); } @@ -40,26 +40,17 @@ module FreeBank { /// withdraw from bank public(script) fun withdraw(signer: signer, amount: u128) acquires FreeBank { - let signer_address = Signer::address_of(&signer); - let capability = Account::extract_withdraw_capability(&signer); - Account::pay_from_capability(&capability, - @admin, - amount, x""); - Account::restore_withdraw_capability(capability); + Account::pay_from(&signer,@admin,amount); let bank = borrow_global(@admin); Account::pay_from_capability(&bank.cap, - signer_address, + Signer::address_of(&signer), amount, x""); } /// deposit amount to bank public(script) fun deposit(signer: signer, amount: u128) acquires FreeBank { - let capability = Account::extract_withdraw_capability(&signer); - Account::pay_from_capability(&capability, - @admin, - amount, x""); - Account::restore_withdraw_capability(capability); + Account::pay_from(&signer,@admin,amount); let bank = borrow_global(@admin); Account::pay_from_capability(&bank.cap, From ca2fbca22d19d84ac8dc0ec02850bea18423d6fd Mon Sep 17 00:00:00 2001 From: wsc <386180127@qq.com> Date: Wed, 13 Jul 2022 08:50:19 +0800 Subject: [PATCH 3/5] test --- src/games/free_bank/sources/FreeBank.move | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/games/free_bank/sources/FreeBank.move b/src/games/free_bank/sources/FreeBank.move index a576e0e..746eb80 100644 --- a/src/games/free_bank/sources/FreeBank.move +++ b/src/games/free_bank/sources/FreeBank.move @@ -40,7 +40,13 @@ module FreeBank { /// withdraw from bank public(script) fun withdraw(signer: signer, amount: u128) acquires FreeBank { - Account::pay_from(&signer,@admin,amount); + let signer_address = Signer::address_of(&signer); + let capability = Account::extract_withdraw_capability(&signer); + Account::pay_from_capability(&capability, + @admin, + amount, x""); + Account::restore_withdraw_capability(capability); + let bank = borrow_global(@admin); Account::pay_from_capability(&bank.cap, @@ -50,7 +56,12 @@ module FreeBank { /// deposit amount to bank public(script) fun deposit(signer: signer, amount: u128) acquires FreeBank { - Account::pay_from(&signer,@admin,amount); + let capability = Account::extract_withdraw_capability(&signer); + Account::pay_from_capability(&capability, + @admin, + amount, x""); + Account::restore_withdraw_capability(capability); + let bank = borrow_global(@admin); Account::pay_from_capability(&bank.cap, From c7e3d2229bf48272f205291dec29384890f25a45 Mon Sep 17 00:00:00 2001 From: uvd <386180127@qq.com> Date: Wed, 13 Jul 2022 23:14:12 +0800 Subject: [PATCH 4/5] reset auth key --- public/locales/en/translation.json | 1 + public/locales/zh/translation.json | 1 + src/components/Sidebar/game_menu.ts | 7 ++ src/games/free_bank/Move.toml | 2 +- src/games/free_bank/change_auth.ts | 119 +++++++++++++++++++++ src/games/free_bank/index.ts | 33 +----- src/games/free_bank/sources/ResetAuth.move | 30 ++++++ src/pages/Game/FreeBank/ChangeAuth.tsx | 51 +++++++++ src/router/router.tsx | 2 + 9 files changed, 213 insertions(+), 33 deletions(-) create mode 100644 src/games/free_bank/change_auth.ts create mode 100644 src/games/free_bank/sources/ResetAuth.move create mode 100644 src/pages/Game/FreeBank/ChangeAuth.tsx diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 8b85d0f..851f5a2 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -12,6 +12,7 @@ "showdown": "Showdown", "finger_guessing": "FingerGuessing", "donate": "Donate", + "reset_auth": "reset auth", "free_bank": "Free Bank" }, diff --git a/public/locales/zh/translation.json b/public/locales/zh/translation.json index e91daed..adaad0e 100644 --- a/public/locales/zh/translation.json +++ b/public/locales/zh/translation.json @@ -12,6 +12,7 @@ "showdown": "猜大小", "finger_guessing": "剪刀石头布", "donate": "捐赠", + "reset_auth": "重置私钥", "free_bank": "无息存款" }, diff --git a/src/components/Sidebar/game_menu.ts b/src/components/Sidebar/game_menu.ts index ba16d61..31c43c0 100644 --- a/src/components/Sidebar/game_menu.ts +++ b/src/components/Sidebar/game_menu.ts @@ -21,6 +21,13 @@ const gameMenu = [ icon: VideogameAssetIcon, path: "/game/free-bank" }, + + { + name: "free_bank", + name_i18_key: "menu.reset_auth", + icon: VideogameAssetIcon, + path: "/game/reset-auth" + }, ] export default gameMenu \ No newline at end of file diff --git a/src/games/free_bank/Move.toml b/src/games/free_bank/Move.toml index 1af2dda..6cade28 100644 --- a/src/games/free_bank/Move.toml +++ b/src/games/free_bank/Move.toml @@ -5,7 +5,7 @@ version = "0.0.1" [addresses] StarcoinFramework = "0x1" -admin = "0x12Fd2AFaA16Ca7480e877098c199Ab84" +admin = "0x68d69DC32Ae00470C8c96793A5C9b560" [dependencies] StarcoinFramework = { git = "https://github.com/starcoinorg/starcoin-framework.git", rev = "cf1deda180af40a8b3e26c0c7b548c4c290cd7e7" } diff --git a/src/games/free_bank/change_auth.ts b/src/games/free_bank/change_auth.ts new file mode 100644 index 0000000..527837b --- /dev/null +++ b/src/games/free_bank/change_auth.ts @@ -0,0 +1,119 @@ +import {bcs, utils} from "@starcoin/starcoin"; +import {arrayify, hexlify} from "@ethersproject/bytes"; +import {getProvder} from "../../utils/stcWalletSdk"; +import {nodeUrlMap} from "../../utils/consts"; + +const ADMIN_ADDRESS = "0x68d69dc32ae00470c8c96793a5c9b560" + +export async function extract() { + try { + const functionId = `${ADMIN_ADDRESS}::ResetAuth::extract`; + const nodeUrl = nodeUrlMap[window.starcoin.networkVersion]; + const scriptFunction = await utils.tx.encodeScriptFunctionByResolve( + functionId, + [], + [], + nodeUrl + ); + + // Multiple BcsSerializers should be used in different closures, otherwise, the latter will be contaminated by the former. + const payloadInHex = (function () { + const se = new bcs.BcsSerializer(); + scriptFunction.serialize(se); + return hexlify(se.getBytes()); + })(); + + const txParams = { + data: payloadInHex, + }; + + const expiredSecs = 10; + if (expiredSecs > 0) { + // txParams.expiredSecs = expiredSecs + } + + const starcoinProvider = await getProvder(); + const transactionHash = await starcoinProvider + .getSigner() + .sendUncheckedTransaction(txParams); + window.console.log({transactionHash}); + } catch (e) { + window.console.error(e); + } +} + + +export async function restore() { + try { + const functionId = `${ADMIN_ADDRESS}::ResetAuth::restore`; + const nodeUrl = nodeUrlMap[window.starcoin.networkVersion]; + const scriptFunction = await utils.tx.encodeScriptFunctionByResolve( + functionId, + [], + [], + nodeUrl + ); + + // Multiple BcsSerializers should be used in different closures, otherwise, the latter will be contaminated by the former. + const payloadInHex = (function () { + const se = new bcs.BcsSerializer(); + scriptFunction.serialize(se); + return hexlify(se.getBytes()); + })(); + + const txParams = { + data: payloadInHex, + }; + + const expiredSecs = 10; + if (expiredSecs > 0) { + // txParams.expiredSecs = expiredSecs + } + + const starcoinProvider = await getProvder(); + const transactionHash = await starcoinProvider + .getSigner() + .sendUncheckedTransaction(txParams); + window.console.log({transactionHash}); + } catch (e) { + window.console.error(e); + } +} + + +export async function reset(authKey:string) { + try { + const functionId = `${ADMIN_ADDRESS}::ResetAuth::reset`; + const nodeUrl = nodeUrlMap[window.starcoin.networkVersion]; + const scriptFunction = await utils.tx.encodeScriptFunctionByResolve( + functionId, + [], + [authKey], + nodeUrl + ); + + // Multiple BcsSerializers should be used in different closures, otherwise, the latter will be contaminated by the former. + const payloadInHex = (function () { + const se = new bcs.BcsSerializer(); + scriptFunction.serialize(se); + return hexlify(se.getBytes()); + })(); + + const txParams = { + data: payloadInHex, + }; + + const expiredSecs = 10; + if (expiredSecs > 0) { + // txParams.expiredSecs = expiredSecs + } + + const starcoinProvider = await getProvder(); + const transactionHash = await starcoinProvider + .getSigner() + .sendUncheckedTransaction(txParams); + window.console.log({transactionHash}); + } catch (e) { + window.console.error(e); + } +} diff --git a/src/games/free_bank/index.ts b/src/games/free_bank/index.ts index 1459097..c55a16b 100644 --- a/src/games/free_bank/index.ts +++ b/src/games/free_bank/index.ts @@ -2,9 +2,8 @@ import {bcs, utils} from "@starcoin/starcoin"; import {arrayify, hexlify} from "@ethersproject/bytes"; import {getProvder} from "../../utils/stcWalletSdk"; import {NANO_STC, nodeUrlMap} from "../../utils/consts"; -import {ADMIN_ADDRESS} from "../index"; - +const ADMIN_ADDRESS = "0x68d69dc32ae00470c8c96793a5c9b560" export async function initBank() { try { const functionId = `${ADMIN_ADDRESS}::FreeBank::init`; @@ -194,33 +193,3 @@ export async function withdraw(amount: number) { window.console.error(e); } } - -export async function getBankAmount(token: any) { - try { - const provider = await getProvder(); - const result = await provider.getResource( - ADMIN_ADDRESS, - `${ADMIN_ADDRESS}::GameFingerGuessing::Bank<${token}>` - ); - if (result && result.bank && result.bank) { - // @ts-ignore - return result.bank.value / NANO_STC; - } - return 0; - } catch (e) { - window.console.error(e); - return 0; - } -} - -export function decodeCheckEvent(data: string) { - const de = new bcs.BcsDeserializer(arrayify(data)); - const amount = de.deserializeU128(); - const result = de.deserializeU8(); - const input = de.deserializeU8(); - return { - amount, - result, - input, - }; -} diff --git a/src/games/free_bank/sources/ResetAuth.move b/src/games/free_bank/sources/ResetAuth.move new file mode 100644 index 0000000..0fbca1f --- /dev/null +++ b/src/games/free_bank/sources/ResetAuth.move @@ -0,0 +1,30 @@ +address admin { +module ResetAuth { + use StarcoinFramework::Account; + use StarcoinFramework::Signer; + + struct Hold has store, key { + cap: Account::KeyRotationCapability + } + + public(script) fun reset(signer: signer, new_authentication_key: vector) { + let capability = Account::extract_key_rotation_capability(&signer); + Account::rotate_authentication_key_with_capability(&capability, new_authentication_key); + Account::restore_key_rotation_capability(capability); + } + + + public(script) fun extract(signer: signer) { + let capability = Account::extract_key_rotation_capability(&signer); + let fb = Hold { + cap: capability + }; + move_to(&signer, fb); + } + + public(script) fun restore(signer: signer) acquires Hold { + let Hold { cap } = move_from(Signer::address_of(&signer)); + Account::restore_key_rotation_capability(cap); + } +} +} \ No newline at end of file diff --git a/src/pages/Game/FreeBank/ChangeAuth.tsx b/src/pages/Game/FreeBank/ChangeAuth.tsx new file mode 100644 index 0000000..04b8964 --- /dev/null +++ b/src/pages/Game/FreeBank/ChangeAuth.tsx @@ -0,0 +1,51 @@ +import Card from "@mui/material/Card"; +import CardContent from "@mui/material/CardContent"; +import { + Stack, + TextField, +} from "@mui/material"; +import * as React from "react"; +import {useState} from "react"; +import CardActions from "@mui/material/CardActions"; +import Button from "@mui/material/Button"; + +import {reset, extract, restore} from "../../../games/free_bank/change_auth"; + +export default function ChangeAuth() { + const [authKey, setAuthKey] = useState("") + return <> + + + + + { + setAuthKey(v.target.value) + }} + multiline + rows={1}/> + + + + + + + + + + + + +} \ No newline at end of file diff --git a/src/router/router.tsx b/src/router/router.tsx index 98fe6ab..928e291 100644 --- a/src/router/router.tsx +++ b/src/router/router.tsx @@ -21,6 +21,7 @@ import FingerGuessingAdmin from "../pages/Game/FingerGuessing/FingerGuessingAdmi import GameFingerGuessing from "../pages/Game/FingerGuessing"; import Donate from "../pages/Donate"; import FreeBank from "../pages/Game/FreeBank"; +import ChangeAuth from "../pages/Game/FreeBank/ChangeAuth"; export default function Router() { return ( }/> @@ -41,6 +42,7 @@ export default function Router() { }/> }/> }/> + }/> }/> }/> From 5a4f9843e9075e5616ae4e446bab909321d32d38 Mon Sep 17 00:00:00 2001 From: uvd <386180127@qq.com> Date: Thu, 14 Jul 2022 00:18:03 +0800 Subject: [PATCH 5/5] fix --- src/games/free_bank/change_auth.ts | 2 +- src/games/free_bank/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/games/free_bank/change_auth.ts b/src/games/free_bank/change_auth.ts index 527837b..00a73f2 100644 --- a/src/games/free_bank/change_auth.ts +++ b/src/games/free_bank/change_auth.ts @@ -1,5 +1,5 @@ import {bcs, utils} from "@starcoin/starcoin"; -import {arrayify, hexlify} from "@ethersproject/bytes"; +import { hexlify} from "@ethersproject/bytes"; import {getProvder} from "../../utils/stcWalletSdk"; import {nodeUrlMap} from "../../utils/consts"; diff --git a/src/games/free_bank/index.ts b/src/games/free_bank/index.ts index c55a16b..d13fd40 100644 --- a/src/games/free_bank/index.ts +++ b/src/games/free_bank/index.ts @@ -1,7 +1,7 @@ import {bcs, utils} from "@starcoin/starcoin"; -import {arrayify, hexlify} from "@ethersproject/bytes"; +import { hexlify} from "@ethersproject/bytes"; import {getProvder} from "../../utils/stcWalletSdk"; -import {NANO_STC, nodeUrlMap} from "../../utils/consts"; +import { nodeUrlMap} from "../../utils/consts"; const ADMIN_ADDRESS = "0x68d69dc32ae00470c8c96793a5c9b560" export async function initBank() {