Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/node_modules
/package-lock.json
/artifacts
/cache
21 changes: 12 additions & 9 deletions contracts/assist/Gas.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract Gas is ERC20{
constructor() ERC20("Test Token", "TestToken") {}
function mint(uint256 amount) public {
_mint(msg.sender, amount);
}
function getBalance() public view returns (uint256) {
return balanceOf(msg.sender);
}

contract Gas is ERC20 {
constructor() ERC20("Test Token", "TestToken") {}

function mint(uint256 amount) public {
_mint(msg.sender, amount);
}

function getBalance() public view returns (uint256) {
return balanceOf(msg.sender);
}
}
21 changes: 12 additions & 9 deletions contracts/assist/Token.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract Token is ERC20{
constructor() ERC20("Test Token", "TestToken") {}
function mint(uint256 amount) public {
_mint(msg.sender, amount);
}
function getBalance() public view returns (uint256) {
return balanceOf(msg.sender);
}

contract Token is ERC20 {
constructor() ERC20("Test Token", "TestToken") {}

function mint(uint256 amount) public {
_mint(msg.sender, amount);
}

function getBalance() public view returns (uint256) {
return balanceOf(msg.sender);
}
}
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
"private": true,
"dependencies": {
"@openzeppelin/contracts": "latest",
"@truffle/hdwallet-provider": "^1.6.0",
"@truffle/hdwallet-provider": "^2.1.15",
"bn.js": "^5.2.0",
"buffer": "^6.0.3",
"crypto-js": "^4.1.1",
"delphinus-curves": "git+ssh://git@github.com:DelphinusLab/delphinus-curves.git",
"zkwasm-deployment": "file:../zkWasm-deployment",
"fs-extra": "^10.0.0",
"mongodb": "^4.1.3",
"node-fetch": "^2.6.6",
"process": "^0.11.10",
"truffle": "latest",
"web3": "^1.7.5",
"web3subscriber": "git+ssh://git@github.com:DelphinusLab/delphinus-web3subscriber.git"
"truffle": "^5.5.19",
"web3": "^4.2.2",
"web3-providers-http": "^4.1.0",
"web3subscriber": "git+ssh://git@github.com:DelphinusLab/delphinus-web3subscriber.git#feature/ethers",
"zkwasm-deployment": "file:../zkWasm-deployment"
},
"scripts": {
"dev": "npx truffle dev",
Expand All @@ -33,10 +34,11 @@
"@types/crypto-js": "^4.0.2",
"@types/fs-extra": "^9.0.12",
"@types/jest": "^26.0.15",
"@types/lru-cache": "^5.1.1",
"@types/node": "^18.15.5",
"@types/retry": "^0.12.2",
"@types/lru-cache": "^5.1.1",
"@types/sha256": "^0.2.0",
"ethers": "^6.9.0",
"jest": "26.6.0",
"retry": "^0.13.1",
"ts-jest": "^26.0.0",
Expand Down
129 changes: 129 additions & 0 deletions src/clients/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
## Example usage for L1ServerClient and L1BrowserClient

### L1 Server Client

Requires the config file to be present in the `zkwasm-deployment` directory.

A simple example to mint ERC-20 tokens and transfer them to another account.

```typescript
import { withL1ServerClient, L1ServerClient } from "src/clients/client";
import { getConfigByChainName } from "zkwasm-deployment/src/config";
import { TxBinder } from "web3subscriber/src/txbinder";
import { L1ClientRole } from "zkwasm-deployment/src/types";

async function main(configName: string, targetAccount: string) {
let config = await getConfigByChainName(L1ClientRole.Monitor, configName);
let account = config.monitorAccount;

// Setup the client by providing the config with required information
await withL1ServerClient(config, async (l1client: L1ServerClient) => {
// Access underlying ethers providers and signers
const { signer, provider } = l1client;

// Signer is only available if the config has a private key, otherwise the client will be in read-only mode

// Access the deployed contracts configured with this client
// Similarly, these are read-only if the config does not have a private key
let gasToken = l1client.getGasContract();
let token = l1client.getTokenContract();
let proxy = l1client.getProxyContract();

// Use TxBinder to assign callbacks to transaction events
// This is optional, but useful for debugging or certain patterns
let txbinder = new TxBinder();

// Assign callbacks to transaction events
// Note the "mint" action string is arbitrary, they are used to identify the transaction and execution in txbinder.
txbinder.when("mint", "transactionHash", (tx) =>
console.log("transactionHash: ", tx?.hash)
);
txbinder.when("mint", "transactionReceipt", (receipt) =>
console.log("receipt", receipt)
);
txbinder.when("mint", "error", (err) => console.log("error", err));

// Execute the transaction by providing method with an ethers transaction
await txbinder.execute("mint", () => {
return token.mint(BigInt("10000000000000000000"));
});

// Directly call the implemented contract methods.
let balance = await token.balanceOf(account);

await txbinder.execute("transfer", () => {
return token.transfer(targetAccount, BigInt("10000000000000000000"));
});

// Access any underlying unimplemented contract methods
// Check ethers v6 documentation for more information
let decimals = token.getEthersContract().decimals.staticCall();
let name = token.getEthersContract().name.staticCall();

// Manual contract call

let tx = await token
.getEthersContract()
.transfer.send(targetAccount, BigInt("10000000000000000000"));

// Wait for the transaction to be mined
let receipt = await tx.wait();

// Manual ethers transaction
let tx = await signer.sendTransaction({
to: targetAccount,
value: 0,
data: "0x",
});

// Wait for the transaction to be mined
await tx.wait();
});
}

main(process.argv[2], process.argv[3]);
```

### L1 Browser Client

```typescript
import { withL1BrowserClient, L1BrowserClient } from "../../src/clients/client";

async function BrowserExample() {
await withL1BrowserClient(
{ chainId: 115511, chainName: "sepolia" },
async (l1client: L1BrowserClient) => {
// Connect to the browser provider such as Metamask
// Most actions will be performed by the browser provider

// init will ask the user to switch networks to the provided config network
await l1client.init();

const { connector } = l1client;
// Connector information such as provider, user address, etc.

const address = (await connector.getJsonRpcSigner()).address;
console.log("address: ", address);

// Contract methods can be called by the client
let token = await l1client.getTokenContract();
let proxy = await l1client.getProxyContract();

// The client can call the functions implemented in the contract class
let approval = await token.approve(
await proxy.getEthersContract().getAddress(),
BigInt("10000000000000000000")
);

await approval.wait();

// Directly call the contract method if necessary
let tx = await token.getEthersContract().transfer.send(BigInt(100));

await tx.wait();

console.log("transactionHash: ", tx?.hash);
}
);
}
```
Loading