Skip to content

Commit

Permalink
feat: add scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
bishalbikram committed Sep 24, 2024
1 parent 1c5d56a commit e96c55c
Show file tree
Hide file tree
Showing 19 changed files with 799 additions and 1 deletion.
10 changes: 9 additions & 1 deletion contracts/solana/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@ cluster = "Localnet"
wallet = "~/.config/solana/id.json"

[scripts]
init-contract = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/begin-initialize/*.ts"
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
initialize-xcall = "yarn ts-node --project ./tsconfig.json ./scripts/xcall/initialize.ts"
set-admin-xcall = "yarn ts-node --project ./tsconfig.json ./scripts/xcall/set-admin.ts"
set-protocol-fee-xcall = "yarn ts-node --project ./tsconfig.json ./scripts/xcall/set-protocol-fee.ts"
set-protocol-fee-handler-xcall = "yarn ts-node --project ./tsconfig.json ./scripts/xcall/set-protocol-fee-handler.ts"

initialize-centralized = "yarn ts-node --project ./tsconfig.json ./scripts/centralized-connection/initialize.ts"

initialize-mock-dapp-multi = "yarn ts-node --project ./tsconfig.json ./scripts/mock-dapp-multi/initialize.ts"
add-connection-mock-dapp = "yarn ts-node --project ./tsconfig.json ./scripts/mock-dapp-multi/add-connection.ts"
41 changes: 41 additions & 0 deletions contracts/solana/scripts/centralized-connection/initialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { PublicKey } from "@solana/web3.js";
import { SYSTEM_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/native/system";

import { ConnectionPDA } from "../../tests/centralized-connection/setup";
import { connection, connectionProgram, wallet } from "..";
import { TxnHelpers } from "../utils/transaction";

const args = process.argv.slice(2);
if (args.length != 2) throw new Error("Invalid arguments");

const xcallKey = new PublicKey(args[0]);
const adminKey = new PublicKey(args[1]);
let txnHelpers = new TxnHelpers(connection, wallet.payer);

const initializeContract = async () => {
let connConfig = await connectionProgram.account.config.fetchNullable(
ConnectionPDA.config().pda
);
if (!connConfig) {
return await connectionProgram.methods
.initialize(xcallKey, adminKey)
.signers([wallet.payer])
.accountsStrict({
signer: wallet.publicKey,
systemProgram: SYSTEM_PROGRAM_ID,
config: ConnectionPDA.config().pda,
authority: ConnectionPDA.authority().pda,
})
.rpc();
}
};

initializeContract()
.then(async (res) => {
console.log("Contract initializing");
if (res) await txnHelpers.logParsedTx(res);
console.log("Contract initialized successfully");
})
.catch((err) => {
console.log(err);
});
44 changes: 44 additions & 0 deletions contracts/solana/scripts/centralized-connection/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { PublicKey } from "@solana/web3.js";

import { uint128ToArray } from "../utils";
import { connectionProgram } from "..";

export class ConnectionPDA {
constructor() {}

static config() {
let [pda, bump] = PublicKey.findProgramAddressSync(
[Buffer.from("config")],
connectionProgram.programId
);

return { bump, pda };
}

static network_fee(networkId: string) {
const [pda, bump] = PublicKey.findProgramAddressSync(
[Buffer.from("fee"), Buffer.from(networkId)],
connectionProgram.programId
);

return { pda, bump };
}

static receipt(networkId: string, sn: number) {
const [pda, bump] = PublicKey.findProgramAddressSync(
[Buffer.from("receipt"), Buffer.from(networkId), uint128ToArray(sn)],
connectionProgram.programId
);

return { pda, bump };
}

static authority() {
let [pda, bump] = PublicKey.findProgramAddressSync(
[Buffer.from("connection_authority")],
connectionProgram.programId
);

return { bump, pda };
}
}
44 changes: 44 additions & 0 deletions contracts/solana/scripts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os from "os";

import * as anchor from "@coral-xyz/anchor";
import { Connection } from "@solana/web3.js";

import { loadKeypairFromFile } from "./utils";

/** PROGRAMS TYPE */
import { Xcall } from "../target/types/xcall";
import { CentralizedConnection } from "../target/types/centralized_connection";
import { MockDappMulti } from "../target/types/mock_dapp_multi";

/** PROGRAMS IDL */
import dappIdl from "../target/idl/mock_dapp_multi.json";
import connectionIdl from "../target/idl/centralized_connection.json";
import xcallIdl from "../target/idl/xcall.json";

/** RPC PROVIDER */
export const RPC_URL = "http://127.0.0.1:8899";
export const connection = new Connection(RPC_URL, "confirmed");

/** WALLET KEYPAIR */
let keypairFilePath = os.homedir + "/.config/solana/id.json";
export const keypair = loadKeypairFromFile(keypairFilePath);
export const wallet = new anchor.Wallet(keypair);

/** PROVIDER FOR CLIENT */
export const provider = new anchor.AnchorProvider(connection, wallet);
anchor.setProvider(provider);

export const mockDappProgram: anchor.Program<MockDappMulti> =
new anchor.Program(
dappIdl as anchor.Idl,
provider
) as unknown as anchor.Program<MockDappMulti>;
export const connectionProgram: anchor.Program<CentralizedConnection> =
new anchor.Program(
connectionIdl as anchor.Idl,
provider
) as unknown as anchor.Program<CentralizedConnection>;
export const xcallProgram: anchor.Program<Xcall> = new anchor.Program(
xcallIdl as anchor.Idl,
provider
) as unknown as anchor.Program<Xcall>;
33 changes: 33 additions & 0 deletions contracts/solana/scripts/mock-dapp-multi/add-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { SYSTEM_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/native/system";

import { DappPDA } from "../../tests/mock-dapp-multi/setup";
import { connection, mockDappProgram, wallet } from "..";
import { TxnHelpers } from "../utils/transaction";

const args = process.argv.slice(2);
if (args.length != 3) throw new Error("Invalid arguments");

const networkId = args[0];
const srcEndpoint = args[1];
const dstEndpoint = args[2];
let txnHelpers = new TxnHelpers(connection, wallet.payer);

const addConnection = async () => {
return await mockDappProgram.methods
.addConnection(networkId, srcEndpoint, dstEndpoint)
.accounts({
connectionAccount: DappPDA.connections(networkId).pda,
sender: wallet.publicKey,
systemProgram: SYSTEM_PROGRAM_ID,
})
.signers([wallet.payer])
.rpc();
};

addConnection()
.then(async (sig) => {
await txnHelpers.logParsedTx(sig);
})
.catch((err) => {
console.log("Error while adding connection: ", err);
});
40 changes: 40 additions & 0 deletions contracts/solana/scripts/mock-dapp-multi/initialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { PublicKey } from "@solana/web3.js";
import { SYSTEM_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/native/system";

import { DappPDA } from "../../tests/mock-dapp-multi/setup";
import { connection, mockDappProgram, wallet } from "..";
import { TxnHelpers } from "../utils/transaction";

const args = process.argv.slice(2);
if (args.length != 1) throw new Error("Invalid arguments");

const xcallKey = new PublicKey(args[0]);
let txnHelpers = new TxnHelpers(connection, wallet.payer);

const initializeContract = async () => {
let dappConfig = await mockDappProgram.account.config.fetchNullable(
DappPDA.config().pda
);
if (!dappConfig) {
return await mockDappProgram.methods
.initialize(xcallKey)
.signers([wallet.payer])
.accountsStrict({
sender: wallet.publicKey,
authority: DappPDA.authority().pda,
systemProgram: SYSTEM_PROGRAM_ID,
config: DappPDA.config().pda,
})
.rpc();
}
};

initializeContract()
.then(async (res) => {
console.log("Contract initializing");
if (res) await txnHelpers.logParsedTx(res);
console.log("Contract initialized successfully");
})
.catch((err) => {
console.log(err);
});
38 changes: 38 additions & 0 deletions contracts/solana/scripts/mock-dapp-multi/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { PublicKey } from "@solana/web3.js";

import { mockDappProgram } from "..";

export class DappPDA {
constructor() {}

static config() {
let [pda, bump] = PublicKey.findProgramAddressSync(
[Buffer.from("config")],
mockDappProgram.programId
);

return { bump, pda };
}

static connections(networkId: string) {
const buffer1 = Buffer.from("connections");
const buffer2 = Buffer.from(networkId);
const seed = [buffer1, buffer2];

const [pda, bump] = PublicKey.findProgramAddressSync(
seed,
mockDappProgram.programId
);

return { pda, bump };
}

static authority() {
let [pda, bump] = PublicKey.findProgramAddressSync(
[Buffer.from("dapp_authority")],
mockDappProgram.programId
);

return { bump, pda };
}
}
37 changes: 37 additions & 0 deletions contracts/solana/scripts/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fs from "fs";
import { createHash } from "crypto";
import { Keypair, PublicKey } from "@solana/web3.js";

export function loadKeypairFromFile(path: string): Keypair {
return Keypair.fromSecretKey(
Buffer.from(JSON.parse(fs.readFileSync(path, "utf-8")))
);
}

export const sleep = (seconds: number) => {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
};

export const hash = (message: Uint8Array) => {
return createHash("sha256").update(message).digest("hex");
};

export const uint128ToArray = (num: any) => {
if (typeof num === "string" || typeof num === "number") {
num = BigInt(num);
} else if (!(num instanceof BigInt)) {
throw new Error("Input must be a BigInt or convertible to a BigInt.");
}

let buffer = new ArrayBuffer(16);
let view = new DataView(buffer);

view.setBigUint64(0, num >> BigInt(64), false);
view.setBigUint64(8, num & BigInt("0xFFFFFFFFFFFFFFFF"), false);

return new Uint8Array(buffer);
};

export const SYSVAR_INSTRUCTIONS_ID = new PublicKey(
"Sysvar1nstructions1111111111111111111111111"
);
Loading

0 comments on commit e96c55c

Please sign in to comment.