Skip to content

Commit

Permalink
Feat/block tag gov votes (#580)
Browse files Browse the repository at this point in the history
* fix: add optional blockhash

* fix: test

* chore: use options
  • Loading branch information
foodaka authored May 8, 2024
1 parent 9a3f35c commit fa9aa1c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,34 @@ describe('GovernanceService', () => {
expect(spy).toHaveBeenCalled();
expect(power[0]).toEqual(userPowerMock);
});

it('Expects token power obj for each token asked with blocknumber', async () => {
const instance = new AaveGovernanceService(provider, {
GOVERNANCE_ADDRESS,
GOVERNANCE_HELPER_ADDRESS,
});

const spy = jest
.spyOn(IGovernanceV2Helper__factory, 'connect')
.mockReturnValue({
getTokensPower: async () => Promise.resolve([userPowerMock]),
} as unknown as IGovernanceV2Helper);

const power = await instance.getTokensPower(
{
user,
tokens,
},
{
blockTag:
'0xbd04f4b86a8ca7592077f62f1b12e56e5684a69e70fb21b4c7fd47e516db71b2',
},
);

expect(spy).toHaveBeenCalled();
expect(power[0]).toEqual(userPowerMock);
});

it('Expects to fail if gov address not eth address', async () => {
const instance = new AaveGovernanceService(provider, {
GOVERNANCE_ADDRESS: 'asdf',
Expand Down
8 changes: 7 additions & 1 deletion packages/contract-helpers/src/governance-contract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import {
GovDelegateTokensBySig,
} from './types';

export type Options = {
blockTag?: string;
};

export const humanizeProposal = (rawProposal: ProposalRPC): Proposal => {
return {
id: Number(rawProposal.id.toString()),
Expand Down Expand Up @@ -182,13 +186,15 @@ export class AaveGovernanceService
@isEthAddress('user')
@isEthAddressArray('tokens')
{ user, tokens }: GovGetPower,
opts: Options = {},
): Promise<Power[]> {
const helper: IGovernanceV2Helper = IGovernanceV2Helper__factory.connect(
this.aaveGovernanceV2HelperAddress,
this.provider,
);
const blockTag = opts.blockTag ? opts.blockTag : 'latest';

return helper.getTokensPower(user, tokens);
return helper.getTokensPower(user, tokens, { blockTag });
}

@GovValidator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { providers } from 'ethers';
import { isAddress } from 'ethers/lib/utils';
import { Options } from '../governance-contract';

import { WalletBalanceProvider as WalletBalanceProviderContract } from './typechain/WalletBalanceProvider';
import { WalletBalanceProviderFactory } from './typechain/WalletBalanceProviderFactory';

import {
BalanceOfResponse,
BatchBalanceOfResponse,
Expand Down Expand Up @@ -56,6 +59,7 @@ export class WalletBalanceProvider {
public async batchBalanceOf(
users: string[],
tokens: string[],
opts: Options = {},
): Promise<BatchBalanceOfResponse> {
if (!users.every(u => isAddress(u))) {
throw new Error(
Expand All @@ -69,7 +73,9 @@ export class WalletBalanceProvider {
);
}

return this._contract.batchBalanceOf(users, tokens);
const blockTag = opts.blockTag ? opts.blockTag : 'latest';

return this._contract.batchBalanceOf(users, tokens, { blockTag });
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ describe('WalletBalanceProvider', () => {
),
).resolves.not.toThrow();
});

it('should not throw if users and tokens and blockHash are a all valid', async () => {
const instance = createValidInstance();
await expect(
instance.batchBalanceOf(
[mockValidEthereumAddress],
[mockValidEthereumAddress],
{ blockTag: 'latest' },
),
).resolves.not.toThrow();
});
});

describe('getUserWalletBalancesForLendingPoolProvider', () => {
Expand Down

0 comments on commit fa9aa1c

Please sign in to comment.