Skip to content

Commit

Permalink
fix dex bindings test
Browse files Browse the repository at this point in the history
  • Loading branch information
jcompagni10 committed May 28, 2024
1 parent 53b2731 commit a24147c
Showing 1 changed file with 127 additions and 62 deletions.
189 changes: 127 additions & 62 deletions src/testcases/run_in_band/dex_bindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ describe('Neutron / dex module bindings', () => {
let neutronChain: CosmosWrapper;
let neutronAccount: WalletWrapper;
let contractAddress: string;
let trancheKeyToWithdraw: string;
let trancheKeyToQuery: string;
let activeTrancheKey: string;
let inactiveTrancheKey: string;

beforeAll(async () => {
testState = new TestStateLocalCosmosTestNet(config);
Expand All @@ -65,8 +65,6 @@ describe('Neutron / dex module bindings', () => {
contractAddress = (
await neutronAccount.instantiateContract(codeId, '{}', 'dex_dev')
)[0]._contract_address;
console.log(contractAddress);

await neutronAccount.msgSend(contractAddress, {
amount: '100000000',
denom: 'untrn',
Expand Down Expand Up @@ -114,8 +112,8 @@ describe('Neutron / dex module bindings', () => {
receiver: contractAddress,
token_a: 'untrn',
token_b: 'uibcusdc',
amounts_a: ['100'], // uint128
amounts_b: ['100'], // uint128
amounts_a: ['1000'], // uint128
amounts_b: ['1000'], // uint128
tick_indexes_a_to_b: [1], // i64
fees: [0], // u64
options: [
Expand Down Expand Up @@ -157,31 +155,33 @@ describe('Neutron / dex module bindings', () => {
// GOOD_TIL_TIME = 4;
// }
test('GOOD_TIL_CANCELLED', async () => {
// Place order deep in orderbook. Doesn't change exisitng liquidity
const res = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
place_limit_order: {
receiver: contractAddress,
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: 1,
amount_in: '10',
tick_index_in_to_out: -2000,
amount_in: '1000000',
order_type: LimitOrderType.GoodTilCancelled,
},
}),
);
expect(res.code).toEqual(0);
});
test('FILL_OR_KILL', async () => {
// Trades through some of LP position at tick 1
const res = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
place_limit_order: {
receiver: contractAddress,
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: 1,
amount_in: '10',
tick_index_in_to_out: 30000,
amount_in: '100',
order_type: LimitOrderType.FillOrKill,
max_amount_out: '100',
},
Expand All @@ -190,61 +190,38 @@ describe('Neutron / dex module bindings', () => {
expect(res.code).toEqual(0);
});
test('IMMEDIATE_OR_CANCEL', async () => {
// Trades through remainder of LP position at tick 1
const res = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
place_limit_order: {
receiver: contractAddress,
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: 1,
amount_in: '10',
tick_index_in_to_out: 20,
amount_in: '1000000',
order_type: LimitOrderType.ImmediateOrCancel,
max_amount_out: '100',
},
}),
);
expect(res.code).toEqual(0);
});
test('JUST_IN_TIME', async () => {
let res = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
place_limit_order: {
receiver: contractAddress,
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: 1,
amount_in: '10',
order_type: LimitOrderType.JustInTime,
},
}),
);
expect(res.code).toEqual(0);
trancheKeyToWithdraw = getEventAttributesFromTx(
{ tx_response: res },
'TickUpdate',
['TrancheKey'],
)[0]['TrancheKey'];
res = await neutronAccount.executeContract(
// Place JIT deep in orderbook
const res = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
place_limit_order: {
receiver: contractAddress,
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: 2,
amount_in: '10',
tick_index_in_to_out: -2000,
amount_in: '1000000',
order_type: LimitOrderType.JustInTime,
},
}),
);
expect(res.code).toEqual(0);
trancheKeyToQuery = getEventAttributesFromTx(
{ tx_response: res },
'TickUpdate',
['TrancheKey'],
)[0]['TrancheKey'];
});
test('GOOD_TIL_TIME', async () => {
const res = await neutronAccount.executeContract(
Expand All @@ -254,8 +231,8 @@ describe('Neutron / dex module bindings', () => {
receiver: contractAddress,
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: 1,
amount_in: '10',
tick_index_in_to_out: -20,
amount_in: '10000000',
expiration_time: Math.ceil(Date.now() / 1000) + 1000,
order_type: LimitOrderType.GoodTilTime,
},
Expand All @@ -272,8 +249,8 @@ describe('Neutron / dex module bindings', () => {
receiver: contractAddress,
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: 1,
amount_in: '10',
tick_index_in_to_out: 20,
amount_in: '10000000',
expiration_time: 1,
order_type: LimitOrderType.GoodTilTime,
},
Expand Down Expand Up @@ -304,29 +281,77 @@ describe('Neutron / dex module bindings', () => {
);
});
});
describe('Withdraw filled lo', () => {
console.log(trancheKeyToWithdraw);
describe('Withdraw filled LO', () => {
test('Withdraw', async () => {
const res = await neutronAccount.executeContract(
// place GTC LO at top of orderbook
const res1 = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
place_limit_order: {
receiver: contractAddress,
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: 200,
amount_in: '1000000',
order_type: LimitOrderType.GoodTilCancelled,
},
}),
);
expect(res1.code).toEqual(0);
activeTrancheKey = getEventAttributesFromTx(
{ tx_response: res1 },
'TickUpdate',
['TrancheKey'],
)[0]['TrancheKey'];
// Trade through some of the GTC order
const res2 = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
place_limit_order: {
receiver: contractAddress,
token_in: 'uibcusdc',
token_out: 'untrn',
tick_index_in_to_out: -10,
amount_in: '100',
order_type: LimitOrderType.ImmediateOrCancel,
},
}),
);
expect(res2.code).toEqual(0);

const res3 = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
withdraw_filled_limit_order: {
tranche_key: trancheKeyToWithdraw,
tranche_key: activeTrancheKey,
},
}),
);
expect(res.code).toEqual(0);
expect(res3.code).toEqual(0);
});
});
describe('cancel lo', () => {
console.log(trancheKeyToWithdraw);
describe('cancel LO', () => {
test('success', async () => {
// Cancel the limit order created above
const res = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
cancel_limit_order: {
tranche_key: activeTrancheKey,
},
}),
);
expect(res.code).toEqual(0);
});

test('cancel failed', async () => {
// Attempt to cancel again fails
await expect(
neutronAccount.executeContract(
contractAddress,
JSON.stringify({
cancel_limit_order: {
tranche_key: trancheKeyToWithdraw,
tranche_key: activeTrancheKey,
},
}),
),
Expand All @@ -335,7 +360,6 @@ describe('Neutron / dex module bindings', () => {
);
});
});

describe('MultiHopSwap', () => {
const denoms: any[] = [];
test('successfull multihops', async () => {
Expand Down Expand Up @@ -420,7 +444,6 @@ describe('Neutron / dex module bindings', () => {
}),
);
expect(res.code).toEqual(0);
console.log(res);
});

test('no route found', async () => {
Expand Down Expand Up @@ -448,6 +471,49 @@ describe('Neutron / dex module bindings', () => {
});
});
describe('DEX queries', () => {
beforeAll(async () => {
// create a new active tranche
const res1 = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
place_limit_order: {
receiver: contractAddress,
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: 200,
amount_in: '1000000',
order_type: LimitOrderType.GoodTilCancelled,
},
}),
);
activeTrancheKey = getEventAttributesFromTx(
{ tx_response: res1 },
'TickUpdate',
['TrancheKey'],
)[0]['TrancheKey'];

// create an expired tranche
const res2 = await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
place_limit_order: {
receiver: contractAddress,
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: -2000,
amount_in: '1000000',
order_type: LimitOrderType.JustInTime,
},
}),
);
inactiveTrancheKey = getEventAttributesFromTx(
{ tx_response: res2 },
'TickUpdate',
['TrancheKey'],
)[0]['TrancheKey'];
// wait a few blocks to make sure JIT order expires
await neutronChain.blockWaiter.waitBlocks(2);
});
test('ParamsQuery', async () => {
await neutronAccount.chain.queryContract<ParamsResponse>(
contractAddress,
Expand All @@ -463,7 +529,7 @@ describe('Neutron / dex module bindings', () => {
{
limit_order_tranche_user: {
address: contractAddress,
tranche_key: trancheKeyToWithdraw,
tranche_key: activeTrancheKey,
},
},
);
Expand Down Expand Up @@ -498,9 +564,9 @@ describe('Neutron / dex module bindings', () => {
{
limit_order_tranche: {
pair_id: 'uibcusdc<>untrn',
tick_index: -2,
tick_index: -200,
token_in: 'untrn',
tranche_key: trancheKeyToQuery,
tranche_key: activeTrancheKey,
},
},
);
Expand All @@ -515,7 +581,7 @@ describe('Neutron / dex module bindings', () => {
pair_id: 'untrn<>notadenom',
tick_index: 1,
token_in: 'untrn',
tranche_key: trancheKeyToWithdraw,
tranche_key: activeTrancheKey,
},
},
),
Expand Down Expand Up @@ -546,7 +612,6 @@ describe('Neutron / dex module bindings', () => {
},
},
);
console.log(resp);
expect(Number(resp.deposits[0].total_shares)).toBeGreaterThan(0);
expect(Number(resp.deposits[0].pool.id)).toEqual(0);

Expand Down Expand Up @@ -582,9 +647,9 @@ describe('Neutron / dex module bindings', () => {
{
inactive_limit_order_tranche: {
pair_id: 'uibcusdc<>untrn',
tick_index: -2,
tick_index: 2000,
token_in: 'untrn',
tranche_key: trancheKeyToQuery,
tranche_key: inactiveTrancheKey,
},
},
);
Expand Down Expand Up @@ -644,7 +709,7 @@ describe('Neutron / dex module bindings', () => {
token_in: 'untrn',
token_out: 'uibcusdc',
tick_index_in_to_out: 1,
amount_in: '10',
amount_in: '100000',
expiration_time: Math.ceil(Date.now() / 1000) + 1000,
order_type: LimitOrderType.GoodTilTime,
},
Expand Down

0 comments on commit a24147c

Please sign in to comment.