Skip to content

Commit

Permalink
test: add test for squid approval tx
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseRFelix committed Jun 6, 2024
1 parent 3eaca84 commit 07e68ba
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions packages/bridge/src/squid/__tests__/squid-bridge-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CacheEntry } from "cachified";
import { LRUCache } from "lru-cache";
// eslint-disable-next-line import/no-extraneous-dependencies
import { rest } from "msw";
import { createPublicClient, http } from "viem";

import { MockAssetLists } from "../../__tests__/mock-asset-lists";
import { server } from "../../__tests__/msw";
Expand All @@ -25,6 +26,7 @@ jest.mock("viem", () => {
http: jest.fn().mockImplementation(() => ({})),
};
});

beforeEach(() => {
server.use(
rest.get("https://api.0xsquid.com/v1/route", (_req, res, ctx) => {
Expand Down Expand Up @@ -263,4 +265,66 @@ describe("SquidBridgeProvider", () => {
})
).rejects.toThrow("toAsset mismatch");
});

it("should return approval transaction data if allowance is less than amount", async () => {
const fromTokenContract = createPublicClient({
transport: http(),
});
(fromTokenContract.readContract as jest.Mock).mockResolvedValueOnce(
BigInt("50")
);

const approvalTx = await provider.getApprovalTx({
fromTokenContract,
tokenAddress: "0xTokenAddress",
isFromAssetNative: false,
fromAmount: "100",
fromAddress: "0xFromAddress",
fromChain: { chainId: "1", chainName: "Ethereum", chainType: "evm" },
targetAddress: "0xTargetAddress",
});

expect(approvalTx).toEqual({
to: "0xTokenAddress",
data: "0xabcdef", // Mocked data from encodeFunctionData
});
});

it("should return undefined if allowance is greater than or equal to amount", async () => {
const fromTokenContract = createPublicClient({
transport: http(),
});
(fromTokenContract.readContract as jest.Mock).mockResolvedValueOnce(
BigInt("150")
);

const approvalTx = await provider.getApprovalTx({
fromTokenContract,
tokenAddress: "0xTokenAddress",
isFromAssetNative: false,
fromAmount: "100",
fromAddress: "0xFromAddress",
fromChain: { chainId: "1", chainName: "Ethereum", chainType: "evm" },
targetAddress: "0xTargetAddress",
});

expect(approvalTx).toBeUndefined();
});

it("should return undefined if the asset is native", async () => {
const fromTokenContract = createPublicClient({
transport: http(),
});
const approvalTx = await provider.getApprovalTx({
fromTokenContract,
tokenAddress: "0xTokenAddress",
isFromAssetNative: true,
fromAmount: "100",
fromAddress: "0xFromAddress",
fromChain: { chainId: "1", chainName: "Ethereum", chainType: "evm" },
targetAddress: "0xTargetAddress",
});

expect(approvalTx).toBeUndefined();
});
});

0 comments on commit 07e68ba

Please sign in to comment.