-
Notifications
You must be signed in to change notification settings - Fork 39
/
GrantRegistryNewGrant.vue
300 lines (280 loc) · 8.8 KB
/
GrantRegistryNewGrant.vue
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<template>
<div v-if="txHash">
<BaseHeader name="Create Grant : Transaction Status" />
<TransactionStatus
:hash="txHash"
buttonLabel="CONTINUE"
:buttonAction="() => (grantId ? pushRoute({ name: 'dgrants-submitted' }) : null)"
/>
</div>
<div v-else>
<BaseHeader name="Setup Grant" />
<form @submit.prevent="createGrant">
<!-- Grant name -->
<InputRow>
<template v-slot:label>Title:</template>
<template v-slot:input>
<BaseInput
v-model="form.name"
width="w-full"
placeholder="Grant name"
id="grant-name"
:rules="isDefined"
errorMsg="Please enter a name"
/>
</template>
</InputRow>
<!-- Owner address -->
<InputRow text="has permission to edit the grant">
<template v-slot:label>Owner address:</template>
<template v-slot:input>
<BaseInput
v-model="form.owner"
width="w-full"
placeholder="owner ethereum address"
id="owner-address"
:rules="isValidAddress"
errorMsg="Please enter a valid address"
/>
</template>
</InputRow>
<!-- Payee address -->
<InputRow text="address all contributions and matching funds are sent to">
<template v-slot:label>Payee address:</template>
<template v-slot:input>
<BaseInput
v-model="form.payee"
width="w-full"
placeholder="payee ethereum address"
id="payee-address"
:rules="isValidAddress"
errorMsg="Please enter a valid address"
/>
</template>
</InputRow>
<!-- Grant Description -->
<InputRow>
<template v-slot:label>Description:</template>
<template v-slot:input>
<BaseTextarea
v-model="form.description"
width="w-full"
:placeholder="LOREM_IPSOM_TEXT"
id="grant-description"
:required="true"
:rules="isDefined"
errorMsg="Please enter a description"
/>
</template>
</InputRow>
<!-- Grant website -->
<InputRow>
<template v-slot:label>Website:</template>
<template v-slot:input>
<BaseInput
v-model="form.website"
width="w-full"
placeholder="https://"
id="grant-website"
:rules="isValidWebsite"
errorMsg="Please enter a valid URL"
:required="false"
/>
</template>
</InputRow>
<!-- Grant github -->
<InputRow>
<template v-slot:label>Github:</template>
<template v-slot:input>
<BaseInput
v-model="form.github"
width="w-full"
placeholder="https://"
id="grant-github"
:rules="isValidGithub"
errorMsg="Please enter a valid Github URL"
:required="false"
/>
</template>
</InputRow>
<!-- Grant twitter handle -->
<InputRow>
<template v-slot:label>Twitter:</template>
<template v-slot:input>
<BaseInput
v-model="form.twitter"
width="w-full"
placeholder="@twitterhandle"
id="grant-handle"
:rules="isValidTwitter"
errorMsg="Please enter a valid Twitter handle"
:required="false"
/>
</template>
</InputRow>
<!-- Grant logo -->
<InputRow>
<template v-slot:label>Upload Logo:</template>
<template v-slot:input>
<BaseImageUpload
v-model="form.logo"
:logoURI="form.logoURI"
width="w-full"
id="grant-logo"
:rules="isValidLogo"
errorMsg="Logo must be in png, jpg, or svg format, under 512 kB, with dimensions of at least 500x500"
:required="false"
@update:modelValue="updateLogo"
:isUploading="isUploadingLogo"
/>
</template>
</InputRow>
<!-- Submit button -->
<div class="px-4 md:px-12 py-12">
<button
type="submit"
class="btn btn-primary ml-auto"
:class="{ disabled: !isFormValid || !isCorrectNetwork }"
:disabled="!isFormValid || !isCorrectNetwork"
>
Create Grant
</button>
</div>
</form>
</div>
</template>
<script lang="ts">
// --- External Imports ---
import { computed, defineComponent, ref } from 'vue';
// --- Component Imports ---
import BaseHeader from 'src/components/BaseHeader.vue';
import InputRow from 'src/components/InputRow.vue';
import BaseInput from 'src/components/BaseInput.vue';
import BaseImageUpload from 'src/components/BaseImageUpload.vue';
import BaseTextarea from 'src/components/BaseTextarea.vue';
import TransactionStatus from 'src/components/TransactionStatus.vue';
// --- Store ---
import useWalletStore from 'src/store/wallet';
// --- Methods and Data ---
import { LOREM_IPSOM_TEXT, NO_LOGO_OBJECT } from 'src/utils/constants';
import { isValidAddress, isValidWebsite, isValidGithub, isValidTwitter, isDefined, pushRoute, urlFromTwitterHandle, isValidLogo, watchTransaction } from 'src/utils/utils'; // prettier-ignore
import * as ipfs from 'src/utils/data/ipfs';
function useNewGrant() {
const { signer, grantRegistry, isCorrectNetwork } = useWalletStore();
const txHash = ref<string>();
const grantId = ref<string>();
const isUploadingLogo = ref<boolean>(false);
// Define form fields and parameters
const form = ref<{
owner: string;
payee: string;
name: string;
description: string;
website: string;
github: string;
twitter: string;
logo: File | undefined;
logoURI: string | undefined;
}>({
owner: '',
payee: '',
name: '',
description: '',
website: '',
github: '',
twitter: '',
logo: undefined,
logoURI: undefined,
});
const isLogoValid = ref(true);
const isFormValid = computed(() => {
return (
isValidAddress(form.value.owner) &&
isValidAddress(form.value.payee) &&
isDefined(form.value.name) &&
isDefined(form.value.description) &&
isValidWebsite(form.value.website) &&
isValidGithub(form.value.github) &&
isValidTwitter(form.value.twitter) &&
isLogoValid.value
);
});
async function updateLogo(logo: File | undefined) {
isLogoValid.value = await isValidLogo(logo);
form.value.logo = logo && isLogoValid.value ? logo : undefined;
if (isLogoValid.value) isUploadingLogo.value = true;
try {
form.value.logoURI = logo
? await ipfs.uploadFile(logo).then((cid) => ipfs.getMetaPtr({ cid: cid.toString() }))
: '';
isUploadingLogo.value = false;
} catch (err) {
isUploadingLogo.value = false;
throw err;
}
}
/**
* @notice Creates a new grant, parses logs for the Grant ID, and navigates to that grant's page
*/
async function createGrant() {
// Send transaction
const { owner, payee, name, description, website, github, twitter, logoURI } = form.value;
const twitterURI = twitter === '' ? twitter : urlFromTwitterHandle(twitter);
const properties = { websiteURI: website, githubURI: github, twitterURI };
if (!signer.value) throw new Error('Please connect a wallet');
if (!isCorrectNetwork.value) throw new Error('Wrong network');
let cid = '';
if (logoURI) {
const splitLogoURI = (logoURI as string).split('/');
cid = splitLogoURI[splitLogoURI.length - 1];
}
const logoPtr = cid ? ipfs.formatMetaPtr(cid) : NO_LOGO_OBJECT;
const metaPtr = await ipfs
.uploadGrantMetadata({ name, description, logoPtr, properties })
.then((cid) => ipfs.formatMetaPtr(cid.toString()));
// watch the transaction to check for any replacements/cancellations and update txHash accordingly
const tx = await watchTransaction(() => grantRegistry.value.createGrant(owner, payee, metaPtr), txHash);
// if the tx resolved...
if (tx) {
// Parse receipt to find the grant ID
const receipt = await tx.wait();
const log = grantRegistry.value.interface.parseLog(receipt.logs[0]); // there is only one emitted event
// set the grantId
grantId.value = log.args.id;
}
}
return {
updateLogo,
createGrant,
isValidAddress,
isValidWebsite,
isValidGithub,
isValidTwitter,
isValidLogo,
isFormValid,
isLogoValid,
isDefined,
isCorrectNetwork,
form,
LOREM_IPSOM_TEXT,
txHash,
grantId,
pushRoute,
isUploadingLogo,
};
}
export default defineComponent({
name: 'GrantRegistryNewGrant',
components: {
BaseHeader,
BaseInput,
BaseImageUpload,
BaseTextarea,
InputRow,
TransactionStatus,
},
setup() {
return { ...useNewGrant() };
},
});
</script>