Skip to content

Commit

Permalink
Add the withdraw test;
Browse files Browse the repository at this point in the history
Add the getTokenTransferIdOrInitialOperationHash utils method
  • Loading branch information
skubarenko committed Mar 20, 2024
1 parent 667c5fe commit a485de3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
61 changes: 61 additions & 0 deletions integration/tests/withdrawal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,65 @@ describe('Withdrawal', () => {
tezosToken
});
}, withdrawalTimeout);

test('Withdraw FA1.2 token, check the transfer status using events (subscribeToTokenTransfer)', done => {
const amount = 3n;
const [tezosToken, etherlinkToken] = [tokens.tezos.ctez, tokens.etherlink.ctez];
let readyForDone = false;

tokenBridge.addEventListener('tokenTransferCreated', tokenTransfer => {
expectPendingWithdrawal(tokenTransfer, {
amount,
source: testEtherlinkAccountAddress,
receiver: testTezosAccountAddress,
etherlinkToken
});
readyForDone = true;
});

tokenBridge.addEventListener('tokenTransferUpdated', tokenTransfer => {
switch (tokenTransfer.status) {
case BridgeTokenTransferStatus.Created:
expectCreatedWithdrawal(tokenTransfer, {
amount,
source: testEtherlinkAccountAddress,
receiver: testTezosAccountAddress,
etherlinkToken,
});

break;
case BridgeTokenTransferStatus.Sealed: {
expectSealedWithdrawal(tokenTransfer, {
amount,
source: testEtherlinkAccountAddress,
receiver: testTezosAccountAddress,
etherlinkToken
});
tokenBridge.finishWithdraw(tokenTransfer);

break;
}
case BridgeTokenTransferStatus.Finished: {
expectFinishedWithdrawal(tokenTransfer, {
inAmount: amount,
outAmount: amount,
source: testEtherlinkAccountAddress,
receiver: testTezosAccountAddress,
tezosToken,
etherlinkToken
});
if (!readyForDone) {
fail('The tokenTransferCreated event has not been fired.');
}

done();
break;
}
}
});

tokenBridge.startWithdraw(amount, etherlinkToken)
.then(result => tokenBridge.stream.subscribeToOperationTokenTransfers(result.tokenTransfer));

}, withdrawalTimeout);
});
14 changes: 8 additions & 6 deletions src/utils/bridgeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { BridgeTokenTransferKind, type BridgeTokenTransfer, type FinishedBridgeTokenDeposit } from '../bridgeCore';
import { BridgeTokenTransferKind, type BridgeTokenTransfer, type FinishedBridgeTokenDeposit, BridgeTokenTransferStatus } from '../bridgeCore';

const tokenTransferIdSeparator = '_';
const isEtherlinkTransaction = (operationHash: string) => operationHash.startsWith('0x');

export const getInitialOperation = (tokenTransfer: BridgeTokenTransfer) => {
return tokenTransfer.kind === BridgeTokenTransferKind.Deposit
? tokenTransfer.tezosOperation
: tokenTransfer.etherlinkOperation;
};
export const getInitialOperation = (tokenTransfer: BridgeTokenTransfer) => tokenTransfer.kind === BridgeTokenTransferKind.Deposit
? tokenTransfer.tezosOperation
: tokenTransfer.etherlinkOperation;

export const getTokenTransferIdOrInitialOperationHash = (tokenTransfer: BridgeTokenTransfer) => tokenTransfer.status === BridgeTokenTransferStatus.Pending
? getInitialOperation(tokenTransfer).hash
: tokenTransfer.id;

export function convertOperationDataToTokenTransferId(etherlinkOperationHash: string, logIndex: number): string;
export function convertOperationDataToTokenTransferId(tezosOperationHash: string, counter: number, nonce: number | null): string;
Expand Down

0 comments on commit a485de3

Please sign in to comment.