Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add carbon chain with optimisations #101

Open
wants to merge 4 commits into
base: v2-finial
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions public/logos/carbon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions src/chains/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@
- **api** the rest api endpoint.(make sure that CORS is enabled: `Allow-Control-Allow-Origin: *`)
- **rpc** the rpc endpoint, make sure that the port is added. rpc endpoint is only used for state sync. it's optional.
- **assets** Native Assets on blockchain.

# Additional Signing Configuration
```json
{
"chain_name": "cosmos",
...
"sign_opts": {
"keplr": {
"preferNoSetFee": true,
"preferNoSetMemo": true
}
},
...
}
```
- **sign_opts** (optional) configuration for sign requests, only Keplr is supported at the moment.
- **sign_opts.keplr** (optional) KeplrSignOptions - See Keplr [documentation](https://docs.keplr.app/api/#interaction-options).

# Test

please add these check points in comments with your PR, and adding your test result by clicking the checkbox of each line
Expand Down
24 changes: 24 additions & 0 deletions src/chains/mainnet/carbon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"chain_name": "carbon",
"coingecko": "switcheo",
"api": "https://api.carbon.network",
"rpc": ["https://tm-api.carbon.network"],
"snapshot_provider": "",
"sdk_version": "0.45.2",
"coin_type": "118",
"min_tx_fee": "100000000",
"addr_prefix": "swth",
"logo": "/logos/carbon.svg",
"sign_opts": {
"keplr": {
"preferNoSetFee": true
}
},
"assets": [{
"base": "swth",
"symbol": "SWTH",
"exponent": "8",
"coingecko_id": "switcheo",
"logo": "/logos/carbon.svg"
}]
}
24 changes: 24 additions & 0 deletions src/chains/testnet/carbon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"chain_name": "carbon",
"coingecko": "switcheo",
"api": "https://test-api.carbon.network",
"rpc": ["https://test-tm-api.carbon.network"],
"snapshot_provider": "",
"sdk_version": "0.45.2",
"coin_type": "118",
"min_tx_fee": "100000000",
"addr_prefix": "swth",
"logo": "/logos/carbon.svg",
"sign_opts": {
"keplr": {
"preferNoSetFee": true
}
},
"assets": [{
"base": "swth",
"symbol": "SWTH",
"exponent": "8",
"coingecko_id": "switcheo",
"logo": "/logos/carbon.svg"
}]
}
16 changes: 14 additions & 2 deletions src/libs/data/signing.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export default class PingWalletClient extends SigningStargateClient {
return instance
}

async signAmino2(signerAddress, messages, fee, memo, { accountNumber, sequence, chainId }) {
async signAmino2(signerAddress, messages, fee, memo, signerData, signOpts = {}) {
const { accountNumber, sequence, chainId } = signerData
// utils_1.assert(!proto_signing_1.isOfflineDirectSigner(this.signer))
const accountFromSigner = (await this.signer.getAccounts()).find(account => account.address === signerAddress)
if (!accountFromSigner) {
Expand All @@ -37,7 +38,18 @@ export default class PingWalletClient extends SigningStargateClient {
const msgs = messages.map(msg => this.aminoTypes.toAmino(msg))
// console.log('msgs:', msgs)
const signDoc = amino_1.makeSignDoc(msgs, fee, chainId, memo, accountNumber, sequence)
const { signature, signed } = await this.signer.signAmino(signerAddress, signDoc)

let signature = null
let signed = null
if (this.signer.keplr && signOpts.keplr?.preferNoSetFee) {
// additional keplr signer specific sign opts
({ signature, signed } = await this.signer.keplr.signAmino(this.signer.chainId, signerAddress, signDoc, {
preferNoSetFee: signOpts.keplr.preferNoSetFee,
}))
} else {
({ signature, signed } = await this.signer.signAmino(signerAddress, signDoc))
}

const signedTxBody = {
messages: signed.msgs.map(msg => this.aminoTypes.fromAmino(msg)),
memo: signed.memo,
Expand Down
19 changes: 17 additions & 2 deletions src/libs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function getHdPath(address) {
return stringToPath(hdPath)
}

export async function sign(device, chainId, signerAddress, messages, fee, memo, signerData) {
export async function sign(device, chainId, signerAddress, messages, fee, memo, signerData, signOpts) {
let transport
let signer
switch (device) {
Expand Down Expand Up @@ -226,7 +226,7 @@ export async function sign(device, chainId, signerAddress, messages, fee, memo,
// Ensure the address has some tokens to spend
const client = await PingWalletClient.offline(signer)
// const client = await SigningStargateClient.offline(signer)
return client.signAmino2(device.startsWith('ledger') ? toSignAddress(signerAddress) : signerAddress, messages, fee, memo, signerData)
return client.signAmino2(device.startsWith('ledger') ? toSignAddress(signerAddress) : signerAddress, messages, fee, memo, signerData, signOpts)
// return signDirect(signer, signerAddress, messages, fee, memo, signerData)
}

Expand Down Expand Up @@ -326,6 +326,21 @@ export function isToken(value) {
return is
}

export function findNativeConfigToken(balances, chain) {
if (!Array.isArray(balances)) return undefined
if (!Array.isArray(chain?.assets)) return balances

const configAssetDenoms = chain.assets.map(i => i.base)

// prioritise config tokens in token selection dropdown if available.
const token = balances.find(i => !i.denom.startsWith('ibc') && configAssetDenoms.includes(i.denom))

if (token) return token

// if no token in config, return any native (non-ibc) token
return balances.find(i => !i.denom.startsWith('ibc'))
}

export function formatTokenDenom(tokenDenom) {
if (tokenDenom && tokenDenom.code === undefined) {
let denom = tokenDenom.denom_trace ? tokenDenom.denom_trace.base_denom : tokenDenom
Expand Down
6 changes: 4 additions & 2 deletions src/views/OperationDelegateComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ import {
required, email, url, between, alpha, integer, password, min, digits, alphaDash, length,
} from '@validations'
import {
abbrAddress, extractAccountNumberAndSequence, formatToken, formatTokenDenom, getLocalAccounts, getUnitAmount, setLocalTxHistory, sign, timeIn,
abbrAddress, extractAccountNumberAndSequence, findNativeConfigToken, formatToken, formatTokenDenom, getLocalAccounts, getUnitAmount, setLocalTxHistory, sign, timeIn,
} from '@/libs/utils'
import vSelect from 'vue-select'
import ToastificationContent from '@core/components/toastification/ToastificationContent.vue'
Expand Down Expand Up @@ -349,7 +349,7 @@ export default {
this.$http.getBankBalances(this.selectedAddress).then(res => {
if (res && res.length > 0) {
this.balance = res.reverse()
const token = this.balance.find(i => !i.denom.startsWith('ibc'))
const token = findNativeConfigToken(this.balance, this.$store.state.chains.selected)
this.token = token.denom
if (token) this.feeDenom = token.denom
this.balance.filter(i => i.denom.startsWith('ibc')).forEach(x => {
Expand Down Expand Up @@ -469,6 +469,7 @@ export default {
chainId: this.chainId,
}

const { sign_opts: signOpts } = this.$store.state.chains.selected
sign(
this.wallet,
this.chainId,
Expand All @@ -477,6 +478,7 @@ export default {
txFee,
this.memo,
signerData,
signOpts,
).then(bodyBytes => {
this.$http.broadcastTx(bodyBytes).then(res => {
setLocalTxHistory({
Expand Down
5 changes: 4 additions & 1 deletion src/views/OperationGovDepositComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ import {
import {
abbrAddress,
extractAccountNumberAndSequence,
findNativeConfigToken,
formatToken, formatTokenDenom, getLocalAccounts, getUnitAmount, setLocalTxHistory, sign, timeIn,
} from '@/libs/utils'
import ToastificationContent from '@core/components/toastification/ToastificationContent.vue'
Expand Down Expand Up @@ -317,7 +318,7 @@ export default {
this.$http.getBankBalances(this.voter).then(res => {
if (res && res.length > 0) {
this.balance = res.reverse().filter(x => !x.denom.startsWith('ibc'))
const token = this.balance.find(i => !i.denom.startsWith('ibc'))
const token = findNativeConfigToken(this.balance, this.$store.state.chains.selected)
if (token) {
this.feeDenom = token.denom
this.token = token.denom
Expand Down Expand Up @@ -408,6 +409,7 @@ export default {
chainId: this.chainId,
}

const { sign_opts: signOpts } = this.$store.state.chains.selected
sign(
this.wallet,
this.chainId,
Expand All @@ -416,6 +418,7 @@ export default {
txFee,
this.memo,
signerData,
signOpts,
).then(bodyBytes => {
this.$http.broadcastTx(bodyBytes, this.selectedChain).then(res => {
setLocalTxHistory({
Expand Down
2 changes: 2 additions & 0 deletions src/views/OperationRedelegateComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export default {
chainId: this.chainId,
}

const { sign_opts: signOpts } = this.$store.state.chains.selected
sign(
this.wallet,
this.chainId,
Expand All @@ -417,6 +418,7 @@ export default {
txFee,
this.memo,
signerData,
signOpts,
).then(bodyBytes => {
this.$http.broadcastTx(bodyBytes).then(res => {
setLocalTxHistory({
Expand Down
2 changes: 2 additions & 0 deletions src/views/OperationTransfer2Component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ export default {
chainId: this.chainId,
}

const { sign_opts: signOpts } = this.$store.state.chains.selected
sign(
this.wallet,
this.chainId,
Expand All @@ -511,6 +512,7 @@ export default {
txFee,
this.memo,
signerData,
signOpts,
).then(bodyBytes => {
this.$http.broadcastTx(bodyBytes, this.selectedChain).then(res => {
setLocalTxHistory({
Expand Down
6 changes: 5 additions & 1 deletion src/views/OperationTransferComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ import {
} from '@validations'
import {
extractAccountNumberAndSequence,
findNativeConfigToken,
formatToken, formatTokenDenom, getLocalAccounts, getLocalChains, getUnitAmount, setLocalTxHistory, sign, timeIn,
} from '@/libs/utils'
import ToastificationContent from '@core/components/toastification/ToastificationContent.vue'
Expand Down Expand Up @@ -324,7 +325,8 @@ export default {
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
const token = findNativeConfigToken(this.balance, this.$store.state.chains.selected)
this.feeDenom = token?.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 => {
Expand Down Expand Up @@ -404,6 +406,7 @@ export default {
chainId: this.chainId,
}

const { sign_opts: signOpts } = this.$store.state.chains.selected
sign(
this.wallet,
this.chainId,
Expand All @@ -412,6 +415,7 @@ export default {
txFee,
this.memo,
signerData,
signOpts,
).then(bodyBytes => {
this.$http.broadcastTx(bodyBytes, this.selectedChain).then(res => {
setLocalTxHistory({
Expand Down
2 changes: 2 additions & 0 deletions src/views/OperationUnbondComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ export default {
chainId: this.chainId,
}

const { sign_opts: signOpts } = this.$store.state.chains.selected
sign(
this.wallet,
this.chainId,
Expand All @@ -388,6 +389,7 @@ export default {
txFee,
this.memo,
signerData,
signOpts,
).then(bodyBytes => {
this.$http.broadcastTx(bodyBytes, this.selectedChain).then(res => {
setLocalTxHistory({
Expand Down
Loading