Skip to content

Commit

Permalink
wip: tsc builds src with ethers v6
Browse files Browse the repository at this point in the history
  • Loading branch information
joewagner committed Mar 9, 2023
1 parent cbc0a2a commit ba82f92
Show file tree
Hide file tree
Showing 12 changed files with 493 additions and 155 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);
}
56 changes: 33 additions & 23 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 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
1 change: 1 addition & 0 deletions 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
16 changes: 13 additions & 3 deletions src/registry/controller.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 { type TableIdentifier, getContractSetup } from "./contract.js";
Expand All @@ -23,7 +24,12 @@ export async function setController(
);
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(
Expand All @@ -35,7 +41,11 @@ export async function lockController(
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);
}
3 changes: 2 additions & 1 deletion src/registry/run.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 @@ -48,5 +49,5 @@ export async function runSQL(
signer,
chainId
);
return await contract.runSQL(caller, tableId, statement, overrides);
return await contract.runSQL(Typed.address(caller), Typed.uint256(tableId), Typed.string(statement), overrides);
}
3 changes: 2 additions & 1 deletion src/registry/tables.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 TableIdentifier, getContractAndOverrides } from "./contract.js";

Expand All @@ -12,6 +13,6 @@ export async function listTables(
signer,
chainId
);
const tokens = await contract.tokensOfOwner(address, overrides);
const tokens = await contract.tokensOfOwner(Typed.address(address), overrides);
return tokens.map((token) => ({ tableId: token.toString(), chainId }));
}
9 changes: 5 additions & 4 deletions src/registry/transfer.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 { type TableIdentifier, getContractSetup } from "./contract.js";
Expand All @@ -22,10 +23,10 @@ export async function safeTransferFrom(
params.tableName
);
const caller = await signer.getAddress();
return await contract["safeTransferFrom(address,address,uint256)"](
caller,
params.to,
tableId,
return await contract.safeTransferFrom(
Typed.address(caller),
Typed.address(params.to),
Typed.uint256(tableId),
overrides
);
}

0 comments on commit ba82f92

Please sign in to comment.