lib.rsfile in your editor of choice and let's take a look.
GumballMachineblueprint looks very similar to the
Helloblueprint that we just reviewed. This time we have 2 vaults: one for the gumballs that will be sold and the other for the XRD tokens that we will collect from those who purchase gumballs. The
pricein XRD for each Gumball is also maintained as a variable so that it can be changed if we provide code to do that. Notice that
Decimal. This is how we save and use numbers that have a fractional component in Scrypto. Floats are not supported on the ledger. It is usually best not to use them at all.
Hello. One difference is that the
newconstructor takes an argument named
price. This argument will be saved into the component's state. Notice how we instantiate the two vaults differently. We instantiate the
gumballsvault with the content of the gumball bucket and we instantiate an empty
collected_xrdvault that will contain XRD tokens.
get_pricemethod returns the price which is held as part of the component's state. If by now you are itching to do some coding, try adding another method named
set_pricethat can be called to change the gumball price.
buy_gumballmethod which is really the heart of this blueprint. This method takes a
paymentBucket as input. It then takes a portion of the tokens from the bucket as dictated by
self.price. The caller's change, if any, remains in the
paymentbucket. Notice that the code makes no effort to know if there is any change. Instead it is just going to return the
paymentbucket to the caller without bothering to check whether or not it is empty.
takemethod from the
takemethod returns a bucket with the amount of tokens specified. Of course the second part of the tuple is the previously discussed bucket containing the change. This tuple matches the functions stated return pattern designation of
(Bucket, Bucket)and so this method is complete.
buy_gumballmethod requires a bucket. How do we send a bucket with resim ?
030000000000000000000000000000000000000000000000000004is the resource definition of the XRD token.