Skip to content

Commit

Permalink
refactor(example): restructure exmaples
Browse files Browse the repository at this point in the history
  • Loading branch information
RanGojo committed Aug 13, 2024
1 parent e12107e commit 048ed7b
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
publish-packages:
if: |
!contains(github.event.head_commit.message , 'ci(release)')
&& !contains(github.event.head_commit.message , 'sdk-example')
&& !contains(github.event.head_commit.message , 'example')
name: Publish Packages
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions examples/basic/node-evm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### How to run the code?

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

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

import { RangoClient, TransactionStatus, TransactionType } from "rango-sdk-basic";
import { findToken } from './utils/meta.js'
import { logMeta, logSelectedTokens, logQuote, logWallet, logSwap, logSwapStatus, logTransactionHash, logApprovalResponse } from "./utils/logger.js";
import { findToken } from '../shared/utils/meta.js'
import { logMeta, logSelectedTokens, logQuote, logWallet, logSwap, logSwapStatus, logTransactionHash, logApprovalResponse } from "../shared/utils/logger.js";
import { TransactionRequest, ethers } from "ethers";
import { setTimeout } from 'timers/promises'

Expand All @@ -12,7 +12,7 @@ const privateKey = 'YOUR_PRIVATE_KEY';
const wallet = new ethers.Wallet(privateKey);
const rpcProvider = new ethers.JsonRpcProvider('https://bsc-dataseed1.defibit.io');
const walletWithProvider = wallet.connect(rpcProvider);
logWallet(walletWithProvider)
logWallet(walletWithProvider.addresslogWallet)

// initiate sdk using your api key
const API_KEY = "c6381a79-2817-4602-83bf-6a641a409e32"
Expand Down
4 changes: 3 additions & 1 deletion examples/basic/node-solana/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Node.js Example for integrating Rango-SDK-Basic for EVM Transactions
## Node.js Example for integrating Rango-SDK-Basic for Solana Transactions

### Requirements:

Expand All @@ -10,6 +10,8 @@

### How to run the code?

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

```sh
cd /path/to/example/
yarn
Expand Down
21 changes: 10 additions & 11 deletions examples/basic/node-solana/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
TransactionStatus,
TransactionType,
} from 'rango-sdk-basic'
import { findToken } from './utils/meta.js'
import { findToken } from '../shared/utils/meta.js'
import {
logMeta,
logSelectedTokens,
Expand All @@ -14,10 +14,10 @@ import {
logSwap,
logSwapStatus,
logTransactionHash,
} from './utils/logger.js'
} from '../shared/utils/logger.js'
import { setTimeout } from 'timers/promises'
import bs58 from 'bs58'
import { Keypair, Transaction, VersionedTransaction } from '@solana/web3.js'
import { Keypair, PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js'
import {
DefaultSolanaSigner,
setSolanaSignerConfig,
Expand All @@ -29,27 +29,26 @@ const privateKey = bs58.decode(base58PrivateKey)
const keypair = Keypair.fromSecretKey(privateKey)
const walletAddress = keypair.publicKey.toString()

// in web based apps, you could use injected provider instead
// e.g. use window.phantom.solana intead of SolanaProvider
class SolanaProvider {
public publicKey?: { toBytes(): Uint8Array; toString(): string }
public publicKey?: PublicKey
private keypair: Keypair

constructor(keypair: Keypair) {
this.keypair = keypair
this.publicKey = {
toBytes: () => this.keypair.publicKey.toBytes(),
toString: () => this.keypair.publicKey.toString(),
}
this.publicKey = keypair.publicKey
}

async signTransaction(transaction: VersionedTransaction | Transaction) {
if (transaction instanceof VersionedTransaction) transaction.sign([keypair])
else transaction.sign(keypair)
if (transaction instanceof VersionedTransaction) transaction.sign([this.keypair])
else transaction.sign(this.keypair)
return transaction
}
}
const solana = new SolanaProvider(keypair)

logWallet(keypair)
logWallet(walletAddress)

// initiate sdk using your api key
const API_KEY = 'c6381a79-2817-4602-83bf-6a641a409e32'
Expand Down
72 changes: 0 additions & 72 deletions examples/basic/node-solana/utils/logger.ts

This file was deleted.

11 changes: 0 additions & 11 deletions examples/basic/node-solana/utils/meta.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Wallet } from "ethers"
import { MetaResponse, QuoteResponse, RoutingResultType, StatusResponse, SwapResponse, Token, TransactionStatus, TransactionType } from "rango-sdk-basic"

export function logMeta(meta: MetaResponse) {
Expand All @@ -23,8 +22,8 @@ export function logQuote(quote: QuoteResponse) {
}
}

export function logWallet(walletWithProvider: Wallet) {
console.log(`- connected to walelt address: ${walletWithProvider.address}`)
export function logWallet(address: string) {
console.log(`- connected to walelt address: ${address}`)
}

export function logSwap(swap: SwapResponse) {
Expand Down
File renamed without changes.
12 changes: 5 additions & 7 deletions packages/rango-sdk-basic/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Rango Exchange SDK

- [Basic-SDK Integration Tutorial](https://docs.rango.exchange/api-integration/basic-api-single-step/tutorial/sdk-example)
- [Basic-SDK Reference](https://docs.rango.exchange/api-integration/basic-api-single-step/api-reference)

## Basic SDK (Single Step Tx)

[![npm version](https://badge.fury.io/js/rango-sdk-basic.svg)](https://badge.fury.io/js/rango-sdk-basic)
Expand All @@ -18,7 +15,8 @@

## Usage

Please checkout the examples' folder for sample usage of the SDK. We will add more examples there soon.

- [Documents](https://docs.rango.exchange/api-integration/basic-api-single-step/tutorial/sdk-example)
- [Examples](https://github.com/rango-exchange/rango-sdk/tree/master/examples/basic)
- [Basic-SDK Integration Tutorial](https://docs.rango.exchange/api-integration/basic-api-single-step/tutorial/sdk-example)
- [Basic-SDK Reference](https://docs.rango.exchange/api-integration/basic-api-single-step/api-reference)
- Examples
- [EVM Example](https://github.com/rango-exchange/rango-sdk/tree/master/examples/basic/node-evm/)
- [Solana Example](https://github.com/rango-exchange/rango-sdk/tree/master/examples/basic/node-solana/)

1 comment on commit 048ed7b

@axelcazorla
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

top !bravo

Please sign in to comment.