Skip to content

Commit

Permalink
rework allowance method
Browse files Browse the repository at this point in the history
  • Loading branch information
H34D committed Mar 24, 2023
1 parent f5c6f4f commit 172373e
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/account/get-balances.ts
@@ -1,4 +1,4 @@
import { PaymentMethod } from "../contracts";
import { ERC20__factory, PaymentMethod } from "../contracts";
import {
SoulboundCreditScore,
SoulboundGreen,
Expand All @@ -7,7 +7,6 @@ import {
} from "@masa-finance/masa-contracts-identity";
import Masa from "../masa";
import { constants, utils } from "ethers";
import { ERC20__factory } from "../contracts/stubs/ERC20__factory";

type BalanceTypes = "Native" | PaymentMethod | SBTContractNames;

Expand Down
9 changes: 5 additions & 4 deletions src/contracts/addresses.ts
Expand Up @@ -12,8 +12,8 @@ import {
} from "./networks";
import { NetworkName } from "../interface";

const erc20 = ["MASA", "WETH", "G$", "USDC", "cUSD"] as const;
export type ERC20 = (typeof erc20)[number];
const erc20Currencies = ["MASA", "WETH", "G$", "USDC", "cUSD"] as const;
export type ERC20Currencies = (typeof erc20Currencies)[number];

const nativeCurrencies = ["ETH", "CELO"] as const;
export type NativeCurrencies = (typeof nativeCurrencies)[number];
Expand All @@ -25,9 +25,10 @@ export const isNativeCurrency = (

export const isERC20Currency = (
paymentMethod: unknown
): paymentMethod is ERC20 => erc20.includes(paymentMethod as ERC20);
): paymentMethod is ERC20Currencies =>
erc20Currencies.includes(paymentMethod as ERC20Currencies);

export type PaymentMethod = NativeCurrencies | ERC20;
export type PaymentMethod = NativeCurrencies | ERC20Currencies;

export type Tokens = Partial<{ [key in PaymentMethod]: string }>;

Expand Down
1 change: 1 addition & 0 deletions src/contracts/index.ts
Expand Up @@ -2,3 +2,4 @@ export * from "./addresses";
export * from "./load-sbt-contract";
export * from "./masa-contracts";
export * from "./load-Identity-contracts";
export * from "./stubs";
23 changes: 12 additions & 11 deletions src/contracts/masa-contracts.ts
@@ -1,7 +1,5 @@
import { BigNumber } from "@ethersproject/bignumber";
import {
IERC20,
IERC20__factory,
MasaSBTSelfSovereign,
MasaSBTSelfSovereign__factory,
SoulLinker,
Expand All @@ -18,6 +16,8 @@ import {
} from "ethers";
import {
ContractFactory,
ERC20,
ERC20__factory,
isERC20Currency,
isNativeCurrency,
loadIdentityContracts,
Expand All @@ -27,7 +27,6 @@ import {
import { IIdentityContracts, MasaConfig } from "../interface";
import { verifyTypedData } from "ethers/lib/utils";
import { generateSignatureDomain, signTypedData } from "../utils";
import { ERC20__factory } from "./stubs/ERC20__factory";

export class MasaContracts {
public instances: IIdentityContracts;
Expand All @@ -53,26 +52,28 @@ export class MasaContracts {
price: BigNumber
): Promise<ContractReceipt | undefined> => {
if (isERC20Currency(paymentMethod)) {
const contract = IERC20__factory.connect(
const contract: ERC20 = ERC20__factory.connect(
paymentAddress,
this.masaConfig.wallet
);

if (
(await contract.allowance(
const allowanceRequired = price.sub(
await contract.allowance(
// owner
await this.masaConfig.wallet.getAddress(),
// spender
this.instances.SoulStoreContract.address
)) < price
) {
)
);

if (allowanceRequired.gt(0)) {
const tx: ContractTransaction = await contract
.connect(this.masaConfig.wallet)
.approve(
// spender
this.instances.SoulStoreContract.address,
// amount
price
allowanceRequired
);

return await tx.wait();
Expand Down Expand Up @@ -108,7 +109,7 @@ export class MasaContracts {
formatPrice: async (paymentAddress: string, price: BigNumber) => {
let decimals = 18;
if (paymentAddress !== constants.AddressZero) {
const contract = ERC20__factory.connect(
const contract: ERC20 = ERC20__factory.connect(
paymentAddress,
this.masaConfig.wallet
);
Expand Down Expand Up @@ -415,7 +416,7 @@ export class MasaContracts {
);

if (isERC20Currency(paymentMethod)) {
const paymentToken: IERC20 = IERC20__factory.connect(
const paymentToken: ERC20 = ERC20__factory.connect(
paymentAddress,
this.masaConfig.wallet
);
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/contracts/stubs/ERC20/index.ts
@@ -0,0 +1,2 @@
export * from "./ERC20";
export * from "./ERC20__factory";
1 change: 1 addition & 0 deletions src/contracts/stubs/index.ts
@@ -0,0 +1 @@
export * from "./ERC20";

0 comments on commit 172373e

Please sign in to comment.