Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
first pass implementing wow buy token test
Browse files Browse the repository at this point in the history
  • Loading branch information
stat committed Dec 3, 2024
1 parent 4cc6556 commit 922aac2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function wowBuyToken(
const result = await invocation.wait();
return `Purchased WoW ERC20 memecoin with transaction hash: ${result.getTransaction().getTransactionHash()}`;
} catch (error) {
return `Error buying Zora Wow ERC20 memecoin ${error}`;
return `Error buying Zora Wow ERC20 memecoin: ${error}`;
}
}

Expand Down
73 changes: 71 additions & 2 deletions cdp-agentkit-core/src/tests/defi_wow_buy_token_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { Coinbase, SmartContract, Wallet } from "@coinbase/coinbase-sdk";
import { Coinbase, ContractInvocation, Wallet } from "@coinbase/coinbase-sdk";
import { wowBuyToken, WowBuyTokenInput } from "../actions/cdp/defi/wow/actions/buy_token";
import { getBuyQuote } from "../actions/cdp/defi/wow/utils";
import { getHasGraduated } from "../actions/cdp/defi/wow/uniswap/utils";

jest.mock("../actions/cdp/defi/wow/utils", () => ({
getBuyQuote: jest.fn(),
}));

jest.mock("../actions/cdp/defi/wow/uniswap/utils", () => ({
getHasGraduated: jest.fn(),
}));

const MOCK_CONTRACT_ADDRESS = "0xabcdef123456789";
const MOCK_AMOUNT_ETH_IN_WEI = "100000000000000000";

Expand All @@ -27,4 +35,65 @@ describe("Wow Buy Token Input", () => {
});
});

describe("Wow Buy Token Action", () => {});
describe("Wow Buy Token Action", () => {
const NETWORK_ID = Coinbase.networks.BaseSepolia;
const TRANSACTION_HASH = "0xghijkl987654321";

let mockContractInvocation: jest.Mocked<ContractInvocation>;
let mockWallet: jest.Mocked<Wallet>;

beforeEach(() => {
mockWallet = {
invokeContract: jest.fn(),
getDefaultAddress: jest.fn().mockResolvedValue({
getId: jest.fn().mockReturnValue(TRANSACTION_HASH),
}),
getNetworkId: jest.fn().mockReturnValue(NETWORK_ID),
} as unknown as jest.Mocked<Wallet>;

mockContractInvocation = {
wait: jest.fn().mockResolvedValue({
getTransaction: jest.fn().mockReturnValue({
getTransactionHash: jest.fn().mockReturnValue(TRANSACTION_HASH),
}),
}),
} as unknown as jest.Mocked<ContractInvocation>;

mockWallet.invokeContract.mockResolvedValue(mockContractInvocation);
});

it("should successfully buy a token", async () => {
const args = {
contractAddress: MOCK_CONTRACT_ADDRESS,
amountEthInWei: MOCK_AMOUNT_ETH_IN_WEI,
};

(getHasGraduated as jest.Mock).mockResolvedValue(true);
(getBuyQuote as jest.Mock).mockResolvedValue(1.0);

const response = await wowBuyToken(mockWallet, args);

expect(mockWallet.invokeContract).toHaveBeenCalled();
expect(getBuyQuote).toHaveBeenCalled();
expect(getHasGraduated).toHaveBeenCalled();
expect(response).toContain(
`Purchased WoW ERC20 memecoin with transaction hash: ${TRANSACTION_HASH}`,
);
});

it("should handle errors when buying a token", async () => {
const args = {
contractAddress: MOCK_CONTRACT_ADDRESS,
amountEthInWei: MOCK_AMOUNT_ETH_IN_WEI,
};

const error = new Error("An error has occurred");
mockWallet.invokeContract.mockRejectedValue(error);
(getHasGraduated as jest.Mock).mockResolvedValue(true);

const response = await wowBuyToken(mockWallet, args);

expect(mockWallet.invokeContract).toHaveBeenCalled();
expect(response).toContain(`Error buying Zora Wow ERC20 memecoin: ${error}`);
});
});

0 comments on commit 922aac2

Please sign in to comment.