Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
docs: add example for gasless transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
RetricSu committed Dec 6, 2022
1 parent cd453c1 commit a62201a
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions docs/addtional-feature.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Also notice that under `instant-finality-hack` mode, there might be some [compat

The gas fee is preventing new users step into the web3 world. Users must learn to get the native token(CKB or ETH) before playing a blockchain game or exchanging tokens with a DEX. The gasless feature can provide a way for developers to sponsor transaction fees for users to give them a smooth experience.

The gas feature is based on the ERC-4337 solution but way simpler. To use a such feature, users sign and send a special gasless transaction to call a specific smart contract named `Entrypoint`, then `Entrypoint` will call another smart contract named `Paymaster` deployed by developers to check if they are willing to pay the gas fee for this transaction. The special gasless transaction must satisfy the requirements:
The gas feature is based on the [ERC-4337 solution](https://eips.ethereum.org/EIPS/eip-4337) but way simpler. To use a such feature, users sign and send a special gasless transaction to call a specific smart contract named `Entrypoint`, then `Entrypoint` will call another smart contract named `Paymaster` deployed by developers to check if they are willing to pay the gas fee for this transaction. The special gasless transaction must satisfy the requirements:

- Must contain an `UserOperation` structure on the `tx.data` field, which contains the target contract and the paymaster address.
- Must set `tx.gasPrice` to 0
Expand All @@ -34,7 +34,7 @@ The gas feature is based on the ERC-4337 solution but way simpler. To use a such
```sh
struct UserOperation {
address callContract; # address of the target contract
bytes callData; # call data
bytes callData; # call data of the target contract
uint256 callGasLimit; # gas used to execute the call
uint256 verificationGasLimit; # gas used to verification
uint256 maxFeePerGas; # gas price
Expand All @@ -43,4 +43,40 @@ struct UserOperation {
}
```

More can be found [here](https://github.com/godwokenrises/godwoken/discussions/860#discussion-4568687)
More specs can be found [here](https://github.com/godwokenrises/godwoken/discussions/860#discussion-4568687)

### Example: How to use gasless transaction for your dapp

Let's say we have the entrypoint contract at `0x9a11f47c0729fc56d9c44c059987d40703249569` and as a game developer, we want to pay the gas fee for some whitelist users, so we wrote a paymaster contract just like [this one](https://github.com/godwokenrises/account-abstraction/blob/gw-gasless/contracts/samples/GaslessDemoPaymaster.sol) and deployed it at `0x6b019795aa36dd19eb4a4d76f3b9a40239b7c19f`.

dapp frontend using ethers.js

```ts
// define UserOp
const userOp: UserOperationStruct = {
callContract: realGameContract.address,
callData: realGameContractCallData,
callGasLimit: gasToExecuteRealGameContractCallData,
verificationGasLimit: gasToVerifyPaymaster,
maxFeePerGas: gasPrice,
maxPriorityFeePerGas: gasPrice,
paymasterAndData: "0x6b019795aa36dd19eb4a4d76f3b9a40239b7c19f"
}

// construct and send gasless transaction
const abiCoder = new ethers.utils.AbiCoder();
const payload = abiCoder.encode(["tuple(address callContract, bytes callData, uint256 callGasLimit, uint256 verificationGasLimit, uint256 maxFeePerGas, uint256 maxPriorityFeePerGas, bytes paymasterAndData) UserOperation"], [userOp])

const gaslessTx = {
from: whitelistUser.address,
to: '0x9a11f47c0729fc56d9c44c059987d40703249569',
data: payload,
gasPrice: 0,
gasLimit: 1000000,
value: 0,
}

const signer = new ethers.Wallet("private key");
const tx = await signer.sendTransaction(gaslessTx);
await tx.wait()
```

0 comments on commit a62201a

Please sign in to comment.