gagliardetto / solana-go

Go SDK library and RPC client for the Solana Blockchain
Apache License 2.0
930 stars 264 forks source link

CancelOrder program #61

Open rafaelvanoni opened 2 years ago

rafaelvanoni commented 2 years ago

Hi folks, I'm trying to write a client that issues CancelOrder transactions and it seems that program hasn't been implemented yet. Is that correct? If so, I'd be happy to propose a PR with it.

To clarify, the problem I'm trying to solve is that I can't create a solana.NewTransaction() with a InstructionCancelOrder{} because the latter doesn't fully implement solana.Instruction. It seems that "fully" implementing CancelOrder similarly to what's done in programs/system/CreateAccount.go for example, would solve that.

aspin commented 2 years ago

Just as a follow up for anyone else experiencing this issue: we figured out that the usage is like this:

    // create the CancelOrder instruction
    cancelInstruction := serum.InstructionCancelOrderByClientIdV2{
        ClientID: clientID,
        Accounts: &serum.CancelOrderByClientIdV2Accounts{
            Market:     solana.Meta(marketPk).WRITE(),
            Bids:       solana.Meta(bidsPk).WRITE(),
            Asks:       solana.Meta(asksPk).WRITE(),
            OpenOrders: solana.Meta(openOrdersPk).WRITE(),
            Owner:      solana.Meta(ownerPk).SIGNER(),
            EventQueue: solana.Meta(eventQueuePk).WRITE(),
        },
    }

    serumInstruction := serum.Instruction{
        BaseVariant: bin.BaseVariant{
            TypeID: serum.InstructionDefVariant.TypeID("cancel_order_by_client_id_v2"),
            Impl:   cancelInstruction,
        },
        Version: serumInstructionVersion,
    }

    buf := new(bytes.Buffer)
    err := bin.NewBinEncoder(buf).Encode(serumInstruction)
    if err != nil {
        return nil, fmt.Errorf("failed to encode CancelOrder instruction (%w)", err)
    }

    serumV3Dex, _ := solana.PublicKeyFromBase58("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin")
    instruction := &solana.GenericInstruction{
        AccountValues: []*solana.AccountMeta{
            cancelInstruction.Accounts.Market,
            cancelInstruction.Accounts.Bids,
            cancelInstruction.Accounts.Asks,
            cancelInstruction.Accounts.OpenOrders,
            cancelInstruction.Accounts.Owner,
            cancelInstruction.Accounts.EventQueue,
        },
        ProgID:    serumV3Dex,
        DataBytes: buf.Bytes(),
    }