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) +}