Skip to content

Commit

Permalink
Merge pull request #376 from hummingbot/fix/solana-tx
Browse files Browse the repository at this point in the history
fix / solana tx costs
  • Loading branch information
cardosofede authored Dec 20, 2024
2 parents 6be2b2d + f86367e commit 033a0ed
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/amm/amm.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import {
price as jupiterPrice,
trade as jupiterTrade,
estimateGas as jupiterEstimateGas,
} from '../connectors/jupiter/jupiter.controllers';
import {
price as carbonPrice,
Expand Down Expand Up @@ -200,21 +201,23 @@ export async function estimateGas(
req: NetworkSelectionRequest
): Promise<EstimateGasResponse> {
const chain = await getInitializedChain<
Algorand | Ethereumish | Tezosish | Osmosis
Algorand | Ethereumish | Tezosish | Osmosis | Solana
>(req.chain, req.network);
if (chain instanceof Osmosis){
return chain.controller.estimateGas(chain as unknown as Osmosis);
}

const connector: Uniswapish | Tinyman | Plenty =
await getConnector<Uniswapish | Tinyman | Plenty>(
const connector: Uniswapish | Tinyman | Plenty | Jupiter =
await getConnector<Uniswapish | Tinyman | Plenty | Jupiter>(
req.chain,
req.network,
req.connector
);

if (connector instanceof Plenty) {
return plentyEstimateGas(<Tezosish>chain, connector);
} else if (connector instanceof Jupiter) {
return jupiterEstimateGas(<Solanaish>chain, connector);
} else if (connector instanceof Carbonamm) {
return carbonEstimateGas(<Ethereumish>chain, connector);
} else if ('routerAbi' in connector) {
Expand Down
4 changes: 4 additions & 0 deletions src/connectors/jupiter/jupiter.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ export namespace JupiterConfig {
tradingTypes: Array<string>;
chainType: string;
availableNetworks: Array<AvailableNetworks>;
gasLimitEstimate: number;
}

export const config: NetworkConfig = {
allowedSlippage: ConfigManagerV2.getInstance().get(
'jupiter.allowedSlippage',
),
gasLimitEstimate: ConfigManagerV2.getInstance().get(
'jupiter.gasLimitEstimate',
),
tradingTypes: ['AMM'],
chainType: 'SOLANA',
availableNetworks: [{ chain: 'solana', networks: ['mainnet-beta', 'devnet'] }],
Expand Down
21 changes: 20 additions & 1 deletion src/connectors/jupiter/jupiter.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PriceRequest,
TradeRequest,
TradeResponse,
EstimateGasResponse,
} from '../../amm/amm.requests';
import {
HttpException,
Expand Down Expand Up @@ -241,4 +242,22 @@ export async function trade(
expectedOut: swapResult.totalOutputSwapped.toString(),
};
}
}
}

export async function estimateGas(
solanaish: Solanaish,
jupiter: Jupiter,
): Promise<EstimateGasResponse> {
// TODO: get gas price from the network
const gasPrice: number = 0.00001
const gasLimit: number = jupiter.gasLimit;
const gasCost: string = (gasPrice * gasLimit).toString()
return {
network: solanaish.network,
timestamp: Date.now(),
gasPrice,
gasPriceToken: solanaish.nativeTokenSymbol,
gasLimit: gasLimit,
gasCost: gasCost,
};
}
14 changes: 2 additions & 12 deletions src/connectors/jupiter/jupiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,22 @@ import {
} from '@jup-ag/api';
import { JupiterConfig } from './jupiter.config';
import { percentRegexp } from '../../services/config-manager-v2';
// import { PriceRequest } from '../../amm/amm.requests';
import { Wallet } from '@coral-xyz/anchor';
import { priorityFeeMultiplier } from '../../chains/solana/solana.controllers';


// import axios from 'axios';
// import {
// JupiterQuoteResponse,
// SwapTransactionBuilderResponse,
// } from './jupiter.requests';
// import { latency } from '../../services/base';
// import Decimal from 'decimal.js-light';
// import { getPairData } from './jupiter.controllers';
// import { pow } from 'mathjs';
// import { Keypair, VersionedTransaction } from '@solana/web3.js';

export class Jupiter {
private static _instances: { [name: string]: Jupiter };
private chain: Solana;
private _ready: boolean = false;
private _config: JupiterConfig.NetworkConfig;
protected jupiterQuoteApi!: ReturnType<typeof createJupiterApiClient>;
public gasLimit: number = 0;

private constructor(network: string) {
this._config = JupiterConfig.config;
this.chain = Solana.getInstance(network);
this.gasLimit = JupiterConfig.config.gasLimitEstimate;
this.loadJupiter();
}

Expand Down
5 changes: 3 additions & 2 deletions src/services/schema/jupiter-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"allowedSlippage": { "type": "string" }
"allowedSlippage": { "type": "string" },
"gasLimitEstimate": { "type": "integer" }
},
"additionalProperties": false,
"required": ["allowedSlippage"]
"required": ["allowedSlippage", "gasLimitEstimate"]
}
3 changes: 3 additions & 0 deletions src/templates/jupiter.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# how much the execution price is allowed to move unfavorably from the trade
# execution price. It uses a rational number for precision.
allowedSlippage: '5/100'

# the transaction gas amount used to estimate gasCost for a jupiter trade.
gasLimitEstimate: 1

0 comments on commit 033a0ed

Please sign in to comment.