-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c5d56a
commit e96c55c
Showing
19 changed files
with
799 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
contracts/solana/scripts/centralized-connection/initialize.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
contracts/solana/scripts/mock-dapp-multi/add-connection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); |
Oops, something went wrong.