-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
467 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./evm"; | ||
export * from "./cosmos"; | ||
export * from "./solana"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Wallet, web3 } from "@project-serum/anchor"; | ||
import { Connection, VersionedTransaction } from "@solana/web3.js"; | ||
import { ExecuteRoute, SolanaSigner, SolanaTxResponse } from "types"; | ||
|
||
export class SolanaHandler { | ||
async executeRoute({ data }: { data: ExecuteRoute }): Promise<SolanaTxResponse> { | ||
const { route } = data; | ||
const signer = data.signer as SolanaSigner; | ||
|
||
const connection = new Connection(web3.clusterApiUrl("mainnet-beta"), "confirmed"); | ||
|
||
// currently we support signing only for Jupiter | ||
const swapRequest = route.transactionRequest!.data; | ||
|
||
// build tx object | ||
const swapTransactionBuf = Buffer.from(swapRequest, "base64"); | ||
let transaction = VersionedTransaction.deserialize(swapTransactionBuf); | ||
|
||
let tx: string; | ||
|
||
// sign tx using provided signer | ||
if (signer instanceof Wallet || signer.constructor.name === "NodeWallet") { | ||
// offline signer | ||
const wallet = signer as Wallet; | ||
transaction.sign([wallet.payer]); | ||
|
||
// send tx onchain | ||
const rawTransaction = transaction.serialize(); | ||
const txid = await connection.sendRawTransaction(rawTransaction, { | ||
skipPreflight: true, | ||
maxRetries: 2, | ||
}); | ||
|
||
// verify tx was broadcasted | ||
const latestBlockHash = await connection.getLatestBlockhash(); | ||
await connection.confirmTransaction({ | ||
blockhash: latestBlockHash.blockhash, | ||
lastValidBlockHeight: latestBlockHash.lastValidBlockHeight + 10, | ||
signature: txid, | ||
}); | ||
|
||
tx = txid; | ||
} else { | ||
// phantom wallet signer | ||
const { signature } = signer.signAndSendTransaction(transaction); | ||
await connection.getSignatureStatus(signature, { searchTransactionHistory: true }); | ||
|
||
tx = signature; | ||
} | ||
|
||
return { tx }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.