Original

DeSo Ecosystem Openfund DEX White Paper Document (2)

This article is machine translated
Show original

Continuing the previous article: DeSo Ecosystem Openfund DEX Whitepaper Document (1)

DeSo Python Development Toolkit

Everything needed to build, sign, and submit transactions is in the DeSo Python SDK. This is a high-level library that makes it incredibly easy to perform all of DeSo's basic (and not-so-basic) transaction types.

The best way to learn is to simply browse the README and run the code. Once you have everything restored to a "success" state, you can proceed to tackle challenges while guiding you to build advanced market-making bots and advanced social bots.

Read the following sections to learn how to get help from the community and test your knowledge.

Getting Help from the Community

First, if you run into trouble and want to talk to someone, the DeSo PoS Discussion Telegram channel is a great resource. Everyone running nodes is there, and members of the DeSo core team are there as well. Many people there are usually very knowledgeable about the ins and outs of the DeSo blockchain and very helpful to new users trying to understand what's going on. We used to run a Discord, but we found a simple developer-centric Telegram channel works better.

If you encounter issues during swaps, the HeroSwap Support channel can help you. This shouldn't be necessary, but for completeness, we've included it here.

https://docs.deso.org/openfund/the-deso-python-sdk

Creating a DeSo Testnet Account

First, if you're going to write a trading bot, it's best to familiarize yourself with the DeSo node testnet UI (accessible here) and the Openfund testnet UI (accessible here). This will allow you to do everything with "fake money" so that you don't put capital at risk before you're sure everything is working.

To set up a testnet account, simply follow these steps:

  1. This is the testnet reference node that most developers start using when testing. Note that anyone can run a DeSo node according to the instructions in the core repository, and this happens to be a fairly reliable node.
  2. Create an account
  3. Note that the DeSo wallet is managed locally in your browser. The DeSo wallet works almost exactly like MetaMask, except that it doesn't require installing a Chrome extension, and it supports more granular and transparent permissions at the application level. This means that creating a wallet on one application will make that wallet accessible to any other DeSo application, including Openfund, Diamond, and Focus (note that these are mainnet links, not testnet links, so your testnet wallet won't be accessible there).
    1. Note that when you use an application, a "derived key" is issued, which has much more limited permissions than your main key, and you must accept this key before using the application. This allows you to use the application without having to click "confirm" for low-value transactions (like posting or liking posts), while being 100% certain the application won't steal funds.
  4. We recommend always using a seed phrase for test accounts, as this is often easier to manage. You can use one seed phrase and create new accounts by clicking "Add Account" in the wallet, with different indexes.
  5. When creating an account, you can enter your phone number to get 1 free testnet $DESO from the faucet
  6. Note that testnet $DESO has no value outside the test environment!
  7. If you don't want to enter a phone number, some applications use advanced verification code flows, like dev.openfund.com. Eventually, this flow will be re-integrated into the reference node.
  8. Once you have a Starter DESO account, you can access the following links for testing:
    1. Here, you can log in and stake your DESO as needed.
    1. Here, you can place orders and use the inspector to see the transactions being built and submitted to the DeSo blockchain.

For a reference of all useful testnet links (including the testnet block explorer), see here.

For tips on creating lots of mainnet accounts with starter DESO, see here.

For an introduction to building DeSo applications, see here. This can serve as a reference, or if you find some of the content in this guide confusing.

Debugging Tips and Code Walkthrough

The DeSo blockchain is written in the Go programming language to achieve high performance, and its code is 100% open-source, with the core and backend repositories being the most important for understanding the node architecture and transaction building, respectively. The core repository represents all the most critical code for processing transactions, while the backend repository is mainly composed of the REST endpoints you can call to build and submit transactions. If you're curious about them, it would be useful to load these two repositories into your IDE to explore them (we recommend VSCode or Goland; our team uses both software libraries). That said, you don't need to understand Go to build and submit transactions! In this section, we'll show you how to use a simple Python library to build, sign, and submit all the basic order management transactions needed to write a trading bot.

The Openfund client builds, signs, and submits the exact same transactions that we'll be handling in this tutorial (mainnet client, testnet client). This means that if you're unsure how to do something, you can simply open the Openfund trade page, open the inspector, go to the network tab, and see how Openfund builds and submits transactions (if you're not familiar with this, look for a tutorial on how to use the Web inspector to do this). The only thing you won't be able to get from this process is the transaction signing, which we'll cover in this tutorial, and the Python library has already implemented for you. In addition to using Openfund to inspect things, you can also use the reference DeSo node (mainnet node, testnet node), which supports a larger set of transactions, including on-chain posting, following users, and so on. When Focus launches, you'll also have more transaction types to explore (Focus mainnet, Focus testnet). Generally, when the core team adds new transaction types, they'll first test them on the reference DeSo node, so these are usually the most "complete" places to understand how transactions work. For example, in this tutorial, you can see on the "DAO" tab of the reference client how tokens are minted, burned, and sent. However, for all transactions, Openfund is the best place to inspect them.

Here is the English translation of the text, with the specified terms translated and the content within <> tags left untranslated:

Furthermore, if you want to get more "hardcore", you can read the DeSo node code itself to see how the transactions you are trying to build are actually processed in the backend. All the endpoints backed by the backend have a RoutePath* variable, such as RoutePathUpdateProfile. If you look for the usage, you'll find they are linked to RoutePath in this format. This allows you to trace what the endpoint is doing (which can be useful if you get weird errors). The open-source DeSo node code also provides the Go functions responsible for building transactions, which all follow a common naming scheme of Create*Txn, such as CreateUpdateProfileTxn, and if you trace the RoutePath of a txn construction endpoint, you'll typically see one of these functions. Each transaction type supported by the DeSo blockchain has an _connect function implemented, such as connectUpdateProfile, which is called when the nodes on the network actually process your specific transaction. To see all the transaction types supported by the DeSo blockchain, you just need to search for all _connect functions in the core repository, and then find the Create functions that call them (which may be in the backend repository). While this information is not absolutely necessary, it can be useful if you're unsure why your transaction is being rejected by the network, or if you want to see what parameters you can provide for a specific transaction type. The best debugging approach is to find the _connect function you're trying to trigger, and then trace it back to the function that actually constructs the transaction, to see what it's doing. Additionally, if you encounter errors, you can always find them in the core+backend repositories if you have them loaded in your IDE. So, in summary:

  • To see all the possible endpoints you can click on to build transactions or fetch data, look for RoutePath* in the core+backend repositories.
  • Find the called functions and trace them to see how transactions are constructed or how data is returned.
  • To see all the txn types the nodes can handle, look for all the _connect functions in core and read through them, or find the one applicable to your specific transaction type.
  • The Create*Txn functions are responsible for constructing transactions, and are typically called in the txn construction endpoints. If you encounter weird errors, you may want to trace them.

The key transactions we care about (due to legacy reasons, DeSo tokens are referred to as "DAO Coins" in the code) are:

  • _connectDaoCoin
  • For minting and burning your tokens, or changing transfer limits
  • _connectDaoCoinLimitOrder
  • For creating market and limit orders, as well as cancelling orders
  • _connectDaoCoinTransfer
  • For transferring your tokens to other accounts
  • We won't cover them here, but you may also be interested in the following functions that allow you to earn yield on your coins:
  • _connectCoinLockup
  • _connectCoinLockupTransfer
  • _connectUpdateCoinLockupParams

If needed, you can trace how the above functions ultimately call into RoutePath, which will then tell you how to trigger them with simple HTTP requests. However, it's usually easier to just inspect the HTTP requests in existing applications (like Openfund) and work "top-down" from the RoutePath, so we generally recommend that approach. We just wanted to provide you with both options, so you have maximum debugging capability.

Whenever you construct transactions, as we'll show you how to do in the next section, you may receive a very long and hard-to-read error message. The key to deciphering these is to keep going all the way to the end and look for the RuleError you get. For example, if you don't have DESO or something similar, you'll typically get a RuleErrorInsufficientBalance. The RuleError should always tell you what went wrong, and it should be clear. But if it's not, then you can dive into the code as described above and trace what happened. That's the beauty of 100% open-source software!

Using AI to Write Blockchain Bots

You may have noticed that the DeSo Python SDK is a Python file. This is hugely valuable because it means you can just "drop" the entire SDK into your favorite AI, and have it write new functions for you, or combine the existing ones in novel ways. Keep this in mind as you go through the rest of the exercises in this section. It may be helpful to load the SDK into the AI before starting, so you're ready to ask it questions!

Market Making Bot

Using the DeSo Python SDK, complete the following challenge exercise and build a fully functional market making bot:

  1. Use the get_limit_orders function to get the mid-market price for $openfund in the openfund/deso market. Be careful to distinguish BID-looking ASKs and vice versa!
  2. Place a market buy order for 0.000001 DESO worth of $openfund. You should be able to do this using just your starting DESO.
  3. After executing the market order, check your $openfund balance to confirm you have the expected amount of $openfund.
  4. Place a limit buy order for $openfund slightly below the mid-market price, and a limit sell order for $openfund slightly above the mid-market price. Now that you have the $openfund from the previous step and $DESO, you should be able to do this! The orders should "sit" on the orderbook, rather than execute immediately. Save the order_id of the transactions, so you can manage the order state! The order_id is just the signature txn hash of the transaction you used to place the order.
  5. Use get_limit_orders to determine if your orders have been filled. The orders will be executed when they are no longer visible on the orderbook.
  6. Practice using the SDK to cancel and replace one of your orders, passing the order_id you got when you placed the order.
  7. Write a simple routine to "flip" your buy order into a sell order (at a slightly higher price, so you can earn the "spread") after it's been filled. Do the same for your other limit orders.
  8. Advanced: Obtain $100 worth of $openfund and $100 worth of $DESO. Use an ATOMIC transaction to place 10 bids and 10 asks on the mid-market, each for $10.
  9. Advanced: Write a routine to "flip" your asks into bids (using the spread, so you can make some money off the volatility!) when they get filled. Do the same for the bids.
  10. Congratulations! If you've made it this far, you are now officially a DeSo DEX market maker! The AMM that provides liquidity for Focus and Openfund is essentially a highly sophisticated and scaled-up version of what you just did.

Social AI Agent

Using the DeSo Python SDK, build a new type of entertaining (and potentially profitable) social agent:

  1. Use the SDK to create a Post from your account. Properly running the main should have already accomplished this. Edit the text to make it more interesting.
  2. Use the SDK to track @nadertheory on the testnet, and @nader on the mainnet.
  3. Use the SDK to repost content from your account. Repost using the same submit_post but setting the RepostedPostHashHex.
  4. Use the SDK to comment on someone's post. The comment is just a post with the ParentPostHash set.
  5. Advanced: Use the SDK to write a bot that queries an AI API to automatically reply to all your posts with meaningful and useful content.
  6. Advanced: Use the SDK to write a bot that automatically replies to anyone who comments on your posts with meaningful content from your personal account.
  7. ADVANCED: Use the SDK to send a paid message to someone.

AI Generating Your Code

While the DeSo Python SDK has many useful features, sometimes the simplest way is to create your own transaction flow by observing another application (like Openfund or Focus). This section will show you how to do this, and how to use AI to automatically generate the code:

  1. Navigate to the "DAO Coin" tab on node.deso.org/Openfund here. Remember, "DAO Coin" is just the old term for "DeSo Token"!
  2. Open the Web Inspector in your browser and navigate to the Network tab.
  3. Filter the requests to find get-hodlers-for-public-key. Click "Copy as CURL" to get the parameters used in the request. Also, note the "Copy Response" as we'll use that too.
  4. After loading the sdk into your favorite AI tool, paste the "Copy as CURL" result and the "Copy Response" result into the chat, and ask it to add a function to get all the holders for a given token.
  5. Congratulations! You just added a NEW FUNCTION to the SDK! You can use this process to automate anything you do on openfund.com, focus.xyz, and any DeSo application!

PS: We may have used this exact method to write some of the DeSo Python SDK. Shh, don't tell anyone! 🥲

How Does Advanced AMM Work?

Coming soon! (After Focus launches).

In the meantime, our advanced AMM is active on the DESO, OPENFUND, BTC, ETH, and SOL markets on Openfund.

Disclaimer: The content above is only the author's opinion which does not represent any position of Followin, and is not intended as, and shall not be understood or construed as, investment advice from Followin.
Like
Add to Favorites
Comments