Skip to content

Commit

Permalink
Move gasdropoff calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex99y committed Jan 20, 2025
1 parent e27493a commit 23c1904
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
14 changes: 3 additions & 11 deletions connect/platforms/solana/src/automatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const NATIVE_MINT_UNIVERSAL = new SolanaAddress(

const KLAM_PER_SOL = 1_000_000n;
const MWEI_PER_MICRO_ETH = 1_000_000n;
const MICROTOKENS_PER_TOKEN = 1_000_000n;
const MWEI_PER_ETH = 1_000_000_000_000n;

export class AutomaticTokenBridgeV3Solana<N extends Network, C extends SolanaChains>
Expand Down Expand Up @@ -166,7 +165,7 @@ export class AutomaticTokenBridgeV3Solana<N extends Network, C extends SolanaCha
}

const senderPk = new PublicKey(params.sender.toNative('Solana').toString());
const gasDropoffAmountMicro = Number(sdkAmount.display(params.gasDropOff)) * Number(MICROTOKENS_PER_TOKEN);
const gasDropoffAmount = Number(sdkAmount.display(params.gasDropOff))

transaction.add(
await this.client.transferTokens(senderPk, {
Expand All @@ -176,7 +175,7 @@ export class AutomaticTokenBridgeV3Solana<N extends Network, C extends SolanaCha
},
userTokenAccount: ata,
transferredAmount: BigInt(params.amount.toString()),
gasDropoffAmount: gasDropoffAmountMicro,
gasDropoffAmount,
maxFeeLamports: BigInt(params.fee.toString() || 0),
unwrapIntent: params.unwrapIntent,
mintAddress: mint,
Expand Down Expand Up @@ -264,14 +263,7 @@ export class AutomaticTokenBridgeV3Solana<N extends Network, C extends SolanaCha
}

async baseRelayingParams(chain: SupportedChains): Promise<BaseRelayingParams> {
const config = await this.client.account.chainConfig(chain).fetch();

return {
maxGasDropoff: config.maxGasDropoffMicroToken / Number(MICROTOKENS_PER_TOKEN),
baseFee: config.relayerFeeMicroUsd,
paused: config.pausedOutboundTransfers,
canonicalPeer: new UniversalAddress(new Uint8Array(config.canonicalPeer)),
};
return this.client.baseRelayingParams(chain);
}

private mintAddress(token: TokenAddress<Chain>): PublicKey {
Expand Down
23 changes: 22 additions & 1 deletion sdk/solana/tbrv3/token-bridge-relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export interface WormholeAddress {
address: UniversalAddress;
}

interface BaseRelayingParams {
canonicalPeer: UniversalAddress;
paused: boolean;
maxGasDropoff: number;
baseFee: number;
}

/**
* @param recipient The address on another chain to transfer the tokens to.
* @param userTokenAccount The end user's account with the token to be transferred.
Expand All @@ -77,6 +84,8 @@ export type AuthBadgeAccount = anchor.IdlAccounts<IdlType>['authBadgeState'];

export type TbrNetwork = Exclude<Network, 'Devnet'> | 'Localnet';

const MICROTOKENS_PER_TOKEN = 1_000_000n;

/**
* Transforms a `UniversalAddress` into an array of numbers `number[]`.
*/
Expand Down Expand Up @@ -637,6 +646,7 @@ export class SolanaTokenBridgeRelayer {
const { address: temporaryAccount, bump: temporaryAccountBump } = this.account.temporary(
tokenBridgeAccounts.mint,
);
const gasDropoffAmountMicro = gasDropoffAmount * Number(MICROTOKENS_PER_TOKEN);

const accounts = {
payer: signer,
Expand Down Expand Up @@ -667,13 +677,24 @@ export class SolanaTokenBridgeRelayer {
uaToArray(recipient.address),
bigintToBn(transferredAmount),
unwrapIntent ?? false,
gasDropoffAmount,
gasDropoffAmountMicro,
bigintToBn(maxFeeLamports),
)
.accountsStrict(accounts)
.instruction();
}

async baseRelayingParams(chain: Chain): Promise<BaseRelayingParams> {
const config = await this.account.chainConfig(chain).fetch();

return {
maxGasDropoff: config.maxGasDropoffMicroToken / Number(MICROTOKENS_PER_TOKEN),
baseFee: config.relayerFeeMicroUsd,
paused: config.pausedOutboundTransfers,
canonicalPeer: new UniversalAddress(new Uint8Array(config.canonicalPeer)),
};
}

/**
* Signer: typically the relayer.
*
Expand Down

0 comments on commit 23c1904

Please sign in to comment.