Skip to content

Commit 5b66b46

Browse files
committed
Removed RouteTransferRequest instance variable from Route
1 parent 0ce41d1 commit 5b66b46

File tree

9 files changed

+201
-191
lines changed

9 files changed

+201
-191
lines changed

connect/__tests__/mocks/routes/automatic.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
nativeTokenId,
1313
amount,
1414
} from "../../../src";
15+
import { RouteTransferRequest } from "../../../src/routes/request";
1516
import { AutomaticRoute, StaticRouteMethods } from "../../../src/routes/route";
1617
import {
1718
Quote,
@@ -67,46 +68,49 @@ export class AutomaticMockRoute<N extends Network>
6768
return true;
6869
}
6970

70-
async validate(params: TransferParams<Op>): Promise<ValidationResult<Op>> {
71+
async validate(
72+
request: RouteTransferRequest<N>,
73+
params: TransferParams<Op>,
74+
): Promise<ValidationResult<Op>> {
7175
await delay(250);
7276
return {
7377
valid: true,
7478
params: { ...params, options: this.getDefaultOptions() },
7579
};
7680
}
7781

78-
async quote(params: ValidatedTransferParams<Op>): Promise<Q> {
82+
async quote(request: RouteTransferRequest<N>, params: ValidatedTransferParams<Op>): Promise<Q> {
7983
await delay(1000);
8084
const fakeQuote: Q = {
8185
success: true,
8286
sourceToken: {
83-
token: this.request.source.id,
84-
amount: amount.parse(params.amount, this.request.source.decimals),
87+
token: request.source.id,
88+
amount: amount.parse(params.amount, request.source.decimals),
8589
},
8690
destinationToken: {
87-
token: this.request.destination!.id,
88-
amount: amount.parse(params.amount, this.request.destination.decimals),
91+
token: request.destination!.id,
92+
amount: amount.parse(params.amount, request.destination.decimals),
8993
},
9094
relayFee: {
91-
token: this.request.source.id,
92-
amount: amount.parse("0.01", this.request.source.decimals),
95+
token: request.source.id,
96+
amount: amount.parse("0.01", request.source.decimals),
9397
},
9498
params,
9599
};
96100
return fakeQuote;
97101
}
98102

99-
async initiate(sender: Signer, _quote: Q): Promise<R> {
103+
async initiate(request: RouteTransferRequest<N>, sender: Signer, _quote: Q): Promise<R> {
100104
await delay(1000);
101105

102106
const fakeTxId =
103-
this.request.from.chain === "Solana"
107+
request.fromChain.chain === "Solana"
104108
? encoding.b58.encode(new Uint8Array(64))
105109
: encoding.hex.encode(new Uint8Array(32));
106110

107111
const fakeReceipt: SourceInitiatedTransferReceipt = {
108-
from: this.request.from.chain,
109-
to: this.request.to.chain,
112+
from: request.fromChain.chain,
113+
to: request.toChain.chain,
110114
state: TransferState.SourceInitiated,
111115
originTxs: [{ chain: sender.chain(), txid: fakeTxId }],
112116
};
@@ -122,11 +126,7 @@ export class AutomaticMockRoute<N extends Network>
122126
attestation: {
123127
id: {} as WormholeMessageId,
124128
},
125-
} satisfies CompletedTransferReceipt<
126-
unknown,
127-
typeof this.request.from.chain,
128-
typeof this.request.to.chain
129-
>;
129+
} satisfies CompletedTransferReceipt<unknown, typeof receipt.from, typeof receipt.to>;
130130
yield fakeReceipt;
131131
}
132132

connect/__tests__/mocks/routes/manual.ts

+17-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
nativeTokenId,
1414
amount,
1515
} from "../../../src";
16-
import { ManualRoute, StaticRouteMethods } from "../../../src/routes/route";
16+
import { ManualRoute, RouteTransferRequest, StaticRouteMethods } from "../../../src/routes/route";
1717
import {
1818
Quote,
1919
Receipt,
@@ -62,46 +62,49 @@ export class ManualMockRoute<N extends Network>
6262
return true;
6363
}
6464

65-
async validate(params: TransferParams<Op>): Promise<ValidationResult<Op>> {
65+
async validate(
66+
request: RouteTransferRequest<N>,
67+
params: TransferParams<Op>,
68+
): Promise<ValidationResult<Op>> {
6669
await delay(250);
6770
return {
6871
valid: true,
6972
params: { ...params, options: this.getDefaultOptions() },
7073
};
7174
}
7275

73-
async quote(params: ValidatedTransferParams<Op>): Promise<Q> {
76+
async quote(request: RouteTransferRequest<N>, params: ValidatedTransferParams<Op>): Promise<Q> {
7477
await delay(1000);
7578
const fakeQuote: Q = {
7679
success: true,
7780
sourceToken: {
78-
token: this.request.source.id,
79-
amount: amount.parse(params.amount, this.request.destination.decimals),
81+
token: request.source.id,
82+
amount: amount.parse(params.amount, request.destination.decimals),
8083
},
8184
destinationToken: {
82-
token: this.request.destination!.id,
83-
amount: amount.parse(params.amount, this.request.destination.decimals),
85+
token: request.destination!.id,
86+
amount: amount.parse(params.amount, request.destination.decimals),
8487
},
8588
relayFee: {
86-
token: this.request.source.id,
87-
amount: amount.parse("0.01", this.request.source.decimals),
89+
token: request.source.id,
90+
amount: amount.parse("0.01", request.source.decimals),
8891
},
8992
params,
9093
};
9194
return fakeQuote;
9295
}
9396

94-
async initiate(sender: Signer, _quote: Q): Promise<R> {
97+
async initiate(request: RouteTransferRequest<N>, sender: Signer, _quote: Q): Promise<R> {
9598
await delay(1000);
9699

97100
const fakeTxId =
98-
this.request.from.chain === "Solana"
101+
request.from.chain === "Solana"
99102
? encoding.b58.encode(new Uint8Array(64))
100103
: encoding.hex.encode(new Uint8Array(32));
101104

102105
const fakeReceipt: SourceInitiatedTransferReceipt = {
103-
from: this.request.from.chain,
104-
to: this.request.to.chain,
106+
from: request.from.chain,
107+
to: request.to.chain,
105108
state: TransferState.SourceInitiated,
106109
originTxs: [{ chain: sender.chain(), txid: fakeTxId }],
107110
};
@@ -117,11 +120,7 @@ export class ManualMockRoute<N extends Network>
117120
attestation: {
118121
id: {} as WormholeMessageId,
119122
},
120-
} satisfies CompletedTransferReceipt<
121-
unknown,
122-
typeof this.request.from.chain,
123-
typeof this.request.to.chain
124-
>;
123+
} satisfies CompletedTransferReceipt<unknown, typeof receipt.from, typeof receipt.to>;
125124
yield fakeReceipt;
126125
}
127126

connect/src/routes/cctp/automatic.ts

+29-29
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
ValidatedTransferParams,
2222
ValidationResult,
2323
} from "../types.js";
24+
import type { RouteTransferRequest } from "../request.js";
2425

2526
export namespace AutomaticCCTPRoute {
2627
export type Options = {
@@ -80,19 +81,16 @@ export class AutomaticCCTPRoute<N extends Network>
8081
return [Wormhole.chainAddress(chain, circle.usdcContract.get(network, chain)!)];
8182
}
8283

83-
// get the liist of destination tokens that may be recieved on the destination chain
84+
// get the list of destination tokens that may be received on the destination chain
8485
static async supportedDestinationTokens<N extends Network>(
8586
sourceToken: TokenId,
8687
fromChain: ChainContext<N>,
8788
toChain: ChainContext<N>,
8889
): Promise<TokenId[]> {
8990
// Ensure the source token is USDC
90-
const sourceChainUsdcContract = circle.usdcContract.get(fromChain.network, fromChain.chain);
91-
if (!sourceChainUsdcContract ) return [];
92-
if (!isSameToken(
93-
sourceToken,
94-
Wormhole.tokenId(fromChain.chain, sourceChainUsdcContract),
95-
)){
91+
const sourceChainUsdcContract = circle.usdcContract.get(fromChain.network, fromChain.chain);
92+
if (!sourceChainUsdcContract) return [];
93+
if (!isSameToken(sourceToken, Wormhole.tokenId(fromChain.chain, sourceChainUsdcContract))) {
9694
return [];
9795
}
9896

@@ -115,10 +113,10 @@ export class AutomaticCCTPRoute<N extends Network>
115113
return true;
116114
}
117115

118-
async validate(params: Tp): Promise<Vr> {
116+
async validate(request: RouteTransferRequest<N>, params: Tp): Promise<Vr> {
119117
try {
120118
const options = params.options ?? this.getDefaultOptions();
121-
const normalizedParams = await this.normalizeTransferParams(params);
119+
const normalizedParams = await this.normalizeTransferParams(request, params);
122120

123121
const validatedParams: Vp = {
124122
normalizedParams,
@@ -136,10 +134,10 @@ export class AutomaticCCTPRoute<N extends Network>
136134
}
137135
}
138136

139-
async quote(params: Vp): Promise<QR> {
137+
async quote(request: RouteTransferRequest<N>, params: Vp): Promise<QR> {
140138
try {
141-
return this.request.displayQuote(
142-
await CircleTransfer.quoteTransfer(this.request.fromChain, this.request.toChain, {
139+
return request.displayQuote(
140+
await CircleTransfer.quoteTransfer(request.fromChain, request.toChain, {
143141
automatic: true,
144142
amount: amount.units(params.normalizedParams.amount),
145143
nativeGas: amount.units(params.normalizedParams.nativeGasAmount),
@@ -154,16 +152,19 @@ export class AutomaticCCTPRoute<N extends Network>
154152
}
155153
}
156154

157-
private async normalizeTransferParams(params: Tp): Promise<AutomaticCCTPRoute.NormalizedParams> {
158-
const amt = this.request.parseAmount(params.amount);
155+
private async normalizeTransferParams(
156+
request: RouteTransferRequest<N>,
157+
params: Tp,
158+
): Promise<AutomaticCCTPRoute.NormalizedParams> {
159+
const amt = request.parseAmount(params.amount);
159160

160-
const ctb = await this.request.fromChain.getAutomaticCircleBridge();
161-
const fee = await ctb.getRelayerFee(this.request.toChain.chain);
161+
const ctb = await request.fromChain.getAutomaticCircleBridge();
162+
const fee = await ctb.getRelayerFee(request.toChain.chain);
162163

163164
const minAmount = (fee * 105n) / 100n;
164165
if (amount.units(amt) < minAmount) {
165166
throw new Error(
166-
`Minimum amount is ${amount.display(this.request.amountFromBaseUnits(minAmount))}`,
167+
`Minimum amount is ${amount.display(request.amountFromBaseUnits(minAmount))}`,
167168
);
168169
}
169170

@@ -184,9 +185,9 @@ export class AutomaticCCTPRoute<N extends Network>
184185
}
185186

186187
return {
187-
fee: this.request.amountFromBaseUnits(fee),
188+
fee: request.amountFromBaseUnits(fee),
188189
amount: amt,
189-
nativeGasAmount: this.request.amountFromBaseUnits(nativeGasAmount),
190+
nativeGasAmount: request.amountFromBaseUnits(nativeGasAmount),
190191
};
191192
}
192193

@@ -204,17 +205,22 @@ export class AutomaticCCTPRoute<N extends Network>
204205
};
205206
}
206207

207-
async initiate(signer: Signer, quote: Q, to: ChainAddress): Promise<R> {
208+
async initiate(
209+
request: RouteTransferRequest<N>,
210+
signer: Signer,
211+
quote: Q,
212+
to: ChainAddress,
213+
): Promise<R> {
208214
const { params } = quote;
209215
let transfer = this.toTransferDetails(
210216
params,
211217
Wormhole.chainAddress(signer.chain(), signer.address()),
212218
to,
213219
);
214-
let txids = await CircleTransfer.transfer<N>(this.request.fromChain, transfer, signer);
220+
let txids = await CircleTransfer.transfer<N>(request.fromChain, transfer, signer);
215221

216222
const msg = await CircleTransfer.getTransferMessage(
217-
this.request.fromChain,
223+
request.fromChain,
218224
txids[txids.length - 1]!.txid,
219225
);
220226

@@ -228,12 +234,6 @@ export class AutomaticCCTPRoute<N extends Network>
228234
}
229235

230236
public override async *track(receipt: R, timeout?: number) {
231-
yield* CircleTransfer.track(
232-
this.wh,
233-
receipt,
234-
timeout,
235-
this.request.fromChain,
236-
this.request.toChain,
237-
);
237+
yield* CircleTransfer.track(this.wh, receipt, timeout);
238238
}
239239
}

0 commit comments

Comments
 (0)