Skip to content

Commit

Permalink
src typescript updated to ethers v6
Browse files Browse the repository at this point in the history
  • Loading branch information
joewagner committed Mar 14, 2023
1 parent 23b3f9c commit b9a4364
Show file tree
Hide file tree
Showing 14 changed files with 520 additions and 176 deletions.
511 changes: 411 additions & 100 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@
"dependencies": {
"@tableland/evm": "^4.0.0",
"@tableland/sqlparser": "^1.0.4",
"ethers": "^5.7.2"
"ethers": "^6.1.0"
}
}
35 changes: 18 additions & 17 deletions src/helpers/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ChainName, getBaseUrl } from "./chains.js";
import { type Signer, type ExternalProvider, getSigner } from "./ethers.js";
import { type Signer, type Eip1193Provider, getSigner } from "./ethers.js";

export interface ReadConfig {
baseUrl: string;
Expand All @@ -19,24 +19,25 @@ export async function extractBaseUrl(
conn: Config = {},
chainNameOrId?: ChainName | number
): Promise<string> {
if (conn.baseUrl == null) {
if (conn.signer == null) {
if (chainNameOrId == null) {
throw new Error(
"missing connection information: baseUrl, signer, or chainId required"
);
}
return getBaseUrl(chainNameOrId);
}
const chainId = await conn.signer.getChainId();
return getBaseUrl(chainId);
if (conn.baseUrl != null) return conn.baseUrl;

const network = await conn.signer?.provider?.getNetwork();
if (network != null) {
return getBaseUrl(Number(network.chainId));
}

if (chainNameOrId != null) {
return getBaseUrl(chainNameOrId);
}
return conn.baseUrl;

throw new Error(
"missing connection information: baseUrl, signer, or chainId required"
);
}

export async function extractSigner(
conn: Config = {},
external?: ExternalProvider
external?: Eip1193Provider
): Promise<Signer> {
if (conn.signer == null) {
return await getSigner(external);
Expand All @@ -46,14 +47,14 @@ export async function extractSigner(

export async function extractChainId(conn: Config = {}): Promise<number> {
const signer = await extractSigner(conn);
const chainId = await signer.getChainId();
const network = await signer.provider?.getNetwork();

if (chainId === 0 || isNaN(chainId) || chainId == null) {
if (network == null || network.chainId == BigInt("0") || network.chainId == null) {
/* c8 ignore next 4 */
throw new Error(
"cannot find chainId: is your signer connected to a network?"
);
}

return chainId;
return Number(network.chainId);
}
58 changes: 34 additions & 24 deletions src/helpers/ethers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {
providers,
getDefaultProvider,
BrowserProvider,
EventLog,
type Eip1193Provider,
type ContractTransactionResponse,
type ContractTransactionReceipt,
type Signer,
type Overrides,
type ContractTransaction,
type ContractReceipt,
} from "ethers";
import { type TransactionReceipt } from "../validator/receipt.js";
import { type SignerConfig } from "./config.js";

type ExternalProvider = providers.ExternalProvider;
const { getDefaultProvider, Web3Provider } = providers;

// eslint-disable-next-line @typescript-eslint/no-namespace
declare module globalThis {
// eslint-disable-next-line no-var
var ethereum: ExternalProvider | undefined;
var ethereum: Eip1193Provider | undefined;
}

/**
Expand All @@ -29,11 +29,13 @@ export async function getOverrides({
const opts: Overrides = {};
const network = await signer.provider?.getNetwork();
/* c8 ignore next 7 */
if (network?.chainId === 137) {
const feeData = await signer.getFeeData();
if (feeData.gasPrice != null) {
if (network?.chainId === BigInt("137")) {
const feeData = await signer.provider?.getFeeData();
if (feeData?.gasPrice != null) {
opts.gasPrice =
Math.floor(feeData.gasPrice.toNumber() * 1.1) ?? undefined;
// NOTE: There's no guarantee `feeData.gasPrice` is a JS safe integer which means
// this might not be accurate, but it's just an estimate so this is probably ok
Math.floor(Number(feeData.gasPrice) * 1.1) ?? undefined;
}
}
return opts;
Expand All @@ -44,20 +46,28 @@ export type RegistryReceipt = Required<
>;

export async function getContractReceipt(
tx: ContractTransaction
tx: ContractTransactionResponse
): Promise<RegistryReceipt> {
const receipt = await tx.wait();

if (receipt == null) {
throw new Error(`could not get receipt for transaction: ${tx}`);
}

/* c8 ignore next */
const events = receipt.events ?? [];
const transactionHash = receipt.transactionHash;
const logs = receipt.logs ?? [];
const transactionHash = receipt.hash;
const blockNumber = receipt.blockNumber;
const chainId = tx.chainId;
// NOTE: chainId is always a JS safe integer
const chainId = Number(tx.chainId);
let tableId: string = "";
for (const event of events) {
switch (event.event) {
for (const log of logs) {
if (!(log instanceof EventLog)) continue;

switch (log.eventName) {
case "CreateTable":
case "RunSQL":
tableId = event.args?.tableId.toString();
tableId = log.args?.tableId.toString();
break;
default:
// Could be a Transfer or other
Expand All @@ -76,7 +86,7 @@ export async function getContractReceipt(
* @returns A promise that resolves to a valid web3 provider/signer
* @throws If no global ethereum object is available.
*/
export async function getSigner(external?: ExternalProvider): Promise<Signer> {
export async function getSigner(external?: Eip1193Provider): Promise<Signer> {
const provider = external ?? globalThis.ethereum;
if (provider == null) {
throw new Error("provider error: missing global ethereum provider");
Expand All @@ -87,14 +97,14 @@ export async function getSigner(external?: ExternalProvider): Promise<Signer> {
);
}
await provider.request({ method: "eth_requestAccounts" });
const web3Provider = new Web3Provider(provider);
const web3Provider = new BrowserProvider(provider);
return web3Provider.getSigner();
}

export {
Signer,
getDefaultProvider,
type ExternalProvider,
type ContractTransaction,
type ContractReceipt,
type Signer,
type ContractTransactionResponse,
type ContractTransactionReceipt,
type Eip1193Provider,
};
6 changes: 3 additions & 3 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export {
} from "./config.js";
export {
type Signer,
type ExternalProvider,
type Eip1193Provider,
getDefaultProvider,
type ContractTransaction,
type ContractReceipt,
type ContractTransactionResponse,
type ContractTransactionReceipt,
type RegistryReceipt,
getSigner,
} from "./ethers.js";
Expand Down
3 changes: 2 additions & 1 deletion src/lowlevel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type Config,
extractBaseUrl,
extractChainId,
extractSigner,
type Signal,
type ReadConfig,
Expand All @@ -24,7 +25,7 @@ export async function exec(
{ type, sql, tables: [first] }: ExtractedStatement
): Promise<WaitableTransactionReceipt> {
const signer = await extractSigner(config);
const chainId = await signer.getChainId();
const chainId = await extractChainId(config);
const baseUrl = await extractBaseUrl(config, chainId);
const _config = { baseUrl, signer };
const _params = { chainId, first, statement: sql };
Expand Down
3 changes: 2 additions & 1 deletion src/registry/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
TablelandTables__factory as Factory,
} from "@tableland/evm";
import type { Overrides } from "ethers";
// TODO: type Signer has changed.
import { getOverrides, type Signer } from "../helpers/ethers.js";
import { validateTableName } from "../helpers/parser.js";
import { getContractAddress } from "../helpers/chains.js";
Expand Down Expand Up @@ -38,7 +39,7 @@ export async function getContractAndOverrides(
chainId: number
): Promise<{ contract: TablelandTables; overrides: Overrides }> {
const address = getContractAddress(chainId);
signer._checkProvider();

const contract = connect(address, signer);
const overrides = await getOverrides({ signer });
return { contract, overrides };
Expand Down
22 changes: 16 additions & 6 deletions src/registry/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Typed } from "ethers";
import { type SignerConfig } from "../helpers/config.js";
import { type ContractTransaction } from "../helpers/ethers.js";
import { type ContractTransactionResponse } from "../helpers/ethers.js";
import { type TableIdentifier, getContractSetup } from "./contract.js";

export interface SetParams {
Expand All @@ -16,26 +17,35 @@ export interface SetParams {
export async function setController(
{ signer }: SignerConfig,
params: SetParams
): Promise<ContractTransaction> {
): Promise<ContractTransactionResponse> {
const { contract, overrides, tableId } = await getContractSetup(
signer,
params.tableName
);
const caller = await signer.getAddress();
const controller = params.controller;
return await contract.setController(caller, tableId, controller, overrides);
return await contract.setController(
Typed.address(caller),
Typed.uint256(tableId),
Typed.address(controller),
overrides,
);
}

export async function lockController(
{ signer }: SignerConfig,
tableName: string | TableIdentifier
): Promise<ContractTransaction> {
): Promise<ContractTransactionResponse> {
const { contract, overrides, tableId } = await getContractSetup(
signer,
tableName
);
const caller = await signer.getAddress();
return await contract.lockController(caller, tableId, overrides);
return await contract.lockController(
Typed.address(caller),
Typed.uint256(tableId),
overrides,
);
}

export async function getController(
Expand All @@ -46,5 +56,5 @@ export async function getController(
signer,
tableName
);
return await contract.getController(tableId, overrides);
return await contract.getController(Typed.uint256(tableId), overrides);
}
3 changes: 2 additions & 1 deletion src/registry/create.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Typed } from "ethers";
import { type SignerConfig } from "../helpers/config.js";
import { type ContractTransaction } from "../helpers/ethers.js";
import { validateTableName } from "../helpers/parser.js";
Expand Down Expand Up @@ -63,5 +64,5 @@ export async function createTable(
signer,
chainId
);
return await contract.createTable(owner, statement, overrides);
return await contract.createTable(Typed.address(owner), Typed.string(statement), overrides);
}
12 changes: 6 additions & 6 deletions src/registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
type SignerConfig,
type Signer,
type ContractTransaction,
type ContractTransactionResponse,
} from "../helpers/index.js";
import { type TableIdentifier } from "./contract.js";
import { listTables } from "./tables.js";
Expand Down Expand Up @@ -70,7 +70,7 @@ export class Registry {
*
* Requires the msg sender to be the owner, approved, or operator
*/
async safeTransferFrom(params: TransferParams): Promise<ContractTransaction> {
async safeTransferFrom(params: TransferParams): Promise<ContractTransactionResponse> {
return await safeTransferFrom(this.config, params);
}

Expand Down Expand Up @@ -98,7 +98,7 @@ export class Registry {
* - `tableId` must exist
* - `tableId` controller must not be locked
*/
async setController(params: SetParams): Promise<ContractTransaction> {
async setController(params: SetParams): Promise<ContractTransactionResponse> {
return await setController(this.config, params);
}

Expand All @@ -119,7 +119,7 @@ export class Registry {
*/
async lockController(
table: string | TableIdentifier
): Promise<ContractTransaction> {
): Promise<ContractTransactionResponse> {
return await lockController(this.config, table);
}

Expand All @@ -142,7 +142,7 @@ export class Registry {
*
* - contract must be unpaused
*/
async createTable(params: CreateTableParams): Promise<ContractTransaction> {
async createTable(params: CreateTableParams): Promise<ContractTransactionResponse> {
return await createTable(this.config, params);
}

Expand All @@ -161,7 +161,7 @@ export class Registry {
* - `caller` must be authorized by the table controller
* - `statement` must be less than or equal to 35000 bytes
*/
async runSQL(params: RunSQLParams): Promise<ContractTransaction> {
async runSQL(params: RunSQLParams): Promise<ContractTransactionResponse> {
return await runSQL(this.config, params);
}
}
12 changes: 9 additions & 3 deletions src/registry/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Typed } from "ethers";
import { type SignerConfig } from "../helpers/config.js";
import { type ContractTransaction } from "../helpers/ethers.js";
import { type ContractTransactionResponse } from "../helpers/ethers.js";
import { validateTableName } from "../helpers/parser.js";
import {
getContractAndOverrides,
Expand Down Expand Up @@ -42,11 +43,16 @@ export interface RunSQLParams extends TableIdentifier {
export async function runSQL(
{ signer }: SignerConfig,
{ statement, tableId, chainId }: RunSQLParams
): Promise<ContractTransaction> {
): Promise<ContractTransactionResponse> {
const caller = await signer.getAddress();
const { contract, overrides } = await getContractAndOverrides(
signer,
chainId
);
return await contract.runSQL(caller, tableId, statement, overrides);
return await contract.runSQL(
/*Typed.address(caller)*/caller,
/*Typed.uint256(tableId)*/tableId,
/*Typed.string(statement)*/statement,
overrides
);
}
Loading

0 comments on commit b9a4364

Please sign in to comment.