Staking/Claiming Example
In this demonstration, we will use ExpressJS
and Typescript
to illustrate a straightforward example.
Our objective is to walk you through the process of staking/claiming assets with the SDK while utilizing your hosted backend.
Stake or Harvest Assets
Setup
import express, { Express, Request, Response } from "express";
import bodyParser from "body-parser";
import { anvil } from "@ada-anvil/sdk-server";
const app: Express = express();
app.use(bodyParser.json());
// Setup API Key provided by Anvil
anvil.config.authenticate("server_XXXXXXXXX");
Stake Assets
/**
* Returns the Transaction and the Stake Id, the Client must sign the Transaction
*/
app.post("/stake-assets", async (req: Request, res: Response) => {
const { stakeCollectionId, assets, timeToLock, utxos, changeAddress } = req.body;
try {
const { transaction, stakeId } = await anvil.staking.stakeAssets({
stakeCollectionId,
assets,
seconds: timeToLock,
utxos,
changeAddress,
});
res.json({ transaction, stakeId });
} catch {
console.log("YOUR ERROR HANDLING");
}
});
To proceed, the client needs to sign the transaction using their preferred wallet.
Afterward, they have to transmit the signature, transaction data, and the corresponding stake ID for submission.
The context
field must be set to STAKING
Claim Assets
app.post("/harvest-stake", async (req: Request, res: Response) => {
try {
const { changeAddress, stakeId, utxos } = req.body;
const { transaction } = await anvil.staking.harvestStake({
mainnet: true,
claim: true,
stakeId,
utxos,
changeAddress,
});
res.json({ transaction, stakeId });
} catch (e) {
console.log("YOUR ERROR HANDLING");
return res.json({ message: e.message });
}
});
To proceed, the client needs to sign the transaction using their preferred wallet.
Afterward, they have to transmit the signature, transaction data, and the corresponding stake ID for submission.
The context
field must be set to HARVESTING
Submit Stake
/**
* Returns the Transaction Hash
*/
app.post("/submit-stake", async (req: Request, res: Response) => {
try {
const { signature, stakeId, transaction, context } = req.body;
const txHash = await anvil.staking.submitStake({
context,
mainnet: true,
signature,
stakeId,
transaction,
});
res.json({ txHash });
} catch (e) {
console.log("YOUR ERROR HANDLING");
return res.json({ message: e.message });
}
});
Start the express Server
app.listen(3333, () => {
console.log("SERVER LISTENING ON PORT 3333");
});
Integrate the Server SDK with the Client SDK.
In your frontend implementation, you can find guidance on acquiring the Change Address and UTXOs by consulting the documentation provided in Obtaining the Change Address & UTXOs.