-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(example): add starknet example
- Loading branch information
Showing
12 changed files
with
1,586 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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,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 | ||
``` |
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,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 | ||
} | ||
} | ||
} |
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,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" | ||
} | ||
} |
Oops, something went wrong.