Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer native SOL #22

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions src/clients/solana.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BN, Program, type Provider } from "@coral-xyz/anchor"
import type { MethodsBuilder } from "@coral-xyz/anchor/dist/cjs/program/namespace/methods"
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token"
import {
Keypair,
Expand Down Expand Up @@ -234,17 +235,44 @@ export class SolanaBridgeClient {
if (!payerPubKey) {
throw new Error("Payer is not configured")
}

const mint = new PublicKey(transfer.tokenAddress.split(":")[1])
const [from] = PublicKey.findProgramAddressSync(
[payerPubKey.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), mint.toBuffer()],
ASSOCIATED_TOKEN_PROGRAM_ID,
)
const vault = (await this.isBridgedToken(mint)) ? null : this.vaultId(mint)[0]
const [solVault] = this.solVaultId()

try {
const tx = await this.program.methods
// biome-ignore lint/suspicious/noExplicitAny: initTransfer or initTransferSol
let method: MethodsBuilder<BridgeTokenFactory, any, any>
if (transfer.tokenAddress === `sol:${PublicKey.default.toBase58()}`) {
method = this.program.methods
.initTransferSol({
amount: new BN(transfer.amount.valueOf()),
recipient: transfer.recipient,
fee: new BN(transfer.fee.valueOf()),
nativeFee: new BN(transfer.nativeFee.valueOf()),
})
.accountsStrict({
authority: this.authority()[0],
solVault,
user: payerPubKey,
wormhole: {
payer: payerPubKey,
config: this.config()[0],
bridge: this.wormholeBridgeId()[0],
feeCollector: this.wormholeFeeCollectorId()[0],
sequence: this.wormholeSequenceId()[0],
clock: SYSVAR_CLOCK_PUBKEY,
rent: SYSVAR_RENT_PUBKEY,
systemProgram: SystemProgram.programId,
wormholeProgram: this.wormholeProgramId,
message: wormholeMessage.publicKey,
},
})
} else {
const mint = new PublicKey(transfer.tokenAddress.split(":")[1])
const [from] = PublicKey.findProgramAddressSync(
[payerPubKey.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), mint.toBuffer()],
ASSOCIATED_TOKEN_PROGRAM_ID,
)
const vault = (await this.isBridgedToken(mint)) ? null : this.vaultId(mint)[0]

method = this.program.methods
.initTransfer({
amount: new BN(transfer.amount.valueOf()),
recipient: transfer.recipient,
Expand Down Expand Up @@ -272,6 +300,10 @@ export class SolanaBridgeClient {
},
tokenProgram: TOKEN_PROGRAM_ID,
})
}

try {
const tx = await method
.signers(payer instanceof Keypair ? [wormholeMessage, payer] : [wormholeMessage])
.rpc()

Expand Down
Loading