Skip to content

Commit

Permalink
merged main
Browse files Browse the repository at this point in the history
  • Loading branch information
brewmaster012 committed Oct 1, 2024
2 parents bc2a7ca + 13e04d9 commit 402ebdf
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 22 deletions.
35 changes: 20 additions & 15 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,41 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable

- name: Install Node.js, Solana CLI and Anchor
- name: Install Node.js, Solana CLI, and Anchor
uses: metadaoproject/setup-anchor@v2
with:
anchor-version: '0.30.0'
with:
anchor-version: '0.30.0'
solana-cli-version: '1.18.15'
node-version: '21.0.0'

- name: Setup Node.js for NPM Publish
uses: actions/setup-node@v4
with:
node-version: '21.0.0'
registry-url: 'https://registry.npmjs.org'
auth-token: ${{ secrets.NPM_TOKEN }}

- name: Build
run: anchor build

- name: Prepare IDL Files
run: |
mv target/idl ./
run: mv target/idl ./

- name: Determine NPM Tag
id: determine-npm-tag
run: |
VERSION_TAG=${GITHUB_REF#refs/tags/v}
VERSION_TAG=${{ github.ref_name }}
if [[ $VERSION_TAG == *"-"* ]]; then
echo "NPM_TAG=${VERSION_TAG#*-}" >> "$GITHUB_ENV"
NPM_TAG=${VERSION_TAG#*-}
else
echo "NPM_TAG=latest" >> "$GITHUB_ENV"
NPM_TAG=latest
fi
env:
GITHUB_REF: ${{ github.ref }}
echo "NPM_TAG=$NPM_TAG" >> "$GITHUB_OUTPUT"
- name: Publish to NPM
run: |
yarn publish --access public --new-version ${GITHUB_REF#refs/tags/v} --tag ${{
steps.determine-npm-tag.outputs.NPM_TAG }} --no-git-tag-version
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_REF: ${{ github.ref }}
yarn publish \
--access public \
--new-version "${{ github.ref_name }}" \
--tag "${{ steps.determine-npm-tag.outputs.NPM_TAG }}" \
--no-git-tag-version
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Meta Protocol, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@zetachain/protocol-contracts-solana",
"private": false,
"license": "MIT",
"version": "0.0.0-set-on-publish",
"description": "Package contains IDL files for the Solana Gateway program, enabling cross-chain functionality with ZetaChain",
"files": [
Expand Down
38 changes: 33 additions & 5 deletions programs/protocol-contracts-solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,35 @@ pub mod gateway {
Ok(())
}

pub fn deposit(ctx: Context<Deposit>, amount: u64, memo: Vec<u8>) -> Result<()> {
require!(memo.len() >= 20, Errors::MemoLengthTooShort);
require!(memo.len() <= 512, Errors::MemoLengthExceeded);
pub fn deposit(ctx: Context<Deposit>, amount: u64, receiver: [u8; 20]) -> Result<()> {
let pda = &mut ctx.accounts.pda;
require!(!pda.deposit_paused, Errors::DepositPaused);

let cpi_context = CpiContext::new(
ctx.accounts.system_program.to_account_info(),
system_program::Transfer {
from: ctx.accounts.signer.to_account_info().clone(),
to: ctx.accounts.pda.to_account_info().clone(),
},
);
system_program::transfer(cpi_context, amount)?;
msg!(
"{:?} deposits {:?} lamports to PDA; receiver {:?}",
ctx.accounts.signer.key(),
amount,
receiver,
);

Ok(())
}

pub fn deposit_and_call(
ctx: Context<Deposit>,
amount: u64,
receiver: [u8; 20],
message: Vec<u8>,
) -> Result<()> {
require!(message.len() <= 512, Errors::MemoLengthExceeded);

let pda = &mut ctx.accounts.pda;
require!(!pda.deposit_paused, Errors::DepositPaused);
Expand All @@ -101,9 +127,11 @@ pub mod gateway {
);
system_program::transfer(cpi_context, amount)?;
msg!(
"{:?} deposits {:?} lamports to PDA",
"{:?} deposits {:?} lamports to PDA and call contract {:?} with message {:?}",
ctx.accounts.signer.key(),
amount
amount,
receiver,
message,
);

Ok(())
Expand Down
17 changes: 15 additions & 2 deletions tests/protocol-contracts-solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ describe("some tests", () => {
const chain_id = 111111;
const chain_id_bn = new anchor.BN(chain_id);

let seeds = [Buffer.from("meta", "utf-8")];
[pdaAccount] = anchor.web3.PublicKey.findProgramAddressSync(
seeds,
gatewayProgram.programId,
);

it("Initializes the program", async () => {
await gatewayProgram.methods.initialize(tssAddress, chain_id_bn).rpc();

Expand Down Expand Up @@ -227,7 +233,7 @@ describe("some tests", () => {
});

it("deposit and withdraw 0.5 SOL from Gateway with ECDSA signature", async () => {
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000_000), address).accounts({pda: pdaAccount}).rpc();
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000_000), Array.from(address)).accounts({pda: pdaAccount}).rpc();
// const transaction = new anchor.web3.Transaction();
// transaction.add(
// web3.SystemProgram.transfer({
Expand Down Expand Up @@ -280,6 +286,13 @@ describe("some tests", () => {
expect(bal3).to.be.gte(500_000_000);
})

it("deposit and call", async () => {
let bal1 = await conn.getBalance(pdaAccount);
await gatewayProgram.methods.depositAndCall(new anchor.BN(1_000_000_000), Array.from(address), Buffer.from("hello", "utf-8")).accounts({pda: pdaAccount}).rpc();
let bal2 = await conn.getBalance(pdaAccount);
expect(bal2-bal1).to.be.gte(1_000_000_000);
})

it("update TSS address", async () => {
const newTss = new Uint8Array(20);
randomFillSync(newTss);
Expand Down Expand Up @@ -313,7 +326,7 @@ describe("some tests", () => {

// now try deposit, should fail
try {
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000), address).accounts({pda: pdaAccount}).rpc();
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000), Array.from(address)).accounts({pda: pdaAccount}).rpc();
} catch (err) {
console.log("Error message: ", err.message);
expect(err).to.be.instanceof(anchor.AnchorError);
Expand Down

0 comments on commit 402ebdf

Please sign in to comment.