Skip to content

Commit

Permalink
chore(example): add tron example
Browse files Browse the repository at this point in the history
  • Loading branch information
RanGojo committed Sep 15, 2024
1 parent ab0972b commit 5c07a1f
Show file tree
Hide file tree
Showing 13 changed files with 1,166 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/basic/node-tron/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
18 changes: 18 additions & 0 deletions examples/basic/node-tron/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Node.js Example for integrating Rango-SDK-Basic for Tron Transactions

### Requirements:

- rango-sdk-basic
- @rango-dev/signer-tron
- tronweb@beta
- Node.js >= 20

### How to run the code?

Set up your wallet in index.ts, then run following codes:

```sh
cd /path/to/example/
yarn
node --import=tsx index.ts
```
102 changes: 102 additions & 0 deletions examples/basic/node-tron/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// run `node --import=tsx index.ts` in the terminal

import {
RangoClient,
TransactionStatus,
TransactionType,
} from 'rango-sdk-basic'
import { findToken } from '../shared/utils/meta.js'
import {
logMeta,
logSelectedTokens,
logQuote,
logWallet,
logSwap,
logSwapStatus,
logTransactionHash,
} from '../shared/utils/logger.js'
import { setTimeout } from 'timers/promises'
import { DefaultTronSigner } from '@rango-dev/signer-tron'
import { TronWeb } from 'tronweb'

// setup wallet and tron web
const privateKey = 'YOUR_PRIVATE_KEY' // Replace with your private key

// in web based apps, you could use injected provider instead
// e.g. use window.tronLink.tronWeb or ... intead
const TRON_API = 'https://api.trongrid.io'
const tronWeb = new TronWeb(TRON_API, TRON_API, TRON_API, privateKey)
const walletAddress = tronWeb.address.fromPrivateKey(privateKey) || ''

logWallet(walletAddress)

// initiate sdk using your api key
const API_KEY = 'c6381a79-2817-4602-83bf-6a641a409e32'
const rango = new RangoClient(API_KEY)

// get blockchains and tokens meta data
const meta = await rango.meta()
logMeta(meta)

// some example tokens for test purpose
const sourceBlockchain = 'TRON'
const sourceTokenAddress = null
const targetBlockchain = 'TRON'
const targetTokenAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
const amount = '1000' // 0.001 TRX

// find selected tokens in meta.tokens
const sourceToken = findToken(meta.tokens, sourceBlockchain, sourceTokenAddress)
const targetToken = findToken(meta.tokens, targetBlockchain, targetTokenAddress)
logSelectedTokens(sourceToken, targetToken)

// get quote
const quoteRequest = {
from: sourceToken,
to: targetToken,
amount,
slippage: 1.0,
}
const quote = await rango.quote(quoteRequest)
logQuote(quote)

const swapRequest = {
...quoteRequest,
fromAddress: walletAddress,
toAddress: walletAddress,
}

// create transaction
const swap = await rango.swap(swapRequest)
logSwap(swap)

const tx = swap.tx

if (!tx) {
throw new Error(`Error creating the transaction ${swap.error}`)
}

if (tx.type === TransactionType.TRON) {
const defaultSigner = new DefaultTronSigner({ tronWeb })

const { hash } = await defaultSigner.signAndSendTx(tx)
logTransactionHash(hash, false)

// track swap status
while (true) {
await setTimeout(10_000)
const state = await rango.status({
requestId: swap.requestId,
txId: hash,
})
logSwapStatus(state)

const status = state.status
if (
status &&
[TransactionStatus.FAILED, TransactionStatus.SUCCESS].includes(status)
) {
break
}
}
}
16 changes: 16 additions & 0 deletions examples/basic/node-tron/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "node-starter",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"type": "module",
"dependencies": {
"@rango-dev/signer-tron": "^0.29.0",
"rango-sdk-basic": "^0.1.56",
"tronweb": "^6.0.0-beta.4"
},
"devDependencies": {
"tsx": "^4.17.0"
}
}
Loading

0 comments on commit 5c07a1f

Please sign in to comment.