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

Improve TON Staker #14

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/ton/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@chorus-one/utils": "^1.0.0",
"@noble/curves": "^1.4.0",
"@ton/ton": "^13.11.2",
"axios": "^1.7.2",
"tonweb-mnemonic": "^1.0.1"
}
}
54 changes: 51 additions & 3 deletions packages/ton/src/TonBaseStaker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios, { AxiosAdapter, AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
import type { Signer } from '@chorus-one/signer'
import type {
TonNetworkConfig,
Expand All @@ -17,7 +18,9 @@ import {
MessageRelaxed,
SendMode,
Address,
TransactionDescriptionGeneric
TransactionDescriptionGeneric,
beginCell,
storeMessage
} from '@ton/ton'
import { createWalletTransferV4, externalMessage, sign } from './tx'
import * as tonMnemonic from 'tonweb-mnemonic'
Expand Down Expand Up @@ -133,8 +136,45 @@ export class TonBaseStaker {
* @returns A promise which resolves once the TonStaker instance has been initialized.
*/
async init (): Promise<void> {
const rateLimitRetryAdapter: AxiosAdapter = async (config: InternalAxiosRequestConfig): Promise<AxiosResponse> => {
const maxRetries = 3
const retryDelay = 1000

let retries = 0

const defaultAdapter = axios.getAdapter(axios.defaults.adapter)
if (!defaultAdapter) {
throw new Error('Axios default adapter is not available')
}

while (retries <= maxRetries) {
try {
// Send the request using the default adapter
return await defaultAdapter(config)
} catch (err) {
const error = err as AxiosError

const status = error.response?.status

// If rate limit hit (429), wait and retry
if (status === 429 && retries < maxRetries) {
retries += 1
console.log(`Rate limit hit, try ${retries}/${maxRetries}`)

await new Promise((resolve) => setTimeout(resolve, retryDelay))
} else {
throw error
}
}
}

// Should never reach this point
throw new Error(`Rate limit exceeded after ${maxRetries} retries.`)
}

this.client = new TonClient({
endpoint: this.networkConfig.rpcUrl
endpoint: this.networkConfig.rpcUrl,
httpAdapter: rateLimitRetryAdapter
})
}

Expand Down Expand Up @@ -381,8 +421,16 @@ export class TonBaseStaker {
const { address, txHash, limit } = params

const transactions = await client.getTransactions(Address.parse(address), { limit: limit ?? 10 })
const transaction = transactions.find((tx) => tx.hash().toString('hex') === txHash)
const transaction = transactions.find((tx) => {
// Check tx hash
if (tx.hash().toString('hex') === txHash) return true

// Check inMessage tx hash(that is the one we get from broadcast method)
if (tx.inMessage && beginCell().store(storeMessage(tx.inMessage)).endCell().hash().toString('hex') === txHash)
return true

return false
})
if (transaction === undefined) {
return { status: 'unknown', receipt: null }
}
Expand Down
Loading