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 b02d306
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 51 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
127 changes: 96 additions & 31 deletions integration_tests/src/testcases/auto-withdrawer.test.ts
Original file line number Diff line number Diff line change
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 @@ -502,7 +503,7 @@ describe('Auto withdrawer', () => {
expect(coreContractInfo.data.contract_info.label).toBe('drop-staking-core');
const withdrawalTokenContractInfo =
await neutronClient.CosmwasmWasmV1.query.queryContractInfo(

Check failure on line 505 in integration_tests/src/testcases/auto-withdrawer.test.ts

View workflow job for this annotation

GitHub Actions / Actions - integration tests lint

Delete `··`
res.withdrawal_voucher_contract,
res.withdrawal_token_contract,

Check failure on line 506 in integration_tests/src/testcases/auto-withdrawer.test.ts

View workflow job for this annotation

GitHub Actions / Actions - integration tests lint

Replace `············` with `········`
);

Check failure on line 507 in integration_tests/src/testcases/auto-withdrawer.test.ts

View workflow job for this annotation

GitHub Actions / Actions - integration tests lint

Delete `··`
expect(withdrawalTokenContractInfo.data.contract_info.label).toBe(
'drop-staking-withdrawal-token',

Check failure on line 509 in integration_tests/src/testcases/auto-withdrawer.test.ts

View workflow job for this annotation

GitHub Actions / Actions - integration tests lint

Delete `··`
Expand Down Expand Up @@ -568,6 +569,7 @@ 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 +838,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 +882,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 @@ -899,25 +900,31 @@ describe('Auto withdrawer', () => {
});
it('unbond', async () => {
const {
neutronClient,
neutronUserAddress,
autoWithdrawerContractClient,
withdrawalVoucherContractClient,
} = 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 () => {
it('bond with withdrawal denoms', async () => {
const {
neutronUserAddress,
autoWithdrawerContractClient,
withdrawalVoucherContractClient,
} = 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
29 changes: 22 additions & 7 deletions ts-client/lib/contractLib/dropAutoWithdrawer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ export type Uint64 = string;
export type BondArgs = {
with_ld_assets: {};
} | {
with_n_f_t: {
token_id: string;
with_withdrawal_denoms: {
batch_id: Uint128;
};
};
/**
* A human readable address.
*
* In Cosmos, this is typically bech32 encoded. But for multi-chain smart contracts no assumptions should be made other than being UTF-8 encoded and of reasonable length.
*
* This type represents a validated address. It can be created in the following ways 1. Use `Addr::unchecked(input)` 2. Use `let checked: Addr = deps.api.addr_validate(input)?` 3. Use `let checked: Addr = deps.api.addr_humanize(canonical_addr)?` 4. Deserialize from JSON. This must only be done from JSON that was validated before such as a contract's state. `Addr` must not be used in messages sent by the user because this would result in unvalidated instances.
*
* This type is immutable. If you really need to mutate it (Really? Are you sure?), create a mutable copy using `let mut mutable = Addr::to_string()` and operate on that `String` instance.
*/
export type Addr = string;
export interface DropAutoWithdrawerSchema {
responses: BondingsResponse | InstantiateMsg;
query: BondingsArgs;
Expand All @@ -46,8 +56,9 @@ export interface BondingsResponse {
}
export interface BondingResponse {
bonder: string;
bonding_id: string;
deposit: Coin[];
token_id: string;
withdrawal_amount: Uint128;
}
export interface Coin {
amount: Uint128;
Expand All @@ -57,8 +68,9 @@ export interface Coin {
export interface InstantiateMsg {
core_address: string;
ld_token: string;
withdrawal_denom_prefix: string;
withdrawal_manager_address: string;
withdrawal_voucher_address: string;
withdrawal_token_address: string;
}
export interface BondingsArgs {
/**
Expand All @@ -75,16 +87,19 @@ export interface BondingsArgs {
user?: string | null;
}
export interface UnbondArgs {
token_id: string;
batch_id: Uint128;
}
export interface WithdrawArgs {
token_id: string;
amount: Uint128;
batch_id: Uint128;
receiver?: Addr | null;
}
export interface InstantiateMsg1 {
core_address: string;
ld_token: string;
withdrawal_denom_prefix: string;
withdrawal_manager_address: string;
withdrawal_voucher_address: string;
withdrawal_token_address: string;
}
export declare class Client {
private readonly client;
Expand Down
Loading

0 comments on commit b02d306

Please sign in to comment.