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

feat(web3/ethers): support blockTag in eth_call #93

Merged
merged 5 commits into from
Mar 24, 2022
Merged
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
19 changes: 10 additions & 9 deletions .github/workflows/test-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
with:
repository: RetricSu/godwoken-kicker
path: tools/kicker/
ref: master
ref: develop

- name: Cache Kicker cache folder
uses: john-shaffer/cache@sudo-tar
Expand Down Expand Up @@ -95,14 +95,15 @@ jobs:
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: Node Cache
uses: actions/cache@v2
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
# Uncomment after fix: https://github.com/actions/runner/issues/449
# - name: Node Cache
# uses: actions/cache@v2
# id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
# with:
# path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
# key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
# restore-keys: |
# ${{ runner.os }}-yarn-

- name: build and test
run: yarn && yarn build && yarn test
Expand Down
88 changes: 88 additions & 0 deletions contract-testcase/BlockParameter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "BlockParameter",
"sourceName": "contracts/BlockParameter.sol",
"abi": [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "currentBlockNumber",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "currentTimestamp",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "hello",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "lastBlockNumber",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "lastTimestamp",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "setHello",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x608060405234801561001057600080fd5b50600160008190555061017b806100286000396000f3fe608060405234801561001057600080fd5b50600436106100625760003560e01c806319d8ac611461006757806319ff1d21146100855780631e2ff94f146100a35780632552317c146100c1578063378ec23b146100df57806373fde99a146100fd575b600080fd5b61006f610107565b6040518082815260200191505060405180910390f35b61008d61010d565b6040518082815260200191505060405180910390f35b6100ab610113565b6040518082815260200191505060405180910390f35b6100c961011b565b6040518082815260200191505060405180910390f35b6100e7610121565b6040518082815260200191505060405180910390f35b610105610129565b005b60015481565b60005481565b600042905090565b60025481565b600043905090565b600160005401600081905550426001819055504360028190555056fea26469706673582212205444b5478298724ebfa63fd16e47b61b742e245e5e250bde650470368860457664736f6c63430007050033",
"deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100625760003560e01c806319d8ac611461006757806319ff1d21146100855780631e2ff94f146100a35780632552317c146100c1578063378ec23b146100df57806373fde99a146100fd575b600080fd5b61006f610107565b6040518082815260200191505060405180910390f35b61008d61010d565b6040518082815260200191505060405180910390f35b6100ab610113565b6040518082815260200191505060405180910390f35b6100c961011b565b6040518082815260200191505060405180910390f35b6100e7610121565b6040518082815260200191505060405180910390f35b610105610129565b005b60015481565b60005481565b600042905090565b60025481565b600043905090565b600160005401600081905550426001819055504360028190555056fea26469706673582212205444b5478298724ebfa63fd16e47b61b742e245e5e250bde650470368860457664736f6c63430007050033",
"linkReferences": {},
"deployedLinkReferences": {}
}
26 changes: 26 additions & 0 deletions contract-testcase/BlockParameter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity >0.7.0 <0.8.0;

contract BlockParameter {
uint256 public hello;
uint256 public lastTimestamp;
uint256 public lastBlockNumber;

constructor() public {
hello = 1;
}

function setHello() public {
hello = hello + 1;
lastTimestamp = block.timestamp;
lastBlockNumber = block.number;
}

function currentTimestamp() public view returns (uint256) {
return block.timestamp;
}

function currentBlockNumber() public view returns (uint256) {
return block.number;
}
}
75 changes: 75 additions & 0 deletions contract-testcase/BlockTimestamp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "BlockTimestamp",
"sourceName": "contracts/BlockTimestamp.sol",
"abi": [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "getDiffTime",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "hello",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "lastBlockNumber",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "lastTimestamp",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "setHello",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x608060405234801561001057600080fd5b50600160008190555061026d806100286000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c806319d8ac611461005c57806319ff1d211461007a5780632552317c1461009857806373fde99a146100b6578063cd594a1c146100c0575b600080fd5b6100646100de565b6040518082815260200191505060405180910390f35b6100826100e4565b6040518082815260200191505060405180910390f35b6100a06100ea565b6040518082815260200191505060405180910390f35b6100be6100f0565b005b6100c861010c565b6040518082815260200191505060405180910390f35b60015481565b60005481565b60025481565b6001600054016000819055504260018190555043600281905550565b6000806101246001544261012d90919063ffffffff16565b90508091505090565b600061016f83836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f770000815250610177565b905092915050565b6000838311158290610224576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b838110156101e95780820151818401526020810190506101ce565b50505050905090810190601f1680156102165780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506000838503905080915050939250505056fea2646970667358221220a9ff081e0af2c949e0b51eb27b088f2d01a25ff5c29d8cb87024451f82a3a4f464736f6c63430007050033",
"deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100575760003560e01c806319d8ac611461005c57806319ff1d211461007a5780632552317c1461009857806373fde99a146100b6578063cd594a1c146100c0575b600080fd5b6100646100de565b6040518082815260200191505060405180910390f35b6100826100e4565b6040518082815260200191505060405180910390f35b6100a06100ea565b6040518082815260200191505060405180910390f35b6100be6100f0565b005b6100c861010c565b6040518082815260200191505060405180910390f35b60015481565b60005481565b60025481565b6001600054016000819055504260018190555043600281905550565b6000806101246001544261012d90919063ffffffff16565b90508091505090565b600061016f83836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f770000815250610177565b905092915050565b6000838311158290610224576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b838110156101e95780820151818401526020810190506101ce565b50505050905090810190601f1680156102165780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506000838503905080915050939250505056fea2646970667358221220a9ff081e0af2c949e0b51eb27b088f2d01a25ff5c29d8cb87024451f82a3a4f464736f6c63430007050033",
"linkReferences": {},
"deployedLinkReferences": {}
}
91 changes: 91 additions & 0 deletions contract-testcase/BlockTimestamp.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-License-Identifier: MIT
pragma solidity >0.7.0 <0.8.0;

library SafeMath {

function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");

return c;
}

function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}

function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;

return c;
}

function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}

uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");

return c;
}

function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}

function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
return c;
}

function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}

function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}

function sqrrt(uint256 a) internal pure returns (uint c) {
if (a > 3) {
c = a;
uint b = add( div( a, 2), 1 );
while (b < c) {
c = b;
b = div( add( div( a, b ), b), 2 );
}
} else if (a != 0) {
c = 1;
}
}
}


contract BlockTimestamp {

using SafeMath for uint;

uint public hello;
uint public lastTimestamp;
uint public lastBlockNumber;

constructor() public {
hello = 1;
}

function setHello() public {
hello = hello + 1;
lastTimestamp = block.timestamp;
lastBlockNumber = block.number;
}

function getDiffTime() public view returns (uint) {
uint diff = block.timestamp.sub(lastTimestamp);
return diff;
}
}
19 changes: 14 additions & 5 deletions packages/base/src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
DeploymentRecords,
} from "./util";
import { serializeAbiItem } from "./abi";
import { SigningMessageType } from "./types";
import { BlockParameter, SigningMessageType } from "./types";
import {
EMPTY_ABI_ITEM_SERIALIZE_STR,
DEFAULT_EMPTY_ETH_ADDRESS,
Expand Down Expand Up @@ -59,12 +59,19 @@ export async function buildSendTransaction(
export async function executeCallTransaction(
abi: Abi,
godwoker: Godwoker,
tx: EthTransaction
tx: EthTransaction,
blockParameter?: BlockParameter
): Promise<string> {
const process: Process = {
type: ProcessTransactionType.call,
};
const callReturnData = await buildProcess(abi, godwoker, tx, process);
const callReturnData = await buildProcess(
abi,
godwoker,
tx,
process,
blockParameter
);
if (typeof callReturnData !== "string")
throw new Error("execute callTransaction end up with non-string return!");

Expand Down Expand Up @@ -183,7 +190,8 @@ export async function buildProcess(
abi: Abi,
godwoker: Godwoker,
tx: EthTransaction,
process: Process
process: Process,
blockParameter?: BlockParameter
): Promise<HexString | RawL2Transaction> {
if (!tx.from && process.type === ProcessTransactionType.send) {
throw new Error("tx.from can not be missing in sendTransaction!");
Expand Down Expand Up @@ -274,7 +282,8 @@ export async function buildProcess(
serializedAbiItem
);
const run_result = await godwoker.poly_executeRawL2Transaction(
polyRawL2Tx
polyRawL2Tx,
blockParameter
);

const abi_item = abi.get_interested_abi_item_by_encoded_data(tx.data);
Expand Down
3 changes: 3 additions & 0 deletions packages/base/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export enum SigningMessageType {
noPrefix,
}

export type BlockTag = "latest" | "earliest" | "pending";
export type BlockParameter = HexString | BlockTag;

export enum FailedReasonStatusType {
"SUCCESS",
"FAILURE",
Expand Down
Loading