Skip to content

Commit

Permalink
Add basic LST integration tests (sei-protocol#1814)
Browse files Browse the repository at this point in the history
* Add test setup

* Add tests for bonding and unbonding

* Add steak tests to test script

* Fix wasm path

* Combine bonding and unbonding tests

* Fix json strings
  • Loading branch information
besated authored Aug 13, 2024
1 parent fc36b39 commit a901610
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 10 deletions.
1 change: 1 addition & 0 deletions integration_test/dapp_tests/dapp_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ npm ci
npx hardhat compile

npx hardhat test --network seilocal uniswap/uniswapTest.js
npx hardhat test --network seilocal steak/SteakTests.js
28 changes: 19 additions & 9 deletions integration_test/dapp_tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion integration_test/dapp_tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@uniswap/v3-periphery": "^1.4.4",
"chai": "^4.2.0",
"ethers": "^5.7.2",
"hardhat": "^2.22.6"
"hardhat": "^2.22.6",
"uuid": "^10.0.0"
}
}
165 changes: 165 additions & 0 deletions integration_test/dapp_tests/steak/SteakTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
const {
storeWasm,
deployErc20PointerForCw20,
ABI,
getEvmAddress,
fundSeiAddress,
associateKey,
} = require("../../../contracts/test/lib.js");
const {
getValidators,
instantiateHubContract,
bond,
addAccount,
queryTokenBalance,
unbond,
transferTokens,
} = require("./utils.js");

const { expect } = require("chai");
const { v4: uuidv4 } = require("uuid");

const STEAK_HUB_WASM =
"../integration_test/dapp_tests/steak/contracts/steak_hub.wasm";
const STEAK_TOKEN_WASM =
"../integration_test/dapp_tests/steak/contracts/steak_token.wasm";

describe("Steak", async function () {
let owner;
let hubAddress;
let tokenAddress;
let tokenPointer;

async function setupAccount(baseName, associate = true) {
const uniqueName = `${baseName}-${uuidv4()}`;
const account = await addAccount(uniqueName);
await fundSeiAddress(account.address);
if (associate) {
await associateKey(account.address);
}
return account;
}

async function deployContracts(ownerAddress) {
// Store CW20 token wasm
const tokenCodeId = await storeWasm(STEAK_TOKEN_WASM);

// Store Hub contract
const hubCodeId = await storeWasm(STEAK_HUB_WASM);

// Instantiate hub and token contracts
const validators = await getValidators();
const instantiateMsg = {
cw20_code_id: parseInt(tokenCodeId),
owner: ownerAddress,
name: "Steak",
symbol: "STEAK",
decimals: 6,
epoch_period: 259200,
unbond_period: 1814400,
validators: validators.slice(0, 3),
};
const contractAddresses = await instantiateHubContract(
hubCodeId,
ownerAddress,
instantiateMsg,
"steakhub"
);

// Deploy pointer for token contract
const pointerAddr = await deployErc20PointerForCw20(
hre.ethers.provider,
contractAddresses.tokenContract
);
const tokenPointer = new hre.ethers.Contract(
pointerAddr,
ABI.ERC20,
hre.ethers.provider
);

return {
hubAddress: contractAddresses.hubContract,
tokenAddress: contractAddresses.tokenContract,
tokenPointer,
};
}

async function testBonding(address, amount) {
const initialBalance = await queryTokenBalance(tokenAddress, address);
expect(initialBalance).to.equal("0");

await bond(hubAddress, address, amount);
const tokenBalance = await queryTokenBalance(tokenAddress, address);
expect(tokenBalance).to.equal(`${amount}`);
}

async function testUnbonding(address, amount) {
const initialBalance = await queryTokenBalance(tokenAddress, address);
const response = await unbond(hubAddress, tokenAddress, address, amount);
expect(response.code).to.equal(0);

// Balance should be updated
const tokenBalance = await queryTokenBalance(tokenAddress, address);
expect(tokenBalance).to.equal(`${Number(initialBalance) - amount}`);
}

before(async function () {
// Set up the owner account
owner = await setupAccount("steak-owner");

// Store and deploy contracts
({ hubAddress, tokenAddress, tokenPointer } = await deployContracts(
owner.address
));
});

describe("Bonding and unbonding", async function () {
it("Associated account should be able to bond and unbond", async function () {
const amount = 1000000;
await testBonding(owner.address, amount);

// Verify that address is associated
const evmAddress = await getEvmAddress(owner.address);
expect(evmAddress).to.not.be.empty;

// Check pointer balance
const pointerBalance = await tokenPointer.balanceOf(evmAddress);
expect(pointerBalance).to.equal(`${amount}`);

await testUnbonding(owner.address, 500000);
});

it("Unassociated account should be able to bond", async function () {
const unassociatedAccount = await setupAccount("unassociated", false);
// Verify that account is not associated yet
const initialEvmAddress = await getEvmAddress(
unassociatedAccount.address
);
expect(initialEvmAddress).to.be.empty;

await testBonding(unassociatedAccount.address, 1000000);

// Account should now be associated
const evmAddress = await getEvmAddress(unassociatedAccount.address);
expect(evmAddress).to.not.be.empty;

// Send tokens to a new unassociated account
const newUnassociatedAccount = await setupAccount("unassociated", false);
const transferAmount = 500000;
await transferTokens(
tokenAddress,
unassociatedAccount.address,
newUnassociatedAccount.address,
transferAmount
);
const tokenBalance = await queryTokenBalance(
tokenAddress,
newUnassociatedAccount.address
);
expect(tokenBalance).to.equal(`${transferAmount}`);

// Try unbonding on unassociated account
await testUnbonding(newUnassociatedAccount.address, transferAmount / 2);
});
});
});
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit a901610

Please sign in to comment.