Skip to content

Commit e05cf0f

Browse files
committed
chore: refactor more
1 parent 5166925 commit e05cf0f

File tree

7 files changed

+69
-138
lines changed

7 files changed

+69
-138
lines changed

foo.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

sdk/definitions/src/ntt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ export interface Ntt<N extends Network, C extends Chain> {
264264
* redeem redeems a set of Attestations to the corresponding transceivers on the destination chain
265265
* @param attestations The attestations to redeem, the length should be equal to the number of transceivers
266266
* @param payer The account that will pay for the transaction
267-
* @param recipient The recipient address (only used for Stacks, can be undefined for other platforms)
267+
* @param recipient The recipient address
268268
*
269269
* TODO: replace with Map<transceiver type, Attestation>
270270
*/

sdk/route/src/automatic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ export class NttAutomaticRoute<N extends Network>
283283
address: vaa.emitterAddress,
284284
});
285285

286-
const dstInfo = NttRoute.resolveDestinationNttContracts(
286+
const { dstContracts: dstInfo } = NttRoute.resolveDestinationNttContracts(
287287
this.staticConfig,
288288
{
289289
chain: vaa.emitterChain,

sdk/route/src/executor/executor.ts

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ import {
4242
fetchCapabilities,
4343
fetchSignedQuote,
4444
fetchStatus,
45-
isRelayStatusFailed,
4645
getNativeRecipientAddress,
46+
isRelayStatusFailed,
4747
} from "./utils.js";
4848
import { Ntt, NttWithExecutor } from "@wormhole-foundation/sdk-definitions-ntt";
49+
import { StacksNttWithExecutor } from "@wormhole-foundation/sdk-stacks-ntt";
4950
import {
5051
isNative,
5152
relayInstructionsLayout,
@@ -405,6 +406,7 @@ export class NttExecutorRoute<N extends Network>
405406
type: "StacksNttReceiveInstruction" as const,
406407
nttManager: Buffer.from(
407408
// TODO: is using Buffer correct?
409+
// what if 0x prefix?
408410
params.normalizedParams.destinationContracts.manager
409411
),
410412
recipient: recipient
@@ -617,50 +619,26 @@ export class NttExecutorRoute<N extends Network>
617619
const { recipientChain, trimmedAmount } =
618620
vaa.payload["nttManagerPayload"].payload;
619621

620-
let token: string, manager: string, whTransceiver: string;
621-
let dstInfo: Ntt.Contracts;
622-
623-
if (chainToPlatform(vaa.emitterChain) === "Stacks") {
624-
const { stacksConfig, dstInfo: stacksDstInfo } =
625-
NttRoute.resolveDestinationNttContractsStacksEmitter(
626-
this.staticConfig.ntt,
627-
vaa.emitterAddress,
628-
recipientChain
629-
);
630-
631-
token = stacksConfig.token;
632-
manager = stacksConfig.manager;
633-
whTransceiver = stacksConfig.transceiver.find(
634-
(t) => t.type === "wormhole"
635-
)!.address;
636-
637-
dstInfo = stacksDstInfo;
638-
} else {
639-
token = canonicalAddress({
640-
chain: vaa.emitterChain,
641-
address: vaa.payload["nttManagerPayload"].payload.sourceToken,
642-
});
643-
manager = canonicalAddress({
622+
const srcManagerAddress = await (async () => {
623+
if (chainToPlatform(tx.chain) === "Stacks") {
624+
// We can't use the manager address from the VAA payload for Stacks
625+
// since it's the hashed address. Instead, we need to fetch it from the transaction.
626+
const stacks = this.wh.getChain("Stacks");
627+
const rpc = await stacks.getRpc();
628+
return StacksNttWithExecutor.getManagerAddressFromTx(rpc, tx.txid);
629+
}
630+
return {
644631
chain: vaa.emitterChain,
645632
address: vaa.payload["sourceNttManager"],
646-
});
647-
whTransceiver =
648-
chainToPlatform(vaa.emitterChain) === "Solana"
649-
? manager
650-
: canonicalAddress({
651-
chain: vaa.emitterChain,
652-
address: vaa.emitterAddress,
653-
});
654-
655-
dstInfo = NttRoute.resolveDestinationNttContracts(
633+
};
634+
})();
635+
636+
const { srcContracts, dstContracts } =
637+
NttRoute.resolveDestinationNttContracts(
656638
this.staticConfig.ntt,
657-
{
658-
chain: vaa.emitterChain,
659-
address: vaa.payload["sourceNttManager"],
660-
},
639+
srcManagerAddress,
661640
recipientChain
662641
);
663-
}
664642

665643
const amt = amount.fromBaseUnits(
666644
trimmedAmount.amount,
@@ -681,20 +659,8 @@ export class NttExecutorRoute<N extends Network>
681659
options: {},
682660
normalizedParams: {
683661
amount: amt,
684-
sourceContracts: {
685-
token,
686-
manager,
687-
transceiver: {
688-
wormhole: whTransceiver,
689-
},
690-
},
691-
destinationContracts: {
692-
token: dstInfo.token,
693-
manager: dstInfo.manager,
694-
transceiver: {
695-
wormhole: dstInfo.transceiver["wormhole"]!,
696-
},
697-
},
662+
sourceContracts: srcContracts,
663+
destinationContracts: dstContracts,
698664
referrerFeeDbps: 0n,
699665
},
700666
},

sdk/route/src/manual.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ export class NttManualRoute<N extends Network>
264264
address: vaa.emitterAddress,
265265
});
266266

267-
const dstInfo = NttRoute.resolveDestinationNttContracts(
267+
const { dstContracts: dstInfo } = NttRoute.resolveDestinationNttContracts(
268268
this.staticConfig,
269269
{
270270
chain: vaa.emitterChain,

sdk/route/src/types.ts

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
TransferReceipt as _TransferReceipt,
1616
amount,
1717
canonicalAddress,
18-
encoding,
1918
isAttested,
2019
isCompleted,
2120
isDestinationQueued,
@@ -31,10 +30,6 @@ import {
3130
toUniversal,
3231
} from "@wormhole-foundation/sdk-connect";
3332
import { MultiTokenNtt, Ntt } from "@wormhole-foundation/sdk-definitions-ntt";
34-
import {
35-
keccak256,
36-
UniversalAddress,
37-
} from "@wormhole-foundation/sdk-definitions";
3833
import { trackAxelar, trackExecutor } from "./tracking.js";
3934

4035
export namespace NttRoute {
@@ -244,7 +239,7 @@ export namespace NttRoute {
244239
config: Config,
245240
srcManager: ChainAddress<C>,
246241
dstChain: Chain
247-
): Ntt.Contracts {
242+
): { srcContracts: Ntt.Contracts; dstContracts: Ntt.Contracts } {
248243
const cfg = Object.values(config.tokens);
249244
const address = canonicalAddress(srcManager);
250245
for (const tokens of cfg) {
@@ -260,7 +255,19 @@ export namespace NttRoute {
260255
`Cannot find destination Ntt contracts in config for: ${address}`
261256
);
262257
}
263-
return {
258+
259+
const srcContracts = {
260+
token: found.token,
261+
manager: found.manager,
262+
transceiver: {
263+
wormhole: found.transceiver.find((v) => v.type === "wormhole")!
264+
.address,
265+
},
266+
quoter: found.quoter,
267+
svmShims: found.svmShims,
268+
};
269+
270+
const dstContracts = {
264271
token: remote.unwrapsOnRedeem ? "native" : remote.token,
265272
manager: remote.manager,
266273
transceiver: {
@@ -270,61 +277,11 @@ export namespace NttRoute {
270277
quoter: remote.quoter,
271278
svmShims: remote.svmShims,
272279
};
273-
}
274-
}
275-
throw new Error("Cannot find Ntt contracts in config for: " + address);
276-
}
277-
278-
export function resolveDestinationNttContractsStacksEmitter(
279-
config: Config,
280-
emitterAddress: UniversalAddress,
281-
dstChain: Chain
282-
): { stacksConfig: TokenConfig; dstInfo: Ntt.Contracts } {
283-
const emitterLower = emitterAddress.toString().toLowerCase();
284-
285-
for (const tokens of Object.values(config.tokens)) {
286-
for (const tokenConfig of tokens) {
287-
if (tokenConfig.chain === "Stacks") {
288-
const whTransceiver = tokenConfig.transceiver.find(
289-
(t) => t.type === "wormhole"
290-
);
291-
if (whTransceiver) {
292-
// The emitter address on the VAA is the keccak256 hash of the transceiver address
293-
const hash = keccak256(whTransceiver.address);
294-
const hashHex = encoding.hex
295-
.encode(hash, true)
296-
.toString()
297-
.toLowerCase();
298-
if (hashHex === emitterLower) {
299-
// Find the destination config in the same token group
300-
const remote = tokens.find((tc) => tc.chain === dstChain);
301-
if (!remote) {
302-
throw new Error(
303-
`Cannot find destination Ntt contracts in config for chain: ${dstChain}`
304-
);
305-
}
306280

307-
const dstInfo: Ntt.Contracts = {
308-
token: remote.unwrapsOnRedeem ? "native" : remote.token,
309-
manager: remote.manager,
310-
transceiver: {
311-
wormhole: remote.transceiver.find(
312-
(v) => v.type === "wormhole"
313-
)!.address,
314-
},
315-
quoter: remote.quoter,
316-
svmShims: remote.svmShims,
317-
};
318-
319-
return { stacksConfig: tokenConfig, dstInfo };
320-
}
321-
}
322-
}
281+
return { srcContracts, dstContracts };
323282
}
324283
}
325-
throw new Error(
326-
`Cannot find Stacks NTT config for emitter: ${emitterAddress}`
327-
);
284+
throw new Error("Cannot find Ntt contracts in config for: " + address);
328285
}
329286

330287
// returns true if the amount is greater than 95% of the capacity

stacks/ts/src/nttWithExecutor.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "@wormhole-foundation/sdk-definitions";
1010
import { NttWithExecutor } from "@wormhole-foundation/sdk-definitions-ntt";
1111
import {
12+
StacksAddress,
1213
StacksChains,
1314
StacksPlatform,
1415
StacksUnsignedTransaction,
@@ -23,6 +24,7 @@ import {
2324
standardPrincipalCV,
2425
uintCV,
2526
} from "@stacks/transactions";
27+
import axios from "axios";
2628
import { StacksNtt, StacksNttContracts } from "./ntt.js";
2729

2830
const nttManagerWithExecutorAddresses: Partial<
@@ -115,7 +117,6 @@ export class StacksNttWithExecutor<N extends Network, C extends StacksChains>
115117

116118
const referrerAddress = quote.referrer.address.toString();
117119

118-
// TODO: ContractCallOptions should be the UnisgnedTransactionType
119120
const tx: ContractCallOptions = {
120121
contractName: executorContractName,
121122
contractAddress: executorDeployer,
@@ -157,4 +158,32 @@ export class StacksNttWithExecutor<N extends Network, C extends StacksChains>
157158

158159
return { msgValue, gasLimit };
159160
}
161+
162+
static async getManagerAddressFromTx(
163+
connection: StacksNetwork,
164+
txHash: string
165+
): Promise<ChainAddress<"Stacks">> {
166+
const url = `${connection.client.baseUrl}/extended/v1/tx/${txHash}`;
167+
168+
const response = await axios.get(url);
169+
const data = response.data;
170+
171+
if (!data.contract_call?.function_args) {
172+
throw new Error("No contract call function args found in transaction");
173+
}
174+
175+
const functionArgs = data.contract_call.function_args;
176+
const nttManagerArg = functionArgs.find(
177+
(arg: any) => arg.name === "ntt-manager"
178+
);
179+
180+
if (!nttManagerArg) {
181+
throw new Error("No ntt-manager argument found in transaction");
182+
}
183+
184+
// For some reason, the value comes with extra single quotes, so we need to remove them
185+
const managerAddress = nttManagerArg.repr.replace(/'/g, "");
186+
187+
return { chain: "Stacks", address: new StacksAddress(managerAddress) };
188+
}
160189
}

0 commit comments

Comments
 (0)