Skip to content

Commit

Permalink
chain by hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
xdeq committed Apr 9, 2024
1 parent e987607 commit 04f1b99
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 85 deletions.
25 changes: 21 additions & 4 deletions components/Connection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Dropdown, DropdownItem, DropdownDivider } from "@/components/ui/Dropdow
/** Services */
import { suggestChain, getAccounts, disconnect } from "@/services/keplr"
import { arabica, mocha } from "@/services/chains"
/** Store */
import { useAppStore } from "@/store/app"
Expand All @@ -20,11 +21,27 @@ const isWalletAvailable = ref(false)
const isFetchingAccounts = ref(false)
const account = ref()
const { hostname } = useRequestURL()
switch (hostname) {
case "dev.celenium.io":
case "arabica.celenium.io":
case "celenium.io":
case "localhost":
appStore.network = arabica
break
case "mocha.celenium.io":
case "mocha-4.celenium.io":
appStore.network = mocha
break
}
const getBalance = async () => {
const key = await window.keplr.getKey("arabica-11")
const key = await window.keplr.getKey(appStore.network.chainId)
if (key) {
const uri = `https://api.celestia-arabica-11.com/cosmos/bank/v1beta1/balances/${key.bech32Address}?pagination.limit=1000`
const uri = `${appStore.network.rest}/cosmos/bank/v1beta1/balances/${key.bech32Address}?pagination.limit=1000`
const data = await $fetch(uri)
const celestiaBalance = data.balances.find((balance) => balance.denom === "utia")
Expand All @@ -39,11 +56,11 @@ onMounted(async () => {
const handleConnect = async () => {
try {
await suggestChain()
await suggestChain(appStore.network)
isFetchingAccounts.value = true
const accounts = await getAccounts()
const accounts = await getAccounts(appStore.network)
if (accounts.length) {
account.value = accounts[0].address
appStore.address = accounts[0].address
Expand Down
6 changes: 4 additions & 2 deletions components/modals/ConstantsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ const searchTerm = ref("")
const rawModules = ref({})
const { data } = await fetchConstants()
rawModules.value = data.value.module
onMounted(async () => {
const data = await fetchConstants()
rawModules.value = data.module
})
const modules = computed(() => {
const result = []
Expand Down
2 changes: 1 addition & 1 deletion components/modals/PayForBlobModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ const handleContinue = async () => {
try {
isAwaiting.value = true
await sendPayForBlob(appStore.address, proto, stdFee, decodableBlob)
await sendPayForBlob(appStore.network, appStore.address, proto, stdFee, decodableBlob)
isAwaiting.value = false
notificationsStore.create({
Expand Down
11 changes: 8 additions & 3 deletions components/modals/SendModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ const runGasLimitEstimation = async () => {
}).finish(),
}
const gasUsed = await simulateMsgs("celestia1pssnclfxcn4vgxyyy7z8sdvr2xcznwq84d0wvy", [protoMsgs], [{ denom: "utia", amount: "1" }])
const gasUsed = await simulateMsgs(
appStore.network,
"celestia1pssnclfxcn4vgxyyy7z8sdvr2xcznwq84d0wvy",
[protoMsgs],
[{ denom: "utia", amount: "1" }],
)
estimatedGasLimit.value = parseInt(gasUsed)
}
Expand Down Expand Up @@ -207,7 +212,7 @@ watch(
)
const handleContinue = async () => {
const key = await window.keplr?.getKey("arabica-11")
const key = await window.keplr?.getKey(appStore.network.chainId)
const proto = [
{
Expand Down Expand Up @@ -239,7 +244,7 @@ const handleContinue = async () => {
try {
isAwaiting.value = true
await sendMsgs(key.bech32Address, proto, stdFee)
await sendMsgs(appStore.network, key.bech32Address, proto, stdFee)
isAwaiting.value = false
notificationsStore.create({
Expand Down
2 changes: 1 addition & 1 deletion services/api/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const fetchHead = async () => {

export const fetchConstants = async () => {
try {
const data = await useFetch(`${useServerURL()}/constants`)
const data = await $fetch(`${useServerURL()}/constants`)
return data
} catch (error) {
console.error(error)
Expand Down
88 changes: 88 additions & 0 deletions services/chains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
export const mocha = {
chainId: "mocha-4",
chainName: "Celestia Mocha Testnet",
rpc: "https://rpc-celestia-testnet-mocha.keplr.app",
rest: "https://lcd-celestia-testnet-mocha.keplr.app",
chainSymbolImageUrl: "https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/mocha/chain.png",
stakeCurrency: {
coinDenom: "TIA",
coinMinimalDenom: "utia",
coinDecimals: 6,
},
bip44: {
coinType: 118,
},
bech32Config: {
bech32PrefixAccAddr: "celestia",
bech32PrefixAccPub: "celestiapub",
bech32PrefixValAddr: "celestiavaloper",
bech32PrefixValPub: "celestiavaloperpub",
bech32PrefixConsAddr: "celestiavalcons",
bech32PrefixConsPub: "celestiavalconspub",
},
currencies: [
{
coinDenom: "TIA",
coinMinimalDenom: "utia",
coinDecimals: 6,
},
],
feeCurrencies: [
{
coinDenom: "TIA",
coinMinimalDenom: "utia",
coinDecimals: 6,
gasPriceStep: {
low: 0.01,
average: 0.02,
high: 0.1,
},
},
],
features: [],
}

export const arabica = {
chainId: "arabica-11",
chainName: "Celestia Arabica Testnet",
rpc: "https://rpc.celestia-arabica-11.com",
rest: "https://api.celestia-arabica-11.com",
bip44: {
coinType: 118,
},
bech32Config: {
bech32PrefixAccAddr: "celestia",
bech32PrefixAccPub: "celestia" + "pub",
bech32PrefixValAddr: "celestia" + "valoper",
bech32PrefixValPub: "celestia" + "valoperpub",
bech32PrefixConsAddr: "celestia" + "valcons",
bech32PrefixConsPub: "celestia" + "valconspub",
},
currencies: [
{
coinDenom: "TIA",
coinMinimalDenom: "utia",
coinDecimals: 6,
coinGeckoId: "celestia",
},
],
feeCurrencies: [
{
coinDenom: "TIA",
coinMinimalDenom: "utia",
coinDecimals: 6,
coinGeckoId: "celestia",
gasPriceStep: {
low: 0.01,
average: 0.025,
high: 0.3,
},
},
],
stakeCurrency: {
coinDenom: "TIA",
coinMinimalDenom: "utia",
coinDecimals: 6,
coinGeckoId: "celestia",
},
}
97 changes: 23 additions & 74 deletions services/keplr.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,17 @@
import Long from "long"
import Base64 from "crypto-js/enc-base64"
import Hex from "crypto-js/enc-hex"

import { AuthInfo, Fee, TxBody, TxRaw, SignerInfo } from "./proto/gen/tx"
import { SignMode } from "./proto/gen/signing"
import { PubKey } from "./proto/gen/keys"

import Long from "long"

const arabica = {
chainId: "arabica-11",
chainName: "Arabica Celestia",
rpc: "https://rpc.celestia-arabica-11.com",
rest: "https://api.celestia-arabica-11.com",
bip44: {
coinType: 118,
},
bech32Config: {
bech32PrefixAccAddr: "celestia",
bech32PrefixAccPub: "celestia" + "pub",
bech32PrefixValAddr: "celestia" + "valoper",
bech32PrefixValPub: "celestia" + "valoperpub",
bech32PrefixConsAddr: "celestia" + "valcons",
bech32PrefixConsPub: "celestia" + "valconspub",
},
currencies: [
{
coinDenom: "TIA",
coinMinimalDenom: "utia",
coinDecimals: 6,
coinGeckoId: "celestia",
},
],
feeCurrencies: [
{
coinDenom: "TIA",
coinMinimalDenom: "utia",
coinDecimals: 6,
coinGeckoId: "celestia",
gasPriceStep: {
low: 0.01,
average: 0.025,
high: 0.3,
},
},
],
stakeCurrency: {
coinDenom: "TIA",
coinMinimalDenom: "utia",
coinDecimals: 6,
coinGeckoId: "celestia",
},
}

export const suggestChain = () => {
export const suggestChain = (network) => {
return new Promise(async (resolve, reject) => {
try {
await window.keplr.experimentalSuggestChain(arabica)
await window.keplr.experimentalSuggestChain(network)

await window.keplr.enable(arabica.chainId)
await window.keplr.enable(network.chainId)

resolve({ success: true })
} catch (error) {
Expand All @@ -70,10 +24,10 @@ export const disconnect = () => {
window.keplr.disable()
}

export const getAccounts = () => {
export const getAccounts = (network) => {
return new Promise(async (resolve, reject) => {
try {
const offlineSigner = window.getOfflineSigner(arabica.chainId)
const offlineSigner = window.getOfflineSigner(network.chainId)
const accounts = await offlineSigner.getAccounts()
resolve(accounts)
} catch (error) {
Expand All @@ -90,9 +44,9 @@ const buildPayForBlob = (tx, blob) => {
return blobTx.serializeBinary()
}

export const sendPayForBlob = async (sender, proto, fee, blob) => {
const account = await fetchAccountInfo(sender)
const { pubKey } = await window.keplr.getKey(arabica.chainId)
export const sendPayForBlob = async (network, sender, proto, fee, blob) => {
const account = await fetchAccountInfo(network, sender)
const { pubKey } = await window.keplr.getKey(network.chainId)

const tx = TxBody.encode(
TxBody.fromPartial({
Expand Down Expand Up @@ -132,11 +86,11 @@ export const sendPayForBlob = async (sender, proto, fee, blob) => {
gasLimit: fee.gas,
}),
}).finish(),
chainId: arabica.chainId,
chainId: network.chainId,
accountNumber: Long.fromString(account.account_number),
}

const signed = await keplr.signDirect(arabica.chainId, sender, signDoc)
const signed = await keplr.signDirect(network.chainId, sender, signDoc)

const body = buildPayForBlob(
TxRaw.encode({
Expand All @@ -152,13 +106,13 @@ export const sendPayForBlob = async (sender, proto, fee, blob) => {
signDoc: signed.signed,
}

const txHash = await broadcastTxSync(arabica.chainId, signedTx.tx)
const txHash = await broadcastTxSync(network.chainId, signedTx.tx)
return txHash
}
}

export const simulateMsgs = async (sender, proto, fee) => {
const account = await fetchAccountInfo(sender)
export const simulateMsgs = async (network, sender, proto, fee) => {
const account = await fetchAccountInfo(network, sender)

if (account) {
const unsignedTx = TxRaw.encode({
Expand Down Expand Up @@ -192,7 +146,7 @@ export const simulateMsgs = async (sender, proto, fee) => {
signatures: [new Uint8Array(64)],
}).finish()

const simulatedResult = await $fetch(`${arabica.rest}/cosmos/tx/v1beta1/simulate`, {
const simulatedResult = await $fetch(`${network.rest}/cosmos/tx/v1beta1/simulate`, {
method: "POST",
headers: {
"content-type": "application/json",
Expand All @@ -213,9 +167,9 @@ export const simulateMsgs = async (sender, proto, fee) => {
return undefined
}

export const sendMsgs = async (sender, proto, fee) => {
const account = await fetchAccountInfo(sender)
const { pubKey } = await window.keplr.getKey(arabica.chainId)
export const sendMsgs = async (network, sender, proto, fee) => {
const account = await fetchAccountInfo(network, sender)
const { pubKey } = await window.keplr.getKey(network.chainId)

const tx = TxBody.encode(
TxBody.fromPartial({
Expand Down Expand Up @@ -255,11 +209,11 @@ export const sendMsgs = async (sender, proto, fee) => {
gasLimit: fee.gas,
}),
}).finish(),
chainId: arabica.chainId,
chainId: network.chainId,
accountNumber: Long.fromString(account.account_number),
}

const signed = await window.keplr.signDirect(arabica.chainId, sender, signDoc)
const signed = await window.keplr.signDirect(network.chainId, sender, signDoc)

const signedTx = {
tx: TxRaw.encode({
Expand All @@ -270,18 +224,13 @@ export const sendMsgs = async (sender, proto, fee) => {
signDoc: signed.signed,
}

const txHash = await broadcastTxSync(arabica.chainId, signedTx.tx)

// const txTracer = new TendermintTxTracer(arabica.rpc, "/websocket")
// txTracer.traceTx(txHash).then((tx) => {
// console.log(tx)
// })
const txHash = await broadcastTxSync(network.chainId, signedTx.tx)
}
}

export const fetchAccountInfo = async (address) => {
export const fetchAccountInfo = async (network, address) => {
try {
const uri = `${arabica.rest}/cosmos/auth/v1beta1/accounts/${address}`
const uri = `${network.rest}/cosmos/auth/v1beta1/accounts/${address}`
const response = await $fetch(uri)
return response.account
} catch (e) {
Expand Down
Loading

0 comments on commit 04f1b99

Please sign in to comment.