-
Notifications
You must be signed in to change notification settings - Fork 204
01WalletStrategy
The main purpose of the @injectivelabs/wallet-ts is to offer developers a way to have different wallet implementations on Injective. All of these wallets implementations are exposing the same ConcreteStrategy interface which means that users can just use these methods without the need to know the underlying implementation for specific wallets as they are abstracted away.
To start, you have to make an instance of the WalletStrategy class which gives you the ability to use different wallets out of the box. You can switch the current wallet that is used by using the setWallet method on the walletStrategy instance.
Lets have a look at the methods that WalletStrategy strategy exposes and what do they mean:
-
getAddressesgets the addresses from the connected wallet strategy, -
sendEthereumTransactionsends an Ethereum transaction using the connected wallet strategy, -
sendTransactionsends an Injective transaction using the connected wallet strategy, -
signCosmosTransactionsigns an Injective transaction using the connected wallet strategy, -
signEthereumTransactionsigns an Ethereum transaction using the connected wallet strategy, -
getPublicKeyget the public key for the Cosmos native wallet strategies, -
getNetworkIdget the network id for the Ethereum native wallet strategies, -
getChainIdget the chain id for the Ethereum native wallet strategies, -
getEthereumTransactionReceiptget the transaction receipt for Ethereum native transactions for the wallet strategy, -
getWeb3gets an Web3 instance. Depending on the wallet strategy it uses a different web3 provider (user provided RPC for some, the window object injected provider for Metamask, etc)
import { WalletStrategy } from '@injectivelabs/wallet-ts'
import { EthereumChainId } from '@injectivelabs/ts-types'
import { CHAIN_ID, ETHEREUM_CHAIN_ID, IS_TESTNET } from '~/app/utils/constants'
export const walletStrategy = new WalletStrategy({
chainId: CHAIN_ID,
/** optional, if you want to use ethereum native wallets */
ethereumOptions: getEthereumOptions()
})
export const getEthereumOptions = () => {
const getRpcUrlsForChainIds = (): Record<EthereumChainId, string> => {
return {
[EthereumChainId.Ganache]: 'http://localhost:8545',
[EthereumChainId.HardHat]: 'http://localhost:8545',
[EthereumChainId.Goerli]: `https://eth-goerli.alchemyapi.io/v2/${process.env.APP_ALCHEMY_GOERLI_KEY}`,
[EthereumChainId.Kovan]: `https://eth-kovan.alchemyapi.io/v2/${process.env.APP_ALCHEMY_KOVAN_KEY}`,
[EthereumChainId.Mainnet]: `https://eth-mainnet.alchemyapi.io/v2/${process.env.APP_ALCHEMY_KEY}`,
[EthereumChainId.Injective]: '',
[EthereumChainId.Rinkeby]: '',
[EthereumChainId.Ropsten]: ''
}
}
const getRpcWsUrlsForChainIds = (): Record<EthereumChainId, string> => {
return {
[EthereumChainId.Ganache]: 'ws://localhost:1318',
[EthereumChainId.HardHat]: 'ws://localhost:1318',
[EthereumChainId.Goerli]: `wss://eth-goerli.ws.alchemyapi.io/v2/${process.env.APP_ALCHEMY_GOERLI_KEY}`,
[EthereumChainId.Kovan]: `wss://eth-kovan.ws.alchemyapi.io/v2/${process.env.APP_ALCHEMY_KOVAN_KEY}`,
[EthereumChainId.Mainnet]: `wss://eth-mainnet.ws.alchemyapi.io/v2/${process.env.APP_ALCHEMY_KEY}`,
[EthereumChainId.Injective]: '',
[EthereumChainId.Rinkeby]: '',
[EthereumChainId.Ropsten]: ''
}
}
const rpcUrls = getRpcUrlsForChainIds()
const wsRpcUrls = getRpcWsUrlsForChainIds()
return {
ethereumChainId: ETHEREUM_CHAIN_ID,
wsRpcUrls,
rpcUrls
},
}
// Get wallet's addresses
export const getAddresses = async (): Promise<string[]> => {
const addresses = await walletStrategy.getAddresses()
if (addresses.length === 0) {
throw new Web3Exception('There are no addresses linked in this wallet.')
}
return addresses
}
// Sign an Cosmos transaction
export const signTransaction = async (tx: TxRaw): Promise<string[]> => {
const response = await walletStrategy.signCosmosTransaction(
transaction: { txRaw: tx; accountNumber: /* */; chainId: 'injective-1' },
address: 'inj1...',
)
return response
}Powering the future of decentralized finance.