Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work object header #106

Merged
merged 20 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@
"eslint"
]
}
}
}
4 changes: 2 additions & 2 deletions src.ts/_tests/test-contract-integ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("Tests contract integration", function() {
before(async function() {
this.timeout(100000);

const factory = new quais.ContractFactory(abi, bytecode, wallet);
const factory = new quais.ContractFactory(abi, bytecode, wallet as quais.ContractRunner);
contract = await factory.deploy(constructorArgs.name, constructorArgs.symbol, constructorArgs.totalSupply, {
gasLimit: 5000000 }) as Contract;
address = await contract.getAddress();
Expand Down Expand Up @@ -66,7 +66,7 @@ describe("Tests contract integration", function() {

const CustomContract = quais.BaseContract.buildClass<ContractAbi>(abi);

const contract = new CustomContract(address, wallet); //quais.Contract.from<ContractAbi>(address, abi, signer);
const contract = new CustomContract(address, wallet as quais.ContractRunner ); //quais.Contract.from<ContractAbi>(address, abi, signer);
await stall(30000);
// Test implicit staticCall (i.e. view/pure)
{
Expand Down
6 changes: 3 additions & 3 deletions src.ts/_tests/test-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert from "assert";
import { getProvider, setupProviders } from "./create-provider.js";

import {
Contract, ContractFactory, isError, Typed,
Contract, ContractFactory, ContractRunner, isError, Typed,
} from "../index.js";
import TestContract from "./contracts/TestContract.js"
import TypedContract from "./contracts/TypedContract.js"
Expand All @@ -23,7 +23,7 @@ describe("Test Contract", function() {
before( async function () {
this.timeout(60000);
await stall(10000);
const factory = new ContractFactory(abi, bytecode, wallet);
const factory = new ContractFactory(abi, bytecode, wallet as ContractRunner);
contract = await factory.deploy({gasLimit: 5000000, maxFeePerGas: quais.parseUnits('10', 'gwei'), maxPriorityFeePerGas: quais.parseUnits('3', 'gwei')}) as Contract;
addr = await contract.getAddress();
console.log("Contract deployed to: ", addr);
Expand Down Expand Up @@ -254,7 +254,7 @@ describe("Test Typed Contract Interaction", function() {
let addr: string
before( async function () {
this.timeout(120000);
const factory = new ContractFactory(abi, bytecode, wallet);
const factory = new ContractFactory(abi, bytecode, wallet as ContractRunner);
contract = await factory.deploy({gasLimit: 5000000, maxFeePerGas: quais.parseUnits('10', 'gwei'), maxPriorityFeePerGas: quais.parseUnits('3', 'gwei'),}) as Contract;
addr = await contract.getAddress();
console.log("Contract deployed to: ", addr);
Expand Down
7 changes: 4 additions & 3 deletions src.ts/_tests/test-provider-jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import assert from "assert";
import {
id, isError, makeError, toUtf8Bytes, toUtf8String,
FetchRequest,
JsonRpcProvider, Transaction, Wallet
JsonRpcProvider, Wallet
} from "../index.js";
import { QuaiTransaction } from "../transaction/quai-transaction.js";

const StatusMessages: Record<number, string> = {
200: "OK",
Expand Down Expand Up @@ -93,7 +94,7 @@ describe("Ensure Catchable Errors", function() {
value: 1,
};
const txSign = await wallet.signTransaction(txInfo);
const txObj = Transaction.from(txSign);
const txObj = QuaiTransaction.from(txSign);

let count = 0;

Expand Down Expand Up @@ -155,7 +156,7 @@ describe("Ensure Catchable Errors", function() {
value: 1,
};
const txSign = await wallet.signTransaction(txInfo);
const txObj = Transaction.from(txSign);
const txObj = QuaiTransaction.from(txSign);

let count = 0;

Expand Down
1 change: 0 additions & 1 deletion src.ts/_tests/test-providers-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ describe("Test Provider Block operations", function() {
parentHash: rpcBlock.parentHash,
parentEntropy: rpcBlock.parentEntropy.map((entropy: string) => BigInt(entropy)),
extTransactions: rpcBlock.extTransactions,
timestamp: Number(rpcBlock.timestamp),
nonce: rpcBlock.nonce,
difficulty: BigInt(rpcBlock.difficulty),
gasLimit: BigInt(rpcBlock.gasLimit),
Expand Down
9 changes: 4 additions & 5 deletions src.ts/_tests/test-providers-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import {
import { getProvider, setupProviders, providerNames } from "./create-provider.js";
import { stall } from "./utils.js";

import type { TransactionResponse } from "../index.js";
//require('dotenv').config();
import dotenv from "dotenv";
import { QuaiTransactionResponse } from "../providers/provider.js";
dotenv.config();

type TestCustomError = {
Expand Down Expand Up @@ -187,14 +186,14 @@ describe("Test Provider Blockchain Errors", function() {

const w = wallet.connect(provider);

let tx1: null | TransactionResponse = null;
let tx1: null | QuaiTransactionResponse = null;
let nonce: null | number = null;;
for (let i = 0; i < 10; i++) {
nonce = await w.getNonce("pending");
try {
tx1 = await w.sendTransaction({
nonce, to: wallet, from: wallet2, value
});
}) as QuaiTransactionResponse;
} catch (error: any) {
// Another CI host beat us to this nonce
if (isError(error, "REPLACEMENT_UNDERPRICED") || isError(error, "NONCE_EXPIRED")) {
Expand Down Expand Up @@ -244,7 +243,7 @@ describe("Test Provider Blockchain Errors", function() {
console.log(tx);
}, (error) => {
return (isError(error, "INSUFFICIENT_FUNDS") &&
typeof(error.transaction.from) === "string" &&
"from" in error.transaction && typeof(error.transaction.from) === "string" &&
error.transaction.from.toLowerCase() === w.address.toLowerCase());
});
});
Expand Down
15 changes: 8 additions & 7 deletions src.ts/_tests/test-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import assert from "assert";
import { loadTests } from "./utils.js";
import type { TestCaseTransaction, TestCaseTransactionTx } from "./types.js";

import { isError, Transaction } from "../index.js";
import {isError} from "../index.js";
import {QuaiTransaction} from "../transaction/quai-transaction";


const BN_0 = BigInt(0);
Expand All @@ -20,7 +21,7 @@ describe("Tests Unsigned Transaction Serializing", function() {
maxFeePerGas: undefined,
maxPriorityFeePerGas: undefined
});
const tx = Transaction.from(txData);
const tx = QuaiTransaction.from(txData);
assert.equal(tx.unsignedSerialized, test.unsignedEip155, "unsignedEip155");
});
}
Expand All @@ -39,7 +40,7 @@ describe("Tests Signed Transaction Serializing", function() {
maxPriorityFeePerGas: 0,
signature: test.signatureEip155
});
const tx = Transaction.from(txData);
const tx = QuaiTransaction.from(txData);
assert.equal(tx.serialized, test.signedEip155, "signedEip155");
});
}
Expand All @@ -50,7 +51,7 @@ function assertTxUint(actual: null | bigint, _expected: undefined | string, name
assert.equal(actual, expected, name);
}

function assertTxEqual(actual: Transaction, expected: TestCaseTransactionTx): void {
function assertTxEqual(actual: QuaiTransaction, expected: TestCaseTransactionTx): void {
assert.equal(actual.to, expected.to, "to");
assert.equal(actual.nonce, expected.nonce, "nonce");

Expand Down Expand Up @@ -95,7 +96,7 @@ describe("Tests Unsigned Transaction Parsing", function() {
for (const test of tests) {
if (!test.unsignedEip155) { continue; }
it(`parses unsigned EIP-155 transaction: ${ test.name }`, function() {
const tx = Transaction.from(test.unsignedEip155);
const tx = QuaiTransaction.from(test.unsignedEip155);

const expected = addDefaults(test.transaction);
expected.maxFeePerGas = 0;
Expand All @@ -112,7 +113,7 @@ describe("Tests Signed Transaction Parsing", function() {
for (const test of tests) {
if (!test.unsignedEip155) { continue; }
it(`parses signed EIP-155 transaction: ${ test.name }`, function() {
let tx = Transaction.from(test.signedEip155);
let tx = QuaiTransaction.from(test.signedEip155);
const expected = addDefaults(test.transaction);
expected.maxFeePerGas = 0;
expected.maxPriorityFeePerGas = 0;
Expand Down Expand Up @@ -169,7 +170,7 @@ describe("Tests Transaction Parameters", function() {
assert.throws(() => {
// The access list is a single value: 0x09 instead of
// structured data
const result = Transaction.from(data);
const result = QuaiTransaction.from(data);
console.log(result)
}, (error: any) => {
return (isError(error, "INVALID_ARGUMENT") &&
Expand Down
2 changes: 1 addition & 1 deletion src.ts/abi/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class Interface {
try {
frags.push(Fragment.from(a));
} catch (error) {
console.log("EE", error);
console.log("Error parsing ABI fragment", error);
}
}

Expand Down
20 changes: 9 additions & 11 deletions src.ts/address/contract-address.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { keccak256 } from "../crypto/index.js";
import {
concat, dataSlice, getBigInt, getBytes, assertArgument, encodeProto
concat,
dataSlice,
getBigInt,
getBytes,
assertArgument,
zeroPadValue,
toBeHex, toBigInt
} from "../utils/index.js";

import { getAddress } from "./address.js";
Expand Down Expand Up @@ -31,17 +37,9 @@ import type { BigNumberish, BytesLike } from "../utils/index.js";
export function getCreateAddress(tx: { from: string, nonce: BigNumberish }): string {
const from = getAddress(tx.from);
const nonce = getBigInt(tx.nonce, "tx.nonce");
const nonceBytes = zeroPadValue(toBeHex(toBigInt(nonce)), 8);

let nonceHex = nonce.toString(16);
if (nonceHex === "0") {
nonceHex = "0x";
} else if (nonceHex.length % 2) {
nonceHex = "0x0" + nonceHex;
} else {
nonceHex = "0x" + nonceHex;
}

return getAddress(dataSlice(keccak256(encodeProto([ from, nonceHex ])), 12));
return getAddress(dataSlice(keccak256(concat([getAddress(from), nonceBytes ])), 12))
}

/**
Expand Down
34 changes: 20 additions & 14 deletions src.ts/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { Interface, Typed } from "../abi/index.js";
import { isAddressable, resolveAddress } from "../address/index.js";
// import from provider.ts instead of index.ts to prevent circular dep
// from quaiscanProvider
import { copyRequest, Log, TransactionResponse } from "../providers/provider.js";
import {
copyRequest,
Log,
QuaiTransactionRequest,
QuaiTransactionResponse,
TransactionResponse,
} from "../providers/provider.js";
import {
defineProperties, getBigInt, isCallException, isHexString, resolveProperties,
isError, assert, assertArgument
Expand Down Expand Up @@ -129,13 +135,13 @@ export async function copyOverrides<O extends string = "data" | "to">(arg: any,
// Create a shallow copy (we'll deep-ify anything needed during normalizing)
const overrides = copyRequest(_overrides);

assertArgument(overrides.to == null || (allowed || [ ]).indexOf("to") >= 0,
"cannot override to", "overrides.to", overrides.to);
assertArgument(overrides.data == null || (allowed || [ ]).indexOf("data") >= 0,
"cannot override data", "overrides.data", overrides.data);
assertArgument((! ("to" in overrides) || overrides.to == null) || (allowed || [ ]).indexOf("to") >= 0,
"cannot override to", "overrides.to", overrides);
assertArgument((! ("data" in overrides) || overrides.data == null) || (allowed || [ ]).indexOf("data") >= 0,
"cannot override data", "overrides.data", overrides);

// Resolve any from
if (overrides.from) { overrides.from = overrides.from; }
if ("from" in overrides && overrides.from) { overrides.from = await overrides.from; }

return <Omit<ContractTransaction, O>>overrides;
}
Expand All @@ -156,7 +162,7 @@ export async function resolveArgs(_runner: null | ContractRunner, inputs: Readon

function buildWrappedFallback(contract: BaseContract): WrappedFallback {

const populateTransaction = async function(overrides?: Omit<TransactionRequest, "to">): Promise<ContractTransaction> {
const populateTransaction = async function(overrides?: Omit<QuaiTransactionRequest, "to">): Promise<ContractTransaction> {
// If an overrides was passed in, copy it and normalize the values

const tx: ContractTransaction = <any>(await copyOverrides<"data">(overrides, [ "data" ]));
Expand Down Expand Up @@ -190,7 +196,7 @@ function buildWrappedFallback(contract: BaseContract): WrappedFallback {
return tx;
}

const staticCall = async function(overrides?: Omit<TransactionRequest, "to">): Promise<string> {
const staticCall = async function(overrides?: Omit<QuaiTransactionRequest, "to">): Promise<string> {
const runner = getRunner(contract.runner, "call");
assert(canCall(runner), "contract runner does not support calling",
"UNSUPPORTED_OPERATION", { operation: "call" });
Expand All @@ -207,27 +213,27 @@ function buildWrappedFallback(contract: BaseContract): WrappedFallback {
}
}

const send = async function(overrides?: Omit<TransactionRequest, "to">): Promise<ContractTransactionResponse> {
const send = async function(overrides?: Omit<QuaiTransactionRequest, "to">): Promise<ContractTransactionResponse> {
const runner = contract.runner;
assert(canSend(runner), "contract runner does not support sending transactions",
"UNSUPPORTED_OPERATION", { operation: "sendTransaction" });

const tx = await runner.sendTransaction(await populateTransaction(overrides));
const tx = await runner.sendTransaction(await populateTransaction(overrides)) as QuaiTransactionResponse;
const provider = getProvider(contract.runner);
// @TODO: the provider can be null; make a custom dummy provider that will throw a
// meaningful error
return new ContractTransactionResponse(contract.interface, <Provider>provider, tx);
}

const estimateGas = async function(overrides?: Omit<TransactionRequest, "to">): Promise<bigint> {
const estimateGas = async function(overrides?: Omit<QuaiTransactionRequest, "to">): Promise<bigint> {
const runner = getRunner(contract.runner, "estimateGas");
assert(canEstimate(runner), "contract runner does not support gas estimation",
"UNSUPPORTED_OPERATION", { operation: "estimateGas" });

return await runner.estimateGas(await populateTransaction(overrides));
}

const method = async (overrides?: Omit<TransactionRequest, "to">) => {
const method = async (overrides?: Omit<QuaiTransactionRequest, "to">) => {
return await send(overrides);
};

Expand Down Expand Up @@ -297,7 +303,7 @@ function buildWrappedMethod<A extends Array<any> = Array<any>, R = any, D extend
assert(canSend(runner), "contract runner does not support sending transactions",
"UNSUPPORTED_OPERATION", { operation: "sendTransaction" });

const tx = await runner.sendTransaction(await populateTransaction(...args));
const tx = await runner.sendTransaction(await populateTransaction(...args)) as QuaiTransactionResponse;
const provider = getProvider(contract.runner);
// @TODO: the provider can be null; make a custom dummy provider that will throw a
// meaningful error
Expand Down Expand Up @@ -669,7 +675,7 @@ export class BaseContract implements Addressable, EventEmitterable<ContractEvent
* optionally connected to a %%runner%% to perform operations on behalf
* of.
*/
constructor(target: string | Addressable, abi: Interface | InterfaceAbi, runner?: null | ContractRunner, _deployTx?: null | TransactionResponse) {
constructor(target: string | Addressable, abi: Interface | InterfaceAbi, runner?: null | ContractRunner, _deployTx?: null | QuaiTransactionResponse) {
assertArgument(typeof(target) === "string" || isAddressable(target),
"invalid value for Contract target", "target", target);

Expand Down
7 changes: 4 additions & 3 deletions src.ts/contract/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BaseContract, copyOverrides, resolveArgs } from "./contract.js";

import type { InterfaceAbi } from "../abi/index.js";
import type { Addressable } from "../address/index.js";
import type { ContractRunner, TransactionRequest } from "../providers/index.js";
import type { ContractRunner } from "../providers/index.js";
import type { BytesLike } from "../utils/index.js";
import { getShardForAddress, isUTXOAddress } from "../utils/index.js";
import type {
Expand All @@ -19,6 +19,7 @@ import type { ContractTransactionResponse } from "./wrappers.js";
import { Wallet, randomBytes } from "../quais.js";
import { getContractAddress } from "../address/address.js";
import { getStatic } from "../utils/properties.js";
import {QuaiTransactionRequest} from "../providers/provider";


// A = Arguments to the constructor
Expand Down Expand Up @@ -195,8 +196,8 @@ static getContractAddress(transaction: {
}

async grindContractAddress(
tx: TransactionRequest
): Promise<TransactionRequest> {
tx: QuaiTransactionRequest
): Promise<QuaiTransactionRequest> {
if (tx.nonce == null && tx.from) {
tx.nonce = await this.runner?.provider?.getTransactionCount(tx.from);
}
Expand Down
5 changes: 3 additions & 2 deletions src.ts/contract/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import type {
EventFragment, FunctionFragment, Result, Typed
} from "../abi/index.js";
import type {
TransactionRequest, PreparedTransactionRequest, TopicFilter
TransactionRequest, TopicFilter
} from "../providers/index.js";

import type { ContractTransactionResponse } from "./wrappers.js";
import {QuaiPreparedTransactionRequest} from "../providers/provider";


/**
Expand Down Expand Up @@ -48,7 +49,7 @@ export interface DeferredTopicFilter {
/**
* When populating a transaction this type is returned.
*/
export interface ContractTransaction extends PreparedTransactionRequest {
export interface ContractTransaction extends QuaiPreparedTransactionRequest {
/**
* The target address.
*/
Expand Down
Loading