MInt/src/wallet/index.js
2024-03-09 03:03:03 +08:00

314 lines
8.3 KiB
JavaScript

import Web3 from 'web3'
import axios from 'axios'
import BEB_abi from './../abi/BEBadgeV2.json'
import NFT_abi from './../abi/NFTClaimer.json'
var abis = {
"BEB": BEB_abi,
"NFT": NFT_abi,
}
var BEBAddress = process.env.VUE_APP_BEB_URL
var NFTAddress = process.env.VUE_APP_NFT_URL
var chainId = process.env.VUE_APP_TOKENID_ID
var connectUrl = process.env.VUE_APP_CONFIG_URL
// Wallet Connect初始化
import { EthereumProvider } from '@walletconnect/ethereum-provider'
const provider = await EthereumProvider.init({
projectId: 'e7743d46923911fa8850619b7a7f6d9d', // required
chains: ['1'], // required
showQrModal: true, // requires @walletconnect/modal
optionalChains: ['1'],
// optionalMethods: ['eth_signTypedData', 'eth_signTypedData_v4'],
metadata: {
name: 'Counter Fire',
description: 'Counter Fire',
url: 'https://mint.counterfire.games/',
icons: ['https://www.counterfire.games/favicon.ico'],
},
});
// 链接Wallet Connect钱包
export async function getConnect() {
await provider.connect()
let result = await provider.request({ method: 'eth_requestAccounts' })
return result
}
// 断开链接
export async function disconnectLink(name) {
if(name == 'ethereum') {
await window.ethereum.request({
"method": "wallet_revokePermissions",
"params": [
{
"eth_accounts": {}
}
]
});
} else if(name == 'okxwallet') {
await okxwallet.request({
"method": "wallet_disconnect",
})
} else if(name == 'connect') {
await provider.request({method: 'eth_requestDisconnect'})
}
}
// 获取钱包地址
export async function getWalletAddress() {
const web3js = window.ethereum
const web3 = await web3js.request({
method: 'eth_accounts'
})
if (web3 !== 'undefined') {
if (web3[0] == '') {
return false
} else {
return await web3[0]
}
}
}
// 查看钱包是否链接
export async function isWalletConnected() {
const ethereum = window.web3;
if (typeof ethereum !== 'undefined') {
let res = await ethereum.request({ method: "eth_accounts" })
// await okxwallet.request({ method: "eth_accounts" });
// console.log(res,'279-----')
if(res[0] == '') {
return false
} else {
return true
}
}
return false
}
// 检查是否有arbitrum-sepolia网络
export async function isSepoliNetwork(name) {
let res
if(name == 'ethereum') {
res = await window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [
{
chainId: chainId
}
]
});
return false
} else if(name == 'okxwallet') {
res =
await window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [
{
chainId: chainId
}
]
});
return false
} else if(name == 'connect') {
return false
}
}
// 添加arbitrum-sepolia网络
export async function addNetwork(name) {
if(name == 'ethereum') {
try{
let res = await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: '0xaa36a7',
chainName: 'Sepolia Testnet Explorer',
rpcUrls: ['https://sepolia.infura.io/v3'],
iconUrls: ['https://xdaichain.com/fake/example/url/xdai.png'],
blockExplorerUrls: ['https://sepolia.etherscan.io'],
nativeCurrency: {
name: "xDAI",
symbol: "xDAI",
decimals: 18
},
},]
})
return await res
} catch(e) {
return false
}
} else if(name == 'okxwallet') {
try{
let res = await okxwallet.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: '11155111',
chainName: 'Sepolia Testnet Explorer',
rpcUrls: ['https://sepolia.infura.io/v3'],
iconUrls: ['https://xdaichain.com/fake/example/url/xdai.png'],
blockExplorerUrls: ['https://sepolia.etherscan.io'],
nativeCurrency: {
name: "xDAI",
symbol: "xDAI",
decimals: 18
},
}]
})
return await res
} catch(e) {
return false
}
} else if(name == 'connect') {
try{
let res = await window.provider.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: '0xaa36a7',
chainName: 'Sepolia Testnet Explorer',
rpcUrls: ['https://sepolia.infura.io/v3'],
iconUrls: ['https://xdaichain.com/fake/example/url/xdai.png'],
blockExplorerUrls: ['https://sepolia.etherscan.io'],
nativeCurrency: {
name: "xDAI",
symbol: "xDAI",
decimals: 18
},
}]
})
return await res
} catch(e) {
return false
}
}
}
// 获取总数
export async function getSupplyLimit(name,address) {
let web3
if(name == 'ethereum') {
web3 = new Web3(window.ethereum);
} else if(name == 'okxwallet') {
web3 = new Web3(window.okxwallet)
} else if(name == 'connect') {
web3 = new Web3(provider)
}
let contract = new web3.eth.Contract(abis['BEB'].abi, BEBAddress, { from: address})
let time = await contract.methods.supplyLimit().call()
return Number(time)
}
// 获取已Mint数量
export async function getTotalLimit(name,address) {
let web3
if(name == 'ethereum') {
web3 = new Web3(window.ethereum);
} else if(name == 'okxwallet') {
web3 = new Web3(okxwallet)
} else if(name == 'connect') {
web3 = new Web3(provider)
}
let contract = new web3.eth.Contract(abis['BEB'].abi, BEBAddress, { from: address})
let time = await contract.methods.totalSupply().call()
return Number(time)
}
// 开始时间
export async function getStartTime(name,address) {
let web3
if(name == 'ethereum') {
web3 = new Web3(window.ethereum);
} else if(name == 'okxwallet') {
web3 = new Web3(okxwallet)
} else if(name == 'connect') {
web3 = new Web3(provider)
}
let contract = new web3.eth.Contract(abis['NFT'].abi, NFTAddress, { from: address})
let startTime = await contract.methods.startTime().call()
return Number(startTime)
}
// 结束时间
export async function getEndTime(name,address) {
let web3
if(name == 'ethereum') {
web3 = new Web3(window.ethereum);
} else if(name == 'okxwallet') {
web3 = new Web3(okxwallet)
} else if(name == 'connect') {
web3 = new Web3(provider)
}
let contract = new web3.eth.Contract(abis['NFT'].abi, NFTAddress, { from: address})
let endTime = await contract.methods.endTime().call()
return Number(endTime)
}
// 开启NFT
export async function testClaim(name,data,address) {
let web3
if(name == 'ethereum') {
web3 = new Web3(window.ethereum);
} else if(name == 'okxwallet') {
web3 = new Web3(okxwallet)
} else if(name == 'connect') {
web3 = new Web3(provider)
}
const gasPrice = await web3.eth.getGasPrice();
let contract = new web3.eth.Contract(abis['NFT'].abi, NFTAddress, { from: address})
let gas = await contract.methods.claim(...data).estimateGas();
gas = (Number(gas) * 1) | 0;
const receipt = await contract.methods.claim(...data).send({ gas, gasPrice });
return receipt
}
// 查看已Mint的tokenId
export async function inquireClaimHistory(name, address) {
let web3
if(name == 'ethereum') {
web3 = new Web3(window.ethereum);
} else if(name == 'okxwallet') {
web3 = new Web3(okxwallet)
} else if(name == 'connect') {
web3 = new Web3(provider)
}
let contract = new web3.eth.Contract(abis['NFT'].abi, NFTAddress, { from: address})
const tokenId = await contract.methods.claimHistory(address).call();
return tokenId
}
// 初始化数据
export async function makeBatchRequestList(name,address) {
const [supplyLimit, totalLimit, startTime, endTime] = [
await getSupplyLimit(name,address),
await getTotalLimit(name,address),
await getStartTime(name,address),
await getEndTime(name,address)
]
return [supplyLimit, totalLimit, startTime, endTime]
}
// 获取数据
export async function getReqConnect(dataID, contract, address) {
const data = {
id: (Date.now() / 1000) | 0,
jsonrpc: '2.0',
method: 'eth_call',
params: [
{
data: dataID,
from: address,
to: contract,
},
'latest',
]
}
let res = await axios.post(connectUrl, data)
if(res.data.result) {
return res.data.result
} else {
return
}
}