Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Oct 17, 2023
1 parent b7a25ad commit a3fe099
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 60 deletions.
2 changes: 1 addition & 1 deletion examples/deployer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function main() {
const network = "goerli";
const provider = new ethers.InfuraProvider(network);
const connectedSigner = turnkeySigner.connect(provider);

const connectedNetwork = await turnkeySigner.provider!.getNetwork();
const chainId = connectedNetwork.chainId;
const address = await connectedSigner.getAddress();
Expand Down
4 changes: 1 addition & 3 deletions examples/rebalancer/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ let provider = new ethers.InfuraProvider(
process.env.INFURA_KEY || DEFAULT_INFURA_COMMUNITY_KEY
);

export function getProvider(
env = Environment.SEPOLIA
): ethers.Provider {
export function getProvider(env = Environment.SEPOLIA): ethers.Provider {
if (env !== Environment.SEPOLIA) {
provider = new ethers.InfuraProvider(
env,
Expand Down
4 changes: 2 additions & 2 deletions examples/rebalancer/src/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export async function sendEth(
}

const feeData = precalculatedFeeData || (await provider.getFeeData());
const gasRequired = (feeData
.maxFeePerGas! + feeData.maxPriorityFeePerGas!) * 21000n;
const gasRequired =
(feeData.maxFeePerGas! + feeData.maxPriorityFeePerGas!) * 21000n;
const totalCost = gasRequired + BigInt(value);

if (balance < totalCost) {
Expand Down
8 changes: 2 additions & 6 deletions examples/sweeper/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ let provider = new ethers.InfuraProvider(
process.env.INFURA_KEY || DEFAULT_INFURA_COMMUNITY_KEY
);

export function getProvider(
env = Environment.GOERLI
): ethers.Provider {
export function getProvider(env = Environment.GOERLI): ethers.Provider {
if (env !== Environment.GOERLI) {
provider = new ethers.InfuraProvider(
env,
Expand All @@ -32,9 +30,7 @@ export function getProvider(

// getTurnkeySigner returns a TurnkeySigner connected to the passed-in Provider
// (https://docs.ethers.org/v6/api/providers/)
export function getTurnkeySigner(
provider: ethers.Provider
): TurnkeySigner {
export function getTurnkeySigner(provider: ethers.Provider): TurnkeySigner {
const turnkeyClient = new TurnkeyClient(
{
baseUrl: process.env.BASE_URL!,
Expand Down
8 changes: 5 additions & 3 deletions examples/trading-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ async function wrapUnwrapImpl(baseAsset: string, baseAmount: string) {
const tokenContract = new ethers.Contract(
metadata!.token.address,
metadata!.abi,
provider,
provider
);

if (baseAsset === "ETH") {
Expand Down Expand Up @@ -560,9 +560,11 @@ async function sweepImpl(asset: string, destination: string, amount: string) {
if (asset === "ETH") {
const balance = await provider.getBalance(address);
const sweepAmount = fromReadableAmount(parseFloat(amount), 18) || balance;
const gasRequired = (feeData.maxFeePerGas! + feeData.maxPriorityFeePerGas!) * BigInt(NATIVE_TRANSFER_GAS_LIMIT);
const gasRequired =
(feeData.maxFeePerGas! + feeData.maxPriorityFeePerGas!) *
BigInt(NATIVE_TRANSFER_GAS_LIMIT);

const finalAmount = BigInt(sweepAmount) - (gasRequired * 2n); // be relatively conservative with sweep amount to prevent overdraft
const finalAmount = BigInt(sweepAmount) - gasRequired * 2n; // be relatively conservative with sweep amount to prevent overdraft

// make balance check to confirm we can make the trade
if (finalAmount <= 0n) {
Expand Down
4 changes: 1 addition & 3 deletions examples/trading-runner/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ let provider = new ethers.InfuraProvider(
process.env.INFURA_KEY || DEFAULT_INFURA_COMMUNITY_KEY
);

export function getProvider(
env = Environment.SEPOLIA
): ethers.Provider {
export function getProvider(env = Environment.SEPOLIA): ethers.Provider {
if (env !== Environment.SEPOLIA) {
provider = new ethers.InfuraProvider(
env,
Expand Down
10 changes: 7 additions & 3 deletions examples/trading-runner/src/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export async function sendEth(
}

const feeData = precalculatedFeeData || (await provider.getFeeData());
const gasRequired = (feeData.maxFeePerGas! + feeData.maxPriorityFeePerGas!) * BigInt(NATIVE_TRANSFER_GAS_LIMIT);
const gasRequired =
(feeData.maxFeePerGas! + feeData.maxPriorityFeePerGas!) *
BigInt(NATIVE_TRANSFER_GAS_LIMIT);
const totalCost = gasRequired + BigInt(value);

if (balance < totalCost) {
Expand Down Expand Up @@ -224,7 +226,7 @@ export async function wrapEth(
(feeData.maxFeePerGas! + feeData.maxPriorityFeePerGas!) *
BigInt(ERC20_TRANSFER_GAS_LIMIT);

if (balance < (BigInt(value) + gasRequired)) {
if (balance < BigInt(value) + gasRequired) {
throw new Error(
`Insufficient ETH balance of ${toReadableAmount(
balance.toString(),
Expand Down Expand Up @@ -304,7 +306,9 @@ export async function unwrapWeth(
}

const feeData = precalculatedFeeData || (await provider.getFeeData());
const gasRequired = (feeData.maxFeePerGas! + feeData.maxPriorityFeePerGas!) * BigInt(ERC20_TRANSFER_GAS_LIMIT);
const gasRequired =
(feeData.maxFeePerGas! + feeData.maxPriorityFeePerGas!) *
BigInt(ERC20_TRANSFER_GAS_LIMIT);

if (balance < gasRequired) {
throw new Error(
Expand Down
1 change: 1 addition & 0 deletions examples/with-ethers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,5 @@ Wrapped 0.00001 ETH:
```

### 4/ Dev notes

For our contract types bindings, we use [Typechain](https://github.com/dethcrypto/TypeChain). To generate new Typechain bindings, run `pnpm typechain`. Note that per our top-level `.gitignore`, we are excluding Typechain-related artifacts from git for the sake of simplicity. Artifacts found in `./src/contracts` were simply moved over from the Typechain-generated files.
4 changes: 1 addition & 3 deletions examples/with-ethers/src/advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ async function main() {
const baseMessage = "Hello Turnkey";

// 1. Sign a raw hex message
const hexMessage = ethers.hexlify(
ethers.toUtf8Bytes(baseMessage)
);
const hexMessage = ethers.hexlify(ethers.toUtf8Bytes(baseMessage));
let signature = await connectedSigner.signMessage(hexMessage);
let recoveredAddress = ethers.verifyMessage(hexMessage, signature);

Expand Down
5 changes: 4 additions & 1 deletion examples/with-ethers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ async function main() {

if (network === "goerli") {
// https://goerli.etherscan.io/address/0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6
const wethContract = WethContractFactory.connect(WETH_TOKEN_ADDRESS_GOERLI, connectedSigner);
const wethContract = WethContractFactory.connect(
WETH_TOKEN_ADDRESS_GOERLI,
connectedSigner
);

// Read from contract
const wethBalance = await wethContract.balanceOf(address);
Expand Down
8 changes: 2 additions & 6 deletions examples/with-nonce-manager/src/managedOptimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,14 @@ function saveTxs(txMap: Map<string, ethers.TransactionRequest>) {
console.log(`Successfully wrote transactions to ${fileName}\n`);
}

async function monitor(
provider: ethers.Provider,
signer: ethers.Signer
) {
async function monitor(provider: ethers.Provider, signer: ethers.Signer) {
const network = (await signer.provider?.getNetwork())?.name;
const transactionCount = await signer.getNonce();
const startTime = Date.now();
let nonce = transactionCount;

// Load saved transactions from local file and fetch the highest nonce
const txMap: Map<string, ethers.TransactionRequest> =
await loadTxs();
const txMap: Map<string, ethers.TransactionRequest> = await loadTxs();
const nonces = [...txMap.keys()].map((v) => parseInt(v));
const finalExpectedNonce = Math.max(...nonces);

Expand Down
4 changes: 1 addition & 3 deletions examples/with-nonce-manager/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ export async function getUpdatedTransaction(
}

// Helper to get the maximum BigNumber in a given array
export function maxBigNumber(
arr: (BigNumberish | undefined)[]
): BigNumberish {
export function maxBigNumber(arr: (BigNumberish | undefined)[]): BigNumberish {
let max = 0n;

for (let i = 0; i < arr.length; i++) {
Expand Down
5 changes: 4 additions & 1 deletion examples/with-uniswap/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ async function getOutputQuote(route: Route<Currency, Currency>) {
data: calldata,
});

return ethers.AbiCoder.defaultAbiCoder().decode(["uint256"], quoteCallReturnData);
return ethers.AbiCoder.defaultAbiCoder().decode(
["uint256"],
quoteCallReturnData
);
}

export async function getTokenTransferApproval(
Expand Down
8 changes: 2 additions & 6 deletions examples/with-uniswap/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ let provider = new ethers.InfuraProvider(
process.env.INFURA_KEY || DEFAULT_INFURA_COMMUNITY_KEY
);

export function getProvider(
env = Environment.GOERLI
): ethers.Provider {
export function getProvider(env = Environment.GOERLI): ethers.Provider {
if (env !== Environment.GOERLI) {
provider = new ethers.InfuraProvider(
env,
Expand All @@ -32,9 +30,7 @@ export function getProvider(

// getTurnkeySigner returns a TurnkeySigner connected to the passed-in Provider
// (https://docs.ethers.org/v5/api/providers/)
export function getTurnkeySigner(
provider: ethers.Provider
): TurnkeySigner {
export function getTurnkeySigner(provider: ethers.Provider): TurnkeySigner {
const turnkeyClient = new TurnkeyClient(
{
baseUrl: process.env.BASE_URL!,
Expand Down
2 changes: 2 additions & 0 deletions packages/ethers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ If you need a lower-level, fully typed HTTP client for interacting with Turnkey
API Docs: https://docs.turnkey.com/

## Upgrading from v5 to v6

For starters, check out the official Ethers v5 to v6 [migration guide](https://docs.ethers.org/v6/migrating/) for a summary of changes. Note: if EIP-1193 is a critical feature for you, we would recommend to continue using our v5 implementation until an Eip1193Bridge is available for Ethers v6.

## Getting started
Expand Down Expand Up @@ -115,4 +116,5 @@ See `.env.example` to get your local testing environment right. Run `pnpm jest`
- [`@turnkey/http`](/packages/http/): lower-level fully typed HTTP client for interacting with Turnkey API

## Dev notes

For our contract types bindings, we use [Typechain](https://github.com/dethcrypto/TypeChain). These Typechain artifacts are included with our hardhat compilation: `pnpm compile:contracts` (you may need to run `pnpm compile:clean` first). Note that per our top-level `.gitignore`, we are excluding Typechain-related artifacts from git for the sake of simplicity. The artifacts found in `./src/__tests__` are the bare minimum required for our tests, and were simply moved over from the Typechain-generated files.
6 changes: 3 additions & 3 deletions packages/ethers/hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const config = {
cache: "./.cache",
},
typechain: {
outDir: 'src/__tests__/typechain',
target: 'ethers-v6',
outDir: "src/__tests__/typechain",
target: "ethers-v6",
alwaysGenerateOverloads: false,
externalArtifacts: [],
dontOverrideCompile: false
dontOverrideCompile: false,
},
};

Expand Down
5 changes: 1 addition & 4 deletions packages/ethers/src/__tests__/Test721Factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import {
} from "ethers";
import type { Signer, ContractDeployTransaction, ContractRunner } from "ethers";
import type { NonPayableOverrides } from "./common";
import type {
Test721,
Test721Interface,
} from "./Test721";
import type { Test721, Test721Interface } from "./Test721";

const _abi = [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/ethers/src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("TurnkeySigner", () => {
expect(connectedSigner.signMessage).toBeTruthy();
expect(await connectedSigner.getAddress()).toBe(expectedEthAddress);
});

testCase("basics for connected signer via constructor", async () => {
expect(signerWithProvider.signMessage).toBeTruthy();
expect(await signerWithProvider.getAddress()).toBe(expectedEthAddress);
Expand Down
23 changes: 12 additions & 11 deletions packages/ethers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { ethers } from "ethers";
import { TurnkeyActivityError, TurnkeyRequestError } from "@turnkey/http";
import type { TurnkeyClient } from "@turnkey/http";
import type {
BytesLike,
TypedDataDomain,
TypedDataField,
} from "ethers";
import type { BytesLike, TypedDataDomain, TypedDataField } from "ethers";

type TConfig = {
/**
Expand All @@ -22,7 +18,10 @@ type TConfig = {
privateKeyId: string;
};

export class TurnkeySigner extends ethers.AbstractSigner implements ethers.Signer {
export class TurnkeySigner
extends ethers.AbstractSigner
implements ethers.Signer
{
private readonly client: TurnkeyClient;

public readonly organizationId: string;
Expand All @@ -32,7 +31,9 @@ export class TurnkeySigner extends ethers.AbstractSigner implements ethers.Signe
super();

if (provider != null) {
ethers.defineProperties(this, { provider: provider } as { [K in keyof this]?: this[K]; });
ethers.defineProperties(this, { provider: provider } as {
[K in keyof this]?: this[K];
});
}

this.client = config.client;
Expand Down Expand Up @@ -122,9 +123,7 @@ export class TurnkeySigner extends ethers.AbstractSigner implements ethers.Signe
async signTransaction(
transaction: ethers.TransactionRequest
): Promise<string> {
const unsignedTx = (await ethers.resolveProperties(
transaction
));
const unsignedTx = await ethers.resolveProperties(transaction);

// Mimic the behavior of ethers' `Wallet`:
// - You don't need to pass in `tx.from`
Expand All @@ -142,7 +141,9 @@ export class TurnkeySigner extends ethers.AbstractSigner implements ethers.Signe
delete unsignedTx.from;
}

const serializedTx = ethers.Transaction.from(unsignedTx as ethers.Transaction).unsignedSerialized;
const serializedTx = ethers.Transaction.from(
unsignedTx as ethers.Transaction
).unsignedSerialized;
const nonHexPrefixedSerializedTx = serializedTx.replace(/^0x/, "");
const signedTx = await this._signTransactionWithErrorWrapping(
nonHexPrefixedSerializedTx
Expand Down

0 comments on commit a3fe099

Please sign in to comment.