Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deniszagumennov committed Oct 16, 2024
1 parent 93840ee commit df38ce3
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 106 deletions.
4 changes: 2 additions & 2 deletions contracts/auto-withdrawer/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ pub fn reply(
CORE_UNBOND.remove(deps.storage);
// it is safe to use unwrap() here since this reply is only called on success
let events = reply.result.unwrap().events;
deps.api.debug(&format!("WASMDEBUG: {:?}", events));
reply_core_unbond(deps, sender, deposit, events)
}
id => Err(ContractError::InvalidCoreReplyId { id }),
Expand All @@ -294,10 +295,9 @@ pub fn reply(
fn get_value_from_events(events: Vec<Event>, key: String) -> String {
events
.into_iter()
.filter(|event| event.ty == "wasm")
.filter(|event| event.ty == "wasm-drop-withdrawal-token-execute-mint")
.flat_map(|event| event.attributes)
.find(|attribute| attribute.key == key)
// it is safe to use unwrap here because cw-721 always generates valid events on success
.unwrap()
.value
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/auto-withdrawer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn reply_after_new_bond_with_ld_assets() {
Reply {
id: CORE_UNBOND_REPLY_ID,
result: SubMsgResult::Ok(SubMsgResponse {
events: vec![Event::new("wasm")
events: vec![Event::new("wasm-drop-withdrawal-token-execute-mint")
.add_attribute("denom", "factory/withdrawal_token_contract/drop:unbond:0")
.add_attribute("receiver", "receiver")
.add_attribute("batch_id", "0")
Expand Down Expand Up @@ -298,7 +298,7 @@ fn reply_after_existing_bond_with_ld_assets() {
Reply {
id: CORE_UNBOND_REPLY_ID,
result: SubMsgResult::Ok(SubMsgResponse {
events: vec![Event::new("wasm")
events: vec![Event::new("wasm-drop-withdrawal-token-execute-mint")
.add_attribute("denom", "factory/withdrawal_token_contract/drop:unbond:0")
.add_attribute("receiver", "receiver")
.add_attribute("batch_id", "0")
Expand All @@ -324,7 +324,7 @@ fn reply_after_existing_bond_with_ld_assets() {
Reply {
id: CORE_UNBOND_REPLY_ID,
result: SubMsgResult::Ok(SubMsgResponse {
events: vec![Event::new("wasm")
events: vec![Event::new("wasm-drop-withdrawal-token-execute-mint")
.add_attribute("denom", "factory/withdrawal_token_contract/drop:unbond:0")
.add_attribute("receiver", "receiver")
.add_attribute("batch_id", "0")
Expand Down Expand Up @@ -404,7 +404,7 @@ fn reply_invalid_attribute() {
Reply {
id: CORE_UNBOND_REPLY_ID,
result: SubMsgResult::Ok(SubMsgResponse {
events: vec![Event::new("wasm")
events: vec![Event::new("wasm-drop-withdrawal-token-execute-mint")
.add_attribute("denom", "factory/withdrawal_token_contract/drop:unbond:0")
.add_attribute("receiver", "receiver")
.add_attribute("batch_id", "0")
Expand Down
165 changes: 115 additions & 50 deletions integration_tests/src/testcases/auto-withdrawer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('Auto withdrawer', () => {
puppeteerContractClient?: InstanceType<typeof DropPuppeteerClass>;
tokenContractClient?: InstanceType<typeof DropTokenClass>;
withdrawalTokenContractClient?: InstanceType<
typeof DropWithdrawalTokenClass
typeof DropWithdrawalTokenClass
>;
withdrawalVoucherContractClient?: InstanceType<
typeof DropWithdrawalVoucherClass
Expand Down Expand Up @@ -114,6 +114,7 @@ describe('Auto withdrawer', () => {
exchangeRate?: number;
neutronIBCDenom?: string;
ldDenom?: string;
withdrawalDenom?: (string) => string;
} = { codeIds: {} };

beforeAll(async (t) => {
Expand Down Expand Up @@ -289,11 +290,11 @@ describe('Auto withdrawer', () => {
}
{
const res = await client.upload(
account.address,
fs.readFileSync(
join(__dirname, '../../../artifacts/drop_withdrawal_token.wasm'),
),
1.5,
account.address,
fs.readFileSync(
join(__dirname, '../../../artifacts/drop_withdrawal_token.wasm'),
),
1.5,
);
expect(res.codeId).toBeGreaterThan(0);
context.codeIds.withdrawalToken = res.codeId;
Expand Down Expand Up @@ -501,11 +502,11 @@ describe('Auto withdrawer', () => {
);
expect(coreContractInfo.data.contract_info.label).toBe('drop-staking-core');
const withdrawalTokenContractInfo =
await neutronClient.CosmwasmWasmV1.query.queryContractInfo(
res.withdrawal_voucher_contract,
);
await neutronClient.CosmwasmWasmV1.query.queryContractInfo(
res.withdrawal_token_contract,
);
expect(withdrawalTokenContractInfo.data.contract_info.label).toBe(
'drop-staking-withdrawal-token',
'drop-staking-withdrawal-token',
);
const withdrawalVoucherContractInfo =
await neutronClient.CosmwasmWasmV1.query.queryContractInfo(
Expand All @@ -532,8 +533,8 @@ describe('Auto withdrawer', () => {
new DropCore.Client(context.client, res.core_contract),
);
context.withdrawalTokenContractClient = new DropWithdrawalToken.Client(
context.client,
res.withdrawal_token_contract,
context.client,
res.withdrawal_token_contract,
);
context.withdrawalVoucherContractClient = new DropWithdrawalVoucher.Client(
context.client,
Expand Down Expand Up @@ -568,6 +569,8 @@ describe('Auto withdrawer', () => {
res.token_contract,
);
context.ldDenom = `factory/${res.token_contract}/drop`;
context.withdrawalDenom = (batchId) =>
`factory/${res.withdrawal_token_contract}/drop:unbond:${batchId}`;
});

it('setup ICA for rewards pump', async () => {
Expand Down Expand Up @@ -836,8 +839,6 @@ describe('Auto withdrawer', () => {
core_address: context.coreContractClient.contractAddress,
withdrawal_token_address:
context.withdrawalTokenContractClient.contractAddress,
withdrawal_voucher_address:
context.withdrawalVoucherContractClient.contractAddress,
withdrawal_manager_address:
context.withdrawalManagerContractClient.contractAddress,
ld_token: ldDenom,
Expand Down Expand Up @@ -882,14 +883,15 @@ describe('Auto withdrawer', () => {
expect(bondings).toEqual({
bondings: [
{
bonding_id: `${neutronUserAddress}_0`,
bonder: neutronUserAddress,
deposit: [
{
amount: '50000',
denom: 'untrn',
},
],
token_id: `0_${autoWithdrawerContractClient.contractAddress}_2`,
withdrawal_amount: '20000',
},
],
next_page_key: null,
Expand All @@ -898,26 +900,31 @@ describe('Auto withdrawer', () => {
await checkExchangeRate(context);
});
it('unbond', async () => {
const {
neutronUserAddress,
autoWithdrawerContractClient,
withdrawalVoucherContractClient,
} = context;
const { neutronClient, neutronUserAddress, autoWithdrawerContractClient } =
context;
const res = await autoWithdrawerContractClient.unbond(
neutronUserAddress,
{
token_id: `0_${autoWithdrawerContractClient.contractAddress}_2`,
batch_id: '0',
},
1.6,
undefined,
[],
);
expect(res.transactionHash).toHaveLength(64);

const owner = await withdrawalVoucherContractClient.queryOwnerOf({
token_id: `0_${autoWithdrawerContractClient.contractAddress}_2`,
const balances =
await neutronClient.CosmosBankV1Beta1.query.queryAllBalances(
neutronUserAddress,
);
expect(
balances.data.balances.find(
(one) => one.denom === context.withdrawalDenom('0'),
),
).toEqual({
denom: context.withdrawalDenom('0'),
amount: '520000',
});
expect(owner.owner).toEqual(neutronUserAddress);

const bondings = await autoWithdrawerContractClient.queryBondings({
user: neutronUserAddress,
Expand All @@ -928,37 +935,23 @@ describe('Auto withdrawer', () => {
});
await checkExchangeRate(context);
});
it('bond with NFT', async () => {
const {
neutronUserAddress,
autoWithdrawerContractClient,
withdrawalVoucherContractClient,
} = context;
it('bond with withdrawal denoms', async () => {
const { neutronUserAddress, autoWithdrawerContractClient } = context;

{
const res = await withdrawalVoucherContractClient.approve(
neutronUserAddress,
{
spender: autoWithdrawerContractClient.contractAddress,
token_id: `0_${autoWithdrawerContractClient.contractAddress}_2`,
},
1.6,
undefined,
[],
);
expect(res.transactionHash).toHaveLength(64);
}
{
const res = await autoWithdrawerContractClient.bond(
neutronUserAddress,
{
with_n_f_t: {
token_id: `0_${autoWithdrawerContractClient.contractAddress}_2`,
with_withdrawal_denoms: {
batch_id: '0',
},
},
1.6,
undefined,
[{ amount: '40000', denom: 'untrn' }],
[
{ amount: '20000', denom: context.withdrawalDenom('0') },
{ amount: '40000', denom: 'untrn' },
],
);
expect(res.transactionHash).toHaveLength(64);
}
Expand All @@ -969,14 +962,15 @@ describe('Auto withdrawer', () => {
expect(bondings).toEqual({
bondings: [
{
bonding_id: `${neutronUserAddress}_0`,
bonder: neutronUserAddress,
deposit: [
{
amount: '40000',
denom: 'untrn',
},
],
token_id: `0_${autoWithdrawerContractClient.contractAddress}_2`,
withdrawal_amount: '20000',
},
],
next_page_key: null,
Expand Down Expand Up @@ -1457,14 +1451,14 @@ describe('Auto withdrawer', () => {
return balances.data.balances.length > 0;
}, 200_000);
});
it('withdraw', async () => {
it('withdraw partial amount', async () => {
const {
neutronUserAddress,
neutronClient,
neutronIBCDenom,
autoWithdrawerContractClient,
} = context;
const expectedWithdrawnAmount = 20000;
const expectedWithdrawnAmount = 5000;

const balanceBefore = parseInt(
(
Expand All @@ -1478,7 +1472,8 @@ describe('Auto withdrawer', () => {
const res = await autoWithdrawerContractClient.withdraw(
neutronUserAddress,
{
token_id: `0_${autoWithdrawerContractClient.contractAddress}_2`,
batch_id: '0',
amount: '5000',
},
1.6,
undefined,
Expand All @@ -1504,6 +1499,76 @@ describe('Auto withdrawer', () => {
parseInt(balance.data.balance.amount, 10) - balanceBefore,
).toBeCloseTo(expectedWithdrawnAmount, -1);

const bondings = await autoWithdrawerContractClient.queryBondings({
user: neutronUserAddress,
});
expect(bondings).toEqual({
bondings: [
{
bonding_id: `${neutronUserAddress}_0`,
bonder: neutronUserAddress,
deposit: [
{
amount: '30000',
denom: 'untrn',
},
],
withdrawal_amount: '15000',
},
],
next_page_key: null,
});
await checkExchangeRate(context);
});
it('withdraw full amount', async () => {
const {
neutronUserAddress,
neutronClient,
neutronIBCDenom,
autoWithdrawerContractClient,
} = context;
const expectedWithdrawnAmount = 15000;
const expectedBatchWithdrawnAmount = 20000;

const balanceBefore = parseInt(
(
await neutronClient.CosmosBankV1Beta1.query.queryBalance(
neutronUserAddress,
{ denom: neutronIBCDenom },
)
).data.balance.amount,
);

const res = await autoWithdrawerContractClient.withdraw(
neutronUserAddress,
{
batch_id: '0',
amount: '15000',
},
1.6,
undefined,
[],
);
expect(res.transactionHash).toHaveLength(64);

const withdrawnBatch =
await context.coreContractClient.queryUnbondBatch({
batch_id: '0',
});
expect(parseInt(withdrawnBatch.withdrawn_amount, 10)).toBeCloseTo(
expectedBatchWithdrawnAmount,
-1,
);

const balance =
await neutronClient.CosmosBankV1Beta1.query.queryBalance(
neutronUserAddress,
{ denom: neutronIBCDenom },
);
expect(
parseInt(balance.data.balance.amount, 10) - balanceBefore,
).toBeCloseTo(expectedWithdrawnAmount, -1);

const bondings = await autoWithdrawerContractClient.queryBondings({
user: neutronUserAddress,
});
Expand Down
Loading

0 comments on commit df38ce3

Please sign in to comment.