forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
326 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { describe, it, expect, beforeEach, vi, afterEach } from "vitest"; | ||
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; | ||
import { Account, Chain, Hex } from "viem"; | ||
|
||
import { TransferAction } from "../actions/transfer"; | ||
import { WalletProvider } from "../providers/wallet"; | ||
import { SwapAction, swapAction } from '../actions/swap'; | ||
|
||
// Mock the ICacheManager | ||
const mockCacheManager = { | ||
get: vi.fn().mockResolvedValue(null), | ||
set: vi.fn(), | ||
}; | ||
|
||
describe("Swap Action", () => { | ||
let wp: WalletProvider; | ||
|
||
beforeEach(async () => { | ||
vi.clearAllMocks(); | ||
mockCacheManager.get.mockResolvedValue(null); | ||
|
||
const pk = generatePrivateKey(); | ||
const customChains = prepareChains(); | ||
wp = new WalletProvider(pk, mockCacheManager as any, customChains); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllTimers(); | ||
}); | ||
|
||
describe("Constructor", () => { | ||
it("should initialize with wallet provider", () => { | ||
const ta = new SwapAction(wp); | ||
|
||
expect(ta).toBeDefined(); | ||
}); | ||
}); | ||
describe("Swap", () => { | ||
let ta: SwapAction; | ||
let receiver: Account; | ||
|
||
beforeEach(() => { | ||
ta = new SwapAction(wp); | ||
receiver = privateKeyToAccount(generatePrivateKey()); | ||
}); | ||
|
||
it("swap throws if not enough gas/tokens", async () => { | ||
const ta = new SwapAction(wp); | ||
await expect( | ||
ta.swap({ | ||
chain: "base", | ||
fromToken: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", | ||
toToken: "0x4200000000000000000000000000000000000006", | ||
amount: "100", | ||
slippage: 0.5, | ||
}) | ||
).rejects.toThrow("Execution failed"); | ||
}); | ||
}); | ||
}); | ||
|
||
const prepareChains = () => { | ||
const customChains: Record<string, Chain> = {}; | ||
const chainNames = ["base"]; | ||
chainNames.forEach( | ||
(chain) => | ||
(customChains[chain] = WalletProvider.genChainFromName(chain)) | ||
); | ||
|
||
return customChains; | ||
}; |
Oops, something went wrong.