Skip to content

Commit

Permalink
pick: feat (bouncer): add swapContext to contract swaps (#4917) (#4920)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs authored May 30, 2024
1 parent 14c8926 commit 53276be
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
12 changes: 11 additions & 1 deletion bouncer/shared/contract_swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { getBalance } from './get_balance';
import { CcmDepositMetadata } from '../shared/new_swap';
import { send } from './send';
import { SwapContext, SwapStatus } from './swapping';

const erc20Assets: Asset[] = ['Flip', 'Usdc', 'Usdt', 'ArbUsdc'];

Expand Down Expand Up @@ -80,8 +81,9 @@ export async function performSwapViaContract(
sourceAsset: Asset,
destAsset: Asset,
destAddress: string,
swapTag?: string,
swapTag = '',
messageMetadata?: CcmDepositMetadata,
swapContext?: SwapContext,
): Promise<ContractSwapParams> {
await using api = await getChainflipApi();

Expand Down Expand Up @@ -114,6 +116,7 @@ export async function performSwapViaContract(
wallet,
);
}
swapContext?.updateStatus(swapTag, SwapStatus.ContractApproved);

const oldBalance = await getBalance(destAsset, destAddress);
console.log(`${tag} Old balance: ${oldBalance}`);
Expand All @@ -131,6 +134,8 @@ export async function performSwapViaContract(
wallet,
messageMetadata,
);
swapContext?.updateStatus(swapTag, SwapStatus.ContractExecuted);

await observeEvent('swapping:SwapScheduled', api, (event) => {
if ('Vault' in event.data.origin) {
const sourceAssetMatches = sourceAsset === (event.data.sourceAsset as Asset);
Expand All @@ -141,6 +146,9 @@ export async function performSwapViaContract(
// Otherwise it was a swap scheduled by requesting a deposit address
return false;
});

swapContext?.updateStatus(swapTag, SwapStatus.SwapScheduled);

console.log(`${tag} Successfully observed event: swapping: SwapScheduled`);

const ccmEventEmitted = messageMetadata
Expand All @@ -158,6 +166,7 @@ export async function performSwapViaContract(
ccmEventEmitted,
]);
console.log(`${tag} Swap success! New balance: ${newBalance}!`);
swapContext?.updateStatus(swapTag, SwapStatus.Success);
return {
sourceAsset,
destAsset,
Expand All @@ -166,6 +175,7 @@ export async function performSwapViaContract(
};
} catch (err) {
console.error('err:', err);
swapContext?.updateStatus(swapTag, SwapStatus.Failure);
if (err instanceof Error) {
console.log(err.stack);
}
Expand Down
2 changes: 2 additions & 0 deletions bouncer/shared/perform_swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export async function doPerformSwap(

await swapScheduledHandle;

swapContext?.updateStatus(tag, SwapStatus.SwapScheduled);

if (log) console.log(`${tag} Waiting for balance to update`);

try {
Expand Down
36 changes: 34 additions & 2 deletions bouncer/shared/swapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ enum SolidityType {
export enum SwapStatus {
Initiated,
Funded,
// Contract swap specific statuses
ContractApproved,
ContractExecuted,
SwapScheduled,
Success,
Failure,
}
Expand Down Expand Up @@ -175,7 +179,14 @@ export async function testSwapViaContract(
);

swapContext?.updateStatus(tag, SwapStatus.Initiated);
return performSwapViaContract(sourceAsset, destAsset, destAddress, tag, messageMetadata);
return performSwapViaContract(
sourceAsset,
destAsset,
destAddress,
tag,
messageMetadata,
swapContext,
);
}

export class SwapContext {
Expand All @@ -198,8 +209,29 @@ export class SwapContext {
assert(currentStatus === SwapStatus.Initiated, `Unexpected status transition for ${tag}`);
break;
}
case SwapStatus.ContractApproved: {
assert(currentStatus === SwapStatus.Initiated, `Unexpected status transition for ${tag}`);
break;
}
case SwapStatus.ContractExecuted: {
assert(
currentStatus === SwapStatus.ContractApproved,
`Unexpected status transition for ${tag}`,
);
break;
}
case SwapStatus.SwapScheduled: {
assert(
currentStatus === SwapStatus.ContractExecuted || currentStatus === SwapStatus.Funded,
`Unexpected status transition for ${tag}`,
);
break;
}
case SwapStatus.Success: {
assert(currentStatus === SwapStatus.Funded, `Unexpected status transition for ${tag}`);
assert(
currentStatus === SwapStatus.SwapScheduled,
`Unexpected status transition for ${tag}`,
);
break;
}
default:
Expand Down

0 comments on commit 53276be

Please sign in to comment.