|
| 1 | +import { TendermintTxTracer } from "@keplr-wallet/cosmos" |
| 2 | + |
| 3 | +export const suggestChain = () => { |
| 4 | + return new Promise(async (resolve, reject) => { |
| 5 | + try { |
| 6 | + await window.keplr.experimentalSuggestChain({ |
| 7 | + chainId: "mocha-4", |
| 8 | + chainName: "Mocha Testnet", |
| 9 | + rpc: "https://rpc-mocha.pops.one/", |
| 10 | + rest: "https://api-mocha.pops.one/", |
| 11 | + bip44: { |
| 12 | + coinType: 118, |
| 13 | + }, |
| 14 | + bech32Config: { |
| 15 | + bech32PrefixAccAddr: "celestia", |
| 16 | + bech32PrefixAccPub: "celestia" + "pub", |
| 17 | + bech32PrefixValAddr: "celestia" + "valoper", |
| 18 | + bech32PrefixValPub: "celestia" + "valoperpub", |
| 19 | + bech32PrefixConsAddr: "celestia" + "valcons", |
| 20 | + bech32PrefixConsPub: "celestia" + "valconspub", |
| 21 | + }, |
| 22 | + currencies: [ |
| 23 | + { |
| 24 | + coinDenom: "TIA", |
| 25 | + coinMinimalDenom: "utia", |
| 26 | + coinDecimals: 6, |
| 27 | + coinGeckoId: "celestia", |
| 28 | + }, |
| 29 | + ], |
| 30 | + feeCurrencies: [ |
| 31 | + { |
| 32 | + coinDenom: "TIA", |
| 33 | + coinMinimalDenom: "utia", |
| 34 | + coinDecimals: 6, |
| 35 | + coinGeckoId: "celestia", |
| 36 | + gasPriceStep: { |
| 37 | + low: 0.01, |
| 38 | + average: 0.02, |
| 39 | + high: 0.1, |
| 40 | + }, |
| 41 | + }, |
| 42 | + ], |
| 43 | + stakeCurrency: { |
| 44 | + coinDenom: "TIA", |
| 45 | + coinMinimalDenom: "utia", |
| 46 | + coinDecimals: 6, |
| 47 | + coinGeckoId: "celestia", |
| 48 | + }, |
| 49 | + }) |
| 50 | + |
| 51 | + await window.keplr.enable("mocha-4") |
| 52 | + |
| 53 | + resolve({ success: true }) |
| 54 | + } catch (error) { |
| 55 | + reject({ success: false, message: error.message }) |
| 56 | + } |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +export const getAccounts = () => { |
| 61 | + return new Promise(async (resolve, reject) => { |
| 62 | + try { |
| 63 | + const offlineSigner = window.getOfflineSigner("mocha-4") |
| 64 | + const accounts = await offlineSigner.getAccounts() |
| 65 | + resolve(accounts) |
| 66 | + } catch (error) { |
| 67 | + reject(error.message) |
| 68 | + } |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +export const sendMsgs = async (sender, proto, fee, memo = "") => { |
| 73 | + const account = await fetchAccountInfo(sender) |
| 74 | + const { pubKey } = await window.keplr.getKey("mocha-4") |
| 75 | + |
| 76 | + if (account) { |
| 77 | + const signDoc = { |
| 78 | + bodyBytes: TxBody.encode( |
| 79 | + TxBody.fromPartial({ |
| 80 | + messages: proto, |
| 81 | + memo, |
| 82 | + }), |
| 83 | + ).finish(), |
| 84 | + authInfoBytes: AuthInfo.encode({ |
| 85 | + signerInfos: [ |
| 86 | + { |
| 87 | + publicKey: { |
| 88 | + typeUrl: "/cosmos.crypto.secp256k1.PubKey", |
| 89 | + value: PubKey.encode({ |
| 90 | + key: pubKey, |
| 91 | + }).finish(), |
| 92 | + }, |
| 93 | + modeInfo: { |
| 94 | + single: { |
| 95 | + mode: SignMode.SIGN_MODE_DIRECT, |
| 96 | + }, |
| 97 | + multi: undefined, |
| 98 | + }, |
| 99 | + sequence: account.sequence, |
| 100 | + }, |
| 101 | + ], |
| 102 | + fee: Fee.fromPartial({ |
| 103 | + amount: fee.amount.map((coin) => { |
| 104 | + return { |
| 105 | + denom: coin.denom, |
| 106 | + amount: coin.amount.toString(), |
| 107 | + } |
| 108 | + }), |
| 109 | + gasLimit: fee.gas, |
| 110 | + }), |
| 111 | + }).finish(), |
| 112 | + chainId: "mocha-4", |
| 113 | + accountNumber: Long.fromString(account.account_number), |
| 114 | + } |
| 115 | + |
| 116 | + const signed = await window.keplr.signDirect("mocha-4", sender, signDoc) |
| 117 | + |
| 118 | + const signedTx = { |
| 119 | + tx: TxRaw.encode({ |
| 120 | + bodyBytes: signed.signed.bodyBytes, |
| 121 | + authInfoBytes: signed.signed.authInfoBytes, |
| 122 | + signatures: [Buffer.from(signed.signature.signature, "base64")], |
| 123 | + }).finish(), |
| 124 | + signDoc: signed.signed, |
| 125 | + } |
| 126 | + |
| 127 | + const txHash = await broadcastTxSync(window.keplr, "mocha-4", signedTx.tx) |
| 128 | + const txTracer = new TendermintTxTracer("https://rpc-mocha.pops.one/", "/websocket") |
| 129 | + txTracer.traceTx(txHash).then((tx) => { |
| 130 | + alert("Transaction commit successfully") |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +export const fetchAccountInfo = async (address) => { |
| 136 | + try { |
| 137 | + const uri = `https://api-mocha.pops.one/cosmos/auth/v1beta1/accounts/${address}` |
| 138 | + const response = await fetch(uri) |
| 139 | + |
| 140 | + return response.account |
| 141 | + } catch (e) { |
| 142 | + console.error("This may be a new account. Please send some tokens to this account first.") |
| 143 | + return undefined |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +export const broadcastTxSync = async (tx) => { |
| 148 | + return window.keplr.sendTx("mocha-4", tx, "sync") |
| 149 | +} |
0 commit comments