casper-erc20-js-client-test
v0.0.2
Published
Casper ERC20(CEP18) JavaScript Client
Downloads
2
Maintainers
Readme
casper-erc20-js-client
This JavaScript client gives you an easy way to install and interact with the Casper ERC-20 contract.
Installation
Run this command to install the client:
npm i casper-erc20-js-client
Usage example
Create an instance of the ERC-20 client:
const erc20 = new ERC20Client( client, // CasperClient 'hash-8e7b7820566b1aa9349f168245bedf003a305282baa8c53a5fa1e0efdfa04b8e', // Contract Hash optional 'hash-eb13cd75118f1eae57ad7be0d707b069b70e5362fe68b045fa4924195735e51b' // Contract Package Hash optional );
Install the contract:
const deploy = await erc20.installERC20( wasm, // Contract wasm { name: tokenName, symbol: tokenSymbol, decimals: tokenDecimals, totalSupply: totalSupply }, 60_000_000_000, // Payment Amount ownerPublicKey, CHAIN_NAME, [owner] ); await client.putDeploy(deploy); const result = await client.nodeClient.waitForDeploy(deploy);
Set the contract hash (a unique identifier for the network):
erc20.setContractHash( 'hash-c2402c3d88b13f14390ff46fde9c06b8590c9e45a9802f7fb8a2674ff9c1e5b1' );
You can retrieve token infomation by calling these methods:
const name = await erc20.name(); const symbol = await erc20.symbol(); const totalSupply = await erc20.totalSupply(); const decimals = await erc20.decimals();
Transfers
Transfer some tokens from the direct caller to a recipient:
const deploy = erc20.transfer( { recipient: recipientPublicKey, amount: 50_000_000_000 }, 5_000_000_000, // Payment amount ownerPublicKey, CHAIN_NAME, [ownerAsymmetricKey] // Optional );
Transfer from an account owner to a recipient given that the direct caller has been previously approved to spend the specified amount on behalf of the owner:
const deploy = erc20.transferFrom( { owner: ownerPublicKey, recipient: recipientPublicKey, amount: transferAmount }, 5_000_000_000, approvedPublicKey, CHAIN_NAME, [approvedAsymmetricKey] );
Balances
Request the balance of an account with balanceOf:
const balance = await erc20.balanceOf(publicKey);
Approvals
Allow a spender to transfer up to a number of the direct caller’s tokens:
const deploy = erc20.approve( { spender: spenderPublicKey, amount: approveAmount }, 5_000_000_000, ownerPublicKey, CHAIN_NAME, [ownerAsymmetricKey] );
Allowance
Return the number of owner’s tokens allowed to be spent by spender:
const allowance = await erc20.allowances( ownersPublicKey, spenderPublicKey );
More examples
You can find all the available examples in the E2E test script.
Test
- Clone this repo
git clone https://github.com/casper-ecosystem/erc20.git
- Go to
client
directory
cd client
- Intall modules using
npm install
- You can test the script by running the local network. After running the local network run the test by
npm test