note: right now this project is still under development, ethers
and web3js
lib are stick together in one module, later we will spilt to different iso packages, please get back to check again.
to use polyjuice-provider, we assume you already running a godwoken-polyjuice devnet chain using Godwoken-Kicker in your local environment.
first, install the packages:
yarn add @polyjuice-provider/core
const Web3 = require("web3");
const PolyjuiceHttpProvider = require("@polyjuice-provider/core/lib/cli");
// init provider and web3
const godwoken_rpc_url = 'godwoken web3 rpc url'; // normally it is http://localhost:8024
const provider_config = {
godwoken: {
rollup_type_hash: 'godwoken rollup type hash', // you can find this value by opening your browser to access http://localhost:6101/get_rollup_type_hash
eth_account_lock: {
code_hash: 'godwoken eth lock code hash', // you can find this value by opening your browser to access http://localhost:6101/get_eth_acccount_lock
hash_type: "type",
},
},
};
provider = new PolyjuiceHttpProvider(
godwoken_rpc_url,
provider_config,
['your contract abi array'],
'your deployer private_key'
);
const web3 = new Web3(provider);
const txResponse = await web3.eth.sendTransaction({
from: 'deployer eth address',
to: `0x${Array(40).fill(0).join("")}`,
value: "0x00",
data: "your contract compiled bytecode with 0x prefix",
gas: "0x30d40", // some gas
gasPrice: "0x00", // you can set gasPrice as 0
});
const txReceipt = await web3.eth.getTransactionReceipt(txResponse.transactionHash);
const contract_deployed_address = txReceipt.contractAddress;
note: later we will support using web3.accounts
to sign transaction and deploy contract.
first, install the packages:
yarn add @polyjuice-provider/core
because hardhat use ethers
, so you will need to make the replacement below:
replace
import { Wallet } from "ethers";
with
import PolyjuiceWallet from "@polyjuice-provider/core/lib/hardhat/wallet-signer";
and use PolyjuiceWallet
instead of Wallet
in your code.
replace
import { providers } from "ethers";
providers.JsonRpcProvider
with
import { PolyjuiceJsonRpcProvider } from "@polyjuice-provider/core/lib/hardhat/providers";
and use PolyjuiceJsonRpcProvider
instead of JsonRpcProvider
in your code.
example:
import { ContractFactory } from "ethers";
import PolyjuiceWallet, { PolyjuiceConfig } from "@polyjuice-provider/core/lib/hardhat/wallet-signer";
import { PolyjuiceJsonRpcProvider } from "@polyjuice-provider/core/lib/hardhat/providers";
const polyjuice_config: PolyjuiceConfig = {
godwokerOption: {
godwoken: {
rollup_type_hash: 'godwoken rollup type hash', // you can find this value by opening your browser to access http://localhost:6101/get_rollup_type_hash,
eth_account_lock: {
code_hash: 'godwoken eth lock code hash', // you can find this value by opening your browser to access http://localhost:6101/get_eth_acccount_lock
hash_type: "type",
},
},
},
web3RpcUrl: process.env.RPC_URL!, // normally it is http://localhost:8024;
abiItems: ['your contract abi array']
};
const rpc = new PolyjuiceJsonRpcProvider(polyjuice_config.godwokerOption, polyjuice_config.abiItems, 'godwoken web3 rpc url'); // normally it is http://localhost:8024;
const deployer = new PolyjuiceWallet('<your deployer private key>', polyjuice_config, rpc);
const implementationFactory = new ContractFactory(
contract.abi,
contract.bytecode,
deployer,
);
const tx = implementationFactory.getDeployTransaction();
tx.gasPrice = 0;
tx.gasLimit = 1_000_000;
deployer.sendTransaction(tx);
a more complete and real example can be found here
if you are using web3.js
and metamask
, you can simply change your dapp like following:
Before:
import Web3 from 'web3';
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8024'));
Now:
import Web3 from 'web3';
import PolyjuiceHttpProvider from '@polyjuice-provider/core';
var web3 = new Web3(new PolyjuiceHttpProvider('http://localhost:8024', GodwokenOption, ['your abi items array']));
for GodwokenOption
: see here.
Before:
import Web3 from 'web3';
var provider = new Web3.providers.HttpProvider('http://localhost:8024');
var web3 = new Web3(provider);
var contract = web3.eth.Contract(abi, contract_address);
Now:
import Web3 from 'web3';
import PolyjuiceHttpProvider from '@polyjuice-provider/core';
var provider = new PolyjuiceHttpProvider('http://localhost:8024', GodwokenOption, ['your abi items array']);
var web3 = new Web3(provider);
var contract = web3.eth.Contract(abi, contract_address);
if you are using ethers
, the usage in dapp is mostly the same with deploying contract above.
lastly, we will introduce more polyjuice compatible lib with ETH in the future.