-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathhelpers.ts
53 lines (49 loc) · 1.9 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { AnchorProvider, BN } from "@coral-xyz/anchor";
import { BigNumber } from "@ethersproject/bignumber";
import { ethers } from "ethers";
import { DepositData } from "../../types/svm";
import { PublicKey } from "@solana/web3.js";
/**
* Returns the chainId for a given solana cluster.
*/
export const getSolanaChainId = (cluster: "devnet" | "mainnet"): BigNumber => {
return BigNumber.from(
BigInt(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(`solana-${cluster}`))) & BigInt("0xFFFFFFFFFFFF")
);
};
/**
* Returns true if the provider is on the devnet cluster.
*/
export const isSolanaDevnet = (provider: AnchorProvider): boolean => {
const solanaRpcEndpoint = provider.connection.rpcEndpoint;
if (solanaRpcEndpoint.includes("devnet")) return true;
else if (solanaRpcEndpoint.includes("mainnet")) return false;
else throw new Error(`Unsupported solanaCluster endpoint: ${solanaRpcEndpoint}`);
};
/**
* Returns the delegate PDA for a deposit.
*/
export const getDepositDelegatePda = (depositData: DepositData, stateSeed: BN, programId: PublicKey) => {
const raw = Buffer.concat([
depositData.inputToken!.toBytes(),
depositData.outputToken.toBytes(),
depositData.inputAmount.toArrayLike(Buffer, "le", 8),
depositData.outputAmount.toArrayLike(Buffer, "le", 8),
depositData.destinationChainId.toArrayLike(Buffer, "le", 8),
]);
const hashHex = ethers.utils.keccak256(raw);
const seedHash = Buffer.from(hashHex.slice(2), "hex");
return PublicKey.findProgramAddressSync(
[Buffer.from("delegate"), stateSeed.toArrayLike(Buffer, "le", 8), seedHash],
programId
)[0];
};
/**
* Returns the delegate PDA for a fill relay.
*/
export const getFillRelayDelegatePda = (relayHash: Uint8Array, stateSeed: BN, programId: PublicKey) => {
return PublicKey.findProgramAddressSync(
[Buffer.from("delegate"), stateSeed.toArrayLike(Buffer, "le", 8), relayHash],
programId
)[0];
};