Skip to content

Commit

Permalink
[HIP-138] Implement HIP-138 (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass authored Dec 13, 2024
1 parent 717b2f6 commit 4172cf4
Show file tree
Hide file tree
Showing 71 changed files with 2,429 additions and 810 deletions.
3 changes: 3 additions & 0 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ url = "https://api.mainnet-beta.solana.com"
[[test.validator.clone]]
address = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s" # token-metadata

[[test.validator.clone]]
address = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" # associated-token-program

[[test.validator.clone]]
address = "BGUMAp9Gq7iTEuizy4pqaxsTyUCBK68MDfK752saRPUY" # bubblegum

Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 59 additions & 76 deletions packages/crons/src/end-epoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,25 +181,6 @@ async function getSolanaUnixTimestamp(connection: Connection): Promise<bigint> {
}
}

if (!daoEpochInfo?.doneIssuingHstPool) {
try {
await sendInstructionsWithPriorityFee(
provider,
[
await heliumSubDaosProgram.methods
.issueHstPoolV0({ epoch })
.accounts({ dao })
.instruction(),
],
{
basePriorityFee: BASE_PRIORITY_FEE,
}
);
} catch (err: any) {
errors.push(`Failed to issue hst pool: ${err}`);
}
}

targetTs = targetTs.add(new BN(EPOCH_LENGTH));
}

Expand Down Expand Up @@ -247,76 +228,78 @@ async function getSolanaUnixTimestamp(connection: Connection): Promise<bigint> {
const hemProgram = await initHem(provider);
const lazyProgram = await initLazy(provider);
const rewardsOracleProgram = await initRewards(provider);
const [lazyDistributor] = lazyDistributorKey(iotMint);
const [keyToAsset] = keyToAssetKey(dao, IOT_OPERATIONS_FUND, "utf8");
const assetId = (await hemProgram.account.keyToAssetV0.fetch(keyToAsset))
.asset;

const [recipient] = recipientKey(lazyDistributor, assetId);
if (!(await provider.connection.getAccountInfo(recipient))) {
const method = lazyProgram.methods.initializeRecipientV0().accounts({
lazyDistributor,
mint: assetId,
});
for (const token of [IOT_MINT, HNT_MINT]) {
const [lazyDistributor] = lazyDistributorKey(token);
const [keyToAsset] = keyToAssetKey(dao, IOT_OPERATIONS_FUND, "utf8");
const assetId = (await hemProgram.account.keyToAssetV0.fetch(keyToAsset))
.asset;

const [recipient] = recipientKey(lazyDistributor, assetId);
if (!(await provider.connection.getAccountInfo(recipient))) {
const method = lazyProgram.methods.initializeRecipientV0().accounts({
lazyDistributor,
mint: assetId,
});

await sendInstructionsWithPriorityFee(
provider,
[await method.instruction()],
{
basePriorityFee: BASE_PRIORITY_FEE,
}
);
}

await sendInstructionsWithPriorityFee(
provider,
[await method.instruction()],
{
basePriorityFee: BASE_PRIORITY_FEE,
}
const rewards = await client.getCurrentRewards(
lazyProgram,
lazyDistributor,
assetId
);
}

const rewards = await client.getCurrentRewards(
lazyProgram,
lazyDistributor,
assetId
);

const pending = await client.getPendingRewards(
lazyProgram,
lazyDistributor,
daoKey(HNT_MINT)[0],
[IOT_OPERATIONS_FUND],
"utf8"
);
const pending = await client.getPendingRewards(
lazyProgram,
lazyDistributor,
daoKey(HNT_MINT)[0],
[IOT_OPERATIONS_FUND],
"utf8"
);

// Avoid claiming too much and tripping the breaker
if (new BN(pending[IOT_OPERATIONS_FUND]).gt(MAX_CLAIM_AMOUNT)) {
rewards[0].currentRewards = new BN(rewards[0].currentRewards)
.sub(new BN(pending[IOT_OPERATIONS_FUND]))
.add(MAX_CLAIM_AMOUNT)
.toString();
}
// Avoid claiming too much and tripping the breaker
if (new BN(pending[IOT_OPERATIONS_FUND]).gt(MAX_CLAIM_AMOUNT)) {
rewards[0].currentRewards = new BN(rewards[0].currentRewards)
.sub(new BN(pending[IOT_OPERATIONS_FUND]))
.add(MAX_CLAIM_AMOUNT)
.toString();
}

const tx = await client.formTransaction({
program: lazyProgram,
rewardsOracleProgram: rewardsOracleProgram,
provider,
rewards,
asset: assetId,
lazyDistributor,
});
const tx = await client.formTransaction({
program: lazyProgram,
rewardsOracleProgram: rewardsOracleProgram,
provider,
rewards,
asset: assetId,
lazyDistributor,
});

const signed = await provider.wallet.signTransaction(tx);
const signed = await provider.wallet.signTransaction(tx);

try {
await sendAndConfirmWithRetry(
provider.connection,
Buffer.from(signed.serialize()),
{ skipPreflight: true },
"confirmed"
);
} catch (err: any) {
errors.push(`Failed to distribute iot op funds: ${err}`);
try {
await sendAndConfirmWithRetry(
provider.connection,
Buffer.from(signed.serialize()),
{ skipPreflight: true },
"confirmed"
);
} catch (err: any) {
errors.push(`Failed to distribute iot op funds: ${err}`);
}
}

// Only do this if that feature has been deployed
if (hemProgram.methods.issueNotEmittedEntityV0) {
console.log("Issuing no_emit");
const noEmitProgram = await initBurn(provider);
const tokens = [MOBILE_MINT, IOT_MINT];
const tokens = [MOBILE_MINT, IOT_MINT, HNT_MINT];
for (const token of tokens) {
const [lazyDistributor] = lazyDistributorKey(token);
const notEmittedEntityKta = keyToAssetKey(dao, NOT_EMITTED, "utf-8")[0];
Expand Down
1 change: 1 addition & 0 deletions packages/crons/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/data-credits-sdk/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
13 changes: 10 additions & 3 deletions packages/distributor-oracle/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,18 +346,22 @@ export async function formBulkTransactions({
assetEndpoint || provider.connection.rpcEndpoint,
assets
);
const willPay = skipOracleSign || (await axios.get(
`${lazyDistributorAcc.oracles[0].url}/will-pay-recipient`
)).data.willPay;
let ixsPerAsset = await Promise.all(
recipientAccs.map(async (recipientAcc, idx) => {
if (!recipientAcc) {
return [
await (
await(
await initializeCompressionRecipient({
program: lazyDistributorProgram,
assetId: assets![idx],
lazyDistributor,
assetEndpoint,
owner: wallet,
payer,
// Temporarily set oracle as the payer to subsidize new HNT wallets.
payer: willPay ? lazyDistributorAcc.oracles[0].oracle : payer,
getAssetFn: () => Promise.resolve(compressionAssetAccs![idx]), // cache result so we don't hit again
getAssetProofFn: assetProofsById
? () =>
Expand Down Expand Up @@ -591,6 +595,9 @@ export async function formTransaction({
const recipientAcc =
await lazyDistributorProgram.account.recipientV0.fetchNullable(recipient);
if (!recipientAcc) {
const willPay = (
await axios.get(`${lazyDistributorAcc.oracles[0].url}/will-pay-recipient`)
).data.willPay;
let initRecipientIx;
if (assetAcc.compression.compressed) {
initRecipientIx = await (
Expand All @@ -600,7 +607,7 @@ export async function formTransaction({
lazyDistributor,
assetEndpoint,
owner: wallet,
payer,
payer: willPay ? lazyDistributorAcc.oracles[0].oracle : payer,
getAssetFn: () => Promise.resolve(assetAcc), // cache result so we don't hit again
getAssetProofFn,
})
Expand Down
24 changes: 24 additions & 0 deletions packages/distributor-oracle/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@ export class OracleServer {
private addRoutes() {
this.app.get("/active-devices", this.getActiveDevicesHandler.bind(this));
this.app.post("/bulk-rewards", this.getAllRewardsHandler.bind(this));
this.app.get(
"/will-pay-recipient",
(
_req: FastifyRequest<{ Body: { entityKeys: string[] } }>,
res: FastifyReply
) => {
res.send({
willPay: process.env.WILL_PAY_RECIPIENT === "true",
});
}
);
this.app.get<{
Querystring: {
assetId?: string;
Expand Down Expand Up @@ -361,6 +372,10 @@ export class OracleServer {
initCompressionRecipientTx.accounts.findIndex(
(x) => x.name === "lazyDistributor"
)!;
const payerIdxInitCompressionRecipient =
initCompressionRecipientTx.accounts.findIndex(
(x) => x.name === "payer"
)!;
const mintIdx = initRecipientTx.accounts.findIndex(
(x) => x.name === "mint"
)!;
Expand Down Expand Up @@ -443,6 +458,15 @@ export class OracleServer {
allAccs[ix.accountKeyIndexes[lazyDistributorIdxInitCompressionRecipient]].toBase58();
const merkleTree =
allAccs[ix.accountKeyIndexes[merkleTreeIdxInitCompressionRecipient]];
const payer =
allAccs[ix.accountKeyIndexes[payerIdxInitCompressionRecipient]].toBase58();

if (process.env.WILL_PAY_RECIPIENT !== "true" && payer === this.oracle.publicKey.toBase58()) {
return {
success: false,
message: "Cannot set this oracle as the payer",
};
}

const index = (decoded.data as any).args.index;
recipientToLazyDistToMint[recipient][lazyDist] = await getLeafAssetId(
Expand Down
1 change: 1 addition & 0 deletions packages/distributor-oracle/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/entity-invalidator/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
12 changes: 4 additions & 8 deletions packages/helium-admin-cli/emissions/hst.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
[
{
"startTime": "2023-04-18T00:00:00Z",
"percent": 32
},
{
"startTime": "2023-08-01T00:00:00Z",
"percent": 31
},
{
"startTime": "2024-08-01T00:00:01Z",
"percent": 30
},
{
"startTime": "2025-08-01T00:00:01Z",
"percent": 0
}
]
Loading

0 comments on commit 4172cf4

Please sign in to comment.