140 lines
3.8 KiB
TypeScript
140 lines
3.8 KiB
TypeScript
// import Web3 from "web3";
|
|
import Web3 from "web3/dist/web3.min.js";
|
|
import EventBus from "./eventBus";
|
|
import usdtAbi from "./abi/usdtAbi";
|
|
import nftAbi from "./abi/nftAbi";
|
|
|
|
declare global {
|
|
interface Window {
|
|
ethereum?: any; //MetaMaskInpageProvider
|
|
}
|
|
}
|
|
|
|
const web3 = new Web3(Web3.givenProvider);
|
|
function parseSendReturn(sendReturn: any): any {
|
|
return sendReturn.hasOwnProperty("result") ? sendReturn.result : sendReturn;
|
|
}
|
|
|
|
export class NoEthereumProviderError extends Error {
|
|
public constructor() {
|
|
super();
|
|
this.name = this.constructor.name;
|
|
this.message = "No Ethereum provider was found on window.ethereum.";
|
|
}
|
|
}
|
|
|
|
export class TBG extends EventBus {
|
|
constructor(params) {
|
|
super();
|
|
this.handleAddListener();
|
|
}
|
|
|
|
private handleAddListener() {
|
|
if (window.ethereum.on) {
|
|
window.ethereum.on("chainChanged", this.handleChainChanged.bind(this));
|
|
window.ethereum.on(
|
|
"accountsChanged",
|
|
this.handleAccountsChanged.bind(this)
|
|
);
|
|
window.ethereum.on("close", this.handleClose.bind(this));
|
|
window.ethereum.on(
|
|
"networkChanged",
|
|
this.handleNetworkChanged.bind(this)
|
|
);
|
|
}
|
|
}
|
|
private handleNetworkChanged() {
|
|
this.emit("networkChanged");
|
|
}
|
|
|
|
private handleChainChanged() {
|
|
this.emit("chainChanged");
|
|
}
|
|
|
|
private handleAccountsChanged(accounts: string[]): void {
|
|
this.emit("accountsChanged", accounts);
|
|
}
|
|
private handleClose() {
|
|
this.emit("close");
|
|
}
|
|
async login() {
|
|
if (!window.ethereum) {
|
|
throw new NoEthereumProviderError();
|
|
}
|
|
if (window.ethereum.isMetaMask) {
|
|
window.ethereum.autoRefreshOnNetworkChange = false;
|
|
}
|
|
let account;
|
|
try {
|
|
account = await window.ethereum
|
|
.request({ method: "eth_requestAccounts" })
|
|
.then((sendReturn) => parseSendReturn(sendReturn)[0]);
|
|
return account;
|
|
} catch (error) {
|
|
if (error.code === 4001) {
|
|
// throw new UserRejectedRequestError()
|
|
}
|
|
}
|
|
// if unsuccessful, try enable
|
|
if (!account) {
|
|
// if enable is successful but doesn't return accounts, fall back to getAccount (not happy i have to do this...)
|
|
account = await window.ethereum
|
|
.enable()
|
|
.then((sendReturn) => sendReturn && parseSendReturn(sendReturn)[0]);
|
|
return account;
|
|
}
|
|
}
|
|
|
|
logout() {
|
|
if (window.ethereum && window.ethereum.removeListener) {
|
|
window.ethereum.removeListener("chainChanged", this.handleChainChanged);
|
|
window.ethereum.removeListener(
|
|
"accountsChanged",
|
|
this.handleAccountsChanged
|
|
);
|
|
window.ethereum.removeListener("close", this.handleClose);
|
|
window.ethereum.removeListener(
|
|
"networkChanged",
|
|
this.handleNetworkChanged
|
|
);
|
|
}
|
|
}
|
|
|
|
async sign(params) {
|
|
const rawData = web3.utils.fromUtf8(params);
|
|
const { result } = await window.ethereum.send("personal_sign", [
|
|
rawData,
|
|
window.ethereum.selectedAddress!.toLowerCase(),
|
|
]);
|
|
return {
|
|
raw: rawData,
|
|
sign: result,
|
|
};
|
|
}
|
|
|
|
async createNft({ contract, from, assetId, signature }) {
|
|
const nftContract = new web3.eth.Contract(nftAbi, contract);
|
|
return await nftContract.methods.Mint(from).send({ from });
|
|
}
|
|
|
|
async transferNft({ contract, to, from, tokenId }) {
|
|
const nftContract = new web3.eth.Contract(nftAbi, contract);
|
|
return nftContract.methods.Transfer(from, to, tokenId).send({ from });
|
|
}
|
|
|
|
async sendTransaction({ from, to, value }) {
|
|
return web3.eth.sendTransaction({
|
|
from,
|
|
to,
|
|
value,
|
|
});
|
|
}
|
|
// "0xaC893B498E2005Af4cb8b03D710F187EC23a8f5f";
|
|
async sendUsdtTransaction({ from, contract, to, value }) {
|
|
const usdtContract = new web3.eth.Contract(usdtAbi, contract);
|
|
return usdtContract.methods.transfer(to, value).send({ from });
|
|
}
|
|
}
|
|
|
|
export default TBG;
|