metisdac-accessor v0.0.2
BlockchainAccessor
BlockchainAccessor is a frontend library that allows you to access the smart contracts of the Metis Governance. With a specified backend apiUrl, BlockchainAccessor can send transactions to Metis Governance smart contracts and decide to use gasless way according to backend settings.
Installation
npm install metisdac-accessor
Usage
Creating a BlockchainAccessor instance
To create a new instance of BlockchainAccessor, you need to provide the following parameters:
chainId
: The ID of the blockchain network you want to connect to.apiUrl
: The URL of the backend API that provides access to the smart contracts.signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
import { BlockchainAccessor } from "metisdac-accessor";
const blockchain = await BlockchainAccessor.create({
chainId: 599,
apiUrl: "https://agw-dev.onrender.com/api/v2",
signerOrProvider: provider.getSigner(),
});
Accessing Smart Contracts
Once you have created a BlockchainAccessor instance, you can access the smart contracts using the contracts
property.
const dac = blockchain.contracts.DAC(dacAddress);
You can access the following smart contracts:
Configuration
DACFactory
DAC
DACHelper
Governance
Mining
Forwarder
PermitManager
RP
Sending Transactions
You can send transactions by calling the functions of the smart contracts.
const tx = await dac.join(0, []);
Gasless Transactions
BlockchainAccessor can decide to use gasless way according to backend settings. If gasless is enabled, the transaction will be sent without requiring the user to pay gas fees.
Full Example
import { BlockchainAccessor } from "metisdac-accessor";
async function join(blockchain: BlockchainAccessor, dacAddress: string) {
console.log("join dac", dacAddress);
const dac = blockchain.contracts.DAC(dacAddress);
const tx = await dac.join(0, []);
console.log("sent tx:", tx);
const receipt = await tx.wait();
console.log("receipt=", receipt);
return tx.hash;
}
async function quit(blockchain: BlockchainAccessor, dacAddress: string) {
console.log("quit dac", dacAddress);
const dac = blockchain.contracts.DAC(dacAddress);
const tx = await dac.quit();
console.log("sent tx:", tx);
const receipt = await tx.wait();
console.log("receipt=", receipt);
return tx.hash;
}
async function fetchBlockchainData() {
//@ts-ignore
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
console.log("create BlockchainAccessor");
const blockchain = await BlockchainAccessor.create({
chainId: 599,
apiUrl: "https://agw-dev.onrender.com/api/v2",
signerOrProvider: provider.getSigner(),
});
return blockchain;
}
function App() {
const [blockchain, setBlockchain] = useState(null);
useEffect(() => {
async function fetchData() {
const blockchainData = await fetchBlockchainData();
setBlockchain(blockchainData);
}
fetchData();
}, []);
if (!blockchain) {
return <div>Loading...</div>;
}
return (
<div className="App">
<DemoForm blockchain={blockchain} />
</div>
);
}
class DemoForm extends React.Component<{ blockchain: BlockchainAccessor }> {
state = {
dacAddress: "",
tx: "-",
};
render() {
return (
<div>
DAC:
<input
placeholder="dac address here"
value={this.state.dacAddress}
onChange={(evt) => this.updateInputValue(evt)}
/>
<button
onClick={async () =>
this.setState({
tx: await join(this.props.blockchain, this.state.dacAddress),
})
}
>
Join
</button>
<button
onClick={async () =>
this.setState({
tx: await quit(this.props.blockchain, this.state.dacAddress),
})
}
>
Quit
</button>
<div>tx: {this.state.tx}</div>
</div>
);
}
updateInputValue(evt) {
console.log("this.state.dacAddress=", this.state.dacAddress, "evt=", evt);
this.setState({ dacAddress: evt.target.value });
}
}
API Reference
BlockchainAccessor
create({ chainId, apiUrl, signerOrProvider }): Promise<BlockchainAccessor>
Creates a new instance of BlockchainAccessor with the specified parameters.
chainId
: The ID of the blockchain network you want to connect to.apiUrl
: The URL of the backend API that provides access to the smart contracts.signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
Returns a Promise that resolves with a new instance of BlockchainAccessor.
Contracts
Configuration(signerOrProvider?: Signer | Provider)
Returns an instance of the Configuration
smart contract.
signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
Returns an instance of the Configuration
smart contract.
DACFactory(signerOrProvider?: Signer | Provider)
Returns an instance of the DACFactory
smart contract.
signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
Returns an instance of the DACFactory
smart contract.
DAC(dacAddress: string, signerOrProvider?: Signer | Provider)
Returns an instance of the DAC
smart contract with the specified address.
dacAddress
: The address of theDAC
smart contract.signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
Returns an instance of the DAC
smart contract.
DACHelper(signerOrProvider?: Signer | Provider)
Returns an instance of the DACHelper
smart contract.
signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
Returns an instance of the DACHelper
smart contract.
Governance(signerOrProvider?: Signer | Provider)
Returns an instance of the Governance
smart contract.
signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
Mining(signerOrProvider?: Signer | Provider)
Returns an instance of the Mining
smart contract.
signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
Returns an instance of the EcoNode
smart contract.
Forwarder(signerOrProvider?: Signer | Provider)
Returns an instance of the Forwarder
smart contract.
signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
Returns an instance of the Forwarder
smart contract.
PermitManager(signerOrProvider?: Signer | Provider)
Returns an instance of the PermitManager
smart contract.
signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
Returns an instance of the PermitManager
smart contract.
RP(signerOrProvider?: Signer | Provider)
Returns an instance of the RP
smart contract.
signerOrProvider
: An optional parameter that allows you to specify a signer or provider for the transactions.
Returns an instance of the RP
smart contract.