SDK Documentation
Diamondz Shadow SDK
The Diamondz Shadow SDK is a comprehensive toolkit for building applications on our Layer 2 blockchain. It provides a set of libraries and utilities to interact with the Diamondz Shadow network, deploy contracts, and integrate with our unique features like the Proof of Contribution system.
Installation
You can install the Diamondz Shadow SDK using npm or yarn:
# Using npm
npm install @diamondz-shadow/sdk
# Using yarn
yarn add @diamondz-shadow/sdk
SDK Components
Core SDK
The foundation of the SDK, providing basic functionality for interacting with the Diamondz Shadow network.
- Network configuration
- Provider management
- Transaction utilities
- Type definitions
Contract Wrappers
Type-safe wrappers for interacting with the core contracts of the Diamondz Shadow ecosystem.
- SDM Token
- Bridge contracts
- Contribution Tracker
- YouTube Oracle
Cross-Domain Messaging
Utilities for sending messages between Layer 1 (Ethereum) and Layer 2 (Diamondz Shadow).
- Message passing
- State verification
- Proof generation
- Message status tracking
Utilities
Helper functions and utilities for common tasks when building on Diamondz Shadow.
- Gas estimation
- Address utilities
- Encoding/decoding
- Signature verification
Basic Usage
Here's a simple example of how to use the SDK to connect to the Diamondz Shadow network and interact with the SDM token:
import { DiamondShadow, SDMToken } from '@diamondz-shadow/sdk';
// Initialize the SDK with a provider
const ds = new DiamondShadow({
rpcUrl: 'https://rpc.testnet.diamondzshadow.net',
chainId: 42069,
});
// Connect to the SDM token contract
const sdmToken = new SDMToken({
address: '0x123...789', // SDM token address
provider: ds.provider,
});
// Get token balance
async function getBalance(address) {
const balance = await sdmToken.balanceOf(address);
console.log(`Balance: ${balance.toString()}`);
return balance;
}
// Transfer tokens
async function transfer(to, amount) {
const tx = await sdmToken.transfer(to, amount);
const receipt = await tx.wait();
console.log(`Transfer successful: ${receipt.transactionHash}`);
return receipt;
}
Cross-Domain Messaging
One of the key features of the Diamondz Shadow SDK is the ability to send messages between Layer 1 (Ethereum) and Layer 2 (Diamondz Shadow). Here's an example:
import { DiamondShadow, CrossDomainMessenger } from '@diamondz-shadow/sdk';
// Initialize the SDK with L1 and L2 providers
const ds = new DiamondShadow({
l1: {
rpcUrl: 'https://mainnet.infura.io/v3/YOUR_INFURA_KEY',
chainId: 1,
},
l2: {
rpcUrl: 'https://rpc.testnet.diamondzshadow.net',
chainId: 42069,
},
});
// Initialize the cross-domain messenger
const messenger = new CrossDomainMessenger({
l1Provider: ds.l1Provider,
l2Provider: ds.l2Provider,
l1MessengerAddress: '0xabc...123', // L1 messenger address
l2MessengerAddress: '0xdef...456', // L2 messenger address,
});
// Send a message from L1 to L2
async function sendL1ToL2Message(targetAddress, data) {
const tx = await messenger.sendMessageToL2(
targetAddress,
data,
1000000 // gas limit
);
const receipt = await tx.wait();
console.log(`Message sent: ${receipt.transactionHash}`);
// Wait for the message to be relayed to L2
const l2Receipt = await messenger.waitForMessageReceipt(tx);
console.log(`Message processed on L2: ${l2Receipt.transactionHash}`);
return l2Receipt;
}
// Send a message from L2 to L1
async function sendL2ToL1Message(targetAddress, data) {
const tx = await messenger.sendMessageToL1(
targetAddress,
data,
1000000 // gas limit
);
const receipt = await tx.wait();
console.log(`Message sent: ${receipt.transactionHash}`);
// Wait for the message to be relayed to L1 (this can take some time)
const l1Receipt = await messenger.waitForMessageReceipt(tx);
console.log(`Message processed on L1: ${l1Receipt.transactionHash}`);
return l1Receipt;
}
Contribution Tracking
The Diamondz Shadow blockchain features a unique Proof of Contribution system. Here's how to interact with it using the SDK:
import { DiamondShadow, ContributionTracker } from '@diamondz-shadow/sdk';
// Initialize the SDK
const ds = new DiamondShadow({
rpcUrl: 'https://rpc.testnet.diamondzshadow.net',
chainId: 42069,
});
// Connect to the Contribution Tracker contract
const tracker = new ContributionTracker({
address: '0x789...abc', // Contribution Tracker address
provider: ds.provider,
});
// Submit a contribution
async function submitContribution(type, description) {
const tx = await tracker.submitContribution(type, description);
const receipt = await tx.wait();
console.log(`Contribution submitted: ${receipt.transactionHash}`);
// Get the contribution ID from the event
const event = receipt.events.find(e => e.event === 'ContributionSubmitted');
const contributionId = event.args.contributionId;
return contributionId;
}
// Get a user's contributions
async function getUserContributions(address) {
const count = await tracker.getContributionsCount(address);
const contributions = [];
for (let i = 0; i < count; i++) {
const contribution = await tracker.getContribution(address, i);
contributions.push({
id: i,
type: contribution.contributionType,
points: contribution.points,
description: contribution.description,
timestamp: contribution.timestamp,
validated: contribution.validated
});
}
return contributions;
}
// Get a user's total contribution points
async function getUserPoints(address) {
const points = await tracker.contributionPoints(address);
console.log(`Total points: ${points.toString()}`);
return points;
}
YouTube Oracle Integration
The Diamondz Shadow blockchain integrates with YouTube metrics through our oracle system. Here's how to interact with it:
import { DiamondShadow, YouTubeOracle } from '@diamondz-shadow/sdk';
// Initialize the SDK
const ds = new DiamondShadow({
rpcUrl: 'https://rpc.testnet.diamondzshadow.net',
chainId: 42069,
});
// Connect to the YouTube Oracle contract
const oracle = new YouTubeOracle({
address: '0xdef...789', // YouTube Oracle address
provider: ds.provider,
});
// Get the latest YouTube metrics
async function getYouTubeMetrics() {
const viewCount = await oracle.viewCount();
const subscriberCount = await oracle.subscriberCount();
const lastUpdated = await oracle.lastUpdated();
return {
viewCount: viewCount.toString(),
subscriberCount: subscriberCount.toString(),
lastUpdated: new Date(lastUpdated.toNumber() * 1000).toISOString()
};
}
// Request an update of YouTube metrics (requires ORACLE_UPDATER_ROLE)
async function requestYouTubeUpdate() {
const tx = await oracle.requestYouTubeData();
const receipt = await tx.wait();
console.log(`Update requested: ${receipt.transactionHash}`);
// Get the request ID from the event
const event = receipt.events.find(e => e.event === 'OracleDataRequested');
const requestId = event.args.requestId;
return requestId;
}
Advanced Topics
Gas Optimization
Learn how to optimize gas usage on the Diamondz Shadow network, including batch transactions and calldata compression.
View GuideSecurity Best Practices
Security considerations when building on Diamondz Shadow, including cross-domain vulnerabilities and oracle safety.
View GuideProof of Contribution
Deep dive into our Proof of Contribution system, including validation mechanisms and reward distribution.
View GuideYouTube Integration
Detailed explanation of how our YouTube oracle works and how to integrate it into your applications.
View GuideNeed Help?
If you have questions about the SDK or need assistance with your integration, you can reach out to our developer community:
- GitHub - Open issues or contribute to our repositories
- Discord - Join our developer community
- Email Support - Contact our developer relations team