project-serum / serum-ts

Project Serum TypeScript monorepo
https://projectserum.com
Apache License 2.0
273 stars 246 forks source link

JS example throwing errors #78

Open 0xtristan opened 3 years ago

0xtristan commented 3 years ago

Was just wondering if the JS example for interfacing with a Serum market needs updating. I've tried running it and run into the same error that the Market.load() expects a programId, otherwise it tries to do equality operations on an undefined argument. I imagine perhaps the API has changed in the meantime? https://github.com/project-serum/serum-ts/blob/master/packages/serum/README.md

import { Account, Connection, PublicKey } from '@solana/web3.js';
import { Market } from '@project-serum/serum';

let connection = new Connection('https://testnet.solana.com');
let marketAddress = new PublicKey('...');
let market = await Market.load(connection, marketAddress);

Which throws the following error as described above:

    return this._bn.eq(publicKey._bn);
                                 ^

TypeError: Cannot read property '_bn' of undefined

Managed to get around it simply by passing in the programId of the Serum v3 program:

let programId = new PublicKey('9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin'); // Serum program v3
let market = await Market.load(connection, marketAddress, {}, programId);
0xtristan commented 3 years ago

Other newcomers also running into this issue. I can raise a PR to update it if you'd like? https://discord.com/channels/739225212658122886/752530209848295555/823214372452564993

npomfret commented 3 years ago

To get this example working, where do I find a programAddress and a marketAddress pls?

armaniferrante commented 3 years ago

To get this example working, where do I find a programAddress and a marketAddress pls?

vikas-speqto commented 2 years ago

Hello @armaniferrante @npomfret @0xtristan I had clone the code, executed npm run build & npm start command successfully. but i don't understand how i will execute place order & other function. when i execute usage code in separate node js program i am getting bad secret key error. kindly help me in this.

Thanks in Advance

danirayan commented 2 years ago

@armaniferrante is it possible to test serum examples on devnet?

Market addresses: https://github.com/project-serum/serum-ts/blob/master/packages/serum/src/markets.json (an incomplete list)

The above list seems to be for the mainnet? Would they also work for the devnet?

pra-dan commented 2 years ago

Guys this is a really poor documentation. You guys @armaniferrante need to seriously explain the params used in the example. I was using the same example

import { Account, Connection, PublicKey } from '@solana/web3.js';
import { Market } from '@project-serum/serum';

let connection = new Connection('https://testnet.solana.com');
// token address: for MSOL/USDC
let marketAddress = new PublicKey('6oGsL2puUgySccKzn9XA9afqF217LfxP5ocq4B3LWsjy');
// program address/PID (IDK) 9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin
let programAddress = new PublicKey("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin");
let market = await Market.load(connection, marketAddress, {}, programAddress);
let mainAcc = '6QneB2NPPhBwZWuKxBWnvUCwBY1vFgYrz746PwrHqKWk';

// Fetching orderbooks
let bids = await market.loadBids(connection);
let asks = await market.loadAsks(connection);
// L2 orderbook data
for (let [price, size] of bids.getL2(20)) {
  console.log(price, size);
}
// Full orderbook data
for (let order of asks) {
  console.log(
    order.orderId,
    order.price,
    order.size,
    order.side, // 'buy' or 'sell'
  );
}

Just to fetch the MSOL/USDC price and orderbook but instead get

/Users/pedro/Desktop/Home/gi/node_modules/@solana/web3.js/lib/index.cjs.js:5459
      throw new Error('failed to get info about account ' + publicKey.toBase58() + ': ' + e);
            ^

Error: failed to get info about account 6oGsL2puUgySccKzn9XA9afqF217LfxP5ocq4B3LWsjy: FetchError: request to https://testnet.solana.com/ failed, reason: connect ETIMEDOUT 139.178.68.207:443
    at Connection.getAccountInfo (/Users/pedro/Desktop/Home/gi/node_modules/@solana/web3.js/lib/index.cjs.js:5459:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Function.load (/Users/pedro/Desktop/Home/gi/node_modules/@project-serum/serum/lib/market.js:133:45)
    at async file:///Users/pedro/Desktop/Home/gi/serum-interactions/src/getOrderbook.js:9:14

Any help guys @0xtristan @npomfret ?

npomfret commented 2 years ago

I'm pretty sure there is an easy way to lookup these ids. I will have a play and report back if I find it

pra-dan commented 2 years ago

@npomfret any hints ?