Skip to content

Commit

Permalink
chore(example): add starknet example
Browse files Browse the repository at this point in the history
  • Loading branch information
RanGojo committed Sep 15, 2024
1 parent 5c07a1f commit 313c9aa
Show file tree
Hide file tree
Showing 12 changed files with 1,586 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/basic/node-starknet/.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-starknet/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 Starknet Transactions

### Requirements:

- rango-sdk-basic
- @rango-dev/signer-starknet
- starknet
- 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
```
103 changes: 103 additions & 0 deletions examples/basic/node-starknet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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 { DefaultStarknetSigner } from '@rango-dev/signer-starknet'
import { Account, RpcProvider } from 'starknet';

// setup wallet
const privateKey = 'YOUR_PRIVATE_KEY' // Replace with your private key
const walletAddress = 'YOUR_WALLET_ADDRESS' // Replace with your wallet address

// in web based apps, you could use injected provider instead
// e.g. use window.starknet_braavos or window.starknet_argentX intead
// https://starknetjs.com/docs/guides/connect_network
const provider = new RpcProvider({ nodeUrl: "https://starknet-mainnet.public.blastapi.io/rpc/v0_7" });
const account = new Account(provider, walletAddress, 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 = 'STARKNET'
const sourceTokenAddress = '0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7'
const targetBlockchain = 'STARKNET'
const targetTokenAddress = '0x4718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d'
const amount = '1000000000'

// 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.STARKNET) {
const defaultSigner = new DefaultStarknetSigner({ account, enable: () => { } })

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-starknet/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-sdk-basic": "^0.1.56",
"@rango-dev/signer-starknet": "^0.30.0",
"starknet": "^6.11.0"
},
"devDependencies": {
"tsx": "^4.17.0"
}
}
Loading

0 comments on commit 313c9aa

Please sign in to comment.