JSON API

Contract Function Call

Method:

stacks_contractCall

Request a Stacks contract function call.

Parameters:

pubkey required - string

The stacks address of sender.

contractAddress required - string

The STX address of the contract.

contractName required - string

The name of the contract.

functionName required - string

The name of the contract function to be called.

functionArgs required - Array<ClarityValue>

An array of function arguments of ClarityValue type.

postConditions optional - Array<PostCondition>

An array of post conditions to attach to the transaction.

postConditionMode optional - PostConditionMode

The post condition mode to use. Defaults to PostConditionMode.Allow.

anchorMode optional - AnchorMode

The anchor mode to use. Defaults to AnchorMode.Any.

nonce optional - BigInt

Custom nonce to set for the transaction. Default value is the next nonce for the address.

version optional - string

Version of parameter format.

sponsored optional - boolean to create a sponsored transaction

Example:

const result = await client.request({
  chainId: "stacks:1", 
  topic: session.topic, // Get this from the session approval
  request: {
    method: "stacks_contractCall",
    params: {
      pubkey: address, 
      postConditions,
      contractAddress: "SP1H1733V5MZ3SZ9XRW9FKYGEZT0JDGEB8Y634C7R",
      contractName: "my_contract_name",
      functionName: "transfer",
      functionArgs: [
        uintCV("123"),
        standardPrincipalCV("SP1BEEN4WP9YT42PR70FYSG6C3WFG54QJDEN0KWR"),
        standardPrincipalCV("SP3F7GQ48JY59521DZEE6KABHBF4Q33PEYJ823ZXQ"),
        noneCV(),
      ],
      postConditionMode: PostConditionMode.Deny,
      version: "1",
      sponsored: true
    },
  },
});

const txId = result.txId;

STX Token Transfer

Method:

stacks_stxTransfer

Request a transfer of STX tokens.

Parameters:

pubkey required - string

The stacks address of sender.

recipient required - string

The STX address of the recipient.

amount required - BigInt

Amount of STX tokens to transfer in microstacks.

memo optional - string

Memo string to be included with the transfer transaction.

postConditions optional - Array<PostCondition>

An array of post conditions to attach to the transaction.

postConditionMode optional - PostConditionMode

The post condition mode to use. Defaults to PostConditionMode.Allow.

version optional - string

Version of parameter format.

Example:

const result = await client.request({
  chainId: chain,
  topic: session.topic,
  request: {
    method: "stacks_stxTransfer",
    params: {
      pubkey: address, 
      recipient: "SP3F7GQ48JY59521DZEE6KABHBF4Q33PEYJ823ZXQ",
      amount: BigInt(1000),
    },
  },
});

const txId = result.txId;

Message Signing

Method:

stacks_signMessage

Request signing of an arbitrary message.

Parameters:

pubkey required - string

The stacks address of sender.

message required - string

Message payload to be signed.

version optional - string

Version of parameter format.

Example:

const message = "loremipsum";
const result = await client.request({
  chainId: chain,
  topic: session.topic,
  request: {
    method: "stacks_signMessage",
    params: {
      pubkey: address,
      message: message,
    },
  },
});

const publicKey = result.publicKey;
const signature = result.signature;
const valid = verifyMessageSignatureRsv({
  message,
  publicKey,
  signature,
});

Structured Message Signing

Method:

stacks_signMessage Request signing of structured ClarityValue message

Parameters:

pubkey required - string

The stacks address of sender.

message required - Buffer

Message to be signed. domain required - string

domain of be signed

version optional - string

Version of parameter format.

Example:

const handleStructuredMessage = async () => {
    const address = session.namespaces.stacks.accounts[0].split(":")[2];
    const domain = "0c0000000308636861696e2d69640100000000000000000000000000000001046e616d650d00000011414c4558204232302050726f746f636f6c0776657273696f6e0d00000005302e302e31";
    try {
     const structuredMessage = serializeCV(structuredData);
      const result = await client.request({
        chainId: chain,
        topic: session.topic,
        request: {
          method: "stacks_signMessage",
          params: {
            pubkey: address, //XXX: This one is required
            message: structuredMessage,
            domain
          },
        },
      });

      setResult({
        method: "stacks_signMessage",
        address,
        result,
      });
    } catch (error) {
      throw new Error(error);
    }
  };

Contract Deploy

Method:

stacks_contractDeploy

Request a Clarity contract deployment

Parameters:

pubkey required - string

The stacks address of sender.

contractName required - string

The name the contract is to be deployed as.

codeBody required - string

Body of the contract source code.

postConditions optional - Array<PostCondition>

An array of post conditions to attach to the transaction.

postConditionMode optional - PostConditionMode

The post condition mode to use. Defaults to PostConditionMode.Allow.

version optional - string

Version of parameter format.

Example:

const codeBody = `
  ;; hello-world
  (define-read-only (echo-number (val int)) (ok val))
  (define-public (say-hi) (ok "hello world"))
`

const result = await client.request({
  chainId: chain,
  topic: session.topic,
  request: {
    method: "stacks_contractDeploy",
    params: {
      pubkey: address, 
      contractName: "my_contract_name_1", 
      codeBody: codeBody,
      postConditionMode: PostConditionMode.Allow,
    },
  },
});

const txId = result.txId;

Last updated