-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #374 from hummingbot/solana-test
feat/Solana connector
- Loading branch information
Showing
33 changed files
with
4,709 additions
and
310 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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { TokenListType } from '../../services/base'; | ||
import { ConfigManagerV2 } from '../../services/config-manager-v2'; | ||
interface NetworkConfig { | ||
name: string; | ||
nodeURLs: string; | ||
tokenListType: TokenListType; | ||
tokenListSource: string; | ||
nativeCurrencySymbol: string; | ||
} | ||
|
||
export interface Config { | ||
network: NetworkConfig; | ||
tokenProgram: string; | ||
transactionLamports: number; | ||
lamportsToSol: number; | ||
timeToLive: number; | ||
} | ||
|
||
export function getSolanaConfig( | ||
chainName: string, | ||
networkName: string | ||
): Config { | ||
return { | ||
network: { | ||
name: networkName, | ||
nodeURLs: ConfigManagerV2.getInstance().get( | ||
chainName + '.networks.' + networkName + '.nodeURLs' | ||
), | ||
tokenListType: ConfigManagerV2.getInstance().get( | ||
chainName + '.networks.' + networkName + '.tokenListType' | ||
), | ||
tokenListSource: ConfigManagerV2.getInstance().get( | ||
chainName + '.networks.' + networkName + '.tokenListSource' | ||
), | ||
nativeCurrencySymbol: ConfigManagerV2.getInstance().get( | ||
chainName + '.networks.' + networkName + '.nativeCurrencySymbol' | ||
), | ||
}, | ||
tokenProgram: ConfigManagerV2.getInstance().get( | ||
chainName + '.tokenProgram' | ||
), | ||
transactionLamports: ConfigManagerV2.getInstance().get( | ||
chainName + '.transactionLamports' | ||
), | ||
lamportsToSol: ConfigManagerV2.getInstance().get( | ||
chainName + '.lamportsToSol' | ||
), | ||
timeToLive: ConfigManagerV2.getInstance().get(chainName + '.timeToLive'), | ||
}; | ||
} |
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,19 @@ | ||
export const constants = { | ||
retry: { | ||
all: { | ||
maxNumberOfRetries: 0, // 0 means no retries | ||
delayBetweenRetries: 0, // 0 means no delay (milliseconds) | ||
}, | ||
}, | ||
timeout: { | ||
all: 0, // 0 means no timeout (milliseconds) | ||
}, | ||
parallel: { | ||
all: { | ||
batchSize: 0, // 0 means no batching (group all) | ||
delayBetweenBatches: 0, // 0 means no delay (milliseconds) | ||
}, | ||
}, | ||
}; | ||
|
||
export default constants; |
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,75 @@ | ||
// import { tokenValueToString } from '../../services/base'; | ||
import { | ||
BalanceRequest, | ||
TokensRequest, | ||
PollRequest, | ||
} from '../../network/network.requests'; | ||
import { CustomTransactionResponse } from '../../services/common-interfaces'; | ||
import { | ||
HttpException, | ||
LOAD_WALLET_ERROR_CODE, | ||
LOAD_WALLET_ERROR_MESSAGE, | ||
} from '../../services/error-handler'; | ||
import { TokenInfo } from '../ethereum/ethereum-base'; | ||
|
||
import { Keypair, TransactionResponse } from '@solana/web3.js'; | ||
import { Solanaish } from './solana'; | ||
import { getNotNullOrThrowError } from './solana.helpers'; | ||
|
||
export class SolanaController { | ||
|
||
static async balances(solanaish: Solanaish, req: BalanceRequest) { | ||
let wallet: Keypair; | ||
try { | ||
wallet = await solanaish.getWallet(req.address); | ||
} catch (err) { | ||
throw new HttpException( | ||
500, | ||
LOAD_WALLET_ERROR_MESSAGE + err, | ||
LOAD_WALLET_ERROR_CODE | ||
); | ||
} | ||
|
||
const balances = await solanaish.getBalance(wallet, req.tokenSymbols); | ||
|
||
return { balances }; | ||
} | ||
|
||
static async poll(solanaish: Solanaish, req: PollRequest) { | ||
const currentBlock = await solanaish.getCurrentBlockNumber(); | ||
const txData = getNotNullOrThrowError<TransactionResponse>( | ||
await solanaish.getTransaction(req.txHash as any) | ||
); | ||
const txStatus = await solanaish.getTransactionStatusCode(txData); | ||
|
||
return { | ||
currentBlock: currentBlock, | ||
txHash: req.txHash, | ||
txBlock: txData.slot, | ||
txStatus: txStatus, | ||
txData: txData as unknown as CustomTransactionResponse | null, | ||
}; | ||
} | ||
|
||
static async getTokens(solanaish: Solanaish, req: TokensRequest) { | ||
let tokens: TokenInfo[] = []; | ||
|
||
if (!req.tokenSymbols) { | ||
tokens = solanaish.storedTokenList; | ||
} else { | ||
for (const symbol of req.tokenSymbols as string[]) { | ||
const token = solanaish.getTokenBySymbol(symbol); | ||
if (token) { | ||
tokens.push(token); | ||
} | ||
} | ||
} | ||
|
||
return { tokens }; | ||
} | ||
} | ||
|
||
export const balances = SolanaController.balances; | ||
export const poll = SolanaController.poll; | ||
export const getTokens = SolanaController.getTokens; | ||
export let priorityFeeMultiplier: number = 1; |
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,100 @@ | ||
import { default as constants } from './../../chains/solana/solana.constants'; | ||
|
||
/** | ||
* | ||
* @param value | ||
* @param errorMessage | ||
*/ | ||
export const getNotNullOrThrowError = <R>( | ||
value?: any, | ||
errorMessage: string = 'Value is null or undefined' | ||
): R => { | ||
if (value === undefined || value === null) throw new Error(errorMessage); | ||
|
||
return value as R; | ||
}; | ||
|
||
/** | ||
* | ||
* @param milliseconds | ||
*/ | ||
export const sleep = (milliseconds: number) => | ||
new Promise((callback) => setTimeout(callback, milliseconds)); | ||
|
||
/** | ||
* @param targetObject | ||
* @param targetFunction | ||
* @param targetParameters | ||
* @param maxNumberOfRetries 0 means no retries | ||
* @param delayBetweenRetries 0 means no delay (milliseconds) | ||
* @param timeout 0 means no timeout (milliseconds) | ||
* @param timeoutMessage | ||
*/ | ||
export const runWithRetryAndTimeout = async <R>( | ||
targetObject: any, | ||
targetFunction: (...args: any[]) => R, | ||
targetParameters: any, | ||
maxNumberOfRetries: number = constants.retry.all.maxNumberOfRetries, | ||
delayBetweenRetries: number = constants.retry.all.delayBetweenRetries, | ||
timeout: number = constants.timeout.all, | ||
timeoutMessage: string = 'Timeout exceeded.' | ||
): Promise<R> => { | ||
const errors = []; | ||
let retryCount = 0; | ||
let timer: any; | ||
|
||
if (timeout > 0) { | ||
timer = setTimeout(() => new Error(timeoutMessage), timeout); | ||
} | ||
|
||
do { | ||
try { | ||
const result = await targetFunction.apply(targetObject, targetParameters); | ||
|
||
if (timeout > 0) { | ||
clearTimeout(timer); | ||
} | ||
|
||
return result as R; | ||
} catch (error: any) { | ||
errors.push(error); | ||
|
||
retryCount++; | ||
|
||
console.debug( | ||
`${targetObject?.constructor.name || targetObject}:${ | ||
targetFunction.name | ||
} => retry ${retryCount} of ${maxNumberOfRetries}` | ||
); | ||
|
||
if (retryCount < maxNumberOfRetries) { | ||
if (delayBetweenRetries > 0) { | ||
await sleep(delayBetweenRetries); | ||
} | ||
} else { | ||
const allErrors = Error( | ||
`Failed to execute "${ | ||
targetFunction.name | ||
}" with ${maxNumberOfRetries} retries. All error messages were:\n${errors | ||
.map((error: any) => error.message) | ||
.join(';\n')}\n` | ||
); | ||
|
||
allErrors.stack = error.stack; | ||
|
||
throw allErrors; | ||
} | ||
} | ||
} while (retryCount < maxNumberOfRetries); | ||
|
||
throw Error('Unknown error.'); | ||
}; | ||
|
||
export function* splitInChunks<T>( | ||
target: T[], | ||
quantity: number | ||
): Generator<T[], void> { | ||
for (let i = 0; i < target.length; i += quantity) { | ||
yield target.slice(i, i + quantity); | ||
} | ||
} |
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,4 @@ | ||
export enum TransactionResponseStatusCode { | ||
FAILED = -1, | ||
CONFIRMED = 1, | ||
} |
Oops, something went wrong.