Skip to content

Commit

Permalink
Feature/close expired proxies cron (#688)
Browse files Browse the repository at this point in the history
* WIP

* wip

* close proxies in governance cron

* import fn from utils

* use batchInstructionsToTxsWithPriorityFee for proper packing

* loop txns and send with sendAndConfirmWithRetry

* no need to fillter now that we're closing 0 index

* Clean up the loop

* move signing to loop
  • Loading branch information
bryzettler authored Aug 9, 2024
1 parent f651cbe commit a6c5f31
Show file tree
Hide file tree
Showing 26 changed files with 265 additions and 184 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"@coral-xyz/anchor-cli": "^0.28.0",
"@coral-xyz/borsh": "^0.2.6",
"@helium/crypto": "^4.10.2",
"@helium/modular-governance-idls": "0.0.8-next.22+7098baa",
"@helium/nft-proxy-sdk": "0.0.8-next.22+7098baa",
"@helium/proposal-sdk": "0.0.8-next.22+7098baa",
"@helium/modular-governance-idls": "0.0.8-next.39+d213e1d",
"@helium/nft-proxy-sdk": "0.0.8-next.39+d213e1d",
"@helium/proposal-sdk": "0.0.8-next.39+d213e1d",
"@helium/transactions": "^3.38.0",
"@metaplex-foundation/mpl-bubblegum": "^0.7.0",
"@pythnetwork/client": "^2.8.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/crons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@helium/proposal-sdk": "^0.0.8",
"@helium/rewards-oracle-sdk": "^0.9.2",
"@helium/spl-utils": "^0.9.2",
"@helium/state-controller-sdk": "0.0.8-next.22+7098baa",
"@helium/state-controller-sdk": "0.0.8-next.39+d213e1d",
"@helium/treasury-management-sdk": "^0.9.2",
"@helium/voter-stake-registry-sdk": "^0.9.2",
"@solana/spl-token": "^0.3.8",
Expand Down
107 changes: 96 additions & 11 deletions packages/crons/src/close-governance.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import * as anchor from "@coral-xyz/anchor";
import { AccountFetchCache, chunks } from "@helium/account-fetch-cache";
import { AccountFetchCache } from "@helium/account-fetch-cache";
import { init as initNftProxy } from "@helium/nft-proxy-sdk";
import {
init as initOrg,
organizationKey,
proposalKey,
} from "@helium/organization-sdk";
import { init as initProposal } from "@helium/proposal-sdk";
import {
bulkSendTransactions,
batchInstructionsToTxsWithPriorityFee,
batchParallelInstructionsWithPriorityFee,
populateMissingDraftInfo,
sendAndConfirmWithRetry,
toVersionedTx,
} from "@helium/spl-utils";
import { init as initState } from "@helium/state-controller-sdk";
import { init as initVsr, positionKey } from "@helium/voter-stake-registry-sdk";
import { PublicKey, SystemProgram, Transaction } from "@solana/web3.js";
import {
PublicKey,
SYSVAR_CLOCK_PUBKEY,
SystemProgram,
TransactionInstruction,
} from "@solana/web3.js";
import pLimit from "p-limit";

async function getSolanaUnixTimestamp(
provider: anchor.AnchorProvider
): Promise<bigint> {
const clock = await provider.connection.getAccountInfo(SYSVAR_CLOCK_PUBKEY);
const unixTime = clock!.data.readBigInt64LE(8 * 4);
return unixTime;
}

(async () => {
try {
if (!process.env.ANCHOR_WALLET)
Expand All @@ -26,15 +43,18 @@ import pLimit from "p-limit";
anchor.setProvider(anchor.AnchorProvider.local(process.env.SOLANA_URL));

const provider = anchor.getProvider() as anchor.AnchorProvider;
const conn = provider.connection;
new AccountFetchCache({
connection: provider.connection,
connection: conn,
commitment: "confirmed",
extendConnection: true,
});
const orgProgram = await initOrg(provider);
const stateProgram = await initState(provider);
const proposalProgram = await initProposal(provider);
const vsrProgram = await initVsr(provider);
const proxyProgram = await initNftProxy(provider);
const solanaTime = await getSolanaUnixTimestamp(provider);
let closedProposals = new Set();
for (const orgName of ["Helium", "Helium MOBILE", "Helium IOT"]) {
console.log(`Checking for expired proposals in ${orgName}`);
Expand Down Expand Up @@ -81,15 +101,11 @@ import pLimit from "p-limit";
closedProposals.add(proposal.pubkey.toBase58());
}
}
await batchParallelInstructionsWithPriorityFee(
provider,
resolveIxs
);
await batchParallelInstructionsWithPriorityFee(provider, resolveIxs);
}

const markers = (await vsrProgram.account.voteMarkerV0.all()).filter(
(m) =>
closedProposals.has(m.account.proposal.toBase58())
const markers = (await vsrProgram.account.voteMarkerV0.all()).filter((m) =>
closedProposals.has(m.account.proposal.toBase58())
);
const limit = pLimit(100);
const relinquishIxns = await Promise.all(
Expand All @@ -114,6 +130,75 @@ import pLimit from "p-limit";
);
await batchParallelInstructionsWithPriorityFee(provider, relinquishIxns);

const proxyAssignments = await proxyProgram.account.proxyAssignmentV0.all();
const expiredProxyAssignments = proxyAssignments.filter((pa) => {
const { account } = pa;
return account.expirationTime.lt(new anchor.BN(Number(solanaTime)));
});

const proxyAssignmentsByAsset: {
[key: string]: (typeof proxyAssignments)[0][];
} = expiredProxyAssignments.reduce(
(acc, assignment) => ({
...acc,
[assignment.account.asset.toBase58()]: [
...(acc[assignment.account.asset.toBase58()] || []),
assignment,
],
}),
{}
);

const multiDimArray: TransactionInstruction[][] = await Promise.all(
Object.entries(proxyAssignmentsByAsset).map(async ([_, proxies], idx) => {
const sortedProxies = proxies.sort((a, b) =>
a.account.index < b.account.index ? 1 : -1
);

return await Promise.all(
sortedProxies.map(async (proxy, index) => {
if (index === sortedProxies.length - 1) {
return proxyProgram.methods
.closeExpiredProxyV0()
.accounts({
proxyAssignment: new PublicKey(proxy.publicKey),
})
.instruction();
}

const prevProxyAssignment = new PublicKey(
sortedProxies[index + 1].publicKey
);

return proxyProgram.methods
.unassignExpiredProxyV0()
.accounts({
prevProxyAssignment,
proxyAssignment: new PublicKey(proxy.publicKey),
})
.instruction();
})
);
})
);

const txs = await batchInstructionsToTxsWithPriorityFee(
provider,
multiDimArray.flat()
);

for (const tx of txs) {
const fullDraft = await populateMissingDraftInfo(conn, tx);
const versionedTx = toVersionedTx(fullDraft);
const signed = await provider.wallet.signTransaction(versionedTx);
await sendAndConfirmWithRetry(
conn,
Buffer.from(signed.serialize()),
{ skipPreflight: true },
"confirmed"
);
}

process.exit(0);
} catch (err) {
console.log(err);
Expand Down
26 changes: 13 additions & 13 deletions packages/crons/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ __metadata:
"@helium/proposal-sdk": ^0.0.8
"@helium/rewards-oracle-sdk": ^0.9.2
"@helium/spl-utils": ^0.9.2
"@helium/state-controller-sdk": 0.0.8-next.22+7098baa
"@helium/state-controller-sdk": 0.0.8-next.39+d213e1d
"@helium/treasury-management-sdk": ^0.9.2
"@helium/voter-stake-registry-sdk": ^0.9.2
"@solana/spl-token": ^0.3.8
Expand Down Expand Up @@ -385,7 +385,7 @@ __metadata:
languageName: unknown
linkType: soft

"@helium/modular-governance-idls@npm:^0.0.8, @helium/modular-governance-idls@npm:^0.0.8-next.22+7098baa":
"@helium/modular-governance-idls@npm:^0.0.8, @helium/modular-governance-idls@npm:^0.0.8-next.39+d213e1d":
version: 0.0.8
resolution: "@helium/modular-governance-idls@npm:0.0.8"
dependencies:
Expand All @@ -395,15 +395,15 @@ __metadata:
languageName: node
linkType: hard

"@helium/nft-proxy-sdk@npm:0.0.8-next.22+7098baa":
version: 0.0.8-next.19
resolution: "@helium/nft-proxy-sdk@npm:0.0.8-next.19"
"@helium/nft-proxy-sdk@npm:0.0.8-next.39+d213e1d":
version: 0.0.8-next.39
resolution: "@helium/nft-proxy-sdk@npm:0.0.8-next.39"
dependencies:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.2.17
"@helium/modular-governance-idls": ^0.0.8-next.22+7098baa
"@helium/modular-governance-idls": ^0.0.8-next.39+d213e1d
"@solana/spl-token": ^0.3.8
checksum: f5c6d9348cfebb8dd2d8fd997c365b5656afa7a6683592e1ed6b0bf04f73f231265a770c96022c930d32c686f57dbf56a0b7790c9d025413c80de30f07f1e587
checksum: bdb16423da6a52581317e191f61db35ae2d4642620ba8c99bc5a454bae052ce89fcd0bb0a45c3625c2d7ab8497e62fbfb5e31b704840ab7a5b177b93f3e6356c
languageName: node
linkType: hard

Expand Down Expand Up @@ -499,14 +499,14 @@ __metadata:
languageName: unknown
linkType: soft

"@helium/state-controller-sdk@npm:0.0.8-next.22+7098baa":
version: 0.0.8-next.19
resolution: "@helium/state-controller-sdk@npm:0.0.8-next.19"
"@helium/state-controller-sdk@npm:0.0.8-next.39+d213e1d":
version: 0.0.8-next.39
resolution: "@helium/state-controller-sdk@npm:0.0.8-next.39"
dependencies:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.5.0
"@helium/modular-governance-idls": ^0.0.8-next.22+7098baa
checksum: 51ac8d1151e7b02843e4ad437d7a20af03678198e67971698870ad1399f83ee976245df915998e9411310dc31d55f26ea7bc25f7235f20a20d573e0ff560a964
"@helium/modular-governance-idls": ^0.0.8-next.39+d213e1d
checksum: 76f09b7f934035351d96025195af61ea618a733f289513d72a4c0f659925b6da64add870e0681e4916f4b7038dcde5f825587eb6eedc327329e04cfe76f17f82
languageName: node
linkType: hard

Expand Down Expand Up @@ -534,7 +534,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.2
"@helium/idls": ^0.9.2
"@helium/nft-proxy-sdk": 0.0.8-next.22+7098baa
"@helium/nft-proxy-sdk": 0.0.8-next.39+d213e1d
"@helium/spl-utils": ^0.9.2
"@metaplex-foundation/mpl-token-metadata": ^2.10.0
"@solana/spl-token": ^0.3.8
Expand Down
14 changes: 7 additions & 7 deletions packages/data-credits-sdk/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ __metadata:
languageName: unknown
linkType: soft

"@helium/modular-governance-idls@npm:^0.0.8-next.22+7098baa":
"@helium/modular-governance-idls@npm:^0.0.8-next.39+d213e1d":
version: 0.0.8
resolution: "@helium/modular-governance-idls@npm:0.0.8"
dependencies:
Expand All @@ -193,15 +193,15 @@ __metadata:
languageName: node
linkType: hard

"@helium/nft-proxy-sdk@npm:0.0.8-next.22+7098baa":
version: 0.0.8-next.19
resolution: "@helium/nft-proxy-sdk@npm:0.0.8-next.19"
"@helium/nft-proxy-sdk@npm:0.0.8-next.39+d213e1d":
version: 0.0.8-next.39
resolution: "@helium/nft-proxy-sdk@npm:0.0.8-next.39"
dependencies:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.2.17
"@helium/modular-governance-idls": ^0.0.8-next.22+7098baa
"@helium/modular-governance-idls": ^0.0.8-next.39+d213e1d
"@solana/spl-token": ^0.3.8
checksum: f5c6d9348cfebb8dd2d8fd997c365b5656afa7a6683592e1ed6b0bf04f73f231265a770c96022c930d32c686f57dbf56a0b7790c9d025413c80de30f07f1e587
checksum: bdb16423da6a52581317e191f61db35ae2d4642620ba8c99bc5a454bae052ce89fcd0bb0a45c3625c2d7ab8497e62fbfb5e31b704840ab7a5b177b93f3e6356c
languageName: node
linkType: hard

Expand Down Expand Up @@ -251,7 +251,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.2
"@helium/idls": ^0.9.2
"@helium/nft-proxy-sdk": 0.0.8-next.22+7098baa
"@helium/nft-proxy-sdk": 0.0.8-next.39+d213e1d
"@helium/spl-utils": ^0.9.2
"@metaplex-foundation/mpl-token-metadata": ^2.10.0
"@solana/spl-token": ^0.3.8
Expand Down
14 changes: 7 additions & 7 deletions packages/distributor-oracle/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ __metadata:
languageName: unknown
linkType: soft

"@helium/modular-governance-idls@npm:^0.0.8-next.22+7098baa":
"@helium/modular-governance-idls@npm:^0.0.8-next.39+d213e1d":
version: 0.0.8
resolution: "@helium/modular-governance-idls@npm:0.0.8"
dependencies:
Expand All @@ -322,15 +322,15 @@ __metadata:
languageName: node
linkType: hard

"@helium/nft-proxy-sdk@npm:0.0.8-next.22+7098baa":
version: 0.0.8-next.19
resolution: "@helium/nft-proxy-sdk@npm:0.0.8-next.19"
"@helium/nft-proxy-sdk@npm:0.0.8-next.39+d213e1d":
version: 0.0.8-next.39
resolution: "@helium/nft-proxy-sdk@npm:0.0.8-next.39"
dependencies:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.2.17
"@helium/modular-governance-idls": ^0.0.8-next.22+7098baa
"@helium/modular-governance-idls": ^0.0.8-next.39+d213e1d
"@solana/spl-token": ^0.3.8
checksum: f5c6d9348cfebb8dd2d8fd997c365b5656afa7a6683592e1ed6b0bf04f73f231265a770c96022c930d32c686f57dbf56a0b7790c9d025413c80de30f07f1e587
checksum: bdb16423da6a52581317e191f61db35ae2d4642620ba8c99bc5a454bae052ce89fcd0bb0a45c3625c2d7ab8497e62fbfb5e31b704840ab7a5b177b93f3e6356c
languageName: node
linkType: hard

Expand Down Expand Up @@ -412,7 +412,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.2
"@helium/idls": ^0.9.2
"@helium/nft-proxy-sdk": 0.0.8-next.22+7098baa
"@helium/nft-proxy-sdk": 0.0.8-next.39+d213e1d
"@helium/spl-utils": ^0.9.2
"@metaplex-foundation/mpl-token-metadata": ^2.10.0
"@solana/spl-token": ^0.3.8
Expand Down
14 changes: 7 additions & 7 deletions packages/entity-invalidator/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ __metadata:
languageName: node
linkType: hard

"@helium/modular-governance-idls@npm:^0.0.8-next.22+7098baa":
"@helium/modular-governance-idls@npm:^0.0.8-next.39+d213e1d":
version: 0.0.8
resolution: "@helium/modular-governance-idls@npm:0.0.8"
dependencies:
Expand All @@ -252,15 +252,15 @@ __metadata:
languageName: node
linkType: hard

"@helium/nft-proxy-sdk@npm:0.0.8-next.22+7098baa":
version: 0.0.8-next.19
resolution: "@helium/nft-proxy-sdk@npm:0.0.8-next.19"
"@helium/nft-proxy-sdk@npm:0.0.8-next.39+d213e1d":
version: 0.0.8-next.39
resolution: "@helium/nft-proxy-sdk@npm:0.0.8-next.39"
dependencies:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.2.17
"@helium/modular-governance-idls": ^0.0.8-next.22+7098baa
"@helium/modular-governance-idls": ^0.0.8-next.39+d213e1d
"@solana/spl-token": ^0.3.8
checksum: f5c6d9348cfebb8dd2d8fd997c365b5656afa7a6683592e1ed6b0bf04f73f231265a770c96022c930d32c686f57dbf56a0b7790c9d025413c80de30f07f1e587
checksum: bdb16423da6a52581317e191f61db35ae2d4642620ba8c99bc5a454bae052ce89fcd0bb0a45c3625c2d7ab8497e62fbfb5e31b704840ab7a5b177b93f3e6356c
languageName: node
linkType: hard

Expand Down Expand Up @@ -326,7 +326,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.2
"@helium/idls": ^0.9.2
"@helium/nft-proxy-sdk": 0.0.8-next.22+7098baa
"@helium/nft-proxy-sdk": 0.0.8-next.39+d213e1d
"@helium/spl-utils": ^0.9.2
"@metaplex-foundation/mpl-token-metadata": ^2.10.0
"@solana/spl-token": ^0.3.8
Expand Down
2 changes: 1 addition & 1 deletion packages/helium-admin-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@helium/helium-sub-daos-sdk": "^0.9.2",
"@helium/lazy-distributor-sdk": "^0.9.2",
"@helium/mobile-entity-manager-sdk": "^0.9.2",
"@helium/nft-proxy-sdk": "0.0.8-next.22+7098baa",
"@helium/nft-proxy-sdk": "0.0.8-next.39+d213e1d",
"@helium/price-oracle-sdk": "^0.9.2",
"@helium/spl-utils": "^0.9.2",
"@helium/treasury-management-sdk": "^0.9.2",
Expand Down
Loading

0 comments on commit a6c5f31

Please sign in to comment.