Skip to content

Commit

Permalink
chore: add docs for creating a wallet-strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Oct 23, 2024
1 parent 7b50184 commit 790bff5
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 8 deletions.
8 changes: 6 additions & 2 deletions packages/wallets/wallet-base/src/types/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,17 @@ export interface ConcreteCosmosWalletStrategy {
}): Promise<AminoSignResponse>
}

export type ConcreteStrategiesArg = {
[key in Wallet]?: ConcreteWalletStrategy
}

export interface WalletStrategyArguments {
chainId: ChainId
options?: ConcreteWalletStrategyOptions
ethereumOptions?: WalletStrategyEthereumOptions
disabledWallets?: Wallet[]
wallet?: Wallet
strategies: Record<Wallet, ConcreteWalletStrategy | undefined>
strategies: ConcreteStrategiesArg
}

export interface ConcreteWalletStrategy
Expand Down Expand Up @@ -224,7 +228,7 @@ export interface ConcreteWalletStrategy
}

export interface WalletStrategy {
strategies: Record<Wallet, ConcreteWalletStrategy | undefined>
strategies: ConcreteStrategiesArg
wallet: Wallet
args: WalletStrategyArguments

Expand Down
22 changes: 20 additions & 2 deletions packages/wallets/wallet-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,32 @@ _Package to use MetaMask Wallets on Injective via the wallet strategy._
## 📚 Installation

```bash
yarn add @injectivelabs/wallet-evm
yarn add @injectivelabs/wallet-cire
```

---

## 📖 Documentation

<!-- TODO -->
To instantiate your MsgBroadcaster, you need to pass it an instance of `BaseWalletStrategy`. You can specify any strategy you want in the `BaseWalletStrategy` constructor and pass it to the `MsgBroadcaster`.

```ts
import { Wallet } from '@injectivelabs/wallet-base'
import { BaseWalletStrategy } from '@injectivelabs/wallet-core'
import { PrivateKeyWalletStrategy } from '@injectivelabs/wallet-private-key'

const strategyArgs: WalletStrategyArguments = {} /** define the args */
const strategyEthArgs: ConcreteEthereumWalletStrategyArgs = {} /** if the wallet is an Ethereum wallet */
const strategies = {
[Wallet.PrivateKey]: new PrivateKeyWalletStrategy(strategyEthArgs)
}

export const walletStrategy = new BaseWalletStrategy({...strategyArgs, strategies})

const broadcasterArgs: MsgBroadcasterOptions = {} /** define the broadcaster args */
export const msgBroadcaster = new MsgBroadcaster({...broadcasterArgs, walletStrategy})
```


Read more and find example usages on our [WalletStrategy Docs](https://docs.ts.injective.network/wallet/wallet-wallet-strategy)

Expand Down
109 changes: 109 additions & 0 deletions packages/wallets/wallet-core/src/broadcaster/MsgBroadcaster.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { Network } from '@injectivelabs/networks'
import { MsgBroadcaster } from './MsgBroadcaster'
import { MsgBroadcasterOptions } from './types'
import {
Wallet,
WalletStrategyArguments,
ConcreteEthereumWalletStrategyArgs,
} from '@injectivelabs/wallet-base'
import { EthereumChainId } from '@injectivelabs/ts-types'
import { PrivateKeyWalletStrategy } from '@injectivelabs/wallet-private-key'

const strategyArgs: WalletStrategyArguments = {} /** define the args */
const strategyEthArgs: ConcreteEthereumWalletStrategyArgs =
{} /** if the wallet is an Ethereum wallet */
const strategies = {
[Wallet.PrivateKey]: new PrivateKeyWalletStrategy(strategyEthArgs),
}

export const walletStrategy = new BaseWalletStrategy({
...strategyArgs,
strategies,
})

const broadcasterArgs: MsgBroadcasterOptions =
{} /** define the broadcaster args */
export const msgBroadcaster = new MsgBroadcaster({
...broadcasterArgs,
walletStrategy,
})

describe('MsgBroadcaster', () => {
test('prepares, simulates, signs and broadcasts a transaction', async () => {
const privateKey = PrivateKey.fromHex(
process.env.TEST_PRIVATE_KEY as string,
)

const network = Network.Devnet
const injectiveAddress = privateKey.toBech32()

const message = MsgSend.fromJSON({
srcInjectiveAddress: injectiveAddress,
dstInjectiveAddress: injectiveAddress,
amount: {
amount: '1',
denom: 'inj',
},
})

const response = await new MsgBroadcasterWithPk({
network,
privateKey,
simulateTx: true,
}).broadcast({ msgs: message })

expect(response.txHash).toBeDefined()
}, 60000)

test.skip('prepares, simulates, signs and broadcasts a transaction with fee delegation', async () => {
const privateKey = PrivateKey.fromHex(
process.env.TEST_PRIVATE_KEY as string,
)

const network = Network.Devnet
const injectiveAddress = privateKey.toBech32()

const message = MsgSend.fromJSON({
srcInjectiveAddress: injectiveAddress,
dstInjectiveAddress: injectiveAddress,
amount: {
amount: '1',
denom: 'inj',
},
})

const response = await new MsgBroadcasterWithPk({
network,
privateKey,
simulateTx: true,
ethereumChainId: EthereumChainId.Sepolia,
}).broadcastWithFeeDelegation({ msgs: message })

expect(response.txHash).toBeDefined()
}, 60000)

test.skip('simulates a transaction', async () => {
const privateKey = PrivateKey.fromHex(
process.env.TEST_PRIVATE_KEY as string,
)

const network = Network.Devnet
const injectiveAddress = privateKey.toBech32()

const message = MsgSend.fromJSON({
srcInjectiveAddress: injectiveAddress,
dstInjectiveAddress: injectiveAddress,
amount: {
amount: '1',
denom: 'inj',
},
})

const response = await new MsgBroadcasterWithPk({
network,
privateKey,
}).simulate({ msgs: message })

expect(response.result).toBeDefined()
}, 60000)
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TxRaw, TxResponse } from '@injectivelabs/sdk-ts'
import { DirectSignResponse } from '@cosmjs/proto-signing'
import {
AccountAddress,
ChainId,
AccountAddress,
EthereumChainId,
} from '@injectivelabs/ts-types'
import { GeneralException, WalletException } from '@injectivelabs/exceptions'
Expand All @@ -11,6 +11,7 @@ import {
isEthWallet,
isCosmosWallet,
WalletDeviceType,
ConcreteStrategiesArg,
SendTransactionOptions,
ConcreteWalletStrategy,
onAccountChangeCallback,
Expand All @@ -34,7 +35,7 @@ const getInitialWallet = (args: WalletStrategyArguments): Wallet => {
}

export default class BaseWalletStrategy implements WalletStrategyInterface {
public strategies: Record<Wallet, ConcreteWalletStrategy | undefined>
public strategies: ConcreteStrategiesArg

public wallet: Wallet

Expand Down
53 changes: 53 additions & 0 deletions packages/wallets/wallet-private-key/src/strategy/strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Network } from '@injectivelabs/networks'
import {
MsgBroadcaster,
BaseWalletStrategy,
MsgBroadcasterOptions,
} from '@injectivelabs/wallet-core'
import { Wallet, WalletStrategyArguments } from '@injectivelabs/wallet-base'
import { MsgSend, PrivateKey } from '@injectivelabs/sdk-ts'
import { ChainId, EthereumChainId } from '@injectivelabs/ts-types'
import { PrivateKeyWalletStrategy } from '@injectivelabs/wallet-private-key'

const strategyArgs: WalletStrategyArguments = {
chainId: ChainId.Devnet,
wallet: Wallet.PrivateKey,
strategies: {
[Wallet.PrivateKey]: new PrivateKeyWalletStrategy({
chainId: ChainId.Devnet,
ethereumOptions: {
ethereumChainId: EthereumChainId.Sepolia,
rpcUrl: '',
},
privateKey: process.env.TEST_PRIVATE_KEY as string,
}),
},
}
const walletStrategy = new BaseWalletStrategy(strategyArgs)

const broadcasterArgs: MsgBroadcasterOptions = {
walletStrategy,
simulateTx: true,
network: Network.Devnet,
}
const msgBroadcaster = new MsgBroadcaster(broadcasterArgs)

describe('MsgBroadcaster', () => {
test('prepares, simulates, signs and broadcasts a transaction', async () => {
const pk = PrivateKey.fromHex(process.env.TEST_PRIVATE_KEY as string)
const injectiveAddress = pk.toBech32()

const message = MsgSend.fromJSON({
srcInjectiveAddress: injectiveAddress,
dstInjectiveAddress: injectiveAddress,
amount: {
amount: '1',
denom: 'inj',
},
})

const response = await msgBroadcaster.broadcast({ msgs: message })

expect(response.txHash).toBeDefined()
}, 60000)
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Wallet,
isEthWallet,
MagicMetadata,
ConcreteStrategiesArg,
ConcreteWalletStrategy,
WalletStrategyArguments,
ConcreteWalletStrategyOptions,
Expand Down Expand Up @@ -127,13 +128,13 @@ const createStrategy = ({

const createAllStrategies = (
args: WalletStrategyArguments,
): Record<Wallet, ConcreteWalletStrategy | undefined> => {
): ConcreteStrategiesArg => {
return Object.values(Wallet).reduce(
(strategies, wallet) => ({
...strategies,
[wallet]: createStrategy({ args, wallet: wallet as Wallet }),
}),
{} as Record<Wallet, ConcreteWalletStrategy | undefined>,
{} as ConcreteStrategiesArg,
)
}

Expand Down

0 comments on commit 790bff5

Please sign in to comment.