Skip to content

Commit

Permalink
Partially fix Hatom tests after chain simulator update.
Browse files Browse the repository at this point in the history
  • Loading branch information
raress96 committed Mar 28, 2024
1 parent 523fb43 commit 5e52900
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 205 deletions.
143 changes: 54 additions & 89 deletions xsuite-chain-simulator-temp/tests/hatom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterAll, assert, beforeAll, test } from 'vitest';
import { assertAccount, d, e, CSContract, CSWallet, CSWorld } from 'xsuite';
import { DummySigner } from 'xsuite/dist/world/signer';
import {
ADMIN_ADDRESS,
ADMIN_ADDRESS, delegationContractDataDecoder, esdtTokenPaymentDecoder, extractContract,
getHatomContractState,
MAINNET_LIQUID_STAKING_CONTRACT_ADDRESS,
SYSTEM_DELEGATION_MANAGER_ADDRESS,
Expand Down Expand Up @@ -32,7 +32,7 @@ beforeAll(async () => {
balance: '1255000000000000000000', // 1255 EGLD
});
alice = await world.createWallet({
balance: '8001000000000000000000000', // 8,001,000 EGLD
balance: '4001000000000000000000000', // 4,001,000 EGLD
});
bob = await world.createWallet({
balance: '10000000000000000000', // 10 EGLD
Expand Down Expand Up @@ -64,43 +64,6 @@ afterAll(async () => {
await world.terminate();
}, 60_000);

const extractContract = (tx): CSContract => {
const events = tx.tx.logs.events;

for (const event: any of events) {
if (event.identifier !== 'SCDeploy') {
continue;
}

const address = Buffer.from(event.topics[0], 'base64');

return world.newContract(address);
}
};

const esdtTokenPaymentDecoder = d.Tuple({
token_identifier: d.Str(),
token_nonce: d.U64(),
amount: d.U(),
});

const delegationContractDataDecoder = d.Tuple({
contract: d.Addr(),
total_value_locked: d.U(),
cap: d.Option(d.U()),
nr_nodes: d.U64(),
apr: d.U(),
service_fee: d.U(),
delegation_score: d.U(),
pending_to_delegate: d.U(),
total_delegated: d.U(),
pending_to_undelegate: d.U(),
total_undelegated: d.U(),
total_withdrawable: d.U(),
outdated: d.Bool(),
blacklisted: d.Bool(),
});

const deployDelegationProvider = async () => {
const tx = await address.callContract({
callee: systemDelegationContract,
Expand All @@ -112,7 +75,7 @@ const deployDelegationProvider = async () => {
e.U(3745), // service fee
],
});
const stakingProviderDelegationContract = extractContract(tx);
const stakingProviderDelegationContract = extractContract(tx, world);
console.log('Deployed new delegation contract', stakingProviderDelegationContract.toString());

const initialWallets = await world.getInitialWallets();
Expand Down Expand Up @@ -175,16 +138,16 @@ const setupLiquidStaking = async (stakingProviderDelegationContract: CSContract)
stakingProviderDelegationContract,
e.U(3750000000000000000000n), // total value locked (3750 EGLD = 2500 EGLD initial + 1250 EGLD from delegate creation)
e.U64(1), // nb of nodes,
e.U(2000), // high apr
e.U(100), // low service fee so this delegation contract is selected instead of some other
e.U(1100), // high apr
e.U(200), // low service fee so this delegation contract is selected instead of some other
],
});
console.log('Whitelisted delegation contract');

let tx = await alice.callContract({
callee: liquidStakingContract,
funcName: 'delegate',
value: 1000000000000000000000000n, // 1,000,000 EGLD,
value: 4000000000000000000000000n, // 4,000,000 EGLD,
gasLimit: 45_000_000,
});
const segldReceived = esdtTokenPaymentDecoder.topDecode(tx.returnData[0]);
Expand All @@ -206,7 +169,7 @@ const setupLiquidStaking = async (stakingProviderDelegationContract: CSContract)
});
let delegationContractData = delegationContractDataDecoder.topDecode(result.returnData[0]);
console.log('Delegation contract data: ', delegationContractData);
assert(delegationContractData.pending_to_delegate === 1000000000000000000000000n);
assert(delegationContractData.pending_to_delegate === 4000000000000000000000000n);

await admin.callContract({
callee: liquidStakingContract,
Expand All @@ -222,7 +185,7 @@ const setupLiquidStaking = async (stakingProviderDelegationContract: CSContract)
callee: stakingProviderDelegationContract,
funcName: 'getTotalActiveStake',
});
assert(d.U().topDecode(result.returnData[0]) === 1003750000000000000000000n); // staked increased by 1,000,000 EGLD
assert(d.U().topDecode(result.returnData[0]) === 4003750000000000000000000n); // staked increased by 4,000,000 EGLD

console.log('Moving forward 3 epochs...');

Expand All @@ -245,12 +208,15 @@ const setupLiquidStaking = async (stakingProviderDelegationContract: CSContract)
// TODO: Balance assertions are not reliable currently
// assert(rewardsReserve === 12360185739713390599n); // 12.360185739713390599 EGLD added as rewards

await admin.callContract({
callee: liquidStakingContract,
funcName: 'delegateRewards',
gasLimit: 45_000_000,
});
console.log('Delegate rewards back to staking provider');
// TODO: This breaks the delegation contract and unDelegate will fail with `signalError - Delegation contract not available`
// This is most probably because of the Hatom algorithm configuration being changed and another provider might have been
// selected if we run this.
// await admin.callContract({
// callee: liquidStakingContract,
// funcName: 'delegateRewards',
// gasLimit: 45_000_000,
// });
// console.log('Delegate rewards back to staking provider');

await world.generateBlocks(3);

Expand Down Expand Up @@ -285,18 +251,18 @@ const setupLiquidStaking = async (stakingProviderDelegationContract: CSContract)
console.log('Delegation contract data: ', delegationContractData);

// TODO: There is no `pending_to_undelegate` amount for this delegation contract so we can not do this
// await admin.callContract({
// callee: liquidStakingContract,
// funcName: 'unDelegatePendingAmount',
// gasLimit: 45_000_000,
// funcArgs: [
// stakingProviderDelegationContract,
// ],
// });
// console.log('Undelegate pending amount successfully. Moving forward 10 epochs...');
//
await admin.callContract({
callee: liquidStakingContract,
funcName: 'unDelegatePendingAmount',
gasLimit: 45_000_000,
funcArgs: [
stakingProviderDelegationContract,
],
});
console.log('Undelegate pending amount successfully. Moving forward 10 epochs...');

// Move forward 10 epochs
// await world.generateBlocks(20 * 10);
await world.generateBlocks(20 * 10);

await alice.callContract({
callee: liquidStakingContract,
Expand All @@ -309,8 +275,7 @@ const setupLiquidStaking = async (stakingProviderDelegationContract: CSContract)
nonce: Number(undelegateNftReceived.token_nonce),
},
],
}).assertFail({ code: 'signalError', message: 'The unbond period has not ended' });
// }).assertFail({ code: 'signalError', message: 'Too much EGLD amount' });
}).assertFail({ code: 'signalError', message: 'Too much EGLD amount' });

await alice.callContract({
callee: liquidStakingContract,
Expand All @@ -322,31 +287,31 @@ const setupLiquidStaking = async (stakingProviderDelegationContract: CSContract)
});
console.log('Withdraw from staking provider successfully')

// tx = await alice.callContract({
// callee: liquidStakingContract,
// funcName: 'withdraw',
// gasLimit: 45_000_000,
// esdts: [
// {
// id: undelegateNftReceived.token_identifier,
// amount: undelegateNftReceived.amount,
// nonce: Number(undelegateNftReceived.token_nonce),
// },
// ],
// });
// const receivedEgldAmount = d.U().topDecode(tx.returnData[0]);
// console.log('Withdraw EGLD successfully. Received EGLD amount: ', receivedEgldAmount);
// // assert(receivedEgldAmount === 400000902905460064767128n) // ~400,000.93 EGLD received back
//
// const balance = await alice.getAccountBalance();
// assert(balance >= receivedEgldAmount);
//
// result = await world.query({
// callee: stakingProviderDelegationContract,
// funcName: 'getTotalActiveStake',
// });
// console.log('Remaining active stake for staking provider: ', d.U().topDecode(result.returnData[0]));
// // assert(d.U().topDecode(result.returnData[0]) === 3611261457280279648623471n); // stake still remaining
tx = await alice.callContract({
callee: liquidStakingContract,
funcName: 'withdraw',
gasLimit: 45_000_000,
esdts: [
{
id: undelegateNftReceived.token_identifier,
amount: undelegateNftReceived.amount,
nonce: Number(undelegateNftReceived.token_nonce),
},
],
});
const receivedEgldAmount = d.U().topDecode(tx.returnData[0]);
console.log('Withdraw EGLD successfully. Received EGLD amount: ', receivedEgldAmount);
// assert(receivedEgldAmount === 400000902905460064767128n) // ~400,000.93 EGLD received back

const balance = await alice.getAccountBalance();
assert(balance >= receivedEgldAmount);

result = await world.query({
callee: stakingProviderDelegationContract,
funcName: 'getTotalActiveStake',
});
console.log('Remaining active stake for staking provider: ', d.U().topDecode(result.returnData[0]));
// assert(d.U().topDecode(result.returnData[0]) === 3611261457280279648623471n); // stake still remaining
};

test('Test', async () => {
Expand Down
46 changes: 5 additions & 41 deletions xsuite-chain-simulator-temp/tests/hatomConcurrency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assertAccount, d, e, Proxy, CSContract, CSWallet, CSWorld, Tx } from 'x
import { mainnetPublicProxyUrl } from 'xsuite/dist/interact/envChain';
import { DummySigner } from 'xsuite/dist/world/signer';
import {
ADMIN_ADDRESS,
ADMIN_ADDRESS, delegationContractDataDecoder, esdtTokenPaymentDecoder, extractContract,
getHatomContractState,
MAINNET_LIQUID_STAKING_CONTRACT_ADDRESS,
SYSTEM_DELEGATION_MANAGER_ADDRESS,
Expand Down Expand Up @@ -65,43 +65,6 @@ afterAll(async () => {
await world.terminate();
}, 60_000);

const extractContract = (tx): CSContract => {
const events = tx.tx.logs.events;

for (const event: any of events) {
if (event.identifier !== 'SCDeploy') {
continue;
}

const address = Buffer.from(event.topics[0], 'base64');

return world.newContract(address);
}
};

const esdtTokenPaymentDecoder = d.Tuple({
token_identifier: d.Str(),
token_nonce: d.U64(),
amount: d.U(),
});

const delegationContractDataDecoder = d.Tuple({
contract: d.Addr(),
total_value_locked: d.U(),
cap: d.Option(d.U()),
nr_nodes: d.U64(),
apr: d.U(),
service_fee: d.U(),
delegation_score: d.U(),
pending_to_delegate: d.U(),
total_delegated: d.U(),
pending_to_undelegate: d.U(),
total_undelegated: d.U(),
total_withdrawable: d.U(),
outdated: d.Bool(),
blacklisted: d.Bool(),
});

const deployDelegationProvider = async () => {
const tx = await address.callContract({
callee: systemDelegationContract,
Expand All @@ -113,7 +76,7 @@ const deployDelegationProvider = async () => {
e.U(3745), // service fee
],
});
const stakingProviderDelegationContract = extractContract(tx);
const stakingProviderDelegationContract = extractContract(tx, world);
console.log('Deployed new delegation contract', stakingProviderDelegationContract.toString());

const initialWallets = await world.getInitialWallets();
Expand Down Expand Up @@ -247,7 +210,6 @@ const setupLiquidStakingDelegateUndelegate = async (stakingProviderDelegationCon
delegationContractData = delegationContractDataDecoder.topDecode(result.returnData[0]);
console.log('Delegation contract data: ', delegationContractData);

// TODO: There is no `pending_to_undelegate` amount for this delegation contract so we can not do this
await admin.callContract({
callee: liquidStakingContract,
funcName: 'unDelegatePendingAmount',
Expand Down Expand Up @@ -295,7 +257,7 @@ const setupLiquidStakingDelegateUndelegate = async (stakingProviderDelegationCon
await world.generateBlocks(1); // Async call from `withdrawFrom` is finished; `withdraw` failed

console.log('Reaching unreliable part, if it fails after here, try and re-run the test');
// This is NOT that reliable, sometimes the transaction is found in the Chain Simulator, sometimes not...
// TODO: This is NOT that reliable, sometimes the transaction is found in the Chain Simulator, sometimes not...
await new Promise((r) => setTimeout(r, 500));
result = await world.proxy.getTx(txHash, { withResults: true });

Expand All @@ -315,6 +277,8 @@ const setupLiquidStakingDelegateUndelegate = async (stakingProviderDelegationCon

assert(signalErrorEvent);
} else {
console.log(result);

console.log('Withdraw transaction is still pending even though we waiting a while... Assertions were NOT done.');
}

Expand Down
Loading

0 comments on commit 5e52900

Please sign in to comment.