Building an Onchain Crowdfunding App: Frontend
Urbe Campus ETHWarsaw Edition 2024 · September 2024 · 1:52:37
Orbulo leads a hands-on Urbe Campus session at the ETHWarsaw edition on building the frontend for an onchain crowdfunding app. Starting from the contracts deployed the day before, a campaign factory and ERC-1155 donation receipts, participants set up a Next.js starter with wagmi, viem and TanStack Query, configure Sepolia and wire the app to the smart contracts.
Speakers
Topics
From Urbe Campus ETHWarsaw Edition 2024, hosted by Urbe.eth.
Transcript
[Music] [Applause] [Music] [Music] okay so guys we're we're going to start now and uh hi everyone I'm I'm arbo and today we're going to see how to interact with smart contracts uh from from a
front end basically and uh the starting point will be the work that has been done yesterday by Frank and it's this repo over here you can you can find it on on gab here you have the the contracts that have been developed yesterday if you were here yesterday you see you saw all the process that was behind it but I think that they should be pretty easy to understand if the Wi-Fi works hopefully okay so just a little bit of recap of what was done yesterday we have this uh campaign Factory which is a smart contract that deploys new
crowdfunding campaigns uh it uses the usdc address and basically just clones a smart contract and it's going to initialize it with some parameters and today we are going to create some campaigns by using this this function over here instead the other the other smart contract it's a little bit more complex and uh it's the campaign itself and it has some some data such as the the admin the metadata of the campaign the target amount of the campaigns since we are talking about crowdfunding campaigns and the start and end of the campaign more in detail we are going to interact with uh all the functions that
we see over here hello and uh so the claim function used by the Admin to claim the funds of the campaign uh withdraw it's used to withdraw the funds of the campaign I don't know the different but we're going to discover them later and donate of course to donate to a to to a given campaign when you donate to a campaign it's going to mean you on ERC 1155 so basically an nft and um so every time you donate basically so yeah in order to start we are going to use next nextjs as the as the starter and more in detail we are
going to use the next UI starter which basically gives you all also um thems and components and stuff like that to speed up the UI development so I've already done that because I was scared of the of the Wi-Fi not working but now it works so you can you can do it so you you just need to install the next UI CLI cloudo drone sorry guys and um you need to install the next UI CLI and then just type next UI in it and the name of your project as you as you can see like over here I called it it it was so campus frontend basically and then you
need to select up the name of the project which will be already populated with the one that you put it and your favorite package manager I use BN because it's faster so let me know whenever you are done with it should be pretty fast but okay so after you create
the the front end basically the the the template you need of course to install the dependency so you need to enter into the folder and execute B install yarn install whatever your package manager of choice npm install and yeah so when you're done we are going to add these libraries over here which are the three main libraries used to uh handle the smart contract interaction from from the front end which are wagi VM and tanac react query which is mainly used for wagm actually so you need to add these three inside your inside your UI
basically hello M Okay so when it's everything installed you should have something like this which is a huge app with a lot of useless boiler plate code basically so we're going to start it and see how how it looks so we just start with ban randev we open the browser and we go to
Local Host 3000 and we have this page with a lot of other Pages like dogs pricing which is basically empty pages but we're going to pretty much remove everything we're going we're going to only use the components themselves so you can safely delete pretty much everything except this Primitives and this theme switch so you can delete all these components over here the counter icons and navbar you can safely delete them you can delete the about page the blog the docs the pricing pretty much all of these and we need to
update for sure in the layout we need to remove the Naf bar because we're not going to use it we are going to remove this footer as well and we should be left with something like we need to change also the home here because we don't have the GitHub icon anymore but we can delete prettyy much everything in the home as well we leave only an empty section okay so we have a black page which is good that's what we wanted so just give me one second because I'm going to I'm going to copy my voice code
[Music] configuration uh should be like this okay so we have this done this done so the first thing we need to do is uh enable wag me inside of our front end uh if if it's your first time using wagi it's pretty simple so to use wagm we need to create um um configuration object where you specify which chain you are using and which RPC URLs um you are using for each given chain so we can actually follow this
pretty easy tutorial on wag me. sh react gettin started so we're going to go to config and type wag me. TS and create this new file and we are going to export a wag me config we're going to create create config hopefully know why it's not taking it automatically but whatever so the chains we're going to work on it's only one actually and it's seoa and we're going to import it from VM chains here you can also import it from wagm chains it's
pretty much the same actually I think that wagi exports all the VM chains so you can type wagi chains or seoa chains is or VM Chains It's it's the same then we have the transports and we're going to have cia. ID we're going to have HTTP which is the default one so this is the only configuration that we need to do right now for for wagi because the smart contracts are were deployed yesterday on on seia you can see you have the the addresses here of the uh of the smart contracts in the in the gab repo so
yeah so after we created the wagi config we need to create the um wagm provider so the next step which is really easy as well we need to wrap our app inside the uh wagm context provider we already have this providers uh TSX file which was kindly provided Us by next UI so the only thing that we need to do is add another provider here which is the wag me provider so I'm going to have a wag me config here going to put the children I don't know why it's not AO importing but whatever this should be wag me Provider
from uh should be always wagi I think and import wag me config don't know if it's export con yeah from config WME okay this one's an empty line okay so now we imported the the wagi provider the next step is to uh also add the um the quiry client provider for uh tanack react query which is basically used by wagi itself and this is really easy as
you can see here you just need to create a new query client for for uh for for um tanac basically and then just add another provider inside here which should be the quiry client provider quy client provider client okay so this is the basically all the setup that we need to do to initialize wagm inside of our nextjs app so that's pretty much it and now as you can see we can start directly using wagm so the cool thing about wagi is that it
provides us with a lot of uh hooks that can be used to interact with smart contracts to retrieve enss names to estimate gas to sign messages pretty much for everything and so it's the kind of has become over the years the the standard for the for the front end development so the first thing that we need to do of course as every app in the in the web tree we need to connect our wallet basically it's always the first step and so we're going to go to the next UI page and we are going to kindly steal one of their nav bars if you go on the left you
have all this menu with all the with all the components we're going to steal one of their nav bars one really easy like this one like the first one we're going to also remove a lot of a lot of their stuff but so we just copy pretty much everything over here we're going to go to components and create a new navb bar. TSX and we are going to call it we're going to call it up na bar otherwise it's going to break with the other one and we're going to say return all of these and then we are also going to
steal the Imports as well we are not going to steal the logo because it's useless let not finding this this one second ah okay so yeah we need to actually when when installing via the nexi CLI you don't install all the nexi packages but the only ones that are that are used basically so we need to run this command over here which is npx nexu I latest add navbar and this adds the navbar component to our npm packages so just going to stop it
going to run the ad Navar so that we can get the component hopefully we just installed it but whatever you already have it added okay so maybe we just need okay yeah we just need to change the UT from navbar to from react to navbar and then as well we need to import these ones from I think this should be link and following the same logic this
should be button okay so we are going to add the Navar as before inside the L layout which is called up Navar and comes from import up navb from components navb and if we go if we restart it again maybe we should be seeing the the Navar
popping up on the on the website so as you can see this is the na bar that we just stole basically without the logo so we're going to remove some stuff of course because we don't need it so we can remove this this navbar content element it disappears we can change this one what's the name of the of the project you didn't give it a name if you want three starter okay so we change the name to three starter we're going to remove the login one so we are only so we only have this
button like here sign up and we're going to change this one with connect wallet so this is the typical nav bar you may say of any web3 application where you just connect your wallet over here so in order to connect your wallet using wagi we need to use use the use connect hook which you can find here on the left or you can also search it in in the wagi website so it's pretty simple we just need to import the the hook inside the inside the Navar in this case I think that we need to use also a a use client here so we import
the the use connect hook and then we change this button we remove the as link we remove the H ref and we say just um it should be on click just see when we click this button we're going to connect and this should be uh it should be injected connector injected we should can't see anything with these lines and we need to import this injected over here
so let's see if it works hopefully it works it does not so this is pretty nice uh this could be the case for okay because I'm already connected because it already knows the the local lost I need to okay unlink it and so now if I click on connect it works now I'm I'm I'm using rabby if you want I can also use metamask if it's more uh new to you but basically rabits another uh browser wallet in order to connect in wag me I need to ignore like all the allers like for for example this
side which is local three 3,000 it's not very popular of course because it's a local website so I'm just going to type ignore all and just connect my wallet so I'm actually connected now but as you can see the button didn't change so I'm basically not connected for a for a user point of view so we are going to change this button really fast and display the address of the user so uh we're going to use the it should be the use account which is another hook and um as you can see it's also pretty easy to use and we're going to actually
log the content of this account so that you can see so we're going to open the console so when I refresh we have some I I hope that you can see from back there but we have a lot of properties the main one that we are looking for of course it's the address so if we log the address which should see my address which is the0 X B1 which is this one okay so really easy we are going to do a a little check here and we're going to say if we have the address which means
if I'm connected basically I think that should be also is connected which is better so if I'm connected I'm going to display in the uh the the button with the with the address and the possibility to disconnect from the App instead if I'm not connected I'm going to show the same Button as before so we're just going to copy and paste it here and so here inside the the button that shows when I'm connected I'm just going to blondly paste the address right now and we're going to remove this connect here because of course I'm already connected and as you can see here's my address which is pretty ugly
as you as you can imagine because it's all the 42 42 characters so we're going to create a little useful function which we can add I don't know whatever we can add something also here what going to call it U.S I'm going to create a function which is called shorten address which I've done so many times so it's going to pop it for me so what it does is basically shortens the address into the first three characters well how many characters you pass so this case the the default is four so we
can change this one with shorten address and then we can import it from what shorten address from uh configs uh of course because this one's the check so if it's connected then we have an address basically and as you can see now the address it's a little bit better because we have the0 xp1 v z we have the three dots and then we have the rest of the address so going back to the to
[Music] the to the smart contract so the first thing that we need to do in my opinion in this kind of app for crowdfunding we need to show and retrieve all the campaigns that were created as so someone enters this website and can see if there are some campaigns or not I don't know if you yesterday you you deployed some campaigns using the fact that okay so let's not do that instead we are going to actually create a campaign so in order to create a campaign and to interact with a smart contract we need two uh important things the first one is of course the address of the smart
contract that we're going to interact with and the second uh thing is the ABI the API is uh basically a Json uh list of all the inputs of of all the functions all the events that have been uh implemented inside that that given smart contract in order to get the the ABI of the campaign and campaign Factory smart contracts you need to run npm install on the on the solidity repo which is this one on on giab and then you need to write to write this command over here which is
n pxr that compile so I'm going to show you actually now this has already everything developed so if I remove this one and type desire. compile it's going to generate some typings and some and some Json for me which are these two files here so in order to get the API you need to go into artifacts contracts and then you have the campaign doall and campaign Factory doall folders and inside here you have the campaign. Json and the CP Factory which is the name of the other smart contract. Json and we only need to get this API over here so we're I'm going to copy this
list basically this Json array and I'm going to go to config and create an ai. TS and say export const uh campaign Factory API going to copy it and say as const this as const is used for uh wagi and we're going to see later which is really nice because if you mark it as constant and then you pass the ABI to wagi wagi is going to uh automatically understand which functions are present in the smart contract so if you're using typ script you get Auto completion and stuff like that also for
the for the part parameters of the functions the second API I'm going to copy it really fast is the one of the campaign itself and we're going to do the same thing so we're going to do export cons campaign Abbi as const and so now we have these two huge objects which we don't really care how they look so we can leave them closed like this so as we were seeing the first thing that we need to do is create a campaign since there are no campaigns so we are going to create uh inside the inside inside the Navar we are going to change
this pattern and instead we are going to make it as a link a link which has an uh HF of Slash uh I don't know create campaign nah uh yeah let's call it let's call it create campaign yeah so when we click on this button over here as you can see it shows this page could not be found because we are going we are trying to go to this create campaign page Let's ignore this error because it's due to the auto refresh it's it's totally ignorable right now so so we need to create this page this create campaign
page in next if it's your first time using it it's you you just need to create a new folder uh inside this app and call it with the name of the route that you want in this case it's create campaign and then you need to create a page. TSX file and of course create a component inside this case we are going to call it create campaign page and we're going to put an empty div for now I don't know why sometimes as you can see there's not anymore the the empty stuff like like the 404 page it's going to remove it for me so
let's ignore it so let's see on the on the smart contract which things we need to uh pass to the campaign Factory to uh create a new campaign so we have the admin which is the address of the user that's creating the campaign so we already have that the CP metadata which I think it's metadata associated with the with the campaign I don't know it's maybe a description what is it Frank uh should be a link onfs if you want or maybe destion okay so it can be a link to the to the to the metadata okay we have the target amount and when it's
going to start and when it's going to end so let's see so we need one field for the for the metadata I don't know if we have time to do the ipfs stuff but maybe we can add it later we need a a field for the for the Target amount for sure which is going to be numeric and it's going to be related to usdc and one for the start time and one for the end time but okay so we're going to create over here we're going to create a section element and like let's not focus really on the on the UI right now let's more
focus on the interaction so I'm going to go pretty fast on this and also the this this template uses Tailwind as well so you can use all the Tailwind classes to handle the uh the style of the page so there's no need to write uh a dedicated CSS file so we're going to create a simple flex and flex column over here and hopefully there's the input input file from from next UI so we're going to check real fast if we have it otherwise we need to install it so next UI input
okay amazing that is so let's go to the next UI and see what inputs we need as I said we need a numeric one which should be just changing the type so we can get one of these so this is also required so it's pretty nice so we copy this one and say type should be number so nobody is complaining about that so this is the campaign Target well let's call it campaign goal if we go here we have this numeric field of course it it cannot be negative
so we're going to say that the minimum is zero so it can go down basically and uh so we have the campaign goal we said we have the metad data which is uh going to be a text one and it's going to be a metadata only for the campaign we don't have a default value we're going to separate these two Fields because otherwise it looks ugly maybe for it's better let's add an H1 and let's call it create campaign okay let's make it like a little bit bigger because otherwise I
cannot see it we can make it bold as well and maybe we should be uh we should be aligning it as well okay to the center so when you when you click on your on your profile o o over here you get back to this page and you have the create campaign so we said we have the campaign goal which is a number we have the uh where is it where is it we have the metadata URI which are those two Fields then we have the start and we have the and we can we can treat these
fields in uh in a particular way so we can let the user choose both dates or we can tell the user for how for how much time he wants the campaign to be active basically and this is what we're going to do which is pretty which is easier actually so we are going to say we're going to get also this number over here and say instead of campaign goal uh campaign duration and let's say in days okay so let's make actually default value let's put like one
here okay so campaign goal let's put also another default value let's put one and this is usdc right is it's USC okay uh so we have create campaign and the last thing of course it's adding a button in this case we're going to add it like primary uh let's make it normal and let's type create this is a pretty easy UI let's make it let's make it that it fills all the all
the space he has so the first thing that we need to do is of course link these fields to uh the react state so that we can get these values later so we're going to create um a campaign goal which is a number over here hopefully it stops from being all red I don't know why but uh yeah we need of course to Market this as a use client which it's a client component in next so that we can use the use state so we have a campaign goal as we said we have the campaign metadata which is a string and then we
have the campaign duration which is a number in order to link them um we're going to say value over here and we're going to say campaign goal and it should be on value change it's going to update the set campaign value uh okay this just wants the set campaign goal which is easier because it gets the value as a string okay so we're going to say pass int of value okay it it wants it as strings okay let's don't know why but let's use
strings and then we're going to pass them later so this is going to be a string and this is going to be a string as well so the same thing we need to do over here and say value this is the campaign metadata home value change we have the value set campaign metadata value we're going to copy this and add it over here and say the same thing which is uh campaign duration and set campaign duration so everything should be linked right now we should be number yeah this one's one this one's one
okay let's now create a simple function which is going to be create campaign of course it's going to be an a sync function and we are just going to log for now the the campaign duration campaign goal and campaign metadata maybe let's put them in order so we know which one which one it is and we are going to say whenever you click this button over here we are going to call this create campaign function so let's put a test inside the metadata Yuri and as you can see we have one test one we can change the the goal to 10 maybe and we have 10 test one on and so on and so forth
so so we said the what we need to do is interact with uh this smart contract uh which is actually deployed at this address over here because we need to um to call the campaign Factory so we're going to save this address in the in the API we can say like export const campaign Factory address you can also save it uh in the environment variables is the actually the same maybe the environment variables is better because you don't need to change the code every time but we are going to put it inside this file for right now and
um so we have pretty much everything no we need to get the uh the user address which is by using the US account that we we are using also in the in the Navar and what what else we need to do let's check this Mar contract so as we said this we have it we have it the start is going to be the time that we are that we are going to deployed basically and the end we are going to add uh X number of days in seconds to the to the start basically and the admin is that address so this is pretty much it so in order to interact
with the with the smart with with the smart contract we're going to use VM and we are going to um we need to go into wagm and get the use wallet client hook okay so we're going to use this hook here we're going to say wallet client use wallet client from wagm this should be returning actually just the wallet client okay and so we have the wallet client which is basically the client that we're going to use to uh execute the
transaction which is connected to the user wallet we can log it when we click the the button here we have this object with well actually we need to get the data and we're going to call it wallet client so this should be better so we have some functions like deploy contract we we have send transaction and so on and so forth we also have right contract which is the function that we are going to use so we're going to say uh transaction Ash in this case we're going to await wallet client in this case we need to check if it exist or not but let's assume that it
that it exists and we're going to use right contract so this is going to get some arcs the first one is the ABI which is the the huge Json list that I was importing before and this is the campaign Factory ABI then it gets the function name which as you can see as I was telling you before it's reading directly from the ABI which functions are available if you use typescript so it's pretty easy we are going to use the create CP is the create campaign function then it gets some args which are the arguments of the function and also these arguments are um uh are type save as well so the first
one is the address which is the the admin the second one should be the campaign metadata the third one should be the goal and then we have the campaign uh the the start of The Campaign which we going to look into it and the end we're going to create these fields over here so the first one is the address of course we need to be connected in order to use this one we can disable this button for example if there's no address and over here we can say if not address or not wallet client we can return so that we have the address as we want the campaign metadata is just a string the campaign goal so we have the
uh this value as one but actually we need to pass this value and put it in six decimals because the usdc uh smart contract on solidity uh like on on sepolia I think should have only six decimals we can check it real fast because when you pass these numbers you cannot pass them in the same way that you get them from the user not not seol seia scan um you need to pass them in the correct number of of decimal so we have this usdc address over here we can check from the contract over here we can check the decimals and it's
six decimal so we need to parse it into six decimals so we're going to say campaign I cannot write campaign goal amount there should be a really uh nice uh function I don't remember the name should be par something we can check it on VM actually which is the other library that we are using uh part units maybe yeah as you can see it takes a string number and the number of decimals and it's going to give us back the uh begin value in this case so we're going to use pass units we're going to pass the
campaign goal and we're going to put six and this is going to be our campaign goal amount and as you can see it's not complaining because it's going to give us directly the um the big so the next one instead it's the it's the start of The Campaign which is a little bit trickier in this case but let's say that we want to start the campaign the day after we we are going to create it let's let's make it really easy otherwise we're going to waste a lot of time on this and so let's say that that the campaign start is uh dat.
now plus uh we're going to add 24 this should be in milliseconds right we need to use seconds but we can divide everything later so we need to add one day to this in milliseconds so it's 24 hours 60 Minutes 60 60 seconds and 1,000 yeah this should be tomorrow basically then we are going to mod. floor this one we're going to put this divided by 1,000 so that everything should be fine and then we're
going to create the campaign end which is basically going to be the campaign start plus the um the number of days that we just passed uh time 24 hours time 60 Minutes time 60 seconds so this is the campaign end and this is the campaign start basically this one it has begins so we need to pass them pass them like this and say begin campaign start and begin campaign end basically and it's complaining because it maybe it's missing one parameter it's going to of of course the address of the smart contract we are going to interact
with so the campaign Factory address so as you can see we passed pretty much everything and we are going to get this transaction Ash so in order to wait for the confirmation of the transaction we need to do one more step which is calling the um weight for transac for transaction receipt and you just pass the transaction app over here well actually this one's the let's say instead of doing this let's create there should be a public client I think use public client or this directly Returns the
public client I think so thata does not exist so this should be just the public client let's use the function from the public client which should be a wait public client do wait for transaction receipt this one's a hash and the transaction hash and this is going to return return the transaction receipt so let's log this one let's say transaction receipt here we can log conso the log the transaction Ash and hopefully if I did everything correct we can execute our first transaction so let's put a campaign goal of two this case we are going to I don't know
add a fake U right now and then let's say that the campaign duration it's 2 days so if I click on create it's going to say the current chain of the wallet does not match because I'm not connected on ethereum right now so we can add this little check over here which is uh we're going to use uh two hooks the first one is the use Network it should be called or use use chain ID yes use use chain ID and this is going to return the the current chain ID that the user is connected to and then we're going to get
instead the uh use switch chain hook which instead allows us to switch chain uh from from the app basically and this should return uh switch chain as sync function so what we do is we check if the chain ID is not 11 55111 which is the chain ID of sepolia which is the chain that we are on in the case it's not we are just going to say a wait switch chain a sync and this just wants the chain ID which should be this one we can also do it better which is like this we pass the directly the chain
and it's ID so so now if I go to and try to do some create it's not swapping let me just check one second what's the what's the chain ID here the couldent chain of the wallet does not match maybe it's it's an issue over here I don't know why it's not let's let's change it directly here for now and this should be working then we're going to check why it's not working but as you can see if I click on create it's going to prompt me for a for a transaction and uh we can basically sign it and send it and let's see if it's
going to create a a campaign or not basically so we click on confirm this is the transaction as we can go and check it on sepolia this is the transaction receipt as well so if we go over here and put the and put the ash actually the the scans are slower than than VM so that's why we already have the receipt over here but instead here we still are waiting so we interacted with with our smart contract which was this one I think yeah so 26 seconds ago and we should see I think in the in the logs
I think that Frank is going to give us the address I don't know yeah we have this campaign created over here and it's going to be somewhere over here so we need to of course the the smart contact is not uh verified so we cannot see it from here but uh we are going to take a little bit of a break right now so we can take a break right now and then move later okay so starting back from where we finished so the last time we created a new campaign that was the transaction now what we need to do is retrieve the campaign on our front
end so uh we have the website over here we need to go on the homepage and we're going to retrieve everything in the homepage so since we are not using the metadata right now we're going to give them some random names to the to the campaigns but so the easiest way in this case is by using events the events emitted by the by the factory for that case we need to use another Hook from uh Alchemy which should be uh should be this one the use watch contract event
no not the use watch it should [Music] be events let's check and don't remember which one is it should be one that give us all the events or [Music] no no so we need to use VM for that so we need to retrieve all the events from a given from a given contract so we go into contracts as you can see we should have this one should be for creating a filter and get Contract events uses retrieves the event so what we need to do is when we load this page so we're
going to use the use effect Hook from from react remember also to import the the use client in this case uh we're going to create a function called Fetch events so this fetch events to be a sync and then here we're going to create a variable in the react State for the event so set events State we're going to give it an any right now we can check we can change it later and we update the UT so we need to get as we we were doing
before we need to retrieve the public client in this case so we use the use public client Hook from wagi and then we go to contract events and we say await public client dot get Contract events get Contract events okay then we need to pass the ABI which it's always the uh campaign Factory what's the name I don't remember campaign Factory ABI yes here so the campaign Factory ABI and we need to import it from campaign
Factory ABI we need to import it from the slash config SL ABI then we need to pass the address of the smart contract uh in this case it's always it's the campaign Factory address then we can also pass the event and as you can see we only have one event defined this in the in the smart contact which is the CP created and this should return all the events that were deployed by the smart contract so if we H we need to give him uh a start uh no it's not start Block it's from from block and we are going to give
it as a from block since this is the first one we can give him the the block of where we created the first campaign so in this case it's this one usually uh you should put the uh the block when the smart contract was created that's the like the best practice basically so as you can see we have one object over here we have this this Con this event which is a CP created event we have some some addresses we have the CP address which is the campaign address and the CP admin which is in this in this case it's my address so if we take this this address over here and we go on
sepolia and check it we should see a smart contract over there so as you can see here's a smart contract so of course it's not verified and stuff like that so um we're going to set events equal to the contract events or an empty array and then really easily here we're going to map and iterate all the events and we are going to let's see if card it's installed hopefully it's installed over here so you say card from next ui. card it's not here so we need to add it using the command we're going to create some cards the homepage using
next UI so we go to card see we need to add it using the CLI from next UI which is the simple command and so it added the components for us so we can say next UI or/ card and we have the card we should have the card adder and card body as well so here we're going to iterate and we're going to say card we're going to give it a key equal to the index to say card adder and we add um we say
campaign index + one let's make it really simple so that's the name of the campaign and inside the body we can say something like P admin and here we can put the event. args uh CP admin should be and we can [Music] change uh let me see if this is okay let see if we can get like this tyy but whatever so hopefully this should work first time so as you can see this is the first campaign like we only have one campaign of course we're going to get the shorten
address as well over here this is my campaign of course we can make it prettier or not but let's leave it this way and we can also make it um we can make this text a little bit bigger and also bold so that we have this campaign over here and here we can say like a border B1 maybe okay so it's pretty ugly but whatever it works so we are retrieving the campaign right now as we said using this get Contract events function as you can see it's pretty easy and as I said before if you use typescript and you use
VM and you use the abis you have the event names and also the ARs and stuff like that already populated so if here I try to maybe say I don't know campaign created it's going to give me an error because it's not a valid event so it's really useful to use both typescript and VM for this type of stuff so um we have the campaign we of course need to have a way to donate to a given campaign so let's make it really easy then we are going to maybe create the metadata for the campaign and stuff like that using epfs and so on so we're going to create a button as I said uh let's make it primary and let's
call it donate so we have this really ugly uh ugly campaign card but it works for now that maybe if we have some time we can make things prettier but so we have this donate button so of course we need to create a donate function let's not call it donate function but just donate and um we are going to pass as an argument the uh smart contract of the of the campaign so it should be campaign up address which is a string which is actually ZX string but okay and here we're going to say on
click we're going to donate and we're going to pass the argument CP Campaign which should be the address of the campaign let's double check by using a console log it should be this one it should be args CP address okay so not CP campaign but CP address of course we want to do this fetch only when the component Mount so we need to pass an empty array otherwise it's going to log it infinitely as it's
doing as it was doing before so we have this donate of course we need also a field uh where users can um let's maybe change this one put it here that's even uglier let's see Flex Flex call maybe let's say space Epsilon 2 and say item start okay so we have this let's make it a little bit bigger maybe okay so we have this button but also we need the amount uh that uh the user wants to donate as well for that given campaign so maybe in
instead of doing the donation over here maybe it's better to to create a dedicated Camp a dedicated campaign page so instead of doing this we are going to change this to a link and put an href to/ campaign campaigns and here we're going to put the event CP address let's say check out maybe check out so here if I click we are going to go to the campaigns and the address of the campaign so we need to create the campaign page basically right now so we
do the same thing as before so we create the campaigns folder then we are going to create the campaign uh let's uh call it address between brackets so this means that it's a dynamic parameter that we pass and then we create the page. TSX and here we say export uh default function create no uh campaign page and we can make it empty for now and as you can see there's no more the 404 uh error on the page so we are inside a given campaign so to interact with with a campaign of course we need to retrieve
the the the address from the URL uh we can do this by next if we go on on the next documentation we can go into the dynamic routes documentation which should be this one make sure to select the app router here because we are using the app router and uh as you can see we have the params inside the page inside as a as a parameter as a parameter of the page so we can go over here and just say pams of a type PMS address string in our case because the name of the parameter is uh address we could also
do something like this but let's keep it simple and just say console.log let's check params do address when we go inside that page we should have the address hopefully not client side because this is not let me check I don't remember if this works with client as well or not yeah so we have also still the log from the Navar maybe let's remove this because otherwise it's going to to create some error so this is the same address that we have inside the URL as you can see so 0x1 19B is the same receive over there here I can type
pretty much anything I can type test and it should be login test over here so let's go back to the other one so this is the address of the campaign so we can say uh cons campaign address we just say params do address so let's uh see now how we can interact with this given campaign so the first thing to do for sure as we as we did before we need the uh the wallet client that we were retrieving before using the the wag Muk the use wallet
client hook of course because we are going to execute a function on a smart contract and uh the functions that we need to develop are several the first one of course is the one to donate then we need also to check to get the uh the address of the user using use account we get the address of the user then we can check for example yeah using this this value we're going to check if the user is the admin of the of the campaign or not then what are the other functions we have the
donate for the for the user we have the claim as well so we have the claim function we need to implement which is for admin only and that's it basically that's also the withdraw but the withdraw I don't actually remember what it does so it checks for the address it checks that it's that the goal was not reached okay so the the withdraw function is is going to be used whenever the the goal has not been reached but the time has expired so
we're going to create this function so anybody can donate so we don't need to do any check over here and so the Donate takes an amount so we're going to to create the uh donation amount set donation amount as we were doing before we're going to use a use State we're going to populate it with one as default and then as before we say par set donation amount we need to bring the donation amount to uh six digits as we were doing before for usdc and then we say transaction Ash is wallet client do write contract and then we put the ABI in this case we are not
going to use the campaign Factor ABI but we're going to use the campaign uh ABI we're going to pass the address uh the campaign address as a 0x string because that's how VM wants it and the function name is donate and the last thing we need to add is the paret donation amount this is how users can easily donate using um using our our app we also need as we were doing before the public client so we say use public client again and then we get the transaction receipt which is
await public client. wait for transaction receipt Ash and we pass the transaction Ash so as be as before we we log the transaction Ash so we say donate Tash uh yeah we need of course to to check if the transaction exist otherwise it's going to cry and we say as well donate transaction receed so as before we're going to create this really simple section that we can copy from here so we going to get this one and put it
here maybe if it if it wants to collaborate with me it should be easier so um pretty easy as before we have we have the campaign over here and we can put the campaign address maybe shorten so when we go inside the page we should see like this this is the address of the smart contract that we have over here which is the campaign that we created then pretty simple then we will go and pretty things up uh we can copy this field over here which is an input field with with a number and we're going
to change these values to donation amount and set donation amount donate and we say we call it usdc of course we need to import this from the next UI we should see it right now donate one usdc and then we recreate the button that we we were doing before which is the Donate so uh we we also set it to Max vws vuul so it's uh EXs bu okay so pretty easy pretty simple and
[Music] um I this is going to fail and I'm going to tell you why in a second so if I click on donate this is going to prompt the transaction but if I send if I sign and try and send this transaction it's going to actually fail why because I did not approve uh the usdc I I I hope that it's going to fail because otherwise I'm going to it it should fail because uh I did not approve uh usdc spending and so it's not the the smart contract the smart contract is not going to be able to um pull the usdc
from from my account so let's wait for the transaction to fail because it's going to fail soon hopefully as you can see we get an error over here which says which says erc20 transfer amount exceeds allowance what does that mean is that as I was saying before I did not approve the uh campaign smart contract to spend my usdc so that's why the transaction reverted so before donating we need the user to approve the amount of usdc that he wants to spend and so what it's pretty simple to do what we need is the uh erc20 ABI uh which we can retrieve pretty much
everywhere online and I think also there should be one yeah there's also one already available on on VM so you do not need to basically import it in your code you already have it and then we need the uh usdc address the usdc address it's available in the in the rmy it's this usdc over here you can get some usdc on sepolia using the faet defos it from Circle you I think you get like 10 usdc per day or per hour I don't know so we go back over here and we say export const um usdc address and we paste it
here and then we uh are going to perform the check so we're going to say um the first thing that we need to check is the allowance so if I have any allowance or not so we say allowance is equal to in this case we are going to read from a smart contract so we use the public client instead of of the of the of the wallet client so we say read contract ABI we're going to pass the erc20 ABI as an address we pass the usdc address as a function we are going to call the allowance function and args the first argument is the address of the
user we're going to put it like this and the second one is the campaign address as a0x string so this is going to return the uh the amount of allowance that the user has for that that the campaign contract has for that given user so if you log the allowance and quickly comment this so that we do not go into the transaction again if I click on donate allowance I have zero of course I I need to spend one westdc the allowance is zero so we are going to check so if if the allowance is um lower than the amount of money that we want to donate we are
going to create the uh approve transaction so we say approve TX Sash and we're going to do the same thing that we did with the with the Donate so we use the wallet the wallet client and the write contract function so we are going to say the ABI again the rc20 we're going to say the function name is the approve uh the address is the usdc address and then the ARs um we have the campaign address of the address of the spender and the par amount uh just let me if allowance or if not allowance okay and uh so this is going to create the approved transaction but
of course we need to await also this transaction otherwise we um other like we need to wait for the approved transaction to be confirmed before sending the Donate one of course because otherwise the Donate uh transaction is going to fail again so and we say approve transaction received is equal to public client. wait for transaction received it's basically when you develop FR 10 for web three apps you use almost every time the same functions but they're really easy to use so and we need as well to check if the approved transaction Ash exists and we're going to log it we're going to uncomment this part here let's
reimport this one let me change this with this because it should be why it's not finding it config whatever let's leave it like this if it works don't don't change it so so let's see the uh since this is a this is a custom network of course the rbit stuff it's not that that beautiful but this is the first transaction that is two as you can see is uh 0x1c which is different from the address that we have over here in the top so we are actually interacting with the uh
usdc so we are approving basically so I I click on confirm this is the transaction Ash of the approval so while this goes now it's waiting for the transaction Reed of course as I was saying before when the transaction receipt comes we can perform the donation and spend our one usdc as you can see that was the this is the approved transaction received and this is the second transaction that the user has to do and it's going to go to the 0x9b which is the campaign contract so if I'm going to click sign I'm going to click confirm this is the second transaction that we can open from
another page so hopefully this is finished as you can see the first transaction was an approve with spender and value you can also decode the input data from here as you can see the spender will be the campaign and the value it's one with six decimals so we have 1 2 3 four five six decimals so that's one and um the second transaction hopefully has finished and as you can see now it didn't reverse so it went perfectly we uh donated one usdc to the campaign and uh we received uh ANC 1155 with token one uh because we
donated to to the given campaign so as I was saying the next steps are are um we did the campaign that's the donation what we what we could do right now is um work on the metadata of The Campaign which so that we can add maybe images to the to the to the campaign we can add a description and stuff like that and uh we're going to use nft storage I think Frank wanted to use so this is a pretty simple product from filecoin I think should be from filecoin
so let's log in with gab whatever get my data so you need to agree okay so in nft storage basically uh what do you do is you upload uh files you upload uh images whatever basically and they give you this this uh URL which is um stored on epfs we are not going to use the the UI but we're going to use the nft storage uh library from npm it should be nf. storage this one amazing upload functionality no
longer functions okay so scratch whatever I said we cannot use NFD storage anymore more amazing I cannot believe it free classic appro okay so finally they decided to make some money so instead of uh using nft storage we can use uh I think it's called dpfs JS Maybe need to find a a library this is eight years ago uh let's
check epfs um typescript mpm let's see if there's something that we can use upload epfs GS let's see npm there are some component let's see what library is using so he's using web three storage which was nftd storage before no this is another one let's see if this is
free amazing five five gigabytes amazing we can use this one so I'm going to use my email we're going to register and use this web threee storage instead zero you think it's normal okay whatever so I need to subscribe for using zero money uh do you know any solutions for the EPF
storage that we can use that does not require a credit card third web let's see if maybe third web loves us and we can use third web if they use the CLI we can use the SDK okay may maybe maybe let's hope we can use third web it's the third one let me log in with rabie can connect we're going to sign a message Jesus
Christ it's insane I let me check on the on the phone let's see if this is free or not because otherwise we are going to not upload anything sent to the yeah yeah I'm checking it on the phone I have it I have it here hopefully iCloud and of course it's not theing I should have already an account
maybe yeah yeah C CTU gfz let's see amazing so finally we can use this stuff so um blah okay so we need to install this this storage let's see from the docs which is the it's just third web storage I think third web SDK so it's just third web maybe let's see if we okay yeah third web
so we're going to install third web in our app and we say B add third web okay so we have third web now installed on our app so we need the third web secret [Music] key we're going to call it it waro campus let's create next yeah yeah that is will deny request from all orig okay amazing so we're going to create a EMV
file if maybe I can call it let's say third third web uh secret key this is the key let's also copy the client ID third web client ID and paste it here okay yeah I securely I'm just streaming it so it's completely secure so um we need to create our third web client it seems so uh let's go back to the create campaign page which was here I think yeah so instead of having the metadata URI here we're going to change and we're
going to create um we are going to give a name a description to the to the campaign so it's more readable so we're going to change the create campaign page and change and say uh we want the campaign name we're going to want also the campaign description as well uh campaign metadata we can remove this we are going to remove it surely we can give it a name we can give it a description yeah let's let's make it easy then we can add something later so we can say campaign name here and set campaign name this is going to be campaign name and we can do also a text area is
required we can call it campaign description okay so this is pretty easy and we should see wow that's insanely large okay so now we have the name the description maybe we can change the order so that the name is the first thing that the user inputs okay so we have the campaign name the campaign description the goal and the duration in days so what we need to do is upload this file to third web before we call the uh create campaign function so we need to create an object and we call it
campaign metadata and it's going to be name campaign name and we it's going to have a description which is going to be the campaign description and following the third web stuff we need to create this client so this is not really secure because maybe we need to do this on the back end so we're going to create an API which I think it's better because otherwise we're going to expose our secret key to all the users so we're going to create an API we're going to call it upload and it's going to be a route.
TS and um it's going to be export I think function post should get a request and a response I think I remember does need to be from okay so from the body we're going to get the campaign name the name and the description are going to come from the rec. Json which is the body that we're going to send to the request and um then we're going to create this third web client which should come
from the third web package so create thir web client okay I don't know why it's complaining right now it's not a secret key okay because we need to pass it like this let me see if the third web secret key okay so we have the third web client right now so we can go back to the EP epfs upload page so we have we're going to create this really simple file which is uh by using the upload so we're going to get the um campaign
URI it's going to be a wait upload which will come from import upload from third web storage and this is going to get the client which is the third web client and then the files the files instead it's just one file uh we are going to generate a random u u uu ID so a random identifier we can add the this package and we're going to call it um uu uh it should be from import V4
from U ID this is going to generate a new file a new j a new file name basically and then this should be the I no this should be the content sorry so the content should be here and this is the name of the file uh inside the content we we are just going to stringify basically name and description and so put the content of the Json file as a as a text and this is going to give us back this campaign URI which is a string and then we can say um rest. Json campaign URI should be maybe this one's a next
response maybe don't know why the linter is not working but next response from next should be from next next API response now it should be response and Jason like to reest I don't know what what is going on ah ah yeah maybe it's
but yeah okay that's that's why so okay so we have this pretty simple API we pass the name and the description of the campaign we upload the file using third web and then we get back the the URI so we're going to use fetch in this case so we say upload response is going to be a fetch Call to um it's going to be two SL API SL upload then we need to pass the method which in this case is post we need to pass the body which is the uh campaign metadata
then we need to pass some adders as well because we want to pass the content type as uh application. Json then we have the upload body which is going to be upload response. Json because it's going to return us as Json as well and here we we know already the name of the field which which is campaign URI so we get this campaign URI and we pass it inside the uh create CP uh function so let's see if everything works hopefully uh and the upload also something we can do is extend the duration of this call so let's make it that it lasts at least 30 seconds the
problem with epfs is that it's really slow and so sometimes May block your uh your request if if you have some timeouts so let's call it uh urbe campus campaign this is a campaign to fund a new urbe campus let's put a Campa goal of 25 usdc and it's going to last for two days so let's see on the network we should see the API calls flying so as you can see it's going to make the first API call which is the upload which returned an epfs URI which is this one and then it opened for me the rabbit wallet to
perform the creation of the campaign so I'm going to click sign I'm going to click confirm and when the transaction finishes I think I removed the logs no we have the ash over here if we go and check the transaction until it finishes we also now have the data and we can see on third web as well if we go on our dashboard which was here the storage dashboard hopefully these are really old files but hopefully it should come another file I don't know if
they they already uploaded here or not uh where where was it I have too many tabs open right now so the transaction is finished went successfully we deployed this was the approval we deployed the the new campaign so if we go in the homepage we should have the campaign over here as you can see so I'm always the admin of this campaign let's change this homepage because it's really ugly like this instead of having it like this we can put it as a grid grid calls three
maybe okay and we say m MX AO maybe no pretty ugly but it's going to put them on like three columns instead so let's go into into the second Campaign which because it's the it's the one with uh with the epfs metadata and um what we need to do now is retrieve that metadata and show it basically so what we need to do is when we enter the page when we enter the campaign page we need to perform a call to the to the smart contract to retrieve the to retrieve the
metadata so we say when we have the campaign address and when we have also the public class client because the public client need to be initialized if not public client return we can add also a loading variable which is always useful so I'm going to say use State Boolean in this case and we're going to say fetch uh campaign metadata this is going to be the function that we are going to create here we we can say if we are loading we are going to show like uh there should be I think a loader or something but for now we're
going to leave only this the address of the campaign in this case so we need to develop this fetch campaign metadata function which should be really easy to do what we need to do is uh read the campaign metadata from the smart contract so we go to the smart contract and there should be why I'm going there when I have it over here we should have in the campaign the CP metadata variable as you can see it's available and it's public so we can read it so reading variables on Smart contracts from a frontend is like
calling that function basically so we set the loading as true when we start the the retrieval of the of the metadata and we say metadata data it's equal to public metadata URI it's equal to public client. read contract that's before we pass the campaign Abby we pass the campaign address the function name is uh as you can see we have the autoc completion which is really useful we say CP metadata and it does not takes any ARs and over here we have the metadata URI we set loading to fals and we can say metadata UI and we should
have it logged inside our page if it did not explode yeah uh metadata URI this is the epfs metadata URI of our Json but of course this does not work on the on the browser because it does not recognize the epfs protocol so when we get this this UI we should be able to retrieve it back um we should be able to retrieve it back and we can use this stuff so we can create another
route and call it Download route. ts I'm going to copy this stuff over here instead this is going to get the campaign URI we're going to uh use the download function instead the download function wants the URI which is uh the campaign URI that's incoming this should be the Gateway URL and this is going to give us um a better URL I don't know this is this is just text okay
and this should gave this should be the download result instead sorry because inside the download result we should have the Json yeah because I want to get the the Json result so uh name description we get the Json hopefully this should work and then we get back the name and description by calling this end point so we do the same thing that we did before so uh we do a metadata response we call again the fetch but in this case we are going to call Api download going to pass a method as post the body we pass the metadata URI
but I think that we called it campign URI which is equal to metadata URI and then the adders are again only the application Json so we get the name and description I'll wait from the response and then we can set the campaign metadata to be in this form of name and description this is my wallet I don't know where it fell and um we can set the campaign metadata to be equal to name and description so when it finishes loading instead of showing the uh uh the campaign with the with the with the
address we can show The Campaign metadata dot name for example so if everything went well when we enter the page it's going to change to urbe campus Campaign which is the data that we stored on um on epfs and we can do the same also as well with the with the campaign metadata do description and this is going to show also the description of the uh hopefully the description let me see what's the return value over here if it's not description but
whatever let's see consol the Lo the name description I need to remove all these errors because I Al so it's undefined so maybe we did I did something wrong on the when uploading them name and description let's see I pass campaign metadata ah okay yeah I called it description amazing that's why it's not reading it so let's do a little a little fix description description and say description is description or
description hopefully this should work it's a pretty work around I don't know if it's getting it or not let's see wise whatever comes from here we say rest and rest let's see on the API instead what's the result I only stored Urus campaign of course because here we we didn't fetch the description okay so this campaign does not have a description right now but we can create another one with a description and it will work so let's revert here and let's keep the description as before uh I fix it here now description
yeah there's no description anywhere okay so uh we also stored the data uh we can now create a better campaign that hopefully works even more so uh we can call it I don't know uh test campaign I have no fantasy whatsoever uh this is a test description for a campaign uh developed during it or we're going to change the campan go to 17 usdc and the duration to 5 days we're going to it create and we're going to do the same thing that we did
before I'm going to sign and confirm this is going to create the transaction for us we can close all this stuff because we don't need it transaction should be executed no not yet okay so it's finished we can go back on the H page and we should see three campaigns so this is the third one if we go onto the page we see test campaign and we see the description this is a test description for a campaign
developed during it Wars uh we can also read other data of the campaign we can also read uh not only the the metadata uh URI here and we can also read for example you you can change this one to uh a promise all and so it's going to execute them execute all these uh functions that we pass in in parallel basically so we want to retrieve the metadata of the campaign we want also to retrieve uh the the goal as well which should be um Target amount so we say
campaign goal and we say over here public client. read contract here we say campaign Abby address again the campaign address and the function name it's called CP Target amount so we get also the goal uh we want to get the admin as well to check if we are the admin or not so we can pretty easily copy this function and just change this to CP admin and we can also set check the comp campaign end so campaign end remember these are in order so I'm adding I'm adding them in order so the
first one retrieves the admin the second one the goal and the third one the the end we're going to update this one and say that the metadata contains also the string the goal and the end the goal is a begin the campaign end it's a begin as well and the admin instead it should be a 0x string if I'm right no it's just a string whatever so let's change this set data here so we have goal equal to campaign goal and so on of course these values need to be populated otherwise the um campaign does not
exist so we can update the page so this is a test description for a campaign blah blah blah we can also put a field and say for example uh span we can put it in bold and say gold and put the campaign metadata do go usdc uh let's say or zero maybe why is it complaining number or begin ah because it wants it like this and this should be to
string and as you can see even if we passed 17 as usdc when we read it back from the smart contract it has all the six decimals as well so we need to instead of passing it now we need to format it back so we need to call the format um units function we pass the campaign campaign metadata. goal and then we pass Six this exist thank you so much something B where something bong sorry
uh. it's giving some error somewhere let's check what is going on cannot read poverties of undefined reading two string campaign page data. go it's a big in we pass it as a big in can read properties of undefined so campaign metadata is undefined actually if loading or not cment data Maybe maybe there's
some call that is oh no okay now it works magically so we pass it back to uh 17 usdc uh we can also check um as we said the the end so the date when when it's going to end so we can say end date we're going to uh we're going to say new date from a number which is the campaign metadata. end we need to put it in um we need to put it in milliseconds and we need to let's say
[Music] to local dat string there's a okay and I don't remember how many days I did put but hopefully it's right I don't remember if it was five or something else so we also have the end date of the campaign and also we should be able from the smart contract to see I know okay we we we cannot read we cannot read directly from the contract but what we can do is we can check the balance of this uh of this of this campaign to see how many uh usdc have been sent to this address so we can add um another value which is
uh amount we can call it and it's the amount of usdc that have been sent to this address and we can say uh campaign amount we can perform another read call to the uh in this case not not to the campaign but to to the usdc address we want to check the um this is wrong this is rc20 ABI we want to call the balance off with the campaign address as an argument and then we're going to set the amount equal to the campaign amount and blah blah blah it's over here
hopefully it works okay and we need to show it over here to see how many usdc have been donated to this to this account to this to this campaign and in this case of course it's zero now I'm going to donate to usdc to this campaign if I click on sign and confirm now I'm approving like before the spending of my usdc and hopefully this is the second transaction instead which is the donation we should have the ash of the what is this stuff this is the Donate Ash when the Donate Ash finishes we can refresh the page and see the
amount of usdc that have been donated it should change to two basically when this finishes okay so I donated two if I refresh the page now this is wrong it's not gold but it's uh received donations received as you can see now donations received as two usdc in it basically so yeah the other functions like the withdraw and the close one wouldn't make sense to implement right now because we would need for a campaign to wait like like to to to end but I think that you
get the point on how like easy it is to interact with smart contracts like using VM and and wagi as well and um what else we can do we did the metadata as well we did the third web what else can we do the functions are there we we can do well of course the the the withdraw function and the claim functions which are only admin uh you need to protect them from a from a front end point of view so you can you need always to check if the
connected user is the current admin of the of the given campaign so always check that and make sure also to always uh check the the decimals of usdc or all the addresses basically when you send any amount also when you send eth you always need to send the amount in 18 decimals as well so yeah I think that's pretty much it if you have any questions thank you [Music]
Related talks
- Web3 Frontend Development, Part 2
- Web3 Frontend Development, Part 1
- Building an Onchain Crowdfunding App: Smart Contract
- Who Can Turn Off Your Ethereum App? A Full-Stack Walk-Away Test
- Building the Data Layer for Ethereum's Institutional Era
- Ethereum and Internet Computer: World Computer
- Solidity Development with Foundry: Cast, Anvil, Chisel, and Forge
- CODE IS LAW? Smart Contracts Explained (Ethereum, DeFi)
- Hackathon Tips and Tricks
