Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/integration-tests/take-1inch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,14 @@ describe('Take with 1inch Integration', () => {
signer,
config: {
subgraphUrl: '',
oneInchRouters: { 1: '0x1111111254EEB25477B68fb85Ed929f73A960582' },
oneInchRouters: { 31337: '0x1111111254EEB25477B68fb85Ed929f73A960582' },
connectorTokens: [],
delayBetweenActions: 1,
},
})
);
expect(liquidations.length).to.equal(1);
expect(liquidations[0].takeStrategy).to.equal(1);
expect(liquidations[0].isTakeable).to.equal(true);

await takeLiquidation({
pool,
Expand All @@ -298,7 +298,7 @@ describe('Take with 1inch Integration', () => {
liquidation: liquidations[0],
config: {
dryRun: false,
oneInchRouters: { 1: '0x1111111254EEB25477B68fb85Ed929f73A960582' },
oneInchRouters: { 31337: '0x1111111254EEB25477B68fb85Ed929f73A960582' },
connectorTokens: [],
keeperTaker: keeperTakerAddress,
delayBetweenActions: 1,
Expand Down Expand Up @@ -371,7 +371,7 @@ describe('Take with 1inch Integration', () => {
signer,
config: {
subgraphUrl: '',
oneInchRouters: { 1: '0x1111111254EEB25477B68fb85Ed929f73A960582' },
oneInchRouters: { 31337: '0x1111111254EEB25477B68fb85Ed929f73A960582' },
connectorTokens: [],
delayBetweenActions: 1,
},
Expand All @@ -395,7 +395,7 @@ describe('Take with 1inch Integration', () => {
liquidation: liquidations[0],
config: {
dryRun: false,
oneInchRouters: { 1: '0x1111111254EEB25477B68fb85Ed929f73A960582' },
oneInchRouters: { 31337: '0x1111111254EEB25477B68fb85Ed929f73A960582' },
connectorTokens: [],
keeperTaker: keeperTakerAddress,
delayBetweenActions: 1,
Expand Down
17 changes: 8 additions & 9 deletions src/take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import subgraph from './subgraph';
import { decimaledToWei, delay, RequireFields, weiToDecimaled } from './utils';
import { KeeperConfig, LiquiditySource, PoolConfig } from './config-types';
import { logger } from './logging';
import { liquidationArbTake } from './transactions';
import { liquidationArbTake, liquidationTakeWithAtomicSwap } from './transactions';
import { DexRouter } from './dex-router';
import { BigNumber, ethers } from 'ethers';
import { convertSwapApiResponseToDetailsBytes } from './1inch';
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function handleTakes({
}
}

interface LiquidationToTake {
export interface LiquidationToTake {
borrower: string;
hpbIndex: number;
collateral: BigNumber; // WAD
Expand Down Expand Up @@ -341,16 +341,15 @@ export async function takeLiquidation({
logger.debug(
`Sending Take Tx - poolAddress: ${pool.poolAddress}, borrower: ${borrower}`
);
const tx = await keeperTaker.takeWithAtomicSwap(
pool.poolAddress,
liquidation.borrower,
liquidation.auctionPrice,
liquidation.collateral,
await liquidationTakeWithAtomicSwap(
keeperTaker,
signer,
pool,
liquidation,
poolConfig.take.liquiditySource,
dexRouter.getRouter(await signer.getChainId())!!,
convertSwapApiResponseToDetailsBytes(swapData.data)
);
await tx.wait();
)
logger.info(
`Take successful - poolAddress: ${pool.poolAddress}, borrower: ${borrower}`
);
Expand Down
44 changes: 42 additions & 2 deletions src/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { FungiblePool, Signer } from '@ajna-finance/sdk';
import { createTransaction, FungiblePool, Signer } from '@ajna-finance/sdk';
import {
removeQuoteToken,
withdrawBonds,
quoteTokenScale,
kick,
bucketTake,
} from '@ajna-finance/sdk/dist/contracts/pool';
import { BigNumber } from 'ethers';
import { BigNumber, BytesLike } from 'ethers';
import { MAX_FENWICK_INDEX, MAX_UINT_256 } from './constants';
import { NonceTracker } from './nonce';
import { Bucket } from '@ajna-finance/sdk/dist/classes/Bucket';
Expand All @@ -15,6 +15,9 @@ import {
approve,
} from '@ajna-finance/sdk/dist/contracts/erc20-pool';
import { Liquidation } from '@ajna-finance/sdk/dist/classes/Liquidation';
import { AjnaKeeperTaker } from '../typechain-types';
import { LiquiditySource } from './config-types';
import { LiquidationToTake } from './take';

export async function poolWithdrawBonds(pool: FungiblePool, signer: Signer) {
const address = await signer.getAddress();
Expand Down Expand Up @@ -150,3 +153,40 @@ export async function liquidationArbTake(
throw error;
}
}

export async function liquidationTakeWithAtomicSwap(
keeperTaker: AjnaKeeperTaker,
signer: Signer,
pool: FungiblePool,
liquidation: LiquidationToTake,
liquiditySource: LiquiditySource,
routerAddress: string,
swapDetails: BytesLike
) {
const address = await signer.getAddress();
try {
const nonce = await NonceTracker.getNonce(signer);
const tx = await createTransaction(
keeperTaker,
{
methodName: 'takeWithAtomicSwap',
args: [
pool.poolAddress,
liquidation.borrower,
liquidation.auctionPrice,
liquidation.collateral,
liquiditySource,
routerAddress,
swapDetails,
],
},
{
nonce: nonce.toString(),
}
);
await tx.verifyAndSubmit();
} catch (error) {
NonceTracker.resetNonce(signer, address);
throw error;
}
}