assembly/composables/useDSA.ts

189 lines
4.2 KiB
TypeScript
Raw Normal View History

2021-07-19 19:35:31 +00:00
import { computed, readonly, ref, watch } from "@nuxtjs/composition-api";
2021-09-03 13:59:09 +00:00
import { useWeb3 } from "@instadapp/vue-web3";
2021-07-19 19:35:31 +00:00
import DSA from "dsa-connect";
2021-08-19 22:12:39 +00:00
import addresses from "~/constant/addresses";
import abis from "~/constant/abis";
import { AbiItem } from "web3-utils";
import { useNotification } from "./useNotification";
2021-08-31 19:23:47 +00:00
import { useNetwork } from "./useNetwork";
2021-07-19 19:35:31 +00:00
const dsa = ref<DSA>();
const accounts = ref<any[]>([]);
const activeAccount = ref<any>();
2021-08-19 22:12:39 +00:00
const authorities = ref<string[]>();
2021-07-19 19:35:31 +00:00
export function useDSA() {
2021-08-31 19:23:47 +00:00
const { active, library, chainId, account } = useWeb3();
const { activeNetworkId } = useNetwork()
2021-08-19 22:12:39 +00:00
const { showWarning } = useNotification();
2021-07-19 19:35:31 +00:00
2021-08-31 19:23:47 +00:00
watch(library, () => {
if (library.value) {
dsa.value = new DSA(library.value, chainId.value);
}
});
watch(active, () => {
if (library.value) {
dsa.value = new DSA(library.value, chainId.value);
2021-08-11 12:32:25 +00:00
}
2021-08-19 22:12:39 +00:00
});
2021-07-19 19:35:31 +00:00
2021-08-19 22:12:39 +00:00
watch(chainId, () => {
2021-08-31 19:23:47 +00:00
if (library.value) {
dsa.value = new DSA(library.value, chainId.value);
2021-08-19 22:12:39 +00:00
}
});
2021-07-26 22:19:20 +00:00
2021-08-21 11:33:31 +00:00
const refreshAccounts = async () => {
accounts.value = await dsa.value.getAccounts(account.value);
2021-07-19 19:35:31 +00:00
2021-08-19 22:12:39 +00:00
if (accounts.value.length > 0) {
activeAccount.value = accounts.value[0];
} else {
activeAccount.value = undefined;
2021-07-19 19:35:31 +00:00
}
2021-08-21 11:33:31 +00:00
}
watch(dsa, async () => {
if (dsa.value) {
refreshAccounts()
2021-08-11 12:32:25 +00:00
}
2021-08-19 22:12:39 +00:00
});
2021-07-19 19:35:31 +00:00
2021-07-26 22:19:20 +00:00
watch(
activeAccount,
async () => {
2021-08-19 22:12:39 +00:00
authorities.value = [];
2021-07-26 22:19:20 +00:00
if (activeAccount.value) {
dsa.value.setAccount(activeAccount.value.id);
2021-08-19 22:12:39 +00:00
fethAuthorities();
2021-07-26 22:19:20 +00:00
}
},
{ immediate: true }
);
2021-07-19 19:35:31 +00:00
const creatingAccount = ref(false);
2021-08-19 22:12:39 +00:00
const creatingAuthority = ref(false);
const removingAuthority = ref(false);
2021-07-19 19:35:31 +00:00
async function createAccount() {
creatingAccount.value = true;
try {
const transactionHash = await dsa.value.build({ version: 2 });
2021-08-22 14:20:28 +00:00
accounts.value = await dsa.value.getAccounts(account.value);
2021-07-19 19:35:31 +00:00
return transactionHash;
} catch (error) {
} finally {
creatingAccount.value = false;
}
}
function setAccount(account: any) {
activeAccount.value = account;
}
2021-08-19 22:12:39 +00:00
async function fethAuthorities() {
try {
2021-08-31 19:23:47 +00:00
const accountsResolverInstance = new library.value.eth.Contract(
2021-08-19 22:12:39 +00:00
abis.resolver.accounts as AbiItem[],
2021-08-31 19:23:47 +00:00
addresses[activeNetworkId.value].resolver.accounts
2021-08-19 22:12:39 +00:00
);
const rawData = await accountsResolverInstance.methods
.getAccountAuthorities(activeAccount.value.address)
.call();
authorities.value = rawData;
} catch (error) {}
}
async function createAuthority(newAuthority: string) {
try {
if (!newAuthority) {
return;
}
const owners = authorities.value.map(x => x.toLowerCase());
if (owners.includes(newAuthority.toLowerCase())) {
showWarning("Create Authority", "Account is already an owner!");
return;
}
creatingAuthority.value = true;
const spells = dsa.value.Spell();
spells.add({
connector: "authority",
method: "add",
args: [newAuthority]
});
const transactionHash = await dsa.value.cast({
spells,
from: account.value
});
creatingAuthority.value = false;
fethAuthorities();
return transactionHash;
} catch (error) {
creatingAuthority.value = false;
}
}
async function removeAuthority(authority) {
try {
if (authorities.value.length <= 1) {
showWarning("Remove Authority", "Cannot remove all authorities!");
return;
}
removingAuthority.value = true;
const spells = dsa.value.Spell();
spells.add({
connector: "authority",
method: "remove",
args: [authority]
});
const transactionHash = await dsa.value.cast({
spells,
from: account.value
});
removingAuthority.value = false;
fethAuthorities();
return transactionHash;
} catch (error) {
removingAuthority.value = false;
}
}
2021-07-19 19:35:31 +00:00
return {
dsa,
2021-08-21 11:33:31 +00:00
refreshAccounts,
2021-07-19 19:35:31 +00:00
activeAccount: readonly(activeAccount),
accounts,
createAccount,
creatingAccount,
setAccount,
2021-08-31 19:23:47 +00:00
library,
2021-08-19 22:12:39 +00:00
chainId,
authorities,
createAuthority,
creatingAuthority,
removeAuthority,
removingAuthority
2021-07-19 19:35:31 +00:00
};
}