Skip to content

Commit

Permalink
Start adding stacks request types
Browse files Browse the repository at this point in the history
  • Loading branch information
aryzing committed Feb 22, 2024
1 parent 591883e commit 6afcacd
Showing 1 changed file with 115 additions and 0 deletions.
115 changes: 115 additions & 0 deletions src/inpage/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
interface Requests {
stx_contractCall: {
args: {
/**
* The stacks address of sender.
*/
pubkey: string;

/**
* The address of the contract.
*/
contractAddress: string;

/**
* The name of the contract.
*/
contractName: string;

/**
* The name of the function to call.
*/
functionName: string;

/**
* An array of hex-encoded strings representing the function arguments.
*
* To convert Clarity values to their hex representation, the `cvToString`
* helper from the `@stacks/transactions` package may be helpful.
*
* ```js
* import { cvToString } from '@stacks/transactions';
*
* const functionArgs = [someClarityValue1, someClarityValue2];
* const hexArgs = functionArgs.map(cvToString);
* ```
*/
functionArgs: Array<string>;

/**
* A hex-encoded string representing the post conditions.
*
* A post condition may be converted to it's hex representation using the `serializePostCondition` helper from the `@stacks/transactions` package,
*
* ```js
* import { serializePostCondition } from '@stacks/transactions';
*
* const postCondition = somePostCondition;
* const hexPostCondition = serializePostCondition(postCondition).toString('hex');
*/
postConditions: Array<string>; // Array<PostCondition>

/**
* The mode of the post conditions.
*/
postConditionMode?: number; // PostConditionMode

/**
* The anchor mode.
*/
anchorMode?: 'TODO'; // AnchorMode
/** A stringified BigInt */
nonce?: string; // BigInt
version?: string;
sponsored?: boolean;
};
return: {};
};
stx_transfer: {
args: {
pubkey: string;
recipient: string;
amount: string; // BigInt
memo?: string;
postConditions?: Array<string>; // Array<PostCond>
postConditionMode?: string; // PostConditionMode
version?: string;
};
return: {};
};
stx_signMessage: {
args: {
pubkey: string;
message: string;
version?: string;
};
return: {};
};
stx_contractDeploy: {
args: {
pubkey: string;
contractName: string;
codeBody: string;
postConditions?: Array<string>; // Array<PostCond>
postConditionMode?: string; // PostConditionMode
version?: string;
};
return: {};
};
}

type Return<Method> = Method extends keyof Requests ? Requests[Method]['return'] : unknown;
type Args<Method> = Method extends keyof Requests ? Requests[Method]['args'] : unknown;

function request<Method extends keyof Requests>(
requestMethod: Method,
args: Args<Method>,
): Return<Method> {
switch (requestMethod) {
// case 'stx_foo1': {
// return { resultA: 'value' } as Return<Method>;
// }
default:
return {} as Return<Method>;
}
}

0 comments on commit 6afcacd

Please sign in to comment.