From 11ac3f4957c52bbbf0520d22b0373120d0ad1214 Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Thu, 1 Aug 2024 13:51:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B4=AD=E4=B9=B0=E7=89=A9?= =?UTF-8?q?=E5=93=81=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/chain/BlockChain.js | 2 + src/components/chain/contract/GameItemMall.js | 50 +++++++++++++++++++ src/utils/marketplace.js | 6 +++ 3 files changed, 58 insertions(+) create mode 100644 src/components/chain/contract/GameItemMall.js diff --git a/src/components/chain/BlockChain.js b/src/components/chain/BlockChain.js index 090dc4e..78b9f32 100644 --- a/src/components/chain/BlockChain.js +++ b/src/components/chain/BlockChain.js @@ -8,6 +8,7 @@ import {isTokenExpired, genRefreshToken, cfgChainId, switchEthereumChain} from " import {ImtblMarket} from "@/components/chain/Market"; import { ALL_PROVIDERS } from "@/configs/configchain"; import {Locker} from "@/components/chain/contract/Locker"; +import {GameItemMall} from "@/components/chain/contract/GameItemMall"; import ConfirmDialog from "@/components/Dialogs/confirmDialog.vue"; import { Widgets } from "./Widgets"; @@ -28,6 +29,7 @@ export class BlockChain { this.initWallet(); this.market = new ImtblMarket(this) this.locker = new Locker(this) + this.mall = new GameItemMall(this) this.widgets = new Widgets(this) BlockChain.instance = this; diff --git a/src/components/chain/contract/GameItemMall.js b/src/components/chain/contract/GameItemMall.js new file mode 100644 index 0000000..1c3759b --- /dev/null +++ b/src/components/chain/contract/GameItemMall.js @@ -0,0 +1,50 @@ +import { apiPreRecharge } from '@/utils/marketplace' + +export class GameItemMall { + constructor(_chainInstance) { + this.bc = _chainInstance + } + + async sendTransaction(provider, {from, to, data}) { + const txHash = await provider.provider.request({ + method: 'eth_sendTransaction', + params: [{ + from, + to, + data + }] + }) + console.log(txHash) + const res = await provider.waitForTransaction(txHash) + if (res.status == 0) { + throw new Error('transaction failed') + } + return txHash + } + + async execBuyItem(itemId) { + await this.bc.checkPassportLogin(); + const chainId = parseInt(import.meta.env.VUE_APP_NET_ID); + const { provider, address } = await this.bc.selectAddress({targetChainId: chainId}) + const preDatas = { + net_id: address, + goods_id: itemId, + account_address: address, + } + const { errocode, errmsg, calls } = await apiPreRecharge(preDatas) + if (errocode) { + throw new Error(errmsg) + } + if (!calls || calls.length == 0) { + throw new Error('no calls') + } + let resutls = [] + for (let i = 0; i < calls.length; i++) { + const { to, data } = calls[i].trans_req + let hash = await this.sendTransaction(provider, {from: address, to, data}) + resutls.push(hash) + } + return resutls + } + +} diff --git a/src/utils/marketplace.js b/src/utils/marketplace.js index 0e12314..0adb1f1 100644 --- a/src/utils/marketplace.js +++ b/src/utils/marketplace.js @@ -165,3 +165,9 @@ export const apiContribution = async (account_address) => { const url = `${API_BASE}/api/activity/stacking/history/${account_address}` return httpGet(url, {}) } + +// 充值-购买 +export const apiPreRecharge = async (data) => { + const url = `${API_BASE}/api/recharge/buy` + return httpPost(url, data) +}