Skip to content

Commit

Permalink
chore: throwable exceptions on broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Nov 11, 2024
1 parent c66e830 commit 634aa7a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 42 deletions.
53 changes: 42 additions & 11 deletions packages/wallet-ts/src/broadcaster/MsgBroadcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from '@injectivelabs/utils'
import {
GeneralException,
isThrownException,
TransactionException,
UnspecifiedErrorCode,
} from '@injectivelabs/exceptions'
Expand Down Expand Up @@ -151,11 +152,21 @@ export class MsgBroadcaster {
)
}

return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmos(txWithAddresses)
: isEip712V2OnlyWallet(walletStrategy.wallet)
? this.broadcastWeb3V2(txWithAddresses)
: this.broadcastWeb3(txWithAddresses)
try {
return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmos(txWithAddresses)
: isEip712V2OnlyWallet(walletStrategy.wallet)
? this.broadcastWeb3V2(txWithAddresses)
: this.broadcastWeb3(txWithAddresses)
} catch (e) {
const error = e as any

if (isThrownException(error)) {
throw error
}

throw new TransactionException(new Error(error))
}
}

/**
Expand Down Expand Up @@ -185,9 +196,19 @@ export class MsgBroadcaster {
)
}

return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmos(txWithAddresses)
: this.broadcastWeb3V2(txWithAddresses)
try {
return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmos(txWithAddresses)
: this.broadcastWeb3V2(txWithAddresses)
} catch (e) {
const error = e as any

if (isThrownException(error)) {
throw error
}

throw new TransactionException(new Error(error))
}
}

/**
Expand Down Expand Up @@ -250,9 +271,19 @@ export class MsgBroadcaster {
)
}

return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmosWithFeeDelegation(txWithAddresses)
: this.broadcastWeb3WithFeeDelegation(txWithAddresses)
try {
return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmosWithFeeDelegation(txWithAddresses)
: this.broadcastWeb3WithFeeDelegation(txWithAddresses)
} catch (e) {
const error = e as any

if (isThrownException(error)) {
throw error
}

throw new TransactionException(new Error(error))
}
}

/**
Expand Down
55 changes: 43 additions & 12 deletions packages/wallets/wallet-core/src/broadcaster/MsgBroadcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import {
getEip712TypedData,
createWeb3Extension,
getEip712TypedDataV2,
IndexerGrpcWeb3GwApi,
ChainGrpcTendermintApi,
createTransactionWithSigners,
createTxRawFromSigResponse,
IndexerGrpcWeb3GwApi,
getGasPriceBasedOnMessage,
recoverTypedSignaturePubKey,
CreateTransactionWithSignersArgs,
Expand All @@ -32,6 +32,7 @@ import {
} from '@injectivelabs/utils'
import {
GeneralException,
isThrownException,
TransactionException,
UnspecifiedErrorCode,
} from '@injectivelabs/exceptions'
Expand Down Expand Up @@ -148,11 +149,21 @@ export class MsgBroadcaster {
)
}

return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmos(txWithAddresses)
: isEip712V2OnlyWallet(walletStrategy.wallet)
? this.broadcastWeb3V2(txWithAddresses)
: this.broadcastWeb3(txWithAddresses)
try {
return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmos(txWithAddresses)
: isEip712V2OnlyWallet(walletStrategy.wallet)
? this.broadcastWeb3V2(txWithAddresses)
: this.broadcastWeb3(txWithAddresses)
} catch (e) {
const error = e as any

if (isThrownException(error)) {
throw error
}

throw new TransactionException(new Error(error))
}
}

/**
Expand All @@ -177,9 +188,19 @@ export class MsgBroadcaster {
)
}

return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmos(txWithAddresses)
: this.broadcastWeb3V2(txWithAddresses)
try {
return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmos(txWithAddresses)
: this.broadcastWeb3V2(txWithAddresses)
} catch (e) {
const error = e as any

if (isThrownException(error)) {
throw error
}

throw new TransactionException(new Error(error))
}
}

/**
Expand All @@ -203,9 +224,19 @@ export class MsgBroadcaster {
)
}

return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmosWithFeeDelegation(txWithAddresses)
: this.broadcastWeb3WithFeeDelegation(txWithAddresses)
try {
return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmosWithFeeDelegation(txWithAddresses)
: this.broadcastWeb3WithFeeDelegation(txWithAddresses)
} catch (e) {
const error = e as any

if (isThrownException(error)) {
throw error
}

throw new TransactionException(new Error(error))
}
}

/**
Expand Down
38 changes: 19 additions & 19 deletions packages/wallets/wallet-wallet-connect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ yarn add @injectivelabs/wallet-wallet-connect

## 📖 Documentation

Injective's wallet packages are intended to make it easy for developers to choose exactly what wallets - and subsequent dependencies - they
want to include in their projects.
Injective's wallet packages are intended to make it easy for developers to choose exactly what wallets - and subsequent dependencies - they want to include in their projects.

Regardless of which wallet package(s) you choose to use you must also have `@injectivelabs/wallet-core` and `@injectivelabs/wallet-base`
installed. These contain all of the types and core wallet functionality, with the separate wallet packages only providing the necessary
dependencies and implementations for their specific wallets.
Keep in mind to include specific wallet images in your public directory, right now we only have:
- Fireblocks (path: `wallet-connect/fireblocks.webp`)

Regardless of which wallet package(s) you choose to use you must also have `@injectivelabs/wallet-core` and `@injectivelabs/wallet-base` installed. These contain all of the types and core wallet functionality, with the separate wallet packages only providing the necessary dependencies and implementations for their specific wallets.

Here's a brief example of how to use this package to send 1 INJ.:

Expand Down Expand Up @@ -58,21 +58,21 @@ const msgBroadcaster = new MsgBroadcaster({
})

const sendTX = async () => {
const injectiveAddress = 'someInjectiveAddress'

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

return await msgBroadcaster.broadcast({ msgs: message })
}
const injectiveAddress = 'someInjectiveAddress'

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

return await msgBroadcaster.broadcast({ msgs: message })
}

const result = await sendTX()
const result = await sendTX()
```

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

0 comments on commit 634aa7a

Please sign in to comment.