Skip to content

Commit f383a43

Browse files
authored
Add fee method to core bridge (#196)
1 parent 33195de commit f383a43

File tree

10 files changed

+49
-22
lines changed

10 files changed

+49
-22
lines changed

core/definitions/src/protocols/core.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface WormholeCore<
1010
P extends Platform,
1111
C extends PlatformToChains<P>,
1212
> {
13+
getMessageFee(): Promise<bigint>;
1314
publishMessage(
1415
sender: AccountAddress<C>,
1516
message: string | Uint8Array,

examples/src/messaging.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Wormhole, encoding, signSendWait } from "@wormhole-foundation/connect-sdk";
2-
import { AlgorandPlatform } from "@wormhole-foundation/connect-sdk-algorand";
2+
import { SolanaPlatform } from "@wormhole-foundation/connect-sdk-solana";
33
import { getStuff } from "./helpers";
44

55
// register the protocol
6-
import "@wormhole-foundation/connect-sdk-algorand-core";
6+
import "@wormhole-foundation/connect-sdk-solana-core";
77

88
(async function () {
9-
const wh = new Wormhole("Testnet", [AlgorandPlatform]);
9+
const wh = new Wormhole("Testnet", [SolanaPlatform]);
1010

11-
const chain = wh.getChain("Algorand");
11+
const chain = wh.getChain("Solana");
1212
const { signer, address } = await getStuff(chain);
1313

1414
// Get a reference to the core messaging bridge

platforms/aptos/protocols/core/src/core.ts

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export class AptosWormholeCore<N extends Network, C extends AptosChains>
3636
throw new Error(`CoreBridge contract Address for chain ${chain} not found`);
3737
this.coreBridge = coreBridgeAddress;
3838
}
39+
getMessageFee(): Promise<bigint> {
40+
throw new Error("Method not implemented.");
41+
}
3942

4043
static async fromRpc<N extends Network>(
4144
connection: AptosClient,

platforms/cosmwasm/protocols/core/src/wormholeCore.ts platforms/cosmwasm/protocols/core/src/core.ts

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export class CosmwasmWormholeCore<N extends Network, C extends CosmwasmChains>
3737
this.coreAddress = coreAddress;
3838
}
3939

40+
getMessageFee(): Promise<bigint> {
41+
throw new Error("Method not implemented.");
42+
}
43+
4044
static async fromRpc<N extends Network>(
4145
rpc: CosmWasmClient,
4246
config: ChainsConfig<N, CosmwasmPlatformType>,

platforms/cosmwasm/protocols/core/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { registerProtocol } from "@wormhole-foundation/connect-sdk";
22
import { _platform } from "@wormhole-foundation/connect-sdk-cosmwasm";
3-
import { CosmwasmWormholeCore } from "./wormholeCore";
3+
import { CosmwasmWormholeCore } from "./core";
44

55
declare global {
66
namespace WormholeNamespace {
@@ -12,4 +12,4 @@ declare global {
1212

1313
registerProtocol(_platform, "WormholeCore", CosmwasmWormholeCore);
1414

15-
export * from "./wormholeCore";
15+
export * from "./core";

platforms/evm/protocols/core/src/wormholeCore.ts platforms/evm/protocols/core/src/core.ts

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ export class EvmWormholeCore<
6262
);
6363
}
6464

65+
async getMessageFee(): Promise<bigint> {
66+
return await this.core.messageFee.staticCall();
67+
}
68+
6569
static async fromRpc<N extends Network>(
6670
provider: Provider,
6771
config: ChainsConfig<N, EvmPlatformType>,

platforms/evm/protocols/core/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { registerProtocol } from '@wormhole-foundation/connect-sdk';
22
import { _platform } from '@wormhole-foundation/connect-sdk-evm';
3-
import { EvmWormholeCore } from './wormholeCore';
3+
import { EvmWormholeCore } from './core';
44

55
declare global {
66
namespace WormholeNamespace {
@@ -13,4 +13,4 @@ declare global {
1313
registerProtocol(_platform, 'WormholeCore', EvmWormholeCore);
1414

1515
export * as ethers_contracts from './ethers-contracts';
16-
export * from './wormholeCore';
16+
export * from './core';

platforms/solana/protocols/core/src/core.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ import {
3030
} from '@wormhole-foundation/connect-sdk';
3131
import { Wormhole as WormholeCoreContract } from './types';
3232
import {
33+
BridgeData,
34+
createBridgeFeeTransferInstruction,
3335
createPostMessageInstruction,
3436
createPostVaaInstruction,
3537
createReadOnlyWormholeProgramInterface,
3638
createVerifySignaturesInstructions,
3739
derivePostedVaaKey,
40+
getWormholeBridgeData,
3841
} from './utils';
3942

4043
const SOLANA_SEQ_LOG = 'Program log: Sequence: ';
@@ -45,6 +48,7 @@ export class SolanaWormholeCore<N extends Network, C extends SolanaChains>
4548
readonly chainId: ChainId;
4649
readonly coreBridge: Program<WormholeCoreContract>;
4750
readonly address: string;
51+
protected bridgeData?: BridgeData;
4852

4953
constructor(
5054
readonly network: N,
@@ -86,6 +90,17 @@ export class SolanaWormholeCore<N extends Network, C extends SolanaChains>
8690
);
8791
}
8892

93+
async getMessageFee(): Promise<bigint> {
94+
// cache lookups since this should not change frequently
95+
if (!this.bridgeData)
96+
this.bridgeData = await getWormholeBridgeData(
97+
this.connection,
98+
this.coreBridge.programId,
99+
);
100+
101+
return this.bridgeData.config.fee;
102+
}
103+
89104
async *publishMessage(
90105
sender: AnySolanaAddress,
91106
message: Uint8Array,
@@ -104,11 +119,18 @@ export class SolanaWormholeCore<N extends Network, C extends SolanaChains>
104119
consistencyLevel,
105120
);
106121

122+
const fee = await this.getMessageFee();
123+
const feeTransferIx = createBridgeFeeTransferInstruction(
124+
this.coreBridge.programId,
125+
payer,
126+
fee,
127+
);
128+
107129
const { blockhash } = await SolanaPlatform.latestBlock(this.connection);
108130
const transaction = new Transaction();
109131
transaction.recentBlockhash = blockhash;
110132
transaction.feePayer = payer;
111-
transaction.add(postMsgIx);
133+
transaction.add(feeTransferIx, postMsgIx);
112134
transaction.partialSign(messageAccount);
113135

114136
yield this.createUnsignedTx(transaction, 'Core.PublishMessage');

platforms/solana/protocols/core/src/utils/instructions/feeTransfer.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
import {
2-
Commitment,
3-
Connection,
42
PublicKey,
53
PublicKeyInitData,
64
SystemProgram,
75
TransactionInstruction,
86
} from '@solana/web3.js';
9-
import { deriveFeeCollectorKey, getWormholeBridgeData } from '../accounts';
7+
import { deriveFeeCollectorKey } from '../accounts';
108

11-
export async function createBridgeFeeTransferInstruction(
12-
connection: Connection,
9+
export function createBridgeFeeTransferInstruction(
1310
wormholeProgramId: PublicKeyInitData,
1411
payer: PublicKeyInitData,
15-
commitment?: Commitment,
16-
): Promise<TransactionInstruction> {
17-
const fee = await getWormholeBridgeData(
18-
connection,
19-
wormholeProgramId,
20-
commitment,
21-
).then((data) => data.config.fee);
12+
fee: bigint,
13+
): TransactionInstruction {
2214
return SystemProgram.transfer({
2315
fromPubkey: new PublicKey(payer),
2416
toPubkey: deriveFeeCollectorKey(wormholeProgramId),

platforms/solana/protocols/tokenBridge/src/tokenBridge.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ export class SolanaTokenBridge<N extends Network, C extends SolanaChains>
216216
// TODO: createNonce().readUInt32LE(0);
217217
const nonce = 0;
218218

219+
const msgFee = await this.coreBridge.getMessageFee();
219220
const transferIx = await coreUtils.createBridgeFeeTransferInstruction(
220-
this.connection,
221221
this.coreBridge.address,
222222
senderAddress,
223+
msgFee,
223224
);
224225
const messageKey = Keypair.generate();
225226
const attestIx = createAttestTokenInstruction(

0 commit comments

Comments
 (0)