-
Notifications
You must be signed in to change notification settings - Fork 33
chore: adds docs for integrating exchanges #2994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
b443b4e
659e267
e08cee3
57ee6a8
bcaa477
beb8485
835997e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,87 +1,141 @@ | ||||||||||||
| # Integrating Exchanges | ||||||||||||
| --- | ||||||||||||
| title: Decentralized exchange (DEX) integration guide | ||||||||||||
| description: "Quick reference for common tasks that enable an exchange to interact with the Kadena blockchain." | ||||||||||||
| id: dex-integration | ||||||||||||
| sidebar_position: 5 | ||||||||||||
| --- | ||||||||||||
|
|
||||||||||||
| This document provides a guide how Kadena blockchain can be integrated with | ||||||||||||
| exchanges. | ||||||||||||
| # Decentralized exchange (DEX) integration guide | ||||||||||||
|
|
||||||||||||
| Typical features that exchanges may utilize include are listed below | ||||||||||||
| This guide provides a quick reference for how to perform the most common tasks when an exchange or a similar application needs to interact with the Kadena blockchain. | ||||||||||||
| The information is this guide is similar to topics covered elsewhere in the documentation. | ||||||||||||
| For example, you can see similar information in the following topics: | ||||||||||||
|
|
||||||||||||
| - [Integrating Exchanges](#integrating-exchanges) | ||||||||||||
| - [Low Level Client](#low-level-client) | ||||||||||||
| - [Retrieve balance](#retrieve-balance) | ||||||||||||
| - [Generate an account on the blockchain](#generate-an-account-on-the-blockchain) | ||||||||||||
| - [Create, sign and send a transaction](#create-sign-and-send-a-transaction) | ||||||||||||
| - [Check status of a transaction](#check-status-of-a-transaction) | ||||||||||||
| - [Single check](#single-check) | ||||||||||||
| - [Polling check](#polling-check) | ||||||||||||
| - [Estimate Gas Usage](#estimate-gas-usage) | ||||||||||||
| - [Listens for Events](#listens-for-events) | ||||||||||||
| - [Chainweb Stream](#chainweb-stream) | ||||||||||||
| - [Third-party Services](#third-party-services) | ||||||||||||
| - [Custom implementation](#custom-implementation) | ||||||||||||
| - [Common task quick reference](/guides/howto-quick-ref) | ||||||||||||
| - [Create new accounts](/guides/accounts/howto-create-accounts) | ||||||||||||
| - [Check account balances](/guides/accounts/howto-get-balances) | ||||||||||||
| - [Sign and submit transactions](/guides/transactions/howto-sign-submit-tx) | ||||||||||||
|
|
||||||||||||
| ## Low Level Client | ||||||||||||
| However, this guide assumes that you are operating an exchange and need to interact with the Kadena blockchain programmatically using scripts and, potentially, automation. | ||||||||||||
|
|
||||||||||||
| An exchange usually hosts their own node to interact with the Kadena blockchain. | ||||||||||||
| This guide also assumes the following: | ||||||||||||
|
|
||||||||||||
| They might want to limit the sub-chains they work with, so a custom host | ||||||||||||
| generator function can be used to connect to specific chains. | ||||||||||||
| - You host one or more Chainweb nodes that you control and monitor. | ||||||||||||
| - You have one or more Chainweb nodes that connect to the Kadena test network or main production network. | ||||||||||||
| - You want to use the Kadena client libraries to write scripts that perform common tasks, like creating transactions and listening for events. | ||||||||||||
|
|
||||||||||||
| Typically, an exchange want to wait for more confirmations than a regular user. | ||||||||||||
| This guide includes code examples written in TypeScript for all of the common exchange-related activity. | ||||||||||||
| If you aren't already hosting your own Chainweb node, see [Get started running a node](/guides/nodes/howto-node-operator) for an introduction to setting up a Chainweb node. | ||||||||||||
|
|
||||||||||||
| See for more details on how to use the **low-level client** | ||||||||||||
| [docs.kadena.io](https://docs.kadena.io/reference/kadena-client#get-started-with-kadena-client) | ||||||||||||
| ## Connect to specific chains | ||||||||||||
|
|
||||||||||||
| ```ts | ||||||||||||
| You can interact with the Kadena network through any of its separate chains. | ||||||||||||
| The chains—with chain identifiers 0 through 19—share a common view of state, but operate independently and in parallel. | ||||||||||||
|
|
||||||||||||
| Because the chains operate independently and you must specify the chain identifier that you want to send transactions to, most exchanges limit the number of chains they work with. | ||||||||||||
lsgunnlsgunn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
|
|
||||||||||||
| If you want to limit the chains you work with, you can write a custom **host generator** function to connect to specific chains. | ||||||||||||
| The following example illustrates creating a Kadena **client** connection that only connects to chains 0, 1, and 2: | ||||||||||||
lsgunnlsgunn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
|
|
||||||||||||
| ```typescript | ||||||||||||
| // Import the createClient function from the library. | ||||||||||||
| import { createClient } from '@kadena/client'; | ||||||||||||
|
|
||||||||||||
| // An exchange may want to only work with a limited set of sub-chains. | ||||||||||||
| // Generally an Exchange will host their own node | ||||||||||||
| // Generally, exchanges host their own nodes and | ||||||||||||
| // only interact with a limited set of chains. | ||||||||||||
lsgunnlsgunn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
| const customHostGeneratorFunction = ({ networkId, chainId }) => { | ||||||||||||
| if (![0, 1, 2].includes(chainId)) { | ||||||||||||
| throw new Error('This Exchange only works with chains 0, 1, and 2'); | ||||||||||||
| throw new Error('Exchange only conducts business on chains 0, 1, and 2.'); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| const hostname = 'kadena-node.my-exchange.tld'; | ||||||||||||
| return `https://${hostname}/chainweb/0.0/${networkId}/chain/${chainId}/pact`; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| // This is a lower-level client that can be used to connect to the blockchain. | ||||||||||||
| // This call creates a client that can connect to the blockchain. | ||||||||||||
| const client = createClient(customHostGeneratorFunction, { | ||||||||||||
| confirmationDepth: 10, // an Exchange may want to wait for more confirmations | ||||||||||||
| // Exchanges typically wait for more blocks to be confirmed by consensus. | ||||||||||||
| confirmationDepth: 10, | ||||||||||||
| }); | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ## Retrieve balance | ||||||||||||
| If you haven't already downloaded and installed the `@kadena/client` libraries, see [Get started with Kadena client](/reference/kadena-client#get-started-with-kadena-client) for an introduction to creating and using client connections to interact with Chainweb nodes and the Kadena blockchain. | ||||||||||||
|
||||||||||||
| If you haven't already downloaded and installed the `@kadena/client` libraries, see [Get started with Kadena client](/reference/kadena-client#get-started-with-kadena-client) for an introduction to creating and using client connections to interact with Chainweb nodes and the Kadena blockchain. | |
| If you haven't already downloaded and installed the `@kadena/client` libraries, see [Get started with Kadena client](/reference/kadena-client#get-started-with-kadena-client) for an introduction to interact with the Kadena blockchain. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose to make this more concise. If they read to this section, they've already seen the code use @kadena/client.
| If you haven't already downloaded and installed the `@kadena/client` libraries, see [Get started with Kadena client](/reference/kadena-client#get-started-with-kadena-client) for an introduction to creating and using client connections to interact with Chainweb nodes and the Kadena blockchain. | |
| You should also download and install the helper utilities defined in the `@kadena/client-utils` library. | |
| This library provides simplified functions for interacting with the Kadena `coin` contract and developing your own custom interfaces. | |
| For more information about the helper utilities, see the [client-utils](https://github.com/kadena-community/kadena.js/tree/main/packages/libs/client-utils) repository and [@kadena/client-utils](https://www.npmjs.com/package/@kadena/client-utils) package registry. | |
| Install `@kadena/client` (see [Get started with Kadena client](/reference/kadena-client#get-started-with-kadena-client)) and `@kadena/client-utils`, which simplifies interactions with Kadena's coin contract and custom interfaces. Documentation: [client-utils repo](https://github.com/kadena-community/kadena.js/tree/main/packages/libs/client-utils) or [npm package](https://www.npmjs.com/package/@kadena/client-utils). |
lsgunnlsgunn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be useful to include other methods to sign the transaction as an alternative to signWithChainweaver.
I can imagine this will be very confusing when this doesn't work with wallet.kadena.io, or when we don't demonstrate the signWithKeypair: https://github.com/kadena-community/kadena.js/blob/main/packages/libs/client/src/signing/keypair/createSignWithKeypair.ts#L65-L93.
Caution
BTW, this is also not documented in the README.md of @kadena/client
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's really confusing. I ran into massive problems signing transactions when I tried to update the election workshop to remove the signWithChainweaver function.
After a lot of monkeying around trying to make things work, the best I could do was brute force with a k: account and a generated key pair:
const electionKeyPair:IKeyPair = {
publicKey:
"02b055c8be0eeaa659d0927f3e2399080c91f3fdf94d079498b04d6987acbd46",
secretKey:
'5a02489796c9ec2ec74edf15b63140224d0516ce6cd8f62303ac63b56a45c336',
};
const signWithKeypair = createSignWithKeypair([electionKeyPair]);
const signedTx = await signWithKeypair(unsignedTransaction);
I'll put in whatever code you provide (maybe even some wallet adapter examples?), but it is frustrating to have examples that don't actually work.
Uh oh!
There was an error while loading. Please reload this page.