Skip to content

Commit

Permalink
feature: additional nep17 support (#2146)
Browse files Browse the repository at this point in the history
* Remove fee buttons if chain is n3

* Fix test

* Adds support for any nep11

* lint

* Fixes pending tx actions
  • Loading branch information
comountainclimber committed Jul 6, 2021
1 parent 61cd1ee commit c26201d
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 69 deletions.
21 changes: 19 additions & 2 deletions app/actions/balancesActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,26 @@ async function getN3Balances({ net, address }: Props) {
console.error({ e })
})
const symbol = atob(tokenNameResponse.stack[0].value)
const parsedAmount = convertToArbitraryDecimals(amount)
balances[symbol] = parsedAmount
const decimalResponse = await new n3Rpc.RPCClient(NODE_URL)
.invokeFunction(assethash, 'decimals')
.catch(e => {
console.error({ e })
})
const decimals = decimalResponse.stack[0].value
const parsedAmount = convertToArbitraryDecimals(amount, decimals)
if (symbol === 'NEO' || symbol === 'GAS') {
balances[symbol] = Number(parsedAmount)
} else {
balances[assethash] = {
symbol,
cryptocompareSymbol: undefined,
scriptHash: assethash,
decimals,
balance: parsedAmount,
}
}
}

return balances
} catch (e) {
return balances
Expand Down
17 changes: 14 additions & 3 deletions app/actions/pendingTransactionActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const INVALID_TX_ERROR_MESSAGE = 'Unknown transaction'
export const parseContractTransaction = async (
transaction: PendingTransaction,
net: string,
chain: string,
): Promise<Array<ParsedPendingTransaction>> => {
const parsedData = []
// eslint-disable-next-line camelcase
Expand All @@ -39,7 +40,14 @@ export const parseContractTransaction = async (
blocktime,
amount: toBigNumber(send.amount).toString(),
to: send.address,
asset: await findAndReturnTokenInfo('', net, send.symbol),
asset:
chain === 'neo3'
? await findAndReturnTokenInfo(
send.contractHash || '',
net,
send.symbol,
)
: await findAndReturnTokenInfo('', net, send.symbol),
})
}
return parsedData
Expand Down Expand Up @@ -77,6 +85,7 @@ export const parseInvocationTransaction = (
export const parseTransactionInfo = async (
pendingTransactionsInfo: Array<PendingTransaction>,
net: string,
chain: string,
) => {
const parsedData: Array<ParsedPendingTransaction> = []

Expand All @@ -85,7 +94,9 @@ export const parseTransactionInfo = async (
if (transaction.type === 'InvocationTransaction') {
parsedData.push(...parseInvocationTransaction(transaction))
} else {
parsedData.push(...(await parseContractTransaction(transaction, net)))
parsedData.push(
...(await parseContractTransaction(transaction, net, chain)),
)
}
}
}
Expand Down Expand Up @@ -168,7 +179,7 @@ export const fetchTransactionInfo = async (
}
}

return parseTransactionInfo(pendingTransactionInfo, net)
return parseTransactionInfo(pendingTransactionInfo, net, chain)
}
return []
}
Expand Down
1 change: 1 addition & 0 deletions app/components/Dashboard/TokenBalancesPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const sortedByImage = a => {

const mapBalanceDataToProps = balances => {
const mutatedBalances = cloneDeep(balances)

// eslint-disable-next-line
Object.keys(mutatedBalances).map(key => {
if (key === 'NEO' || key === 'GAS') {
Expand Down
36 changes: 19 additions & 17 deletions app/modules/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ export const sendTransaction = ({
sendEntries,
fees = 0,
isWatchOnly,
chain,
}: {
sendEntries: Array<SendEntryType>,
fees: number,
Expand All @@ -200,7 +199,6 @@ export const sendTransaction = ({
const publicKey = getPublicKey(state)
const isHardwareSend = getIsHardwareLogin(state)
const { tokens, chain } = state.spunky.settings.data

return chain === 'neo3'
? new Promise(async (resolve, reject) => {
try {
Expand Down Expand Up @@ -228,21 +226,25 @@ export const sendTransaction = ({
signingCallback: n3Api.signWithAccount(CONFIG.account),
}

const nep17Intents = sendEntries.map(
({ address, amount, symbol }) => {
const { scriptHash } = tokens.find(
// eslint-disable-next-line eqeqeq
t => t.networkId == 2 && t.symbol === symbol,
)
const intent = {
from: CONFIG.account,
to: address,
decimalAmt: amount,
contractHash: scriptHash,
}
return intent
},
)
const nep17Intents = sendEntries.map(entry => {
const { address, amount, symbol } = entry
const token = tokens.find(
// eslint-disable-next-line eqeqeq
t => t.networkId == 2 && t.symbol === symbol,
)
const contractHash = token
? token.scriptHash
: tokensBalanceMap[symbol] && tokensBalanceMap[symbol].scriptHash

entry.contractHash = contractHash || ''
const intent = {
from: CONFIG.account,
to: address,
decimalAmt: amount,
contractHash,
}
return intent
})

const results = await facade.transferToken(
nep17Intents,
Expand Down
62 changes: 36 additions & 26 deletions app/util/findAndReturnTokenInfo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import { api } from '@cityofzion/neon-js'
import N3Neon from '@cityofzion/neon-js-next'
import { api, rpc } from '@cityofzion/neon-js'
import N3Neon, { rpc as n3Rpc } from '@cityofzion/neon-js-next'

import { imageMap } from '../assets/nep5/png'
import { getDefaultTokens } from '../core/nep5'
Expand Down Expand Up @@ -38,33 +38,43 @@ export const findAndReturnTokenInfo = async (
)
return GAS

// TODO: support other nep11 contracts here
} else {
const NEO = {
symbol: ASSETS.NEO,
image: getImageBySymbol(ASSETS.NEO),
}
const GAS = {
symbol: ASSETS.GAS,
image: getImageBySymbol(ASSETS.GAS),
}
const NODE_URL = 'https://testnet2.neo.coz.io:443'

if (symbol === ASSETS.NEO || scriptHash.includes(NEO_ID)) return NEO
if (symbol === ASSETS.GAS || scriptHash.includes(GAS_ID)) return GAS
const tokenNameResponse = await new n3Rpc.RPCClient(NODE_URL)
.invokeFunction(scriptHash, 'symbol')
.catch(e => {
console.error({ e })
})
const foundSymbol = atob(tokenNameResponse.stack[0].value)

const tokens = await getDefaultTokens()
// if token is found in our list return it
const token = tokens.find(token => token.scriptHash.includes(scriptHash))
if (token) return token

// if token is unknown to application query neoscan
const endpoint = await getRPCEndpoint(net)
const tokenInfo = await api.nep5.getToken(endpoint, scriptHash).catch(e => {
console.error(e)
return {}
})
return {
symbol: tokenInfo.symbol,
symbol: foundSymbol,
}
}
const NEO = {
symbol: ASSETS.NEO,
image: getImageBySymbol(ASSETS.NEO),
}
const GAS = {
symbol: ASSETS.GAS,
image: getImageBySymbol(ASSETS.GAS),
}

if (symbol === ASSETS.NEO || scriptHash.includes(NEO_ID)) return NEO
if (symbol === ASSETS.GAS || scriptHash.includes(GAS_ID)) return GAS

const tokens = await getDefaultTokens()
// if token is found in our list return it
const token = tokens.find(token => token.scriptHash.includes(scriptHash))
if (token) return token

// if token is unknown to application query neoscan
const endpoint = await getRPCEndpoint(net)
const tokenInfo = await api.nep5.getToken(endpoint, scriptHash).catch(e => {
console.error(e)
return {}
})
return {
symbol: tokenInfo.symbol,
}
}
1 change: 1 addition & 0 deletions flow-typed/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ declare type SendEntryType = {
amount: string,
address: string,
symbol: SymbolType,
contractHash?: string,
}

declare type ThemeType = THEME.LIGHT | THEME.DARK
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
},
"dependencies": {
"@cityofzion/neon-js": "3.11.9",
"@cityofzion/neon-js-next": "npm:@cityofzion/neon-js@5.0.0-next.11",
"@cityofzion/neon-js-next": "npm:@cityofzion/neon-js@5.0.0-next.12",
"@formatjs/intl-pluralrules": "^1.5.2",
"@ledgerhq/hw-transport-node-hid": "5.0.0",
"axios": "0.21.1",
Expand Down
34 changes: 14 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,19 @@
lodash "^4.17.19"
to-fast-properties "^2.0.0"

"@cityofzion/neon-api@^5.0.0-next.11":
version "5.0.0-next.11"
resolved "https://registry.yarnpkg.com/@cityofzion/neon-api/-/neon-api-5.0.0-next.11.tgz#090b364094e356d2f14f65cf7fb28f342a15caa0"
integrity sha512-TEq1OB+6h2sdBZlK9jS2CH8KmkHVydwlgPksOldH8afRDZRCU96HF3nafhXWE/E29qIRZfddB2dFY0E7Q9Tb/A==
"@cityofzion/neon-api@^5.0.0-next.12":
version "5.0.0-next.12"
resolved "https://registry.yarnpkg.com/@cityofzion/neon-api/-/neon-api-5.0.0-next.12.tgz#a664554bcb72bdcb0abc6ff60f7894ca5a77ba10"
integrity sha512-AV4TVil2aA2+ao+f9rWrvFr5E/U3tqVD73Px2HptaFMCL/fsxwE04oyJRatrA663xqd5CC6XCT7xzJC433m0Iw==
dependencies:
axios "0.21.1"
isomorphic-ws "4.0.1"
ws "7.4.4"

"@cityofzion/neon-core@^5.0.0-next.11":
version "5.0.0-next.11"
resolved "https://registry.yarnpkg.com/@cityofzion/neon-core/-/neon-core-5.0.0-next.11.tgz#6282d7b85db0649055a6d1b7e0bf8c193c9f500e"
integrity sha512-ZYa1xtDMLLG7XuhjYZwCCB7o70WwZLyJrom5Gi+65cmigpwCvGm3wNxaBLmCitT8XknxoSnTrDsgQ5dmniy45g==
"@cityofzion/neon-core@^5.0.0-next.12":
version "5.0.0-next.12"
resolved "https://registry.yarnpkg.com/@cityofzion/neon-core/-/neon-core-5.0.0-next.12.tgz#17da929501285553f6be5192ececdbf59ed8ebaa"
integrity sha512-PNcnkYDwLrcbZAf/3BPQlOMm4VlygsnCMzJu5lPZPjoMqziyU9PO0xw+UKI1n7a/K1GVa8LE1ZTkTIA3PswYqg==
dependencies:
abort-controller "3.0.0"
bignumber.js "7.2.1"
Expand All @@ -190,15 +190,14 @@
loglevel "1.7.1"
loglevel-plugin-prefix "0.8.4"
scrypt-js "3.0.1"
secure-random "1.1.2"

"@cityofzion/neon-js-next@npm:@cityofzion/neon-js@5.0.0-next.11":
version "5.0.0-next.11"
resolved "https://registry.yarnpkg.com/@cityofzion/neon-js/-/neon-js-5.0.0-next.11.tgz#a9b89bef6abf242fecc0a7730ef0e9d1fc2da441"
integrity sha512-O8OW5jbbjStsTJb3L2dkjqW2SEk4d1bgcADSL9PrMETbHRi6dioyZYp49iRfWMZCcBtK8N/JWW2SkTYHgmh3JQ==
"@cityofzion/neon-js-next@npm:@cityofzion/neon-js@5.0.0-next.12":
version "5.0.0-next.12"
resolved "https://registry.yarnpkg.com/@cityofzion/neon-js/-/neon-js-5.0.0-next.12.tgz#f69b90fe60b5ef97cacc56c3dcf22290be8d076a"
integrity sha512-Uc9FyXPNJa0BHqjmw2zwdtR6LSOVKd96kp11juIR/VVAulTDY9rjzNK0iCVNDfKj+XVCJm4nl5jY2W09mKY4wg==
dependencies:
"@cityofzion/neon-api" "^5.0.0-next.11"
"@cityofzion/neon-core" "^5.0.0-next.11"
"@cityofzion/neon-api" "^5.0.0-next.12"
"@cityofzion/neon-core" "^5.0.0-next.12"

"@cityofzion/neon-js@3.11.9":
version "3.11.9"
Expand Down Expand Up @@ -12529,11 +12528,6 @@ secure-random@1.1.1:
resolved "https://registry.yarnpkg.com/secure-random/-/secure-random-1.1.1.tgz#0880f2d8c5185f4bcb4684058c836b4ddb07145a"
integrity sha1-CIDy2MUYX0vLRoQFjINrTdsHFFo=

secure-random@1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/secure-random/-/secure-random-1.1.2.tgz#ed103b460a851632d420d46448b2a900a41e7f7c"
integrity sha512-H2bdSKERKdBV1SwoqYm6C0y+9EA94v6SUBOWO8kDndc4NoUih7Dv6Tsgma7zO1lv27wIvjlD0ZpMQk7um5dheQ==

select-hose@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/select-hose/-/select-hose-2.0.0.tgz#625d8658f865af43ec962bfc376a37359a4994ca"
Expand Down

0 comments on commit c26201d

Please sign in to comment.