-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: DOT swap output less than existential deposit (#4062)
* swap (output less than ED of DOT) test to bouncer * fix lint * throw error in case we observe BroadcastFailure * fix lint * Added timeout in case no good/bad event is received * fix: use extrinsic success event for dot egress * fix: increase bouncer timeout * refactor: move out of deposit witnessing * chore: some cosmetic changes --------- Co-authored-by: Jamie Ford <Jamie@chainflip.io> Co-authored-by: kylezs <zsembery.kyle@gmail.com>
- Loading branch information
1 parent
2768d3c
commit abb0f48
Showing
5 changed files
with
176 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
bouncer/tests/swap_less_than_existential_deposit_dot.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#!/usr/bin/env -S pnpm tsx | ||
import assert from 'assert'; | ||
import { getBalance } from '../shared/get_balance'; | ||
import { CcmDepositMetadata } from '../shared/new_swap'; | ||
import { SwapParams, requestNewSwap } from '../shared/perform_swap'; | ||
import { sendDot } from '../shared/send_dot'; | ||
import { sendErc20 } from '../shared/send_erc20'; | ||
import { | ||
newAddress, | ||
getChainflipApi, | ||
observeEvent, | ||
observeSwapScheduled, | ||
observeCcmReceived, | ||
observeBalanceIncrease, | ||
getEthContractAddress, | ||
observeBadEvents, | ||
runWithTimeout, | ||
} from '../shared/utils'; | ||
|
||
// This code is duplicated to allow us to specify a specific amount we want to swap | ||
// and to wait for some specific events | ||
export async function doPerformSwap( | ||
{ sourceAsset, destAsset, destAddress, depositAddress, channelId }: SwapParams, | ||
amount: string, | ||
balanceIncrease: boolean, | ||
tag = '', | ||
messageMetadata?: CcmDepositMetadata, | ||
) { | ||
const oldBalance = await getBalance(destAsset, destAddress); | ||
|
||
console.log(`${tag} Old balance: ${oldBalance}`); | ||
|
||
const swapScheduledHandle = observeSwapScheduled(sourceAsset, destAsset, channelId); | ||
|
||
const ccmEventEmitted = messageMetadata | ||
? observeCcmReceived(sourceAsset, destAsset, destAddress, messageMetadata) | ||
: Promise.resolve(); | ||
|
||
const contractAddress = getEthContractAddress('USDC'); | ||
await sendErc20(depositAddress, contractAddress, amount); | ||
|
||
console.log(`${tag} Funded the address`); | ||
|
||
await swapScheduledHandle; | ||
|
||
console.log(`${tag} Waiting for balance to update`); | ||
|
||
if (!balanceIncrease) { | ||
const api = await getChainflipApi(); | ||
await observeEvent('polkadotBroadcaster:BroadcastSuccess', api); | ||
|
||
const newBalance = await getBalance(destAsset, destAddress); | ||
|
||
assert.strictEqual(newBalance, oldBalance, 'Balance should not have changed'); | ||
console.log(`${tag} Swap success! Balance (Same as before): ${newBalance}!`); | ||
} else { | ||
try { | ||
const [newBalance] = await Promise.all([ | ||
observeBalanceIncrease(destAsset, destAddress, oldBalance), | ||
ccmEventEmitted, | ||
]); | ||
|
||
console.log(`${tag} Swap success! New balance: ${newBalance}!`); | ||
} catch (err) { | ||
throw new Error(`${tag} ${err}`); | ||
} | ||
} | ||
} | ||
|
||
export async function swapLessThanED() { | ||
console.log('=== Testing USDC -> DOT swaps obtaining less than ED ==='); | ||
|
||
let stopObserving = false; | ||
const observingBadEvents = observeBadEvents(':BroadcastAborted', () => stopObserving); | ||
|
||
// The initial price is 10USDC = 1DOT, | ||
// we will swap only 5 USDC and check that the swap is completed successfully | ||
const tag = `USDC -> DOT (less than ED)`; | ||
const address = await newAddress('DOT', 'random seed'); | ||
|
||
console.log('Generated DOT address: ' + address); | ||
const swapParams = await requestNewSwap('USDC', 'DOT', address, tag); | ||
await doPerformSwap(swapParams, '5', false, tag); | ||
|
||
await sendDot(address, '50'); | ||
console.log('Account funded, new balance: ' + (await getBalance('DOT', address))); | ||
|
||
// We will then send some dot to the address and perform another swap with less than ED | ||
const tag2 = `USDC -> DOT (to active account)`; | ||
const swapParams2 = await requestNewSwap('USDC', 'DOT', address, tag2); | ||
await doPerformSwap(swapParams2, '5', true, tag2); | ||
|
||
stopObserving = true; | ||
await observingBadEvents; | ||
|
||
console.log('=== Test complete ==='); | ||
} | ||
|
||
runWithTimeout(swapLessThanED(), 500000) | ||
.then(() => { | ||
process.exit(0); | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
process.exit(-1); | ||
}); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.