Paste the generated API key in the “Your ChainGPT API key” placeholder.
import { SmartContractAuditor } from "@chaingpt/smartcontractauditor";
const smartcontractauditor = new SmartContractAuditor({
apiKey: 'Your ChainGPT API Key',
});
async function main() {
const stream = await smartcontractauditor.createSmartContractStream({
question: 'Write a smart contract that counts. It will have two functions.',
chatHistory: "off"
});
stream.on('data', (chunk: any) => console.log(chunk.toString()));
stream.on('end', () => console.log("Stream ended"));
}
main();
Core Libraries
We provide TypeScript/ JavaScript libraries with support for Node.js. Install it by running:
npm install --save @chaingpt/smartcontractauditor
# or
yarn add smartcontractauditor
Once installed, you can use the library and your secret key to run the following:
import { SmartContractAuditor } from "@chaingpt/smartcontractauditor";
const smartcontractauditor = new SmartContractAuditor({
apiKey: 'Your ChainGPT API Key',
});
async function main() {
const stream = await smartcontractauditor.auditSmartContractStream({
question:
`Audit the following contract:
pragma solidity ^0.8.0;
contract Counter {
uint256 private count; // This variable will hold the count
constructor() {
count = 0; // Initialize count to 0
}
function increment() public {
count += 1;
emit CountChanged(count); // Emit an event whenever the count changes
}
}`,
chatHistory: "on"
});
stream.on('data', (chunk: any) => console.log(chunk.toString()));
stream.on('end', () => console.log("Stream ended"));
}
main();
Advanced Features
ChainGPT Smart Contract Auditor SDK provides the following features:
Stream Response
Retrieve a chat response as a stream:
import { SmartContractAuditor } from "@chaingpt/smartcontractauditor";
const smartcontractauditor = new SmartContractAuditor({
apiKey: 'Your ChainGPT API Key',
});
async function main() {
const stream = await smartcontractauditor.auditSmartContractStream({
question: `Audit the following contract:
pragma solidity ^0.8.0;
contract Counter {
uint256 private count; // This variable will hold the count
constructor() {
count = 0; // Initialize count to 0
}
function increment() public {
count += 1;
emit CountChanged(count); // Emit an event whenever the count changes
}
}`,
chatHistory: "on"
});
stream.on('data', (chunk: any) => console.log(chunk.toString()));
stream.on('end', () => console.log("Stream ended"));
}
main();
Blob Response
Retrieve the chat Response in the form of Blob
import { SmartContractAuditor } from "@chaingpt/smartcontractauditor";
const smartcontractauditor = new SmartContractAuditor({
apiKey: 'Your ChainGPT API Key',
});
async function main() {
const response = await smartcontractauditor.auditSmartContractBlob({
question: `Audit the following contract:
pragma solidity ^0.8.0;
contract Counter {
uint256 private count; // This variable will hold the count
constructor() {
count = 0; // Initialize count to 0
}
function increment() public {
count += 1;
emit CountChanged(count); // Emit an event whenever the count changes
}
}`,
chatHistory: "off"
});
console.log(response.data.bot);
}
main();
Get Chat history:
Our Smart Contract Auditor SDK also stores the chat history for later usage and users can retrieve chat history by following the code:
import { SmartContractAuditor } from "@chaingpt/smartcontractauditor";
const smartcontractauditor = new SmartContractAuditor({
apiKey: 'Your ChainGPT API Key',
});
async function main() {
const response = await smartcontractauditor.getChatHistory({
limit: 10,
offset: 0,
sortBy: "createdAt",
sortOrder: "DESC"
});
console.log(response.data.rows);
}
main();
Error Handling
When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), an error of the class SmartContractAuditorError will be thrown:
import { Errors } from "@chaingpt/smartcontractauditor";
async function main() {
try {
const stream = await smartcontractauditor.auditSmartContractStream({
question: `Audit the following contract:
pragma solidity ^0.8.0;
contract Counter {
uint256 private count; // This variable will hold the count
constructor() {
count = 0; // Initialize count to 0
}
function increment() public {
count += 1;
emit CountChanged(count); // Emit an event whenever the count changes
}
}`,
chatHistory: "on"
});
stream.on('data', (chunk: any) => console.log(chunk.toString()));
stream.on('end', () => console.log("Stream ended"));
} catch (error) {
if(error instanceof Errors.SmartContractAuditorError) {
console.log(error.message)
}
}
}
main();
Language/ Framework Compatibility.
Our SDK supports Javascript language and will run on Node applications.
Security Considerations
To ensure security, the SDK is accessible using an authentication key. Users with credits in the web app and a valid API key can access the SDK.
Request limitations have been handled to avoid misuse. Users can make 200 requests per minute, and 1 credit will be deducted for each request.
Release Version
Release history is maintained. However, this is the first release; in the future, more features will be added, and the latest version will be released for users.
Code Documentation
Please check out this link for SDK code documentation