Skip to content

Commit

Permalink
Add display deposit types to fe-sdk (#426)
Browse files Browse the repository at this point in the history
* Update sdk.ts

* changeset

* pr feedback

* add display deposit types to fe-sdk

* remove 'as type'

* Add changeset

---------

Signed-off-by: Daniel Park <[email protected]>
  • Loading branch information
panieldark authored Sep 12, 2023
1 parent 25556c7 commit 60a70c1
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-houses-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nocturne-xyz/frontend-sdk": minor
---

Add adapter types for deposits at the frontend-sdk layer
6 changes: 4 additions & 2 deletions packages/frontend-sdk/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Erc20Config } from "@nocturne-xyz/config";
import {
Address,
AssetWithBalance,
DepositRequest,
DepositQuoteResponse,
OperationRequestWithMetadata,
ProvenOperation,
Expand All @@ -16,6 +15,7 @@ import { GetSnapsResponse, Snap } from "./metamask/types";
import {
DepositHandle,
DepositHandleWithReceipt,
DisplayDepositRequest,
GetBalanceOpts,
OperationHandle,
SyncWithProgressOutput,
Expand Down Expand Up @@ -64,7 +64,9 @@ export interface NocturneSdkApi {
/**
* Initiates a deposit retrieval from the deposit manager contract.
*/
retrievePendingDeposit(req: DepositRequest): Promise<ContractTransaction>;
retrievePendingDeposit(
displayRequest: DisplayDepositRequest
): Promise<ContractTransaction>;

// *** OPERATION METHODS *** //

Expand Down
3 changes: 1 addition & 2 deletions packages/frontend-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export {
DepositHandleWithReceipt,
DepositRequestStatus,
DepositRequestStatusWithMetadata,
DepositRequestWithMetadata,
DisplayDepositRequestWithMetadata,
GetBalanceOpts,
NocturneSdkConfig,
OperationHandle,
Expand All @@ -21,7 +21,6 @@ export {
AssetWithBalance,
ConfidentialPaymentMetadata,
DepositQuoteResponse,
DepositRequest,
OperationRequest,
OperationRequestWithMetadata,
OperationStatus,
Expand Down
49 changes: 27 additions & 22 deletions packages/frontend-sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
AssetWithBalance,
ClosableAsyncIterator,
DepositQuoteResponse,
DepositRequest,
DepositStatusResponse,
JoinSplitProofWithPublicSignals,
newOpRequestBuilder,
Expand Down Expand Up @@ -69,7 +68,7 @@ import {
} from "@urql/core";
import retry from "async-retry";
import * as JSON from "bigint-json-serialization";
import { ContractTransaction, ethers } from "ethers";
import { BigNumber, ContractTransaction, ethers } from "ethers";
import JOINSPLIT_VKEY from "../circuit-artifacts/joinsplit/joinsplitVkey.json";
import CANON_ADDR_SIG_CHECK_VKEY from "../circuit-artifacts/canonAddrSigCheck/canonAddrSigCheckVkey.json";
import { NocturneSdkApi, SnapStateApi } from "./api";
Expand All @@ -86,7 +85,8 @@ import {
DepositHandle,
DepositHandleWithReceipt,
DepositRequestStatusWithMetadata,
DepositRequestWithMetadata,
DisplayDepositRequest,
DisplayDepositRequestWithMetadata,
Endpoints,
GetBalanceOpts,
NocturneSdkConfig,
Expand All @@ -99,6 +99,7 @@ import {
flattenDepositRequestStatus,
getNocturneSdkConfig,
getTokenContract,
toDepositRequest,
toDepositRequestWithMetadata,
} from "./utils";
import { Erc20Plugin } from "@nocturne-xyz/op-request-plugins";
Expand Down Expand Up @@ -205,7 +206,6 @@ export class NocturneSdk implements NocturneSdkApi {
url: this.endpoints.subgraphEndpoint,
exchanges: [fetchExchange],
});

const kv = new IdbKvStore(`nocturne-fe-sdk-${networkName}`);
this.db = new NocturneDB(kv);

Expand All @@ -227,6 +227,7 @@ export class NocturneSdk implements NocturneSdkApi {
);
});
}

protected get wethAddress(): string | undefined {
return this.config.config.erc20s.get("weth")?.address;
}
Expand Down Expand Up @@ -420,14 +421,15 @@ export class NocturneSdk implements NocturneSdkApi {
}

async retrievePendingDeposit(
req: DepositRequest,
displayRequest: DisplayDepositRequest,
retrieveEthDepositsAs: "ETH" | "WETH" = "ETH"
): Promise<ContractTransaction> {
const signer = await this.getWindowSigner();
const signerAddress = await signer.getAddress();
if (signerAddress.toLowerCase() !== req.spender.toLowerCase()) {
if (signerAddress.toLowerCase() !== displayRequest.spender.toLowerCase()) {
throw new Error("Spender and signer addresses do not match");
}
const req = toDepositRequest(displayRequest);
const depositManagerContract = await this.depositManagerContractThunk();
const isOutstandingDeposit =
await depositManagerContract._outstandingDepositHashes(
Expand All @@ -436,10 +438,9 @@ export class NocturneSdk implements NocturneSdkApi {
if (!isOutstandingDeposit) {
throw new Error("Deposit request does not exist");
}
const asset = AssetTrait.decode(req.encodedAsset);
if (
retrieveEthDepositsAs === "ETH" &&
asset.assetAddr === this.wethAddress
displayRequest.asset.assetAddr === this.wethAddress
) {
return depositManagerContract.retrieveETHDeposit(req);
} else {
Expand Down Expand Up @@ -851,29 +852,33 @@ export class NocturneSdk implements NocturneSdkApi {
return Promise.all(
events.map(async (event) => {
const {
encodedAsset,
encodedAsset: _encodedAsset,
value,
nonce,
depositAddr,
gasCompensation,
spender,
} = event.args;

const request: DepositRequestWithMetadata & {
const encodedAsset = {
encodedAssetAddr: _encodedAsset.encodedAssetAddr.toBigInt(),
encodedAssetId: _encodedAsset.encodedAssetId.toBigInt(),
};
const asset = AssetTrait.decode(encodedAsset);

const request: DisplayDepositRequestWithMetadata & {
subgraphStatus?: GqlDepositRequestStatus;
} = {
spender,
encodedAsset: {
encodedAssetAddr: encodedAsset.encodedAssetAddr.toBigInt(),
encodedAssetId: encodedAsset.encodedAssetId.toBigInt(),
},
value: value.toBigInt(),
depositAddr: {
h1: depositAddr.h1.toBigInt(),
h2: depositAddr.h2.toBigInt(),
asset: {
assetAddr: asset.assetAddr,
assetType: asset.assetType,
id: BigNumber.from(asset.id),
},
nonce: nonce.toBigInt(),
gasCompensation: gasCompensation.toBigInt(),
value,
depositAddr,
nonce,
gasCompensation,
createdAtBlock: tx.blockNumber,
subgraphStatus: GqlDepositRequestStatus.Pending,
};
Expand All @@ -887,12 +892,12 @@ export class NocturneSdk implements NocturneSdkApi {
}

private async makeDepositHandle(
requestWithStatus: DepositRequestWithMetadata & {
requestWithStatus: DisplayDepositRequestWithMetadata & {
subgraphStatus?: GqlDepositRequestStatus;
}
): Promise<DepositHandle> {
const { subgraphStatus, ...request } = requestWithStatus;
const depositRequestHash = hashDepositRequest(request);
const depositRequestHash = hashDepositRequest(toDepositRequest(request));
const getStatus = async () =>
await getDepositRequestStatus(
this.endpoints.screenerEndpoint,
Expand Down
41 changes: 31 additions & 10 deletions packages/frontend-sdk/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { NocturneConfig } from "@nocturne-xyz/config";

import {
AssetType,
ClosableAsyncIterator,
DepositRequest,
OperationMetadata,
OperationStatusResponse,
} from "@nocturne-xyz/core";
import { ContractReceipt, ethers } from "ethers";
import { BigNumber, ContractReceipt, ethers } from "ethers";

export interface Endpoints {
screenerEndpoint: string;
Expand Down Expand Up @@ -46,13 +46,6 @@ export interface DepositHandleWithReceipt {
handle: DepositHandle;
}

export interface DepositRequestWithMetadata extends DepositRequest {
createdAtBlock?: number;
txHashInstantiated?: string;
txHashCompleted?: string;
txHashRetrieved?: string;
}

export enum DepositRequestStatus {
// deposit has been initiated on-chain
// and funds are in escrow, but
Expand Down Expand Up @@ -89,7 +82,7 @@ export interface DepositRequestStatusWithMetadata {

export interface DepositHandle {
depositRequestHash: string;
request: DepositRequestWithMetadata;
request: DisplayDepositRequestWithMetadata;
currentStatus: DepositRequestStatusWithMetadata;
getStatus: () => Promise<DepositRequestStatusWithMetadata>;
}
Expand All @@ -108,3 +101,31 @@ export interface TokenDetails {
export type SupportedProvider =
| ethers.providers.JsonRpcProvider
| ethers.providers.Web3Provider;

// *** CONVERTED TYPES *** //

export interface ConvertedAsset {
assetType: AssetType;
assetAddr: string;
id: BigNumber;
}
export interface ConvertedCompressedStealthAddress {
h1: BigNumber;
h2: BigNumber;
}
export interface DisplayDepositRequest {
spender: string;
asset: ConvertedAsset;
value: BigNumber;
depositAddr: ConvertedCompressedStealthAddress;
nonce: BigNumber;
gasCompensation: BigNumber;
}

export interface DisplayDepositRequestWithMetadata
extends DisplayDepositRequest {
createdAtBlock?: number;
txHashInstantiated?: string;
txHashCompleted?: string;
txHashRetrieved?: string;
}
49 changes: 38 additions & 11 deletions packages/frontend-sdk/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { loadNocturneConfigBuiltin } from "@nocturne-xyz/config";
import {
Address,
AssetTrait,
AssetType,
DepositRequest,
DepositRequestStatus as ScreenerDepositRequestStatus,
TotalEntityIndexTrait,
} from "@nocturne-xyz/core";
import { ethers } from "ethers";
import { BigNumber, ethers } from "ethers";
import ERC1155 from "./abis/ERC1155.json";
import ERC20 from "./abis/ERC20.json";
import ERC721 from "./abis/ERC721.json";
Expand All @@ -15,11 +17,12 @@ import {
} from "./gql/autogenerated/graphql";
import {
DepositRequestStatus,
DepositRequestWithMetadata,
DisplayDepositRequestWithMetadata,
NocturneSdkConfig,
SupportedNetwork,
Endpoints,
TokenDetails,
DisplayDepositRequest,
} from "./types";

const ENDPOINTS = {
Expand Down Expand Up @@ -130,7 +133,7 @@ export function getNocturneSdkConfig(

export function toDepositRequestWithMetadata(
gqlDeposit: Omit<GqlDepositRequest, "id">
): DepositRequestWithMetadata & {
): DisplayDepositRequestWithMetadata & {
subgraphStatus: GqlDepositRequestStatus;
} {
const {
Expand All @@ -147,19 +150,23 @@ export function toDepositRequestWithMetadata(
completionTxHash,
retrievalTxHash,
} = gqlDeposit;
const asset = AssetTrait.decode({
encodedAssetAddr: BigInt(encodedAssetAddr),
encodedAssetId: BigInt(encodedAssetId),
});
return {
spender,
subgraphStatus: status,
value,
nonce,
gasCompensation,
encodedAsset: {
encodedAssetAddr,
encodedAssetId,
value: BigNumber.from(value),
nonce: BigNumber.from(nonce),
gasCompensation: BigNumber.from(gasCompensation),
asset: {
...asset,
id: BigNumber.from(asset.id),
},
depositAddr: {
h1: depositAddrH1,
h2: depositAddrH2,
h1: BigNumber.from(depositAddrH1),
h2: BigNumber.from(depositAddrH2),
},
createdAtBlock: TotalEntityIndexTrait.convertToBlockNumber(
BigInt(gqlDeposit.createdAtTotalEntityIndex)
Expand All @@ -170,6 +177,26 @@ export function toDepositRequestWithMetadata(
};
}

export function toDepositRequest(
displayDepositRequest: DisplayDepositRequest
): DepositRequest {
const asset = {
...displayDepositRequest.asset,
id: displayDepositRequest.asset.id.toBigInt(),
};
return {
spender: displayDepositRequest.spender,
encodedAsset: AssetTrait.encode(asset),
value: displayDepositRequest.value.toBigInt(),
depositAddr: {
h1: displayDepositRequest.depositAddr.h1.toBigInt(),
h2: displayDepositRequest.depositAddr.h2.toBigInt(),
},
nonce: displayDepositRequest.nonce.toBigInt(),
gasCompensation: displayDepositRequest.gasCompensation.toBigInt(),
};
}

export function flattenDepositRequestStatus(
subgraphStatus: GqlDepositRequestStatus,
screenerStatus: ScreenerDepositRequestStatus
Expand Down

0 comments on commit 60a70c1

Please sign in to comment.