Skip to content

Commit

Permalink
Add fee method to core bridge (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin authored Jan 3, 2024
1 parent 33195de commit f383a43
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 22 deletions.
1 change: 1 addition & 0 deletions core/definitions/src/protocols/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface WormholeCore<
P extends Platform,
C extends PlatformToChains<P>,
> {
getMessageFee(): Promise<bigint>;
publishMessage(
sender: AccountAddress<C>,
message: string | Uint8Array,
Expand Down
8 changes: 4 additions & 4 deletions examples/src/messaging.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Wormhole, encoding, signSendWait } from "@wormhole-foundation/connect-sdk";
import { AlgorandPlatform } from "@wormhole-foundation/connect-sdk-algorand";
import { SolanaPlatform } from "@wormhole-foundation/connect-sdk-solana";
import { getStuff } from "./helpers";

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

(async function () {
const wh = new Wormhole("Testnet", [AlgorandPlatform]);
const wh = new Wormhole("Testnet", [SolanaPlatform]);

const chain = wh.getChain("Algorand");
const chain = wh.getChain("Solana");
const { signer, address } = await getStuff(chain);

// Get a reference to the core messaging bridge
Expand Down
3 changes: 3 additions & 0 deletions platforms/aptos/protocols/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class AptosWormholeCore<N extends Network, C extends AptosChains>
throw new Error(`CoreBridge contract Address for chain ${chain} not found`);
this.coreBridge = coreBridgeAddress;
}
getMessageFee(): Promise<bigint> {
throw new Error("Method not implemented.");
}

static async fromRpc<N extends Network>(
connection: AptosClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export class CosmwasmWormholeCore<N extends Network, C extends CosmwasmChains>
this.coreAddress = coreAddress;
}

getMessageFee(): Promise<bigint> {
throw new Error("Method not implemented.");
}

static async fromRpc<N extends Network>(
rpc: CosmWasmClient,
config: ChainsConfig<N, CosmwasmPlatformType>,
Expand Down
4 changes: 2 additions & 2 deletions platforms/cosmwasm/protocols/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { registerProtocol } from "@wormhole-foundation/connect-sdk";
import { _platform } from "@wormhole-foundation/connect-sdk-cosmwasm";
import { CosmwasmWormholeCore } from "./wormholeCore";
import { CosmwasmWormholeCore } from "./core";

declare global {
namespace WormholeNamespace {
Expand All @@ -12,4 +12,4 @@ declare global {

registerProtocol(_platform, "WormholeCore", CosmwasmWormholeCore);

export * from "./wormholeCore";
export * from "./core";
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export class EvmWormholeCore<
);
}

async getMessageFee(): Promise<bigint> {
return await this.core.messageFee.staticCall();
}

static async fromRpc<N extends Network>(
provider: Provider,
config: ChainsConfig<N, EvmPlatformType>,
Expand Down
4 changes: 2 additions & 2 deletions platforms/evm/protocols/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { registerProtocol } from '@wormhole-foundation/connect-sdk';
import { _platform } from '@wormhole-foundation/connect-sdk-evm';
import { EvmWormholeCore } from './wormholeCore';
import { EvmWormholeCore } from './core';

declare global {
namespace WormholeNamespace {
Expand All @@ -13,4 +13,4 @@ declare global {
registerProtocol(_platform, 'WormholeCore', EvmWormholeCore);

export * as ethers_contracts from './ethers-contracts';
export * from './wormholeCore';
export * from './core';
24 changes: 23 additions & 1 deletion platforms/solana/protocols/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ import {
} from '@wormhole-foundation/connect-sdk';
import { Wormhole as WormholeCoreContract } from './types';
import {
BridgeData,
createBridgeFeeTransferInstruction,
createPostMessageInstruction,
createPostVaaInstruction,
createReadOnlyWormholeProgramInterface,
createVerifySignaturesInstructions,
derivePostedVaaKey,
getWormholeBridgeData,
} from './utils';

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

constructor(
readonly network: N,
Expand Down Expand Up @@ -86,6 +90,17 @@ export class SolanaWormholeCore<N extends Network, C extends SolanaChains>
);
}

async getMessageFee(): Promise<bigint> {
// cache lookups since this should not change frequently
if (!this.bridgeData)
this.bridgeData = await getWormholeBridgeData(
this.connection,
this.coreBridge.programId,
);

return this.bridgeData.config.fee;
}

async *publishMessage(
sender: AnySolanaAddress,
message: Uint8Array,
Expand All @@ -104,11 +119,18 @@ export class SolanaWormholeCore<N extends Network, C extends SolanaChains>
consistencyLevel,
);

const fee = await this.getMessageFee();
const feeTransferIx = createBridgeFeeTransferInstruction(
this.coreBridge.programId,
payer,
fee,
);

const { blockhash } = await SolanaPlatform.latestBlock(this.connection);
const transaction = new Transaction();
transaction.recentBlockhash = blockhash;
transaction.feePayer = payer;
transaction.add(postMsgIx);
transaction.add(feeTransferIx, postMsgIx);
transaction.partialSign(messageAccount);

yield this.createUnsignedTx(transaction, 'Core.PublishMessage');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import {
Commitment,
Connection,
PublicKey,
PublicKeyInitData,
SystemProgram,
TransactionInstruction,
} from '@solana/web3.js';
import { deriveFeeCollectorKey, getWormholeBridgeData } from '../accounts';
import { deriveFeeCollectorKey } from '../accounts';

export async function createBridgeFeeTransferInstruction(
connection: Connection,
export function createBridgeFeeTransferInstruction(
wormholeProgramId: PublicKeyInitData,
payer: PublicKeyInitData,
commitment?: Commitment,
): Promise<TransactionInstruction> {
const fee = await getWormholeBridgeData(
connection,
wormholeProgramId,
commitment,
).then((data) => data.config.fee);
fee: bigint,
): TransactionInstruction {
return SystemProgram.transfer({
fromPubkey: new PublicKey(payer),
toPubkey: deriveFeeCollectorKey(wormholeProgramId),
Expand Down
3 changes: 2 additions & 1 deletion platforms/solana/protocols/tokenBridge/src/tokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@ export class SolanaTokenBridge<N extends Network, C extends SolanaChains>
// TODO: createNonce().readUInt32LE(0);
const nonce = 0;

const msgFee = await this.coreBridge.getMessageFee();
const transferIx = await coreUtils.createBridgeFeeTransferInstruction(
this.connection,
this.coreBridge.address,
senderAddress,
msgFee,
);
const messageKey = Keypair.generate();
const attestIx = createAttestTokenInstruction(
Expand Down

0 comments on commit f383a43

Please sign in to comment.