Skip to content
by Unity Nodes
NEAR Protocol

NEAR Certified Demo | Episode 1.2 -- Wagers

about 13:30 long

Nothing is loaded from YouTube until you press play, and then without tracking cookies.

About this video

This demonstration was presented by the author(s) after just 1 week of working with the NEAR platform as part of the NEAR Certified Developer program.

You can find the source code to this demo here: https://github.com/Learn-NEAR/NCD-01-...

--- Follow the latest from NEAR Protocol on: Website: near.org Discord: near.chat Blog: near.org/blog Twitter:   / nearprotocol   GitHub: github.com/near github.com/nearprotocol #Blockchain #FutureIsNEAR #NEAR #nearprotocol

Transcript

1,929 words

Captions as YouTube publishes them, usually machine-generated, so product and people names can be misheard.

  1. 0:00

    uh jacob you're up next uh can you share your screen and please present your demo and let's try and keep it to five or ten minutes uh marco's was about ten minutes okay sure sounds good can you hear me yep okay perfect um so i only worked on writing the contract so there isn't a nice fancy front end sorry yeah that was that was the goal nothing to apologize for so uh i'll send the link to just the github gist that i put the code on earlier today so

  2. 0:32

    you can follow along and also share my screen so you can okay so okay so i wrote a contract that will support um setting wagers on like the value of certain things like maybe a stock or a cryptocurrency and then um in oracle we'll update those values at a particular time and you know whoever wins the wager at a particular time will just the winner takes all kind of um yeah so i kind of modeled this after a lot of

  3. 1:11

    the different near demos that i saw also this is written in assembly script instead of rust uh i'm a lot more familiar with typescript i'm a web developer by day um yeah so i suppose we can go through this um there is a couple things to note that these functions are not intended to be used in regular times it's just because i messed up my storage a bunch of times so i needed to clear it out um yeah so i suppose we can start from minting which is down here

  4. 1:49

    uh this follows very closely to the fungible token um kind of idea here as i was writing this i was thinking it might actually be a better idea for me to like create my own separate fungal token contract and then have some inter-contract communication here like one that manages the wagers and one that manages the currency um but i didn't get around to doing that um pretty much all it does is it sets the

  5. 2:15

    mentor of the contract as the one and only oracle who can update the values of symbols at particular times um so that's kind of like an improvement that i could see for this contract is allow multiple oracles to register themselves for different symbols so after we mint we have our standard you know um get functions get balance for a user get the total supply transfer nothing too much interesting there

  6. 2:45

    um but this is where things kind of start to get more unique under the create wager function where uh any user can uh create a wager for a symbol uh they can choose whether they're going well then they choose a value that they predict that symbol will either fall or fall below or go above that's what this boolean flag says is if they're betting that it goes above or below that value how much they're betting that it will do that and at what block time stamp

  7. 3:15

    um and we have a bunch of assertions to make sure that all the values are same we create a wager object and then update a couple of data structures with the wager's id so that we can associate wagers with a user and so that we can associate wagers with a symbol um so for example a symbol would be something like btc for bitcoin if you're betting on the price of bitcoin or it could be something like

  8. 3:41

    um you know the symbol for tesla if you're betting on stock market um okay so then another user can accept a wager given by their id a couple of just sanity assertions here making sure that you know you can't accept a wager that's already expired making sure that someone else hasn't accepted the wager already and you also can't accept your own wager because that would be dumb um yeah and so then it just fills in either the over the under field whichever was

  9. 4:12

    empty with whoever is accepting the contract or whoever is accepting the uh wager rather um and updates a couple of those lists again um you can only delete a wager if it hasn't already or sorry you can only rescind a wager delete a wager that's a helper function so this is an actually exported function you can only rescind a wager if um no one's accepted it yet um and then it will restore your balance by the way so this kind of has its own fungible token

  10. 4:43

    uh that is minted along with this contract more or less um so whenever you create a wager it kind of it takes some of that some of your balance and ties it up in that wager so it removes that from your actual user balance but the wager is still tied to your account id so you can still recover that if you rescind the wager before it's been accepted um okay and then only the oracle is allowed to report a symbol so for example you're going to update the contract with the price of bitcoin

  11. 5:15

    um and so then you're going to get all the wagers that are associated with the btc symbol um and then this is kind of expensive i haven't really written many smart con writing a loop is probably going to be relatively expensive so i'm trying to figure out if there's a better way more optimal way to do this um i didn't come up with anything so it's just this would just be rather expensive to run if you have a lot of

  12. 5:48

    uh wagers on a particular symbol but then it checks that the wager's time has passed and that the wager has been accepted then it will distribute the winnings based on the simple algorithm of determining if the symbol value is above the value or if the symbol value is below the value and it will distribute the winnings to the under or over better accordingly otherwise in the chance that there's a

  13. 6:12

    tie it will just distribute them evenly um uh these again are just for debugging purposes and then a couple of git functions for getting uh wagers associated with an account for a symbol these just return wager ids um and then this actually returns the details about a particular wager um yeah and that's that's pretty much it uh it is deployed on test net right now um so if i have time i could right a couple commands in my terminal here you do yeah you're just over you're just over five five minutes so if

  14. 6:52

    you want to demonstrate some of the interface that's fine okay cool so um something i noticed is that uh whenever i'm calling things on uh from from testnet it tends to time out about three times before it actually uh works so i i don't know why that is um so i just need to copy that okay so then i want to say near call this guy get balance and my json will be account here and i will say this is my i know i'll do one of my test net accounts and we'll

  15. 7:44

    say account id i believe hopefully i wrote that correctly this is all very new to me so i might be doing things oh i should have said near view because then it would have been free um so i think this account has around four 000 tokens which is not very many because the total supply is that much which is a lot yeah so i don't know why it takes forever and times out a lot but there we go

  16. 8:31

    okay oh yeah okay so that's about 9 000 tokens um and then i can create a wager for um i'll create a wager not on the oracle account because that sounds dishonest um here and so then i need to put in a symbol and i'll just have my symbol be actually let's just btc um i just need to check my because i forget everything i need to put in okay and then is over we'll say true and then value let's um so technically you'd probably want to put this in in satoshi's or something

  17. 9:24

    um i'm just going to put in a dummy value so let's say the price of bitcoin is five um and i have to put that in as a string because i discovered last night um that's a json doesn't support 120 bit eight bit integers um so then my bet i'm gonna bet um 40 tokens oh that needs to be a string two 40 tokens and then at i have to generate this value just give me one second so i generate that by saying um so if i give myself i have

  18. 10:08

    given myself two minutes here so at that time hopefully this works okay so that looks like it works now we can say um get wagers and that will list the ids of our wagers oh no ah shoot i that's not the name of the function it's get wagers for account get wagers for account which is also a view oh yeah well it should be i'm just trying to not uh take too much time sorry um we could get the details for that wager

  19. 11:02

    uh technically i suppose i have two oh sh i have to accept it too so sorry just a second that that's okay i mean we're at we're at about 10 minutes jacob but i mean i'm also able to verify all your transactions here on testnet i can see that uh that all this is coming through and um and and it's it's uh it's it's obviously making sense uh in terms of the story that you're building don't feel the pressure to kind of

  20. 11:26

    finish out uh the entire the entire narrative if you don't if you don't have to i'm gonna try and accept the wager hopefully i'm still under the two minute time uh that i give myself okay so it looks like i uh myaccount5.testnet has now accepted that wager and so if i get wager i'm just leaving these all as calls even though they should be views oh well get wager wager id is seven

  21. 11:58

    then we should see the over and the under are bit and five respectively yeah so those that have been assigned hopefully i'm past my time now so like the wager time has passed um and let's see i need to obviously a web developer because this is how i figure out what what time it is um okay 840 yeah okay we're past time now so i can report this symbol btc report symbol it's a report

  22. 12:39

    symbol i think that's what it's called um and it's going to be symbol is btc yeah and um the value so we're gonna say the value is six so that means that bit.testnet should win uh 80 of my tokens and only only hatchet.testnet can call this because they're the oracle so um anyone else can't call this function all right so now if i say um get balance for bit.testnet we should see that it is um 40 more than

  23. 13:30

    uh here so it should be knowing 8 six zero and there we go all right that's that's the conclusion of my demo that's great congratulations