Skip to main content

The opcode

The opcode tells a vault which operation to run: a buffer of up to 16 bytes whose first byte is the operation.

Reading it​

Vaults read byte 0 like this (from the sublink template):

(define-private (get-byte (opcode (optional (buff 16))) (position uint))
(default-to 0x00 (element-at? (default-to 0x00 opcode) position)))

none or an empty buffer reads as 0x00. A pool treats that as a swap from A to B; a sublink rejects it.

dexterity-sdk sends all 16 bytes, zero-padded:

export const opcodeCV = (op: number): ClarityValue => {
const b = new Uint8Array(16).fill(0);
b[0] = op;
return someCV(bufferCV(b));
};

One byte works too: Invest's liquidity buttons send 02 and 03.

Operations​

Byte 0OPCODES nameamountdxdydk
0x00SWAP_A_TO_BToken A inInput¹Token B out0
0x01SWAP_B_TO_AToken B inInput¹Token A out0
0x02ADD_LIQUIDITYLP tokens to mintToken A paid inToken B paid inLP minted
0x03REMOVE_LIQUIDITYLP tokens to burnToken A paid outToken B paid outLP burned
0x04LOOKUP_RESERVESIgnored, send 0Reserve AReserve BLP supply
0x05OP_DEPOSITTokens into the subnetamountamount0
0x06OP_WITHDRAWTokens out of the subnetamountamount0
0x07Not in the SDKIgnoredBlocks since the last harvest²Balance over that time²Energy harvested²

¹ Charisma pools report the input after their LP fee. Wrappers report the input consumed. ² From execute. quote returns 0, 0 and the blocks since the last harvest.

Contracts prefix every constant with OP_ (OP_SWAP_A_TO_B, OP_HARVEST_ENERGY). OPCODES in dexterity-sdk drops the prefix for 0x00 to 0x04 and has no 0x07.

Who implements what​

VaultexecutequoteAny other opcode
Charisma pool (Launchpad template)00 to 0300 to 04err u400
Early Charisma pool, e.g. charismatic-flow00 to 0300 to 03err u400
Sublink05, 0605, 06err u4002
External DEX wrapper00, 0100, 01, 04err u400
energize-v10707err u4002
hooter-farm-rewards00, 0100, 01, 04err u400
CallerSends
dexterity-sdk Router, building a route00, 01, 05 into a subnet, 06 out of one
Invest, adding and removing liquidity02, 03
Invest, refreshing reserves04

Room to grow​

  • Byte 0 has 256 values. 0x00 to 0x07 are taken.
  • No vault reads bytes 1 to 15 yet. They are room for an operation's parameters.
  • A new operation needs a vault that understands it and a caller that sends it. The trait, the routers and existing vaults stay as they are.

Not the Blaze opcode​

Blaze intents also have an opcode field of the same type, with a different job.

Dexterity opcodeBlaze opcode
Lives inEach execute or quote call, one per hopA signed intent
Set byWhoever builds the transactionThe signer, inside the signature
SaysWhich operation a vault runsExtra terms the signer commits to
Used todayEvery vault callNever: always none

Both appear in one Blaze swap:

See Signing: the opcode field.