Skip to content

Commit

Permalink
Send to Jito /transactions and double down RPC endpoints (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipzeta authored May 16, 2024
1 parent 22a11ef commit 5fb4e54
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
Version changes are pinned to SDK releases.

## [1.31.2]

- Send to Jito transactions endpoint too. ([#398](https://github.com/zetamarkets/sdk/pull/398))

## [1.31.1]

- Add rndr, fix oracle loading with dummy wallet and revert some pyth changes for stale prices. ([#396](https://github.com/zetamarkets/sdk/pull/396))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zetamarkets/sdk",
"repository": "https://github.com/zetamarkets/sdk/",
"version": "1.31.1",
"version": "1.31.2",
"description": "Zeta SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ export const ACTIVE_MARKETS = ACTIVE_EXPIRIES * PRODUCTS_PER_EXPIRY + 1; // +1 f
export const TOTAL_EXPIRIES = 5;
export const TOTAL_MARKETS = PRODUCTS_PER_EXPIRY * (TOTAL_EXPIRIES + 1);
export const PERP_INDEX = TOTAL_MARKETS - 1;
export const ACTIVE_PERP_MARKETS = 15;
export const UNUSED_PERP_MARKETS = 10;
export const ACTIVE_PERP_MARKETS = 16;
export const UNUSED_PERP_MARKETS = 9;

export const DEFAULT_EXCHANGE_POLL_INTERVAL = 30;
export const DEFAULT_MARKET_POLL_INTERVAL = 5;
Expand Down
15 changes: 15 additions & 0 deletions src/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ export class Exchange {
}
private _jitoTip: number = 0;

// extra connection objects to send transactions down
public get doubleDownConnections(): Connection[] {
return this._doubleDownConnections;
}
public addDoubleDownConnection(connection: Connection) {
this._doubleDownConnections.push(connection);
}
private _doubleDownConnections: Connection[] = [];

public get useAutoPriorityFee(): boolean {
return this._useAutoPriorityFee;
}
Expand Down Expand Up @@ -592,6 +601,12 @@ export class Exchange {
this._httpProvider = bloxrouteHttpProvider;
}

if (loadConfig.doubleDownConnections) {
for (var con of loadConfig.doubleDownConnections) {
this.addDoubleDownConnection(con);
}
}

this._riskCalculator = new RiskCalculator(this.assets);

// Load variables from state.
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ export interface LoadExchangeConfig {
loadFromStore: boolean;
TIFBufferSeconds: number;
loadAssets?: Asset[];
doubleDownConnections?: Connection[];
}

export function defaultLoadExchangeConfig(
Expand Down
62 changes: 54 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,33 @@ export async function processTransactionJito(
throw Error(`Transaction ${txSig} was not confirmed`);
}

export async function sendRawTransactionCaught(con: Connection, rawTx: any) {
try {
let txSig = await con.sendRawTransaction(rawTx, {
skipPreflight: true,
preflightCommitment: con.commitment,
});
return txSig;
} catch (e) {
console.log(`Error sending tx: ${e}`);
}
}

export async function sendJitoTxCaught(payload: any) {
try {
let response = await axios.post(
"https://mainnet.block-engine.jito.wtf/api/v1/transactions",
payload,
{
headers: { "Content-Type": "application/json" },
}
);
return response.data.result;
} catch (e) {
console.log(`Error sending tx: ${e}`);
}
}

export async function processTransaction(
provider: anchor.AnchorProvider,
tx: Transaction,
Expand Down Expand Up @@ -1271,6 +1298,9 @@ export async function processTransaction(

let txOpts = opts || commitmentConfig(provider.connection.commitment);
let txSig;
let allConnections = [provider.connection].concat(
Exchange.doubleDownConnections
);
try {
// Integration tests don't like the split send + confirm :(
if (Exchange.network == Network.LOCALNET) {
Expand All @@ -1281,21 +1311,37 @@ export async function processTransaction(
);
}

txSig = await provider.connection.sendRawTransaction(rawTx, {
skipPreflight: true,
preflightCommitment: provider.connection.commitment,
});
let promises = [];
for (var con of allConnections) {
promises.push(sendRawTransactionCaught(con, rawTx));
}

// Jito's transactions endpoint, not a bundle
// Might as well send it here for extra success, it's free
if (Exchange.network == Network.MAINNET) {
const encodedTx = bs58.encode(rawTx);
const payload = {
jsonrpc: "2.0",
id: 1,
method: "sendTransaction",
params: [encodedTx],
};
promises.push(sendJitoTxCaught(payload));
}

// All tx sigs are the same
let txSig = await Promise.race(promises);

// Poll the tx confirmation for N seconds
// Polling is more reliable than websockets using confirmTransaction()
let currentBlockHeight = 0;
if (!Exchange.skipRpcConfirmation) {
while (currentBlockHeight < recentBlockhash.lastValidBlockHeight) {
// Keep resending to maximise the chance of confirmation
await provider.connection.sendRawTransaction(rawTx, {
skipPreflight: true,
preflightCommitment: provider.connection.commitment,
});
for (var con of allConnections) {
promises.push(sendRawTransactionCaught(con, rawTx));
}
await Promise.race(promises);

let status = await provider.connection.getSignatureStatus(txSig);
currentBlockHeight = await provider.connection.getBlockHeight(
Expand Down

0 comments on commit 5fb4e54

Please sign in to comment.