Skip to content

Commit

Permalink
standardize, add back receipt overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Jan 4, 2024
1 parent f4c18d1 commit fd76875
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
55 changes: 31 additions & 24 deletions connect/src/protocols/cctpTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,11 @@ export class CircleTransfer<N extends Network = Network>

const originTxs = xfer.txids.filter((txid) => txid.chain === xfer.transfer.from.chain);
if (originTxs.length > 0) {
receipt = { ...receipt, state: TransferState.SourceInitiated, originTxs } as Partial<
SourceInitiatedTransferReceipt<CircleTransferProtocol, Chain, Chain>
>;
receipt = {
...receipt,
state: TransferState.SourceInitiated,
originTxs,
} as SourceInitiatedTransferReceipt<CircleTransferProtocol>;
}

const att = xfer.attestations?.filter((a) => isWormholeMessageId(a.id)) ?? [];
Expand All @@ -502,7 +504,7 @@ export class CircleTransfer<N extends Network = Network>
...receipt,
state: TransferState.Attested,
attestation: attestation,
} as AttestedTransferReceipt<CircleTransferProtocol, Chain, Chain>;
} as AttestedTransferReceipt<CircleTransferProtocol>;
}

const destinationTxs = xfer.txids.filter((txid) => txid.chain === xfer.transfer.to.chain);
Expand All @@ -511,7 +513,7 @@ export class CircleTransfer<N extends Network = Network>
...receipt,
state: TransferState.DestinationInitiated,
destinationTxs,
} as CompletedTransferReceipt<CircleTransferProtocol, Chain, Chain>;
} as CompletedTransferReceipt<CircleTransferProtocol>;
}

return receipt as TransferReceipt<CircleTransferProtocol>;
Expand Down Expand Up @@ -543,11 +545,12 @@ export class CircleTransfer<N extends Network = Network>

const initTx = receipt.originTxs[receipt.originTxs.length - 1]!;
const xfermsg = await CircleTransfer.getTransferMessage(_fromChain, initTx.txid);
yield {
receipt = {
...receipt,
attestation: { id: xfermsg },
state: TransferState.SourceFinalized,
} as SourceFinalizedTransferReceipt<CircleTransferProtocol, SC, DC>;
yield receipt;
}

if (isSourceFinalized(receipt)) {
Expand All @@ -564,48 +567,52 @@ export class CircleTransfer<N extends Network = Network>
receipt.attestation.id as WormholeMessageId,
leftover(start, timeout),
);
yield {
receipt = {
...receipt,
attestation: { id: receipt.attestation.id, attestation: vaa },
state: TransferState.Attested,
} as AttestedTransferReceipt<CircleTransferProtocol, SC, DC>;
yield receipt;
}
}
}

if (isAttested(receipt)) {
// First try to grab the tx status from the API
// Note: this requires a subsequent async step on the backend
// to have the dest txid populated, so it may be delayed by some time
if (isAttested(receipt) || isSourceFinalized(receipt)) {
if (!receipt.attestation) throw "Invalid state transition";

// First try to grab the tx status from the API
// Note: this requires a subsequent async step on the backend
// to have the dest txid populated, so it may be delayed by some time
const txStatus = await wh.getTransactionStatus(
receipt.attestation.id as WormholeMessageId,
leftover(start, timeout),
);

if (txStatus && txStatus.globalTx?.destinationTx?.txHash) {
const { chainId, txHash } = txStatus.globalTx.destinationTx;
yield {
receipt = {
...receipt,
destinationTxs: [{ chain: toChain(chainId) as DC, txid: txHash }],
state: TransferState.DestinationFinalized,
} as CompletedTransferReceipt<CircleTransferProtocol, SC, DC>;
yield receipt;
}
}

// Fall back to asking the destination chain if this VAA has been redeemed
// assuming we have the full attestation
if (isAttested(receipt)) {
yield {
// Fall back to asking the destination chain if this VAA has been redeemed
// assuming we have the full attestation
if (isAttested(receipt)) {
const isComplete = await CircleTransfer.isTransferComplete(
_toChain,
receipt.attestation.attestation,
);
if (isComplete) {
receipt = {
...receipt,
state: (await CircleTransfer.isTransferComplete(
_toChain,
receipt.attestation.attestation,
))
? TransferState.DestinationFinalized
: TransferState.Attested,
} as AttestedTransferReceipt<CircleTransferProtocol, SC, DC>;
state: TransferState.DestinationFinalized,
destinationTxs: [],
} as CompletedTransferReceipt<CircleTransferProtocol, SC, DC>;
}
yield receipt;
}
}
}
28 changes: 22 additions & 6 deletions connect/src/protocols/tokenTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import { Wormhole } from "../wormhole";
import {
AttestedTransferReceipt,
CompletedTransferReceipt,
SourceFinalizedTransferReceipt,
SourceInitiatedTransferReceipt,
TransferQuote,
TransferReceipt,
TransferState,
Expand Down Expand Up @@ -541,7 +543,11 @@ export class TokenTransfer<N extends Network = Network>

const originTxs = xfer.txids.filter((txid) => txid.chain === transfer.from.chain);
if (originTxs.length > 0) {
receipt = { ...receipt, state: TransferState.SourceInitiated, originTxs: originTxs };
receipt = {
...receipt,
state: TransferState.SourceInitiated,
originTxs: originTxs,
} as SourceInitiatedTransferReceipt<TokenTransferProtocol>;
}

const att =
Expand Down Expand Up @@ -596,7 +602,12 @@ export class TokenTransfer<N extends Network = Network>
txid,
leftover(start, timeout),
);
yield { ...receipt, state: TransferState.SourceFinalized, attestation: { id: msg } };
receipt = {
...receipt,
state: TransferState.SourceFinalized,
attestation: { id: msg },
} as SourceFinalizedTransferReceipt<TokenTransferProtocol, SC, DC>;
yield receipt;
}

// If the source is finalized, we need to fetch the signed attestation
Expand All @@ -606,7 +617,12 @@ export class TokenTransfer<N extends Network = Network>
if (!receipt.attestation.id) throw "Attestation id required to fetch attestation";
const { id } = receipt.attestation;
const attestation = await TokenTransfer.getTransferVaa(wh, id, leftover(start, timeout));
yield { ...receipt, attestation: { id, attestation }, state: TransferState.Attested };
receipt = {
...receipt,
attestation: { id, attestation },
state: TransferState.Attested,
} as AttestedTransferReceipt<TokenTransferProtocol, SC, DC>;
yield receipt;
}

// First try to grab the tx status from the API
Expand Down Expand Up @@ -638,13 +654,13 @@ export class TokenTransfer<N extends Network = Network>
);

if (isComplete) {
yield {
receipt = {
...receipt,
state: TransferState.DestinationFinalized,
} as CompletedTransferReceipt<TokenTransferProtocol, SC, DC>;
} else {
yield receipt;
}

yield receipt;
}

yield receipt;
Expand Down

0 comments on commit fd76875

Please sign in to comment.