@mictonode/widget 0.3.16 → 0.3.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/.github/FUNDING.yml +3 -3
  2. package/.github/workflows/npm-publish.yml +34 -34
  3. package/.prettierrc.json +9 -9
  4. package/.vscode/extensions.json +3 -3
  5. package/LICENSE +674 -674
  6. package/README.md +41 -41
  7. package/dist/{main-d0e5d1e4.js → main-bbea3e54.js} +155 -153
  8. package/dist/ping-widget.js +2 -2
  9. package/dist/ping-widget.umd.cjs +18 -18
  10. package/dist/{query.lcd-3d4d3495.js → query.lcd-88036522.js} +1 -1
  11. package/dist/{query.rpc.Query-b7807c29.js → query.rpc.Query-3a2b14f8.js} +1 -1
  12. package/dist/{tx.rpc.msg-59c3408a.js → tx.rpc.msg-39f0b824.js} +1 -1
  13. package/example/.vscode/extensions.json +3 -3
  14. package/example/README.md +7 -7
  15. package/example/index.html +12 -12
  16. package/example/package.json +19 -19
  17. package/example/pnpm-lock.yaml +465 -465
  18. package/example/public/cdn.html +19 -19
  19. package/example/src/App.vue +73 -73
  20. package/example/src/main.js +4 -4
  21. package/example/vite.config.js +7 -7
  22. package/index.html +12 -12
  23. package/lib/components/ConnectWallet/index.vue +262 -262
  24. package/lib/components/TokenConvert/index.vue +1033 -1033
  25. package/lib/components/TokenConvert/tokens.ts +36 -36
  26. package/lib/components/TxDialog/index.vue +422 -422
  27. package/lib/components/TxDialog/messages/Delegate.vue +174 -174
  28. package/lib/components/TxDialog/messages/Deposit.vue +96 -96
  29. package/lib/components/TxDialog/messages/Redelegate.vue +160 -160
  30. package/lib/components/TxDialog/messages/Send.vue +142 -142
  31. package/lib/components/TxDialog/messages/Transfer.vue +248 -248
  32. package/lib/components/TxDialog/messages/Unbond.vue +103 -103
  33. package/lib/components/TxDialog/messages/Vote.vue +62 -62
  34. package/lib/components/TxDialog/messages/Withdraw.vue +55 -55
  35. package/lib/components/TxDialog/messages/WithdrawCommission.vue +74 -74
  36. package/lib/components/TxDialog/wasm/ClearAdmin.vue +55 -55
  37. package/lib/components/TxDialog/wasm/ExecuteContract.vue +98 -98
  38. package/lib/components/TxDialog/wasm/InstantiateContract.vue +108 -108
  39. package/lib/components/TxDialog/wasm/MigrateContract.vue +76 -76
  40. package/lib/components/TxDialog/wasm/MigrateContract2.vue +76 -76
  41. package/lib/components/TxDialog/wasm/StoreCode.vue +100 -100
  42. package/lib/components/TxDialog/wasm/UpdateAdmin.vue +74 -74
  43. package/lib/main.css +591 -7
  44. package/lib/main.ts +24 -23
  45. package/lib/utils/TokenUnitConverter.ts +44 -44
  46. package/lib/utils/format.ts +16 -16
  47. package/lib/utils/http.ts +154 -154
  48. package/lib/utils/type.ts +56 -56
  49. package/lib/wallet/EthermintMessageAdapter.ts +135 -135
  50. package/lib/wallet/UniClient.ts +144 -144
  51. package/lib/wallet/Wallet.ts +142 -142
  52. package/lib/wallet/wallets/KeplerWallet.ts +141 -141
  53. package/lib/wallet/wallets/LeapWallet.ts +141 -141
  54. package/lib/wallet/wallets/LedgerWallet.ts +202 -202
  55. package/lib/wallet/wallets/MetamaskSnapWallet.ts +105 -105
  56. package/lib/wallet/wallets/MetamaskWallet.ts +173 -173
  57. package/lib/wallet/wallets/OKXWallet.ts +202 -202
  58. package/lib/wallet/wallets/UnisatWallet.ts +198 -198
  59. package/package.json +5 -6
  60. package/postcss.config.js +6 -6
  61. package/src/App.vue +225 -225
  62. package/src/main.ts +7 -4
  63. package/src/styles/design-system.css +184 -0
  64. package/src/vite-env.d.ts +1 -1
  65. package/tailwind.config.js +158 -34
  66. package/tsconfig.json +25 -25
  67. package/tsconfig.node.json +10 -10
  68. package/vite.config.ts +62 -62
  69. package/vue-shim.d.ts +6 -6
@@ -1,136 +1,136 @@
1
- import { EncodeObject } from "@cosmjs/proto-signing"
2
- import {
3
- createMsgWithdrawDelegatorReward,
4
- createIBCMsgTransfer,
5
- createMsgSend,
6
- createMsgBeginRedelegate,
7
- createMsgDelegate,
8
- createMsgUndelegate,
9
- createMsgVote,
10
- // createMsgWithdrawValidatorCommission,
11
- } from '@tharsis/proto'
12
- import {
13
- MSG_WITHDRAW_DELEGATOR_REWARD_TYPES,
14
- MSG_BEGIN_REDELEGATE_TYPES,
15
- MSG_DELEGATE_TYPES,
16
- MSG_SEND_TYPES,
17
- MSG_UNDELEGATE_TYPES,
18
- MSG_VOTE_TYPES,
19
- IBC_MSG_TRANSFER_TYPES,
20
- MSG_WITHDRAW_VALIDATOR_COMMISSION_TYPES,
21
- } from "@tharsis/eip712"
22
-
23
- export interface MessageAdapter {
24
- toProto(message: EncodeObject): object
25
- getTypes: ()=> object
26
- }
27
-
28
- export class WithdrawMessageAdapter implements MessageAdapter {
29
- toProto(message: EncodeObject) {
30
- const param = message.value
31
- return createMsgWithdrawDelegatorReward(param.delegatorAddress, param.validatorAddress)
32
- }
33
- getTypes() {
34
- return MSG_WITHDRAW_DELEGATOR_REWARD_TYPES
35
- }
36
- }
37
-
38
- export class WithdrawCommissionMessageAdapter implements MessageAdapter {
39
- toProto(message: EncodeObject) {
40
- const param = message.value
41
- return createMsgWithdrawDelegatorReward(param.delegatorAddress, param.validatorAddress)
42
- }
43
- getTypes() {
44
- return MSG_WITHDRAW_VALIDATOR_COMMISSION_TYPES
45
- }
46
- }
47
-
48
- export class BeginRedelegateMessageAdapter implements MessageAdapter {
49
- toProto(message: EncodeObject) {
50
- const param = message.value
51
- return createMsgBeginRedelegate(
52
- param.delegatorAddress,
53
- param.validatorSrcAddress,
54
- param.validatorDstAddress,
55
- param.amount.amount,
56
- param.amount.denom)
57
- }
58
- getTypes() {
59
- return MSG_BEGIN_REDELEGATE_TYPES
60
- }
61
- }
62
-
63
- export class DelegateMessageAdapter implements MessageAdapter {
64
- toProto(message: EncodeObject) {
65
- const param = message.value
66
- const amount = Array.isArray(param.amount) ? param.amount[0] : param.amount
67
- return createMsgDelegate(param.delegatorAddress, param.validatorAddress, amount.amount, amount.denom)
68
- }
69
- getTypes() {
70
- return MSG_DELEGATE_TYPES
71
- }
72
- }
73
-
74
- export class SendMessageAdapter implements MessageAdapter {
75
- toProto(message: EncodeObject) {
76
- const param = message.value
77
- const amount = Array.isArray(param.amount) ? param.amount[0] : param.amount
78
- return createMsgSend(param.fromAddress, param.toAddress, amount.amount, amount.denom)
79
- }
80
- getTypes() {
81
- return MSG_SEND_TYPES
82
- }
83
- }
84
-
85
- export class UndelegateMessageAdapter implements MessageAdapter {
86
- toProto(message: EncodeObject) {
87
- const param = message.value
88
- const amount = Array.isArray(param.amount) ? param.amount[0] : param.amount
89
- return createMsgUndelegate(param.delegatorAddress, param.validatorAddress, amount.amount, amount.denom)
90
- }
91
- getTypes() {
92
- return MSG_UNDELEGATE_TYPES
93
- }
94
- }
95
-
96
- export class VoteMessageAdapter implements MessageAdapter {
97
- toProto(message: EncodeObject) {
98
- const param = message.value
99
- return createMsgVote(param.proposalId, param.option, param.voter)
100
- }
101
- getTypes() {
102
- return MSG_VOTE_TYPES
103
- }
104
- }
105
-
106
- export class IBCMessageAdapter implements MessageAdapter {
107
- toProto(message: EncodeObject) {
108
- const param = message.value
109
- return createIBCMsgTransfer(
110
- param.sourcePort,
111
- param.sourceChannel,
112
- param.amount.amount,
113
- param.amount.denom,
114
- param.sender,
115
- param.receiver,
116
- param.revisionNumber,
117
- param.revisionHeight,
118
- param.timeoutTimestamp
119
- )
120
- }
121
- getTypes() {
122
- return IBC_MSG_TRANSFER_TYPES
123
- }
124
- }
125
-
126
-
127
- export const defaultMessageAdapter: Record<string, MessageAdapter> = {
128
- "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward": new WithdrawMessageAdapter(),
129
- "/cosmos.staking.v1beta1.MsgDelegate": new DelegateMessageAdapter(),
130
- "/cosmos.staking.v1beta1.MsgBeginRedelegate": new BeginRedelegateMessageAdapter(),
131
- "/cosmos.staking.v1beta1.MsgUndelegate": new UndelegateMessageAdapter(),
132
- "/cosmos.bank.v1beta1.MsgSend": new SendMessageAdapter(),
133
- "/cosmos.gov.v1beta1.MsgVote": new VoteMessageAdapter(),
134
- "/ibc.applications.transfer.v1.MsgTransfer": new IBCMessageAdapter(),
135
- "/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission": new WithdrawCommissionMessageAdapter()
1
+ import { EncodeObject } from "@cosmjs/proto-signing"
2
+ import {
3
+ createMsgWithdrawDelegatorReward,
4
+ createIBCMsgTransfer,
5
+ createMsgSend,
6
+ createMsgBeginRedelegate,
7
+ createMsgDelegate,
8
+ createMsgUndelegate,
9
+ createMsgVote,
10
+ // createMsgWithdrawValidatorCommission,
11
+ } from '@tharsis/proto'
12
+ import {
13
+ MSG_WITHDRAW_DELEGATOR_REWARD_TYPES,
14
+ MSG_BEGIN_REDELEGATE_TYPES,
15
+ MSG_DELEGATE_TYPES,
16
+ MSG_SEND_TYPES,
17
+ MSG_UNDELEGATE_TYPES,
18
+ MSG_VOTE_TYPES,
19
+ IBC_MSG_TRANSFER_TYPES,
20
+ MSG_WITHDRAW_VALIDATOR_COMMISSION_TYPES,
21
+ } from "@tharsis/eip712"
22
+
23
+ export interface MessageAdapter {
24
+ toProto(message: EncodeObject): object
25
+ getTypes: ()=> object
26
+ }
27
+
28
+ export class WithdrawMessageAdapter implements MessageAdapter {
29
+ toProto(message: EncodeObject) {
30
+ const param = message.value
31
+ return createMsgWithdrawDelegatorReward(param.delegatorAddress, param.validatorAddress)
32
+ }
33
+ getTypes() {
34
+ return MSG_WITHDRAW_DELEGATOR_REWARD_TYPES
35
+ }
36
+ }
37
+
38
+ export class WithdrawCommissionMessageAdapter implements MessageAdapter {
39
+ toProto(message: EncodeObject) {
40
+ const param = message.value
41
+ return createMsgWithdrawDelegatorReward(param.delegatorAddress, param.validatorAddress)
42
+ }
43
+ getTypes() {
44
+ return MSG_WITHDRAW_VALIDATOR_COMMISSION_TYPES
45
+ }
46
+ }
47
+
48
+ export class BeginRedelegateMessageAdapter implements MessageAdapter {
49
+ toProto(message: EncodeObject) {
50
+ const param = message.value
51
+ return createMsgBeginRedelegate(
52
+ param.delegatorAddress,
53
+ param.validatorSrcAddress,
54
+ param.validatorDstAddress,
55
+ param.amount.amount,
56
+ param.amount.denom)
57
+ }
58
+ getTypes() {
59
+ return MSG_BEGIN_REDELEGATE_TYPES
60
+ }
61
+ }
62
+
63
+ export class DelegateMessageAdapter implements MessageAdapter {
64
+ toProto(message: EncodeObject) {
65
+ const param = message.value
66
+ const amount = Array.isArray(param.amount) ? param.amount[0] : param.amount
67
+ return createMsgDelegate(param.delegatorAddress, param.validatorAddress, amount.amount, amount.denom)
68
+ }
69
+ getTypes() {
70
+ return MSG_DELEGATE_TYPES
71
+ }
72
+ }
73
+
74
+ export class SendMessageAdapter implements MessageAdapter {
75
+ toProto(message: EncodeObject) {
76
+ const param = message.value
77
+ const amount = Array.isArray(param.amount) ? param.amount[0] : param.amount
78
+ return createMsgSend(param.fromAddress, param.toAddress, amount.amount, amount.denom)
79
+ }
80
+ getTypes() {
81
+ return MSG_SEND_TYPES
82
+ }
83
+ }
84
+
85
+ export class UndelegateMessageAdapter implements MessageAdapter {
86
+ toProto(message: EncodeObject) {
87
+ const param = message.value
88
+ const amount = Array.isArray(param.amount) ? param.amount[0] : param.amount
89
+ return createMsgUndelegate(param.delegatorAddress, param.validatorAddress, amount.amount, amount.denom)
90
+ }
91
+ getTypes() {
92
+ return MSG_UNDELEGATE_TYPES
93
+ }
94
+ }
95
+
96
+ export class VoteMessageAdapter implements MessageAdapter {
97
+ toProto(message: EncodeObject) {
98
+ const param = message.value
99
+ return createMsgVote(param.proposalId, param.option, param.voter)
100
+ }
101
+ getTypes() {
102
+ return MSG_VOTE_TYPES
103
+ }
104
+ }
105
+
106
+ export class IBCMessageAdapter implements MessageAdapter {
107
+ toProto(message: EncodeObject) {
108
+ const param = message.value
109
+ return createIBCMsgTransfer(
110
+ param.sourcePort,
111
+ param.sourceChannel,
112
+ param.amount.amount,
113
+ param.amount.denom,
114
+ param.sender,
115
+ param.receiver,
116
+ param.revisionNumber,
117
+ param.revisionHeight,
118
+ param.timeoutTimestamp
119
+ )
120
+ }
121
+ getTypes() {
122
+ return IBC_MSG_TRANSFER_TYPES
123
+ }
124
+ }
125
+
126
+
127
+ export const defaultMessageAdapter: Record<string, MessageAdapter> = {
128
+ "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward": new WithdrawMessageAdapter(),
129
+ "/cosmos.staking.v1beta1.MsgDelegate": new DelegateMessageAdapter(),
130
+ "/cosmos.staking.v1beta1.MsgBeginRedelegate": new BeginRedelegateMessageAdapter(),
131
+ "/cosmos.staking.v1beta1.MsgUndelegate": new UndelegateMessageAdapter(),
132
+ "/cosmos.bank.v1beta1.MsgSend": new SendMessageAdapter(),
133
+ "/cosmos.gov.v1beta1.MsgVote": new VoteMessageAdapter(),
134
+ "/ibc.applications.transfer.v1.MsgTransfer": new IBCMessageAdapter(),
135
+ "/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission": new WithdrawCommissionMessageAdapter()
136
136
  }
@@ -1,145 +1,145 @@
1
- import { toBase64, fromBase64, toHex, fromBech32 } from "@cosmjs/encoding";
2
- import { EncodeObject, encodePubkey, Registry } from '@cosmjs/proto-signing'
3
- import { encodeSecp256k1Pubkey } from "@cosmjs/amino";
4
- import { defaultRegistryTypes } from "@cosmjs/stargate";
5
- import { AuthInfo, TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
6
- import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
7
- import { AbstractWallet, WalletArgument, WalletName, createWallet } from "./Wallet";
8
- import { post } from "../utils/http";
9
- import { BroadcastMode, Transaction, TxResponse } from "../utils/type";
10
- import { wasmTypes } from "@cosmjs/cosmwasm-stargate/build/modules";
11
- import { makeAuthInfoBytes, makeSignDoc, TxBodyEncodeObject } from "@cosmjs/proto-signing/build";
12
- import { Any } from "cosmjs-types/google/protobuf/any";
13
-
14
- export function isEthermint(chainId: string) {
15
- return chainId.search(/\w+_\d+-\d+/g) > -1
16
- }
17
-
18
- export class UniClient {
19
- registry: Registry
20
- wallet: AbstractWallet
21
- constructor(name: WalletName, arg: WalletArgument) {
22
- this.registry = new Registry([...defaultRegistryTypes, ...wasmTypes])
23
- this.wallet = createWallet(name, arg, this.registry)
24
- }
25
-
26
- async getAccounts() {
27
- return this.wallet.getAccounts()
28
- }
29
-
30
- async sign(transaction: Transaction): Promise<TxRaw> {
31
- // const { signature, signed } = await this.wallet.sign(transaction);
32
- // return TxRaw.fromPartial({
33
- // bodyBytes: signed.bodyBytes,
34
- // authInfoBytes: signed.authInfoBytes,
35
- // signatures: [fromBase64(signature.signature)],
36
- // });
37
- return this.wallet.sign(transaction)
38
- }
39
-
40
- async simulate (
41
- endpoint: string,
42
- transaction: Transaction,
43
- mode: BroadcastMode = BroadcastMode.SYNC
44
- ) {
45
-
46
- const pubkey = Any.fromPartial({
47
- typeUrl: '/cosmos.crypto.ed25519.PubKey',
48
- value: new Uint8Array()
49
- })
50
- const txBodyEncodeObject: TxBodyEncodeObject = {
51
- typeUrl: "/cosmos.tx.v1beta1.TxBody",
52
- value: {
53
- messages: transaction.messages,
54
- memo: transaction.memo,
55
- },
56
- };
57
-
58
- const txBodyBytes = this.registry.encode(txBodyEncodeObject);
59
- const gasLimit = Number(transaction.fee.gas);
60
- const authInfoBytes = makeAuthInfoBytes(
61
- [{ pubkey, sequence: transaction.signerData.sequence }],
62
- transaction.fee.amount,
63
- gasLimit,
64
- transaction.fee.granter,
65
- transaction.fee.payer,
66
- );
67
-
68
- const txRaw = TxRaw.fromPartial({
69
- bodyBytes: txBodyBytes,
70
- authInfoBytes: authInfoBytes,
71
- signatures: [new Uint8Array()],
72
- });
73
-
74
- const txbytes = toBase64(TxRaw.encode(txRaw).finish())
75
- const request = {
76
- tx_bytes: txbytes,
77
- mode, // BROADCAST_MODE_SYNC, BROADCAST_MODE_BLOCK, BROADCAST_MODE_ASYNC
78
- }
79
- return post(`${endpoint}/cosmos/tx/v1beta1/simulate`, request).then(res => {
80
- if (res.code && res.code !== 0) {
81
- throw new Error(res.message)
82
- }
83
- if (res.tx_response && res.tx_response.code !== 0) {
84
- throw new Error(res.tx_response.raw_log)
85
- }
86
- return Number(res.gas_info.gas_used)
87
- })
88
- }
89
-
90
- // async simulate2(
91
- // endpoint: string,
92
- // messages: readonly EncodeObject[],
93
- // memo: string | undefined,
94
- // sequence: number
95
- // ) {
96
-
97
- // const [first] = await this.wallet.getAccounts()
98
- // const pubkey = encodeSecp256k1Pubkey(first.pubkey);
99
- // const anyMsgs = messages.map((m) => this.registry.encodeAsAny(m));
100
- // const url = `${endpoint}/cosmos/tx/v1beta1/simulate`
101
- // const tx = Tx.fromPartial({
102
- // authInfo: AuthInfo.fromPartial({
103
- // fee: Fee.fromPartial({}),
104
- // signerInfos: [
105
- // {
106
- // publicKey: encodePubkey(pubkey),
107
- // sequence: sequence,
108
- // modeInfo: { single: { mode: SignMode.SIGN_MODE_UNSPECIFIED } },
109
- // },
110
- // ],
111
- // }),
112
- // body: TxBody.fromPartial({
113
- // messages: Array.from(anyMsgs),
114
- // memo: memo,
115
- // }),
116
- // signatures: [new Uint8Array()],
117
- // });
118
- // const request = SimulateRequest.fromPartial({
119
- // tx
120
- // // txBytes: Tx.encode(tx).finish(),
121
- // });
122
- // console.log(tx, request)
123
- // return await post(url, request)
124
- // }
125
-
126
-
127
- async broadcastTx(endpoint, bodyBytes: TxRaw, mode: BroadcastMode = BroadcastMode.SYNC) : Promise<{tx_response: TxResponse}> {
128
- // const txbytes = bodyBytes.authInfoBytes ? TxRaw.encode(bodyBytes).finish() : bodyBytes
129
- const txbytes = TxRaw.encode(bodyBytes).finish()
130
- const txString = toBase64(txbytes)
131
- const txRaw = {
132
- tx_bytes: txString,
133
- mode, // BROADCAST_MODE_SYNC, BROADCAST_MODE_BLOCK, BROADCAST_MODE_ASYNC
134
- }
135
- return post(`${endpoint}/cosmos/tx/v1beta1/txs`, txRaw).then(res => {
136
- if (res.code && res.code !== 0) {
137
- throw new Error(res.message)
138
- }
139
- if (res.tx_response && res.tx_response.code !== 0) {
140
- throw new Error(res.tx_response.raw_log)
141
- }
142
- return res
143
- })
144
- }
1
+ import { toBase64, fromBase64, toHex, fromBech32 } from "@cosmjs/encoding";
2
+ import { EncodeObject, encodePubkey, Registry } from '@cosmjs/proto-signing'
3
+ import { encodeSecp256k1Pubkey } from "@cosmjs/amino";
4
+ import { defaultRegistryTypes } from "@cosmjs/stargate";
5
+ import { AuthInfo, TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
6
+ import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
7
+ import { AbstractWallet, WalletArgument, WalletName, createWallet } from "./Wallet";
8
+ import { post } from "../utils/http";
9
+ import { BroadcastMode, Transaction, TxResponse } from "../utils/type";
10
+ import { wasmTypes } from "@cosmjs/cosmwasm-stargate/build/modules";
11
+ import { makeAuthInfoBytes, makeSignDoc, TxBodyEncodeObject } from "@cosmjs/proto-signing/build";
12
+ import { Any } from "cosmjs-types/google/protobuf/any";
13
+
14
+ export function isEthermint(chainId: string) {
15
+ return chainId.search(/\w+_\d+-\d+/g) > -1
16
+ }
17
+
18
+ export class UniClient {
19
+ registry: Registry
20
+ wallet: AbstractWallet
21
+ constructor(name: WalletName, arg: WalletArgument) {
22
+ this.registry = new Registry([...defaultRegistryTypes, ...wasmTypes])
23
+ this.wallet = createWallet(name, arg, this.registry)
24
+ }
25
+
26
+ async getAccounts() {
27
+ return this.wallet.getAccounts()
28
+ }
29
+
30
+ async sign(transaction: Transaction): Promise<TxRaw> {
31
+ // const { signature, signed } = await this.wallet.sign(transaction);
32
+ // return TxRaw.fromPartial({
33
+ // bodyBytes: signed.bodyBytes,
34
+ // authInfoBytes: signed.authInfoBytes,
35
+ // signatures: [fromBase64(signature.signature)],
36
+ // });
37
+ return this.wallet.sign(transaction)
38
+ }
39
+
40
+ async simulate (
41
+ endpoint: string,
42
+ transaction: Transaction,
43
+ mode: BroadcastMode = BroadcastMode.SYNC
44
+ ) {
45
+
46
+ const pubkey = Any.fromPartial({
47
+ typeUrl: '/cosmos.crypto.ed25519.PubKey',
48
+ value: new Uint8Array()
49
+ })
50
+ const txBodyEncodeObject: TxBodyEncodeObject = {
51
+ typeUrl: "/cosmos.tx.v1beta1.TxBody",
52
+ value: {
53
+ messages: transaction.messages,
54
+ memo: transaction.memo,
55
+ },
56
+ };
57
+
58
+ const txBodyBytes = this.registry.encode(txBodyEncodeObject);
59
+ const gasLimit = Number(transaction.fee.gas);
60
+ const authInfoBytes = makeAuthInfoBytes(
61
+ [{ pubkey, sequence: transaction.signerData.sequence }],
62
+ transaction.fee.amount,
63
+ gasLimit,
64
+ transaction.fee.granter,
65
+ transaction.fee.payer,
66
+ );
67
+
68
+ const txRaw = TxRaw.fromPartial({
69
+ bodyBytes: txBodyBytes,
70
+ authInfoBytes: authInfoBytes,
71
+ signatures: [new Uint8Array()],
72
+ });
73
+
74
+ const txbytes = toBase64(TxRaw.encode(txRaw).finish())
75
+ const request = {
76
+ tx_bytes: txbytes,
77
+ mode, // BROADCAST_MODE_SYNC, BROADCAST_MODE_BLOCK, BROADCAST_MODE_ASYNC
78
+ }
79
+ return post(`${endpoint}/cosmos/tx/v1beta1/simulate`, request).then(res => {
80
+ if (res.code && res.code !== 0) {
81
+ throw new Error(res.message)
82
+ }
83
+ if (res.tx_response && res.tx_response.code !== 0) {
84
+ throw new Error(res.tx_response.raw_log)
85
+ }
86
+ return Number(res.gas_info.gas_used)
87
+ })
88
+ }
89
+
90
+ // async simulate2(
91
+ // endpoint: string,
92
+ // messages: readonly EncodeObject[],
93
+ // memo: string | undefined,
94
+ // sequence: number
95
+ // ) {
96
+
97
+ // const [first] = await this.wallet.getAccounts()
98
+ // const pubkey = encodeSecp256k1Pubkey(first.pubkey);
99
+ // const anyMsgs = messages.map((m) => this.registry.encodeAsAny(m));
100
+ // const url = `${endpoint}/cosmos/tx/v1beta1/simulate`
101
+ // const tx = Tx.fromPartial({
102
+ // authInfo: AuthInfo.fromPartial({
103
+ // fee: Fee.fromPartial({}),
104
+ // signerInfos: [
105
+ // {
106
+ // publicKey: encodePubkey(pubkey),
107
+ // sequence: sequence,
108
+ // modeInfo: { single: { mode: SignMode.SIGN_MODE_UNSPECIFIED } },
109
+ // },
110
+ // ],
111
+ // }),
112
+ // body: TxBody.fromPartial({
113
+ // messages: Array.from(anyMsgs),
114
+ // memo: memo,
115
+ // }),
116
+ // signatures: [new Uint8Array()],
117
+ // });
118
+ // const request = SimulateRequest.fromPartial({
119
+ // tx
120
+ // // txBytes: Tx.encode(tx).finish(),
121
+ // });
122
+ // console.log(tx, request)
123
+ // return await post(url, request)
124
+ // }
125
+
126
+
127
+ async broadcastTx(endpoint, bodyBytes: TxRaw, mode: BroadcastMode = BroadcastMode.SYNC) : Promise<{tx_response: TxResponse}> {
128
+ // const txbytes = bodyBytes.authInfoBytes ? TxRaw.encode(bodyBytes).finish() : bodyBytes
129
+ const txbytes = TxRaw.encode(bodyBytes).finish()
130
+ const txString = toBase64(txbytes)
131
+ const txRaw = {
132
+ tx_bytes: txString,
133
+ mode, // BROADCAST_MODE_SYNC, BROADCAST_MODE_BLOCK, BROADCAST_MODE_ASYNC
134
+ }
135
+ return post(`${endpoint}/cosmos/tx/v1beta1/txs`, txRaw).then(res => {
136
+ if (res.code && res.code !== 0) {
137
+ throw new Error(res.message)
138
+ }
139
+ if (res.tx_response && res.tx_response.code !== 0) {
140
+ throw new Error(res.tx_response.raw_log)
141
+ }
142
+ return res
143
+ })
144
+ }
145
145
  }