Building an Onchain Crowdfunding App: Smart Contract

Urbe Campus ETHWarsaw Edition 2024 · September 2024 · 2:55:20

Urbe co-founder Frankc teaches Solidity smart contract development by replicating a Kickstarter-style crowdfunding app for web3. Campaigns accept donations in USDC on Base, track donors and funds raised and support optional perks, and the session builds the contract step by step as a project suited to a hackathon submission. Part of the Urbe Campus ETHWarsaw Edition.

Speakers

Topics

From Urbe Campus ETHWarsaw Edition 2024, hosted by Urbe.eth.

Transcript

[Music] [Applause] [Music] okay let's go so hello everyone I'm Frank and as lion was seeing before I'm I'm a proud Ur member one of the first one and um uh and a web Tre

developer and today we will start uh with solity smart contract development for this Kickstarter like uh application okay so why because uh when we were thinking about uh this uh Ur campus we wanted to develop during the campus um an application that can be a good uh submission for an aaton okay so for example for E wor of uh and why um a cofounding application because it's pretty easy to understand uh because a web 3 version is very similar to the Web

Two uh classic version of a craft founding application and also because it's easy uh the smart contract part okay of this uh of this application and um so it's easy and is a good way to to understand and to learn more about solidity and Spar contract development so let's start uh with Kickstarter okay we want to replicate something uh similar to to this one okay so if we go to Kickstarter we can see that we have different projects that basically are different campaigns and if I click on a specific campaign I can see uh here a button to donate some money to this

campaign uh I can see the number of donators donors donors uh I can see how much money uh this campaign uh has reached and I Can See For example how many days are left uh for the for the end basically of of this campaign okay and then here I can see some metadata so a description of this project uh so text text text and here I can see also uh some uh let's say some per perks that I can choose when I donate okay so for example I can choose to to donate 7 in order to buy this product or I can I

can choose this one for 15 EUR or I can choose this one for uh 25 EUR and so on or I can decide to donate without taking anything back okay so if this is clear uh what we are going to do is to try to replicate um in an easier way of course uh Kickstarter okay for web free what does it mean for web free uh it means that basically we uh we will have an application where people can donate in usdc on Bas network uh they can donate for one or multiple campaigns uh and they can uh receive back uh for example an nft

representing their positions or Their donation um so that the nft can maybe can be transferred after the after the donation so let's say I donate for a specific product on a campaign I receive back an nft and maybe I can trade my position on a on a let's say nft market place or whatever so this is the main idea so to today we will start with the smart contract part while tomorrow uh Paulo uh will try to replicate a little bit this frontend so is is everything clear

okay okay in order to start with the smart Contra development uh we need art do so our smart Contra project will be powered by AR do so do you know our do there's someone that doesn't know our do or never use our DOT okay okay so in in a few words uh our do is a very simple and familiar uh framework to to write compile test and deploy uh smart contracts so solid smart contracts okay and the cool thing about our that is that uh is easy to

use uh all the test deployment and whatever is written in typescript or JavaScript while the smart Contra of course are in solidity and the cool thing is that the there is a huge Community uh behind hard Dot and there are a lot of uh plugins developed by the community that you can import and use in your project okay so even if something is not uh supported by design by hard do we have the a lot of plugins developed by the community that we can uh we can take and use in our project okay so let's start and I think uh while using our do will be very clear

and very easy to use so the the first thing is uh I have um uh in this case is csor but I can use Visual Studio or whatever editor uh the first thing to do is to create an empty folder so in this case Ur campus 2024 open the terminal and the first thing to do is to install uh hard do okay you can install with npm or you can you can install it with yarn whatever so I already have hard do installed uh if you have to install hard do please do it right now after we have hard do installed we can we can start uh creating an. project

how we can create an. project with this simple command here so NPS hard hit okay now hard do will ask you will ask us uh which kind of project to create so a JavaScript one a typescript one a typescript with BM because by default it uses uh eers or an MTR dot so without uh VM or or uh eers so in this case I will use um typescript and I suggest you to use typescript uh in this case I will use eers but it's pretty the same if we if

we choose to to use BM so let's click on this our. project route in this case is my folder so ampus 2024 so now it asks if we want to add a git ignore that can be useful when we push on on GitHub so we uh if we say yes we already have a g git ignore created by our DOT so we don't have to create later a g g ignore for our giab repo let's say yes and now our data ask us if we want to install all the dependencies needed uh to start an.

project and let let's let's type yes so right now it's basically is installing uh all the dependencies all the things that are needed to compile uh a smart contract code uh but also to to test it to deploy and so on so let's wait a little bit some word

okay so we successfully created uh our. project and if you do it you should see a contract folder an ignition folder uh along with some node modules that were installed um when we sold the dependencies before and also we have an n. config typescript file that we will see later okay so once we have our hard do project uh initiated we can start basically to use our DOT so these are uh let's see okay this here I available task are

some um relevant commments that you should know if you want to uh develop using a hard dot basically okay the first comment that one of the most useful is npx hard. compile okay this command is used to compile uh the contracts under the under the folder contracts here okay so in this case under the folder contracts we we can find um lock. Sol contract that is basically the simple smart contract that is created when we initialize the first time hard do okay if we type I said before npx hard. compile we can

compile our contract okay it means that if we have some errors in our in our uh smart contract uh the compilation of course will fail and uh it will tell us uh the problems that we are facing while compiling the contracts another uh useful command uh that we can use in our do is npx hard. test okay in this case uh we are basically launching the test under the folder test so in this case is this lock. typescript file and we will see later other uh useful commments okay but right now just

to start the most important one is npx r. compile okay I already compiled the lock. Sol smart contract so if I try to compile another time it will say nothing to compile no need to generate any newer typings because I already comp successfully compiled the the smart contract okay so now let's clear a little bit our terminal and we can go deeper and start uh with our smart contracts so do you have any question any question about our do initialization

whatever everything clear okay so that's cool and I think we can start with the development of our Kickstarter like uh smart contract application okay so the first thing to do is to create another contract here under the folder contracts that we can call let's say campaign so and we can delete the lock one okay so right now we have the campaign do s contract that there is an empty smart contract and here we can start uh we can start typing and writing

some code okay the first thing to do is to uh write this uh this line that is basically um the the license that we are going to use in this smart contract so this is not mandatory but if we don't select if we don't write a license we will have some warning so it's always better to uh specify um a license for the smart Contra so in this case it's the uh MIT um open source uh license okay then the the first thing to do in order to create our smart contract is to

type pragma solidity and the version that we want to use of the solidity compiler okay so in this case we can simply use the 0.8.0 uh that these are relative old uh version of the compiler but is is very uh solid let's say and is uh is good enough for for what we have to do right now okay so two two things the license and the version of the uh solidity compiler okay now we can start to um create a contract okay so uh

contract campaign and then we we create an empty contract right now okay so contract is the KW in order to define a smart contract in solidity and then we have the name of the smart contract so in this case it's campaign okay so in this case my file campaign. s has has the same name of my contract within the the code but we I can also write a different name okay so in this case just for uh just because it's easier we can CH we can choose the same name for the file of the smart contract and the for

the contract name itself okay so we have a compiler version we we have our name and we have our license and right now we can start uh developing our smart contract okay now I want to um I want to think uh with you a little bit of how how our contract should work taking as a reference Kickstarter okay so we said that we have different campaigns so here we can explore different products where each product is

a different campaign okay so we have different campaign each campaign has some uh parameters okay that are Dynamic so each campaign has some amounts of uh of funds received as a number of donors uh some day some days left uh for the end of the campaign and so on okay so we will start uh writing our smart contract defining what is a campaign okay then of course we can uh modify it later if we need but let's start with some basics okay so the first thing and um the first

thing that I uh that I want I want to do is to define a struct a struct called campaign okay this campaign struct we have uh some okay yeah of course I can't um let's call it CP okay CP is campaign okay we will use CP in all our code to uh to say basically campaign okay so what is a campaign a campaign as in my opinion as an admin address so a wallet address that is capable to manage the campaign

so for example in uh in Kickstarter uh we should find who is the creator of this campaign some way in some way okay yeah for example here so in this case this campaign on Kickstarter was created by this Kudos editor that is an Italian uh publishing house so let's try to replicate it so we we will have an admin in our struct we we will have some metadata so basically this text that can be a string and we will call it CP metadata okay this metadata is a string

I will uh explain it better uh later but basically we will save on ipfs that is a distributed storage we will save all the metadata that we can find in a campaign page and this string CP metadata is basically only the reference of the URL of uh our our metadata on ipfs but we will see it later um for now is enough to understand that we will have some metad data represented by a string okay then what we have here so we have a goal for sure I'm pretty sure that we have a goal some

some way uh let's see where we can find the goal let's see on another campaign okay here this is Italian uh but it says that uh is All or Nothing okay so on Kickstarter each campaign can start only if the uh goal set by the Admin is reached okay so for example if this uh um if this campaign uh was set um with a

goal of I don't know 100K Euro it means that the admin of the campaign can take that money and so the campaign can successfully end only if this campaign reach uh this 100K Euro for example okay so we will follow the same uh pattern okay so we will um we will um we will have a goal to reach in our in in our campaign struct okay this is perfect tar Target amount let's call it Target amount so this is basically let's write some comment so this is the admin address uh admin address that can manage the

campaign this is the metadata of the campaign that we will save we will store store on ipfs and this is the target amount of the campaign uh to reach so if we don't reach this goal the campaign can't start and the donors can take back their money okay this is the logic that we want to implement okay so what else we have in a campaign uh uh uh let's see yeah it's just a ZX address so it's an

itum address okay it's an address that we will see later but that can call specific function in a West smart contract in order to manage a campaign okay so for example let's make an example the admin can pose the campaign even on Kickstarter an admin can POS a campaign okay for example uh if an if a campaign admin thinks that uh it it doesn't want anymore to to have this campaign running okay he can post the campaign and when the caign is posed all the donors can take back their money okay so for example in our smart contract we will have a post function

that will be called only by the Admin so that by this address admin uh or for example or maybe he can uh he can have other privileges so he can call um other function in our smart contract okay so do you have any question we are basically trying to Define what is a campaign okay taking this as reference and put it back in our smart contract so we said that uh our campaign as an admin a metadata and a Target amount then for sure our

campaign we will have some a start date okay because uh I can create a campaign that can start later maybe okay so we can we can call it CP start okay this will be CP Target amount and this will be CP admin okay and this is the start time of the campaign perfect then of course we we will have a campaign end so uh our campaign should end some way okay so we said that our Target

amount is the amount of uh usdc in this in this case that we have to reach in order to successfully close a campaign while this CPN is a a parameter that basically will tell donors if they can donate or no because if the CPN is reached it means that donors can't uh donate anymore okay what else any idea about met includes yeah all the metadata that we

can see on a Kickstarter page so image this text here description what maybe yeah yeah okay what else do you think should be in the in a campaign yeah uh this is not something that we have to Define within a struct but we have to Define in uh in the logic of the

function in the smart contract okay so a struct is just um so when when we write a struct it means that we we are taking all these parameters that we are defining here okay so a campaign is defined with all these parameters together okay then we will use this campaign struct so these parameters uh within a business logic of course that we are going to implement in the smart contract so for example do you think that we should have the donors should we track the donors for example of a campaign I would say yes

but I will teach you a a trick to better uh manage the the donor's part okay in order to save gas to be more efficient and so on what else so for example we don't have the amount donated so far okay we don't have it other parameters let's say so this is the donors this is the amount collected this is the hours left this is the button to the Nate uh these are just metadata metadata

metadata and this is um a way to buy this product basically okay so it mean it feels like we uh we have all the parameters need needed in order to define a campaign okay now we can start to use these campaigns in some way okay so so let's start okay there is um there is something that that is missed right now for example I said at the beginning that we want to uh accept usdc only okay so we should

Define what is usdc okay so usdc is defined by an address okay that we can put public and we call it usdc address okay so in this way we can Define what is usdc okay then so this is a struct while these are storage variables okay uh I want to Define also is the campaign is active so is the

campaign started or not and also I want to Define if the campaign is ended or not okay so I can let's put a com a comment and write campaign status okay so this Boolean value will be true when the CP start is reached while this Boolean will be uh true so is ended is true if this CPN is reached okay so this is the usdc token address on base

Network okay okay let's start so the first thing that we need is a way to initialize our campaign with these parameters okay because right now these are just empty parameters okay so if we see if we check these parameters right now we will see that usdc address is empty is active is false because all the Boolean parameters in solidity are false by default so if not initialized as true uh will always be false so this is will will be false this will be false this

will will empty this will be empty everything will be empty so we need a way to initialize our campaign parameters how can we do it let's define a function that is called init okay what we have to Define in this uh init function we have to Define these parameters so for example we have to define the address the usdc address we have to Define for example the uh address of the CP admin we have to define the string of the metadata we need the keyword

memory for uh for a string in solidity okay so what does it mean uh it means that um for memory we have to specify uh what kind of memory we we want to use okay because in solidity we have three different kind of memory we have memory we have uh storage and we have C data okay so the storage one is a persistent memory okay while the me memory and C data are temporary memory okay in this case we are defining this CP metadata as memory because later we we are going to uh

override this parameter that is a storage one with this memory one okay so we need just temporary memory for string because we are going to override the storage one then let's put uh all basically all these parameters so the CP Target amount the CP start and the CP end okay and we can put uh and we can put this function as public do you know the difference in uh inv visibility for functions in solidity so we have different kind of

visibility in solidity We have basically we have four kind of of of visibility we have public we have private uh we have external and we have internal okay so in this case uh so basically internal is quite easy because it means that an internal function can only be called by another function of our smart contract okay external means that the a function can only be called by an external um an external smart contract or an an external operator okay so external from this smart contract

okay uh public is basically can can uh it can be called by anyone um so it means that it can be called by external operators or internal ones whatever uh in this case we need public because we need to initialize from the external we need to initialize our smart contract okay so who will initialize this contract basically the admin that will create the campaign okay so the the admin will create a campaign and it will initialize the campaign according to uh its needs basically okay so how can we

initialize uh some parameters in a smart contract we we wrote an init function and then it's super simple we need to override basically our parameter in storage with the parameters that are inputs in our function okay so in this case usdc address so this one will be equal to underscore usdc address CPI CP admin the same thing metadata Target amount start and end okay as we can see we have some

errors because in solidity we can't uh write we can't write directly on a parameters if this parameters is part of a struct okay so for example we can s successfully override usdc address because Us usdc address is a um is a storage variable outside from our camp struct uh while all these um parameters are uh within our uh compa struct okay so basically now we we have uh two

options the first one is to take these parameters and um and and make these parameters as ex as um storage variables so for example uh address CP admin can be uh parameters within the struct or can be a um a variable in the in the storage okay or we can initialize uh uh we can initialize the struct basically so the best thing to do in this case is to

uh write all the parameters that we previously put in the struct as a storage variables okay I will explain why so in order to campaign details and then we have address public CP admin we have string public that is the P metadata we have wi 256 that this the target amount we have the CP start and we have the CP end okay and we can basically delete all these things okay

why so why uh choose a why I should choose um a struct and why or why I should decided to have all the parameters as storage variables okay so with the design that I I thought uh when thinking about uh this campus in my idea each campaign will be represented by a separate smart contract okay so when I create a Kickstarter live campaign I will automatically deployed a a new smart contract okay for for a campaign okay so in this case this

campaign do Sol contract will basically store just one campaign okay if we need to have multiple campaigns okay because we are we are building a platform for creating campaign so for sure we we will have multiple campaigns we can uh we can do it in two different ways Maybe three but let's say true so the first one is to define a campaign struct as said before and then have a mapping that will map each campaign to a different Ed okay so that we can store a lot of campaigns multiple campaigns within the same smart

contract and then each campaign will be defined by a unique identifier okay the alternative that is more elegant let's say and a little bit complex and is it's my choice is to use a factory do you know what is a factory so basically a factory is a smart contract that is capable to deploy other contracts so in particular is a is a smart contract that is capable to clone a smart contract and to deploy different clones of the same contract okay so what does it mean it means that we are going to define a campaign smart

contract and then we will have a factory that will deploy multiple times our campaign smart contract one for each new campaign created on our platform okay in this way having just one having one smart contract for one campaign we can Define all our parameters defining a campaign within storage variables okay because what will happens is that I will deploy so our Factory will deploy multiple uh campaign contracts and each campaign contracts will be initialized with different campaign parameters so it means that I create my own campaign with these

parameters basically then another person can create another campaigns with different parameters and so on and so on okay is it clear the design because it's a a little bit complex uh but but if you understand the factory approach it can be really powerful and and pretty useful for your D development so I repeat another time we will have basically two smart contract the first one is the campaign doall uh containing uh made of uh all the business logic and all the parameters for a single campaign and then we will have a new smart contract called

campaign Factory that basically will clone our campaign do s contract and um it will deploy multiple times our campaign contract one for each campaign created on our platform so let's continue with the development of our contract so right now we are in the function in it so this function is used to initialize Storage storage variables of our campaign so basically we we will have to we have to

override all the parameters defining a campaign so SDC address CP admin CP metadata CP Target amount CP start CP end okay this okay while this parameters that is the status we don't have to initialize the status because the the status will depend on on the status of our smart contract is it clear okay so now we have our smart

contract that can be inish ized so let's put a comment comment here initialize the campaign okay now we have to create a campaign or maybe the admin of the campaign that is the CP admin should create a campaign okay so we need basically uh function to create a campaign we need a function to actually

create our campaign will be external because uh this function will be called by the the admin of the campaign when creating it okay and okay let's make um let's explain a step

before uh before the create campaign so we pre we previously said that a campaign is defined by these parameters okay that are initialized by when deploying a a new smart contract okay these parameters Define a a a a campaign so what is a campaign a campaign is made of these parameters so when we create campaign basically we need to define the parameters to uh we need to define the parameters for a campaign okay so what we we can do

here is a little trick that is this one so let's create a function called underscore create campaign with internal visibility okay so when we use the underscore before an function name it means that the visibility is usually internal or private okay so create a campaign and we will Define the same parameters defined in the init function

okay with the same exact input parameters okay and the trick here is that we can have an external function called create campaign accepting of course all the parameters needed in order to define a campaign and then we can simply call the underscore create campaign function so it means that this function

will take these input parameters and it will call this function that is the underscore create campaign and the underscore create Campaign Will initialize the the parameters needed for aamp campaign okay at this point we can simply delete this parameters here and just call the create campaign okay so what happens here basically we have a initialize function and then we have initialize function that will call the

underscore crate campaign and then a an external crate campaign that will call the underscore crate campaign so what does it mean it means that if we don't initialize the smart contract when deploying it through the init function we can later initialize this contract using the create campaign so we have basically a double Choice here it's basically the same but this will be called when deploying the contract while this can be called at a later stage okay now we have to be sure that we can create just one campaign for each smart

contract because otherwise we can override existing campaign parameters okay how can we do it we can simply create another bullan value that will be called easy initialized okay and when we create when we call the underscore create campaign we can put this is initialized parameter as true okay in this way we know that the first time that we call the underscore create campaign we are going to um put this initialized parameter as true and now

we need a requirement so we need that is initialized is false in order to call this function okay while because otherwise we can override the parameters while what happens here is that the first time that we call the underscore create campaign using the init or using the cre campaign the uh require is okay because is initialized will be false because by Design as said before all the bullan parameters are false and then we put it as true so the second time that we try to call the underscore crate campaign this requirement will not be okay

because we need that is initialized is false but we will have as an is initialized through and so our function will revert with an error and the error will be this one campaign already initialized now just to be clearer we can put uh okay so usually when we write a contract we have the uh private and internal function at the end of the code okay so first the external and public and then

the private and internal so we can create a comment function to create a campaign or maybe internal function to create a campaign used by uh used by create campaign function or init function so basically right now we are able to create a campaign any

questions is it clear okay so so we said that we can the admin can create a campaign but the admin can also pose the campaign okay so let's create a POS function so function to pose the campaign so we we have a pose campaign uh

Campa P post campaign function that will be external okay who can call this function the campaign admin only the campaign admin right okay so we need a requirement okay we need that the message sender is equal to the parameter CP admin that we previously defined here so this requirement is

okay or if we don't want to use a requirement we can use a modifier okay what is a modifier so so basically a modifier is something that we can plug on each function and uh each function that is declared with a modifier should um should follow this requirement

in order to be called okay so what what does it means it means that if we copy the only admin modifier and we put the only the only admin modifier here and we delete or maybe we can comment this requirement the result will be the same so if this function is a only admin function it means that this function can only be called if the message sender so uh the address that is calling the function is equals to the CP admin okay in this way using a modifier we can use the same requirement um on different

function in our smart contract without rewriting the same requirement multiple times so basically uh we can avoid to rewrite the same things uh different times okay so we can delete these requirements and so we will have a modifier that is only admin for the poost campaign function this one 21 this one no this one is just a syntax rule that you have to follow when

writing modifier so the modifier ends with the underscore and the comma in this way it's not meaning like make code after like this Dash you mean this one yeah yeah what is the question oh it's like it's it's meaning like go code down and run function with their original statements yeah yeah yeah yeah but but still with modifier yeah so the modifier will be checked when when calling the function okay so before basically before executing the

code that we will put there here okay some code here we are sure that this modifier will check um the requirement before calling it okay so what the post campaign function does the campaign pause the campaign yeah so we need we can for example uh create another bullan value that is called exposed and the post campaign just uh just uh isos as true just set these post parameters

through but before posing the campaign we we should uh check that the campaign has started because we can put a uh we can pose a campaign only if it previously started okay so we need a requirements that is is active campaign because otherwise we will have an error campaign is not active Okay we didn't Define what what is is active right now we will Define it later okay so right now we have a create

campaign function and a post campaign function so we can uh we can go deeper a little bit in the underscore create campaign because right now we are not checking anything basically on the parameters that we take in order to uh create a campaign for example the only thing that we check if is um if the campaign has already been initialized but it's not enough okay because because for example we should

check that the usdc address is different from address zero so the underscore usdc address that we are passing in the underscore create campaign is actually um populated address okay it's different from an empty address basically so this is the first check then we should check the same thing on the uh campaign admin so a campaign admin uh should be a valid and populated address okay

because otherwise if if this is not populated right now correctly so for example if this address is empty so address zero basically uh we will have troubles later for example in the modifier because no one will be able to call the uh create or post campaign because no one uh controls address zero so the CP admin should be an address okay another thing is that we need for example a Target amount that is greater than zero because at least one should be our goal to to to reach

then this is a good suggestion because we need also a CP start so CP start is the uh moment when we uh where when our campaign will start and our start point should be in the future okay so in the future it means that it should be greater than the block do timestamp because the block. Tim stamp in solidity returns the exact moment okay this exact moment so if we uh create a requirement that is CP start greater than the block timestamp we are sure that we are creating a campaign with a start point

that is in the future and at the same time we need that the end of the campaign is greater than the start of course okay so if we write CPN greater then uh CP start we are also sure that the CPN is uh in the future because CP start is greater than block timestamp and CPN is greater than CP start okay so right now basically we are validating the parameters that we are passing in the create campaign underscore create campaign function so in this way we are sure that we are

creating a a a valid campaign basically okay yeah so could say could you could you say again why we using bracket zero address address bracket zero so address zero is this one Z Zer X okay all the zero okay so in solidity when we when we write different from address zero uh it means that the address is actually an address so it's populated it be BN our mind yeah yeah okay and also the address zero

identifies ether e on ethereum and all the native coins on different chains so when we when we when we says that usdc address should be different from address zero we are saying that the address should be populated and our address should be different from it okay should be an nc20 token basically we cannot pass multiple conditions in re so we are using no no now we need different uh requirements okay

so we validated basically the parameters in order to create a campaign uh we validated and we wrote the post campaign function so now we can start to write for example the donation logic okay how we can donate okay so the create and POS are CP admin function okay while the

uh we now are going to create a donate function that is donor functions okay so for example I'm thinking that when we Define the campaign parameters we didn't set for example a minimum for a donation or a Max okay so in this way right now we can donate uh whatever we

want basically from uh one to infinite so we can decide to create here some parameters to manage this aspect of our donation or maybe not so in this case just keep it simple we we we will not Define some minimum maximum donation parameters but if we want to do you can pretty easily do it so let's write a donate function okay so this function will be used by donors on of our campaigns so the first thing

is that we have to Define how much we want to donate Okay so is a wi because it's a number and we can call it amount simply amount okay and we can create a requirement that um the amount must be what the amount must be greater than zero because we can't donate zero of

course okay and then in our business logic we can donate to a campaign only if the campaign has started okay and only if our comp campign has not been posed and only if our campaign has not ended okay make it makes sense I think so these are good suggestion so requirement is active is not posed

and the last one is that the block do time stamp so this exact moment is less than the CPN that we previously defined okay in this way we can be sure that our our campaign has started our campaign has not been posed and our campaign has not ended

okay so the next thing to do is to actually transfer usdc from the donor wallet to this comp smart contract okay how can we do it so we need to Define so do you know what is an rc20 okay no so an rc20 is basically a token that follows a specific standard defined by the rc20 standard okay so it means that all the existing tokens following

the Earth C20 standard will have some specific functions okay so we already know that an nc20 has for example the transfer function the transfer from function the approv function and so and so on okay usdc on base is a standard rc2 and so in order to write a trans in solidity we have to um use the interface of an rc20 that is defined by the ic20 keyword the IR C20 interface wants a an

address so the address of the rc20 token that we want to interact with in this case uh what is the rc20 is this us this address that we previously defined so we copy this uh variable name we put it here so in this way we know that we are interacting with an rc20 through its interface that is the irc2 and in particular we are interacting with this s that is this address defined by the variable usdc address okay now we can access the rc20

functions okay in this case how can we do it it's just Dot and then the name of the function on the rc20 that we want to use in this case is the transfer from okay the transfer from function now I will explain what is the transfer from function is basically a function of the rc20 that is used to transfer some amount of tokens defined by the amount variable from the message sender so the address who is calling this function to a recipient address that in this case is address this so the address of this Mar contract okay so it

means the address of this campaign contract Al so like uh why we not put Dash to amount of like uh because uh we when we putting uh like some variables to function we putting a dash sign to and after the name of function like why we put and the underscore you mean oh sorry underscore uh because uh in this case this this is an external function okay so this is okay so uh yeah just to be sure that we are on the same page we can delete the

underscore from the previous uh now let's let's make it in this way so we have the underscore here because this is the initialization and so we can't use the same name CP admin and CP admin so here we need the underscore just to say that are two different uh variables but if you want we can um we can delete the underscore here because this is an external function it's not an internal private or it's not the

initialization so let's delete the underscore here and here no roll back roll back we we can do it because because uh otherwise we will we will use the same name for okay it only question no it's a good question so basically if we Define some variables in the storage we then we can't use the same name for uh inputs um okay so if we if you want to delete the underscore you can you should call the these parameters in a different

way okay but right now let's let's stay in uh let's stay here okay so we were talking about the transfer from function of the uh Earth C20 standard so do you know open zapping yeah Okay cool so basically open zapping is open open source library for smart contract uh smart contract solidity code and is basically open zapping is the the standard for smart contract development okay so for example open zeppin defines

what is anc20 tokens what is NC 721 so nft tokens uh along with other um utility libraries uh that are used in in the solidity development okay so basically open zapping is the main reference that you should use When developing some contract okay so for example open Zeppelin has the uh developed the Earth C20 uh standard code and we can see here in the docs.

openen Zeppelin website we can we can see uh how uh the open Zepp lineer C20 standard works Works uh and if we go on API or C2 we can check all the functions uh that uh that are inside the earth20 token okay so this is pretty useful I suggest you to to study a little bit the open zapping contracts because is the industry standard basically so in order to

interact with an nc20 we need the interface okay this one IR C20 IR C20 then we need the transfer from function okay let's click on transfer from transfer from function moves amount tokens from from to to using the allance mechanism amount is then deducted from the Coler allance returns a bull and value indicated whether the operation succeeded and also emit a transfer event okay so the transfer from from function should be called passing three different parameters that is the from so basically the older the address holding the token

the two that is the recipient address of the transfer and the amount that the amount of token that we are going to transfer okay the cool thing um about the transfer from function is that the transfer from function can be called by the older of the token or by an external address okay if called by an external address you need to be sure that the address hold in the token has approved as previously approved the spending of that amount of tokens okay do you know what is approve how an approve

works so basically in the rc20 token standard we have a an alance parameter okay is this one so the allance parameter takes two input parameters that is the owner that is basically the token holder and dis spender okay so an exter an a third party address that can spend some tokens on behalf of the older okay so this function allowance returns a wi uh a wi that is basically a number and it uh it says the amount of token that the spender address can spend on behalf of

the owner okay how how we can increase or decrease this amount using the approved functions spender amount so owner is the owner holding the tokens spender is the is the address that can spend the amount of tokens defined by the approv okay so let's go back to our code I wrote irc2 of usdc address so we are interacting with this rc20 and we are calling the transfer from function passing as uh from from parameters because if we take the transfer from it

wants from and in this case it's message sender so the address calling this function the Donate function the two so the recipient that in this case is this address so the SM contract campaign address and the amount defined by the amount parameters okay what happens if the uh address calling this function so the message sender has not enough funds in order to donate this amount so for example let's say I have only 10 usdc but I'm calling this function and I'm passing as an input 100 usdc it will fail so the function will fail

because this transfer from returns a bullan value indicating where whether the operation succeeded so this function returns a bull and that can be true or false true if the transfer um was good or false if uh there was a problem such as not enough balance and so on so we can write this cool thing that is a requirement require rc20 transfer from okay what does it mean it means that we need true so we need that this function returns true if this function returns

false this requirement will not be satisfied and we will have an error that is transfer failed okay so right now as you can see we have an error here that is Undeclared identifier why because this smart contract this campaign contract doesn't know what is high20 okay because IR C20 is not something in the um that is implemented by Design in solidity okay ic20 is just an interface for a standard that has

been developed by developers so for example open open zapping has the rc20 uh standard contract okay so it's basically an external dependency in this case okay we need to import in some way this ic20 interface in order to interact with an with a ny20 contract how can we do it is super super simple we need to import the open Zeppelin ic20 interface this one so if we go here in open Z documentation you can see an

import interface of the20 standard as defined by the EAP so if we copy this line of code this import and we put it here okay we don't have any more the error because right now we are importing the interface ic20 that we are using here okay okay now we have another error that is file import call back not supported okay it means that we didn't install the open zapping dependency okay

this dependency that we want to use we need to install this dependency in our project in our r. project how can we do it super simple we need this command npm install open zapping contract in this way in our um in our packages we will add the open zeppin dependency so if we put mpm install opens Z PR contracts we can check in our package.json that we successfully installed this dependency

open Zepplin and now we can use the import so this is a csor bug uh so let's try to import another time okay now we don't have the error anymore so basically we installed the open zeppin Library using npm install open zeppin contract and now we can use this dependency in our smart contract so we can basically use the open zeppin

libraries how we are using this Library use uh using basically the ir1 interface so here we are interacting with the irc2 interface in particular with the usdc address calling the transfer from transferring some tokens some usdc tokens from the donor to the campaign smart contract any question on open Zepplin rc20 whatever no is it clear okay so what time is

it uh okay I think 10 minutes and we can have a a break okay 10 minutes so we have the initial initialization function so in initialize the campaign when deploying the smart contract we have the create campaign that is basically the same function called by the uh initialization but uh it can be used if we want to create a campaign later so we deploy it first without create a campaign and then we can create a campaign with this function we have a a function to post the campaign and we have a function to donate uh to the campaign

now let's see if uh our campaign status makes sense at this point okay so is initialized has been used here in theore create campaign to be sure that we create just one and only one campaign for each smart contract campaign is posed is a status uh is a bullan parameter that can be set as true here in the post campaign and also he's used here in the Donate function to check that the campaign has not been posed in order to donate so it's okay then is

active is active has been used here in the post campaign to be sure that uh we can POS only campaigns that uh has been started and also we are using the active uh status here in the Donate uh function to check that um that we uh basically donate only to active campaigns but we didn't Define uh when a campaign turns as active Okay we said at the beginning that uh this is

crucial because we want that our Campaign Will will be active only when the CP start is reached okay but we didn't write this logic so we have to write this logic right now how can we uh set basically the exactive as true so we can wait for a specific time stamp or we can just send some Center transaction to the yeah so you are completely right so the

two alternative so the second one is to just have a function that maybe is called set set active something like this only admin and then is active through this is a this is a way to do it a more elegant way to do it is to embed this logic within the Donate function Okay so does it mean it means that we can put here an if condition

so if if the caign is not active so if the campaign is not active or maybe if the CP start is greater than the block time

stamp so basically our start time is reached this active is set true okay we can have does it make sense for you so basically here every time that um a donation that a donor try tries to donate something we check if the CP start is greater than the block Tim

stamp so we set is active as true one thing missing here is that we need to have CP star greater than the block time step but also that is not already activate Okay so the owner would activate it before CP

start the function no we we at this point we we we will uh we can uh maintain this function so that the admin can set as true but if we put this logic here at this point we can delete this function so automatically without any admin interaction automatically we can be sure that donations happen only if the CP start is greater than the block Tim stamp okay are you checking uh either one like time or if no it's an end both yeah both both

together yeah so basically we don't have right now we don't have any logic for the admin to activate the campaign so the only logic to activate the campaign is this one so but you're going to implement it later no we can't we we we can um we can have only this fun this logic if we want we don't need an admin function to to activate it do you have any reason why we we we should it's safer why I mean you're you're repeating the the start I mean if you're already put in a start time it doesn't really make

sense to have an active field right yeah start time should be enough right yeah yeah we we can we can uh if we want we can delete this condition here this bullan parameters and we or maybe we can yeah we can delete this maintain this one the CP start and just check that CP start is a require yeah we can do it let's do it so it makes sense in other things but not really but for other kind of for a token sale or something yeah but I will say for this it's not necessary right yeah so we can have this

condition campaign has not started so in this way we are sure that we can donate only if the start has been reached basically does it have an end time because I cannot from it should check like it's before after yeah so we can put it Che yeah no no it's okay so here the requirements are uh amount greater than

zero uh start is reached uh and is not reached we can delete the is active the campaign should not be posed by the Admin and also we have this requirement that uh we need to transfer to actually transfer usdc token uh from the donors to the comp contract any questions I change also this requirement

here is it possible to somehow create a like like like for example in other languages I don't know whether you could do it in solidity you could create like a getter function that would have this CP start greater than block time stamp as for example some kind of active condition and then you can just change it there and because if the change if the condition would change now we would have to modify the code in different places but then it would be just like in this one place yeah yeah we can define a basically a view function that returns may be true or false if the condition is is true or false so for

example if the starts if the block time stamp is greater than the start uh or maybe we can write a view function that returns I don't know how many uh was the time uh Missing for the start time whatever yeah we can do that I mean can you create just a modifier like yeah we can create also here yeah we can create a modifier that is only start only started campaign for example yeah guys if you don't have any question let's take a break and then uh we we will start in 10 minutes 15 minutes

maybe 15 minutes okay okay so we have one hour one hour and a half uh for completing everything basically so so we have our campaign contract with some function to create a campaign posst a campaign uh we have a function for donors to donate uh to the campaign so now we have some things missing here that we have to add in order to complete the the base logic let's say of the of the campaign contract okay so the first thing that is missing

right now is the Constructor do you know what is a Constructor okay so theconstructor is basically a keyword in uh in solidity uh that can be used to initialize a smart contract when we deploy a smart contract manually okay manually means that uh we are deploying a smart contract without a factory okay so right now we created the init function that uh is going to be used by the factory contract that we have to write uh in order to deploy multiple

contracts of the same campaign contract initializing it with different parameters according to the uh campaigns to the campaign uh but just for um completeness let's say we can have also a Constructor you just need to write Constructor and we can basically initialize in the Constructor in the same way that we made for the init function

okay now I will explain so as the in function this function will call the create campaign okay what does it mean okay it means that if we deploy this contract manually we are going to use the Constructor if this contract will be deployed by a factory the factory will use the init function so basically we have a double Choice here okay so in this way our contract is compatible both for a standard deployment and for a factory deployment and initialization okay okay

now let's complete the Donate part okay so we write all our conditions uh we write the trans ER from of usdc from the donor to the campaign contract okay now uh I want to implement a near C 1155 do you know what is a near c155 we can go to um open Zeppelin the rc15 is another standard similar to the rc20 okay the difference the difference between an rc20 token and

an RC 1155 is that within the same ear c1155 we can create different ID and then each ID will have its Supply okay so an rc20 is a contract where we have a token address that is the rc20 token contract address and your C20 will have a supply of tokens with the ERC 1155 we can defi Define the same contract different ID and then each ID has its own Supply okay so what I want to do right now is that every time I make a donation here I receive back an earc

1155 okay that basically will represent my donation okay so that I can later make maybe uh sell uh my ur55 uh in a for example on openc or another nft Marketplace I can transfer I can trade whatever okay so how can we Implement a near c155 in our campaign contract is super easy so the only thing that we have to do is write this thing contract campaign is here C55 okay and we have to import of course

the r55 from open Zeppelin as explained before for the irc2 okay what happens here and what is the difference between using the interface for the rc20 or implementing the entire rc55 logic okay if we use the interface okay we are we can interact with any rc20 contract by specifying what is the token address that we want interact with okay so in this case I'm using the IR C20 interface passing the usdc address to interact with usdc on base that's it

okay while if time implement the rc115 logic in this way writing is it means that this contract so the campaign contract will inherit all the logic of an near c1155 contract okay so this is the implementation it's not just an interface it's the actual implementation of anc1 155 and we can check on open Zeppelin how ANC 1155 works and what are the function that this standard uh exposes basically okay so Ur 1155 is a no novel token standard that

aims to take the best from previous standard to create a fungibility agnostic and gas efficient token standard okay the distinctive feature of rc55 is that it use a single smart contract to represent multiple tokens at once basically each uh uh each token within the rc55 will have its unique identifier its unique ID okay and so when we want to interact with a specific ID of one 1155 contract we have to define the ID basically okay so writing this so campaign

is 1155 it means that we are implementing in our campaign contract all the base logic that offer uh that can offer the rc1 155 okay so for example the me sorry uh we in if we go to API we can see all the functions so for example the balance off balance of batch set approval safe transfer from Save B transfer from and so on okay and also

okay and also internal and private functions uh of the rc55 contract okay so let's make an example now my campaign contract is an 1155 contract it means that I can access the function of the RC 1155 so for example the underscore mint function of the IRC 1155 or the or the balance of balance of batch and so on so if you see carefully this function so the underscore mint the

balance off are not function that I Define here in my contract campaign but are inherited by the RC 1155 contract okay how we will use the rc55 so for example when a donor makes a donation here I can mint to the donor an amount of my RC 1155 okay using the underscore mint because if we if we take a look at

how the underscore mint uh function Works meore me creates amount tokens of token type ID and assigns them to to so basically it sends the tokens to the two address okay so in our code we will implement this function in this way so the two address is the message sender so the the donor the ID we can uh use the ID one the amount is equals to the amount donated by the donor and then this is an extra uh parameter data that can be uh passed as

empty uh or we can use use for basically custom mint logic okay but in this case it's not needed so for example let's take an example if uh we want to meet a token we can use the underscore mint but then if we want to execute a another logic we can put this logic as a byes parameter here okay so of course this is for advanced uh implementation uh in in uh in this case uh for us is not needed so we will just mint an 1155 token to the message sender of ed1 so we will use

only the ed1 equals to the amount so basically what happens that the donor will send usdc and he will um receive an amount of our ec155 okay super simple now you will understand better why we can use this approach of basically Implement an 1155 because now we write the withdraw function okay okay the withdraw function is used

by a donor to withdraw the usdc that uh they donated okay so for example they donated to a a campaign but then I don't know something happens uh so we have a news that uh the admin of a campaign is a scammer what else or maybe you Chang your idea I you don't want to donate anymore so you want your money back so we need a withdraw function after implementing thec I have an error that no arguments pass to the base Constructor yeah yeah

something for later for us is okay okay okay yeah but but my it has marked my whole code uh so the error that you get means that uh the 1155 uh token contract needs some parameters in the Constructor okay okay so if we take for example this line of code and we put it in our Constructor you should not have the

error anymore so go to the open Zepp Doc and copy the this line of code is okay okay I will explain later uh what does it mean so withraw function this function can be used by a donor to withdraw uh their money okay so we need to specify an amount of tokens to withdraw that is

amount okay we need of course that this amount is greater than zero okay okay then here what we what we can do depends on our business logic because for example we can decide that uh donors can withdraw money only if the campaign is not ended or maybe only if the campaign is ended whatever okay depends on us okay so for example in this case let's make uh the withdraw available only if the campaign is not

ended okay so for example CPN is is less then this is wrong uh only if the CPN is greater then the block time stamp okay so if our block time stamp is less than the CPN we can still withdraw uh the donation okay how it works so we need for sure to send back

usdc okay so as as as made before we can write irc2 of usdc address. transfer to message sender of amount okay in this case we don't use the transfer from because is the uh contract itself that we will call this transfer function so it's not a thirdparty uh address okay so in the in the Donate function in this function here is the contract so is the campaign contract the message sender of the this function okay while

the donor is the message sender of this function okay because this transfer from is an internal logic is a logic uh executed internally in this donate function okay so the message sender the Donate function is the message sender so the donor basically the message sender of this transfer from is the company contract because it's this contract this function on this contract that will call the transfer from so in this case we need the transfer from because we need uh the donors to uh approve this amount of token to spend okay while in the

withdraw is the campaign contract the message sender of the transfer and is the campaign contract the older of the usdc token okay because the usdc are inside the contract at this point if we are calling withdraw okay so in this case we don't need an approve basically we can just simply call transfer okay we called uh we are basically sending back the um the amount of token but before we need to burn the RC

155 of the message sender okay so I will put a require uh that will go something like this so what what what is the logic here the

logic is if this requirements is okay it means that the message sender has enough token to be burned okay so if we if this requirement is okay then we can transfer SDC if this burn fail it means for example that the master sender has not enough uh 11 1155 tokens okay so basically it means that the address calling this withdraw function is an address that has not donated before basically because if he donated before

he will have a an amount on of 1155 meant a year or he transfer or or he transferred to another address so he he can't no longer withdraw the money because the new older will be able to withdraw the money and how about if heed for example $1,000 and he want to 2,000 he can do it because in in this case he can pass uh an amount for example 100 and this code will burn 11155 so the token the the address will still have 900 for example 955 so it can be used also for partial

withdraw okay the other reason why is cool to uh manage position using 1155 is that in this way we don't have to track how many money the campaign raised because why because the balance of usdc address of the campaign contract will will automatically be the total uh money raised by the campaign okay and on the other side we don't have to track uh how many

how um we don't have to track what each donor donated okay because it is represented by an amount of 1155 because the alternative if we don't use an 1155 we have to track in the storage we need to to track each address how much he donated okay in this way we can skip basically this logic because we can be sure that If he if the the address has enough 1155 it means that he previously donated at least that amount is it clear yes okay so we have the Donate

logic we have the withdraw logic we have the create campaign logic so one big big thing that is missing right now is the claim that the admin can do if the campaign has been s success okay so for example if the goal has been reached so the goal set by the Admin that is CP Target amount has been reached and the campaign has ended so the admin should be able to withdraw the money okay so we need to to create basically another function that is an

admin function so I put here after Theos that is claim so function claim claim that is external because it will be called by the Admin address so this this is an only admin function of course because only the admin can um can claim the money of it of its campaign and we we need some requirements because the the first requirement that we need is

that uh the block Tim stamp is greater than the CPN so the campaign is finished and also that the IR C20 usdc address balance of address this so the balance of the campaign contract is greater than the CP Target amount otherwise we will have an error that is compan has not reach Target amount so if they will not reach the target after the CPN what happen that uh

donors can withdraw the money because we are trying to replicate the same logic of um Kickstarter Kickstarter works in the same way so if a campaign doesn't reach the target then all the donors can withdraw the money from the more like the withdraw function yeah yeah yeah so let's finish the claim first and then we will uh modify the withdraw so the claim uh campaign should be end and uh the balance of of usdc address of the campaign token to the of the campaign

contract uh must be greater or equals to the CP Target amount in this case we can transfer the money to the CP admin address okay so here instead of transferring for example the CP Target amount we can simply transfer all the balance okay so this this balance here because so why because if for example some wallet or some address

uh for a mistake send some usdc token to this contract without calling the Donate function what happens is that we will have some usdc blocked basically forever in this smart contract okay while if we use the balance off we can be sure that the CP admin can withdraw all the usdc amount even if it was uh transfer for a mistake or whatever from external addresses so in the in the best case scenario the CP the CP admin will receive more money than the target

okay or in the normal scenario let's say this balance off will be equal to the CP Target amount okay let's see one thing okay at this point we can set the uh is ended variable as true okay and so here in the donate let's add another

condition that the campaign is not uh ended because this is ended will be set as through only if the claim uh was made by the CP admin okay so it means that if is ended we are sure that no more donations this can be also maybe it's a duplicate uh because we have already this condition

but but yeah just to be sure we can have this condition too so now we can create campaign post campaign claim successful campaigns donate to campaign withdraw from a campaign and that's it uh so let's modify a little bit the withraw logic so that if the campaign doesn't reach the target the donors can withdraw their money okay so we can do basically in two different things one is to create

another function that is something like withraw uh let's say we draw not finished campaign or whatever or we can uh modify a little bit the withdraw function okay so the withdraw function requirements are amount greater than zero CPN must be um greater than the block time stamp we require Le and also we can create a condition that uh I'm thinking sorry guys

I'm trying to find the best way to do it uh so this should work as well for the donator to withdraw this money he spend before like you want to use this function also to withdraw the money by the donator yeah this is for donors only so this is donor only the claim is for the admin Okay so what happens if okay we can create a condition that is so CP start must

be no it's not not here is in the in the withdraw sorry so the CPN must be uh greater than the block time stamp so we can withdraw basically only if the campaigns uh reach its end or or if or if the CPN is less than the block time stamp so if uh the campaign is running

basically no it doesn't work in this way so we need a way to basically say uh say if the campaign is ended and so and the uh amount the goal amount is not reached we can withdraw okay so the best way to do it is this one because with this condition we are saying uh we can withdraw money money

only if the campaign has reached the end okay or the campaign has not reached the end and the uh campaign is not ended what and is ended is uh defined in the claim function and is ended is set only if the uh goal is reached does it make sense like but this flag is ended it

doesn't update automatically like it would have to do this this this like I think it would be possible that that the could be let's say that the some donor could withdraw the money after after it has been raised but not yet collected by the Creator that's a good point how can we fix this we have to check whether the you have to check so we can check for example using a view function that returns true or false if the money are enough compared

to the uh to the goal or in the Donate function we could say we could set some flag that it was raised fully uh because I think checking this every time would be more less efficient than St okay so you are suggesting to uh basically check here if the balance of the contract is great than the Target because we are because we are not storing the current current amount amount of no we we can use the balance the balance of I'm think what would be more

efficient so uh the probably the most efficient way here is to is to check here in the Donate because what we can do is after the mean okay we can have something like this that can be uh an internal

function that is something like this so if the IR C20 usdc address balance off add this is greater than the CP Target amount we can have a not is ended in this case but uh um something like gold reached for example that can be set as through and we can store a goal reached uh buan here value that will be false by default

yeah could we just store the state of the of the campaign not as a set ofs but yeah that's St as an enum yeah yeah yeah yeah we can do as an enum if you want so in this case gold reach will be true and here gold reached and we can basically delete the is ended parameter

because is no more needed at this point so what so what we did we created a uh bullan value that is gold reached that will be false by default and will be set at true only if after a donation the goal is reached so the balance of uh usdc of this contract is greater than the target amount that we set okay if so we set gold reach as true and we use uh these uh parameters to make a check on the withdraw functions

so that in this case the withdraw function can be used in two different ways the first one is uh uh if is if the campaign has has not ended okay so the CPN is greater than the current block time stamp or we can use the withdraw if the campaign is ended and the goal has not been reached okay should we then change CN time for current time stamp no why this is basically the condition that

you suggested before so in this way we can use the withdraw function in two different ways but for withdrawing during the campaign or after is the if the goal um was not reached by the campaign also in the claim method we can I think change this logic of checking the bance could be simplified to checking this value you mean here because here when the go is yeah we can we can we can use right now this bullan here instead of calling because this is gas expensive okay so the claim can be done

only if the goal is reached and in this way in this case we will transfer the entire balance of usdc to the CP admin so let's create a comment here uh what will happen when the goal reached will be before CPN and here in require is that CPN has to be uh smaller than block time so we can the owner can't claim before the end no same this

is the same thing of Kickstarter for example so in this way in this case uh no oh yeah this this one so the the goal was uh 93k and the campaign um received 1 million and and so on okay but the campaign has 3 hours left so the campaign is running even if the the amount collected is greater than the gold reached the difference is that if the uh the amount collected is not greater than the amount reached the ca the the admin can't withdraw can't claim

the money basically so is all all or nothing but it's greater he has to also wait the end of the time yeah yeah yeah yeah one thing is the campaign end the campaign end is used to claim okay but the claim can only be done if the goal is reached okay so I think we finished basically the the logic of our campaign contract let's see so we have a campaign that we can initialize in two different ways One is

using the Constructor so deploying um the contract in a standard way the second one is deploying the contract using a factory then the admin then we can create a campaign this should be a nly admin function the admin can also post the campaign and the admin can also claim the campaign while donors can donate and can withdraw money well this is are these are internal function okay

so what function should he use to if he post okay that's a good question so on Kickstarter if you delete a campaign that is basically our pose then the admin can't restart the campaign another time okay if we want to have uh a more flexible logic we can create um we can pass basically a Boolean value here this way so that it can be true or false and the the variable will

be uh true or false but in this case I will suggest to maintain this simple logic okay so we basically finished the uh the campaign contract now for the first time let's try to compile the contract okay so n NP xr. compile and we have one error so we have uh okay this is a warning on the caign Factor ah yeah this is the

campaign Factory so we can avoid this warning CP Factory and for the moment it is an empty contractor okay and right now we have only one error that is the requirement at L at line 72 uh 72 that is this uh

burn while because probably the burn function of open Zeppelin doesn't return a bullan value uh so let's check it doesn't specify it so let's

try okay so basically here the burn function uh implemented by developed by uh open Zeppelin doesn't return a true false okay it simply revers if the underscore burn can't be done okay so in this way we can't use a requirement because a requirement uh requires that uh the result is true or false in this way if the burn can't be done because for example the message sender doesn't have enough amount of 1155 the function will revert

automatically without our requirement basically okay so we successfully compile our contract let's do it another time just to be sure okay perfect so it means that our contract compiles okay so at least uh um grammatically let's say following this the solidity guidelines the contract is correct okay even if the compilation is successful it doesn't

mean uh that the contract is correct so the actual logic that we brought inside the contract is correct okay maybe there are some bugs okay but at least the compilation is okay so there are no formal errors let's say so for example I don't know if I if I cancel this amount parameter and I try to compile I I will have an error here so this means uh compilation errors okay now we have basically our campaign contract that is finished okay that is also an 1155 token

contract so now we have to finish or we have to start B maybe the factory contract so this Factory contract will be able to deploy clones of the campaign contract so imagine the flow on the application so you are you you are here on Kickstarter uh I don't know where I can create a new campaign but let's think we are in the create uh tab okay I click create and what happens under the hood is that my factory contract will deploy

a new clone of the campaign initialized with my parameters so my admin address my re goal to reach and so on okay is clear perfect so let's write this uh Factory contract so uh even in this case we use Open zapping Standards in particular we are going to use the Clones Library okay let's read this text so uh there is an UT of course is a

standard AP 11 67 is a standard for deploying minimal proxy contract also known as clones to Simply achly clone contract functionality in an immutable way uh blah blah blah uh this standard specify mini Cod implementation okay so basically if we use this Library our Factory will be able to clone our uh um our campaign contract how it works under the wood is quite complex but we can simplify so the this clones Library basically every time that we call the Clone function this this

one uh it wants an implementation address as input that in our case will be our campaign contract okay and it will deploy a minimal proxy contract is called okay a minimal proxy contract is basically a a contract that can um that can replicate in some way the logic of our implementation contract so our campaign contract how it works and this is a low level it makes delegate calls okay so each time we call the Clone function

and we are deploying a new clone really we are deploying a minimal proxy contract that points to to the implementation contract this is our actual campaign contract in this way each minimal proxy contract can make delegate call to the implementation contract and uh store and and basically use the storage of its minimal uh uh minimal proxy contract so what happens that we will have an an implementation contract this the campaign contract that we we we have to deploy once we have the address of our implementation contract

we put the implementation contract address in this function clone of our Factory and this clone function will deploy a minimal proxy contract every time that is called and each minimal proxy contract we will use the logic of the our of our implementation contract with its own storage okay so the result is that we will we can have multiple clones of the campaign contract where each clones has its own storage variables but the logic is shared because the logic is the one defined by the implementation contract is it clear it this is a

complex um approach let's say but if we implement it is super easy because the only thing that we have to do is basically to import our campaign contract so I import from uh from the contracts folder and I import the comp the campaign. so contract and also I need to import the uh let's take this one I need to import the Clones library of open

zeine okay in this way my CP Factory contractor will know the functions to interact with the campaign contract and with the Clones Library okay so let's start first thing we need a Constructor because we need to deploy this Factory contract and we need to initialize this Factory contract with the

implementation campaign address okay we can store this address in the storage and we can initialize this variable in the Constructor uh okay okay so basically we will deploy first the campaign contract and then we'll we will past the address of the campaign contract here in the implementation in the sorry in the Constructor okay once we have the

implementation uh address we can write the create CP function that is an external function okay what's the logic that we need within our create C P function we need all the parameters that the init function wants okay so basically we can copy all these

parameters here okay and then the suggestion is good because a new campaign will be the result of the cloning of the factory how can we clone we just need to use the Clone function of the Clones Library okay so with just one line of code so address newp equals to clones do clone implementation

CP the result is that we are creating new campaign contract okay so every time we call this function create campaign a new smart contract a new campaign smart contract will be deployed a new clone of the campaign contract will be clone will be deployed but of course we need to initialize the the campaign contract because if you remember we write this init function uh for factory initialization

so what we have to do within the create CP function of the factory is just this so we access the campaign contract from our inut okay we pass the address of the new uh deployed the new cloned campaign that is new CP and on the campaign contract we call the unit function okay what are the parameters that that we pass to the unit function this one

I think it's okay but it's just um okay A Memory no you have address types of the variables the types in the function invocation should be ah yeah sure sure sure yeah yeah yeah you are right so right now we are passing uh the parameters needed for the campaign contract to be correctly initialized

okay now something that we can polish a little bit so the first thing is that if we want to be sure that every time the an admin create a campaign using only usdc address on base we can Define the usdc address as of usdc address as the actual Base usdc address so if I go to basc usdc this is my this is the Base usdc address I put as a constant usdc address

and here I don't pass dynamically the usdc address but I take it from the storage okay this way we can be sure that each campaign is correctly initialized using only usdc on base okay then another another thing that maybe we want to do is to emit an event here

so after we uh clone a new campaign and after we initialize a new campaign maybe we want to emit an event how can we emit an event we have to Define an event in the smart contract okay and maybe in our event we want to uh to emit not only the new clone campaign smart contract address but maybe

also the CP admin that created that uh campaign okay so we can do this thing do you have any question on this Factory approach so basically what we have to do or maybe what we did is we created a new contract called CP Factory this contract import the campaign contract and the open zapping

clones Library no don't want this turn off okay we Define a contract and then we have a Constructor that is is used to initialize the the factory uh setting the implementation contract as the campaign contract address that we will deploy and then we simply have a function that is create campaign that clones the implementation contract that is the campaign contract and then initialize the campaign the newly created campaign and emit an events is everything

clear so in this way we can have we can have basically multiple campaigns contract one for each uh campaign created on our Kickstarter like application so the admin that creates The Campaign Will interact with the factory to create the contract the campaign contract and then we will he will interact with its uh cloned campaign contract in order to manage it so to post claim or whatever so let's try to compile another

time so every time you type mpxr do compile you are compiling all the contracts within the contract folder okay compilation is good okay so the last thing we have to uh we have to finish so the last thing that uh I want to do with you is to uh try to deploy the factory this inherited Constructor that

you showed me is this basic Json file that is in the documentation okay or do we have to change it because I justop so basically uh this one so um this this is the um URL of the metadata of the RC 1155 token okay so so in this case of course this is an example so probably this will be empty or maybe doesn't exist whatever so but in in our case of course when we deploy uh in production we need to

um to pass here uh an URL pointing to an existing metadata of the token it doesn't have to be for the cont yeah exactly the important is that a string just a string I can can be even like this okay so because this is the metadata of the 1155 okay and then we have the metadata of the campaign so it's different okay maybe this one will be empty because we don't need it but of course we need the campaign metadata okay

so now just to visualize uh what happens let's go on remix do You Know Remix is a is a web tool to compile and deploy contracts basically from a UI so it's super simple so here um I already add a workshop Factory that is this one so copy and paste it's super simple yeah

but ah yeah sure so this will be verse of okay uh okay and we need to change this one so okay if if I go here uh solid compiler so I can select a contract in this case the factory no let let's select the campaign

because we the flow is we uh deploy first the campaign and then we deploy the factory okay so let's select the campaign go to solidity compiler click on compile okay the compile is good so this compilation is the same of our do compile of this one okay exactly the same so once we compile the contract uh we can go to deploy let's modify something one sec because uh no it's okay okay so

let's uh let's deploy the contract okay this is the campaign contract okay and we we will deploy it with let's deploy it without the Constructor okay because I said before this is a double choice because if we have both the Constructor and an init function it means that this contract can be uh deployed and initialized both by a factory and manually okay but in this case we need we don't need the Constructor because this campaign contract will be

just our implementation contract so it should be deployed without any storage variables it should be empty because then it will be initialize every time by the factory okay so let's remove right now think you can remove it because it's ANC yeah yeah I just removed this the parameters I'll maintain the 1155 okay okay in this case as you saw it changed because now the deploy doesn't need any parameters any input parameter because this deploy here is the standard deployment and the standard deployment

uses the Constructor okay so let's no sorry let's open metamask I'm on seia let's click on injected provider metamask in this way ramix will be connected to my metamask uh and so to sepolia uh this is my address let's click uh unit some

bugs ah here connect account okay perfect so contract vers of factory vers of campaign compilation deployment injected provider okay right now we are connected with this wallet ZX uh 06 and so on one it and we have one it on sepolia let's click on deploy this will deploy basically our campaign contract that is our implement ation contract for our

Factory okay we successfully deployed the contract let's wait a little bit that the sepolia eers can show me the result of the transaction perfect so this transaction uh was made from this address that is my wallet address here and the result is the creation of this contract okay this will be the address of my

campaign implementation contract okay so I copy this address here let's go back to remix let's take this time the factory let's compile the factory and let's deploy the factory passing as implementation campaign address the newly deployed campaign contract okay so this is the contract that I deployed uh two minutes ago let's click on transact so now I'm deploying and initializing the factory

let's see okay so this is the transaction made from my wallet this one to this address that is basically the newly created Factory contract so as you can see this is the address of the factory this is the address of the campaign implementation contract and are

different now we have both the factory and the campaign contract now we can use the factory to clones to clone the uh campaign contract okay so what we have to do is going here these are the contract deployed let's go to the CP Factory and I have this function that I can call that is the create CP function this function I remember you that can clone the campaign contract okay so for example let's imagine that this is the

interface of our uh Kickstarter like application okay we we are on a tab where I'm setting my campaign parameters okay this this okay so I put all the uh parameters and then when I create the campaign I under the wood I'm creating I'm cloning the campaign Factory and deploying my campaign contract okay so I need this PP admin that is my address let's say some metadata that can be C tomorrow we will create some campaigns with real metadata okay CP Target amount

uh is uh expressed in usdc usdc has six decimal so it means that to say one us DC we have to write one with 6 zero one 2 3 4 five six so this is one usdc okay let's uh create our campaign with a target of 1,000 usdc so this is one this is 10 100 1,000 okay this is 1,000 usdc CP start so uh C start Unix time

stamp so let's say that I want to create so we have this Unix time stamp as the uh start time this basically in 1 hour and we have a n time that is in one month sorry okay now if everything works fine now I click this

button and I'm cloning the campaign implementation contract and I will initialize this clone with these parameters so the transaction was good and in the logs that are basically dents I have this one that should be my cloned campaign contract uh this

one I can see this I think we need to uh uh 60 let's see so contract also and also on uh you mean it will work yeah network network yeah yeah yeah yeah so

here this is the um the cloning transaction okay so the one that I obtain clicking this button here is uh this one okay here in the logs or maybe in the State uh I can check this address here uh let's see this 0x n and so on that is this one that should be the cloned contract address so the campaign clone contract address

okay now just to be clearer I can um for example I can write here a returns parameter so for example I can return the address of the uh new CP that is the Clone campaign within the function so that I can see in the output what is the uh the Clone campaign contract address otherwise I need to uh verify my contract on ether scan so that I can check the event clearer because right now the contract is not verified so I can't see

uh the logs so the events in a human readable version basically okay so we need to verify the contract or to uh put a return statement here so that we can read the the return statement or otherwise we can check the contract in the uh as as I as I did in the state change that is more complex and how to verify the contract so we can verify the contract directly on ether scan so for example um this is the factory contract okay if I click

here and if I go to contract it says are you the contract Creator verify and publish your contract source code today okay if I click here there is a a procedure that I can make in order to verify the contract or I can use a plugin for our do okay that is little bit complex uh but I can verify directly through our do so if the contract is not verified we should be like 99% sure that this SC or yeah you should verify the contract of course it's better to verify the

contract because otherwise you can check only the bite code of the contract now the final test is taking this address that is our uh clone contract address load this contract address here in remix and check if I if the parameters are initialized correctly okay how can I do it so I should select verv campaign so the campaign contract compile the

contract go to to deployment and instead of transact to deploy another time I can past the address here and click on load here at address uh some months ago uh it was load contract now is at address and I as you can see I load this contract here so I can interact with a contract now I can see that CPN is initialized as I um with with the same parameters that I passed when cloning the contract to the factory same thing

for the metadata CP Stars Target and gold reach of course is is false the address admin is m m admin address so it means that right now we successfully cloned the campaign contract and uh being the admin I'm the admin of the of this campaign I can start interacting with the contract and donors can start to donate to this contract so that's pretty much it this should be the end of our campus lesson thank you

[Music]

Related talks