Skip to content

Commit

Permalink
wip: causes issues #1 and #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ltfschoen committed Jun 28, 2020
1 parent 7ab1231 commit 23ed7bd
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
33 changes: 29 additions & 4 deletions helpers/contract.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
const fs = require('fs');
const Web3 = require('web3');
const HDWalletProvider = require('@truffle/hdwallet-provider');
const TruffleContract = require("@truffle/contract");
const BN = require('bn.js');
const debug = require('debug')('app');

const DApp = {
contractAddressMXC: null,
contractInstanceMXC: null,
contractJSONMXC: null,
coinbase: null,
init: async () => {
DApp.infuraHttpProvider = 'https://ropsten.infura.io/v3/' + process.env.INFURA_API_PROJECT_ID;
DApp.provider = new HDWalletProvider(process.env.MNENOMIC, DApp.infuraHttpProvider);
DApp.web3 = new Web3(DApp.provider);
DApp.contractAddressMXC = process.env.CONTRACT_ADDRESS_MXC_TESTNET;
DApp.contractJSONMXC = JSON.parse(fs.readFileSync('./assets/data/MXCToken.json').toString());
DApp.contractInstanceMXC = new DApp.web3.eth.Contract(DApp.contractJSONMXC, process.env.CONTRACT_ADDRESS_MXC_TESTNET,
{ gasPrice: '45000', from: process.env.ETHEREUM_ADDRESS });
DApp.contractInstanceMXC = TruffleContract(DApp.contractJSONMXC);
DApp.contractInstanceMXC.setProvider(DApp.provider);
// Use .deployed() instead when using development chain
DApp.contractInstanceMXC.at(DApp.contractAddressMXC);
DApp.coinbase = await DApp.web3.eth.getCoinbase();
DApp.getBlock();
},
infuraHttpProvider: null,
getBlock: async () => {
const block = await DApp.web3.eth.getBlock("latest");
debug('Current block: ', block.timestamp);
// FIXME - encounter same error as here https://github.com/MetaMask/web3-provider-engine/issues/313
// so web3.eth needs a fix similar to the one MetaMask made (i.e. `getBlockByNumberWithRetry`)
// consider switching to using https://github.com/MetaMask/web3-provider-engine
// const block = await DApp.web3.eth.getBlock("latest");
// debug('Current block: ', block.timestamp);
},
provider: null,
sendTransactionEth: async (to) => {
Expand All @@ -47,6 +54,24 @@ const DApp = {
transactionHashUrl: `https://ropsten.etherscan.io/tx/${transactionHash}`
};
},
sendTransactionMxc: async (to) => {
debug('Recipient: ', to);
debug('Sending Funds. Please wait...');
// Show Ethereum address associated with mnemonic
const amount = new BN(1, 10);
// https://web3js.readthedocs.io/en/v1.2.6/web3-utils.html#towei
// const approved = await DApp.contractInstanceMXC.methods.approve(DApp.coinbase, amount);
DApp.contractInstanceMXC.at(DApp.contractAddressMXC);
const transactionHash = await DApp.contractInstanceMXC.methods.transfer(
to.toLowerCase(),
amount
)
debug('transactionHash', transactionHash);

return {
transactionHashUrl: `https://ropsten.etherscan.io/tx/${transactionHash}`
};
},
web3: null,
};

Expand Down
22 changes: 21 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const path = require('path');
const app = express();
const port = process.env.PORT || 5000;
const { DApp: {
web3, provider, contractInstanceMXC, sendTransactionEth
web3, provider, contractInstanceMXC, sendTransactionEth, sendTransactionMxc
} } = require('./helpers/contract.js');
// debug('provider', provider);
// debug('contract', contractInstanceMXC);
Expand Down Expand Up @@ -35,6 +35,26 @@ app.get('/api/faucet/eth/ropsten', async (req, res, next) => {
}
});

/**
* Example: http://localhost:5000/api/faucet/mxc/ropsten?address=0x1f7ace08af5c49a5d69fbb98fb9339a729b27161
*/
app.get('/api/faucet/mxc/ropsten', async (req, res, next) => {
// Handle error in async function
try {
const to = req.query.address;
const { transactionHashUrl } = await sendTransactionMxc(to);
res.send({
result: 'Ropsten MXC ERC-20 sent',
tx: transactionHashUrl
});
} catch (error) {
debug(error);
// Handle error by Express or pass to custom error handler
// return next(error); // Crashes
return;
}
});

app.post('/api/world', (req, res) => {
console.log(req.body);
res.send(
Expand Down
38 changes: 37 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@
dependencies:
defer-to-connect "^1.0.1"

"@truffle/blockchain-utils@^0.0.11":
version "0.0.11"
resolved "https://registry.yarnpkg.com/@truffle/blockchain-utils/-/blockchain-utils-0.0.11.tgz#9886f4cb7a9f20deded4451ac78f8567ae5c0d75"
integrity sha512-9MyQ/20M96clhIcC7fVFIckGSB8qMsmcdU6iYt98HXJ9GOLNKsCaJFz1OVsJncVreYwTUhoEXTrVBc8zrmPDJQ==

"@truffle/blockchain-utils@^0.0.20":
version "0.0.20"
resolved "https://registry.yarnpkg.com/@truffle/blockchain-utils/-/blockchain-utils-0.0.20.tgz#17af5712504861cb374d3bbfa201ff0a2d2fead2"
Expand All @@ -204,7 +209,7 @@
utf8 "^3.0.0"
web3-utils "1.2.1"

"@truffle/contract-schema@^3.2.0":
"@truffle/contract-schema@^3.0.14", "@truffle/contract-schema@^3.2.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@truffle/contract-schema/-/contract-schema-3.2.0.tgz#4ddd43cf3eda11ec82bb2661725a19c2f4326e96"
integrity sha512-yeb4UoK9cbrT5/Nuz0I0p2XKbf0K1wEmyyBQmo3Q4JOrLidxf59LtDupo9Uq74RtlTAxZC0cy9DnsfWeWVma4A==
Expand Down Expand Up @@ -246,6 +251,11 @@
highlightjs-solidity "^1.0.16"
node-dir "0.1.17"

"@truffle/error@^0.0.6":
version "0.0.6"
resolved "https://registry.yarnpkg.com/@truffle/error/-/error-0.0.6.tgz#75d499845b4b3a40537889e7d04c663afcaee85d"
integrity sha512-QUM9ZWiwlXGixFGpV18g5I6vua6/r+ZV9W/5DQA5go9A3eZUNPHPaTKMIQPJLYn6+ZV5jg5H28zCHq56LHF3yA==

"@truffle/error@^0.0.8":
version "0.0.8"
resolved "https://registry.yarnpkg.com/@truffle/error/-/error-0.0.8.tgz#dc94ca36393403449d4b7461bf9452c241e53ec1"
Expand Down Expand Up @@ -4757,6 +4767,32 @@ trim-right@^1.0.1:
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"
integrity sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=

truffle-contract@^4.0.31:
version "4.0.31"
resolved "https://registry.yarnpkg.com/truffle-contract/-/truffle-contract-4.0.31.tgz#e43b7f648e2db352c857d1202d710029b107b68d"
integrity sha512-u3q+p1wiX5C2GpnluGx/d2iaJk7bcWshk2/TohiJyA2iQiTfkS7M4n9D9tY3JqpXR8PmD/TrA69RylO0RhITFA==
dependencies:
"@truffle/blockchain-utils" "^0.0.11"
"@truffle/contract-schema" "^3.0.14"
"@truffle/error" "^0.0.6"
bignumber.js "^7.2.1"
ethers "^4.0.0-beta.1"
truffle-interface-adapter "^0.2.5"
web3 "1.2.1"
web3-core-promievent "1.2.1"
web3-eth-abi "1.2.1"
web3-utils "1.2.1"

truffle-interface-adapter@^0.2.5:
version "0.2.5"
resolved "https://registry.yarnpkg.com/truffle-interface-adapter/-/truffle-interface-adapter-0.2.5.tgz#aa0bee635517b4a8e06adcdc99eacb993e68c243"
integrity sha512-EL39OpP8FcZ99ne1Rno3jImfb92Nectd4iVsZzoEUCBfbwHe7sr0k+i45guoruSoP8nMUE81Mov2s8I5pi6d9Q==
dependencies:
bn.js "^4.11.8"
ethers "^4.0.32"
lodash "^4.17.13"
web3 "1.2.1"

tslib@^1.9.0:
version "1.13.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.13.0.tgz#c881e13cc7015894ed914862d276436fa9a47043"
Expand Down

0 comments on commit 23ed7bd

Please sign in to comment.