@cube3/sdk-nodejs
v1.1.6
Published
Example
Downloads
42
Readme
Example
DApp's backend
we can ask CUBE3 to validate transaction for us using cube3ValidateTransaction method from @cube3/sdk-nodejs library. For that, you need to set your apiKey, validationUrl.
import { cube3ValidateTransaction, Cube3Api, Cube3ValidationData } from '@cube-web3/sdk-nodejs'
import { ethers } from "ethers";
export class ValidationController {
public static async validateTransaction(req: Request, res: Response) {
const apiKey = "<...>"
const validationUrl = "https://<...>/api/transaction/validate"
const cube3ValidationData: Cube3ValidationData = {
transactionData: req.body,
clientIp: req.ip
}
const validation = await cube3ValidateTransaction(validationUrl, apiKey, cube3ValidationData)
return res.status(200).send(JSON.stringify(cube3ValidationResponse))
}
}
This is what validation returns
interface Cube3ValidationResponse {
valid: boolean
payload?: string
error?: string
combinedScore: number
cube3TxId: string
}
After sending the transaction to the chain, we will save the transaction ID to the system. To do so, you will need a transaction ID and an internal CUBE3 transaction ID. However, we cannot do that on frontend because our API-KEY would get exposed. So, we will call dApp’s backend with receipt hash and internal CUBE3 transaction ID in body.
import { cube3ValidateTransaction, Cube3ValidationData } from '@cube3/sdk-nodejs'
import { ethers } from "ethers";
export class ValidationController {
public static async saveTransaction(req: Request, res: Response) {
const apiKey: "<...>",
const saveTransactionUrl: "https://<...>/api/transaction"
const transactionId = req.body.transactionId
const cube3TxId = req.body.cube3TxId
const response = await cube3SaveTransaction(saveTransactionUrl, apiKey, transactionId, cube3TxId)
return res.status(200).send(JSON.stringify(response))
}
}