Pyth Workshop - ETHGlobal Brussels 2024
ETHGlobal·Wed, Oct 9, 2024, 12:00 AM
Join Aditya Arora from Pyth, for a workshop that helps you learn how to integrate Pyth oracles to fetch low latency data and random numbers. This workshop took place during ETHGlobal Brussels, a 3-day hackathon held in Brussels, Belgium from July 12 to 14, 2024. ETHGlobal Brussels brought together the most skilled web3 developers, designers and product builders from all around the globe for a weekend-long adventure to advance the Ethereum ecosystem! ---------------- Follow us: x.com/ethglobal Website: https://ethglobal.com Email us at: hello@ethglobal.com
Transcript
[Music] okay so we are a pull Oracle it's very different from other oracles out there which are push Oracle the benefits are is you can retrieve the data anytime you want the other post oracles send the data like in 30 minutes 15 minutes so they're not data is not updated so if you're building very high frequency app or doing anything it's better to use SP because you get faster data you have more more price feeds and we support all the chains that you can name even don't use any other chain please like any weird chains so how we are different we have we take the data from first party data providers not aggregators or coin Geck or coin Des something like that really the data where data come from like qcp Capital CMS you don't care about that I know but I have to say that and we support almost 60 plus evm chains so either you're building on XYZ chain or chain I don't care we have the data there and you can ask the question anytime don't wait for AMA uh just so that you know what you're getting data from and push they send the data every 30 minutes or whatever you then get the data from Smart contract whereas you request the data before you want to update it now coming to the code part because it and I'll show you how to integrate pit it takes only 5 minutes you don't have to stake XYZ token to some contract to start using pit here we have three bounties one to start if you want to use price data on your like on your chain uh like any dab you have to use it onchain we have a method of using offchain as well but we want you guys to use onchain that's the first criteria okay second we have random number method we call it entropy pit entropy it's similar to vrf but way better than that okay and third is an open Bounty you can create any tutorial or make or make any improvements to our SDK if you feel like okay this method is if you add this method it will great for people we'll give you Bounty for that we want to know how people like P what's a developing experience look like any question till now I've not you have not seen code or anything so that's why uh yeah I'll just start with this this is how a normal main function looks like for erc721 everybody is worse of that okay now to use P you need three things one the chain which chains you are building on second what data what price data you need you need e price BTC price or gold price I don't know why but if you need it that's second thing and third is Hermes that's something different so three things price feed contract address on that chain and the heres data point what is Hermes hermies an off service you fetch the data and update it I can see some confused fa faces right now about homies yes so I I knew that this will happen so this is we have a pet that sens to the Wormhole this is uh the hermus and this is the contract that you will build here okay we have a contract on every chain out there sorry okay so this is the contract you build we have pit deployed on every chain out there now if you want to read data from some Oracle how do you go read price feed data whatever and you put the price feed ID am I right that's how you do it in this case if you read like that you'll get an old data which you don't want for your dap so it's a PO orle I told you it's not a push Oracle so you have to pull it so from homies you will pull the data and you will update it on chain and consume it that's how you get the latest data Hermes is a service which you can see here from where you will pull the data I'll show you with a code how simple it is so is something you use from a javascri yes we have a homies SDK it's a offs and service that is running from different companies out there you don't have to implement homies it's a normal SDK from which you will F the data one line of code that's it uh now with the code I'll show you how to do that so we have if you go to like uh P Workshop I have not open it sorry is it visible so this is the same contract that we were using for the maining an nft it's example I took so if you see here uh uh for the minting I want e USD price right now okay and if you see there's the argument called price update on the second argument this is the price update you f from heres so we have a heres Swagger file which is linked which looks like this uh just for testing you can use this and run it or like in your dab also you just tell which price feed I want and you'll fetch an update data you pass it in your contract here and that's it when you pass it you fetch the price like this and we these you don't need that other thing so we have all the examples in pet examples for mat uh repository so that's how you fet it this is the main method you need to click a photo for if you want so why why is that an array is that because you can fetch 10 to 20 prices in one call so you don't have to worry about it so yep how do you create a price create a price Fe for something that you uh you can't create a new price feed right now no it's it's by governance the Dow decide what's the next price can be there so I'll just help you to navigate The Docks if you want to find this is how a docks looks like that's how you go uh go to Price feeds if you see here we have all the uh contract addresses okay on the left you can see go to there I'm pretty sure you all be building on evm these are all all the change that we support and you don't need separate addresses for different price feeds so you just need one address where p is there you take all of this you have support all these chains okay you take uh like I want to do do it on arbitrum okay so when you see the code here okay it's gone wow no it's here that uh address you will copy it and you will put it there in a Constructor in your contract this is the pit where the pit contract is implemented now you need price feed IDs so we have a page which is already in the the eth global website these are all the price feed IDs so give me a random price feed ID any crypto price if you want any give me a random gold you search it here it's GLD I think I mean yeah GLD something like e gold or us gold Equity copy this okay and uh in that same code if you see uh the is USB price like I'm hardcoded USB but you can change it to gold price here pass that bytes which I copied from the that page pass it there the this method will we do okay if you see it's a payable method why it's a payable method every time you update the price you have to pay One V the smallest denominator on any chain if it's something else on other chain that's what you pay one way so you're pulling the data you're updating on chain and you're using it so if she pulls a data and within a minute you want to consume the data you don't have to update it because it's already updated it's I think it's not clear for some no why the reason is if you see this um we the what does Hermes do is we are getting data from Wormhole and then you are fetching from Hermes that data is getting verified so contract already has the data so what it does it verifies the data so she can't put up pull up malicious data that's how we do verification so always get correct data which is there and you can fetch data anytime you want so that's way how polar orle works so do we need to put a check in there let's say go back to Cod let's say that I updated already and it's already updated yes so that method update price feeds already have that check so if you if you're already updating it so if it the data is already there it won't update it so we have an API reference uh I am helping you to uh like navigate the dogs so that you get everything if you go there uh API reference page evm and you can see all the prices that contract supports so if you see the get price I will defin I'm pretty sure everybody will face this issue the get price will give you the latest data like uh there's an example execute the query there's a stale price error coming why because this price is not being updated okay so every price you update it will get stale of after 60 seconds that's how we keep the freshness on the Chain okay so but now you update the price and you try to fetch it within 60 seconds you will get the price it will not throw this error does that mean zero let's say that he updated it yes so my yes but that means that my contract needs to have oh it it can take from message or sender the eoa and if you if don't care about the latest price you can use the get price unsave method so it will always return you something will that help me with my issue with the yes so I should use get price yes you can use that and uh you can fetch and like this I want data not older than 4 seconds so a lot of Perpetual dexes they don't want data very old so they can use this I don't want seconds older than 5 Seconds you can put the age here that can happen EMA price I'm not sure if you got want to use it's like exponentially moved average price similar methods and these are like uh update fees you can get on your chain what's update fees it's one right now for every chain but you can try if you want to so in a like if you see Hermes we have SDK also for Hermes typescript python both but it's a normal curl command I don't think you will need an SDK for that but we are there it's there so if you if you use this API uh I'm going to try it out you can add so many price feeds in one call so in the same contract if you want to fetch five price feeds you don't have to update five times you can use update one time you get like I'll show you suppose these are five different price feeds I'm not doing it uh one hex encoding and then execute take this this is update data that you will use it as an argument in the contract okay the difference here for you guys as new Vel opers who is using p you have used oracles before it's easier to use an oracle that they're out there because you can directly fetch it you don't have to do all this thing but to get started you have to put uh some token somewhere you have used those kind of oracles we don't need to do everything is permissionless you don't need any human being to intervene in between or any of our community members that's one thing second it's one way for update third you are getting latest prices that is something different so if you see all the oracles are moving towards full Oracle because they can't expand the reason we could expand to Every Chain out there it's only one price feed ID for all the chains that's also remember if you're using btcusd on five chains you have to hardcode those BTC USD price feed on every chain different IDs it's one ID so when you see all the price feed IDs here this is ID that we use on all evm chain salana stocket whatever you call so that's one good thing about soat Us One D update yes but I get two yeah two IDs inside put that in Hermes you get the update data or via SDK and push it to the contract you pay two way because two price so should I show how that would be but then yes yes yes is how de are yeah so uh if you when you get the price of price object it has price decimals confidence interval as well last publish time I'll show you how like from home is if you want to use offchain prices you don't want to use onchain prices you can use hmes to fetch the prices and the price object looks like this price confidence interval exponential which is the decimals you need and the publish time last publish type okay so I'm I need to the negative number to a positive number and we have a method if you uh we have a helper method sorry it's common for me to yes this is the convert print to X USD you can uh we have helper methods in our SDK you can use that and uh since I have two minutes I'll just explain how entropy Works uh this is a second bounty we have for random number it's quite different from any random number that you have used there so if you're want to use random number for gaming or something the only difference here is uh how do you use a random number you fetch the random number that's it but it's a two commit hash so if I have a random number you have a random numbers you have a random number the random number that entropy will give you is a has of two so it's like I can't change my random number because hash would get changed you can't change your random number hash would change so if you want to use entropy you will call this fetch entropy random number and you'll pass your random number okay any random you want to put that you'll pass that you get the fees which is whatever there and you check the fees and you'll see you get a sequence number from that request with call back you'll get a sequence number which will store on the Chain after that there's a entropy call back which will be done by the pit decentralized uh service which generates that random number from your and it will uh do a call back entropy call back and with a random number which you can consume that's how entropy Works uh that's the only fees you have to pay you don't have to link some tokens or something like that that's another entropy this is more safer because you can see how random numbers are generated it's a two commit it's it BAS based on you if you don't change a random number you'll get the actual random number I have to pack I have to pack a lot of things in 20 minutes that's why I have to rush it I will be available just outside of this for any AMA I know for me also as a Dev I took a lot of time to explain how things are happening but I'm happy to help on this okay we have we can come see yes and I'll be putting some links on the E Global website my personal telegram if I you can not see me here you can ping me there [Music]
Automatic transcript — names and jargon may be misspelled.