Skip to content

Commit

Permalink
chore: Add balance check on maker bid (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xShisui authored Apr 4, 2023
1 parent 8fc36a7 commit 7d2493b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/LooksRare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "./utils/calls/transferManager";
import { verifyMakerOrders } from "./utils/calls/orderValidator";
import { encodeParams, getTakerParamsTypes, getMakerParamsTypes } from "./utils/encodeOrderParams";
import { setApprovalForAll, isApprovedForAll, allowance, approve } from "./utils/calls/tokens";
import { setApprovalForAll, isApprovedForAll, allowance, approve, balanceOf } from "./utils/calls/tokens";
import { strategyInfo } from "./utils/calls/strategies";
import {
ErrorMerkleTreeDepth,
Expand Down Expand Up @@ -189,7 +189,7 @@ export class LooksRare {
/**
* Create a maker bid object ready to be signed
* @param CreateMakerInput
* @returns the maker object and isCurrencyApproved
* @returns the maker object, isCurrencyApproved, and isBalanceSufficient
*/
public async createMakerBid({
collection,
Expand All @@ -215,7 +215,8 @@ export class LooksRare {
const spenderAddress = this.addresses.EXCHANGE_V2;

// Use this.provider (MulticallProvider) in order to batch the calls
const [currentAllowance, userBidAskNonce] = await Promise.all([
const [balance, currentAllowance, userBidAskNonce] = await Promise.all([
balanceOf(this.provider, currency, signerAddress),
allowance(this.provider, currency, signerAddress, spenderAddress),
viewUserBidAskNonces(this.provider, this.addresses.EXCHANGE_V2, signerAddress),
]);
Expand All @@ -241,6 +242,7 @@ export class LooksRare {
return {
maker: order,
isCurrencyApproved: BigNumber.from(currentAllowance).gte(price),
isBalanceSufficient: BigNumber.from(balance).gte(price),
};
}

Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/looksrare/createMakerBid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ describe("Create maker bid", () => {
expect(isCurrencyApproved).to.be.true;
});

it("balance checks are false if balance is not sufficient", async () => {
const { isBalanceSufficient } = await lrUser1.createMakerBid({
...baseMakerInput,
price: utils.parseEther("100000"),
});
expect(isBalanceSufficient).to.be.false;
});

it("balance checks are true if balance is sufficient", async () => {
const { isBalanceSufficient } = await lrUser1.createMakerBid(baseMakerInput);
expect(isBalanceSufficient).to.be.true;
});

it("create a simple maker bid with default values", async () => {
const output = await lrUser1.createMakerBid(baseMakerInput);
const makerOrder: Maker = {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface CreateMakerAskOutput {
export interface CreateMakerBidOutput {
maker: Maker;
isCurrencyApproved: boolean;
isBalanceSufficient: boolean;
}

/** Input of the createMakerAsk function */
Expand Down
4 changes: 2 additions & 2 deletions src/utils/calls/nonces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, BigNumber, Overrides, providers, BigNumberish } from "ethers";
import { Contract, BigNumber, Overrides, CallOverrides, providers, BigNumberish } from "ethers";
import { LooksRareProtocol } from "../../typechain/@looksrare/contracts-exchange-v2/contracts/LooksRareProtocol";
import abi from "../../abis/LooksRareProtocol.json";
import { Signer, ContractMethods } from "../../types";
Expand All @@ -7,7 +7,7 @@ export const viewUserBidAskNonces = async (
signerOrProvider: providers.Provider | Signer,
address: string,
account: string,
overrides?: Overrides
overrides?: CallOverrides
): Promise<{ bidNonce: BigNumber; askNonce: BigNumber }> => {
const contract = new Contract(address, abi, signerOrProvider) as LooksRareProtocol;
const nonces = await contract.userBidAskNonces(account, { ...overrides });
Expand Down
4 changes: 2 additions & 2 deletions src/utils/calls/orderValidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, Overrides, providers } from "ethers";
import { Contract, CallOverrides, providers } from "ethers";
import { OrderValidatorV2A } from "../../typechain/@looksrare/contracts-exchange-v2/contracts/helpers/OrderValidatorV2A";
import abi from "../../abis/OrderValidatorV2A.json";
import { Signer, Maker, MerkleTree, OrderValidatorCode } from "../../types";
Expand All @@ -9,7 +9,7 @@ export const verifyMakerOrders = async (
makerOrders: Maker[],
signatures: string[],
merkleTrees: MerkleTree[],
overrides?: Overrides
overrides?: CallOverrides
): Promise<OrderValidatorCode[][]> => {
const contract = new Contract(address, abi, signerOrProvider) as OrderValidatorV2A;
const orders = await contract.checkMultipleMakerOrderValidities(makerOrders, signatures, merkleTrees, {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/calls/strategies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, Overrides, providers } from "ethers";
import { Contract, CallOverrides, providers } from "ethers";
import { LooksRareProtocol } from "../../typechain/@looksrare/contracts-exchange-v2/contracts/LooksRareProtocol";
import abi from "../../abis/LooksRareProtocol.json";
import { Signer, StrategyType, StrategyInfo } from "../../types";
Expand All @@ -7,7 +7,7 @@ export const strategyInfo = async (
signerOrProvider: providers.Provider | Signer,
address: string,
strategyId: StrategyType,
overrides?: Overrides
overrides?: CallOverrides
): Promise<StrategyInfo> => {
const contract = new Contract(address, abi, signerOrProvider) as LooksRareProtocol;
const strategy = await contract.strategyInfo(strategyId, { ...overrides });
Expand Down
20 changes: 15 additions & 5 deletions src/utils/calls/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, providers, Overrides, CallOverrides, BigNumber } from "ethers";
import { Contract, providers, Overrides, CallOverrides, BigNumber, ContractTransaction } from "ethers";
import { ERC721 } from "../../typechain/solmate/src/tokens/ERC721.sol/ERC721";
import { ERC20 } from "../../typechain/solmate/src/tokens/ERC20";
import abiIERC721 from "../../abis/IERC721.json";
Expand All @@ -13,7 +13,7 @@ export const setApprovalForAll = (
operator: string,
approved: boolean,
overrides?: Overrides
) => {
): Promise<ContractTransaction> => {
const contract = new Contract(collection, abiIERC721, signer) as ERC721;
return contract.setApprovalForAll(operator, approved, { ...overrides });
};
Expand All @@ -24,7 +24,7 @@ export const isApprovedForAll = (
account: string,
operator: string,
overrides?: CallOverrides
) => {
): Promise<boolean> => {
const contract = new Contract(collection, abiIERC721, signerOrProvider) as ERC721;
return contract.isApprovedForAll(account, operator, { ...overrides });
};
Expand All @@ -37,7 +37,7 @@ export const allowance = (
account: string,
operator: string,
overrides?: Overrides
) => {
): Promise<BigNumber> => {
const contract = new Contract(currency, abiIERC20, signerOrProvider) as ERC20;
return contract.allowance(account, operator, { ...overrides });
};
Expand All @@ -48,7 +48,17 @@ export const approve = (
operator: string,
amount: BigNumber,
overrides?: Overrides
) => {
): Promise<ContractTransaction> => {
const contract = new Contract(currency, abiIERC20, signer) as ERC20;
return contract.approve(operator, amount, { ...overrides });
};

export const balanceOf = (
signerOrProvider: providers.Provider | Signer,
currency: string,
account: string,
overrides?: CallOverrides
): Promise<BigNumber> => {
const contract = new Contract(currency, abiIERC20, signerOrProvider) as ERC20;
return contract.balanceOf(account, { ...overrides });
};
4 changes: 2 additions & 2 deletions src/utils/calls/transferManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, Overrides, providers } from "ethers";
import { Contract, Overrides, CallOverrides, providers } from "ethers";
import { TransferManager } from "../../typechain/@looksrare/contracts-exchange-v2/contracts/TransferManager";
import abi from "../../abis/TransferManager.json";
import { Signer, ContractMethods, BatchTransferItem } from "../../types";
Expand All @@ -8,7 +8,7 @@ export const hasUserApprovedOperator = async (
address: string,
user: string,
operator: string,
overrides?: Overrides
overrides?: CallOverrides
): Promise<boolean> => {
const contract = new Contract(address, abi, signerOrProvider) as TransferManager;
const hasApproved = await contract.hasUserApprovedOperator(user, operator, { ...overrides });
Expand Down

0 comments on commit 7d2493b

Please sign in to comment.