Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit Tests. #43

Open
wants to merge 10 commits into
base: monter82/mc-fix
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' --ignore-path .gitignore",
"serve": "mm-snap serve",
"start": "mm-snap watch"
"start": "mm-snap watch",
"test": "mocha --colors -r ts-node/register \"test/**/*.test.ts\""
},
"husky": {
"hooks": {
Expand All @@ -50,8 +51,12 @@
"@metamask/eslint-config-typescript": "^10.0.0",
"@metamask/key-tree": "^9.0.0",
"@metamask/snaps-cli": "^0.32.2",
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@types/sinon": "^10.0.16",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"chai": "^4.3.7",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.26.0",
Expand All @@ -62,11 +67,15 @@
"ethers": "^5.6.8",
"husky": "^8.0.3",
"io-ts": "^2.2.20",
"jest": "^29.6.4",
"lint-staged": "^14.0.0",
"mocha": "^10.2.0",
"prettier": "^2.2.1",
"prettier-plugin-packagejson": "^2.2.11",
"rimraf": "^3.0.2",
"sinon": "^15.2.0",
"through2": "^4.0.2",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
},
"packageManager": "yarn@3.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/celo-org/gas-snap.git"
},
"source": {
"shasum": "PYoA3v5TRGUAvOYlwxpFXD+44Pt23MSpeu+gIUpy+O0=",
"shasum": "aCQ8/7Ak/aoj7mQrPfFvI2xUW3nR/o1P+qoUNBDpifs=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
1 change: 1 addition & 0 deletions packages/snap/src/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
}
}

/**

Check failure on line 117 in packages/snap/src/currency.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (18.x)

Missing JSDoc @param "Contract" declaration
* Finds the optimal gas currency to send a transaction with based on user balances.
* This may differ from the feeCurrency specified in the transaction body.
*
Expand All @@ -130,6 +130,7 @@
export async function getOptimalFeeCurrency(
tx: CeloTransactionRequest,
wallet: CeloWallet,
Contract: any,

Check failure on line 133 in packages/snap/src/currency.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (18.x)

'Contract' is already declared in the upper scope on line 7 column 21
): Promise<string | undefined> {
const registry = new Contract(REGISTRY_ADDRESS, REGISTRY_ABI, wallet);
const sortedOraclesAddress = await registry.getAddressForString(
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OnRpcRequestHandler } from '@metamask/snaps-types';
import { CeloProvider, CeloWallet } from '@celo-tools/celo-ethers-wrapper';
import { constants } from 'ethers';
import { constants, Contract } from 'ethers';
import { getNetworkConfig } from './utils/network';
import { RequestParamsSchema } from './utils/types';
import { handleNumber } from './utils/utils';
Expand Down Expand Up @@ -76,7 +76,7 @@
throw new Error(REJECTION_MESSAGE);
}

tx.feeCurrency ??= await getOptimalFeeCurrency(tx, wallet);
tx.feeCurrency ??= await getOptimalFeeCurrency(tx, wallet, Contract);
const suggestedFeeCurrency = getFeeCurrencyNameFromAddress(
tx.feeCurrency,
network.name,
Expand All @@ -91,10 +91,10 @@
],
placeholder: `cusd, ceur, creal, celo`,
});
if(overrideFeeCurrency === '') {

Check failure on line 94 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (18.x)

Insert `·`
overrideFeeCurrency = suggestedFeeCurrency

Check failure on line 95 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (18.x)

Insert `;`
}
if ( VALID_CURRENCIES.includes(overrideFeeCurrency.toLowerCase()) ) {

Check failure on line 97 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (18.x)

Expected blank line before this statement

Check failure on line 97 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (18.x)

Replace `·VALID_CURRENCIES.includes(overrideFeeCurrency.toLowerCase())·` with `VALID_CURRENCIES.includes(overrideFeeCurrency.toLowerCase())`
tx.feeCurrency = getFeeCurrencyAddressFromName(
overrideFeeCurrency.toLowerCase(),
network.name,
Expand All @@ -117,7 +117,7 @@
`${network.explorer}/tx/${txReceipt?.transactionHash}`,
],
});
return txReceipt?.transactionHash;

Check failure on line 120 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (18.x)

Async arrow function expected no return value
} catch (e) {
const message = (e as Error).message.includes('insufficient funds')
? INSUFFICIENT_FUNDS_MESSAGE
Expand Down
45 changes: 45 additions & 0 deletions packages/snap/test/constants.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { BigNumber } from 'ethers';
import { SortedOraclesRates } from '../src/utils/types';

Check failure on line 2 in packages/snap/test/constants.test.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (18.x)

'SortedOraclesRates' is defined but never used
// currency.test.ts
export const REGISTRY_ADDRESS = '0x000000000000000000000000000000000000ce10';
export const CELO_MAINNET = 'mainnet';
export const CELO_ALFAJORES = 'alfajores';
export const cusdAddressAlfajores =
'0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1';
export const ceurAddressAlfajores =
'0x10c892A6EC43a53E45D0B916B4b7D383B1b78C0F';
export const crealAddressAlfajores =
'0xE4D517785D091D3c54818832dB6094bcc2744545';
export const cusdAddressMainnet = '0x765DE816845861e75A25fCA122bb6898B8B1282a';
export const ceurAddressMainnet = '0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73';
export const crealAddressMainnet = '0xe8537a3d056DA446677B9E9d6c5dB704EaAb4787';
export const CELO = 'celo';
export const CUSD = 'cusd';
export const CEUR = 'ceur';
export const CREAL = 'creal';
export const sortedOraclesAddress =
'0xEb70DF9C0eCe284A5b7CCb88B9bBdC4d1E453d37';
export const feeCurrencyWhitelistAddress =
'0xd9251192b3aE36CCE34D5D8471815d283B51C706';

export const walletAddressWithMoreUsd =
'0x49e3aEA43AaD61d7a47efb2f993Bd9f58db5E60F';
export const walletAddressWithMoreEur =
'0xa6DE49480dE155413f7D802513f409A5b23165F8';
export const walletAddressWithMoreReal =
'0xD355fd821D13F70B6e313D3DE313D05524fc3703';
export const walletPKWithMoreUsd = // Private keys for these addresses
'6820cfce5e3bcfd15e723d1252e05f429969a49dc50e80350c5f67b514871ad3';
export const walletPKWithMoreEur =
'a3a79bfc80b94d1f48f7af82767b4eed8580abe8d7f149460b63807fc217630f';
export const walletPKWithMoreReal =
'd68208fc514505c201221d6d1e03145784046fa07496821a38d64fe3f68ddfd5';

export const addressRatePair: Record<string, BigNumber[]> = {
// 1:1 mapping with usd
[cusdAddressAlfajores]: [BigNumber.from(100000), BigNumber.from(100)],
// 1:1.18 euro to usd
[ceurAddressAlfajores]: [BigNumber.from(118000), BigNumber.from(100)],
// 1:0.19 real to usd
[crealAddressAlfajores]: [BigNumber.from(19000), BigNumber.from(100)],
};
72 changes: 72 additions & 0 deletions packages/snap/test/contract.mock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { BigNumber } from 'ethers';
import {
sortedOraclesAddress,
feeCurrencyWhitelistAddress,
addressRatePair,
cusdAddressAlfajores,
ceurAddressAlfajores,
crealAddressAlfajores,
walletAddressWithMoreEur,

Check failure on line 9 in packages/snap/test/contract.mock.test.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (18.x)

'walletAddressWithMoreEur' is defined but never used
walletAddressWithMoreReal,
walletAddressWithMoreUsd,
} from './constants.test';

export interface Contract {

Check failure on line 14 in packages/snap/test/contract.mock.test.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (18.x)

Use a `type` instead of an `interface`
address: string;
defaultBalance: BigNumber;
getAddressForString: (name: string) => string;
getWhitelist: () => string[];
medianRate: (address: string) => BigNumber[] | undefined;
balanceOf: (address: string) => BigNumber | undefined;
}

export class ContractMock implements Contract {
public address: string;
public defaultBalance: BigNumber = BigNumber.from(100000);

constructor(tokenAddress: string, ABI: any, wallet: any) {
this.address = tokenAddress;
}

public getAddressForString(name: string): string {
if (name === 'SortedOracles') {
return sortedOraclesAddress;
} else {
return feeCurrencyWhitelistAddress;
}
}

public getWhitelist(): string[] {
return Object.keys(addressRatePair);
}

public medianRate(address: string): BigNumber[] | undefined {
const rate = addressRatePair[address];
if (rate !== undefined) {
return rate;
}

return undefined;
}

public balanceOf(walletAddress: string): BigNumber | undefined {
switch (this.address) {
case cusdAddressAlfajores:
if (walletAddress === walletAddressWithMoreUsd) {
return BigNumber.from(250000); // Make usd balance bigger than euro so that when multiplied with rates >>>
} else {
return this.defaultBalance;
}
case crealAddressAlfajores:
if (walletAddress === walletAddressWithMoreReal) {
return BigNumber.from(700000); // Make real balance bigger than euro & usd so when multiplied with rates >>>
} else {
return this.defaultBalance;
}
case ceurAddressAlfajores:
return this.defaultBalance; // Since euro already larger than usd & real when used with rates
default:
return undefined;
}
}
}
Loading
Loading