project-serum / serum-ts

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

CancelOrderByClientId not working? #18

Open leofisG opened 3 years ago

leofisG commented 3 years ago
    const connection = new Connection('...');
    const marketAddress = new PublicKey('...');
    const programAccount = new PublicKey("...");
    let market = await Market.load(connection, marketAddress,  { skipPreflight: false, confirmations: 0 }, programAccount);

    const owner = new Account(...)
    const payer = new PublicKey(...)

    const openOrders = await market.findOpenOrdersAccountsForOwner(connection, payer)
    for (const openOrder of openOrders) {
        console.log(openOrder)
        await market.cancelOrderByClientId(connection, owner, openOrder, 0)
    }

And it's telling me:

(node:10605) UnhandledPromiseRejectionWarning: TypeError: src.toArrayLike is not a function
    at BNLayout.encode (/home/wangge/serum_exp/node_modules/@project-serum/serum/lib/layout.js:44:33)
    at Structure.encode (/home/wangge/serum_exp/node_modules/buffer-layout/lib/Layout.js:1272:26)
    at VariantLayout.encode (/home/wangge/serum_exp/node_modules/buffer-layout/lib/Layout.js:1914:19)
    at Union.encode (/home/wangge/serum_exp/node_modules/buffer-layout/lib/Layout.js:1730:16)
    at VersionedLayout.encode (/home/wangge/serum_exp/node_modules/@project-serum/serum/lib/layout.js:94:31)
    at encodeInstruction (/home/wangge/serum_exp/node_modules/@project-serum/serum/lib/instructions.js:41:50)
    at Function.cancelOrderByClientId (/home/wangge/serum_exp/node_modules/@project-serum/serum/lib/instructions.js:156:19)
    at Market.makeCancelOrderByClientIdTransaction (/home/wangge/serum_exp/node_modules/@project-serum/serum/lib/market.js:412:56)
    at Market.cancelOrderByClientId (/home/wangge/serum_exp/node_modules/@project-serum/serum/lib/market.js:407:40)
    at main (file:///home/wangge/serum_exp/index.js:24:22)
(node:10605) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:10605) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

If it's due to the error on my side, it will be really appreciated if someone can provide a working example. Many thanks~

leofisG commented 3 years ago

It turns out that it's an error on my end. So basically the above error is caused by the clientId argument should be a BN instead of a number.

Furthermore, client Id of 0 gets ignored. And it will reports InstructionError(0, Custom(16778880)). So I think the order must have a non-zero client id for it to be able to get cancelled.

If the user supplies an id that doesn't exist in the open order, say the order book only has an order with client id 22, but the client tries to cancel 23 it will throw an InstructionError(0, Custom(41)) corresponding to ClientIdNotFound(here).

Also, I think this error only get thrown if there are no (whether cancelled or not) orders from the given OpenOrdersAccount. So say, I used to have an order with client Id, say, 22 but it got cancelled. If I want to cancel it again, it will not throw Custom(41) error.

leofisG commented 3 years ago

Actually not sure if we want to support 0 as a valid client id, maybe easier to understand if 0 can also work instead of producing an error.... Or at least output an error on the client side say something like it should be non-zero.