Skip to content

Commit

Permalink
Add priority fees and limit to batches of 5 txns (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass authored Jan 29, 2024
1 parent 17054e3 commit 9394857
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 88 deletions.
63 changes: 57 additions & 6 deletions components/LockTokensAccount.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { BN } from "@coral-xyz/anchor";
import {
useAnchorProvider,
useAssociatedTokenAccount,
useMint,
useOwnedAmount,
} from "@helium/helium-react-hooks";
import { toBN, toNumber } from "@helium/spl-utils";
import {
batchInstructionsToTxsWithPriorityFee,
batchParallelInstructionsWithPriorityFee,
sendAndConfirmWithRetry,
toBN,
toNumber,
} from "@helium/spl-utils";
import {
calcLockupMultiplier,
getRegistrarKey,
Expand All @@ -15,7 +22,15 @@ import {
useSubDaos,
} from "@helium/voter-stake-registry-hooks";
import { useConnection, useWallet } from "@solana/wallet-adapter-react";
import { useWalletModal } from "@solana/wallet-adapter-react-ui";
import {
Keypair,
LAMPORTS_PER_SOL,
TransactionInstruction,
} from "@solana/web3.js";
import axios from "axios";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { useAsync } from "react-async-hook";
import { AiFillLock } from "react-icons/ai";
import { BsFillLightningChargeFill, BsLink45Deg } from "react-icons/bs";
import { useMetaplexMetadata } from "../hooks/useMetaplexMetadata";
Expand All @@ -27,10 +42,7 @@ import { LockCommunityTokensButton } from "./LockCommunityTokensButton";
import { LockTokensModal, LockTokensModalFormValues } from "./LockTokensModal";
import { PositionCard } from "./PositionCard";
import { VotingPowerBox } from "./VotingPowerBox";
import { useAsync } from "react-async-hook";
import axios from "axios";
import { LAMPORTS_PER_SOL } from "@solana/web3.js";
import { useWalletModal } from "@solana/wallet-adapter-react-ui";
import { MAX_TRANSACTIONS_PER_SIGNATURE_BATCH } from "./constants";

function daysToSecs(days: number): number {
return days * 60 * 60 * 24;
Expand Down Expand Up @@ -63,6 +75,41 @@ export const LockTokensAccount: React.FC = (props) => {
const { setVisible } = useWalletModal();
const { symbol: tokenName } = useMetaplexMetadata(mint);
const canDelegate = true;
const provider = useAnchorProvider();

const onInstructions = async (
instructions: TransactionInstruction[],
sigs?: Keypair[]
) => {
if (sigs) {
const transactions = await batchInstructionsToTxsWithPriorityFee(
provider,
instructions
);
for (const tx of await provider.wallet.signAllTransactions(
transactions
)) {
sigs.forEach((sig) => {
if (tx.signatures.some((s) => s.publicKey.equals(sig.publicKey))) {
tx.partialSign(sig);
}
});

await sendAndConfirmWithRetry(
provider.connection,
tx.serialize(),
{
skipPreflight: true,
},
"confirmed"
);
}
} else {
await batchParallelInstructionsWithPriorityFee(provider, instructions, {
maxSignatureBatch: MAX_TRANSACTIONS_PER_SIGNATURE_BATCH,
});
}
};

const { info: registrar } = useRegistrar(getRegistrarKey(mint));
const { info: mintAcc } = useMint(mint);
Expand Down Expand Up @@ -148,13 +195,17 @@ export const LockTokensAccount: React.FC = (props) => {
lockupPeriodsInDays: lockupPeriodInDays,
lockupKind: lockupKind.value,
mint,
onInstructions,
});
await refetchState();
};

const handleClaimAllRewards = async () => {
try {
await claimAllPositionsRewards({ positions: positionsWithRewards });
await claimAllPositionsRewards({
positions: positionsWithRewards,
onInstructions,
});

if (!claimingAllRewardsError) {
await refetchState();
Expand Down
69 changes: 64 additions & 5 deletions components/PositionCard.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import React, { useCallback, useState, useMemo } from "react";
import { useMint, useSolanaUnixNow } from "@helium/helium-react-hooks";
import {
useAnchorProvider,
useMint,
useSolanaUnixNow,
} from "@helium/helium-react-hooks";
import { BN } from "@coral-xyz/anchor";
import Button, { SecondaryButton } from "./Button";
import { HNT_MINT, toNumber } from "@helium/spl-utils";
import {
HNT_MINT,
batchInstructionsToTxsWithPriorityFee,
batchParallelInstructionsWithPriorityFee,
sendAndConfirmWithRetry,
toNumber,
} from "@helium/spl-utils";
import { notify } from "../utils/notifications";
import {
daysToSecs,
Expand Down Expand Up @@ -39,6 +49,8 @@ import { useMetaplexMetadata } from "../hooks/useMetaplexMetadata";
import { FaCodeBranch } from "react-icons/fa6";
import { FaPauseCircle, FaPlayCircle, FaCalendarPlus } from "react-icons/fa";
import { BiTransfer } from "react-icons/bi";
import { MAX_TRANSACTIONS_PER_SIGNATURE_BATCH } from "./constants";
import { Keypair, TransactionInstruction } from "@solana/web3.js";

interface PositionCardProps {
subDaos?: SubDaoWithMeta[];
Expand All @@ -56,6 +68,7 @@ export const PositionCard: React.FC<PositionCardProps> = ({
const [isSplitModalOpen, setIsSplitModalOpen] = useState(false);
const [isDelegateModalOpen, setIsDelegateModalOpen] = useState(false);
const { loading: isLoading, positions, refetch } = useHeliumVsrState();
const provider = useAnchorProvider();

const transferablePositions: PositionWithMeta[] = useMemo(() => {
if (!unixNow || !positions.length) {
Expand Down Expand Up @@ -91,6 +104,41 @@ export const PositionCard: React.FC<PositionCardProps> = ({
});
}, [position, unixNow, positions]);

const onInstructions = async (
instructions: TransactionInstruction[],
sigs?: Keypair[]
) => {
if (sigs) {
const transactions = await batchInstructionsToTxsWithPriorityFee(
provider,
instructions
);
for (const tx of await provider.wallet.signAllTransactions(
transactions
)) {
sigs.forEach((sig) => {
if (tx.signatures.some((s) => s.publicKey.equals(sig.publicKey))) {
tx.partialSign(sig);
}
});

console.log(tx.signatures)
await sendAndConfirmWithRetry(
provider.connection,
tx.serialize(),
{
skipPreflight: true,
},
"confirmed"
);
}
} else {
await batchParallelInstructionsWithPriorityFee(provider, instructions, {
maxSignatureBatch: MAX_TRANSACTIONS_PER_SIGNATURE_BATCH,
});
}
};

const {
loading: isExtending,
error: extendingError,
Expand Down Expand Up @@ -178,7 +226,10 @@ export const PositionCard: React.FC<PositionCardProps> = ({

const handleFlipPositionLockupKind = async () => {
try {
await flipPositionLockupKind({ position });
await flipPositionLockupKind({
position,
onInstructions,
});

if (!flippingError) {
await refetchState();
Expand All @@ -199,6 +250,7 @@ export const PositionCard: React.FC<PositionCardProps> = ({
await extendPosition({
position,
lockupPeriodsInDays: values.lockupPeriodInDays,
onInstructions,
});

if (!extendingError) {
Expand All @@ -212,6 +264,7 @@ export const PositionCard: React.FC<PositionCardProps> = ({
amount: values.amount,
lockupKind: values.lockupKind.value,
lockupPeriodsInDays: values.lockupPeriodInDays,
onInstructions,
});

if (!splitingError) {
Expand All @@ -227,6 +280,7 @@ export const PositionCard: React.FC<PositionCardProps> = ({
sourcePosition: position,
amount,
targetPosition,
onInstructions,
});

if (!transferingError) {
Expand All @@ -238,6 +292,7 @@ export const PositionCard: React.FC<PositionCardProps> = ({
await delegatePosition({
position,
subDao,
onInstructions,
});

if (!delegatingError) {
Expand All @@ -247,7 +302,7 @@ export const PositionCard: React.FC<PositionCardProps> = ({

const handleUndelegateTokens = async () => {
try {
await undelegatePosition({ position });
await undelegatePosition({ position, onInstructions });

if (!undelegatingError) {
await refetchState();
Expand All @@ -263,7 +318,10 @@ export const PositionCard: React.FC<PositionCardProps> = ({

const handleClaimRewards = async () => {
try {
await claimPositionRewards({ position });
await claimPositionRewards({
position,
onInstructions,
});

if (!claimingRewardsError) {
await refetchState();
Expand All @@ -281,6 +339,7 @@ export const PositionCard: React.FC<PositionCardProps> = ({
try {
await closePosition({
position,
onInstructions,
});

if (!closingError) {
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
},
"dependencies": {
"@coral-xyz/anchor": "^0.28.0",
"@helium/account-fetch-cache": "^0.6.23",
"@helium/account-fetch-cache-hooks": "^0.6.23",
"@helium/account-fetch-cache": "^0.6.25",
"@helium/account-fetch-cache-hooks": "^0.6.25",
"@helium/currency": "^4.10.1",
"@helium/helium-react-hooks": "^0.6.23",
"@helium/helium-react-hooks": "^0.6.25",
"@helium/modular-governance-hooks": "^0.0.8",
"@helium/organization-sdk": "^0.0.8",
"@helium/spl-utils": "^0.6.23",
"@helium/spl-utils": "^0.6.25",
"@helium/state-controller-sdk": "^0.0.8",
"@helium/voter-stake-registry-hooks": "^0.6.23",
"@helium/voter-stake-registry-sdk": "^0.6.23",
"@helium/voter-stake-registry-hooks": "^0.6.25",
"@helium/voter-stake-registry-sdk": "^0.6.25",
"@metaplex-foundation/mpl-token-metadata": "2.10.0",
"@project-serum/anchor": "^0.25.0",
"@solana/spl-token": "^0.3.8",
Expand Down Expand Up @@ -54,11 +54,11 @@
},
"resolutions": {
"@solana/web3.js": "^1.78.4",
"@helium/account-fetch-cache": "^0.6.23",
"@helium/account-fetch-cache-hooks": "^0.6.23",
"@helium/helium-react-hooks": "^0.6.23",
"@helium/voter-stake-registry-hooks": "^0.6.23",
"@helium/spl-utils": "^0.6.23",
"@helium/account-fetch-cache": "^0.6.25",
"@helium/account-fetch-cache-hooks": "^0.6.25",
"@helium/helium-react-hooks": "^0.6.25",
"@helium/voter-stake-registry-hooks": "^0.6.25",
"@helium/spl-utils": "^0.6.25",
"@helium/modular-governance-hooks": "^0.0.8",
"@solana/wallet-adapter-react": "^0.15.32"
},
Expand Down
Loading

0 comments on commit 9394857

Please sign in to comment.