import { ethers } from 'ethers';
import ERC20ABI from './ERC20ABI.json';
// 发起交易
export const EtherPay = async (to: string, price: number) => {
  try {
    let eth: any = window;
    // 代币地址
    const USDT = '0xc5a25e92e691635BDd6DF2e904633Dc3152360cD';
    const provider = new ethers.providers.Web3Provider(eth.ethereum);
    const signer = new ethers.providers.Web3Provider(eth.ethereum).getSigner();
    const tokenContract = new ethers.Contract(USDT, ERC20ABI, provider);
    // const tokenBalance = await tokenContract.balanceOf(
    //   eth.ethereum.selectedAddress
    // );
    // const tokenAmount = ethers.utils.parseUnits(
    //   ethers.utils.formatUnits(price),
    //   18
    // );
    let tokenAmount: any = price * Math.pow(10, 18);
    tokenAmount = BigInt(Math.floor(tokenAmount));
    const tokenSigner = tokenContract.connect(signer);
    const transaction = await tokenSigner.transfer(to, tokenAmount);
    return transaction.hash ? transaction.hash : '';
  } catch (e) {
    console.log(e);
    return '';
  }
};
