From 0d042d518953ddd4daddd0b41300addf3cfe1acf Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Tue, 16 Jul 2024 15:29:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B8=B8=E6=88=8F?= =?UTF-8?q?=E5=86=85=E8=B5=84=E4=BA=A7=E4=B8=8A=E9=93=BE=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 | 9 ++++--- src/components/chain/contract/Locker.js | 33 +++++++++++++++++++++++-- src/configs/allchain.ts | 8 ++++++ src/utils/marketplace.js | 7 +++++- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/components/chain/BlockChain.js b/src/components/chain/BlockChain.js index d87c6dc..db1942b 100644 --- a/src/components/chain/BlockChain.js +++ b/src/components/chain/BlockChain.js @@ -148,11 +148,12 @@ export class BlockChain { /** * 检查并切换到目标链, 各上链前须调用该方法 */ - async checkAndChangeChain() { + async checkAndChangeChain(targetChainId) { + targetChainId = targetChainId || cfgChainId; let chainId = await this.getChainId(); - if (chainId !== cfgChainId) { - console.log(`current chain: ${chainId}, want: ${cfgChainId}`) - chainId = await switchEthereumChain(this.web3Provider.provider, cfgChainId); + if (chainId !== targetChainId) { + console.log(`current chain: ${chainId}, want: ${targetChainId}`) + chainId = await switchEthereumChain(this.web3Provider.provider, targetChainId); } } diff --git a/src/components/chain/contract/Locker.js b/src/components/chain/contract/Locker.js index f334a6e..8f7061f 100644 --- a/src/components/chain/contract/Locker.js +++ b/src/components/chain/contract/Locker.js @@ -1,8 +1,10 @@ import { ethers } from 'ethers' +import { apiUnlockOrMint } from '@/utils/marketplace' const lockAbi = [ - 'function lock(address nft, address to, uint256[] tokenIds) external' + 'function lock(address nft, address to, uint256[] tokenIds) external', + 'function unlockOrMint(address nft, tuple[] nftList, uint256 signTime, uint256 saltNonce, bytes signature) external' ] const erc721Abi = [ @@ -40,4 +42,31 @@ export class Locker { await this.bc.web3Provider.waitForTransaction(res.hash) return res.hash } -} \ No newline at end of file + // 游戏内资产上链, 用于解锁或铸造 + // 创世英雄, 普通英雄, 金砖 + async unlockOrMintGameNft(nft, tokenIds) { + console.log('unlock nft', nft, tokenIds) + await this.bc.checkPassportLogin(); + await this.bc.checkAndChangeChain(); + const preDatas = { + net_id: import.meta.env.VUE_APP_NET_ID, + contract_address: nft, + tokens: tokenIds.map(tokenId => {return { tokenId }}), + } + const { errcode, errmsg, trans_req } = await apiUnlockOrMint(preDatas) + if (errcode) { + throw new Error(errmsg) + } + const { to, data } = trans_req + const web3Provider = this.bc.passportProvider || this.bc.web3Provider + const txHash = await web3Provider.request({ + method: 'eth_sendTransaction', + params: [{ + to, + data + }] + }) + console.log(txHash) + return txHash + } +} diff --git a/src/configs/allchain.ts b/src/configs/allchain.ts index 08cad38..5303e0c 100644 --- a/src/configs/allchain.ts +++ b/src/configs/allchain.ts @@ -298,5 +298,13 @@ export const AllChains = [ id: 13473, symbol: 'tIMX', explorerurl: 'https://explorer.testnet.immutable.com' + }, + { + name: 'Sepolia Testnet', + type: 'Testnet', + rpc: 'https://rpc.sepolia.org', + id: 11155111, + symbol: 'ETH', + explorerurl: 'https://sepolia.etherscan.io' } ] diff --git a/src/utils/marketplace.js b/src/utils/marketplace.js index 4ef3c3f..edc9251 100644 --- a/src/utils/marketplace.js +++ b/src/utils/marketplace.js @@ -104,4 +104,9 @@ export const apiClearCartList = async () => { export const nftDetail = async(address, tokenId) => { const url = `${API_BASE}/api/market/product/goods/${net_id}/${address}/${tokenId}` return httpGet(url, {}) -} \ No newline at end of file +} + +export const apiUnlockOrMint = async (data) => { + const url = `${API_BASE}/api/nft/stacking/unlock` + return httpPost(url, data) +} From 467ba45f59969702439744ee42c8aae6d375e698 Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Wed, 17 Jul 2024 11:43:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B0=86=E8=8B=B1=E9=9B=84=E4=B8=8A?= =?UTF-8?q?=E9=93=BE=E6=96=B9=E6=B3=95=E7=8B=AC=E7=AB=8B=E5=87=BA=E6=9D=A5?= =?UTF-8?q?,=20=E5=A2=9E=E5=8A=A0=E5=8F=AF=E6=8C=87=E5=AE=9A=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E5=9C=B0=E5=9D=80=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/chain/BlockChain.js | 36 +++++++++++++ src/components/chain/WalletSelectModel.vue | 26 +++++++--- src/components/chain/contract/Locker.js | 59 +++++++++++++++------- src/utils/marketplace.js | 12 ++--- 4 files changed, 103 insertions(+), 30 deletions(-) diff --git a/src/components/chain/BlockChain.js b/src/components/chain/BlockChain.js index db1942b..174442a 100644 --- a/src/components/chain/BlockChain.js +++ b/src/components/chain/BlockChain.js @@ -132,6 +132,20 @@ export class BlockChain { return token+suffix } + /** + * 如果是用passport登录的, 直接返回store中的token, 否则调用passport的getAccessToken + * @returns + */ + async passportToken() { + if (this.store.walletType == 3) { + return this.store.token + } + if (!this.passportProvider) { + return '' + } + const res = await new PassportWallet().getAccessToken(); + return res.token + } async logout() { if (this.store.walletType != 3 && this.passportProvider) { @@ -176,4 +190,26 @@ export class BlockChain { await this.appendPassport(); } } + + async selectAddress({title, subTitle, targetChainId}) { + const initData = {} + initData[this.store.walletType] = this.store.address + const rewardModal = createModal(WalletSelectModel, { + title: title || 'Select Address', + message: subTitle || 'Please select the address you want to use', + initData: { 1: 'MetaMask', 2: 'OKX Wallet', 3: 'Passport' } + }); + const { errcode, errmsg, walletInstance, provider, accounts } = await rewardModal.show(); + if (errcode) { + console.log(`select address result : ${errmsg}`); + throw new Error(errmsg); + } + targetChainId = targetChainId || cfgChainId; + let chainId = await walletInstance.getChainId(); + if (chainId !== targetChainId) { + console.log(`current chain: ${chainId}, want: ${targetChainId}`) + chainId = await switchEthereumChain(provider.provider, targetChainId); + } + return { provider, address: accounts[0] }; + } } diff --git a/src/components/chain/WalletSelectModel.vue b/src/components/chain/WalletSelectModel.vue index 6eddbfc..e0b7874 100644 --- a/src/components/chain/WalletSelectModel.vue +++ b/src/components/chain/WalletSelectModel.vue @@ -3,7 +3,7 @@