Skip to content

Commit 6366eea

Browse files
committed
helper for getting a gateway and locator singleton
1 parent 846fdfc commit 6366eea

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

src/getGateway.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { assertEx } from '@xylabs/assert'
2+
import { isDefined } from '@xylabs/typeof'
3+
import type {
4+
CreatableProviderContext,
5+
ProviderFactoryLocator,
6+
RpcSchemaMap, TransportFactory, XyoConnection,
7+
} from '@xyo-network/xl1-sdk'
8+
import {
9+
ADDRESS_INDEX, buildJsonRpcProviderLocator, buildSimpleXyoSigner, generateXyoBaseWalletFromPhrase,
10+
HttpRpcTransport, SimpleXyoGatewayRunner, XyoConnectionMoniker,
11+
} from '@xyo-network/xl1-sdk'
12+
13+
let locator: ProviderFactoryLocator<CreatableProviderContext, string[]>
14+
15+
export const getGateway = async (mnemonic?: string, rpcEndpoint = 'http://localhost:8080/rpc') => {
16+
// Load the account to use for the transaction
17+
const walletMnemonic = assertEx(process.env.XYO_WALLET_MNEMONIC ?? mnemonic, () => 'Unable to resolve mnemonic from environment variable or argument')
18+
const account = await (await generateXyoBaseWalletFromPhrase(walletMnemonic)).derivePath(ADDRESS_INDEX.XYO)
19+
console.log('Using account:', account.address)
20+
21+
// Build the signer
22+
const signer = await buildSimpleXyoSigner({ account })
23+
24+
// Get the provider locator
25+
const locator = await getLocator(rpcEndpoint)
26+
27+
// Get the XyoConnection instance
28+
const connection = await locator.getInstance<XyoConnection>(XyoConnectionMoniker)
29+
30+
// Return a new SimpleXyoGatewayRunner instance
31+
return new SimpleXyoGatewayRunner(connection, signer)
32+
}
33+
34+
export const getLocator = async (rpcEndpoint = 'http://localhost:8080/rpc') => {
35+
if (isDefined(locator)) return locator
36+
// Determine the RPC endpoint to use for the chain connection
37+
const endpoint = process.env.XYO_CHAIN_RPC_URL ?? rpcEndpoint
38+
console.log('Using endpoint:', endpoint)
39+
40+
const transportFactory: TransportFactory = (schemas: RpcSchemaMap) => new HttpRpcTransport(endpoint, schemas)
41+
locator = await buildJsonRpcProviderLocator({ transportFactory })
42+
return locator
43+
}

0 commit comments

Comments
 (0)