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 25d4f1b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 44 deletions.
74 changes: 45 additions & 29 deletions connect/src/protocols/cctpTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,20 +489,31 @@ 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)) ?? [];
const ctt = xfer.attestations?.filter((a) => isCircleMessageId(a.id)) ?? [];
const attestation = att.length > 0 ? att[0]! : ctt.length > 0 ? ctt[0]! : undefined;
if (attestation && attestation.attestation) {
receipt = {
...receipt,
state: TransferState.Attested,
attestation: attestation,
} as AttestedTransferReceipt<CircleTransferProtocol, Chain, Chain>;
if (attestation) {
if (attestation.id) {
receipt = {
...receipt,
state: TransferState.SourceFinalized,
attestation: attestation,
} as SourceFinalizedTransferReceipt<CircleTransferProtocol>;
}

if (attestation.attestation)
receipt = {
...receipt,
state: TransferState.Attested,
attestation: { id: attestation.id, attestation: attestation.attestation },
} as AttestedTransferReceipt<CircleTransferProtocol>;
}

const destinationTxs = xfer.txids.filter((txid) => txid.chain === xfer.transfer.to.chain);
Expand All @@ -511,7 +522,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 +554,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 +576,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;
}
}
}
53 changes: 39 additions & 14 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,19 +543,32 @@ 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 =
xfer.attestations && xfer.attestations.length > 0 ? xfer.attestations![0]! : undefined;
const attestation =
att && att.id.emitter ? { id: att.id, attestation: att.attestation } : undefined;
if (attestation && attestation.attestation) {
receipt = {
...receipt,
state: TransferState.Attested,
attestation: attestation,
} as AttestedTransferReceipt<TokenTransferProtocol>;
const attestation = att && att.id ? { id: att.id, attestation: att.attestation } : undefined;
if (attestation) {
if (attestation.id) {
receipt = {
...receipt,
state: TransferState.SourceFinalized,
attestation: { id: attestation.id },
} as SourceFinalizedTransferReceipt<TokenTransferProtocol>;
}

if (attestation.attestation) {
receipt = {
...receipt,
state: TransferState.Attested,
attestation: { id: attestation.id, attestation: attestation.attestation },
} as AttestedTransferReceipt<TokenTransferProtocol>;
}
}

const destinationTxs = xfer.txids.filter((txid) => txid.chain === transfer.to.chain);
Expand Down Expand Up @@ -596,7 +611,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 +626,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 +663,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
2 changes: 1 addition & 1 deletion connect/src/wormholeTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface CompletedTransferReceipt<
> extends BaseTransferReceipt<PN, SC, DC> {
state: TransferState.DestinationInitiated | TransferState.DestinationFinalized;
originTxs: TransactionId<SC>[];
attestation: Required<AttestationReceipt<PN>>;
attestation: AttestationReceipt<PN>;
destinationTxs?: TransactionId<DC>[];
}

Expand Down

0 comments on commit 25d4f1b

Please sign in to comment.