Skip to content

Instantly share code, notes, and snippets.

View 0xdeepmehta's full-sized avatar
🤿
Solana

0xDeep 0xdeepmehta

🤿
Solana
View GitHub Profile
@0xdeepmehta
0xdeepmehta / aprToApy.js
Created July 11, 2024 08:38
APY calculation from APR on the Basis of solana block time
function calculateAPYFromAPR(annualPercentageRate) {
// Slot calculations
const SLOTS_PER_SECOND = 2;
const SLOTS_PER_MINUTE = SLOTS_PER_SECOND * 60;
const SLOTS_PER_HOUR = SLOTS_PER_MINUTE * 60;
const SLOTS_PER_DAY = SLOTS_PER_HOUR * 24;
const SLOTS_PER_YEAR = SLOTS_PER_DAY * 365;
// Step 1: Calculate the rate per slot
const ratePerSlot = new Decimal(1).plus(
async function profiledFunctionExecution(asyncFunctionToProfile, functionName, additionalData = []) {
const startTime = Date.now();
const PROFILE_LOG_PREFIX = "[PROFILING]";
// Start a transaction for monitoring (assuming a monitoring library is used)
const transaction = startTransaction({ name: functionName });
// Log the start of function execution
console.log(PROFILE_LOG_PREFIX, `function=${functionName} event=start ts=${new Date(startTime).toISOString()}`);
async processTrancationJito(
jitoTip: number, // in ui
tx: Transaction,
luts?: AddressLookupTableAccount[],
priorityFee?: number, // priorityFeeUi
) {
console.log(`this.provider.connection.commitment :: ${this.provider.connection.commitment}`);
const jitoTipInLamport = jitoTip * LAMPORTS_PER_SOL;
console.log(`jitoTipInLamport :: ${jitoTipInLamport}`)
#!/bin/sh
git filter-branch --env-filter '
if [ "$GIT_COMMITTER_EMAIL" = "old_email" ]; then
export GIT_COMMITTER_EMAIL="new_email"
fi
if [ "$GIT_AUTHOR_EMAIL" = "old_email" ]; then
export GIT_AUTHOR_EMAIL="new_email"
fi
' --tag-name-filter cat -- --branches --tags
@0xdeepmehta
0xdeepmehta / sol_wrap_unwrap.rs
Created October 3, 2023 17:38
wrap and unwrap sol snippet
use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
use spl_token::instruction::close_account;
pub fn make_wrap_sol_ixs(config: &Config) -> Result<()> {
println!("sol transfer :: {}", config.payer.pubkey());
// ata get -> ata create_idempotent -> trasnfer -> sync
let deposit_ata = anchor_spl::associated_token::get_associated_token_address(
&config.payer.pubkey(),
&pubkey!("So11111111111111111111111111111111111111112"),
@0xdeepmehta
0xdeepmehta / keypair_utils.ts
Created September 29, 2023 17:08
Read and write keypair in jsonfile anchor solana
import * as fs from 'fs/promises';
import * as path from 'path';
const writeKeypairToFile = async (sk: Uint8Array, fileName: string): Promise<void> => {
const filePath = path.join('tests/keys', `${fileName}.json`);
try {
await fs.writeFile(filePath, JSON.stringify(Array.from(sk)));
console.log(`Keypair written to file: ${filePath}`);
} catch (error) {
const credential = (await
navigator
.credentials.create({
publicKey: {
challenge: new Uint8Array(16),
rp: {
name: "0xdeep.com",
},
user: {
id: new Uint8Array(16),
import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { HelloSupersec } from "../target/types/hello_supersec";
describe("hello-supersec", () => {
// Configure the client to use the local cluster.
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const program = anchor.workspace.HelloSupersec as Program<HelloSupersec>;
// Define the size of each chunk
const chunkSize = 1000;
// Create a function to iterate over the items in chunks
async function iterateInChunks(items: any[], callback: (chunk: any[]) => void) {
// Calculate the number of chunks
const numChunks = Math.ceil(items.length / chunkSize);
// Create an array to hold the promises for the async operations
const promises = [];
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract AMM is ReentrancyGuard{
using Counters for Counters.Counter;
Counters.Counter private caseID;