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

Send invoke implementation #21

Merged
merged 1 commit into from
Dec 21, 2017
Merged
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
4 changes: 3 additions & 1 deletion src/app/containers/Send/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ export default class Send extends Component {
errorMsg: ''
})

if (!this.address || !this.amount) {
if (!this.address || !this.address.value || !this.amount || !this.amount.value) {
this.setState({
loading: false,
errorMsg: 'All fields are required'
})

return
}

var amounts = {}
Expand Down
3 changes: 3 additions & 0 deletions src/app/containers/SendInvoke/SendInvoke.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.transactionId {
word-wrap: break-word;
}
73 changes: 61 additions & 12 deletions src/app/containers/SendInvoke/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { connect } from 'react-redux'

import Neon, { api } from '@cityofzion/neon-js'

import style from './SendInvoke.css'

@connect(
state => ({
network: state.network,
Expand Down Expand Up @@ -31,39 +33,86 @@ export default class SendInvoke extends Component {
resetState = () => {
this.setState({
errorMsg: '',
loading: false
loading: false,
txid: ''
})
}

String2Hex (tmp) {
var str = ''
for (var i = 0; i < tmp.length; i++) {
str += tmp[i].charCodeAt(0).toString(16)
}
return str
}

handleSubmit = (event) => {
event.preventDefault()
const { network, account } = this.props
this.setState({
loading: true,
errorMsg: ''
errorMsg: '',
txid: ''
})

if (!this.scriptHash || !this.scriptHash.value || !this.operation || !this.operation.value || !this.amount || !this.amount.value) {
this.setState({
loading: false,
errorMsg: 'Error! Script hash, operation and amount are all required!'
})

return
}

const txArgs = [this.arg1.value, this.arg2.value]
const args = []
txArgs.forEach((arg) => {
if (arg !== '') args.push({'type': 7, 'value': arg})
if (arg !== '') args.push(this.String2Hex(arg))
})

// todo!
this.setState({
loading: false,
errorMsg: 'Work in progress'
})
const myAccount = Neon.create.account(account.wif)

const config = {
net: network.name,
privateKey: myAccount.privateKey,
address: myAccount.address,
intents: [{ assetId: Neon.CONST.ASSET_ID[this.type.value], value: parseFloat(this.amount.value), scriptHash: this.scriptHash.value }],
script: { scriptHash: this.scriptHash.value, operation: this.operation.value, args: args },
gas: 0
}

return api.doInvoke(config)
.then((c) => {
if (c.response.result === true) {
this.setState({
loading: false,
txid: c.response.txid
})
} else {
this.setState({
loading: false,
errorMsg: 'Invoke failed'
})
}
})
.catch((e) => {
console.log('e', e)
this.setState({
loading: false,
errorMsg: 'Invoke failed'
})
})
}

render () {
const { result, loading, errorMsg } = this.state
const { txid, loading, errorMsg } = this.state

return (
<div>
<p>Invoke Contract</p>
<form onSubmit={this.handleSubmit}>
<input
autoFocus
type='text'
placeholder='Operation'
ref={(input) => { this.operation = input }}
Expand Down Expand Up @@ -97,12 +146,12 @@ export default class SendInvoke extends Component {
<option value='NEO'>Neo</option>
<option value='GAS' selected>Gas</option>
</select>
<button>Invoke</button>
<button disabled={loading}>Invoke</button>
</form>

{ result &&
{ txid &&
<div>
result: { JSON.stringify(result) }
Success! txid: <span className={style.transactionId}>{ txid }</span>
</div>
}
{ loading &&
Expand Down
6 changes: 4 additions & 2 deletions src/app/containers/TestInvoke/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ export default class TestInvoke extends Component {
result: ''
})

if (!this.scriptHash || this.operation) {
if (!this.scriptHash || !this.scriptHash.value || !this.operation || !this.operation.value) {
this.setState({
loading: false,
errorMsg: 'Error! Script hash and operation at both required!'
errorMsg: 'Error! Script hash and operation are both required!'
})

return
}

const txArgs = []
Expand Down