#chain #solana #transfer #infrastructure #value #programs #debridge

debridge-solana-sdk

debridge-solana-sdk 允许您轻松快速地将您的 Solana 程序连接到 debridge 基础设施。Debridge 协议能够实现不同区块链之间任意消息和价值的去中心化转移。

2 个稳定版本

1.0.1 2023 年 7 月 4 日
1.0.0 2023 年 6 月 29 日

#74 in #infrastructure

LGPL-3.0

82KB
1.5K SLoC

debridge-solana-sdk

关于

debridge-solana-sdk 允许您轻松快速地将您的 Solana 程序连接到 debridge 基础设施。Debridge 协议能够实现不同区块链之间任意消息和价值的去中心化转移。有关更多信息,请参阅 Debridge 文档 docs

项目结构

  • 在 git 仓库的根目录下,您可以找到用于集成 debridge 的辅助方法
  • example-program 文件夹中,您可以找到具有不同用例的示例程序

创建一个简单的消息和价值传输到其他链

此示例演示了如何使用 debridge-solana-sdk crate 向任何支持的链发送一些代币和消息

使用 Anchor 创建新程序

Debridge-solana-sdk 不依赖于任何 solana 框架,如 Anchor,可以连接到任何 Solana 程序。但为了简化示例,我们将使用用 Anchor 构建的示例程序。

anchor init send-via-debridge

添加 debridge-solana-sdk crate

下一步是将 debridge-solana-sdk crate 添加到程序依赖项

cargo add --git ssh://[email protected]/debridge-finance/debridge-solana-sdk.git debridge-solana-sdk

创建一个简单的使用 Debridge 协议的智能合约

./send-via-debridge/programs/src/lib.rs 中添加简单智能合约的代码,该合约使用 debridge solana 程序将代币和消息传输到其他链

use anchor_lang::prelude::*;

declare_id!("3botMWU4s1Lcs4Q2wQBkZqsCW1vc3N9H9tY9SZYVs5vZ");

#[program]
pub mod send_via_debridge {
    use debridge_solana_sdk::prelude::*;

    use super::*;

    pub fn send_via_debridge(ctx: Context<SendViaDebridge>) -> Result<()> {
        invoke_debridge_send(
            SendIx {
                target_chain_id: chain_ids::POLYGON_CHAIN_ID,
                receiver: hex::decode("bd1e72155Ce24E57D0A026e0F7420D6559A7e651").unwrap(),
                is_use_asset_fee: false,
                amount: 1000,
                submission_params: None,
                referral_code: None,
            },
            ctx.remaining_accounts,
        )
        .map_err(|err| err.into())
    }
}

#[derive(Accounts)]
pub struct SendViaDebridge {}

更多

要查看程序示例,请访问

构建和部署示例合约

使用 anchor 构建程序

cd example_program; anchor build

部署程序

solana program deploy --program-id target/deploy/send_via_debridge-keypair.json ./target/deploy/send_via_debridge.so

编写和运行测试

添加文件 ./send-via-debridge/tests/send-via-debridge.ts,包含以下测试用例

import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { SendViaDebridge } from "../target/types/send_via_debridge";
import { AccountMeta, PublicKey, Signer } from "@solana/web3.js";

function accountsToMeta() {
  const result: AccountMeta[] = [
    {
      isSigner: false,
      isWritable: true,
      pubkey: new PublicKey("6SW1N9Rq2TqT3uQCD4F5zwtTTSFSarZmfyrk829SzsBX"),
    },
    {
      isSigner: false,
      isWritable: true,
      pubkey: new PublicKey("So11111111111111111111111111111111111111112"),
    },
    {
      isSigner: false,
      isWritable: true,
      pubkey: new PublicKey("8gjgVkHXTttCoSGGtzucFkJUWujQ8pgWuvnHCLSN7i3o"),
    },
    {
      isSigner: false,
      isWritable: false,
      pubkey: new PublicKey("7FmGdfJfDrrM6P68y7jijjj4xU9rH3hsUK2Kyp54iJUx"),
    },
    {
      isSigner: false,
      isWritable: false,
      pubkey: new PublicKey("8L81QZBfwA6Xi9zd49fyUfMRWJBCAxiUxd6jGHPnu1BQ"),
    },
    {
      isSigner: false,
      isWritable: false,
      pubkey: new PublicKey("DeSetTwWhjZq6Pz9Kfdo1KoS5NqtsM6G8ERbX4SSCSft"),
    },
    {
      isSigner: false,
      isWritable: false,
      pubkey: new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"),
    },
    {
      isSigner: false,
      isWritable: true,
      pubkey: new PublicKey("CcjkxrCJvfXrmds78hwCnovkdmTgE12wqojiVLrtW1qn"),
    },
    {
      isSigner: false,
      isWritable: true,
      pubkey: new PublicKey("5MgAaNomDg4Y88v7gJ7LSWAyoLpDfcfbXZGQQnFddjJT"),
    },
    {
      isSigner: false,
      isWritable: true,
      pubkey: new PublicKey("2LKQceMRwfJNZovtSbsHmfszDYM5kTZHajFry2nqD2pi"),
    },
    {
      isSigner: false,
      isWritable: true,
      pubkey: new PublicKey("BzoSTqbp8vZ54Baq2K4LTwGnC8fYvKiEFQDNxdEDnosG"),
    },
    {
      isSigner: false,
      isWritable: false,
      pubkey: new PublicKey("11111111111111111111111111111111"),
    },
    {
      isSigner: false,
      isWritable: true,
      pubkey: new PublicKey("dPLMV1ky3H61yRGFfNC6AYmzBePhsdes9oNZ7chPbYW"),
    },
    {
      isSigner: false,
      isWritable: true,
      pubkey: new PublicKey("2cU8vjsMnRcusX1WdwZy1AwCLrUWbDw6frnk3XDz3VVK"),
    },
    {
      isSigner: true,
      isWritable: true,
      pubkey: new PublicKey("FsiBNh2KcPrjZFMF7EBCWpUpAo95DfrMXB2U2XrqSFWF"),
    },
    {
      isSigner: false,
      isWritable: false,
      pubkey: new PublicKey("4kQYWVy6Vu8YUXVp5BgQC12ZX1HLRUfkK3bLzBFFjnNW"),
    },
    {
      isSigner: false,
      isWritable: false,
      pubkey: new PublicKey("APMGxdbtubfWLQUACsN2yv2pxkvAgWwuxBe8ohFYoB37"),
    },
    {
      isSigner: false,
      isWritable: false,
      pubkey: new PublicKey("DEbrdGj3HsRsAzx6uH4MKyREKxVAfBydijLUF3ygsFfh"),
    },
  ];

  return result;
}

describe("send-via-debridge", async () => {
  anchor.setProvider(anchor.AnchorProvider.env());

  const program = anchor.workspace.SendViaDebridge as Program<SendViaDebridge>;

  it("Send via Debridge!", async () => {
    const builder = await program.methods
      .sendViaDebridge()
      .signers([program.provider.wallet.payer]);
    builder.remainingAccounts([...accountsToMeta()]);

    let tx = await builder.rpc();

    console.log("Your transaction signature", tx);
  });
});

然后在 ./send-via-debridge/Anchor.toml 中将集群更改为 mainnet 以使用 Debridge 生产合约

[provider]
cluster = "mainnet"

要运行测试,请使用构建和部署合约

anchor test

要单独运行测试,请使用

anchor test --skip-build --skip-deploy

其他示例

您可以在路径 ./send-via-debridge/exampleprogram 中的 Solana 程序示例中找到 SDK 的使用示例。

依赖项

~18-26MB
~446K SLoC