Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/contract-helpers/src/commons/gasStation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BigNumber, providers } from 'ethers';
import { estimateGas, estimateGasByNetwork } from './gasStation';
import { ChainId } from './types';

describe('gasStation', () => {
const provider: providers.Provider = new providers.JsonRpcProvider();
Expand Down Expand Up @@ -41,5 +42,33 @@ describe('gasStation', () => {
const gas = await estimateGasByNetwork(tx, provider, 10);
expect(gas).toEqual(BigNumber.from(110));
});
it('Expects to return 230000 for zksync when connected with contract address', async () => {
jest
.spyOn(provider, 'getNetwork')
.mockImplementationOnce(async () =>
Promise.resolve({ chainId: ChainId.zksync, name: 'zksync' }),
);

jest
.spyOn(provider, 'getCode')
.mockImplementationOnce(async () => Promise.resolve('0x1234'));

const gas = await estimateGasByNetwork({ from: '0x123abc' }, provider);
expect(gas).toEqual(BigNumber.from(230000));
});
it('Expects to return default for zksync when connected with EOA', async () => {
jest
.spyOn(provider, 'getNetwork')
.mockImplementationOnce(async () =>
Promise.resolve({ chainId: ChainId.zksync, name: 'zksync' }),
);

jest
.spyOn(provider, 'getCode')
.mockImplementationOnce(async () => Promise.resolve('0x'));

const gas = await estimateGasByNetwork({ from: '0x123abc' }, provider);
expect(gas).toEqual(BigNumber.from(130));
});
});
});
16 changes: 15 additions & 1 deletion packages/contract-helpers/src/commons/gasStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,22 @@ export const estimateGasByNetwork = async (
provider: providers.Provider,
gasSurplus?: number,
): Promise<BigNumber> => {
const estimatedGas = await provider.estimateGas(tx);
const providerNework: providers.Network = await provider.getNetwork();
if (providerNework.chainId === ChainId.zksync && tx.from) {
/**
* Trying to estimate gas on zkSync when connected with a smart contract address
* will fail. In that case, we'll just return a default value for all transactions.
*
* See here for more details: https://github.com/zkSync-Community-Hub/zksync-developers/discussions/144
*/
const data = await provider.getCode(tx.from);
console.log(data);
if (data !== '0x') {
return BigNumber.from(230000);
}
}

const estimatedGas = await provider.estimateGas(tx);

if (providerNework.chainId === ChainId.polygon) {
return estimatedGas.add(estimatedGas.mul(POLYGON_SURPLUS).div(100));
Expand Down
20 changes: 20 additions & 0 deletions packages/contract-helpers/src/commons/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,26 @@ export enum ProtocolAction {
batchMetaDelegate = 'batchMetaDelegate',
updateRepresentatives = 'updateRepresentatives',
migrateABPT = 'migrateABPT',
umbrellaStake = 'umbrellaStake',
umbrellaStakeWithPermit = 'umbrellaStakeWithPermit',
umbrellaStakeWithATokens = 'umbrellaStakeWithATokens',
umbrellaStakeWithATokensWithPermit = 'umbrellaStakeWithATokensWithPermit',
umbrellaRedeem = 'umbrellaRedeem',
umbrellaRedeemATokens = 'umbrellaRedeemATokens',
umbrellaStakeTokenCooldown = 'umbrellaStakeTokenCooldown',
umbrellaStakeTokenDeposit = 'umbrellaStakeTokenDeposit',
umbrellaStakeTokenDepositWithPermit = 'umbrellaStakeTokenDepositWithPermit',
umbrellaStakeTokenRedeem = 'umbrellaStakeTokenRedeem',
umbrellaClaimAllRewards = 'umbrellaClaimAllRewards',
umbrellaClaimSelectedRewards = 'umbrellaClaimSelectedRewards',
umbrellaStakeGatewayStake = 'umbrellaStakeGatewayStake',
umbrellaStakeGatewayStakeWithPermit = 'umbrellaStakeGatewayStakeWithPermit',
umbrellaStakeGatewayStakeATokens = 'umbrellaStakeGatewayStakeATokens',
umbrellaStakeGatewayStakeATokensWithPermit = 'umbrellaStakeGatewayStakeATokensWithPermit',
umbrellaStakeGatewayStakeNativeTokens = 'umbrellaStakeGatewayStakeNativeTokens',
umbrellaStakeGatewayRedeem = 'umbrellaStakeGatewayRedeem',
umbrellaStakeGatewayRedeemATokens = 'umbrellaStakeGatewayRedeemATokens',
umbrellaStakeGatewayRedeemNativeTokens = 'umbrellaStakeGatewayRedeemNativeTokens',
}

export enum GovernanceVote {
Expand Down
36 changes: 35 additions & 1 deletion packages/contract-helpers/src/commons/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumber } from 'ethers';
import { BigNumber, Wallet } from 'ethers';
import { isAddress } from 'ethers/lib/utils';
import { transactionType } from './types';
import {
API_ETH_MOCK_ADDRESS,
Expand All @@ -9,6 +10,8 @@ import {
getTxValue,
augustusToAmountOffsetFromCalldata,
convertPopulatedTx,
makePair,
generateEIP712PermitMock,
} from './utils';

describe('Utils', () => {
Expand Down Expand Up @@ -130,4 +133,35 @@ describe('Utils', () => {
expect(convertedTx.value).toEqual(BigNumber.from('0'));
});
});

describe('makePair', () => {
it('Generates a valid pair', () => {
const { address, privateKey } = makePair('test_id');
expect(isAddress(address)).toBeTruthy();
expect(privateKey).toHaveLength(66);
const wallet = new Wallet(privateKey);
expect(wallet.address).toEqual(address);
});
it('Generate always the same pair with same id', () => {
const { address: address1, privateKey: privateKey1 } =
makePair('test_id');
const { address: address2, privateKey: privateKey2 } =
makePair('test_id');
expect(address1).toEqual(address2);
expect(privateKey1).toEqual(privateKey2);
});
});

describe('generateEIP712PermitMock', () => {
it('Generates valid EIP712 Permit values', () => {
const { value } = generateEIP712PermitMock('0x0', '0x1', '100', '1');
expect(value).toEqual({
owner: '0x0',
spender: '0x1',
value: '100',
nonce: '0',
deadline: '1',
});
});
});
});
124 changes: 123 additions & 1 deletion packages/contract-helpers/src/commons/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigNumber as BigNumberJs } from 'bignumber.js';
import { BigNumber, constants, PopulatedTransaction } from 'ethers';
import { BigNumber, constants, PopulatedTransaction, utils } from 'ethers';
import {
GasRecommendationType,
ProtocolAction,
Expand Down Expand Up @@ -150,6 +150,86 @@ export const gasLimitRecommendations: GasRecommendationType = {
limit: '750000',
recommended: '750000',
},
[ProtocolAction.umbrellaStake]: {
limit: '400000',
recommended: '400000',
},
[ProtocolAction.umbrellaStakeWithPermit]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeWithATokens]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeWithATokensWithPermit]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaRedeem]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaRedeemATokens]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeTokenCooldown]: {
limit: '60000',
recommended: '60000',
},
[ProtocolAction.umbrellaStakeTokenDeposit]: {
limit: '200000',
recommended: '200000',
},
[ProtocolAction.umbrellaStakeTokenDepositWithPermit]: {
limit: '300000',
recommended: '300000',
},
[ProtocolAction.umbrellaStakeTokenRedeem]: {
limit: '200000',
recommended: '200000',
},
[ProtocolAction.umbrellaClaimAllRewards]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaClaimSelectedRewards]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeGatewayStake]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeGatewayStakeWithPermit]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeGatewayStakeATokens]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeGatewayStakeATokensWithPermit]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeGatewayStakeNativeTokens]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeGatewayRedeem]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeGatewayRedeemATokens]: {
limit: '310000',
recommended: '310000',
},
[ProtocolAction.umbrellaStakeGatewayRedeemNativeTokens]: {
limit: '310000',
recommended: '310000',
},
};

export const mintAmountsPerToken: Record<string, string> = {
Expand Down Expand Up @@ -232,3 +312,45 @@ export const convertPopulatedTx = (
value: tx.value ? BigNumber.from(tx.value) : BigNumber.from('0'),
};
};

export const makePair = (id: string) => {
const privateKey = utils.id(id);
const address = utils.computeAddress(privateKey);
return { privateKey, address };
};

export const generateEIP712PermitMock = (
owner: string,
spender: string,
amount: string,
deadline: string,
) => {
const domain = {
name: 'Mocked token',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
};
const types = {
Permit: [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
],
};
const value = {
owner,
spender,
value: amount,
nonce: '0',
deadline,
};

return { domain, types, value };
};

export function expectToBeDefined<T>(value: T | undefined): asserts value is T {
expect(value).toBeDefined();
}
1 change: 1 addition & 0 deletions packages/contract-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export * from './governance-v3/aave-token-v3';
export * from './governance-v3/payloads-data-helper';
export * from './governance-v3/delegate-helper';
export * from './abpt-migration';
export * from './umbrella';

// commons
export * from './commons/types';
Expand Down
68 changes: 68 additions & 0 deletions packages/contract-helpers/src/umbrella/RewardsDistributor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ProtocolAction } from '../commons/types';
import {
expectToBeDefined,
gasLimitRecommendations,
makePair,
} from '../commons/utils';
import { IRewardsDistributor__factory } from './typechain/IRewardsDistributor__factory';
import { RewardsDistributorService } from './';

describe('Umbrella Rewards distributor', () => {
const { address: STAKE_TOKEN } = makePair('STAKE_TOKEN');
const { address: ALICE } = makePair('ALICE');

const REWARD_1 = makePair('REWARD_1').address;
const REWARD_2 = makePair('REWARD_2').address;

const stakeTokenService = new RewardsDistributorService(STAKE_TOKEN);
const stakeTokenInterface = IRewardsDistributor__factory.createInterface();
describe('claimAllRewards', () => {
it('should properly create the transaction', () => {
const tx = stakeTokenService.claimAllRewards({
stakeToken: STAKE_TOKEN,
sender: ALICE,
});
expect(tx.from).toEqual(ALICE);
expect(tx.to).toEqual(STAKE_TOKEN);
expectToBeDefined(tx.gasLimit);
expectToBeDefined(tx.data);
expect(tx.gasLimit.toString()).toEqual(
gasLimitRecommendations[ProtocolAction.umbrellaClaimAllRewards]
.recommended,
);
const decoded = stakeTokenInterface.decodeFunctionData(
'claimAllRewards',
tx.data,
);
expect(decoded).toHaveLength(2);
expect(decoded[0]).toEqual(STAKE_TOKEN);
expect(decoded[1]).toEqual(ALICE);
});
});
describe('claimSelectedRewards', () => {
it('should properly create the transaction', () => {
const rewards = [REWARD_1, REWARD_2];
const tx = stakeTokenService.claimSelectedRewards({
stakeToken: STAKE_TOKEN,
rewards,
sender: ALICE,
});
expect(tx.from).toEqual(ALICE);
expect(tx.to).toEqual(STAKE_TOKEN);
expectToBeDefined(tx.gasLimit);
expectToBeDefined(tx.data);
expect(tx.gasLimit.toString()).toEqual(
gasLimitRecommendations[ProtocolAction.umbrellaClaimSelectedRewards]
.recommended,
);
const decoded = stakeTokenInterface.decodeFunctionData(
'claimSelectedRewards',
tx.data,
);
expect(decoded).toHaveLength(3);
expect(decoded[0]).toEqual(STAKE_TOKEN);
expect(decoded[1]).toEqual(rewards);
expect(decoded[2]).toEqual(ALICE);
});
});
});
Loading
Loading