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

SDK: EVM prep for protocol implementations, update to ethers v6 #185

Open
wants to merge 5 commits into
base: sdk/protocol-def
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build:esm": "tsc -p tsconfig.esm.json",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build": "npm run build:esm && npm run build:cjs",
"generate": "typechain --target=ethers-v5 --out-dir=ts/src/types out/[!build-info]*/*.json",
"generate": "typechain --target=ethers-v6 --out-dir=ts/src/types out/[!build-info]*/*.json",
"clean": "rm -rf dist && rm -rf node_modules && rm -f ./*.tsbuildinfo"
},
"exports": {
Expand All @@ -34,11 +34,11 @@
"@wormhole-foundation/sdk-definitions": "^0.7.0-beta.6",
"@wormhole-foundation/sdk-evm": "^0.7.0-beta.6",
"@wormhole-foundation/example-liquidity-layer-definitions": "0.0.1",
"ethers": "^5.7.2"
"ethers": "^6.5.1"
},
"devDependencies": {
"envfile": "^7.1.0",
"@typechain/ethers-v5": "^10.2.0",
"@typechain/ethers-v6":"0.5.1",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@types/node": "^18.14.5",
Expand Down
70 changes: 30 additions & 40 deletions evm/ts/src/MatchingEngine/evm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainId } from "@wormhole-foundation/sdk-base";
import { ChainId, asChainId } from "@wormhole-foundation/sdk-base";
import { ethers } from "ethers";
import { RouterEndpoint, LiveAuctionData, MatchingEngine, RedeemParameters } from ".";
import { LiquidityLayerTransactionResult } from "..";
Expand All @@ -23,74 +23,66 @@ export class EvmMatchingEngine implements MatchingEngine<ethers.ContractTransact
};

constructor(
connection: ethers.Signer | ethers.providers.Provider,
contractAddress: string,
circleBridge: string,
private connection: ethers.Provider,
readonly contractAddress: string,
readonly circleBridge: string,
) {
this.contract = IMatchingEngine__factory.connect(contractAddress, connection);
this.circle = ITokenMessenger__factory.connect(circleBridge, connection);
}

get address(): string {
return this.contract.address;
return this.contractAddress;
}

get signer(): ethers.Signer {
return this.contract.signer;
get provider(): ethers.Provider {
return this.connection;
}

get signerAddress(): Promise<string> {
return this.contract.signer.getAddress();
}

get provider(): ethers.providers.Provider {
return this.contract.provider;
}

connect(connection: ethers.Signer | ethers.providers.Provider): EvmMatchingEngine {
return new EvmMatchingEngine(connection, this.address, this.circle.address);
connect(connection: ethers.Provider): EvmMatchingEngine {
return new EvmMatchingEngine(connection, this.address, this.circleBridge);
}

async addRouterEndpoint(
chain: number,
endpoint: RouterEndpoint,
domain: number,
): Promise<ethers.ContractTransaction> {
return this.contract.addRouterEndpoint(chain, endpoint, domain);
return this.contract.addRouterEndpoint.populateTransaction(chain, endpoint, domain);
}

async placeInitialBid(
fastTransferVaa: Buffer | Uint8Array,
feeBid: bigint | ethers.BigNumberish,
): Promise<ethers.ContractTransaction> {
return this.contract.placeInitialBid(fastTransferVaa, feeBid);
return this.contract.placeInitialBid.populateTransaction(fastTransferVaa, feeBid);
}

async improveBid(
auctionId: Buffer | Uint8Array,
feeBid: bigint | ethers.BigNumberish,
): Promise<ethers.ContractTransaction> {
return this.contract.improveBid(auctionId, feeBid);
return this.contract.improveBid.populateTransaction(auctionId, feeBid);
}

async executeFastOrder(
fastTransferVaa: Buffer | Uint8Array,
): Promise<ethers.ContractTransaction> {
return this.contract.executeFastOrder(fastTransferVaa);
return this.contract.executeFastOrder.populateTransaction(fastTransferVaa);
}

async executeSlowOrderAndRedeem(
fastTransferVaa: Buffer | Uint8Array,
params: RedeemParameters,
): Promise<ethers.ContractTransaction> {
return this.contract.executeSlowOrderAndRedeem(fastTransferVaa, params);
return this.contract.executeSlowOrderAndRedeem.populateTransaction(fastTransferVaa, params);
}

async calculateDynamicPenalty(
auctionId?: Buffer | Uint8Array,
amount?: bigint | ethers.BigNumberish,
blocksElapsed?: bigint | ethers.BigNumberish,
): Promise<[ethers.BigNumberish, ethers.BigNumberish]> {
): Promise<[bigint, bigint]> {
if (auctionId !== undefined) {
return this.contract["calculateDynamicPenalty(bytes32)"](auctionId);
} else if (amount !== undefined && blocksElapsed !== undefined) {
Expand All @@ -104,27 +96,27 @@ export class EvmMatchingEngine implements MatchingEngine<ethers.ContractTransact
return this.contract.liveAuctionInfo(auctionId);
}

async auctionStatus(auctionId: Buffer | Uint8Array): Promise<number> {
async auctionStatus(auctionId: Buffer | Uint8Array) {
return this.contract.liveAuctionInfo(auctionId).then((res) => res.status);
}

async getAuctionGracePeriod(): Promise<number> {
async getAuctionGracePeriod() {
return this.contract.getAuctionGracePeriod();
}

async getAuctionDuration(): Promise<number> {
async getAuctionDuration() {
return this.contract.getAuctionDuration();
}

async getPenaltyBlocks(): Promise<number> {
async getPenaltyBlocks() {
return this.contract.getAuctionPenaltyBlocks();
}

async getInitialPenaltyBps(): Promise<number> {
async getInitialPenaltyBps() {
return this.contract.getInitialPenaltyBps();
}

async getUserPenaltyRewardBps(): Promise<number> {
async getUserPenaltyRewardBps() {
return this.contract.getUserPenaltyRewardBps();
}

Expand All @@ -136,35 +128,33 @@ export class EvmMatchingEngine implements MatchingEngine<ethers.ContractTransact
// Check cached contracts.
const { chainId, coreBridge, circleTransmitterAddress } = await this._cacheIfNeeded();

return this.contract.provider
.getTransactionReceipt(txHash)
const coreAddress = await coreBridge.getAddress();

return this.connection
.provider!.getTransactionReceipt(txHash)
.then((txReceipt) =>
LiquidityLayerTransactionResult.fromEthersTransactionReceipt(
chainId,
this.address,
coreBridge.address,
txReceipt,
coreAddress,
txReceipt!,
circleTransmitterAddress,
),
);
}

private async _cacheIfNeeded() {
if (this.cache === undefined) {
const provider = this.contract.provider;
const provider = this.connection;
const coreBridge = await this.contract
.wormhole()
.then((addr) => IWormhole__factory.connect(addr, provider));
const circleTransmitterAddress = await this.circle.localMessageTransmitter();

// If this isn't a recognized ChainId, we have problems.
const chainId = await coreBridge.chainId();
const chainId = asChainId(Number(await coreBridge.chainId()));

this.cache = {
chainId: chainId as ChainId,
coreBridge,
circleTransmitterAddress,
};
this.cache = { chainId, coreBridge, circleTransmitterAddress };
}

return this.cache;
Expand Down
34 changes: 17 additions & 17 deletions evm/ts/src/MatchingEngine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export type RedeemParameters = {
};

export type LiveAuctionData = {
status: number;
startBlock: bigint | ethers.BigNumberish;
status: bigint;
startBlock: bigint;
highestBidder: string;
initialBidder: string;
amount: bigint | ethers.BigNumberish;
securityDeposit: bigint | ethers.BigNumberish;
bidPrice: bigint | ethers.BigNumberish;
amount: bigint;
securityDeposit: bigint;
bidPrice: bigint;
};

export type RouterEndpoint = {
Expand All @@ -30,47 +30,47 @@ export abstract class MatchingEngine<PreparedTransactionType extends PreparedIns
abstract addRouterEndpoint(
chain: number,
endpoint: RouterEndpoint,
domain: number
domain: number,
): Promise<PreparedTransactionType>;

abstract liveAuctionInfo(auctionId: Buffer | Uint8Array): Promise<LiveAuctionData>;

abstract auctionStatus(auctionId: Buffer | Uint8Array): Promise<number>;
abstract auctionStatus(auctionId: Buffer | Uint8Array): Promise<bigint>;

abstract placeInitialBid(
fastTransferVaa: Buffer | Uint8Array,
feeBid: bigint
feeBid: bigint,
): Promise<PreparedTransactionType>;

abstract improveBid(
auctionId: Buffer | Uint8Array,
feeBid: bigint
feeBid: bigint,
): Promise<PreparedTransactionType>;

abstract executeFastOrder(
fastTransferVaa: Buffer | Uint8Array
fastTransferVaa: Buffer | Uint8Array,
): Promise<PreparedTransactionType>;

abstract executeSlowOrderAndRedeem(
fastTransferVaa: Buffer | Uint8Array,
params: RedeemParameters
params: RedeemParameters,
): Promise<PreparedTransactionType>;

abstract calculateDynamicPenalty(
auctionId?: Buffer | Uint8Array,
amount?: bigint,
blocksElapsed?: bigint
blocksElapsed?: bigint,
): Promise<[ethers.BigNumberish, ethers.BigNumberish]>;

abstract getAuctionGracePeriod(): Promise<number>;
abstract getAuctionGracePeriod(): Promise<bigint>;

abstract getAuctionDuration(): Promise<number>;
abstract getAuctionDuration(): Promise<bigint>;

abstract getPenaltyBlocks(): Promise<number>;
abstract getPenaltyBlocks(): Promise<bigint>;

abstract getInitialPenaltyBps(): Promise<number>;
abstract getInitialPenaltyBps(): Promise<bigint>;

abstract getInitialPenaltyBps(): Promise<number>;
abstract getInitialPenaltyBps(): Promise<bigint>;

abstract wormhole(): Promise<string>;

Expand Down
Loading
Loading