use truffle contracts in dapp and scripts

This commit is contained in:
Andrea Franz 2020-09-23 11:22:04 +02:00 committed by Bitgamma
parent 9f4a48e282
commit 8978ae31e2
10 changed files with 2229 additions and 7622 deletions

3
.gitignore vendored
View File

@ -11,4 +11,5 @@ embarkArtifacts
node_modules node_modules
yarn-error.log yarn-error.log
/src/react-app-env.d.ts /src/react-app-env.d.ts
/scripts/contracts /client/src/react-app-env.d.ts
/client/src/contracts

View File

@ -1,19 +1,17 @@
-include .env -include .env
CONTRACTS_PATH="./contracts" # CONTRACTS_PATH="./contracts"
RELAYER_CONTRACTS_PATH=$$(realpath ./scripts/contracts-build)
RELAYER_DOCKER_FILE="./_assets/Dockerfile.relayer" RELAYER_DOCKER_FILE="./_assets/Dockerfile.relayer"
RELAYER_DOCKER_IMAGE="redeem-relayer" RELAYER_DOCKER_IMAGE="redeem-relayer"
RELAYER_DOCKER_CONTAINER_NAME="redeem-relayer" RELAYER_DOCKER_CONTAINER_NAME="redeem-relayer"
RELAYER_CONTRACTS_PATH=$$(realpath ./scripts/contracts)
CONTAINER_KEYSTORES_PATH="/usr/keystores" CONTAINER_KEYSTORES_PATH="/usr/keystores"
build-relayer-contracts: compile-contracts:
rm -rf $(RELAYER_CONTRACTS_PATH); \ truffle compile
yarn run solcjs -o $(RELAYER_CONTRACTS_PATH) --bin --abi $$(find $(CONTRACTS_PATH) -name *.sol); \
for fileName in $$(ls $(RELAYER_CONTRACTS_PATH)); do \ deploy-contracts: compile-contracts
newName=$${fileName##*_}; \ truffle deploy
mv $(RELAYER_CONTRACTS_PATH)/$${fileName} $(RELAYER_CONTRACTS_PATH)/$${newName}; \
done;
check-relayer-env-variables: check-relayer-env-variables:
ifndef ENDPOINT ifndef ENDPOINT

View File

@ -40,28 +40,14 @@
"solc": "^0.6.10", "solc": "^0.6.10",
"typeface-roboto": "^0.0.75", "typeface-roboto": "^0.0.75",
"typescript": "^3.8.3", "typescript": "^3.8.3",
"web3": "^1.2.7", "web3": "^1.2.11",
"web3-eth": "1.2.7" "web3-eth": "1.2.11"
}, },
"eslintConfig": { "eslintConfig": {
"extends": "react-app" "extends": "react-app"
}, },
"devDependencies": { "devDependencies": {
"@types/classnames": "^2.2.10", "@types/classnames": "^2.2.10"
"embark": "^5.3.0-nightly.7",
"embark-geth": "^5.3.0-nightly.7",
"embark-graph": "^5.3.0-nightly.7",
"embark-ipfs": "^5.3.0-nightly.7",
"embark-parity": "^5.3.0-nightly.7",
"embark-profiler": "^5.3.0-nightly.7",
"embark-swarm": "^5.3.0-nightly.7",
"embark-whisper-geth": "^5.3.0-nightly.7",
"embarkjs": "^5.3.0-nightly.6",
"embarkjs-ens": "^5.3.0-nightly.6",
"embarkjs-ipfs": "^5.3.0-nightly.4",
"embarkjs-swarm": "^5.3.0-nightly.4",
"embarkjs-web3": "^5.3.0-nightly.4",
"embarkjs-whisper": "^5.3.0-nightly.4"
}, },
"browserslist": { "browserslist": {
"production": [ "production": [

View File

@ -95,6 +95,7 @@ export const redeem = (bucketAddress: string, recipientAddress: string, cleanCod
} }
dispatch(redeeming()); dispatch(redeeming());
const state = getState(); const state = getState();
const web3Type = state.web3.type; const web3Type = state.web3.type;
@ -128,7 +129,6 @@ export const redeem = (bucketAddress: string, recipientAddress: string, cleanCod
} }
dispatch<any>(sendTransaction(account, bucket, bucketAddress, message, sig)); dispatch<any>(sendTransaction(account, bucket, bucketAddress, message, sig));
}).catch((err: any) => { }).catch((err: any) => {
console.error("sign redeem error reason:", err.reason); console.error("sign redeem error reason:", err.reason);
console.error("sign redeem error:", err); console.error("sign redeem error:", err);
@ -202,8 +202,7 @@ const signWithWeb3 = (signer: string, data: any): Promise<SignRedeemResponse> =>
const signWithKeycard = (data: any): Promise<SignRedeemResponse> => { const signWithKeycard = (data: any): Promise<SignRedeemResponse> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
(window as any).ethereum.send("keycard_signTypedData", JSON.stringify(data)).then((resp: any) => { (window as any).ethereum.send("keycard_signTypedData", JSON.stringify(data)).then((sig: any) => {
const sig = resp.result;
const signer = recoverTypedSignature({ const signer = recoverTypedSignature({
data, data,
sig sig
@ -219,6 +218,7 @@ const signWithKeycard = (data: any): Promise<SignRedeemResponse> => {
const sendTransaction = (account: string, bucket: any, bucketAddress: string, message: RedeemMessage, sig: string) => { const sendTransaction = (account: string, bucket: any, bucketAddress: string, message: RedeemMessage, sig: string) => {
return (dispatch: Dispatch, getState: () => RootState) => { return (dispatch: Dispatch, getState: () => RootState) => {
bucket.methods.relayerURI().call().then((uri: string) => { bucket.methods.relayerURI().call().then((uri: string) => {
dispatch(debug(`relayer URI: ${uri}`));
if (uri === "") { if (uri === "") {
dispatch<any>(sendEthTransaction(account, bucket, message, sig)); dispatch<any>(sendEthTransaction(account, bucket, message, sig));
} else { } else {

View File

@ -1,14 +1,27 @@
import { RootState } from '../reducers'; import { RootState } from '../reducers';
import ERC20BucketFactory from '../embarkArtifacts/contracts/ERC20BucketFactory'; import ERC20BucketFactory from '../contracts/ERC20BucketFactory.json';
import NFTBucketFactory from '../embarkArtifacts/contracts/NFTBucketFactory'; import NFTBucketFactory from '../contracts/NFTBucketFactory.json';
import ERC20Bucket from '../embarkArtifacts/contracts/ERC20Bucket'; import ERC20Bucket from '../contracts/ERC20Bucket.json';
import Bucket from '../embarkArtifacts/contracts/Bucket'; import Bucket from '../contracts/Bucket.json';
import IERC20Detailed from '../embarkArtifacts/contracts/IERC20Detailed'; import IERC20Detailed from '../contracts/IERC20Detailed.json';
import IERC721Metadata from '../embarkArtifacts/contracts/IERC721Metadata'; import IERC721Metadata from '../contracts/IERC721Metadata.json';
import { config } from "../config"; import { config } from "../config";
import { Dispatch } from 'redux'; import { Dispatch } from 'redux';
import { ZERO_ADDRESS } from "../utils"; import { ZERO_ADDRESS } from "../utils";
import { debug } from "./debug"; import { debug } from "./debug";
import { AbiItem } from "web3-utils";
interface ContractSpecs {
networks: {
[id: string]: {
address: string
}
}
}
const contractAddress = (specs: ContractSpecs, networkID: string | number): string => {
return specs.networks[networkID.toString()].address;
}
export const ERROR_REDEEMABLE_NOT_FOUND = "ERROR_REDEEMABLE_NOT_FOUND"; export const ERROR_REDEEMABLE_NOT_FOUND = "ERROR_REDEEMABLE_NOT_FOUND";
export interface ErrRedeemableNotFound { export interface ErrRedeemableNotFound {
@ -166,21 +179,22 @@ export const tokenMetadataLoaded = (tokenAddress: string, recipient: string, met
}); });
export const newBucketContract = (address: string) => { export const newBucketContract = (address: string) => {
const bucketAbi = Bucket.options.jsonInterface; const bucketAbi = Bucket.abi as AbiItem[];
const bucket = new config.web3!.eth.Contract(bucketAbi, address); const bucket = new config.web3!.eth.Contract(bucketAbi, address);
return bucket; return bucket;
} }
export const newERC20BucketContract = (address: string) => { export const newERC20BucketContract = (address: string) => {
const bucketAbi = ERC20Bucket.options.jsonInterface; const bucketAbi = ERC20Bucket.abi as AbiItem[];
const bucket = new config.web3!.eth.Contract(bucketAbi, address); const bucket = new config.web3!.eth.Contract(bucketAbi, address);
return bucket; return bucket;
} }
export const loadRedeemable = (bucketAddress: string, recipientAddress: string) => { export const loadRedeemable = (bucketAddress: string, recipientAddress: string) => {
return async (dispatch: Dispatch, getState: () => RootState) => { return async (dispatch: Dispatch, getState: () => RootState) => {
dispatch(debug(`erc20 factory address: ${ERC20BucketFactory.address}`)); const networkID = getState().web3.networkID!;
dispatch(debug(`nft factory address: ${NFTBucketFactory.address}`)); dispatch(debug(`erc20 factory address: ${contractAddress(ERC20BucketFactory, networkID)}`));
dispatch(debug(`nft factory address: ${contractAddress(NFTBucketFactory, networkID)}`));
dispatch(debug(`bucket address: ${bucketAddress}`)); dispatch(debug(`bucket address: ${bucketAddress}`));
dispatch(debug(`recipient address: ${recipientAddress}`)); dispatch(debug(`recipient address: ${recipientAddress}`));
dispatch(loadingRedeemable(bucketAddress, recipientAddress)); dispatch(loadingRedeemable(bucketAddress, recipientAddress));
@ -232,7 +246,7 @@ export const loadToken = (bucket: any, data: string, recipient: string) => {
export const loadERC20Token = (bucket: any, data: string, recipient: string) => { export const loadERC20Token = (bucket: any, data: string, recipient: string) => {
return (dispatch: Dispatch, getState: () => RootState) => { return (dispatch: Dispatch, getState: () => RootState) => {
bucket.methods.tokenAddress().call().then(async (address: string) => { bucket.methods.tokenAddress().call().then(async (address: string) => {
const erc20Abi = IERC20Detailed.options.jsonInterface; const erc20Abi = IERC20Detailed.abi as AbiItem[];
const erc20 = new config.web3!.eth.Contract(erc20Abi, address); const erc20 = new config.web3!.eth.Contract(erc20Abi, address);
dispatch(loadingToken(address)); dispatch(loadingToken(address));
@ -251,7 +265,7 @@ export const loadERC20Token = (bucket: any, data: string, recipient: string) =>
export const loadNFTToken = (bucket: any, data: string, recipient: string) => { export const loadNFTToken = (bucket: any, data: string, recipient: string) => {
return (dispatch: Dispatch, getState: () => RootState) => { return (dispatch: Dispatch, getState: () => RootState) => {
bucket.methods.tokenAddress().call().then(async (address: string) => { bucket.methods.tokenAddress().call().then(async (address: string) => {
const nftAbi = IERC721Metadata.options.jsonInterface; const nftAbi = IERC721Metadata.abi as AbiItem[];
const nft = new config.web3!.eth.Contract(nftAbi, address); const nft = new config.web3!.eth.Contract(nftAbi, address);
dispatch(loadingToken(address)); dispatch(loadingToken(address));

View File

@ -6,8 +6,11 @@ import {
import { RootState } from '../reducers'; import { RootState } from '../reducers';
import { debug } from "./debug"; import { debug } from "./debug";
export const VALID_NETWORK_NAME = "Ropsten"; export const VALID_NETWORK_NAME = "Ganache";
export const VALID_NETWORK_ID = 3; export const VALID_NETWORK_ID = 5777;
// export const VALID_NETWORK_NAME = "Ropsten";
// export const VALID_NETWORK_ID = 3;
// export const VALID_NETWORK_NAME = "Goerli"; // export const VALID_NETWORK_NAME = "Goerli";
// export const VALID_NETWORK_ID = 5; // export const VALID_NETWORK_ID = 5;

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,9 @@
"express": "^4.17.1", "express": "^4.17.1",
"minimist": "^1.2.5", "minimist": "^1.2.5",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"solc": "0.5.16",
"web3": "^1.2.7" "web3": "^1.2.7"
},
"devDependencies": {
} }
} }

View File

@ -1,19 +1,21 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const CONTRACTS_PATH="./contracts"; const CONTRACTS_PATH="./client/src/contracts";
module.exports.loadContractFile = (fileName) => { module.exports.loadContractFile = (fileName) => {
let content = fs.readFileSync(path.join(__dirname, CONTRACTS_PATH, fileName), "utf-8"); let content = fs.readFileSync(path.join(__dirname, "../", CONTRACTS_PATH, `${fileName}.json`), "utf-8");
return content; return content;
}; };
module.exports.loadContractCode = (contractName) => { module.exports.loadContractCode = (contractName) => {
return this.loadContractFile(`${contractName}.bin`); const content = this.loadContractFile(contractName);
const obj = JSON.parse(content);
return obj.bytecode;
}; };
module.exports.loadContract = (web3, contractName) => { module.exports.loadContract = (web3, contractName) => {
let content = this.loadContractFile(`${contractName}.abi`); let content = this.loadContractFile(contractName);
let abi = JSON.parse(content); const obj = JSON.parse(content);
return new web3.eth.Contract(abi); return new web3.eth.Contract(obj.abi);
}; };

1076
yarn.lock

File diff suppressed because it is too large Load Diff