@stacks/connect
🐎 Getting Started
1. Add the dependency
Add the @stacks/connect
dependency to your project using your favorite package manager.
Some options below
npm install @stacks/connect
pnpm install @stacks/connect
yarn add @stacks/connect
2. Creating AppConfig
and UserSession
Add a reusable UserSession
instance to your project.
This will allow your website to store authentication state in localStorage.
import { AppConfig, UserSession } from '@stacks/connect';
const appConfig = new AppConfig(['store_write', 'publish_data']);
export const userSession = new UserSession({ appConfig });
3. Interacting with the wallet
"Connect" aka authentication (showConnect
)
Connecting the wallet is a very simple form of authentication.
This process gives the web-app information about a wallet account (selected by the user).
The snippet below lets your web-app trigger the wallet to open and authenticate an account.
If no wallet is installed, an informational modal will be displayed in the web-app.
import { showConnect } from '@stacks/connect';
import { userSession } from './userSession';
const myAppName = 'My Stacks Web-App';
const myAppIcon = window.location.origin + '/my_logo.png';
showConnect({
userSession,
appDetails: {
name: myAppName,
icon: myAppIcon,
},
onFinish: () => {
window.location.reload();
},
onCancel: () => {
console.log('oops');
},
});
Sending STX (openSTXTransfer
)
Sending STX tokens is also possible through web-apps interacting with a user's wallet.
The snippet below will open the wallet to confirm and broadcast a smart-contract transaction.
Here, we are sending 10000
micro-STX tokens to a recipient address.
import { openSTXTransfer } from '@stacks/connect';
import { StacksTestnet } from '@stacks/network';
import { AnchorMode, PostConditionMode } from '@stacks/transactions';
import { userSession } from './userSession';
openSTXTransfer({
network: new StacksTestnet(),
anchorMode: AnchorMode.Any,
recipient: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK',
amount: 10000,
memo: 'Nr. 1337',
postConditionMode: PostConditionMode.Deny,
postConditions: [],
onFinish: response => {
console.log(response.txid);
},
onCancel: () => {
console.log('User canceled');
},
});
Sending transactions with post-conditions (openSTXTransfer
)
Consider the example above.
Using post-conditions, a feature of the Stacks blockchain, we can ensure something happened after a transaction.
Here, we could ensure that the recipient indeed receives a certain amount of STX.
import {
PostConditionMode,
FungibleConditionCode,
makeStandardSTXPostCondition,
} from '@stacks/transactions';
const myPostCondition = makeStandardSTXPostCondition(
'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK',
FungibleConditionCode.GreaterEqual,
5000
);
postConditionMode: PostConditionMode.Deny,
postConditions: [ myPostCondition ],
For more examples on constructing different kinds of post-conditions read the Post-Conditions Guide of Stacks.js.
Post-Condition Modes
If post-conditions postConditions: [ ... ]
are specified, they will ALWAYS be checked by blockchain nodes.
If ANY conditions fails, the transaction will fail.
The Post-Condition Mode only relates to transfers of assets, which were not specified in the postConditions
.
PostConditionMode.Deny
will fail the transaction if any unspecified assets are transferredPostConditionMode.Allow
will allow unspecified assets to be transferred- In both cases, all
postConditions
will be checked
Calling Smart-Contracts (openContractCall
)
Calling smart-contracts lets users interact with the blockchain through transactions.
The snippet below will open the wallet to confirm and broadcast a smart-contract transaction.
Here, we are passing our pick Alice
to an imaginary deployed voting smart-contract.
import { openContractCall } from '@stacks/connect';
import { StacksTestnet } from '@stacks/network';
import { AnchorMode, PostConditionMode, stringUtf8CV } from '@stacks/transactions';
import { userSession } from './userSession';
const pick = stringUtf8CV('Alice');
openContractCall({
network: new StacksTestnet(),
anchorMode: AnchorMode.Any,
contractAddress: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK',
contractName: 'example-contract',
functionName: 'vote',
functionArgs: [pick],
postConditionMode: PostConditionMode.Deny,
postConditions: [],
onFinish: response => {
},
onCancel: () => {
},
});
🤔 Pitfalls
- Connect can currently not set manual nonces, since this is not supported by wallets.
- For some projects it might be necessary to also install the
regenerator-runtime
package. npm install --save-dev regenerator-runtime
. This is a build issue of @stacks/connect
and we are working on a fix.
📚 Option Glossary
A glossary of the most common options of openSTXTransfer
and openContractCall
openSTXTransfer
Required
| Description | Type | Example |
---|
recipient | The recipient (STX principal) address | string | 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK' |
amount | The amount (in micro-STX) to transfer | Integer (e.g. number , bigint ) | 10000 |
openContractCall
Required
| Description | Type | Example |
---|
contractAddress | The (STX contract) address of the smart contract | string | 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK' |
contractName | The contract name | string | 'example-contract' |
functionName | The contract function name | string | 'vote' |
functionArgs | The contract function arguments | Array of Clarity Values | [] , [uintCV(100)] |
Optional
| Default | Description | Type | Example |
---|
network | Mainnet | The network to broadcast the transaction to | StacksNetwork | new StacksMainnet() |
anchorMode | Any | The type of block the transaction should be mined in | AnchorMode Enum | AnchorMode.OnChainOnly |
memo | Empty '' | The memo field (used for additional data) | string | 'a memo' |
fee | Handled by Wallet | The transaction fee (the wallet will estimate fees as well) | Integer (e.g. number , bigint ) | 1000 |
onFinish | No-op | The callback function to run after broadcasting the transaction | Function (receiving response ) | |
onCancel | No-op | The callback function to run after the user cancels/closes the wallet | Function | |