stx-transfer?
Transferring STX between principals in Clarity smart contracts.
Function Signature
(stx-transfer? amount sender recipient)
- Input:
uint, principal, principal
- Output:
(response bool uint)
Why it matters
The stx-transfer?
function is crucial for:
- 1Transferring STX between principals.
- 2Implementing logic that requires moving STX from one account to another.
- 3Ensuring data integrity by validating the transfer operation.
- 4Simplifying the process of handling STX transfers in smart contracts.
When to use it
Use stx-transfer?
when you need to:
- Transfer STX between principals.
- Implement logic that requires moving STX from one account to another.
- Validate the transfer operation to ensure data integrity.
- Handle STX transfers in your smart contract.
Best Practices
- Ensure the
amount
is positive and thesender
has sufficient balance. - Use meaningful variable names for better readability.
- Combine with other STX functions for comprehensive account management.
- Handle the possible error cases to ensure robust contract behavior.
Practical Example: Transferring STX
Let's implement a function that transfers STX from the tx-sender
to a recipient:
(define-public (transfer-stx (amount uint) (recipient principal))(stx-transfer? amount tx-sender recipient));; Usage(transfer-stx u60 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR);; Returns (ok true) if successful(transfer-stx u50 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR);; Returns (err u1) if the sender does not have enough balance
This example demonstrates:
- 1Using
stx-transfer?
to transfer STX from thetx-sender
to a recipient. - 2Implementing a public function to handle the STX transfer.
- 3Handling both successful and error cases.
Common Pitfalls
- 1Using
stx-transfer?
with a non-positive amount, causing the operation to fail. - 2Assuming the transfer operation will always succeed, leading to unhandled error cases.
- 3Not handling all possible conditions, resulting in incomplete account management.
- 4Overlooking the need for proper error handling and validation.
Related Functions
stx-get-balance
: Queries the STX balance of a principal.stx-transfer-memo?
: Transfers STX from one principal to another with a memo.stx-burn?
: Burns STX from a principal's account.
Conclusion
The stx-transfer?
function is a fundamental tool for transferring STX between principals in Clarity smart contracts. It allows developers to implement logic that requires moving STX from one account to another, ensuring data integrity and simplifying STX transfer operations. When used effectively, stx-transfer?
enhances the reliability and maintainability of your smart contract code by providing a clear and concise way to handle STX transfers.