From 67edf8f1491a2f0c62281d58bc1408815eebebc3 Mon Sep 17 00:00:00 2001 From: Andrea Franz Date: Wed, 1 Jul 2020 12:35:02 +0200 Subject: [PATCH] fix bucket/redeemable deploy and add relayer URI --- scripts/account.js | 8 +++++++- scripts/create-redeemable.js | 27 +++++++++++++++++++++++---- src/actions/redeem.ts | 2 +- src/components/Home.tsx | 10 ---------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/scripts/account.js b/scripts/account.js index bda91da..b1f5c0f 100644 --- a/scripts/account.js +++ b/scripts/account.js @@ -43,9 +43,15 @@ module.exports = class Account { receipt = await methodCall.send({from: this.sender, gas: gasAmount}); } else { let gasAmount = await methodCall.estimateGas({from: this.sender.address}); + console.log("estimated gas", gasAmount) let data = methodCall.encodeABI(); - let signedTx = await this.sender.signTransaction({to: to, data: data, gas: gasAmount}); + let signedTx = await this.sender.signTransaction({ + to: to, + data: data, + gas: gasAmount, + }); receipt = await this.web3.eth.sendSignedTransaction(signedTx.rawTransaction); + console.log("gas used", receipt["gasUsed"]); } return receipt; diff --git a/scripts/create-redeemable.js b/scripts/create-redeemable.js index fb19956..8989ae5 100644 --- a/scripts/create-redeemable.js +++ b/scripts/create-redeemable.js @@ -21,6 +21,9 @@ const Bucket = utils.loadContract(web3, `${classPrefix}Bucket`); const ERC721 = utils.loadContract(web3, "IERC721"); const ERC20 = utils.loadContract(web3, "IERC20Detailed"); +const KECCAK_EMPTY_STRING = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"; +const KECCAK_EMPTY_STRING2 = web3.utils.sha3(KECCAK_EMPTY_STRING); + async function deployFactory() { let code = "0x" + BucketFactoryCode; let methodCall = BucketFactory.deploy({data: code}); @@ -38,7 +41,8 @@ async function deployBucket(factory, token, startInDays, validityInDays, maxTxDe try { let receipt = await account.sendMethod(methodCall, BucketFactory.options.address); - return receipt.events.BucketCreated.returnValues.bucket; + let bucketAddress = receipt.logs[0].topics[2].slice(26); + return `0x${bucketAddress}`; } catch(err) { console.error(err); return null; @@ -46,6 +50,7 @@ async function deployBucket(factory, token, startInDays, validityInDays, maxTxDe } async function createRedeemable(keycard) { + console.log("creating redeemable", keycard.keycard, keycard.amount) let methodCall = Bucket.methods.createRedeemable(keycard.keycard, keycard.amount, keycard.code); try { @@ -74,11 +79,15 @@ async function transferNFT(keycard) { } function processCode(code) { + if (code === "" || code === undefined) { + return KECCAK_EMPTY_STRING2; + } + if (!code.startsWith("0x")) { code = "0x" + Buffer.from(code, 'utf8').toString('hex'); } - return "0x" + keccak256(code); + return "0x" + web3.utils.sha3(code); } function processAmount(amount, decimals) { @@ -146,6 +155,15 @@ async function run() { } bucket = await deployBucket(factory, argv["token"], argv["start-in-days"], argv["validity-days"], argv["max-tx-delay-blocks"]); + + if (argv["relayer-uri"] !== undefined && argv["relayer-uri"] !== "") { + const uri = argv["relayer-uri"]; + Bucket.options.address = bucket; + console.log("setting relayer URI to ", uri); + let setRelayerURI = Bucket.methods.setRelayerURI(uri); + await account.sendMethod(setRelayerURI, bucket); + } + hasDoneSomething = true; console.log("Bucket deployed at: " + bucket); } else { @@ -165,10 +183,11 @@ async function run() { const decimals = await getDecimals(argv["amount-decimals"], !argv["nft"]); let file = fs.readFileSync(argv["file"], 'utf8'); - keycards = file.split("\n").map((line) => processLine(line, decimals)); + keycards = file.split("\n").filter(line => line.trim() !== "").map((line) => processLine(line, decimals)); for (let keycard of keycards) { - await argv["nft"] ? transferNFT(keycard) : createRedeemable(keycard); + const create = argv["nft"] ? transferNFT : createRedeemable; + await create(keycard); } } else if (!hasDoneSomething) { console.error("the --file option must be specified"); diff --git a/src/actions/redeem.ts b/src/actions/redeem.ts index 58d79ed..533d562 100644 --- a/src/actions/redeem.ts +++ b/src/actions/redeem.ts @@ -222,7 +222,7 @@ const sendTransaction = (account: string, bucket: any, bucketAddress: string, me if (uri === "") { dispatch(sendEthTransaction(account, bucket, message, sig)); } else { - dispatch(sendTransactionToRelayer(uri, bucket.address, message, sig)); + dispatch(sendTransactionToRelayer(uri, bucket.options.address, message, sig)); } }).catch((err: any) => { dispatch(debug(`error getting relayerURI: ${err}`)); diff --git a/src/components/Home.tsx b/src/components/Home.tsx index c053dbc..ef1f17a 100644 --- a/src/components/Home.tsx +++ b/src/components/Home.tsx @@ -2,20 +2,10 @@ import React from 'react'; import { useDispatch, } from 'react-redux'; -import { - deployERC20, - deployNFT, -} from '../actions/newContract'; export default function() { const dispatch = useDispatch(); - const deployERC20Handler = () => dispatch(deployERC20()); - const deployNFTHandler = () => dispatch(deployNFT()); return <> -

- - -

; }