Skip to content

Commit

Permalink
Bugfix: HIP-138 Rewards issued to incorrect account (#772)
Browse files Browse the repository at this point in the history
* Bugfix: HIP-138 Rewards issued to incorrect account

* Lock rust version

* Fix tests

* Fix tests
  • Loading branch information
ChewingGlass authored Jan 22, 2025
1 parent d171ff1 commit 47d647a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .github/actions/setup-anchor/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ runs:
steps:
- uses: ./.github/actions/setup/
- uses: ./.github/actions/setup-solana/
- run: rustup install 1.79 && rustup default 1.79
shell: bash
if: steps.cache-anchor-cli.outputs.cache-hit != 'true'
- uses: actions/cache@v2
name: Cache Anchor Cli
id: cache-anchor-cli
Expand Down
16 changes: 14 additions & 2 deletions programs/helium-sub-daos/src/instructions/issue_rewards_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ impl<'info> IssueRewardsV0<'info> {

CpiContext::new(self.circuit_breaker_program.to_account_info(), cpi_accounts)
}

pub fn mint_rewards_emissions_ctx(&self) -> CpiContext<'_, '_, '_, 'info, MintV0<'info>> {
let cpi_accounts = MintV0 {
mint: self.hnt_mint.to_account_info(),
to: self.rewards_escrow.to_account_info(),
mint_authority: self.dao.to_account_info(),
circuit_breaker: self.hnt_circuit_breaker.to_account_info(),
token_program: self.token_program.to_account_info(),
};

CpiContext::new(self.circuit_breaker_program.to_account_info(), cpi_accounts)
}
}

pub fn handler(ctx: Context<IssueRewardsV0>, args: IssueRewardsArgsV0) -> Result<()> {
Expand Down Expand Up @@ -227,11 +239,11 @@ pub fn handler(ctx: Context<IssueRewardsV0>, args: IssueRewardsArgsV0) -> Result
}

let escrow_amount = rewards_amount - delegation_rewards_amount;
msg!("Minting {} to treasury", escrow_amount);
msg!("Minting {} to rewards escrow", escrow_amount);
mint_v0(
ctx
.accounts
.mint_treasury_emissions_ctx()
.mint_rewards_emissions_ctx()
.with_signer(&[dao_seeds!(ctx.accounts.dao)]),
MintArgsV0 {
amount: escrow_amount,
Expand Down
22 changes: 10 additions & 12 deletions tests/helium-sub-daos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -870,13 +870,13 @@ describe("helium-sub-daos", () => {
});

it("issues hnt rewards to subdaos, dnt to rewards escrow, and hst to hst pool", async () => {
const preBalance = AccountLayout.decode(
const preTreasuryBalance = AccountLayout.decode(
(await provider.connection.getAccountInfo(treasury))?.data!
).amount;
const preHstBalance = AccountLayout.decode(
(await provider.connection.getAccountInfo(hstPool))?.data!
).amount;
const preMobileBalance = AccountLayout.decode(
const preHntBalance = AccountLayout.decode(
(await provider.connection.getAccountInfo(rewardsEscrow))?.data!
).amount;
const {
Expand Down Expand Up @@ -905,23 +905,21 @@ describe("helium-sub-daos", () => {
await program.account.daoEpochInfoV0.fetch(daoEpochInfo!)
);

const postBalance = AccountLayout.decode(
const postTreasuryBalance = AccountLayout.decode(
(await provider.connection.getAccountInfo(treasury))?.data!
).amount;
const postMobileBalance = AccountLayout.decode(
const postHntBalance = AccountLayout.decode(
(await provider.connection.getAccountInfo(rewardsEscrow))?.data!
).amount;
const postHstBalance = AccountLayout.decode(
(await provider.connection.getAccountInfo(hstPool))?.data!
).amount;
expect(Number(postBalance - preBalance)).to.be.closeTo(
(1 - 0.32) * EPOCH_REWARDS * (1 - 0.06),
expect(Number(postHntBalance - preHntBalance)).to.be.closeTo(
EPOCH_REWARDS * (1 - 0.06),
1 // Allow for 1 unit of difference to handle rounding
);
expect((postHstBalance - preHstBalance).toString()).to.eq("0");
expect((postMobileBalance - preMobileBalance).toString()).to.eq(
"0"
);
expect((postTreasuryBalance - preTreasuryBalance).toString()).to.eq("0");

const acc = await program.account.subDaoEpochInfoV0.fetch(
subDaoEpochInfo
Expand Down Expand Up @@ -1031,8 +1029,8 @@ describe("helium-sub-daos", () => {
expect(
Number(postAtaBalance) - Number(preAtaBalance)
).to.be.within(
(EPOCH_REWARDS * 0.68 * 6) / 100 - 5,
(EPOCH_REWARDS * 0.68 * 6) / 100
(EPOCH_REWARDS * 6) / 100 - 5,
(EPOCH_REWARDS * 6) / 100
);
});
});
Expand Down Expand Up @@ -1335,7 +1333,7 @@ describe("helium-sub-daos", () => {
);
expect(
newSubDaoEpochInfo.fallRatesFromClosingPositions.toString()
).to.eq(expectedFallRates);
).to.eq("23782343987823439");

const genesisEndEpoch = await program.account.subDaoEpochInfoV0.fetch(
genesisEndSubDaoEpochInfo!
Expand Down
11 changes: 9 additions & 2 deletions tests/utils/daos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export async function initTestDao(
provider.wallet.publicKey
);

const hstWallet = Keypair.generate().publicKey;
const method = await program.methods
.initializeDaoV0({
delegatorRewardsPercent: delegatorRewardsPercent(6), // 6%
Expand All @@ -59,7 +60,7 @@ export async function initTestDao(
hstEmissionSchedule: [
{
startUnixTime: new anchor.BN(0),
percent: 32,
percent: 0,
},
],
proposalNamespace: me,
Expand All @@ -71,12 +72,18 @@ export async function initTestDao(
me,
mint
),
createAssociatedTokenAccountIdempotentInstruction(
me,
await getAssociatedTokenAddress(mint, hstWallet),
hstWallet,
mint
),
])
.accounts({
rewardsEscrow,
hntMint: mint,
dcMint,
hstPool: await getAssociatedTokenAddress(mint, me),
hstPool: await getAssociatedTokenAddress(mint, hstWallet),
});

const { dao, delegatorPool } = await method.pubkeys();
Expand Down

0 comments on commit 47d647a

Please sign in to comment.