Skip to content

Commit

Permalink
fix(platforms): fixed type issues with eth/erc20 possession providers (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianHymer authored Jun 22, 2023
1 parent 8c0248f commit 5493b22
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
6 changes: 3 additions & 3 deletions platforms/src/ETH/Providers-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ export const providers: Provider[] = [
new FirstEthTxnProvider(),
new EthGTEOneTxnProvider(),
new EthErc20PossessionProvider({
threshold: 32,
threshold: "32",
recordAttribute: "ethPossessionsGte",
error: "ETH Possessions >= 32 Provider verify Error",
}),
new EthErc20PossessionProvider({
threshold: 10,
threshold: "10",
recordAttribute: "ethPossessionsGte",
error: "ETH Possessions >= 10 Provider verify Error",
}),
new EthErc20PossessionProvider({
threshold: 1,
threshold: "1",
recordAttribute: "ethPossessionsGte",
error: "ETH Possessions >= 1 Provider verify Error",
}),
Expand Down
28 changes: 11 additions & 17 deletions platforms/src/ETH/Providers/ethErc20Possession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,33 @@ type EthErc20Context = ProviderContext & {
};
};

type Erc20Contract = Contract & {
balanceOf: (address: string) => Promise<BigNumber>;
};

// set the network rpc url based on env
export const RPC_URL = process.env.RPC_URL;

export async function getTokenBalance(
address: string,
tokenContractAddress: string,
decimalNumber: number,
payload: RequestPayload,
context: EthErc20Context
): Promise<BigNumber> {
if (context.ethErc20?.counts?.[tokenContractAddress] === undefined) {
// define a provider using the rpc url
const staticProvider = getRPCProvider(payload);
// load Token contract
const readContract = new Contract(tokenContractAddress, ERC20_ABI, staticProvider);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const tokenBalance: string = await readContract?.balanceOf(address);
const bnTokenBalance: BigNumber = parseUnits(tokenBalance, 0);
const readContract = new Contract(tokenContractAddress, ERC20_ABI, staticProvider) as Erc20Contract;
const tokenBalance = await readContract?.balanceOf(address);

if (!context.ethErc20) {
context.ethErc20 = {};
}
if (!context.ethErc20.counts) {
context.ethErc20.counts = {};
}
context.ethErc20.counts[tokenContractAddress] = bnTokenBalance;
context.ethErc20.counts[tokenContractAddress] = tokenBalance;
}
return context.ethErc20.counts[tokenContractAddress];
}
Expand All @@ -88,7 +89,6 @@ export async function getEthBalance(
// define a provider using the rpc url
const staticProvider = getRPCProvider(payload);
const ethBalance = await staticProvider?.getBalance(address);
// convert a currency unit from wei to ether

if (!context.ethErc20) {
context.ethErc20 = {};
Expand All @@ -101,7 +101,7 @@ export async function getEthBalance(
return context.ethErc20.counts.eth;
}

export type ethErc20PossessionProviderOptions = {
export type EthErc20PossessionProviderOptions = {
threshold: string;
recordAttribute: string;
contractAddress: string;
Expand All @@ -115,7 +115,7 @@ export class EthErc20PossessionProvider implements Provider {
type = "";

// Options can be set here and/or via the constructor
_options: ethErc20PossessionProviderOptions = {
_options: EthErc20PossessionProviderOptions = {
threshold: "1",
recordAttribute: "",
contractAddress: "",
Expand All @@ -124,7 +124,7 @@ export class EthErc20PossessionProvider implements Provider {
};

// construct the provider instance with supplied options
constructor(options: ProviderOptions = {}) {
constructor(options: Partial<EthErc20PossessionProviderOptions> = {}) {
this._options = { ...this._options, ...options };
this.type = `${this._options.recordAttribute}#${this._options.threshold}`;
}
Expand All @@ -137,13 +137,7 @@ export class EthErc20PossessionProvider implements Provider {

try {
if (this._options.contractAddress.length > 0) {
amount = await getTokenBalance(
address,
this._options.contractAddress,
this._options.decimalNumber,
payload,
context
);
amount = await getTokenBalance(address, this._options.contractAddress, payload, context);
} else {
amount = await getEthBalance(address, payload, context);
}
Expand Down
2 changes: 1 addition & 1 deletion platforms/src/ETH/__tests__/ethErc20PossessionEth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jest.mock("@ethersproject/providers", () => {
const MOCK_ADDRESS = "0x738488886dd94725864ae38252a90be1ab7609c7";
const MOCK_ADDRESS_LOWER = MOCK_ADDRESS.toLowerCase();
const MOCK_FAKE_ADDRESS = "FAKE_ADDRESS";
const MOCK_BALANCE_ETH = units.parseUnits("200000000000000000000", 0);
const MOCK_BALANCE_ETH = units.parseUnits("200", 18);

describe("Attempt verification", function () {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions platforms/src/GTC/Providers-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export const ProviderConfig: PlatformGroupSpec[] = [

export const providers: Provider[] = [
new EthErc20PossessionProvider({
threshold: 100,
threshold: "100",
recordAttribute: "gtcPossessionsGte",
contractAddress: "0xde30da39c46104798bb5aa3fe8b9e0e1f348163f",
error: "GTC Possessions >= 100 Provider verify Error",
}),
new EthErc20PossessionProvider({
threshold: 10,
threshold: "10",
recordAttribute: "gtcPossessionsGte",
contractAddress: "0xde30da39c46104798bb5aa3fe8b9e0e1f348163f",
error: "GTC Possessions >= 10 Provider verify Error",
Expand Down
11 changes: 5 additions & 6 deletions platforms/src/GTC/__tests__/ethErc20PossessionGtc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jest.mock("ethers", () => {
const MOCK_ADDRESS = "0x738488886dd94725864ae38252a90be1ab7609c7";
const MOCK_ADDRESS_LOWER = MOCK_ADDRESS.toLowerCase();
const MOCK_FAKE_ADDRESS = "FAKE_ADDRESS";
const MOCK_BALANCE = "200000000000000000000";
const MOCK_BALANCE = units.parseUnits("200", 18);

describe("Attempt verification", function () {
beforeEach(() => {
Expand All @@ -48,7 +48,6 @@ describe("Attempt verification", function () {
expect(mockBalanceOf).toBeCalledWith(MOCK_ADDRESS_LOWER);
// Check that parseUnits is called for threshold and balance
expect(parseUnits).toBeCalledWith("100", 18);
expect(parseUnits).toBeCalledWith(MOCK_BALANCE, 0);
expect(verifiedPayload).toEqual({
valid: true,
record: {
Expand Down Expand Up @@ -112,7 +111,7 @@ describe("Check valid cases for GTC Balances", function () {
});

it("Expected Greater than 10 GTC and GTC Balance is 15", async () => {
mockBalanceOf.mockResolvedValueOnce("15000000000000000000");
mockBalanceOf.mockResolvedValueOnce(units.parseUnits("15", 18));
const gtcPossessions = new EthErc20PossessionProvider({
threshold: "10",
recordAttribute: "gtcPossessionsGte",
Expand All @@ -135,7 +134,7 @@ describe("Check valid cases for GTC Balances", function () {
});
});
it("Expected Greater than 100 GTC and GTC Balance is 150", async () => {
mockBalanceOf.mockResolvedValueOnce("150000000000000000000");
mockBalanceOf.mockResolvedValueOnce(units.parseUnits("150", 18));
const gtcPossessions = new EthErc20PossessionProvider({
threshold: "100",
recordAttribute: "gtcPossessionsGte",
Expand Down Expand Up @@ -165,7 +164,7 @@ describe("Check invalid cases for GTC Balances", function () {
mockBalanceOf.mockResolvedValue(MOCK_BALANCE);
});
it("Expected Greater than 10 GTC and GTC Balance is 7", async () => {
mockBalanceOf.mockResolvedValueOnce("7000000000000000000");
mockBalanceOf.mockResolvedValueOnce(units.parseUnits("7", 18));
const gtcPossessions = new EthErc20PossessionProvider({
threshold: "10",
recordAttribute: "gtcPossessionsGte",
Expand All @@ -185,7 +184,7 @@ describe("Check invalid cases for GTC Balances", function () {
});
});
it("Expected Greater than 100 GTC and GTC Balance is 75", async () => {
mockBalanceOf.mockResolvedValueOnce("75000000000000000000");
mockBalanceOf.mockResolvedValueOnce(units.parseUnits("75", 18));
const gtcPossessions = new EthErc20PossessionProvider({
threshold: "100",
recordAttribute: "gtcPossessionsGte",
Expand Down

0 comments on commit 5493b22

Please sign in to comment.