我安装了web3以将Solidity链接到React-native。如下面的代码所示,我从Remix部署了它,并输入了地址和ABI。
import Web3 from "web3";
const web3 = new Web3(
new Web3.providers.HttpProvider("https://127.0.0.1:8545")
);
const address = "0x6cc33e32853a7fb0ac9ac2fb0d54cb56e4fb0f30";
const abi = [
{
constant: false,
inputs: [
{
name: "_MASTER",
type: "address"
},
{
name: "_sitter",
type: "address"
},
{
name: "_sitterprice",
type: "uint256"
}
],
name: "CompleteSharePet",
outputs: [],
payable: true,
stateMutability: "payable",
type: "function"
},
{
constant: false,
inputs: [
{
name: "_OWNER",
type: "address"
},
{
name: "_ownersharetext",
type: "string"
},
{
name: "_ownerprice",
type: "uint256"
}
],
name: "OwnerShareText",
outputs: [
{
name: "",
type: "string"
}
],
payable: false,
stateMutability: "nonpayable",
type: "function"
},
{
constant: false,
inputs: [
{
name: "_ownerseller",
type: "address"
},
{
name: "_petsitter",
type: "address"
},
{
name: "_MASTER",
type: "address"
},
{
name: "_ownerprice",
type: "uint256"
}
],
name: "PetChange",
outputs: [],
payable: true,
stateMutability: "payable",
type: "function"
},
{
constant: false,
inputs: [
{
name: "_PETSITTER",
type: "address"
},
{
name: "_master",
type: "address"
},
{
name: "_ownerprice",
type: "uint256"
}
],
name: "PetChangeTransaction",
outputs: [],
payable: true,
stateMutability: "payable",
type: "function"
},
{
constant: false,
inputs: [
{
name: "_petsittertext",
type: "string"
},
{
name: "_sitterprice",
type: "uint256"
}
],
name: "PetSitterShareText",
outputs: [
{
name: "",
type: "string"
}
],
payable: false,
stateMutability: "nonpayable",
type: "function"
},
{
constant: false,
inputs: [
{
name: "_sitter",
type: "address"
}
],
name: "testsitter",
outputs: [],
payable: true,
stateMutability: "payable",
type: "function"
},
{
constant: false,
inputs: [
{
name: "_master",
type: "address"
},
{
name: "_OWNER",
type: "address"
},
{
name: "_sitterprice",
type: "uint256"
}
],
name: "TransactionSharePet",
outputs: [],
payable: true,
stateMutability: "payable",
type: "function"
},
{
constant: false,
inputs: [
{
name: "hash",
type: "string"
},
{
name: "ipfs",
type: "string"
}
],
name: "uploadImage",
outputs: [],
payable: false,
stateMutability: "nonpayable",
type: "function"
},
{
constant: true,
inputs: [],
name: "AllOwnerCount",
outputs: [
{
name: "",
type: "uint256"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: true,
inputs: [],
name: "AllPetSitter",
outputs: [
{
name: "",
type: "uint256"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: true,
inputs: [],
name: "getOwnerAddresses",
outputs: [
{
name: "",
type: "address[]"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: true,
inputs: [
{
name: "_owner",
type: "address"
}
],
name: "getOwnerInfo",
outputs: [
{
name: "",
type: "string"
},
{
name: "",
type: "string"
},
{
name: "",
type: "string"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: true,
inputs: [],
name: "getPetSitterAddresses",
outputs: [
{
name: "",
type: "address[]"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: true,
inputs: [
{
name: "_petsitter",
type: "address"
}
],
name: "getPetSitterInfo",
outputs: [
{
name: "",
type: "string"
},
{
name: "",
type: "string"
},
{
name: "",
type: "string"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: true,
inputs: [
{
name: "",
type: "uint256"
}
],
name: "OwnerAddresses",
outputs: [
{
name: "",
type: "address"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: true,
inputs: [
{
name: "",
type: "uint256"
}
],
name: "PetSitterAddresses",
outputs: [
{
name: "",
type: "address"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: true,
inputs: [
{
name: "_OWNER",
type: "address"
}
],
name: "test",
outputs: [
{
name: "",
type: "uint256"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: true,
inputs: [
{
name: "_MASTER",
type: "address"
}
],
name: "test2",
outputs: [
{
name: "",
type: "uint256"
}
],
payable: false,
stateMutability: "view",
type: "function"
}
];
export class WalletSetting extends Component {
test() {
web3.eth.getAccounts().then(console.log);
}
render() {
return this.test;
}
}
export default (myContract = new web3.eth.Contract(abi, address));
该设置没有错误,但在使用mycontact进行导入时发生了错误
import WalletSetting from "../web3";
.
.
.
</TouchableOpacity>
<WalletSetting />
</LinearGradient>
我删除了所有的node_modules和所有的缓存,就像图片中的一样。但是,发生了相同的错误,尽管我们使用Goolgle搜索进行了艰苦的搜索,但尚未解决。
我正在等待一个好的答案。请为我提供解决方案。
我已将Web3版本下载为“ ^ 1.0.0-beta.34”,以解决现有错误,但发生了另一个错误。
错误是
无法从crypto
解析模块/Users/lucky/pettest/node_modules/web3-eth-accounts/src/index.js
:急速模块图中不存在模块crypto
所以我去Google参考https://gist.github.com/dougbacelar/29e60920d8fa1982535247563eb63766并安装“ node-libs-browser”:“ ^ 2.2.0”,“ babelpresetes2015”:“ ^ 6.24.1”,并创建一个名为rn-cli.config.js,global.js的文件,并将globaljs文件导入到您的web3.js文件中,但是错误是相同的。
我该如何解决?
https://gist.github.com/dougbacelar/29e60920d8fa1982535247563eb63766
此链接解决方案无法解决我的问题。
web3
可以在浏览器环境中使用。本机android环境不同于浏览器,例如,它没有crypto
使用的web3
类。结合使用web3
和react native将需要一些额外的配置。您可以遵循此指南:https://gist.github.com/dougbacelar/29e60920d8fa1982535247563eb63766。
关于加密,我有部分问题的解决方案。安装react-native-crypto
并将其另存为依赖项,然后将babel-plugin-module-resolver
保存为dev依赖项,然后将.babelrc
文件放入您的项目根目录中,该文件包含以下内容:
{
"plugins": [
["module-resolver", {
"root": ["."],
"alias": {
"crypto": "react-native-crypto"
}
}]
]
}
这将您安装的软件包中的所有require('crypto')
替换为require('react-native-crypto')
,并且对我有用。但是,就我而言,缺少密码的同一软件包也丢失了http
,所以我来到了这个线程,希望也能找到一个polyfill。我对有兴趣的人使用RN v0.60.3,npm v6.9.0和节点v10.16.0。同样,我需要此“填充”的程序包是request
程序包,它似乎是jest的子子子依赖项。 (npm list request
)。
我希望这对某人有帮助!
我在这里找到了此解决方案:https://stackoverflow.com/a/46605706/12375465此处:https://medium.com/studioarmix/the-joy-of-joi-hacking-node-deps-1337fd5e586f