Skip to content
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "4.0.0-beta.3",
"version": "4.0.0-alpha.22",
"private": true,
"sideEffects": false,
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-provider-bitcoin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lifi/sdk-provider-bitcoin",
"version": "4.0.0-beta.3",
"version": "4.0.0-alpha.22",
"description": "LI.FI Bitcoin SDK Provider for Any-to-Any Cross-Chain-Swap",
"homepage": "https://github.com/lifinance/sdk",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-provider-bitcoin/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const name = '@lifi/sdk-provider-bitcoin'
export const version = '4.0.0-beta.3'
export const version = '4.0.0-alpha.22'
2 changes: 1 addition & 1 deletion packages/sdk-provider-ethereum/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lifi/sdk-provider-ethereum",
"version": "4.0.0-beta.3",
"version": "4.0.0-alpha.22",
"description": "LI.FI Ethereum SDK Provider for Any-to-Any Cross-Chain-Swap",
"homepage": "https://github.com/lifinance/sdk",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-provider-ethereum/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const name = '@lifi/sdk-provider-ethereum'
export const version = '4.0.0-beta.3'
export const version = '4.0.0-alpha.22'
2 changes: 1 addition & 1 deletion packages/sdk-provider-solana/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lifi/sdk-provider-solana",
"version": "4.0.0-beta.3",
"version": "4.0.0-alpha.22",
"description": "LI.FI Solana SDK Provider for Any-to-Any Cross-Chain-Swap",
"homepage": "https://github.com/lifinance/sdk",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-provider-solana/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const name = '@lifi/sdk-provider-solana'
export const version = '4.0.0-beta.3'
export const version = '4.0.0-alpha.22'
6 changes: 3 additions & 3 deletions packages/sdk-provider-sui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lifi/sdk-provider-sui",
"version": "4.0.0-beta.3",
"version": "4.0.0-alpha.22",
"description": "LI.FI Sui SDK Provider for Any-to-Any Cross-Chain-Swap",
"homepage": "https://github.com/lifinance/sdk",
"bugs": {
Expand All @@ -15,15 +15,15 @@
"author": "Eugene Chybisov <eugene@li.finance>",
"type": "module",
"sideEffects": false,
"main": "./dist/cjs/index.js",
"main": "./dist/esm/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"typings": "./dist/esm/index.d.ts",
"exports": {
".": {
"types": "./dist/esm/index.d.ts",
"import": "./dist/esm/index.js",
"default": "./dist/cjs/index.js"
"default": "./dist/esm/index.js"
},
"./package.json": "./package.json"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-provider-sui/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const name = '@lifi/sdk-provider-sui'
export const version = '4.0.0-beta.3'
export const version = '4.0.0-alpha.22'
102 changes: 102 additions & 0 deletions packages/sdk-provider-tron/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"name": "@lifi/sdk-provider-tron",
"version": "4.0.0-alpha.22",
"description": "LI.FI Tron SDK Provider for Any-to-Any Cross-Chain-Swap",
"homepage": "https://github.com/lifinance/sdk",
"bugs": {
"url": "https://github.com/lifinance/sdk/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/lifinance/sdk.git",
"directory": "packages/sdk-provider-tron"
},
"license": "Apache-2.0",
"author": "Eugene Chybisov <eugene@li.finance>",
"type": "module",
"sideEffects": false,
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"typings": "./dist/types/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/esm/index.js",
"default": "./dist/cjs/index.js"
},
"./package.json": "./package.json"
},
"scripts": {
"build": "pnpm clean && pnpm build:version && pnpm build:cjs && pnpm build:esm && pnpm build:types && pnpm build:clean",
"build:cjs": "tsc --project ./tsconfig.json --module commonjs --outDir ./dist/cjs --removeComments --verbatimModuleSyntax false && mkdir -p ./dist/cjs && printf '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json",
"build:esm": "tsc --project ./tsconfig.json --outDir ./dist/esm && mkdir -p ./dist/esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/esm/package.json",
"build:types": "tsc --project ./tsconfig.json --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap",
"build:prerelease": "node ../../scripts/prerelease.js && cpy '../../*.md' .",
"build:postrelease": "node ../../scripts/postrelease.js && rm -rf *.md",
"build:clean": "rm -rf tsconfig.tsbuildinfo ./dist/tsconfig.tsbuildinfo",
"build:version": "node ../../scripts/version.js",
"clean": "pnpm build:clean && rm -rf dist",
"coverage": "vitest run --coverage",
"test": "vitest --run --dangerouslyIgnoreUnhandledErrors",
"test:cov": "pnpm test --coverage",
"test:unit": "pnpm test .unit.spec.ts --passWithNoTests",
"check:types": "tsc --noEmit",
"check:circular-deps": "madge --circular $(find ./src -name '*.ts')",
"check:circular-deps-graph": "madge --circular $(find ./src -name '*.ts') --image graph.svg",
"watch": "tsc -w -p ./tsconfig.json"
},
"dependencies": {
"@lifi/sdk": "workspace:*",
"@tronweb3/tronwallet-abstract-adapter": "^1.1.10",
"tronweb": "^6.0.4"
},
"devDependencies": {
"@vitest/coverage-v8": "^4.0.18",
"cpy-cli": "^7.0.0",
"madge": "^8.0.0",
"typescript": "^5.9.3",
"vitest": "^4.0.18"
},
"publishConfig": {
"access": "public"
},
"files": [
"dist/**",
"!dist/**/*.tsbuildinfo",
"src/**/*.ts",
"!src/**/*.spec.ts",
"!src/**/*.test.ts",
"!src/**/*.mock.ts",
"!src/**/*.spec.ts",
"!src/**/*.handlers.ts",
"!src/**/*.tsbuildinfo",
"!**/__mocks__/**",
"!*.tmp",
"!*.env",
"!tsconfig.json"
],
"keywords": [
"swap",
"bridge",
"bridge-aggregation",
"cross-chain",
"cross-chain-applications",
"cross-chain-bridge",
"dapp",
"defi",
"ethereum",
"bitcoin",
"solana",
"sui",
"tron",
"lifi",
"multi-chain",
"sdk",
"ethers",
"viem",
"wagmi",
"web3",
"web3-react"
]
}
48 changes: 48 additions & 0 deletions packages/sdk-provider-tron/src/TronProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
ChainType,
LiFiErrorCode,
ProviderError,
type StepExecutorOptions,
} from '@lifi/sdk'
import { TronWeb } from 'tronweb'
import { getTronBalance } from './actions/getTronBalance.js'
import { resolveTronAddress } from './actions/resolveTronAddress.js'
import { TronStepExecutor } from './core/TronStepExecutor.js'
import type { TronProviderOptions, TronSDKProvider } from './types.js'

export function TronProvider(options?: TronProviderOptions): TronSDKProvider {
const _options: TronProviderOptions = options ?? {}
return {
get type() {
return ChainType.TVM
},
isAddress: (address: string) => TronWeb.isAddress(address),
resolveAddress: resolveTronAddress,
getBalance: getTronBalance,
async getStepExecutor(
options: StepExecutorOptions
): Promise<TronStepExecutor> {
if (!_options.getWallet) {
throw new ProviderError(
LiFiErrorCode.ProviderUnavailable,
'TronProvider requires a getWallet function.'
)
}

const wallet = await _options.getWallet()

const executor = new TronStepExecutor({
wallet,
routeId: options.routeId,
executionOptions: {
...options.executionOptions,
},
})

return executor
},
setOptions(options: TronProviderOptions) {
Object.assign(_options, options)
},
}
}
83 changes: 83 additions & 0 deletions packages/sdk-provider-tron/src/actions/getTronBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { SDKClient, Token, TokenAmount } from '@lifi/sdk'
import { withDedupe } from '@lifi/sdk'
import { callTronRpcsWithRetry } from '../rpc/callTronRpcsWithRetry.js'
import { isZeroAddress } from '../utils/isZeroAddress.js'

export const getTronBalance = async (
client: SDKClient,
walletAddress: string,
tokens: Token[]
): Promise<TokenAmount[]> => {
if (tokens.length === 0) {
return []
}
const { chainId } = tokens[0]
for (const token of tokens) {
if (token.chainId !== chainId) {
console.warn('Requested tokens have to be on the same chain.')
}
}

return getTronBalanceDefault(client, tokens, walletAddress)
}

const getTronBalanceDefault = async (
client: SDKClient,
tokens: Token[],
walletAddress: string
): Promise<TokenAmount[]> => {
const [blockNumber, results] = await callTronRpcsWithRetry(
client,
async (tronWeb) => {
const host = tronWeb.fullNode.host
const queue: Promise<bigint>[] = tokens.map((token) => {
if (isZeroAddress(token.address)) {
return withDedupe(
async () => BigInt(await tronWeb.trx.getBalance(walletAddress)),
{ id: `${getTronBalanceDefault.name}.getBalance.${host}` }
)
}
return withDedupe(
async () => {
const contract = await tronWeb.contract().at(token.address)
const balance = await contract
.balanceOf(walletAddress)
.call({ from: walletAddress })
return BigInt(balance.toString())
},
{
id: `${getTronBalanceDefault.name}.balanceOf.${token.address}.${host}`,
}
)
})

return Promise.all([
withDedupe(
async () => {
const block = await tronWeb.trx.getCurrentBlock()
return BigInt(block.block_header?.raw_data?.number ?? 0)
},
{ id: `${getTronBalanceDefault.name}.getCurrentBlock.${host}` }
),
Promise.allSettled(queue),
])
}
)

const tokenAmounts: TokenAmount[] = tokens.map((token, index) => {
const result = results[index]
if (result.status === 'rejected') {
return {
...token,
blockNumber,
}
}
return {
...token,
amount: result.value,
blockNumber,
}
})

return tokenAmounts
}
4 changes: 4 additions & 0 deletions packages/sdk-provider-tron/src/actions/resolveTronAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function resolveTronAddress(name: string): Promise<string> {
// Tron does not have a name service, return the address as-is
return name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, expect, it } from 'vitest'
import { resolveTronAddress } from './resolveTronAddress.js'

describe('resolveTronAddress', () => {
it('should return the address as-is', async () => {
const address = 'TJRabPrwbZy45sbavfcjinPJC18kjpRTv8'

expect(await resolveTronAddress(address)).toBe(address)
})
})
Loading
Loading