Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1231 Solana client #1254

Merged
merged 22 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-apricots-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-solana': patch
---

First release version of the package
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Blockchain clients with whom you can prepare, make and broadcast transactions, e
| [@xchainjs/xchain-bsc](https://github.com/xchainjs/xchainjs-lib/tree/master/packages/xchain-bsc) | ✅ | ✅ | [![npm](https://img.shields.io/npm/v/@xchainjs/xchain-bsc)](https://www.npmjs.com/package/@xchainjs/xchain-bsc) |
| [@xchainjs/xchain-kujira](https://github.com/xchainjs/xchainjs-lib/tree/master/packages/xchain-kujira) | ✅ | ❌ | [![npm](https://img.shields.io/npm/v/@xchainjs/xchain-kujira)](https://www.npmjs.com/package/@xchainjs/xchain-kujira) |
| [@xchainjs/xchain-cosmos](https://github.com/xchainjs/xchainjs-lib/tree/master/packages/xchain-cosmos) | ✅ | ✅ | [![npm](https://img.shields.io/npm/v/@xchainjs/xchain-cosmos)](https://www.npmjs.com/package/@xchainjs/xchain-cosmos) |
| [@xchainjs/xchain-solana](https://github.com/xchainjs/xchainjs-lib/tree/master/packages/xchain-solana) | ✅ | ❌ | [![npm](https://img.shields.io/npm/v/@xchainjs/xchain-solana)](https://www.npmjs.com/package/@xchainjs/xchain-solana) |
| [@xchainjs/xchain-binance](https://github.com/xchainjs/xchainjs-lib/tree/master/packages/xchain-binance) | ✅ | ❌ | [![npm](https://img.shields.io/npm/v/@xchainjs/xchain-binance)](https://www.npmjs.com/package/@xchainjs/xchain-binance) |


Expand Down
10 changes: 10 additions & 0 deletions examples/solana/.codesandbox/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// These tasks will run in order when initializing your CodeSandbox project.
"setupTasks": [
{
"name": "Install Dependencies",
"command": "yarn install"
}
]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
}


5 changes: 5 additions & 0 deletions examples/solana/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Devcontainer",
"image": "ghcr.io/codesandbox/devcontainers/typescript-node:latest"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
}


2 changes: 2 additions & 0 deletions examples/solana/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# xchainjs-solana

59 changes: 59 additions & 0 deletions examples/solana/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Solana

Solana examples to show different use cases using the its client

## Examples

### Balances

#### Get all balances

Check out how you should get all balances an address owns in this [example](https://github.com/xchainjs/xchainjs-lib/blob/master/examples/solana/balances-all.ts) or run it as

```sh
yarn allBalances address
```

#### Get specific asset balance

Check out how you should get specific token balances an address owns in this [example](https://github.com/xchainjs/xchainjs-lib/blob/master/examples/solana/balance.ts) or run it as

```sh
yarn tokenBalance address token
```

### Addresses

#### Get address by index

Check out how you should get you account address at certain index in this [example](https://github.com/xchainjs/xchainjs-lib/blob/master/examples/solana/address.ts) or run it as

```sh
yarn address phrase index
```

### Transactions

#### Prepare transaction

Check out how you should prepare a transaction to be signed in this [example](https://github.com/xchainjs/xchainjs-lib/blob/master/examples/solana/address.ts) or run it as

```sh
yarn prepareTx sender recipient asset assetDecimals amount
```

#### Make transaction

Check out how you should make a Solana native asset transaction in this [example](https://github.com/xchainjs/xchainjs-lib/blob/master/examples/solana/address.ts) or run it as

```sh
yarn transfer phrase recipient amount
```

#### Make token transaction

Check out how you should make a Solana token transaction in this [example](https://github.com/xchainjs/xchainjs-lib/blob/master/examples/solana/address.ts) or run it as

```sh
yarn transferToken phrase recipient asset assetDecimals amount
```
15 changes: 15 additions & 0 deletions examples/solana/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Client, defaultSolanaParams } from '@xchainjs/xchain-solana'

const main = async () => {
const phrase = `${process.argv[2]}`
const index = process.argv[3] ? Number(process.argv[3]) : 0
const client = new Client({ ...defaultSolanaParams, phrase })

const address = await client.getAddressAsync(index)

console.log(`You account at index ${index} is ${address}`)
}

main()
.then(() => process.exit(0))
.catch((err) => console.error(err))
22 changes: 22 additions & 0 deletions examples/solana/balance-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Client } from '@xchainjs/xchain-solana'
import { assetToString, baseToAsset } from '@xchainjs/xchain-util'

const main = async () => {
const address = `${process.argv[2]}`
const client = new Client()

const balances = await client.getBalance(address)

console.log('---------------------------------------------------')
console.log(`${address} balances`)
console.log('---------------------------------------------------')
console.table(
balances.map((balance) => {
return { Asset: assetToString(balance.asset), Amount: baseToAsset(balance.amount).amount().toString() }
}),
)
}

main()
.then(() => process.exit(0))
.catch((err) => console.error(err))
23 changes: 23 additions & 0 deletions examples/solana/balance-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Client } from '@xchainjs/xchain-solana'
import { TokenAsset, assetFromStringEx, assetToString, baseToAsset } from '@xchainjs/xchain-util'

const main = async () => {
const address = `${process.argv[2]}`
const asset = assetFromStringEx(`${process.argv[3]}`) as TokenAsset
const client = new Client()

const balances = await client.getBalance(address, [asset])

console.log('---------------------------------------------------')
console.log(`${address} balances`)
console.log('---------------------------------------------------')
console.table(
balances.map((balance) => {
return { Asset: assetToString(balance.asset), Amount: baseToAsset(balance.amount).amount().toString() }
}),
)
}

main()
.then(() => process.exit(0))
.catch((err) => console.error(err))
26 changes: 26 additions & 0 deletions examples/solana/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "xchainjs-solana",
"private": true,
"version": "0.0.1",
"scripts": {
"allBalances": "npx ts-node balance-all.ts",
"tokenBalance": "npx ts-node balance-token.ts",
"address": "npx ts-node address.ts",
"prepareTx": "npx ts-node transaction-prepare.ts",
"transfer": "npx ts-node transaction-transfer.ts",
"transferToken": "npx ts-node transaction-transfer-token.ts",
"build": "tsc --noEmit"
},
"description": "Examples using Solana client",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@xchainjs/xchain-solana": "workspace:*",
"@xchainjs/xchain-util": "workspace:*"
},
"devDependencies": {
"@types/node": "20.11.28",
"ts-node": "10.9.2",
"typescript": "^5.0.4"
}
}
24 changes: 24 additions & 0 deletions examples/solana/transaction-prepare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Client } from '@xchainjs/xchain-solana'
import { Asset, TokenAsset, assetAmount, assetFromStringEx, assetToBase } from '@xchainjs/xchain-util'

const main = async () => {
const sender = `${process.argv[2]}`
const recipient = `${process.argv[3]}`
const asset = assetFromStringEx(`${process.argv[4]}`) as Asset | TokenAsset
const amount = assetAmount(`${process.argv[6]}`, Number(process.argv[5]))

const client = new Client()

const { rawUnsignedTx } = await client.prepareTx({
sender,
recipient,
asset,
amount: assetToBase(amount),
})

console.log(rawUnsignedTx)
}

main()
.then(() => process.exit(0))
.catch((err) => console.error(err))
29 changes: 29 additions & 0 deletions examples/solana/transaction-transfer-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Client, defaultSolanaParams } from '@xchainjs/xchain-solana'
import { Asset, TokenAsset, assetAmount, assetFromStringEx, assetToBase } from '@xchainjs/xchain-util'

const main = async () => {
const phrase = `${process.argv[2]}`
const recipient = `${process.argv[3]}`
const asset = assetFromStringEx(`${process.argv[4]}`) as Asset | TokenAsset
const amount = assetAmount(`${process.argv[6]}`, Number(process.argv[5]))

const client = new Client({
...defaultSolanaParams,
phrase,
})

const hash = await client.transfer({
recipient,
asset,
amount: assetToBase(amount),
})

console.log({
hash,
url: client.getExplorerTxUrl(hash),
})
}

main()
.then(() => process.exit(0))
.catch((err) => console.error(err))
27 changes: 27 additions & 0 deletions examples/solana/transaction-transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Client, SOL_DECIMALS, defaultSolanaParams } from '@xchainjs/xchain-solana'
import { assetAmount, assetToBase } from '@xchainjs/xchain-util'

const main = async () => {
const phrase = `${process.argv[2]}`
const recipient = `${process.argv[3]}`
const amount = assetAmount(`${process.argv[4]}`, SOL_DECIMALS)

const client = new Client({
...defaultSolanaParams,
phrase,
})

const hash = await client.transfer({
recipient,
amount: assetToBase(amount),
})

console.log({
hash,
url: client.getExplorerTxUrl(hash),
})
}

main()
.then(() => process.exit(0))
.catch((err) => console.error(err))
10 changes: 10 additions & 0 deletions examples/solana/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module":"commonjs",
"target": "es5",
"noEmitOnError": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": ["es6", "dom", "es2016", "es2017"]
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
}

1 change: 1 addition & 0 deletions packages/xchain-solana/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
75 changes: 75 additions & 0 deletions packages/xchain-solana/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<div align="center">
<h1 align="center">Solana client</h1>

<p align="center">
<a href='https://www.npmjs.com/package/@xchainjs/xchain-solana' target='_blank'>
<img alt="NPM Version" src="https://img.shields.io/npm/v/%40xchainjs%2Fxchain-solana" />
</a>
<a href='https://www.npmjs.com/package/@xchainjs/xchain-solana' target='_blank'>
<img alt="NPM Downloads" src="https://img.shields.io/npm/d18m/%40xchainjs%2Fxchain-solana" />
</a>
</p>
</div>

<br />

Client that allows to perform operations on the Solana blockchain abstracting developers from its particularities, thus allowing developers to focus on their projects. The Solana client is built on top of [@solana/web3.js](https://github.com/solana-labs/solana-web3.js) and the suite of packages developed by the [Metaplex](https://www.metaplex.com/) foundation.

If you want to read more about Solana blockchain, go to its official [web site](https://solana.com/)


## Installation

```sh
yarn add @xchainjs/xchain-solana
```
or

```sh
npm install @xchainjs/xchain-solana
```

## Initialization

Using the Solana client you can initialize the main class of the module in consultation mode if you do not provide any parameters, this means you could retrieve information from the blockchain and prepare transactions to sign, but you will not be able to sign transactions, or generate addresses.

```ts
import { Client } from '@xchainjs/xchain-solana'

const client = new Client()

// Make read operations with your client
```

Otherwise, if you want to sign transactions and get the addresses you own, you will need to initialize the main class of the protocol as follows

```ts
import { Client, defaultSolanaParams } from '@xchainjs/xchain-solana'

const client = new Client({
phrase: 'your secret phrase',
...defaultSolanaParams
})

// Make read or write operations with your client
```

## Features

Thanks to the Solana client you will be able to:
- Get the Solana and tokens balances that an address owns
- Generate addresses given a secret phrase
- Transfer Solana and tokens to another address
- Get details of a transaction
- Get address transaction history



## Examples

You can find examples using the Solana package in the [solana](https://github.com/xchainjs/xchainjs-lib/tree/master/examples/solana) examples folder.


## Documentation

More information about how to use the Solana client can be found on [documentation](https://xchainjs.gitbook.io/xchainjs/clients/xchain-solana)
Loading