@@ -91,13 +91,13 @@ Erc20TokensController.prototype.getBalance = function (req, res, next) {
9191} ;
9292
9393/**
94- * Transfer tokens from owner address to any token
94+ * Transfer tokens from owner address to any address
9595 * Requires: owner address is unlocked in the node, the owner address has tokens for the requested token name
9696 * @param {Request } req
9797 * @param {Response } res
9898 * @param {Next } next
9999 */
100- Erc20TokensController . prototype . transfer = function ( req , res , next ) {
100+ Erc20TokensController . prototype . requestTransfer = function ( req , res , next ) {
101101 // console.log('test params:', req.params)
102102 // console.log('test body:', req.body)
103103 let obj = new Object ( ) ;
@@ -153,4 +153,78 @@ Erc20TokensController.prototype.transfer = function (req, res, next) {
153153 }
154154} ;
155155
156+ /**
157+ * Transfer tokens from any address to another address
158+ * Requires: owner address is unlocked in the node, the owner address has tokens for the requested token name
159+ * @param {Request } req
160+ * @param {Response } res
161+ * @param {Next } next
162+ */
163+ Erc20TokensController . prototype . transfer = function ( req , res , next ) {
164+ // console.log('test params:', req.params)
165+ // console.log('test body:', req.body)
166+ let obj = new Object ( ) ;
167+ let tokenRequested = req . params . tokenName ;
168+ // let addressRequested = req.params.address;
169+ let toAddress = req . body . toAddress ;
170+ let tokenValue = req . body . value ;
171+ let address ;
172+ let tokenHelpers ;
173+ let contractAddress ;
174+ const ownerAddress = connections . ownerAddress ;
175+
176+ obj . method = 'transfer' ;
177+ obj . params = req . params
178+ obj . body = req . body
179+
180+ /**
181+ * Check address parameter is a valid ethereum address
182+ * Check token name and check it against a json file to make sure its available and has a contract to connect to
183+ * Instanciate the token helpers class and call contract method
184+ */
185+ try {
186+ address = toAddress != null && web3 . utils . isAddress ( req . body . toAddress ) ? toAddress : false ;
187+ if ( address === false ) {
188+ obj . error = `Address ${ toAddress } is not valid ethereum address` ;
189+ throw obj ;
190+ }
191+ // check token
192+ obj . erc20Available = erc20Tokens . filter ( ( token ) => {
193+ return token . name === tokenRequested ;
194+ } )
195+ if ( obj . erc20Available . length > 0 ) {
196+ contractAddress = obj . erc20Available [ 0 ] . contractAddress ;
197+ tokenHelpers = new TokenHelpers ( contractAddress , ownerAddress ) ;
198+ // call contract method
199+ obj . transferABI = tokenHelpers . transferABI ( toAddress , tokenValue )
200+ // tokenHelpers.transferABI(toAddress, tokenValue).then((abi) => {
201+ // obj.transferABI = abi
202+ // })
203+ console . log ( 'obj.transferABI' , obj . transferABI )
204+ // let txObject = {
205+ // from: "",
206+
207+ // }
208+ // web3.eth.signTransaction(txObject);
209+ res . send ( obj ) ;
210+ // return tokenHelpers.transfer(toAddress, tokenValue)
211+ // .then((balance) => {
212+ // console.log('erc transfer controller transferred: ', balance)
213+ // obj.txHash = balance
214+ // return res.send(obj)
215+ // })
216+ // .catch((err) => {
217+ // console.log('erc transfer controller err: ', err)
218+ // obj.error = err;
219+ // throw obj;
220+ // })
221+ } else {
222+ obj . error = `Token ${ tokenRequested } is not available on this api`
223+ throw obj ;
224+ }
225+ } catch ( error ) {
226+ res . status ( 404 ) . send ( error ) . end ( ) ;
227+ }
228+ } ;
229+
156230module . exports = Erc20TokensController ;
0 commit comments