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

Typescript SDK changes and bug fixes needed for Connect #474

Merged
merged 4 commits into from
Jul 18, 2024
Merged
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
18 changes: 9 additions & 9 deletions evm/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@
"test": "jest --config ./jest.config.ts"
},
"dependencies": {
"@wormhole-foundation/sdk-base": "^0.7",
"@wormhole-foundation/sdk-definitions": "^0.7",
"@wormhole-foundation/sdk-base": "^0.8",
"@wormhole-foundation/sdk-definitions": "^0.8",
"@wormhole-foundation/sdk-definitions-ntt": "0.1.0-beta.0",
"@wormhole-foundation/sdk-evm": "^0.7",
"@wormhole-foundation/sdk-evm-core": "^0.7",
"@wormhole-foundation/sdk-evm": "^0.8",
"@wormhole-foundation/sdk-evm-core": "^0.8",
"ethers": "^6.5.1"
},
"peerDependencies": {
"@wormhole-foundation/sdk-definitions-ntt": "0.1.0-beta.0",
"@wormhole-foundation/sdk-base": "^0.7",
"@wormhole-foundation/sdk-definitions": "^0.7",
"@wormhole-foundation/sdk-evm": "^0.7",
"@wormhole-foundation/sdk-evm-core": "^0.7"
"@wormhole-foundation/sdk-base": "^0.8",
"@wormhole-foundation/sdk-definitions": "^0.8",
"@wormhole-foundation/sdk-evm": "^0.8",
"@wormhole-foundation/sdk-evm-core": "^0.8"
},
"devDependencies": {
"@typechain/ethers-v6": "^0.5.1",
Expand All @@ -76,4 +76,4 @@
}
}
}
}
}
30 changes: 18 additions & 12 deletions evm/ts/src/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Provider } from "ethers";

import { _0_1_0, _1_0_0, _1_1_0 } from "./ethers-contracts/index.js";

export const AbiVersions = {
"0.1.0": _0_1_0,
"1.0.0": _1_0_0,
"1.1.0": _1_1_0,
default: _1_1_0,
} as const;
export type AbiVersion = keyof typeof AbiVersions;
import { Ntt } from "@wormhole-foundation/sdk-definitions-ntt";

// This is a descending list of all ABI versions the SDK is aware of.
// We check for the first match in descending order, allowing for higher minor and patch versions
// being used by the live contract (these are supposed to still be compatible with older ABIs).
export const abiVersions = [
["1.1.0", _1_1_0],
["1.0.0", _1_0_0],
["0.1.0", _0_1_0],
] as const;
export type AbiVersion = (typeof abiVersions)[number][0];

export interface NttBindings {
NttManager: NttManagerBindings;
Expand Down Expand Up @@ -36,8 +39,11 @@ export interface NttManagerBindings {
connect(address: string, provider: Provider): NttManagerBindings.NttManager;
}

export function loadAbiVersion(version: string) {
if (!(version in AbiVersions))
throw new Error(`Unknown ABI version: ${version}`);
return AbiVersions[version as AbiVersion];
export function loadAbiVersion(targetVersion: string) {
for (const [abiVersion, abi] of abiVersions) {
if (Ntt.abiVersionMatches(targetVersion, abiVersion)) {
return abi;
}
}
throw new Error(`Unknown ABI version: ${targetVersion}`);
}
58 changes: 42 additions & 16 deletions evm/ts/src/ntt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
ChainAddress,
ChainsConfig,
Contracts,
TokenAddress,
VAA,
serialize,
universalAddress,
} from "@wormhole-foundation/sdk-definitions";
Expand Down Expand Up @@ -100,7 +98,7 @@ export class EvmNtt<N extends Network, C extends EvmChains>
readonly chain: C,
readonly provider: Provider,
readonly contracts: Contracts & { ntt?: Ntt.Contracts },
readonly version: string = "default"
readonly version: string = "1.0.0"
) {
if (!contracts.ntt) throw new Error("No Ntt Contracts provided");

Expand Down Expand Up @@ -143,19 +141,41 @@ export class EvmNtt<N extends Network, C extends EvmChains>
return enabled.filter((x) => x).length > 0;
}

getIsExecuted(attestation: Ntt.Attestation): Promise<boolean> {
const { emitterChain: chain, payload } =
attestation as VAA<"Ntt:WormholeTransfer">;
return this.manager.isMessageExecuted(
Ntt.messageDigest(chain, payload["nttManagerPayload"])
async getIsExecuted(attestation: Ntt.Attestation): Promise<boolean> {
const payload =
attestation.payloadName === "WormholeTransfer"
? attestation.payload
: attestation.payload.payload;
const isExecuted = await this.manager.isMessageExecuted(
Ntt.messageDigest(attestation.emitterChain, payload["nttManagerPayload"])
);
if (!isExecuted) return false;
// Also check that the transfer is not queued for it to be considered complete
return !(await this.getIsTransferInboundQueued(attestation));
}

async getIsTransferInboundQueued(
attestation: Ntt.Attestation
): Promise<boolean> {
const payload =
attestation.payloadName === "WormholeTransfer"
? attestation.payload
: attestation.payload.payload;
return (
(await this.getInboundQueuedTransfer(
attestation.emitterChain,
payload["nttManagerPayload"]
)) !== null
);
}

getIsApproved(attestation: Ntt.Attestation): Promise<boolean> {
const { emitterChain: chain, payload } =
attestation as VAA<"Ntt:WormholeTransfer">;
const payload =
attestation.payloadName === "WormholeTransfer"
? attestation.payload
: attestation.payload.payload;
return this.manager.isMessageApproved(
Ntt.messageDigest(chain, payload["nttManagerPayload"])
Ntt.messageDigest(attestation.emitterChain, payload["nttManagerPayload"])
);
}

Expand Down Expand Up @@ -311,7 +331,13 @@ export class EvmNtt<N extends Network, C extends EvmChains>

for (const idx in this.xcvrs) {
const xcvr = this.xcvrs[idx]!;
yield* xcvr.receive(attestations[idx]);
const attestation = attestations[idx];
if (attestation?.payloadName !== "WormholeTransfer") {
// TODO: support standard relayer attestations
// which must be submitted to the delivery provider
throw new Error("Invalid attestation type for redeem");
}
yield* xcvr.receive(attestation);
}
}

Expand Down Expand Up @@ -349,12 +375,12 @@ export class EvmNtt<N extends Network, C extends EvmChains>
async *completeInboundQueuedTransfer(
fromChain: Chain,
transceiverMessage: Ntt.Message,
token: TokenAddress<C>,
payer?: AccountAddress<C>
) {
const tx = await this.manager.completeInboundQueuedTransfer(
Ntt.messageDigest(fromChain, transceiverMessage)
);
const tx =
await this.manager.completeInboundQueuedTransfer.populateTransaction(
Ntt.messageDigest(fromChain, transceiverMessage)
);
yield this.createUnsignedTx(tx, "Ntt.completeInboundQueuedTransfer");
}

Expand Down
Loading
Loading