From ec4ae4ec4beab2ab4d747fd4e2ac67b72fd931c7 Mon Sep 17 00:00:00 2001 From: elliot Date: Thu, 29 Oct 2020 16:11:57 +0900 Subject: [PATCH] add decimal option at token transfer --- src/components/SmartContractExecution.js | 102 ++++++++++------- src/components/SmartContractExecutionFD.js | 82 +++++++++----- .../SmartContractExecutionFDRatio.js | 103 ++++++++++++------ .../SmartContractExecutionLegacy.js | 100 ++++++++++------- 4 files changed, 248 insertions(+), 139 deletions(-) diff --git a/src/components/SmartContractExecution.js b/src/components/SmartContractExecution.js index 3ad1cd9..691b303 100644 --- a/src/components/SmartContractExecution.js +++ b/src/components/SmartContractExecution.js @@ -16,6 +16,7 @@ class SmartContractExecution extends Component { txHash: null, receipt: null, error: null, + decimal: 18 } } @@ -26,49 +27,72 @@ class SmartContractExecution extends Component { return null } - handleChange = (e) => { + handleChange = e => { this.setState({ - [e.target.name]: e.target.value, + [e.target.name]: e.target.value }) } signTransaction = () => { - const { from, contractAddress, to, amount, gas } = this.state - const data = caver.klay.abi.encodeFunctionCall({ - name: 'transfer', - type: 'function', - inputs: [{ - type: 'address', - name: 'recipient', - }, { - type: 'uint256', - name: 'amount', - }], - }, [to, caver.utils.toPeb(amount, 'KLAY')]) + const { from, contractAddress, to, amount, gas, decimal } = this.state + const data = caver.klay.abi.encodeFunctionCall( + { + name: 'transfer', + type: 'function', + inputs: [ + { + type: 'address', + name: 'recipient' + }, + { + type: 'uint256', + name: 'amount' + } + ] + }, + [ + to, + caver.utils + .toBN(amount) + .mul(caver.utils.toBN(Number(`1e${decimal}`))) + .toString() + ] + ) - caver.klay.sendTransaction({ - type: 'SMART_CONTRACT_EXECUTION', - from, - to: contractAddress, - data, - gas, - }) - .on('transactionHash', (transactionHash) => { + caver.klay + .sendTransaction({ + type: 'SMART_CONTRACT_EXECUTION', + from, + to: contractAddress, + data, + gas + }) + .on('transactionHash', transactionHash => { console.log('txHash', transactionHash) this.setState({ txHash: transactionHash }) }) - .on('receipt', (receipt) => { + .on('receipt', receipt => { console.log('receipt', receipt) this.setState({ receipt: JSON.stringify(receipt) }) }) - .on('error', (error) => { + .on('error', error => { console.log('error', error) this.setState({ error: error.message }) }) } render() { - const { from, to, amount, contractAddress, gas, txHash, receipt, error } = this.state + const { + from, + to, + amount, + contractAddress, + gas, + txHash, + receipt, + error, + decimal + } = this.state return (
@@ -77,45 +101,45 @@ class SmartContractExecution extends Component { label="From" value={from} onChange={this.handleChange} - placeholder="Account you logged in metamask" + placeholder="Kaikas account" /> + -
) } diff --git a/src/components/SmartContractExecutionFD.js b/src/components/SmartContractExecutionFD.js index 7bc42e0..c00e5eb 100644 --- a/src/components/SmartContractExecutionFD.js +++ b/src/components/SmartContractExecutionFD.js @@ -5,7 +5,6 @@ import Button from 'components/Button' import Message from 'components/Message' import FeeDelegation from 'components/FeeDelegation' - class SmartContractExecutionFD extends Component { constructor(props) { super(props) @@ -17,6 +16,7 @@ class SmartContractExecutionFD extends Component { gas: 3000000, senderAddress: '', senderRawTransaction: null, + decimal: 18 } } @@ -27,36 +27,50 @@ class SmartContractExecutionFD extends Component { return null } - handleChange = (e) => { + handleChange = e => { this.setState({ - [e.target.name]: e.target.value, + [e.target.name]: e.target.value }) } signTransaction = async () => { - const { from, to, amount, contractAddress, gas } = this.state + const { from, to, amount, contractAddress, gas, decimal } = this.state - const data = caver.klay.abi.encodeFunctionCall({ - name: 'transfer', - type: 'function', - inputs: [{ - type: 'address', - name: 'recipient', - }, { - type: 'uint256', - name: 'amount', - }], - }, [to, caver.utils.toPeb(amount, 'KLAY')]) + const data = caver.klay.abi.encodeFunctionCall( + { + name: 'transfer', + type: 'function', + inputs: [ + { + type: 'address', + name: 'recipient' + }, + { + type: 'uint256', + name: 'amount' + } + ] + }, + [ + to, + caver.utils + .toBN(amount) + .mul(caver.utils.toBN(Number(`1e${decimal}`))) + .toString() + ] + ) const txData = { type: 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION', from, to: contractAddress, gas, - data, + data } - const { rawTransaction: senderRawTransaction } = await caver.klay.signTransaction(txData) + const { + rawTransaction: senderRawTransaction + } = await caver.klay.signTransaction(txData) this.setState({ senderAddress: from, @@ -65,49 +79,61 @@ class SmartContractExecutionFD extends Component { } render() { - const { from, to, amount, contractAddress, gas, senderRawTransaction } = this.state + const { + from, + to, + amount, + contractAddress, + gas, + senderRawTransaction, + decimal + } = this.state return (
+ -
) }