增加游戏内资产上链方法

This commit is contained in:
CounterFire2023 2024-07-16 15:29:42 +08:00
parent c79c6223b4
commit 0d042d5189
4 changed files with 50 additions and 7 deletions

View File

@ -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);
}
}

View File

@ -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
}
}
// 游戏内资产上链, 用于解锁或铸造
// 创世英雄, 普通英雄, 金砖
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
}
}

View File

@ -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'
}
]

View File

@ -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, {})
}
}
export const apiUnlockOrMint = async (data) => {
const url = `${API_BASE}/api/nft/stacking/unlock`
return httpPost(url, data)
}