Skip to content

Commit

Permalink
solana interact
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Aug 19, 2024
1 parent 5eb16d7 commit 7b76520
Show file tree
Hide file tree
Showing 10 changed files with 605 additions and 884 deletions.
19 changes: 2 additions & 17 deletions universal/hello/contracts/Hello.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ import {RevertContext} from "@zetachain/protocol-contracts/contracts/Revert.sol"
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/UniversalContract.sol";

contract Hello is UniversalContract {
event ContextData(
bytes origin,
address sender,
uint256 chainID,
address msgSender,
string message
);

event Foo(string message);
event HelloEvent(string message);

event ContextDataRevert(RevertContext revertContext);

Expand All @@ -25,18 +17,11 @@ contract Hello is UniversalContract {
uint256 amount,
bytes calldata message
) external override {
emit Foo("Hello from Hello contract");
string memory decodedMessage;
if (message.length > 0) {
decodedMessage = abi.decode(message, (string));
}
emit ContextData(
context.origin,
context.sender,
context.chainID,
msg.sender,
decodedMessage
);
emit HelloEvent("Hello from a universal app");
}

function onRevert(RevertContext calldata revertContext) external override {
Expand Down
1 change: 1 addition & 0 deletions universal/hello/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./tasks/interact";
import "./tasks/deploy";
import "./tasks/solana/interact";
import "@zetachain/localnet/tasks";
import "@nomicfoundation/hardhat-toolbox";
import "@zetachain/toolkit/tasks";
Expand Down
8 changes: 7 additions & 1 deletion universal/hello/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,11 @@
"typechain": "^8.1.0",
"typescript": ">=4.5.0"
},
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72",
"dependencies": {
"@coral-xyz/anchor": "0.30.0",
"@solana-developers/helpers": "^2.4.0",
"@solana/spl-memo": "^0.2.5",
"@solana/web3.js": "^1.95.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import * as anchor from "@coral-xyz/anchor";
import { Program, web3, BN } from "@coral-xyz/anchor";
import Gateway from "./gateway.json";
import Gateway_IDL from "./gateway.json";
import { getKeypairFromFile } from "@solana-developers/helpers";

const DEVNET = "https://api.devnet.solana.com";
const PROGRAM_ID = "2kJndCL9NBR36ySiQ4bmArs4YgWQu67LmCDfLzk5Gb7s";
const SEED = "meta";
const MEMO = "0x3345664691f65614Ec3b76d451B1A005CEfF3b16";
const ID_JSON_PATH = "~/.config/solana/id.json";
const AMOUNT = 0.0001;

const main = async () => {
const keypair = await getKeypairFromFile(ID_JSON_PATH);
export const deposit = async (args: {
amount: number;
memo: string;
api: string;
idPath: string;
}) => {
const keypair = await getKeypairFromFile(args.idPath);
const wallet = new anchor.Wallet(keypair);

const connection = new anchor.web3.Connection(DEVNET);
const connection = new anchor.web3.Connection(args.api);
const provider = new anchor.AnchorProvider(
connection,
wallet,
anchor.AnchorProvider.defaultOptions()
);
anchor.setProvider(provider);

const programId = new web3.PublicKey(PROGRAM_ID);
const gatewayProgram = new Program(Gateway as anchor.Idl, provider);
const programId = new web3.PublicKey(Gateway_IDL.address);
const gatewayProgram = new Program(Gateway_IDL as anchor.Idl, provider);

const seeds = [Buffer.from(SEED, "utf-8")];
const [pdaAccount] = web3.PublicKey.findProgramAddressSync(seeds, programId);

const depositAmount = new BN(web3.LAMPORTS_PER_SOL * AMOUNT);
const memo = Buffer.from(MEMO);
const depositAmount = new BN(web3.LAMPORTS_PER_SOL * args.amount);
const memo = Buffer.from(args.memo);

try {
const tx = await gatewayProgram.methods
Expand All @@ -53,5 +53,3 @@ const main = async () => {
console.error("Transaction failed:", error);
}
};

main();
File renamed without changes.
14 changes: 14 additions & 0 deletions universal/hello/tasks/solana/interact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { task } from "hardhat/config";
import type { HardhatRuntimeEnvironment } from "hardhat/types";
import { deposit } from "./deposit";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const { amount, memo, api, idPath } = args;
await deposit({ amount, memo, api, idPath });
};

task("interact-solana", "", main)
.addParam("amount", "Amount of SOL to deposit")
.addParam("memo", "Memo")
.addOptionalParam("api", "Solana API", "https://api.devnet.solana.com")
.addOptionalParam("idPath", "Path to id.json", "~/.config/solana/id.json");
Loading

0 comments on commit 7b76520

Please sign in to comment.