forked from snapshot-labs/snapshot-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseSpaceController.ts
144 lines (131 loc) · 4.12 KB
/
useSpaceController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { computed, ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import { getInstance } from '@snapshot-labs/lock/plugins/vue3';
import namehash from '@ensdomains/eth-ens-namehash';
import networks from '@snapshot-labs/snapshot.js/src/networks.json';
import { getAddress, isAddress } from '@ethersproject/address';
import {
sendTransaction,
getSpaceUri
} from '@snapshot-labs/snapshot.js/src/utils';
import {
useTxStatus,
useI18n,
useWeb3,
useApp,
useFlashNotification
} from '@/composables';
const spaceControllerInput = ref('');
const modalUnsupportedNetworkOpen = ref(false);
const modalConfirmSetTextRecordOpen = ref(false);
const settingENSRecord = ref(false);
const pendingENSRecord = ref(false);
const uriAddress = ref('');
const defaultNetwork = import.meta.env.VITE_DEFAULT_NETWORK;
export function useSpaceController() {
const { web3 } = useWeb3();
const { pendingCount } = useTxStatus();
const auth = getInstance();
const { t } = useI18n();
const route = useRoute();
const { domain } = useApp();
const { notify } = useFlashNotification();
const ensAbi = ['function setText(bytes32 node, string key, string value)'];
const controllerInputIsValid = computed(() =>
isAddress(spaceControllerInput.value)
);
const networkKey = computed(() => web3.value.network.key);
const ensAddress = computed(
() => domain || route.params.ens || route.params.key
);
const textRecord = computed(() => {
const keyURI = encodeURIComponent(ensAddress.value as string);
const address = spaceControllerInput.value
? getAddress(spaceControllerInput.value)
: null;
const registryNetworkPath = defaultNetwork === '1' ? '' : 'testnet/';
return `ipns://storage.snapshot.page/registry/${registryNetworkPath}${address}/${keyURI}`;
});
async function waitForSetRecord(tx) {
pendingENSRecord.value = true;
pendingCount.value++;
const receipt = await tx.wait();
await loadUriAddress();
pendingCount.value--;
pendingENSRecord.value = false;
notify(t('notify.ensSet'));
console.log('Receipt', receipt);
return receipt;
}
async function setRecord() {
settingENSRecord.value = true;
try {
const ensPublicResolverAddress = networks[networkKey.value].ensResolver;
if (!ensPublicResolverAddress) {
throw new Error('No ENS resolver address for this network');
}
const ensname = ensAddress.value;
const node = namehash.hash(ensname);
const tx = await sendTransaction(
auth.web3,
ensPublicResolverAddress,
ensAbi,
'setText',
[node, 'snapshot', textRecord.value]
);
settingENSRecord.value = false;
notify(t('notify.transactionSent'));
waitForSetRecord(tx);
return tx;
} catch (e) {
notify(['red', t('notify.somethingWentWrong')]);
console.log(e);
} finally {
settingENSRecord.value = false;
}
}
function confirmSetRecord() {
if (networkKey.value !== defaultNetwork)
modalUnsupportedNetworkOpen.value = true;
else modalConfirmSetTextRecordOpen.value = true;
}
async function loadUriAddress() {
const uri = await getSpaceUri(ensAddress.value, defaultNetwork);
console.log('URI', uri);
const uriArray = uri?.split('/') ?? [];
if (defaultNetwork === '1') {
return (uriAddress.value = uriArray[4] ?? '');
}
if (uriArray[4] === 'testnet') {
return (uriAddress.value = uriArray[5] ?? '');
}
}
// Checks if a text-record with the connected wallet address exists
// and skips to step 3 if it does
const loadingTextRecord = ref(false);
onMounted(async () => {
try {
loadingTextRecord.value = true;
await loadUriAddress();
loadingTextRecord.value = false;
} catch (e) {
console.log(e);
loadingTextRecord.value = false;
}
});
return {
spaceControllerInput,
controllerInputIsValid,
settingENSRecord,
pendingENSRecord,
modalUnsupportedNetworkOpen,
modalConfirmSetTextRecordOpen,
uriAddress,
loadingTextRecord,
setRecord,
confirmSetRecord,
loadUriAddress,
ensAddress,
textRecord
};
}