From 1ea3b8b1490b919c20e66519a47796ee7bce0fe5 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Tue, 14 Aug 2018 16:03:15 -0400 Subject: [PATCH] add sendwithapproval method use send with approval --- app/components/ens/registerSubDomain.js | 190 +++++++++++---------- contracts/token/ApprovalReceiver.sol | 5 - contracts/token/ApproveAndCallFallBack.sol | 5 + contracts/token/TestToken.sol | 14 +- 4 files changed, 116 insertions(+), 98 deletions(-) delete mode 100644 contracts/token/ApprovalReceiver.sol create mode 100644 contracts/token/ApproveAndCallFallBack.sol diff --git a/app/components/ens/registerSubDomain.js b/app/components/ens/registerSubDomain.js index c43b39b..81173ef 100644 --- a/app/components/ens/registerSubDomain.js +++ b/app/components/ens/registerSubDomain.js @@ -12,10 +12,10 @@ import { getStatusContactCode, getSNTAllowance } from '../../reducers/accounts'; import FieldGroup from '../standard/FieldGroup'; import LinearProgress from '@material-ui/core/LinearProgress'; import { generateXY } from '../../utils/ecdsa'; - -console.log(TestToken) +import { BigNumber } from '../standard/utils'; const { soliditySha3, fromWei } = web3.utils; +const unlimitedAllowance = new BigNumber(2).pow(256).sub(1); const InnerForm = ({ values, @@ -33,98 +33,98 @@ const InnerForm = ({ }) => (
- {!subDomain && - } - {!domainName && - } + {!domainName && + { ENSSubdomainRegistry.methods.getPrice(hash(values.domainName)) .call() .then((res) => { setFieldValue('price', fromWei(res)); }); }} - > - Get Price - - } - />} - {!domainPrice && - } - - - setFieldValue('address', web3.eth.defaultAccount)}>Use My Primary Address} - /> - {!isSubmitting ? : } - - - - + Get Price + + } + />} + {!domainPrice && + } + + setFieldValue('statusAddress', statusContactCode)} - wide /> - - - + setFieldValue('address', web3.eth.defaultAccount)} - required - wide /> - -
- -
-
+ onBlur={handleBlur} + value={values.address} + error={errors.address} + button={} + /> + {!isSubmitting ? : } + + + + setFieldValue('statusAddress', statusContactCode)} + wide /> + + + setFieldValue('address', web3.eth.defaultAccount)} + required + wide /> + +
+ +
+
); @@ -142,22 +142,34 @@ const RegisterSubDomain = withFormik({ handleSubmit(values, { setSubmitting, props }) { const { address, statusAddress } = values; const { subDomain, domainName, registeredCallbackFn } = props || values; + const { SNTAllowance } = props; const { methods: { register } } = ENSSubdomainRegistry; const subdomainHash = soliditySha3(subDomain); const domainNameHash = hash(domainName); const resolveToAddr = address || zeroAddress; const points = statusAddress ? generateXY(statusAddress) : null; - const toSend = register( + const registerAbi = ENSSubdomainRegistry.options.jsonInterface.find(x => x.name === 'register'); + const encodedRegister = web3.eth.abi.encodeFunctionCall(registerAbi, [ + subdomainHash, + domainNameHash, + resolveToAddr, + points ? points.x : zeroBytes32, + points ? points.y : zeroBytes32 + ]); + + const sendWithApproval = TestToken.methods.approveAndCall(ENSSubdomainRegistry._address, unlimitedAllowance, encodedRegister); + const send = register( subdomainHash, domainNameHash, resolveToAddr, points ? points.x : zeroBytes32, points ? points.y : zeroBytes32 ); - toSend.estimateGas().then(gasEstimated => { - console.log("Register would work. :D Gas estimated: "+gasEstimated) - console.log("Trying: register(\""+subdomainHash+"\",\""+domainNameHash+"\",\""+resolveToAddr+"\",\""+zeroBytes32+"\",\""+zeroBytes32+"\")") + const toSend = Number(SNTAllowance) > 0 ? send : sendWithApproval; + toSend.estimateGas({ data: encodedRegister }).then(gasEstimated => { + console.log("Register would work. :D Gas estimated: " + gasEstimated); + console.log("Trying: register(\"" + subdomainHash + "\",\"" + domainNameHash+"\",\""+resolveToAddr+"\",\""+zeroBytes32+"\",\""+zeroBytes32+"\")") toSend.send({gas: gasEstimated+1000}).then(txId => { if(txId.status == "0x1" || txId.status == "0x01"){ console.log("Register send success. :)") diff --git a/contracts/token/ApprovalReceiver.sol b/contracts/token/ApprovalReceiver.sol deleted file mode 100644 index 866e1b0..0000000 --- a/contracts/token/ApprovalReceiver.sol +++ /dev/null @@ -1,5 +0,0 @@ -pragma solidity ^0.4.11; - -contract ApprovalReceiver { - function receiveApproval(address from, uint value, address tokenContract, bytes extraData) returns (bool); -} diff --git a/contracts/token/ApproveAndCallFallBack.sol b/contracts/token/ApproveAndCallFallBack.sol new file mode 100644 index 0000000..dd04956 --- /dev/null +++ b/contracts/token/ApproveAndCallFallBack.sol @@ -0,0 +1,5 @@ +pragma solidity ^0.4.11; + +contract ApproveAndCallFallBack { + function receiveApproval(address from, uint256 _amount, address _token, bytes _data) returns (bool); +} diff --git a/contracts/token/TestToken.sol b/contracts/token/TestToken.sol index 0845c45..29e4290 100644 --- a/contracts/token/TestToken.sol +++ b/contracts/token/TestToken.sol @@ -1,7 +1,7 @@ pragma solidity ^0.4.23; import "./StandardToken.sol"; -import './ApprovalReceiver.sol'; +import './ApproveAndCallFallBack.sol'; /** * @notice ERC20Token for test scripts, can be minted by anyone. @@ -18,9 +18,15 @@ contract TestToken is StandardToken { mint(msg.sender, _amount); } - function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) { - assert(approve(_spender, _value)); - return ApprovalReceiver(_spender).receiveApproval(msg.sender, _value, this, _extraData); + function approveAndCall(address _spender, uint256 _amount, bytes _extraData) returns (bool success) { + if (!approve(_spender, _amount)) throw; + ApproveAndCallFallBack(_spender).receiveApproval( + msg.sender, + _amount, + this, + _extraData + ); + return true; } }