Skip to content

Commit

Permalink
create quickstart example
Browse files Browse the repository at this point in the history
  • Loading branch information
strykerin committed Feb 11, 2025
1 parent 0a83aab commit 0df8ad0
Show file tree
Hide file tree
Showing 4 changed files with 1,273 additions and 14 deletions.
58 changes: 58 additions & 0 deletions examples/chain-quickstart/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Import required modules
import { PushChain, CONSTANTS } from '@pushchain/devnet';
import { hexToBytes } from 'viem';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';

// ====================================================================
// Initialize Push Chain SDK without a signer. This is used for *read-only* operations.
// ====================================================================
const pushChainReadOnly = await PushChain.initialize();
console.log(
'Initialized PushChain in read-only mode. Fetching the latest transaction:'
);
console.log(await pushChainReadOnly.tx.get('*', { limit: 1 }));

// ====================================================================
// Initialize Push Chain SDK with a signer. This is used for *read-write* operations.
// ====================================================================

// Generate Private Key
const privateKey = generatePrivateKey();
const account = privateKeyToAccount(privateKey);

const signer = {
chain: CONSTANTS.CHAIN.ETHEREUM,
chainId: CONSTANTS.CHAIN_ID.ETHEREUM.SEPOLIA,
address: account.address, // Ethereum address derived from the private key
signMessage: async (data) => {
const signature = await account.signMessage({
message: { raw: data }, // Data to be signed
});
return hexToBytes(signature); // Convert signature to a byte array
},
};

// Initialize the SDK with the Signer
const pushChainReadWrite = await PushChain.initialize(signer);
console.log('Initialized PushChain with a signer for read-write operations.');
console.log('Signer details:', {
chain: signer.chain,
chainId: signer.chainId,
address: signer.address,
});

const tx = await pushChainReadWrite.tx.send(
[
{
chain: CONSTANTS.CHAIN.SOLANA,
chainId: CONSTANTS.CHAIN_ID.SOLANA.DEVNET,
account: 'ySYrGNLLJSK9hvGGpoxg8TzWfRe8ftBtDSMECtx2eJR',
},
],
{
category: 'MY_CUSTOM_CATEGORY',
data: 'Hi!',
}
);

console.log('Transaction sent successfully! Transaction hash:', tx.txHash);
Loading

0 comments on commit 0df8ad0

Please sign in to comment.