234 lines
6.8 KiB
JavaScript
234 lines
6.8 KiB
JavaScript
!(function (EXPORTS) {
|
|
if (!window.ethers) return console.error("ethers.js not found");
|
|
const maticOperator = EXPORTS,
|
|
isValidAddress = (maticOperator.isValidAddress = (address) => {
|
|
try {
|
|
const isValidChecksum = ethers.utils.isAddress(address),
|
|
isValidNonChecksum =
|
|
ethers.utils.getAddress(address) === address.toLowerCase();
|
|
return isValidChecksum || isValidNonChecksum;
|
|
} catch (error) {
|
|
return !1;
|
|
}
|
|
}),
|
|
ERC20ABI = [
|
|
{
|
|
constant: !0,
|
|
inputs: [],
|
|
name: "name",
|
|
outputs: [{ name: "", type: "string" }],
|
|
payable: !1,
|
|
stateMutability: "view",
|
|
type: "function",
|
|
},
|
|
{
|
|
constant: !1,
|
|
inputs: [
|
|
{ name: "_spender", type: "address" },
|
|
{ name: "_value", type: "uint256" },
|
|
],
|
|
name: "approve",
|
|
outputs: [{ name: "", type: "bool" }],
|
|
payable: !1,
|
|
stateMutability: "nonpayable",
|
|
type: "function",
|
|
},
|
|
{
|
|
constant: !0,
|
|
inputs: [],
|
|
name: "totalSupply",
|
|
outputs: [{ name: "", type: "uint256" }],
|
|
payable: !1,
|
|
stateMutability: "view",
|
|
type: "function",
|
|
},
|
|
{
|
|
constant: !1,
|
|
inputs: [
|
|
{ name: "_from", type: "address" },
|
|
{ name: "_to", type: "address" },
|
|
{ name: "_value", type: "uint256" },
|
|
],
|
|
name: "transferFrom",
|
|
outputs: [{ name: "", type: "bool" }],
|
|
payable: !1,
|
|
stateMutability: "nonpayable",
|
|
type: "function",
|
|
},
|
|
{
|
|
constant: !0,
|
|
inputs: [],
|
|
name: "decimals",
|
|
outputs: [{ name: "", type: "uint8" }],
|
|
payable: !1,
|
|
stateMutability: "view",
|
|
type: "function",
|
|
},
|
|
{
|
|
constant: !0,
|
|
inputs: [{ name: "_owner", type: "address" }],
|
|
name: "balanceOf",
|
|
outputs: [{ name: "balance", type: "uint256" }],
|
|
payable: !1,
|
|
stateMutability: "view",
|
|
type: "function",
|
|
},
|
|
{
|
|
constant: !0,
|
|
inputs: [],
|
|
name: "symbol",
|
|
outputs: [{ name: "", type: "string" }],
|
|
payable: !1,
|
|
stateMutability: "view",
|
|
type: "function",
|
|
},
|
|
{
|
|
constant: !1,
|
|
inputs: [
|
|
{ name: "_to", type: "address" },
|
|
{ name: "_value", type: "uint256" },
|
|
],
|
|
name: "transfer",
|
|
outputs: [{ name: "", type: "bool" }],
|
|
payable: !1,
|
|
stateMutability: "nonpayable",
|
|
type: "function",
|
|
},
|
|
{
|
|
constant: !0,
|
|
inputs: [
|
|
{ name: "_owner", type: "address" },
|
|
{ name: "_spender", type: "address" },
|
|
],
|
|
name: "allowance",
|
|
outputs: [{ name: "", type: "uint256" }],
|
|
payable: !1,
|
|
stateMutability: "view",
|
|
type: "function",
|
|
},
|
|
{ payable: !0, stateMutability: "payable", type: "fallback" },
|
|
{
|
|
anonymous: !1,
|
|
inputs: [
|
|
{ indexed: !0, name: "owner", type: "address" },
|
|
{ indexed: !0, name: "spender", type: "address" },
|
|
{ indexed: !1, name: "value", type: "uint256" },
|
|
],
|
|
name: "Approval",
|
|
type: "event",
|
|
},
|
|
{
|
|
anonymous: !1,
|
|
inputs: [
|
|
{ indexed: !0, name: "from", type: "address" },
|
|
{ indexed: !0, name: "to", type: "address" },
|
|
{ indexed: !1, name: "value", type: "uint256" },
|
|
],
|
|
name: "Transfer",
|
|
type: "event",
|
|
},
|
|
]
|
|
const CONTRACT_ADDRESSES = {
|
|
usdc: "0x3c499c542cef5e3811e1192ce70d8cc03d5c3359",
|
|
usdt: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f"
|
|
}
|
|
function getProvider() {
|
|
// switches provider based on whether the user is using MetaMask or not
|
|
if (window.ethereum) {
|
|
return new ethers.providers.Web3Provider(window.ethereum);
|
|
} else {
|
|
return new ethers.providers.JsonRpcProvider(`https://polygon-mainnet.infura.io/v3/804ecc59729248fc9cf5b3713289a321`)
|
|
}
|
|
}
|
|
(maticOperator.getBalance = async (address) => {
|
|
try {
|
|
if (!address || !isValidAddress(address))
|
|
return new Error("Invalid address");
|
|
const provider = getProvider(),
|
|
balanceWei = await provider.getBalance(address);
|
|
return parseFloat(ethers.utils.formatEther(balanceWei));
|
|
} catch (error) {
|
|
return console.error("Error:", error.message), error;
|
|
}
|
|
}),
|
|
(maticOperator.getTokenBalance = async (
|
|
address,
|
|
token,
|
|
{ contractAddress: contractAddress } = {}
|
|
) => {
|
|
try {
|
|
if (!token) return new Error("Token not specified");
|
|
if (!CONTRACT_ADDRESSES[token] && contractAddress)
|
|
return new Error("Contract address of token not available");
|
|
const usdcContract = new ethers.Contract(
|
|
CONTRACT_ADDRESSES[token] || contractAddress,
|
|
ERC20ABI,
|
|
getProvider()
|
|
);
|
|
let balance = await usdcContract.balanceOf(address);
|
|
return (
|
|
(balance = parseFloat(ethers.utils.formatUnits(balance, 6))), balance
|
|
);
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
});
|
|
const estimateGas = (maticOperator.estimateGas = async ({
|
|
privateKey: privateKey,
|
|
receiver: receiver,
|
|
amount: amount,
|
|
}) => {
|
|
try {
|
|
const provider = getProvider(),
|
|
signer = new ethers.Wallet(privateKey, provider);
|
|
return provider.estimateGas({
|
|
from: signer.address,
|
|
to: receiver,
|
|
value: ethers.utils.parseUnits(amount, "ether"),
|
|
});
|
|
} catch (e) {
|
|
throw new Error(e);
|
|
}
|
|
});
|
|
(maticOperator.sendTransaction = async ({
|
|
privateKey: privateKey,
|
|
receiver: receiver,
|
|
amount: amount,
|
|
}) => {
|
|
try {
|
|
const provider = getProvider(),
|
|
signer = new ethers.Wallet(privateKey, provider),
|
|
limit = await estimateGas({
|
|
privateKey: privateKey,
|
|
receiver: receiver,
|
|
amount: amount,
|
|
});
|
|
return signer.sendTransaction({
|
|
to: receiver,
|
|
value: ethers.utils.parseUnits(amount, "ether"),
|
|
gasLimit: limit,
|
|
nonce: signer.getTransactionCount(),
|
|
maxPriorityFeePerGas: ethers.utils.parseUnits("2", "gwei"),
|
|
});
|
|
} catch (e) {
|
|
throw new Error(e);
|
|
}
|
|
}),
|
|
(maticOperator.sendToken = async ({
|
|
token: token,
|
|
privateKey: privateKey,
|
|
amount: amount,
|
|
receiver: receiver,
|
|
contractAddress: contractAddress,
|
|
}) => {
|
|
const wallet = new ethers.Wallet(privateKey, getProvider()),
|
|
tokenContract = new ethers.Contract(
|
|
CONTRACT_ADDRESSES[token] || contractAddress,
|
|
ERC20ABI,
|
|
wallet
|
|
),
|
|
amountWei = ethers.utils.parseUnits(amount.toString(), 6);
|
|
return tokenContract.transfer(receiver, amountWei);
|
|
});
|
|
})("object" == typeof module ? module.exports : (window.maticOperator = {}));
|