How do crypto transactions work?
In DeFi, a crypto transaction involves the transfer of assets between parties, typically recorded on a blockchain. However, off-chain transactions don’t necessarily always take place on a blockchain.
Key takeaways:
A crypto transaction is a transfer of cryptocurrency from one party to another. There are on-chain and off-chain transactions. The former occur on a blockchain and the latter are executed outside a blockchain network. On a blockchain, transfers occur between addresses.
Addresses, public and private keys
Addresses indicate funds’ sender and receiver. A crypto address is a unique string of characters that identifies a specific crypto account participating in a transaction. An address can be automatically generated by a wallet.
Each account has a public key and a private key – cryptographic codes. An account’s public address is derived from the public key, and the public key is derived from the account’s private key.
Both private and public keys are used for encrypting - signing - transaction data before it starts the verification process on a blockchain. A transaction has to pass several stages before it is completed.
The main stages of on-chain transactions:
- After a user fills in transaction details in a wallet app and clicks the send button, the transaction is initiated. The next step is a digital signature, which is created with a private key. Signing a transaction with a private key proves its legitimacy.
- When a transaction is signed with a private key, it is submitted to the blockchain. Most wallets perform signing and broadcasting simultaneously. The submitted transaction goes to the memory pool - commonly referred to as mempool.
- In the mempool, transactions wait to be taken for further validation performed by validators or miners. These are nodes - active distributed computers shaping the network and following a specific set of rules: a consensus algorithm. Miners and validators are responsible for collecting transactions into blocks in order to verify and record them. Validators are responsible for this service on blockchains using the Proof of stake consensus algorithm (Ethereum, for example). And how does Bitcoin work? Miners validate transactions on Bitcoin and other blockchains using the Proof of Work consensus algorithm.
- Validators and miners include transactions in a block. Once the block containing transactions has been finalized, the transaction is executed and recorded on a blockchain. After this, the transaction is “immutable”: it can’t be reverted.
Transaction details
Along with the sender’s and receiver’s addresses, the transaction details may contain such information as gas price and transaction cost or network fee. The network fee is the amount paid to the block producer regardless of whether the transaction is successful or not. Gas price applies to the Ethereum network fee along with its L2s (blockchains built on top of Ethereum) and defines the cost of gas spent on a transaction. It can be adjusted by users in order to speed up a transaction. Gas is paid with native tokens of the network hosting the transaction.
Transaction statuses
- ‘Pending’ refers to the transactions broadcast to the mempool and waiting to be chosen for validation.
- ‘Canceled’ - also referred to as dropped and replaced transactions - is the status of a transaction replaced by another one.
- ‘Failed’ is usually the result of some error or insufficient gas to proceed.
- ‘Executed’ applies to a completed transaction.
- Transactions can include corresponding notes and messages linked by encoding into data fields. Ethereum transactions can interact with smart contracts (they also have addresses).
- Block explorers, like Etherscan, track details of all transactions and show their status and details in real-time.
Off-chain transactions
Off-chain transactions take place outside a blockchain. Cryptographic assets may remain in custody by a trusted third party that can guarantee and secure a transaction outside of the blockchain. Such services include, but are not limited to, centralized exchanges and custodial wallets.
This type of transaction can be used by companies when transferring assets by making entries only on each other's internal ledgers.
Centralized exchanges also use off-chain order books for trading.
Since transactions take place off-chain, there are no changes to the blockchain state. This eliminates work of validators and miners and a need to pay fees. Off-chain transactions can be faster than those executed on-chain due to the removal of the confirmation procedure. However, on-chain transactions are recorded, irreversible and thoroughly validated – their security is guaranteed by the blockchain technology, not by a middleman.
Using centralized services excludes transaction fees, but a user instead pays withdrawal fees, trading fees or any other fees the intermediary earns from. In addition, when trusting a middleman, you must be aware that all your funds are at their disposal, unlike, for example, when using non-custodial wallets, where only the user has access to the private key and funds.
Layer 2 solutions, built on top of blockchain networks, also conduct transactions outside the main network while still relying on its security.