This commit is contained in:
sayantank 2022-06-08 13:35:49 +00:00
parent 596bb74307
commit 22b494b470
7 changed files with 29 additions and 27 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -8,7 +8,7 @@
<h2>Prerequisites</h2>
</a>
<p>You will need to use the <code>FileKeypair</code> class that extends the <a href="https://solana-labs.github.io/solana-web3.js/classes/Keypair.html"><code>Keypair</code></a> class from <a href="https://npmjs.com/package/@solana/web3.js"><code>@solana/web3.js</code></a> to provide easy file-based keypair management, which is required for the market making features provided by this package.</p>
<pre><code><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">owner</span><span class="hl-1"> = </span><span class="hl-3">FileKeypair</span><span class="hl-1">.</span><span class="hl-4">generate</span><span class="hl-1">(</span><span class="hl-5">&quot;./scripts/keys/owner.json&quot;</span><span class="hl-1">);</span><br/><br/><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">airdropSig</span><span class="hl-1"> = </span><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">connection</span><span class="hl-1">.</span><span class="hl-4">requestAirdrop</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">.</span><span class="hl-3">publicKey</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-7">10</span><span class="hl-1"> * </span><span class="hl-2">LAMPORTS_PER_SOL</span><span class="hl-1">,</span><br/><span class="hl-1">);</span><br/><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">connection</span><span class="hl-1">.</span><span class="hl-4">confirmTransaction</span><span class="hl-1">(</span><span class="hl-3">airdropSig</span><span class="hl-1">);</span>
<pre><code class="language-javascript"><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">owner</span><span class="hl-1"> = </span><span class="hl-3">FileKeypair</span><span class="hl-1">.</span><span class="hl-4">generate</span><span class="hl-1">(</span><span class="hl-5">&quot;./scripts/keys/owner.json&quot;</span><span class="hl-1">);</span><br/><br/><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">airdropSig</span><span class="hl-1"> = </span><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">connection</span><span class="hl-1">.</span><span class="hl-4">requestAirdrop</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">.</span><span class="hl-3">publicKey</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-7">10</span><span class="hl-1"> * </span><span class="hl-2">LAMPORTS_PER_SOL</span><span class="hl-1">,</span><br/><span class="hl-1">);</span><br/><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">connection</span><span class="hl-1">.</span><span class="hl-4">confirmTransaction</span><span class="hl-1">(</span><span class="hl-3">airdropSig</span><span class="hl-1">);</span>
</code></pre>
<p>You need to have a <a href="https://github.com/project-serum/serum-dex/">serum-dex</a> program deployed on the cluster you wish to interact with.</p>
<p>You can either,</p>
@ -22,28 +22,30 @@
<a href="#get-started" id="get-started" style="color: inherit; text-decoration: none;">
<h2>Get Started</h2>
</a>
<pre><code><span class="hl-3">yarn</span><span class="hl-1"> </span><span class="hl-3">add</span><span class="hl-1"> @</span><span class="hl-3">project</span><span class="hl-1">-</span><span class="hl-3">serum</span><span class="hl-1">/</span><span class="hl-3">serum</span><span class="hl-1">-</span><span class="hl-3">dev</span><span class="hl-1">-</span><span class="hl-3">tools</span>
</code></pre>
<a href="#initialize-a-dex" id="initialize-a-dex" style="color: inherit; text-decoration: none;">
<h3>Initialize a <code>Dex</code></h3>
</a>
<pre><code><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">connection</span><span class="hl-1"> = </span><span class="hl-0">new</span><span class="hl-1"> </span><span class="hl-4">Connection</span><span class="hl-1">(</span><span class="hl-5">&quot;http://localhost:8899&quot;</span><span class="hl-1">, </span><span class="hl-5">&quot;confirmed&quot;</span><span class="hl-1">);</span><br/><br/><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">dexAddress</span><span class="hl-1"> = </span><span class="hl-0">new</span><span class="hl-1"> </span><span class="hl-4">PublicKey</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-5">&quot;7zo7HCQAZPRb4pYiQQ6fLjC8ssN3E8LkavVs8JUA5NMn&quot;</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">dex</span><span class="hl-1"> = </span><span class="hl-0">new</span><span class="hl-1"> </span><span class="hl-4">Dex</span><span class="hl-1">(</span><span class="hl-3">dexAddress</span><span class="hl-1">, </span><span class="hl-3">connection</span><span class="hl-1">);</span>
<pre><code class="language-javascript"><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">connection</span><span class="hl-1"> = </span><span class="hl-0">new</span><span class="hl-1"> </span><span class="hl-4">Connection</span><span class="hl-1">(</span><span class="hl-5">&quot;http://localhost:8899&quot;</span><span class="hl-1">, </span><span class="hl-5">&quot;confirmed&quot;</span><span class="hl-1">);</span><br/><br/><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">dexAddress</span><span class="hl-1"> = </span><span class="hl-0">new</span><span class="hl-1"> </span><span class="hl-4">PublicKey</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-5">&quot;7zo7HCQAZPRb4pYiQQ6fLjC8ssN3E8LkavVs8JUA5NMn&quot;</span><span class="hl-1">,</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">dex</span><span class="hl-1"> = </span><span class="hl-0">new</span><span class="hl-1"> </span><span class="hl-4">Dex</span><span class="hl-1">(</span><span class="hl-3">dexAddress</span><span class="hl-1">, </span><span class="hl-3">connection</span><span class="hl-1">);</span>
</code></pre>
<a href="#create-coin-instances-for-your-market" id="create-coin-instances-for-your-market" style="color: inherit; text-decoration: none;">
<h3>Create <code>Coin</code> instances for your <code>Market</code></h3>
</a>
<pre><code><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">baseCoin</span><span class="hl-1"> = </span><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">dex</span><span class="hl-1">.</span><span class="hl-4">createCoin</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-5">&quot;SAYA&quot;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-7">9</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">quoteCoin</span><span class="hl-1"> = </span><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">dex</span><span class="hl-1">.</span><span class="hl-4">createCoin</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-5">&quot;SRM&quot;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-7">9</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-8">// Fund the FileKeypair object to place orders.</span><br/><br/><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">baseCoin</span><span class="hl-1">.</span><span class="hl-4">fundAccount</span><span class="hl-1">(</span><span class="hl-7">1000000</span><span class="hl-1">, </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">, </span><span class="hl-3">connection</span><span class="hl-1">);</span><br/><br/><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">quoteCoin</span><span class="hl-1">.</span><span class="hl-4">fundAccount</span><span class="hl-1">(</span><span class="hl-7">2000000</span><span class="hl-1">, </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">, </span><span class="hl-3">connection</span><span class="hl-1">);</span><br/>
<pre><code class="language-javascript"><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">baseCoin</span><span class="hl-1"> = </span><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">dex</span><span class="hl-1">.</span><span class="hl-4">createCoin</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-5">&quot;SAYA&quot;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-7">9</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">quoteCoin</span><span class="hl-1"> = </span><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">dex</span><span class="hl-1">.</span><span class="hl-4">createCoin</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-5">&quot;SRM&quot;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-7">9</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-8">// Fund the FileKeypair object to place orders.</span><br/><br/><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">baseCoin</span><span class="hl-1">.</span><span class="hl-4">fundAccount</span><span class="hl-1">(</span><span class="hl-7">1000000</span><span class="hl-1">, </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">, </span><span class="hl-3">connection</span><span class="hl-1">);</span><br/><br/><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">quoteCoin</span><span class="hl-1">.</span><span class="hl-4">fundAccount</span><span class="hl-1">(</span><span class="hl-7">2000000</span><span class="hl-1">, </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">, </span><span class="hl-3">connection</span><span class="hl-1">);</span>
</code></pre>
<a href="#initialize-a-market" id="initialize-a-market" style="color: inherit; text-decoration: none;">
<h3>Initialize a <code>Market</code></h3>
</a>
<pre><code><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">market</span><span class="hl-1"> = </span><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">dex</span><span class="hl-1">.</span><span class="hl-4">initDexMarket</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">baseCoin</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">quoteCoin</span><span class="hl-1">,</span><br/><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-3">lotSize:</span><span class="hl-1"> </span><span class="hl-7">1e-3</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">tickSize:</span><span class="hl-1"> </span><span class="hl-7">1e-2</span><span class="hl-1">,</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">);</span>
<pre><code class="language-javascript"><span class="hl-0">const</span><span class="hl-1"> </span><span class="hl-2">market</span><span class="hl-1"> = </span><span class="hl-6">await</span><span class="hl-1"> </span><span class="hl-3">dex</span><span class="hl-1">.</span><span class="hl-4">initDexMarket</span><span class="hl-1">(</span><span class="hl-3">owner</span><span class="hl-1">.</span><span class="hl-3">keypair</span><span class="hl-1">, </span><span class="hl-3">baseCoin</span><span class="hl-1">, </span><span class="hl-3">quoteCoin</span><span class="hl-1">, {</span><br/><span class="hl-1"> </span><span class="hl-3">lotSize:</span><span class="hl-1"> </span><span class="hl-7">1e-3</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">tickSize:</span><span class="hl-1"> </span><span class="hl-7">1e-2</span><span class="hl-1">,</span><br/><span class="hl-1">});</span>
</code></pre>
<a href="#run-a-market-maker" id="run-a-market-maker" style="color: inherit; text-decoration: none;">
<h3>Run a Market Maker</h3>
</a>
<pre><code><span class="hl-3">dex</span><span class="hl-1">.</span><span class="hl-4">runMarketMaker</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-3">market</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">owner</span><span class="hl-1">,</span><br/><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-3">durationInSecs:</span><span class="hl-1"> </span><span class="hl-7">30</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">orderCount:</span><span class="hl-1"> </span><span class="hl-7">3</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">initialBidSize:</span><span class="hl-1"> </span><span class="hl-7">1000</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">baseGeckoSymbol:</span><span class="hl-1"> </span><span class="hl-5">&quot;solana&quot;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">quoteGeckoSymbol:</span><span class="hl-1"> </span><span class="hl-5">&quot;usd&quot;</span><span class="hl-1">,</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">);</span>
<pre><code class="language-javascript"><span class="hl-3">dex</span><span class="hl-1">.</span><span class="hl-4">runMarketMaker</span><span class="hl-1">(</span><span class="hl-3">market</span><span class="hl-1">, </span><span class="hl-3">owner</span><span class="hl-1">, {</span><br/><span class="hl-1"> </span><span class="hl-3">durationInSecs:</span><span class="hl-1"> </span><span class="hl-7">30</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">orderCount:</span><span class="hl-1"> </span><span class="hl-7">3</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">initialBidSize:</span><span class="hl-1"> </span><span class="hl-7">1000</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">baseGeckoSymbol:</span><span class="hl-1"> </span><span class="hl-5">&quot;solana&quot;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">quoteGeckoSymbol:</span><span class="hl-1"> </span><span class="hl-5">&quot;usd&quot;</span><span class="hl-1">,</span><br/><span class="hl-1">});</span>
</code></pre>
</div></div><div class="col-4 col-menu menu-sticky-wrap menu-highlight"><nav class="tsd-navigation primary"><ul><li class="current"><a href="modules.html">Exports</a></li></ul></nav><nav class="tsd-navigation secondary menu-sticky"><ul><li class="tsd-kind-class"><a href="classes/Coin.html" class="tsd-kind-icon">Coin</a></li><li class="tsd-kind-class"><a href="classes/Dex.html" class="tsd-kind-icon">Dex</a></li><li class="tsd-kind-class"><a href="classes/DexMarket.html" class="tsd-kind-icon">Dex<wbr/>Market</a></li><li class="tsd-kind-class"><a href="classes/FileKeypair.html" class="tsd-kind-icon">File<wbr/>Keypair</a></li><li class="tsd-kind-interface"><a href="interfaces/MarketAccounts.html" class="tsd-kind-icon">Market<wbr/>Accounts</a></li><li class="tsd-kind-type-alias"><a href="modules.html#MarketMakerOpts" class="tsd-kind-icon">Market<wbr/>Maker<wbr/>Opts</a></li><li class="tsd-kind-type-alias"><a href="modules.html#MarketParams" class="tsd-kind-icon">Market<wbr/>Params</a></li><li class="tsd-kind-type-alias"><a href="modules.html#OrderType" class="tsd-kind-icon">Order<wbr/>Type</a></li><li class="tsd-kind-type-alias"><a href="modules.html#TransactionWithSigners" class="tsd-kind-icon">Transaction<wbr/>With<wbr/>Signers</a></li></ul></nav></div></div></div><footer class="with-border-bottom"><div class="container"><h2>Legend</h2><div class="tsd-legend-group"><ul class="tsd-legend"><li class="tsd-kind-constructor tsd-parent-kind-class"><span class="tsd-kind-icon">Constructor</span></li><li class="tsd-kind-property tsd-parent-kind-class"><span class="tsd-kind-icon">Property</span></li><li class="tsd-kind-method tsd-parent-kind-class"><span class="tsd-kind-icon">Method</span></li></ul><ul class="tsd-legend"><li class="tsd-kind-property tsd-parent-kind-interface"><span class="tsd-kind-icon">Property</span></li></ul><ul class="tsd-legend"><li class="tsd-kind-property tsd-parent-kind-class tsd-is-private"><span class="tsd-kind-icon">Private property</span></li></ul><ul class="tsd-legend"><li class="tsd-kind-method tsd-parent-kind-class tsd-is-static"><span class="tsd-kind-icon">Static method</span></li></ul></div><h2>Settings</h2><p>Theme <select id="theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></p></div></footer><div class="container tsd-generator"><p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></div><div class="overlay"></div><script src="assets/main.js"></script></body></html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long