Skip to content

Commit 1cce55b

Browse files
committed
Resolve ENV VARs once
1 parent 74f23d2 commit 1cce55b

8 files changed

Lines changed: 22 additions & 28 deletions

src/getGateway.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import { getLocator } from './getLocator.ts'
88

99
let gateway: SimpleXyoGatewayRunner | undefined
1010

11-
export const getGateway = async (walletMnemonic: string, rpcUrl: string) => {
11+
export const getGateway = async () => {
1212
// If existing gateway, return it
1313
if (isDefined(gateway)) return gateway
1414

1515
// Otherwise, build a new gateway
1616

1717
// Get locator
18-
const locator = await getLocator(walletMnemonic, rpcUrl)
18+
const locator = await getLocator()
1919

2020
// Use locator to get connection and signer
2121
const connection = await locator.getInstance<XyoConnection>(XyoConnectionMoniker)

src/getLocator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import { getTransportFactory } from './getTransportFactory.ts'
77

88
let locator: ProviderFactoryLocator
99

10-
export const getLocator = async (walletMnemonic: string, rpcUrl: string) => {
10+
export const getLocator = async () => {
1111
// If existing locator, return it
1212
if (isDefined(locator)) return locator
1313

1414
// Build a new locator
15-
const transportFactory = getTransportFactory(rpcUrl)
15+
const transportFactory = getTransportFactory()
1616
locator = await buildJsonRpcProviderLocator({ transportFactory })
1717

1818
// Register the signer with the locator
19-
const account = await getSignerAccount(walletMnemonic)
19+
const account = await getSignerAccount()
2020
locator.register(SimpleXyoSigner.factory<SimpleXyoSigner>(SimpleXyoSigner.dependencies, { account }))
2121

2222
// Return the locator

src/getSignerAccount.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import { isDefined } from '@xylabs/typeof'
22
import type { AccountInstance } from '@xyo-network/account-model'
33
import { ADDRESS_INDEX, generateXyoBaseWalletFromPhrase } from '@xyo-network/xl1-sdk'
44

5+
import { getWalletMnemonic } from './getWalletMnemonic.ts'
6+
57
let signerAccount: AccountInstance | undefined
68

79
/**
810
* Retrieves the signer account derived from the provided mnemonic.
9-
* @param walletMnemonic The mnemonic to use for deriving the account.
1011
* @returns The derived account
1112
*/
12-
export const getSignerAccount = async (walletMnemonic: string) => {
13+
export const getSignerAccount = async () => {
1314
// If existing signer account, return it
1415
if (isDefined (signerAccount)) return signerAccount
1516

1617
// Create new signer account
18+
const walletMnemonic = getWalletMnemonic()
1719
const wallet = await generateXyoBaseWalletFromPhrase(walletMnemonic)
1820
signerAccount = await wallet.derivePath(ADDRESS_INDEX.XYO)
1921
console.log('Using signer account:', signerAccount.address)

src/getTransportFactory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import { isDefined } from '@xylabs/typeof'
22
import type { RpcSchemaMap, TransportFactory } from '@xyo-network/xl1-sdk'
33
import { HttpRpcTransport } from '@xyo-network/xl1-sdk'
44

5+
import { getRpcUrl } from './getRpcUrl.ts'
6+
57
let transportFactory: TransportFactory | undefined
68

79
/**
810
* Retrieves a transport factory for the given RPC URL.
9-
* @param rpcUrl The RPC endpoint to use for interacting with the chain
1011
* @returns A transport factory for the given RPC URL
1112
*/
12-
export const getTransportFactory = (rpcUrl: string) => {
13+
export const getTransportFactory = () => {
1314
// If existing transport factory, return it
1415
if (isDefined(transportFactory)) return transportFactory
1516

1617
// Build a new transport factory
18+
const rpcUrl = getRpcUrl()
1719
console.log('Using rpcUrl:', rpcUrl)
1820
transportFactory = (schemas: RpcSchemaMap) => new HttpRpcTransport(rpcUrl, schemas)
1921
return transportFactory
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ const mnemonic = process.env.XYO_WALLET_MNEMONIC ?? HDWallet.generateMnemonic()
1111
* Gets the mnemonic to use for the signer
1212
* @returns The mnemonic to use for the signer
1313
*/
14-
export const getMnemonic = (): string => mnemonic
14+
export const getWalletMnemonic = (): string => mnemonic

src/helloWorld.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ const logger = console
99

1010
/**
1111
* Runs a simple "Hello World" transaction on the XL1 network.
12-
* @param mnemonic The mnemonic to use for signing transactions
13-
* @param rpcUrl The RPC URL to connect to the XL1 network
1412
*/
15-
export async function helloWorld(mnemonic: string, rpcUrl: string): Promise<void> {
13+
export async function helloWorld(): Promise<void> {
1614
try {
1715
console.log('\n**** Starting XL1 Hello World NodeJs Sample ****\n')
1816

1917
// Generate random data to send in the transaction
2018
const { onChainData, offChainData } = await getRandomTransactionData()
2119

2220
// Get gateway to interact with the chain
23-
const gateway = await getGateway(mnemonic, rpcUrl)
21+
const gateway = await getGateway()
2422

2523
// Send the transaction to the network
2624
const [txHash] = await gateway.addPayloadsToChain(onChainData, offChainData)

src/helloWorldRunner.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import type { XyoViewer } from '@xyo-network/xl1-protocol-sdk'
44
import { XyoViewerMoniker } from '@xyo-network/xl1-protocol-sdk'
55

66
import { getLocator } from './getLocator.ts'
7-
import { getMnemonic } from './getMnemonic.ts'
8-
import { getRpcUrl } from './getRpcUrl.ts'
97
import { getSignerAccount } from './getSignerAccount.ts'
8+
import { getWalletMnemonic } from './getWalletMnemonic.ts'
109
import { helloWorld } from './helloWorld.js'
1110
import { waitForInitialBlocks } from './waitForInitialBlocks.js'
1211

1312
// Parse the relevant ENV VARs or use defaults
14-
const mnemonic = getMnemonic()
15-
const rpcUrl = getRpcUrl()
13+
const mnemonic = getWalletMnemonic()
1614

1715
/**
1816
* Starts the XL1 node using command in a child process
@@ -52,7 +50,7 @@ async function startXl1(): Promise<string> {
5250

5351
try {
5452
// Log out the mnemonic and signer address in case random was generated
55-
const account = await getSignerAccount(mnemonic)
53+
const account = await getSignerAccount()
5654
console.log('Using signer mnemonic:', mnemonic)
5755
console.log('Using producer address:', account.address)
5856

@@ -88,7 +86,7 @@ async function startXl1(): Promise<string> {
8886
})
8987

9088
// Get the XyoViewer instance
91-
const locator = await getLocator(mnemonic, rpcUrl)
89+
const locator = await getLocator()
9290
const viewer = await locator.getInstance<XyoViewer>(XyoViewerMoniker)
9391

9492
// Wait for the initial blocks to be created
@@ -112,7 +110,7 @@ try {
112110
console.log('XL1 is ready, starting sample...')
113111

114112
try {
115-
await helloWorld(mnemonic, rpcUrl)
113+
await helloWorld()
116114
} catch (error) {
117115
console.error('Error importing application:', error)
118116
}

src/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import { getMnemonic } from './getMnemonic.ts'
2-
import { getRpcUrl } from './getRpcUrl.ts'
31
import { helloWorld } from './helloWorld.js'
42

5-
// Parse the relevant ENV VARs or use defaults
6-
const mnemonic = getMnemonic()
7-
const rpcUrl = getRpcUrl()
8-
9-
await helloWorld(mnemonic, rpcUrl)
3+
await helloWorld()

0 commit comments

Comments
 (0)