assembly/composables/useWeb3.ts

145 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-08-18 20:20:45 +00:00
import { computed, onMounted, ref, watch } from "@nuxtjs/composition-api";
2021-07-19 19:35:31 +00:00
import Web3 from "web3";
import Web3Modal from "web3modal";
2021-07-21 20:53:46 +00:00
import { Network } from "./useNetwork";
2021-07-19 19:35:31 +00:00
let web3Modal: Web3Modal;
let web3Provider: any;
2021-07-20 21:39:22 +00:00
let providerOptions = {};
2021-07-19 19:35:31 +00:00
2021-07-21 20:53:46 +00:00
const chains = [
{
name: "mainnet" as Network,
chainId: 1,
displayName: "Mainnet"
},
{
name: "polygon" as Network,
chainId: 137,
node: "https://rpc-mainnet.matic.network",
displayName: "Polygon"
}
];
2021-07-19 19:35:31 +00:00
const active = ref(false);
const chainId = ref<number>();
2021-07-21 20:53:46 +00:00
const networkName = computed<Network>(
() => chains.find(c => c.chainId === chainId.value)?.name || Network.Mainnet
);
2021-07-19 19:35:31 +00:00
const account = ref<string>();
const web3 = ref<Web3>();
2021-07-20 21:39:22 +00:00
export function setProviders(providers: any) {
providerOptions = providers;
}
2021-07-19 19:35:31 +00:00
export function useWeb3() {
onMounted(async () => {
if (web3Modal) {
return;
}
web3Modal = new Web3Modal({
disableInjectedProvider: false,
cacheProvider: true,
providerOptions
});
if (web3Modal.cachedProvider) {
await activate();
}
});
const activate = async () => {
web3Provider = await web3Modal.connect();
active.value = true;
if (web3Provider.selectedAddress) {
account.value = web3Provider.selectedAddress;
} else if (web3Provider.accounts && web3Provider.accounts.length) {
account.value = web3Provider.accounts[0];
}
let newWeb3 = new Web3(web3Provider);
chainId.value = await newWeb3.eth.getChainId();
web3.value = newWeb3;
2021-07-27 21:43:51 +00:00
setProvider(web3Provider);
2021-07-19 19:35:31 +00:00
};
const deactivate = async () => {
if (
web3.value &&
web3.value.currentProvider &&
typeof web3.value.currentProvider === "object"
) {
//@ts-ignore
if (typeof web3.value.currentProvider.disconnect === "function") {
//@ts-ignore
web3.value.currentProvider.disconnect();
}
}
web3Modal.clearCachedProvider();
web3Provider = undefined;
active.value = false;
web3.value = undefined;
account.value = undefined;
chainId.value = undefined;
};
2021-07-27 21:43:51 +00:00
const setProvider = provider => {
if (web3Modal.cachedProvider === "walletconnect") {
provider.on("accountsChanged", () => {
location.reload();
});
// Subscribe to networkId change
provider.on("networkChanged", () => {
location.reload();
});
// Subscribe to session connection/open
provider.on("open", () => {
location.reload();
});
// Subscribe to session disconnection/close
provider.on("close", () => {
location.reload();
});
}
// Subscribe to chainId change
provider.on("chainChanged", refreshWeb3);
2021-08-11 19:43:46 +00:00
provider.on("accountsChanged", refreshWeb3);
2021-07-27 21:43:51 +00:00
};
const refreshWeb3 = async () => {
2021-07-28 21:50:35 +00:00
if (!web3Provider) {
return;
}
2021-07-27 21:43:51 +00:00
let newWeb3 = new Web3(web3Provider);
chainId.value = await newWeb3.eth.getChainId();
web3.value = newWeb3;
};
2021-08-18 20:20:45 +00:00
const setWeb3 = (newWeb3: Web3) => {
web3.value = newWeb3;
}
watch(web3, () => {
window.web3 = web3.value;
})
2021-07-19 19:35:31 +00:00
return {
2021-07-26 22:19:20 +00:00
account,
2021-07-19 19:35:31 +00:00
chainId,
web3,
active,
activate,
2021-07-21 20:53:46 +00:00
deactivate,
2021-07-27 21:43:51 +00:00
networkName,
2021-08-18 20:20:45 +00:00
refreshWeb3,
setWeb3,
2021-07-19 19:35:31 +00:00
};
}