Staking on Hedera for Developers – Back to the Basics | Hedera Hedera Network Services Token Service Mint and configure tokens and accounts. Consensus Service Verifiable timestamps and ordering of events. Smart Contracts Run Solidity smart contracts. HBAR The Hedera network's native cryptocurrency. Insights How It Works Learn about Hedera from end to end. Explorers View live and historical data on Hedera. Dashboards Analyze network activity and metrics. Network Nodes Understand networks and node types. Devs Start Building Get Started Learn core concepts and build the future. Documentation Review the API and build using your favorite language. Developer Resources Integrations Plugins and microservices for Hedera. Fee Estimator Understand and estimate transaction costs. Open Source Hedera is committed to open, transparent code. Learning Center Learn about web3 and blockchain technologies. Grants Grants & accelerators for your project. Bounties Find bugs. Submit a report. Earn rewards. Ecosystem ECOSYSTEM Hedera Ecosystem Applications, developer tools, network explorers, and more. NFT Ecosystem Metrics Analyze on-chain and market NFT ecosystem metrics. CATEGORIES Web3 Applications Connect into the innovative startups decentralizing the web on Hedera. Enterprise Applications Learn about the Fortune 500 companies decentralizing the web on Hedera. Wallets & Custodians Create a Hedera account to manage HBAR, fungible tokens, and NFTs. Network Explorers Hedera mainnet and testnet graphical network explorers. Developer Tooling Third-party APIs, integrations, and plugins to build apps on Hedera. Grants & Accelerators Boost your project with support from the Hedera ecosystem. Partner Program Explore our partners to bring your vision into reality. Hedera Council Over 30 highly diversified organizations govern Hedera. Use Cases Hedera Solutions Asset Tokenization Studio Open source toolkit for tokenizing assets securely. Stablecoin Studio All-in-one toolkit for stablecoin solutions. Hedera Guardian Auditable carbon markets and traceability. Functional Use Cases Data Integrity & AI Reliable, secure, and ethically governed insights. Sustainability Enabling fair carbon markets with trust. Real-World Asset Tokenization Seamless tokenization of real-world assets and digital at scale. Consumer Engagement & Loyalty Mint, distribute, and redeem loyalty rewards. Decentralized Identity Maintain the lifecycle of credentials. Decentralized Logs Scalable, real-time timestamped events. DeFi Dapps built for the next-generation of finance. NFTs Low, fixed fees. Immutable royalties. Payments Scalable, real-time, and affordable crypto-payments. HBAR Overview Learn about Hedera's token, HBAR. Treasury Management Hedera’s report of the HBAR supply. Governance Decentralized Governance Hedera Council See the world's leading organizations that own Hedera. About Meet Hedera's Board of Directors and team. Journey Watch Hedera's journey to build an empowered digital future for all. Transparent Governance Public Policy Hedera's mission is to inform policy and regulation that impact the industry. Meeting Minutes Immutably recorded on Hedera. Roadmap Follow Hedera's roadmap in its journey to build the future. Resources Company What's New Partners Papers Careers Media Blog Technical Press Podcast Community Events Meetups Store Brand Navigation QUICKSTART Staking on Hedera for Developers – Back to the Basics technical Jul 29, 2022 by Ed Marquez Head of Developer Relations In this tutorial, you will learn how to configure staking options for Hedera accounts and contracts programmatically. Perhaps you need to integrate these staking capabilities with wallets, decentralized applications, marketplaces, or other cool things you may be building on Hedera. The article Introducing Native Staking Phase 1 on the Hedera Network provides an overview of how staking is being rolled out on the Hedera network, so be sure to give that and HIP-406 a good read for more general information about staking. Try It Yourself Get a Hedera testnet account This Codesandbox is already setup for you to try this example Fork the sandbox Remember to provide your testnet account credentials in the .env file And open a new terminal to execute accountStaking.js and contractStaking.js Get the example code from GitHub Key Takeaways If you’re a developer, here are a few key points to keep in mind about staking for accounts and contracts using the Hedera SDKs (Java, JavaScript, Go): Staking options for accounts and contracts can be changed during creation or at any time by doing an account/contract update The SDK classes that provide access to staking functionality are: AccountCreateTransaction(), AccountUpdateTransaction(), ContractCreateTransaction(), and ContractUpdateTransaction() Under these classes, the methods that enable changing staking options for accounts or contracts are: setStakedAccountId(), setStakedNodeId(), and setDeclineStakingReward() setStakedAccountId() takes an account or contract ID (like 0.0.1234) as input setStakedNodeId() takes a node ID (like 25) as input In a single transaction, specify either setStakedAccountId() or setStakedNodeId() – NEVER specify both Hedera accounts and contracts can stake their balance to nodes, accounts, or contracts. Note that an account earns rewards only after staking to a node with declineReward=false for at least one full UTC calendar day. See HIP-406 for details. 1. Staking for Hedera Accounts This JavaScript example performs two steps:      1.1 Create a new Hedera account and specify its staking options      1.2 Update the staking options for the account created in step 1.1 In step 1.1, Alice’s new account stakes its balance to account 0.0.2520793 and declines staking rewards. Then in step 1.2, the staking options for Alice’s new account are updated to stake to node 3 and receive staking rewards. Here is the console output when you run the entire example: Here is the main function that implements the steps described: async function main() { // Create a new Hedera account const aliceKey = PrivateKey.generateED25519(); const aliceBalance = 100; const [accStatus, aliceId] = await accountCreatorFcn(aliceKey, aliceBalance, operatorId, true); console.log( `\n- 1.1) ${accStatus}: Created account ${aliceId} for Alice with initial balance of ${aliceBalance} hbar` ); await getStakingInfoFcn(aliceId); // Update an existing account const updateStatus = await accountUpdaterFcn(aliceId, aliceKey, 3, false); console.log(`\n- 1.2) ${updateStatus}: Updated account ${aliceId}:`); await getStakingInfoFcn(aliceId); console.log(`\n- THE END ============================================`); } Copy 1.1: Staking for Newly Created Accounts Let’s take a closer look at the function accountCreatorFcn (see below). This function creates a new Hedera account and sets its staking options. The function takes as inputs the private key and initial balance for the new account, the account ID to stake to, and a Boolean for declining or receiving the staking rewards. Create an instance of AccountCreateTransaction() with the provided inputs and execute it using the client to create the new account. Be sure to set either setStakedAccountId() or setStakedNodeId(). Do NOT set both – the same applies for the update transaction. Note that additional method calls are commented out in case you need them in the future. The last steps are to get a transaction receipt with the Hedera client, and return the transaction status and account ID. async function accountCreatorFcn(pvKey, iBal, stakeAccount, noRewardFlag) { const accountCreateTx = await new AccountCreateTransaction() .setKey(pvKey.publicKey) .setInitialBalance(new Hbar(iBal)) .setStakedAccountId(stakeAccount) // SET THIS ONE... // .setStakedNodeId(stakeNode) // OR THIS ONE - DON'T SET BOTH .setDeclineStakingReward(noRewardFlag) // .setReceiverSignatureRequired(booleanValue) // .setMaxAutomaticTokenAssociations(amount) // .setAccountMemo(memo) // .setAutoRenewPeriod(autoRenewPeriod) .execute(client); const accountCreateRx = await accountCreateTx.getReceipt(client); return [accountCreateRx.status, accountCreateRx.accountId]; } Copy 1.2: Staking for Existing Accounts The function accountUpdaterFcn user AccountUpdateTransaction() to modify properties of the Hedera account created in the previous step. The inputs for the function are the account ID to update, the private key of that account (to authorize the update), the node ID to stake to, and a Boolean for declining or receiving the staking rewards. Note that this transaction must be signed (authorized) using the private key of the account being updated. Lastly, the client submits the transaction and gets a receipt. The function returns the transaction status for the update. Just like in the previous step, additional method calls for this class are shown and commented out in case you need them. Also, keep in mind that you can edit these functions to receive any inputs and create or update accounts according to your needs in the future. async function accountUpdaterFcn(accountId, pvKey, stakeNode, noRewardFlag) { const accountUpdateTx = new AccountUpdateTransaction() .setAccountId(accountId) // .setStakedAccountId(stakeAccount) // SET THIS ONE... .setStakedNodeId(stakeNode) // OR THIS ONE - DON'T SET BOTH .setDeclineStakingReward(noRewardFlag) // .setKey(key) // .setReceiverSignatureRequired(booleanValue) // .setMaxAutomaticTokenAssociations(amount) // .setAccountMemo(memo) // .setAutoRenewPeriod(autoRenewPeriod) // .setExpirationTime(expirationTime) .freezeWith(client); const accountUpdateSign = await accountUpdateTx.sign(pvKey); const accountUpdateSubmit = await accountUpdateSign.execute(client); const accountUpdateRx = await accountUpdateSubmit.getReceipt(client); return accountUpdateRx.status; } Copy Helper Functions The function getStakingInfoFcn uses AccountInfoQuery() (or ContractInfoQuery() ) to get and show the staking information for the account (or contract) of interest. async function getStakingInfoFcn(id) { const accountInfo = await new AccountInfoQuery().setAccountId(id).execute(client); console.log(`- Staking info:`); console.log(`-- stakedAccountId: ${accountInfo.stakingInfo.stakedAccountId}`); console.log(`-- stakedNodeId: ${accountInfo.stakingInfo.stakedNodeId}`); console.log(`-- declineStakingReward: ${accountInfo.stakingInfo.declineStakingReward}`); } Copy 2. Staking for Hedera Smart Contracts This JavaScript example performs two steps:      2.1 Deploy a new Hedera smart contract and specify its staking options      2.2 Update the staking options for the contract deployed in step 2.1 As shown in the console output, the contract stakes its balance to account 0.0.2520793. Even though the contract opted into receiving staking rewards, those rewards may be received by the account that the contract staked to. For details on rewards distribution scenarios, refer to HIP-406. Then in step 2.2, the staking options for the new contract are updated to stake to node 4, but the setDeclineStakingReward flag is set to true so no rewards will be received by the contract. Here is the main function that implements the steps described: async function main() { // Import the compiled contract bytecode const contractBytecode = fs.readFileSync("simpleContract.bin"); // Deploy a contract on Hedera const [contractId, contractAddress] = await contractCreatorFcn( contractBytecode, operatorKey, operatorId, false ); console.log(`\n- 2.1) The smart contract ID is: ${contractId}`); console.log(`- The smart contract ID in Solidity format is: ${contractAddress}`); await getStakingInfoFcn(contractId); // Update an existing smart contract (must have admin key) const updateStatus = await contractUpdaterFcn(contractId, operatorKey, null, 4, true); console.log(`\n- 2.2) ${updateStatus}: Updated contract ${contractId}:`); await getStakingInfoFcn(contractId); console.log(`\n- THE END ============================================`); } Copy 2.1: Staking for Newly Created Contracts The function contractCreatorFcn deploys a new Hedera contract and sets its staking options. The function takes as inputs the compiled bytecode and admin key for the contract, the account ID to stake to, and a Boolean for declining or receiving the staking rewards. Use ContractCreateFlow() to deploy the new contract with the inputs provided. Be sure to set either setStakedAccountId() or setStakedNodeId(). Do NOT set both – the same applies for the update transaction. Note that additional method calls are commented out in case you need them in the future. The last steps are to get a transaction receipt with the Hedera client and return the contract ID and contract address in Solidity format. async function contractCreatorFcn(contractBytecode, adminKey, stakeAccount, noRewardFlag) { const contractDeployTx = await new ContractCreateFlow() .setBytecode(contractBytecode) .setGas(100000) .setAdminKey(adminKey) .setStakedAccountId(stakeAccount) // SET THIS ONE... // .setStakedNodeId(stakeNode) // OR THIS ONE - DON'T SET BOTH // .setDeclineStakingReward(noRewardFlag) // MISSING IN SDK V2.17 FOR ContractCreateFlow() // .setInitialBalance(initialBalance) // .setConstructorParameters(constructorParameters) // .setContractMemo(memo) // .setAutoRenewAccountId(autoRenewAccountId) // .setAutoRenewPeriod(autoRenewPeriod) // .setMaxAutomaticTokenAssociations(amount) .execute(client); const contractDeployRx = await contractDeployTx.getReceipt(client); const contractId = contractDeployRx.contractId; const contractAddress = contractId.toSolidityAddress(); return [contractId, contractAddress]; } Copy 2.2: Staking for Existing Contracts The function contractUpdaterFcn uses the ContractUpdateTransaction() class to modify properties of the Hedera contract deployed in the previous step. The inputs for the function are the contract ID to update, the admin key of that contract (to authorize the update), the node ID to stake to, and a Boolean for declining or receiving the staking rewards. Note that this transaction must be signed (authorized) using the admin key of the contract being updated. If a contract doesn’t have an admin key, that means it’s immutable, so the staking options must be set during creation. Lastly, the client submits the transaction and gets a receipt. The function returns the transaction status for the update. Just like in the previous step, additional methods for this class are shown and commented out in case you need them. Also, keep in mind that you can edit these functions to receive any inputs and create or update accounts according to your needs in the future. async function contractUpdaterFcn(id, adminKey, stakeAccount, stakeNode, noRewardFlag) { const contractUpdateTx = new ContractUpdateTransaction() .setContractId(id) // .setStakedAccountId(stakeAccount) // SET THIS ONE... .setStakedNodeId(stakeNode) // OR THIS ONE - DON'T SET BOTH .setDeclineStakingReward(noRewardFlag) // .setAdminKey(adminKey) // .setContractMemo(memo) // .setAutoRenewAccountId(autoRenewAccountId) // .setAutoRenewPeriod(autoRenewPeriod) // .setContractExpirationTime(expirationTime) // .setMaxAutomaticTokenAssociations(amount) .freezeWith(client); const contractUpdateSign = await contractUpdateTx.sign(adminKey); const contractUpdateSubmit = await contractUpdateSign.execute(client); const contractUpdateRx = await contractUpdateSubmit.getReceipt(client); return contractUpdateRx.status; } Copy Now you know how to stake HBAR balances for Hedera accounts and contracts upon creation and at any time by making updates to those entities. This example used the Hedera JavaScript SDK. However, you can try this with the other officially supported SDKs for Java and Go. Continue Learning Open a Testnet Account Try Examples and Tutorials Join the Developer Discord Read the Learning Center Share This Back to blog What is gRPC, gRPC-Web, and Proxies? Ed Marquez Pragmatic Blockchain Design Patterns – Integrating Blockchain into Business Processes Michiel Mulders Zero Cost EthereumTransaction on Success: Hedera's New Fee Model for Relay Operators Oliver Thorn Hedera Adopts Chainlink Standard for Cross-Chain Interoperability To Accelerate Ecosystem Adoption Hedera Team Hedera Developer Highlights March 2025 Michiel Mulders Hedera Release Cycle Overview Ed Marquez View All Posts Sign up for the newsletter CONNECT WITH US Transparency Open Source Audits & Standards Sustainability Commitment Carbon Offsets Governance Hedera Council Public Policy Treasury Management Meeting Minutes LLC Agreement Node Requirements Community Events Meetups HBAR Telegram Developer Discord Twitter Community Support FAQ Network Status Developer Discord StackOverflow Brand Brand Guidelines Built on Hedera Logo Hedera Store About Team Partners Journey Roadmap Careers Contact General Inquiry Public Relations © 2018-2025 Hedera Hashgraph, LLC. All trademarks and company names are the property of their respective owners. All rights in the Deutsche Telekom mark are protected by Deutsche Telekom AG. All rights reserved. Hedera uses the third party marks with permission. Terms of Use  |  Privacy Policy