Skip to content

Commit

Permalink
Feature/n3 tx refactor ii (#2144)
Browse files Browse the repository at this point in the history
* Remove fee buttons if chain is n3

* Fix test
  • Loading branch information
comountainclimber committed Jul 2, 2021
1 parent 35d54c1 commit 61cd1ee
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 66 deletions.
52 changes: 30 additions & 22 deletions app/components/Send/SendPanel/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Props = {
showImportModal: (props: Object) => any,
pushQRCodeData: (data: Object) => any,
calculateMaxValue: (asset: string, index: number) => string,
chain: string,
}

const shouldDisableSendButton = sendRowDetails =>
Expand Down Expand Up @@ -83,6 +84,7 @@ const SendPanel = ({
calculateMaxValue,
isWatchOnly,
showImportModal,
chain,
}: Props) => {
if (noSendableAssets) {
return <ZeroAssets address={address} />
Expand All @@ -102,14 +104,16 @@ const SendPanel = ({
calculateMaxValue={calculateMaxValue}
isWatchOnly={isWatchOnly}
/>
<div className={styles.priorityFeeContainer}>
<PriorityFee
availableGas={Number(get(sendableAssets, 'GAS.balance', 0))}
handleAddPriorityFee={handleAddPriorityFee}
fees={fees}
disabled={shouldDisableSendButton(sendRowDetails)}
/>
</div>
{chain === 'neo2' && (
<div className={styles.priorityFeeContainer}>
<PriorityFee
availableGas={Number(get(sendableAssets, 'GAS.balance', 0))}
handleAddPriorityFee={handleAddPriorityFee}
fees={fees}
disabled={shouldDisableSendButton(sendRowDetails)}
/>
</div>
)}
{isWatchOnly ? (
<Button
className={styles.generateTransactionButton}
Expand All @@ -133,21 +137,25 @@ const SendPanel = ({
>
{/* Send {pluralize('Asset', sendRowDetails.length)}{' '}
{fees ? 'With Fee' : 'Without Fee'} */}

{fees ? (
<FormattedMessage
id="sendWithFee"
values={{
itemCount: sendRowDetails.length,
}}
/>
{/* eslint-disable-next-line no-nested-ternary */}
{chain === 'neo2' ? (
fees ? (
<FormattedMessage
id="sendWithFee"
values={{
itemCount: sendRowDetails.length,
}}
/>
) : (
<FormattedMessage
id="sendWithoutFee"
values={{
itemCount: sendRowDetails.length,
}}
/>
)
) : (
<FormattedMessage
id="sendWithoutFee"
values={{
itemCount: sendRowDetails.length,
}}
/>
'Send'
)}
</Button>
)}
Expand Down
2 changes: 2 additions & 0 deletions app/containers/Send/Send.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ export default class Send extends React.Component<Props, State> {
showSendModal,
isWatchOnly,
showImportModal,
chain,
} = this.props
const noSendableAssets = Object.keys(sendableAssets).length === 0

Expand Down Expand Up @@ -574,6 +575,7 @@ export default class Send extends React.Component<Props, State> {
pushQRCodeData={this.pushQRCodeData}
isWatchOnly={isWatchOnly}
showImportModal={showImportModal}
chain={chain}
/>
</section>
)
Expand Down
72 changes: 28 additions & 44 deletions app/modules/transactions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
/* eslint-disable camelcase */
import { api, sc, u, wallet, settings } from '@cityofzion/neon-js'
import { wallet as n3Wallet, experimental } from '@cityofzion/neon-js-next'
import { api as n3Api, wallet as n3Wallet } from '@cityofzion/neon-js-next'
import { flatMap, keyBy, isEmpty, get } from 'lodash-es'

import {
Expand Down Expand Up @@ -206,8 +206,6 @@ export const sendTransaction = ({
try {
/*
TODO:
- Ability to send multiple assets in a single transaction
- Ability to attach custom fees
- Ledger support
- Support for test AND main net
- Node url should come from settings
Expand All @@ -218,24 +216,39 @@ export const sendTransaction = ({
const CONFIG = {
account: FROM_ACCOUNT,
rpcAddress: NODE_URL,
// TODO: this will have to by dynamic based on test/mainnets
networkMagic: 844378958,
}
// Hardcoded to filter out testnet tokens and hardcoded to only work with the first sendEntry

const CONTRACT_HASH = tokens.find(
// eslint-disable-next-line eqeqeq
t => t.networkId == 2 && t.symbol === sendEntries[0].symbol,
).scriptHash
const facade = await n3Api.NetworkFacade.fromConfig({
node: NODE_URL,
})

const signingConfig = {
signingCallback: n3Api.signWithAccount(CONFIG.account),
}

const Contract = new experimental.nep17.Nep17Contract(
CONTRACT_HASH,
CONFIG,
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 results = await Contract.transfer(
new n3Wallet.Account(fromAddress).address, // source address
new n3Wallet.Account(sendEntries[0].address).address, // destination address
sendEntries[0].amount, // amount

const results = await facade.transferToken(
nep17Intents,
signingConfig,
)

dispatch(
showSuccessNotification({
message:
Expand All @@ -257,35 +270,6 @@ export const sendTransaction = ({
)
}
return resolve({ txid: results })
/*
NOTE: potential alternate and more complex solution below
const builder = new n3Sc.ScriptBuilder()
builder.emitAppCall(CONTRACT_HASH, 'transfer', [
n3U.HexString.fromHex(
n3Wallet.getScriptHashFromAddress(sendEntries[0].address),
),
n3U.HexString.fromHex(
n3Wallet.getScriptHashFromAddress(CONFIG.account.address),
),
amountToTransfer,
n3Sc.ContractParam.any(null),
])
builder.emit(n3Sc.OpCode.ASSERT)
const transaction = new n3Tx.Transaction()
transaction.script = n3U.HexString.fromHex(builder.build())
await setBlockExpiry(transaction, CONFIG)
transaction.addSigner({
account: CONFIG.account.scriptHash,
scopes: 'CalledByEntry',
})
await addFees(transaction, CONFIG)
transaction.sign(CONFIG.account, CONFIG.networkMagic)
const results = await rpcClient.sendRawTransaction(transaction)
*/
} catch (e) {
console.error({ e })
return reject(e)
Expand Down

0 comments on commit 61cd1ee

Please sign in to comment.