Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: ops script to initialize gateway program #20

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ wallet = "~/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
#test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/ops.ts"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/ops.ts"

90 changes: 90 additions & 0 deletions tests/ops.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// this is for ops on devnet/mainnet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear what ops means, if this is to deploy the gateway, it should be reflected in the name of the script?

// uncomment Anchor.toml [test] to run this script
// #test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/ops.ts"
Comment on lines +2 to +3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear what does this mean. We should put instruction to run the script in the readme


import * as anchor from "@coral-xyz/anchor";
import {Program, web3} from "@coral-xyz/anchor";
import {Gateway} from "../target/types/gateway";
import {expect} from "chai";
import {bufferToHex} from "ethereumjs-util";
import {getAccount} from "@solana/spl-token";

const programId = new web3.PublicKey("ZETAjseVjuFsxdRxo6MmTCvqFwb3ZHUx56Co3vCmGis");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move all hardcoded values to constants in a separate file, and add the ability of overriding them via env vars?


(async ()=> {
console.log("=====================================");
console.log("BEGIN OPS ON DEVNET........");

const provider = anchor.AnchorProvider.local("https://api.devnet.solana.com");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment above mention it c mainnet but here the URL is devnet. I think we should use an environment variable for it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, same as mentioned before: we could group all constants in const.ts, adding the ability to override them via env var.

anchor.setProvider(provider);
console.log("wallet address:", provider.publicKey.toBase58());
const conn = provider.connection;
const bal = await conn.getBalance(provider.publicKey);
console.log("balance:", bal);
const wallet = anchor.workspace.Gateway.provider.wallet.payer;
console.log("payer address:", wallet.publicKey.toBase58());

const acctInfo = await conn.getAccountInfo(programId);
console.log("acctInfo of gateway program :", acctInfo);

const seeds = [Buffer.from("meta", "utf-8")];
const [pdaAccount] = anchor.web3.PublicKey.findProgramAddressSync(
seeds,
programId,
);
console.log("pdaAccount:", pdaAccount.toBase58());

const gatewayProgram = anchor.workspace.Gateway as Program<Gateway>;

console.log("gateway provider", gatewayProgram.provider);
const tssAddress = "0x8531a5ab847ff5b22d855633c25ed1da3255247e";
// translate the above hex string into an array of number into tssAddress
const tssAddressArray = tssAddress.slice(2).match(/.{1,2}/g).map(byte => parseInt(byte, 16));
const chain_id = 901;
const chain_id_bn = new anchor.BN(chain_id);

console.log("tssAddressArray:", tssAddressArray);
console.log("chain_id_bn:", chain_id_bn.toNumber());

// Uncomment the following to initialize the gateway program;
// can only be initialized once
// try {
// const inst = await gatewayProgram.methods.initialize(tssAddressArray,chain_id_bn).transaction();
// console.log("initialize inst:", inst);
// const tx = await anchor.web3.sendAndConfirmTransaction(conn, inst, [wallet]);
// console.log("tx:", tx);
// } catch (err) {
// console.log("initialize err:", err);
// }
Comment on lines +49 to +58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rather have argument or using environment variable to customize script logic


const pdaAccountInfo = await conn.getAccountInfo(pdaAccount);
console.log("pdaAccountInfoData:", pdaAccountInfo.data);
// const pdaAccountData = await gatewayProgram.account.pda.fetch(pdaAccount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// const pdaAccountData = await gatewayProgram.account.pda.fetch(pdaAccount);

try {
console.log("fetching pda account data....", pdaAccount.toBase58());
// const data = await gatewayProgram.account.pda.fetch(pdaAccount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// const data = await gatewayProgram.account.pda.fetch(pdaAccount);

const ainfo = await gatewayProgram.account.pda.fetch(pdaAccount)
console.log(`pda account data: nonce ${ainfo.nonce}`);
const hexAddr = bufferToHex(Buffer.from(ainfo.tssAddress));
console.log(`pda account data: tss address ${hexAddr}`);
console.log(`authority: ${ainfo.authority.toBase58()}`);
console.log(`depositPaused: ${ainfo.depositPaused}`);
console.log(`chain_id: ${ainfo.chainId}`);

// console.log(`pda data:`, ainfo);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// console.log(`pda data:`, ainfo);

} catch(e) {
console.log("error:", e);
}
// console.log(`pda account data: nonce ${pdaAccountData.nonce}`);
// const hexAddr = bufferToHex(Buffer.from(pdaAccountData.tssAddress));
// console.log(`pda account data: tss address ${hexAddr}`);
// console.log(`authority: ${pdaAccountData.authority.toBase58()}`);
Comment on lines +78 to +81
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// console.log(`pda account data: nonce ${pdaAccountData.nonce}`);
// const hexAddr = bufferToHex(Buffer.from(pdaAccountData.tssAddress));
// console.log(`pda account data: tss address ${hexAddr}`);
// console.log(`authority: ${pdaAccountData.authority.toBase58()}`);



console.log("END OPS ON DEVNET........")
console.log("=====================================");
})();




Loading