Skip to content

Commit 8bd672c

Browse files
authored
sdk: Add svmShims to Ntt.Contracts to configure Wormhole Shims (#718)
This PR adds: * Update `Ntt.Contracts` to add a `svmShims` field that works like how [the current Solana shimOverrides argument in the constructor works](#663 (comment)) — the main difference being that it is now backwards compatible * Update `SolanaNtt` and `SolanaNttWormholeTransceiver` classes to use the `svmShims` field * Add default shim addresses as a `SolanaChains`-keyed constant that can be reference by `SolanaNttWormholeTransceiver` * Update tests
1 parent 4a8bdb0 commit 8bd672c

File tree

4 files changed

+91
-53
lines changed

4 files changed

+91
-53
lines changed

sdk/__tests__/utils.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export const NETWORK: "Devnet" = "Devnet";
4848
type NativeSdkSigner<P extends Platform> = P extends "Evm"
4949
? ethers.Wallet
5050
: P extends "Solana"
51-
? web3.Keypair
52-
: never;
51+
? web3.Keypair
52+
: never;
5353

5454
interface Signers<P extends Platform = Platform> {
5555
address: ChainAddress;
@@ -212,7 +212,7 @@ export async function link(chainInfos: Ctx[], accountantPrivateKey: string) {
212212
);
213213
vaas.push(serialize(vaa));
214214
}
215-
215+
216216
// Submit all registrations at once
217217
await submitAccountantVAAs(vaas, accountantPrivateKey);
218218
}
@@ -267,8 +267,6 @@ export async function transferWithChecks(sourceCtx: Ctx, destinationCtx: Ctx) {
267267
const [managerBalanceAfterRecv, userBalanceAfterRecv] =
268268
await getManagerAndUserBalance(destinationCtx);
269269

270-
271-
272270
checkBalances(
273271
sourceCtx.mode,
274272
[managerBalanceBeforeSend, managerBalanceAfterSend],
@@ -300,9 +298,8 @@ async function waitForRelay(
300298
let success = false;
301299
while (!success) {
302300
try {
303-
const successBlock = await wormholeRelayer.deliverySuccessBlock(
304-
deliveryHash
305-
);
301+
const successBlock =
302+
await wormholeRelayer.deliverySuccessBlock(deliveryHash);
306303
if (successBlock > 0) success = true;
307304
console.log("Relayer delivery: ", success);
308305
} catch (e) {
@@ -468,10 +465,12 @@ async function deployEvm(ctx: Ctx): Promise<Ctx> {
468465
console.log("Initialize the manager");
469466
await tryAndWaitThrice(() => manager.initialize());
470467
console.log("Initialize the transceiver");
471-
const coreFee = await (await ctx.context.getWormholeCore()).getMessageFee()
472-
await tryAndWaitThrice(() => transceiver.initialize({
473-
value: coreFee
474-
}));
468+
const coreFee = await (await ctx.context.getWormholeCore()).getMessageFee();
469+
await tryAndWaitThrice(() =>
470+
transceiver.initialize({
471+
value: coreFee,
472+
})
473+
);
475474

476475
// Setup the initial calls, like transceivers for the manager
477476
console.log("Set transceiver for manager");
@@ -538,6 +537,7 @@ async function deploySolana(ctx: Ctx): Promise<Ctx> {
538537
transceiver: {
539538
wormhole: transceiverProgramId,
540539
},
540+
svmShims: {},
541541
};
542542

543543
const manager = (await getNtt(ctx)) as SolanaNtt<typeof NETWORK, "Solana">;
@@ -600,6 +600,7 @@ async function deploySolana(ctx: Ctx): Promise<Ctx> {
600600
},
601601
manager: manager.program.programId.toString(),
602602
token: mint.toString(),
603+
svmShims: {},
603604
},
604605
};
605606
}
@@ -751,22 +752,22 @@ async function tryAndWaitThrice(
751752
}
752753

753754
export async function setMessageFee(chains: Chain[], fee: bigint) {
754-
console.log(`Setting message fee for ${chains} to ${fee}`)
755+
console.log(`Setting message fee for ${chains} to ${fee}`);
755756
for (const chain of chains) {
756-
const chainCtx = wh.getChain(chain)
757-
const core = await chainCtx.getWormholeCore()
758-
const coreAddress = chainCtx.config.contracts.coreBridge
759-
const existingFee = await core.getMessageFee()
760-
console.log(`Existing core bridge fee for ${chain}: ${existingFee}`)
761-
const rpc = await chainCtx.getRpc()
757+
const chainCtx = wh.getChain(chain);
758+
const core = await chainCtx.getWormholeCore();
759+
const coreAddress = chainCtx.config.contracts.coreBridge;
760+
const existingFee = await core.getMessageFee();
761+
console.log(`Existing core bridge fee for ${chain}: ${existingFee}`);
762+
const rpc = await chainCtx.getRpc();
762763
await rpc.send("anvil_setStorageAt", [
763764
coreAddress,
764765
7, // messageFee storage slot
765-
ethers.zeroPadValue(ethers.toBeHex(fee), 32)
766+
ethers.zeroPadValue(ethers.toBeHex(fee), 32),
766767
]);
767-
768-
const newFee = await core.getMessageFee()
769-
console.log(`New core bridge fee for ${chain}: ${newFee}`)
768+
769+
const newFee = await core.getMessageFee();
770+
console.log(`New core bridge fee for ${chain}: ${newFee}`);
770771
}
771772
}
772773

sdk/definitions/src/ntt.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
toChainId,
55
type Chain,
66
type Network,
7+
type PlatformToChains,
78
} from "@wormhole-foundation/sdk-base";
89

910
import {
@@ -17,6 +18,7 @@ import {
1718
keccak256,
1819
} from "@wormhole-foundation/sdk-definitions";
1920

21+
import { PublicKey } from "@solana/web3.js";
2022
import {
2123
NttManagerMessage,
2224
nativeTokenTransferLayout,
@@ -25,7 +27,6 @@ import {
2527
transceiverInstructionLayout,
2628
transceiverRegistration,
2729
} from "./layouts/index.js";
28-
import { PublicKey } from "@solana/web3.js";
2930

3031
/**
3132
* @namespace Ntt
@@ -42,6 +43,31 @@ export namespace Ntt {
4243
[type: string]: string;
4344
};
4445
quoter?: string;
46+
svmShims?: {
47+
postMessageShimOverride?: string;
48+
verifyVaaShimOverride?: string;
49+
};
50+
};
51+
52+
export const DEFAULT_SVM_SHIM_ADDRESSES: {
53+
[chain in PlatformToChains<"Solana">]: {
54+
postMessageShim: string;
55+
verifyVaaShim: string;
56+
};
57+
} = {
58+
Solana: {
59+
postMessageShim: "EtZMZM22ViKMo4r5y4Anovs3wKQ2owUmDpjygnMMcdEX",
60+
verifyVaaShim: "EFaNWErqAtVWufdNb7yofSHHfWFos843DFpu4JBw24at",
61+
},
62+
// TODO: update once shim is configured
63+
Pythnet: {
64+
postMessageShim: "",
65+
verifyVaaShim: "",
66+
},
67+
Fogo: {
68+
postMessageShim: "",
69+
verifyVaaShim: "",
70+
},
4571
};
4672

4773
export type Message = NttManagerMessage<typeof nativeTokenTransferLayout>;
@@ -365,7 +391,7 @@ export interface Ntt<N extends Network, C extends Chain> {
365391
export interface NttTransceiver<
366392
N extends Network,
367393
C extends Chain,
368-
A extends Ntt.Attestation
394+
A extends Ntt.Attestation,
369395
> {
370396
getTransceiverType(payer?: AccountAddress<C>): Promise<string>;
371397

@@ -427,15 +453,15 @@ export interface WormholeNttTransceiver<N extends Network, C extends Chain>
427453
export interface SolanaNttTransceiver<
428454
N extends Network,
429455
C extends Chain,
430-
A extends Ntt.Attestation
456+
A extends Ntt.Attestation,
431457
> extends NttTransceiver<N, C, A> {
432458
programId: PublicKey;
433459
}
434460

435461
export interface EvmNttTransceiver<
436462
N extends Network,
437463
C extends Chain,
438-
A extends Ntt.Attestation
464+
A extends Ntt.Attestation,
439465
> extends NttTransceiver<N, C, A> {}
440466

441467
declare module "@wormhole-foundation/sdk-definitions" {

solana/tests/anchor.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
serialize,
1515
serializePayload,
1616
} from "@wormhole-foundation/sdk";
17+
import { Ntt } from "@wormhole-foundation/sdk-definitions-ntt";
1718
import * as testing from "@wormhole-foundation/sdk-definitions/testing";
1819
import {
1920
SolanaAddress,
@@ -24,9 +25,8 @@ import {
2425
SolanaWormholeCore,
2526
utils,
2627
} from "@wormhole-foundation/sdk-solana-core";
27-
2828
import { IdlVersion, NTT, getTransceiverProgram } from "../ts/index.js";
29-
import { SolanaNtt, WormholeShimOverrides } from "../ts/sdk/index.js";
29+
import { SolanaNtt } from "../ts/sdk/index.js";
3030
import {
3131
TestDummyTransferHook,
3232
TestHelper,
@@ -49,7 +49,7 @@ const NTT_ADDRESS: anchor.web3.PublicKey =
4949
anchor.workspace.ExampleNativeTokenTransfers.programId;
5050
const WH_TRANSCEIVER_ADDRESS: anchor.web3.PublicKey =
5151
anchor.workspace.NttTransceiver.programId;
52-
const USE_WORMHOLE_SHIMS: WormholeShimOverrides = {};
52+
const SOLANA_WORMHOLE_SHIMS: Ntt.Contracts["svmShims"] = {};
5353

5454
/**
5555
* Test Helpers
@@ -158,10 +158,10 @@ describe("example-native-token-transfers", () => {
158158
transceiver: {
159159
wormhole: nttTransceivers["wormhole"].programId.toBase58(),
160160
},
161+
svmShims: SOLANA_WORMHOLE_SHIMS,
161162
},
162163
},
163-
VERSION,
164-
USE_WORMHOLE_SHIMS
164+
VERSION
165165
);
166166
});
167167

@@ -559,6 +559,7 @@ describe("example-native-token-transfers", () => {
559559
transceiver: {
560560
wormhole: nttTransceivers["wormhole"].programId.toBase58(),
561561
},
562+
svmShims: SOLANA_WORMHOLE_SHIMS,
562563
},
563564
};
564565

@@ -615,9 +616,8 @@ describe("example-native-token-transfers", () => {
615616
});
616617
const whTransceiver = await ntt.getWormholeTransceiver();
617618
expect(whTransceiver).toBeTruthy();
618-
const transceiverType = await whTransceiver!.getTransceiverType(
619-
payerAddress
620-
);
619+
const transceiverType =
620+
await whTransceiver!.getTransceiverType(payerAddress);
621621
expect(transceiverType).toBe("wormhole");
622622
});
623623
});

solana/ts/sdk/ntt.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,11 @@ import { IDL as WormholePostMessageShimIdl } from "../idl/wormhole_shim/ts/wormh
5555
import { type WormholeVerifyVaaShim } from "../idl/wormhole_shim/ts/wormhole_verify_vaa_shim.js";
5656
import { IDL as WormholeVerifyVaaShimIdl } from "../idl/wormhole_shim/ts/wormhole_verify_vaa_shim.js";
5757

58-
export type WormholeShimOverrides = {
59-
postMessageShimOverride?: PublicKey;
60-
verifyVaaShimOverride?: PublicKey;
61-
};
62-
6358
export class SolanaNttWormholeTransceiver<
64-
N extends Network,
65-
C extends SolanaChains
66-
> implements
59+
N extends Network,
60+
C extends SolanaChains,
61+
>
62+
implements
6763
WormholeNttTransceiver<N, C>,
6864
SolanaNttTransceiver<N, C, WormholeNttTransceiver.VAA>
6965
{
@@ -76,21 +72,37 @@ export class SolanaNttWormholeTransceiver<
7672
readonly manager: SolanaNtt<N, C>,
7773
readonly program: Program<NttBindings.Transceiver<IdlVersion>>,
7874
readonly version: string = "3.0.0",
79-
readonly shimOverrides: WormholeShimOverrides | null = {}
75+
readonly svmShims: Ntt.Contracts["svmShims"] = undefined
8076
) {
8177
this.programId = program.programId;
8278
this.pdas = NTT.transceiverPdas(program.programId);
83-
if (shimOverrides) {
79+
80+
if (svmShims) {
81+
const postMessageShimAddress =
82+
svmShims.postMessageShimOverride ??
83+
Ntt.DEFAULT_SVM_SHIM_ADDRESSES[this.manager.chain]["postMessageShim"];
84+
if (!postMessageShimAddress) {
85+
throw new Error(
86+
"No default or override Wormhole Post Message Shim address provided"
87+
);
88+
}
8489
this.postMessageShim = new Program<WormholePostMessageShim>(
8590
WormholePostMessageShimIdl,
86-
shimOverrides.postMessageShimOverride ??
87-
new PublicKey("EtZMZM22ViKMo4r5y4Anovs3wKQ2owUmDpjygnMMcdEX"),
91+
postMessageShimAddress,
8892
{ connection: this.program.provider.connection }
8993
);
94+
95+
const verifyVaaShimAddress =
96+
svmShims.verifyVaaShimOverride ??
97+
Ntt.DEFAULT_SVM_SHIM_ADDRESSES[this.manager.chain]["verifyVaaShim"];
98+
if (!verifyVaaShimAddress) {
99+
throw new Error(
100+
"No default or override Wormhole Verify VAA Shim address provided"
101+
);
102+
}
90103
this.verifyVaaShim = new Program<WormholeVerifyVaaShim>(
91104
WormholeVerifyVaaShimIdl,
92-
shimOverrides.verifyVaaShimOverride ??
93-
new PublicKey("EFaNWErqAtVWufdNb7yofSHHfWFos843DFpu4JBw24at"),
105+
verifyVaaShimAddress,
94106
{ connection: this.program.provider.connection }
95107
);
96108
}
@@ -562,8 +574,7 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
562574
readonly chain: C,
563575
readonly connection: Connection,
564576
readonly contracts: Contracts & { ntt?: Ntt.Contracts },
565-
readonly version: string = "3.0.0",
566-
readonly shimOverrides: WormholeShimOverrides | null = {}
577+
readonly version: string = "3.0.0"
567578
) {
568579
if (!contracts.ntt) throw new Error("Ntt contracts not found");
569580

@@ -609,7 +620,7 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
609620
version as IdlVersion
610621
),
611622
version,
612-
shimOverrides
623+
contracts.ntt!.svmShims
613624
);
614625
this.transceivers.push(whTransceiver.program);
615626
} else {
@@ -658,7 +669,7 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
658669
this,
659670
transceiverProgram,
660671
this.version,
661-
this.shimOverrides
672+
this.contracts.ntt!.svmShims
662673
);
663674
return null;
664675
}

0 commit comments

Comments
 (0)