-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from denbite/add_transaction_data
Brings service functions to sign Ethereum contract transactions
- Loading branch information
Showing
8 changed files
with
223 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
import PropTypes from 'prop-types'; | ||
import { forwardRef } from 'react'; | ||
import { useImperativeHandle } from 'react'; | ||
|
||
const abi = [ | ||
{ | ||
inputs: [ | ||
{ | ||
internalType: 'uint256', | ||
name: '_num', | ||
type: 'uint256', | ||
}, | ||
], | ||
name: 'set', | ||
outputs: [], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
{ | ||
inputs: [], | ||
name: 'get', | ||
outputs: [ | ||
{ | ||
internalType: 'uint256', | ||
name: '', | ||
type: 'uint256', | ||
}, | ||
], | ||
stateMutability: 'view', | ||
type: 'function', | ||
}, | ||
{ | ||
inputs: [], | ||
name: 'num', | ||
outputs: [ | ||
{ | ||
internalType: 'uint256', | ||
name: '', | ||
type: 'uint256', | ||
}, | ||
], | ||
stateMutability: 'view', | ||
type: 'function', | ||
}, | ||
]; | ||
|
||
const contract = '0xe2a01146FFfC8432497ae49A7a6cBa5B9Abd71A3'; | ||
|
||
export const FunctionCallForm = forwardRef(({ props: { Eth, senderAddress, loading } }, ref) => { | ||
const [number, setNumber] = useState(1000); | ||
const [currentNumber, setCurrentNumber] = useState(''); | ||
|
||
async function getNumber() { | ||
const result = await Eth.getContractViewFunction(contract, abi, 'get'); | ||
setCurrentNumber(String(result)); | ||
} | ||
|
||
useEffect(() => { getNumber() }, []); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
async createPayload() { | ||
const data = Eth.createTransactionData(contract, abi, 'set', [number]); | ||
const { transaction, payload } = await Eth.createPayload(senderAddress, contract, 0, data); | ||
return { transaction, payload }; | ||
}, | ||
|
||
async afterRelay() { | ||
getNumber(); | ||
} | ||
})); | ||
|
||
return ( | ||
<> | ||
<div className="row mb-3"> | ||
<label className="col-sm-2 col-form-label col-form-label-sm">Counter:</label> | ||
<div className="col-sm-10"> | ||
<input | ||
type="text" | ||
className="form-control form-control-sm" | ||
value={contract} | ||
disabled | ||
/> | ||
<div className="form-text">Contract address</div> | ||
</div> | ||
</div> | ||
<div className="row mb-3"> | ||
<label className="col-sm-2 col-form-label col-form-label-sm"> | ||
Number: | ||
</label> | ||
<div className="col-sm-10"> | ||
<input | ||
type="number" | ||
className="form-control form-control-sm" | ||
value={number} | ||
onChange={(e) => setNumber(e.target.value)} | ||
step="1" | ||
disabled={loading} | ||
/> | ||
<div className="form-text"> The number to save, current value: <b> {currentNumber} </b> </div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
}); | ||
|
||
FunctionCallForm.propTypes = { | ||
props: PropTypes.shape({ | ||
senderAddress: PropTypes.string.isRequired, | ||
loading: PropTypes.bool.isRequired, | ||
Eth: PropTypes.shape({ | ||
createPayload: PropTypes.func.isRequired, | ||
createTransactionData: PropTypes.func.isRequired, | ||
getContractViewFunction: PropTypes.func.isRequired, | ||
}).isRequired, | ||
}).isRequired | ||
}; | ||
|
||
FunctionCallForm.displayName = 'EthereumContractView'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useState } from "react"; | ||
|
||
import PropTypes from 'prop-types'; | ||
import { forwardRef } from "react"; | ||
import { useImperativeHandle } from "react"; | ||
|
||
export const TransferForm = forwardRef(({ props: { Eth, senderAddress, loading } }, ref) => { | ||
const [receiver, setReceiver] = useState("0x427F9620Be0fe8Db2d840E2b6145D1CF2975bcaD"); | ||
const [amount, setAmount] = useState(0.005); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
async createPayload() { | ||
const { transaction, payload } = await Eth.createPayload(senderAddress, receiver, amount, undefined); | ||
return { transaction, payload }; | ||
}, | ||
async afterRelay() { } | ||
})); | ||
|
||
return ( | ||
<> | ||
<div className="row mb-3"> | ||
<label className="col-sm-2 col-form-label col-form-label-sm">To:</label> | ||
<div className="col-sm-10"> | ||
<input type="text" className="form-control form-control-sm" value={receiver} onChange={(e) => setReceiver(e.target.value)} disabled={loading} /> | ||
</div> | ||
</div> | ||
<div className="row mb-3"> | ||
<label className="col-sm-2 col-form-label col-form-label-sm">Amount:</label> | ||
<div className="col-sm-10"> | ||
<input type="number" className="form-control form-control-sm" value={amount} onChange={(e) => setAmount(e.target.value)} step="0.01" disabled={loading} /> | ||
<div className="form-text"> Ethereum units </div> | ||
</div> | ||
</div> | ||
</> | ||
) | ||
}); | ||
|
||
TransferForm.propTypes = { | ||
props: PropTypes.shape({ | ||
senderAddress: PropTypes.string.isRequired, | ||
loading: PropTypes.bool.isRequired, | ||
Eth: PropTypes.shape({ | ||
createPayload: PropTypes.func.isRequired | ||
}).isRequired | ||
}).isRequired | ||
}; | ||
|
||
TransferForm.displayName = 'TransferForm'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters