Skip to content

Commit

Permalink
fix: the sdk node example (#189)
Browse files Browse the repository at this point in the history
Co-authored-by: Eugene Chybisov <[email protected]>
  • Loading branch information
DNR500 and chybisov committed Jun 10, 2024
1 parent dffdcc2 commit bab34fc
Show file tree
Hide file tree
Showing 23 changed files with 5,561 additions and 1,939 deletions.
7 changes: 7 additions & 0 deletions examples/node/.env-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# You should be able to obtain your private key from the account details section
# from your wallet
#
# NOTE: the private key is a Hex type and should being with 0x - some wallets may
# not include this at the beginning of your key when you try to obtain it

PRIVATE_KEY="your-private-key-here"
138 changes: 31 additions & 107 deletions examples/node/README.md
Original file line number Diff line number Diff line change
@@ -1,118 +1,42 @@
# LI.FI SDK - Node Demo
# LI.FI SDK - Node Demo Examples

The demo of [our SDK](https://github.com/lifinance/sdk) executes a simple cross chain transfer of 1 USDT from Polygon to xDai using the best bridge it can find.
This project presents a number of demos for [our SDK](https://github.com/lifinance/sdk) that you can run from the command line.

It needs access to an actual wallet and makes real transactions on chain.
They use our SDK, along with [viem](https://viem.sh/), to execute common work flows in node.js

1. First you need to specify your `MNEMONIC` in your environment, e.g.
These scripts need to be provided access to an actual wallet and will make real transactions on chains.

```bash
export MNEMONIC="..."
```
To run these examples you will need a private key for your wallet and enough funds for the tokens and chains that these scripts use.

2. Execute the script.
Take a look at the scripts in the `./examples` folder.

Either the TypeScript version:
## Setting up the scripts to use your wallet

```bash
ts-node index.ts
```
- First you will need to obtain the private key for your wallet
- Then duplicate the `.env-template` file renaming it to `.env`
- Add your private key to your `.env` file - replacing the text in quote marks with your private key

Or the Javascript version:
NOTE: it's important to keep your private key safe and secure. Don't share it with anyone and make sure you never commit it to a git repository.

```bash
node index.js
```
## Executing the scripts

3. Sit back, relax and watch the show.

First a route is searched and the script prints out what it found. The property `toAmount` will tell you how much USDT will end up on xDAI.
Then the SDK will execute all necessary steps to do the transfer (approval, send Transaction, wait the bridge, claim, ... ). It prints out status updates for each of these steps

Sample output:

```bash
{
route: {
id: '0xcae9da9a53573ee1b5f81f6fe7ebfcb49945a082455fa2be3662b44434cd156b',
fromChainId: 137,
fromAmountUSD: '1.00',
fromAmount: '1000000',
fromToken: {
id: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f',
symbol: 'USDT',
decimals: 6,
chainId: 137,
name: '(PoS) Tether USD',
chainKey: 'pol',
key: 'USDT',
priceUSD: '1',
logoURI: 'https://static.debank.com/image/matic_token/logo_url/0xc2132d05d31c914a87c6611c10748aeb04b58e8f/66eadee7b7bb16b75e02b570ab8d5c01.png'
},
toChainId: 100,
toAmountUSD: '0.85',
toAmount: '851698',
toAmountMin: '851698',
toToken: {
id: '0x4ecaba5870353805a9f068101a40e0f32ed605c6',
symbol: 'USDT',
decimals: 6,
chainId: 100,
name: 'Tether USD on xDai',
chainKey: 'dai',
key: 'USDT',
priceUSD: '1',
logoURI: 'https://static.debank.com/image/xdai_token/logo_url/0x4ecaba5870353805a9f068101a40e0f32ed605c6/66eadee7b7bb16b75e02b570ab8d5c01.png'
},
gasCostUSD: '0.01',
steps: [ [Object] ]
}
}

{ status: 'NOT_STARTED', process: [] }
{
status: 'PENDING',
process: [
{
id: 'allowanceProcess',
startedAt: 1638960397132,
message: 'Set Allowance for USDT',
status: 'PENDING'
}
]
}
...
{
status: 'DONE',
process: [
{
id: 'allowanceProcess',
startedAt: 1638960397132,
message: 'Already Approved',
status: 'DONE',
doneAt: 1638960397438
},
{
id: 'crossProcess',
startedAt: 1638960397438,
message: 'Transfer started: ',
status: 'DONE',
txHash: '0xfc8f43109ccdd7ea6446b770e99b0e3e449ebb13b0d07aae3553bb10994ac24d',
txLink: 'https://polygonscan.com/tx/0xfc8f43109ccdd7ea6446b770e99b0e3e449ebb13b0d07aae3553bb10994ac24d',
doneAt: 1638960430378
},
{
id: 'claimProcess',
startedAt: 1638960430378,
message: 'Swapped:',
status: 'DONE',
txHash: '0x55b2472502a6e7768eea47add832d06f5c7083ff2a808e5648985197bd84ed04',
txLink: 'https://blockscout.com/xdai/mainnet/tx/0x55b2472502a6e7768eea47add832d06f5c7083ff2a808e5648985197bd84ed04',
doneAt: 1638960852668
}
],
fromAmount: '1000000',
toAmount: '851698'
}
DONE
First install the dependencies

```
yarn install
```

Each of the scripts is referenced in the scripts section of the package.json file.
To run each example you can use the following commands

- `yarn example:swap` will run `examples/swap.ts`
- `yarn example:bridge` will run `examples/bridge.ts`
- `yarn example:multihop` will run `examples/multihop.ts`
- `yarn example:klima` will run `examples/klimaRetireExactCarbon.ts`
- `yarn example:polynomial` will run `examples/polynomialDeposit.ts`

Be sure to take a look inside the scripts to find out what they do.




105 changes: 0 additions & 105 deletions examples/node/contractCalls/klimaRetireExactCarbon.ts

This file was deleted.

87 changes: 0 additions & 87 deletions examples/node/contractCalls/multihop.ts

This file was deleted.

Loading

0 comments on commit bab34fc

Please sign in to comment.