diff --git a/src/libs/data/data.js b/src/libs/data/data.js index a19c679c..a4924cb5 100644 --- a/src/libs/data/data.js +++ b/src/libs/data/data.js @@ -327,9 +327,12 @@ export function isTestnet() { || window.location.search.indexOf('testnet') > -1) } -export function formatToken(token, IBCDenom = {}, decimals = 2) { +export function formatToken(token, IBCDenom = {}, decimals = 2, withDenom = true) { if (token) { - return `${formatTokenAmount(token.amount, decimals, token.denom)} ${formatTokenDenom(IBCDenom[token.denom] || token.denom)}` + if (withDenom) { + return `${formatTokenAmount(token.amount, decimals, token.denom)} ${formatTokenDenom(IBCDenom[token.denom] || token.denom)}` + } + return formatTokenAmount(token.amount, decimals, token.denom) } return token } diff --git a/src/libs/osmos.js b/src/libs/osmos.js index cebe89e4..572cdbfb 100644 --- a/src/libs/osmos.js +++ b/src/libs/osmos.js @@ -35,34 +35,46 @@ export const poolIds = { } export const CoinGeckoMap = { - ATOM: 'cosmos', - OSMO: 'osmosis', - IRIS: 'iris-network', - AKT: 'akash-network', - LUNA: 'terra-luna', - UST: 'terrausd', - KRT: 'terra-krw', - BAND: 'band-protocol', - CRO: 'crypto-com-chain', - KAVA: 'kava', - OKT: 'okexchain', - CTK: 'certik', - XPRT: 'persistence', - REGEN: 'regen', - SCRT: 'secret', - DVPN: 'sentinel', - ION: 'ion', - ROWAN: 'sifchain', - IOV: 'starname', - BTSG: 'bitsong', - NGM: 'e-money', - EEUR: 'e-money-eur', - LIKE: 'likecoin', - JUNO: 'juno-network', - STGZ: 'stargaze-protocol', - VDL: 'vidulum', - XKI: 'ki', - INJ: 'injective-protocol', + ATOM: ['cosmos'], + OSMO: ['osmosis'], + IRIS: ['iris-network'], + AKT: ['akash-network'], + LUNA: ['terra-luna'], + UST: ['terrausd'], + KRT: ['terra-krw'], + BAND: ['band-protocol'], + CRO: ['crypto-com-chain'], + KAVA: ['kava'], + OKT: ['okexchain'], + CTK: ['certik'], + XPRT: ['persistence'], + REGEN: ['regen'], + SCRT: ['secret'], + DVPN: ['sentinel'], + ION: ['ion'], + ROWAN: ['sifchain'], + IOV: ['starname'], + BTSG: ['bitsong'], + NGM: ['e-money'], + EEUR: ['e-money-eur'], + LIKE: ['likecoin'], + JUNO: ['juno-network'], + STGZ: ['stargaze-protocol'], + VDL: ['vidulum'], + XKI: ['ki'], + INJ: ['injective-protocol'], +} + +export function getChainConfigForSymbol(symbol) { + const key = CoinGeckoMap[symbol] + if (key) { + const confs = getLocalChains() + return Object.values(confs).find(x => { + console.log(x.coingecko, key) + return x.coingecko === key[0] + }) + } + return null } export function getPairName(pool, denomTrace, type = 'base', isFormat = true) { diff --git a/src/views/components/KlineTrade/DepositeWindow.vue b/src/views/components/KlineTrade/DepositeWindow.vue index 681a14d1..12a528f6 100644 --- a/src/views/components/KlineTrade/DepositeWindow.vue +++ b/src/views/components/KlineTrade/DepositeWindow.vue @@ -4,14 +4,14 @@ id="trading-deposte-window" centered size="md" - title="Deposit / Withdraw" + title="Cross Chain Deposit Tokens" ok-title="Send" hide-header-close scrollable :ok-disabled="!address" @hidden="resetModal" @ok="handleOk" - @show="loadBalance" + @show="init" > @@ -19,56 +19,54 @@ - - - + + + + + - - - + + {{ errors[0] }} + - + - - - - -- Please select a token -- - - - - {{ format(item) }} - - + + + {{ errors[0] }} @@ -76,10 +74,16 @@ - + + Amount + + Available: {{ format(balance) }} {{ symbol }} + + + - {{ printDenom() }} + {{ symbol }} {{ errors[0] }} @@ -102,52 +106,6 @@ - - - - - - {{ errors[0] }} - - - - - - - - - - - - {{ errors[0] }} - - - - - - + + {{ feeDenom }} {{ errors[0] }} @@ -228,7 +181,7 @@ import { ValidationProvider, ValidationObserver } from 'vee-validate' import { - BModal, BRow, BCol, BInputGroup, BInputGroupAppend, BFormInput, BAvatar, BFormGroup, BFormSelect, BFormSelectOption, - BForm, BFormRadioGroup, BFormRadio, BInputGroupPrepend, BFormCheckbox, + BAvatar, BModal, BRow, BCol, BInputGroup, BInputGroupAppend, BInputGroupPrepend, BFormInput, BFormGroup, BFormSelect, BForm, BFormRadioGroup, BFormRadio, BFormCheckbox, } from 'bootstrap-vue' import { required, email, url, between, alpha, integer, password, min, digits, alphaDash, length, } from '@validations' import { - formatToken, formatTokenDenom, getLocalAccounts, getLocalChains, getUnitAmount, setLocalTxHistory, sign, timeIn, + formatToken, formatTokenDenom, getLocalAccounts, getUnitAmount, setLocalTxHistory, sign, timeIn, } from '@/libs/data' -import { Cosmos } from '@cosmostation/cosmosjs' -import vSelect from 'vue-select' import ToastificationContent from '@core/components/toastification/ToastificationContent.vue' import { coin } from '@cosmjs/amino' +import { getChainConfigForSymbol } from '@/libs/osmos' export default { name: 'TransforDialogue', components: { + BAvatar, BModal, BRow, BCol, BForm, BInputGroup, BInputGroupAppend, - BInputGroupPrepend, BFormInput, - BAvatar, BFormGroup, BFormSelect, - BFormSelectOption, BFormRadioGroup, BFormRadio, BFormCheckbox, - vSelect, + BInputGroupPrepend, ValidationProvider, ValidationObserver, @@ -320,16 +269,22 @@ export default { ToastificationContent, }, props: { - address: { + symbol: { type: String, - default: '', + default: () => '', + }, + denomTrace: { + type: Object, + default: () => {}, }, }, data() { return { + address: '', // from address for deposit / to address for withdraw + addressOptions: [], chainId: '', selectedChain: '', - balance: [], + balance: {}, token: '', amount: null, memo: '', @@ -361,72 +316,55 @@ export default { alphaDash, } }, - computed: { - feeDenoms() { - return this.balance.filter(item => !item.denom.startsWith('ibc')) - }, - destinationOptions() { - if (!this.token && this.token === '') return [] - const options = this.channels.map(x => ({ port_id: x.port_id, channel_id: x.channel_id, label: `${x.chain_id ? x.chain_id : ''} ${x.port_id}/${x.channel_id}` })) - const query = this.paths[this.token] - return query && String(this.token).startsWith('ibc/') ? options.filter(x => x.channel_id === query.channel_id) : options - }, - placeholder() { - return 'Input a destination address' - }, - }, - created() { - // console.log('address: ', this.address) - }, methods: { - tokenChange() { - this.destination = null - this.recipient = null - }, - printDenom() { - return formatTokenDenom(this.IBCDenom[this.token] || this.token) + recipientAddress() { + const { chain } = this.$route.params + const accounts = getLocalAccounts() + const current = this.$store.state.chains.defaultWallet + if (accounts && accounts[current]) { + const acc = accounts[current].address.find(x => x.chain === chain) + if (acc) { + this.recipient = acc.addr + } + } + this.selectedChain = getChainConfigForSymbol(this.symbol) + console.log('selected chain: ', this.selectedChain) }, computeAccount() { + this.recipientAddress() const accounts = getLocalAccounts() - const chains = getLocalChains() + this.addressOptions = [] if (accounts) { const values = Object.values(accounts) for (let i = 0; i < values.length; i += 1) { - const addr = values[i].address.find(x => x.addr === this.address) + const addr = values[i].address.find(x => x.chain === this.selectedChain.chain_name) if (addr) { - this.selectedChain = chains[addr.chain] - return addr + if (this.addressOptions.length === 0) this.address = addr.addr + this.addressOptions.push({ value: addr.addr, text: addr.addr }) } } } - return null + return [] }, - loadBalance() { + init() { this.destination = null this.token = '' - this.account = this.computeAccount() - if (this.account && this.account.length > 0) this.address = this.account[0].addr + this.computeAccount() + this.loadBalance() + console.log('denom trace:', this.denomTrace) + if (this.denomTrace) { + const part = this.denomTrace.path.split('/') + this.destination = { sourcePort: part[0], sourceChannel: part[1] } + console.log(this.destination) + } + }, + loadBalance() { if (this.address) { this.$http.getBankBalances(this.address, this.selectedChain).then(res => { if (res && res.length > 0) { - this.balance = res.reverse() - // this.token = this.balance[0].denom - this.feeDenom = this.balance.find(x => !x.denom.startsWith('ibc')).denom - this.balance.filter(i => i.denom.startsWith('ibc')).forEach(x => { - if (!this.IBCDenom[x.denom]) { - this.$http.getIBCDenomTrace(x.denom, this.selectedChain).then(denom => { - this.IBCDenom[x.denom] = denom.denom_trace.base_denom - // console.log(denom.denom_trace) - const path = denom.denom_trace.path.split('/') - if (path.length >= 2) { - this.paths[x.denom] = { - channel_id: path[path.length - 1], - port_id: path[path.length - 2], - } - } - }) - } - }) + this.balance = res.find(x => formatTokenDenom(x.denom) === this.symbol) + this.denom = this.balance.denom + this.feeDenom = this.balance.denom } }) this.$http.getLatestBlock(this.selectedChain).then(ret => { @@ -448,22 +386,6 @@ export default { this.sequence = ret.value.sequence ? ret.value.sequence : 0 } }) - - const channels = this.$store.state.chains.ibcChannels[this.selectedChain.chain_name] - if (!channels) { - this.$http.getIBCChannels(this.selectedChain, null).then(ret => { - const chans = ret.channels.filter(x => x.state === 'STATE_OPEN').map(x => ({ channel_id: x.channel_id, port_id: x.port_id })) - chans.forEach((x, i) => { - this.$http.getIBCChannelClientState(x.channel_id, x.port_id, this.selectedChain).then(cs => { - chans[i].chain_id = cs.identified_client_state.client_state.chain_id - this.$store.commit('setChannels', { chain: this.selectedChain.chain_name, channels: chans }) - this.$set(this, 'channels', chans) - }) - }) - }) - } else { - this.channels = channels - } } }, handleOk(bvModalEvt) { @@ -482,11 +404,7 @@ export default { this.error = null }, format(v) { - return formatToken(v, this.IBCDenom) - }, - async sendCosmos() { - const cosmos = new Cosmos(this.selectedChain.api, this.chainId) - cosmos.getAccounts() + return formatToken(v, {}, 6, false) }, async send() { if (!this.destination) { @@ -497,13 +415,13 @@ export default { { typeUrl: '/ibc.applications.transfer.v1.MsgTransfer', value: { - sourcePort: this.destination.port_id, - sourceChannel: this.destination.channel_id, - token: coin(Number(getUnitAmount(this.amount, this.token)), this.token), + sourcePort: this.destination.sourcePort, + sourceChannel: 'channel-141', // this.destination.sourceChannel, + token: coin(Number(getUnitAmount(this.amount, this.denomTrace.base_denom)), this.denomTrace.base_denom), sender: this.address, receiver: this.recipient, // timeoutHeight: undefined, // { revisionHeight: '0', revisionNumber: '0' }, - timeoutTimestamp: String((Math.floor(Date.now() / 1000) + 60) * 1_000_000_000), + timeoutTimestamp: String((Math.floor(Date.now() / 1000) + 10) * 1_000_000_000), }, }, ] @@ -524,6 +442,8 @@ export default { chainId: this.chainId, } + console.log(txMsgs, signerData, txFee) + sign( this.wallet, this.chainId, diff --git a/src/views/components/KlineTrade/PlaceForm.vue b/src/views/components/KlineTrade/PlaceForm.vue index 4f900c95..d869658a 100644 --- a/src/views/components/KlineTrade/PlaceForm.vue +++ b/src/views/components/KlineTrade/PlaceForm.vue @@ -202,7 +202,10 @@ - + @@ -283,23 +286,25 @@ export default { const p1 = this.$store.state.chains.quotes[this.target] return p1 && this.total > 0 ? (p1.usd * this.total).toFixed(2) : '-' }, - computeAccounts() { + currentDenom() { + if (this.pool && this.pool.poolAssets) { + const mode = this.type === 1 ? 0 : 1 + const { denom } = this.pool.poolAssets[mode].token + return denom + } return '' }, available() { if (this.pool && this.pool.poolAssets) { - const mode = this.type === 1 ? 0 : 1 - const { denom } = this.pool.poolAssets[mode].token let amount = 0 if (Array.isArray(this.balance)) { - console.log('balance', this.balance) this.balance.forEach(x => { - if (x.denom === denom) { + if (x.denom === this.currentDenom) { amount = x.amount } }) } - return formatTokenAmount(amount, 6, denom) + return formatTokenAmount(amount, 6, this.currentDenom) } return 0 }, @@ -327,10 +332,11 @@ export default { }, methods: { initialAddress() { + const { chain } = this.$route.params const accounts = getLocalAccounts() const current = this.$store.state.chains.defaultWallet if (accounts && accounts[current]) { - const acc = accounts[current].address.find(x => x.chain === 'osmosis') + const acc = accounts[current].address.find(x => x.chain === chain) if (acc) { this.address = acc.addr }