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 6b74694..ca486a7 100644 --- a/src/utils/marketplace.js +++ b/src/utils/marketplace.js @@ -177,3 +177,9 @@ export const apiRechargeHistory = async (account_address) => { const url = `${API_BASE}/api/recharge/history/${net_id}/${account_address}` return httpPost(url, {}) } + +// 充值-购买 +export const apiPreRecharge = async (data) => { + const url = `${API_BASE}/api/recharge/buy` + return httpPost(url, data) +}