Skip to content

Commit 63888d9

Browse files
committed
adding a first version of the radix client
1 parent 8e82dc3 commit 63888d9

File tree

9 files changed

+356
-0
lines changed

9 files changed

+356
-0
lines changed

packages/xchain-radix/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `@xchainjs/xchain-client`
2+
3+
## Modules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Network } from '@xchainjs/xchain-client/src'
2+
import { RadixClient } from '@xchainjs/xchain-radix/src'
3+
4+
describe('RadixClient Test', () => {
5+
let radixClient: RadixClient
6+
const phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
7+
const params = {
8+
network: Network.Mainnet,
9+
}
10+
11+
beforeEach(async () => {
12+
radixClient = new RadixClient(phrase, params)
13+
})
14+
15+
afterEach(async () => {
16+
radixClient.purgeClient()
17+
})
18+
19+
it('should start with empty wallet', async () => {
20+
console.log(radixClient)
21+
})
22+
})

packages/xchain-radix/jest.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
testPathIgnorePatterns: ['<rootDir>/node_modules', '<rootDir>/lib'],
5+
setupFilesAfterEnv: ['./jest.setup.js'],
6+
}

packages/xchain-radix/jest.setup.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jest.setTimeout(30000)

packages/xchain-radix/package.json

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "@xchainjs/xchain-radix",
3+
"version": "0.1.0",
4+
"description": "Custom Radix client and utilities used by XChainJS clients",
5+
"keywords": [
6+
"XRD",
7+
"Radix",
8+
"XChain"
9+
],
10+
"author": "RadixDLT",
11+
"homepage": "https://github.com/xchainjs/xchainjs-lib",
12+
"license": "MIT",
13+
"main": "lib/index.js",
14+
"module": "lib/index.esm.js",
15+
"typings": "lib/index.d.ts",
16+
"directories": {
17+
"lib": "lib",
18+
"test": "__tests__"
19+
},
20+
"files": [
21+
"lib"
22+
],
23+
"repository": {
24+
"type": "git",
25+
"url": "git@github.com:xchainjs/xchainjs-lib.git"
26+
},
27+
"scripts": {
28+
"clean": "rm -rf .turbo && rm -rf lib",
29+
"build": "yarn clean && rollup -c",
30+
"test": "jest",
31+
"compile": "tsc -p tsconfig.build.json",
32+
"lint": "eslint \"{src,__tests__}/**/*.ts\" --fix --max-warnings 0"
33+
},
34+
"devDependencies": {
35+
"@xchainjs/xchain-client": "^0.16.2",
36+
"@xchainjs/xchain-crypto": "^0.3.1",
37+
"@xchainjs/xchain-util": "^0.13.3",
38+
"nock": "13.0.5"
39+
},
40+
"publishConfig": {
41+
"access": "public"
42+
},
43+
"peerDependencies": {
44+
"@xchainjs/xchain-client": "^0.16.2",
45+
"@xchainjs/xchain-crypto": "^0.3.1",
46+
"@xchainjs/xchain-util": "^0.13.3"
47+
},
48+
"dependencies": {
49+
"@radixdlt/radix-engine-toolkit": "^1.0.3"
50+
}
51+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import commonjs from '@rollup/plugin-commonjs'
2+
import json from '@rollup/plugin-json'
3+
import resolve from '@rollup/plugin-node-resolve'
4+
import external from 'rollup-plugin-peer-deps-external'
5+
import typescript from 'rollup-plugin-typescript2'
6+
7+
import pkg from './package.json'
8+
9+
export default {
10+
input: 'src/index.ts',
11+
output: [
12+
{
13+
file: pkg.main,
14+
format: 'cjs',
15+
exports: 'named',
16+
sourcemap: false,
17+
},
18+
{
19+
file: pkg.module,
20+
format: 'es',
21+
exports: 'named',
22+
sourcemap: false,
23+
},
24+
],
25+
plugins: [
26+
json({}),
27+
external(),
28+
resolve({ preferBuiltins: true, browser: true }),
29+
typescript({
30+
exclude: '__tests__/**',
31+
}),
32+
commonjs(),
33+
],
34+
external: ['readable-stream', 'buffer', 'crypto', 'stream', 'string_decoder'],
35+
}

packages/xchain-radix/src/client.ts

+233
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
import { LTSRadixEngineToolkit, NetworkId, PrivateKey, RadixEngineToolkit } from '@radixdlt/radix-engine-toolkit'
2+
import {
3+
AssetInfo,
4+
Balance,
5+
BaseXChainClient,
6+
Fees,
7+
Network,
8+
PreparedTx,
9+
Tx,
10+
TxHistoryParams,
11+
TxParams,
12+
TxsPage,
13+
} from '@xchainjs/xchain-client'
14+
import { getSeed } from '@xchainjs/xchain-crypto/lib'
15+
import { Address, Asset } from '@xchainjs/xchain-util'
16+
17+
const axios = require('axios')
18+
19+
class RadixClient extends BaseXChainClient {
20+
/**
21+
* Get transaction fees.
22+
*
23+
* @param {TxParams} params - The transaction parameters.
24+
* @returns {Fees} The average, fast, and fastest fees.
25+
* @throws {"Params need to be passed"} Thrown if parameters are not provided.
26+
*/
27+
getFees(): never
28+
getFees(params: TxParams): Promise<Fees>
29+
async getFees(params?: TxParams): Promise<Fees> {
30+
if (!params) throw new Error('Params need to be passed')
31+
const fees = await this.estimateFees(params)
32+
return fees
33+
}
34+
35+
/**
36+
* Get the address for a given account.
37+
* @deprecated Use getAddressAsync instead.
38+
*/
39+
getAddress(): string {
40+
throw new Error('getAddress is synchronous and cannot retrieve addresses directly. Use getAddressAsync instead.')
41+
}
42+
43+
/**
44+
* Get the current address asynchronously for a given account.
45+
* @returns {Address} A promise resolving to the current address.
46+
* @throws {Error} Thrown if the phrase has not been set before.
47+
* A phrase is needed to create a wallet and to derive an address from it.
48+
*/
49+
async getAddressAsync(): Promise<string> {
50+
if (!this.phrase) throw new Error('Phrase not set')
51+
const seed = getSeed(this.phrase)
52+
const hexString = seed.toString('hex')
53+
const privateKey = new PrivateKey.Ed25519(hexString)
54+
const publicKey = privateKey.publicKey()
55+
const network = this.getNetwork()
56+
const networkId = network === Network.Mainnet ? NetworkId.Mainnet : NetworkId.Stokenet
57+
const address = await LTSRadixEngineToolkit.Derive.virtualAccountAddress(publicKey, networkId)
58+
return address.toString()
59+
}
60+
61+
/**
62+
* Get the explorer URL based on the network.
63+
*
64+
* @returns {string} The explorer URL based on the network.
65+
*/
66+
getExplorerUrl(): string {
67+
switch (this.getNetwork()) {
68+
case Network.Mainnet:
69+
return 'https://explorer.radixdlt.com'
70+
case Network.Testnet:
71+
return 'https://stokenet-dashboard.radixdlt.com/'
72+
default:
73+
throw new Error('Unsupported network')
74+
}
75+
}
76+
77+
/**
78+
* Get the explorer URL for a given account address based on the network.
79+
* @param {Address} address The address to generate the explorer URL for.
80+
* @returns {string} The explorer URL for the given address.
81+
*/
82+
getExplorerAddressUrl(address: Address): string {
83+
return `${this.getExplorerUrl()}/account/${address}`
84+
}
85+
86+
/**
87+
* Get the explorer URL for a given transaction ID based on the network.
88+
* @param {string} txID The transaction ID to generate the explorer URL for.
89+
* @returns {string} The explorer URL for the given transaction ID.
90+
*/
91+
getExplorerTxUrl(txID: string): string {
92+
return `${this.getExplorerUrl()}/transactions/${txID}`
93+
}
94+
95+
/**
96+
* Validate the given address.
97+
* @param {Address} address The address to validate.
98+
* @returns {boolean} `true` if the address is valid, `false` otherwise.
99+
*/
100+
validateAddress(address: string): boolean {
101+
try {
102+
RadixEngineToolkit.Address.decode(address)
103+
return true
104+
} catch (error) {
105+
return false
106+
}
107+
}
108+
109+
/**
110+
* Retrieves the balance of a given address.
111+
* @param {Address} address - The address to retrieve the balance for.
112+
* @param {Asset[]} assets - Assets to retrieve the balance for (optional).
113+
* @returns {Promise<Balance[]>} An array containing the balance of the address.
114+
* @throws {"Invalid asset"} Thrown when the provided asset is invalid.
115+
*/
116+
async getBalance(address: string, assets?: Asset[] | undefined): Promise<Balance[]> {
117+
console.log(address + assets)
118+
try {
119+
// Assuming walletIndex is used to construct the URL
120+
const url = `https://jsonplaceholder.typicode.com/posts/}`
121+
console.log('Making GET request to:', url)
122+
const response = await axios.get(url)
123+
console.log('Response data:', response.data)
124+
// Assuming the response contains an address field
125+
const address = response.data.address
126+
return address
127+
} catch (error) {
128+
console.error('Error fetching address:', error)
129+
throw error
130+
}
131+
}
132+
async getTransactions(params?: TxHistoryParams | undefined): Promise<TxsPage> {
133+
try {
134+
console.log(params)
135+
// Assuming walletIndex is used to construct the URL
136+
const url = `https://jsonplaceholder.typicode.com/posts/`
137+
console.log('Making GET request to:', url)
138+
const response = await axios.get(url)
139+
console.log('Response data:', response.data)
140+
// Assuming the response contains an address field
141+
const address = response.data.address
142+
return address
143+
} catch (error) {
144+
console.error('Error fetching address:', error)
145+
throw error
146+
}
147+
}
148+
async getTransactionData(txId: string, assetAddress?: string | undefined): Promise<Tx> {
149+
try {
150+
console.log(txId + assetAddress)
151+
// Assuming walletIndex is used to construct the URL
152+
const url = `https://jsonplaceholder.typicode.com/posts/`
153+
console.log('Making GET request to:', url)
154+
const response = await axios.get(url)
155+
console.log('Response data:', response.data)
156+
// Assuming the response contains an address field
157+
const address = response.data.address
158+
return address
159+
} catch (error) {
160+
console.error('Error fetching address:', error)
161+
throw error
162+
}
163+
}
164+
async transfer(params: TxParams): Promise<string> {
165+
try {
166+
console.log(params)
167+
// Assuming walletIndex is used to construct the URL
168+
const url = `https://jsonplaceholder.typicode.com/posts/`
169+
console.log('Making GET request to:', url)
170+
const response = await axios.get(url)
171+
console.log('Response data:', response.data)
172+
// Assuming the response contains an address field
173+
const address = response.data.address
174+
return address
175+
} catch (error) {
176+
console.error('Error fetching address:', error)
177+
throw error
178+
}
179+
}
180+
async broadcastTx(txHex: string): Promise<string> {
181+
try {
182+
console.log(txHex)
183+
// Assuming walletIndex is used to construct the URL
184+
const url = `https://jsonplaceholder.typicode.com/posts/`
185+
console.log('Making GET request to:', url)
186+
const response = await axios.get(url)
187+
console.log('Response data:', response.data)
188+
// Assuming the response contains an address field
189+
const address = response.data.address
190+
return address
191+
} catch (error) {
192+
console.error('Error fetching address:', error)
193+
throw error
194+
}
195+
}
196+
getAssetInfo(): AssetInfo {
197+
throw new Error('Method not implemented.')
198+
}
199+
async prepareTx(params: TxParams): Promise<PreparedTx> {
200+
try {
201+
console.log(params)
202+
// Assuming walletIndex is used to construct the URL
203+
const url = `https://jsonplaceholder.typicode.com/posts/`
204+
console.log('Making GET request to:', url)
205+
const response = await axios.get(url)
206+
console.log('Response data:', response.data)
207+
// Assuming the response contains an address field
208+
const address = response.data.address
209+
return address
210+
} catch (error) {
211+
console.error('Error fetching address:', error)
212+
throw error
213+
}
214+
}
215+
async estimateFees(params?: TxParams): Promise<Fees> {
216+
try {
217+
console.log(params)
218+
// check if we can estimate the fees using the transaction manifest somehow
219+
// if we can estimate them, do an http request and return the fees
220+
const url = `fees_estimation_url`
221+
console.log('Making GET request to:', url)
222+
const response = await axios.get(url)
223+
console.log('Response data:', response.data)
224+
const fees = response.data.fees
225+
return fees
226+
} catch (error) {
227+
console.error('Error fetching address:', error)
228+
throw error
229+
}
230+
}
231+
}
232+
233+
export { RadixClient }

packages/xchain-radix/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './client'

packages/xchain-radix/tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"include": [ "src/**/*" ]
4+
}

0 commit comments

Comments
 (0)