#cardano #marlowe #sdk

marlowe_rust_sdk

Marlowe 运行时 REST API 的 SDK

1 个不稳定版本

0.0.5 2024 年 2 月 27 日

#3#marlowe

Apache-2.0

265KB
5.5K SLoC

marlowe-rust-sdk

用法

配置初始化

let base_path = String::from("https://marlowe-runtime-preprod-web.scdev.aws.iohkdev.io");
let config = init_client(base_path);

合约创建示例

async fn test_create_swap_contract(config: &Configuration) {
    let swap_contract: Contract = Contract::When(When {
        timeout: 2556057600000,
        timeout_continuation: Box::new(Contract::Close(Close::Close)),
        when: vec![Case::Then(CaseThen {
            case: Box::new(Action::DepositAction(DepositAction {
                deposits: Box::new(Value::TokenAmount(3000000)),
                into_account: Box::new(Party::PartyRoleName(PartyRoleName {
                    role_token: "provider".to_owned(),
                })),
                of_token: Box::new(Token {
                    currency_symbol: "".to_owned(),
                    token_name: "".to_owned(),
                }),
                party: Box::new(Party::PartyRoleName(PartyRoleName {
                    role_token: "provider".to_owned(),
                })),
            })),
            then: Box::new(Contract::When(When {
                timeout: 2556057600000,
                timeout_continuation: Box::new(Contract::Pay(Pay {
                    from_account: Box::new(Party::PartyRoleName(PartyRoleName {
                        role_token: "provider".to_owned(),
                    })),
                    pay: Box::new(Value::TokenAmount(3_000_000)),
                    token: Box::new(Token {
                        currency_symbol: "".to_owned(),
                        token_name: "".to_owned(),
                    }),
                    to: Box::new(Payee::PayToParty(PayToParty {
                        party: Box::new(Party::PartyRoleName(PartyRoleName {
                            role_token: "provider".to_owned(),
                        })),
                    })),
                    then: Box::new(Contract::Close(Close::Close)),
                })),
                when: vec![Case::Then(CaseThen {
                    case: Box::new(Action::DepositAction(DepositAction {
                        deposits: Box::new(Value::TokenAmount(3_000_000)),
                        into_account: Box::new(Party::PartyRoleName(PartyRoleName {
                            role_token: "swapper".to_owned(),
                        })),
                        of_token: Box::new(Token {
                            currency_symbol: "".to_owned(),
                            token_name: "".to_owned(),
                        }),
                        party: Box::new(Party::PartyRoleName(PartyRoleName {
                            role_token: "swapper".to_owned(),
                        })),
                    })),
                    then: Box::new(Contract::Pay(Pay {
                        pay: Box::new(Value::TokenAmount(3_000_000)),
                        token: Box::new(Token {
                            currency_symbol: "".to_owned(),
                            token_name: "".to_owned(),
                        }),
                        to: Box::new(Payee::PayToParty(PayToParty {
                            party: Box::new(Party::PartyRoleName(PartyRoleName {
                                role_token: "swapper".to_owned(),
                            })),
                        })),
                        from_account: Box::new(Party::PartyRoleName(PartyRoleName {
                            role_token: "provider".to_owned(),
                        })),
                        then: Box::new(Contract::Pay(Pay {
                            from_account: Box::new(Party::PartyRoleName(PartyRoleName {
                                role_token: "swapper".to_owned(),
                            })),
                            pay: Box::new(Value::TokenAmount(3_000_000)),
                            then: Box::new(Contract::Close(Close::Close)),
                            to: Box::new(Payee::PayToParty(PayToParty {
                                party: Box::new(Party::PartyRoleName(PartyRoleName {
                                    role_token: "provider".to_owned(),
                                })),
                            })),
                            token: Box::new(Token {
                                currency_symbol: "".to_owned(),
                                token_name: "".to_owned(),
                            }),
                        })),
                    })),
                })],
            })),
        })],
    });
    let contract: Box<PostContractsRequestContract> =
        Box::new(PostContractsRequestContract::Contract(swap_contract));
    let metadata = HashMap::new();
    let min_utx_o_deposit = Some(5_000_000);
    let mut roles_hash_map: HashMap<String, RoleTokenConfig> = HashMap::new();
    roles_hash_map.insert(
        "swapper".to_owned(),
        RoleTokenConfig::Address(
            // insert your address here
            String::from("<your address>")
        ),
    );
    roles_hash_map.insert(
        "provider".to_owned(),
        RoleTokenConfig::Address(
            // insert your address here
            String::from("<your address>"),
        ),
    );

    let roles: Box<RolesConfig> = Box::new(RolesConfig::AdditionalRolesConfigProp(roles_hash_map));
    let contract_request: PostContractsRequest = PostContractsRequest {
        contract,
        metadata,
        min_utx_o_deposit,
        roles: Some(roles),
        tags: HashMap::new(),
        version: marlowe_rust_sdk::models::MarloweVersion::V1,
        thread_token_name: None,
    };
    let maybe_contract = marlowe_rust_sdk::create_contract(
        config,
        "<your address>",
        None,
        None,
        None,
        contract_request,
    )
    .await;
    match maybe_contract {
        Ok(contract) => {
            println!("{:#?}", contract);
            println!("Contract created successfully");
        }
        Err(error) => {
            println!("{:#?}", error);
            println!("An error ocurred creating your swap contract");
        }
    }
}

SDK 结构

  • src/lib.rs 文件中为自动生成的端点交互提供的包装函数。
  • 初始化客户端配置的函数。

SDK 开发

  • 从 Marlowe 运行时 REST API 获取 OpenAPI 规范。
  • 手动格式化 OpenAPI 规范(即,将 oneOf 字段命名为相应名称,使用旧版本来帮助您命名模式)。
  • 将规范 OpenAPI 的版本设置为 3.0.0,该工具可以安全处理。
  • 最复杂的问题模式之一是 MarloweState,请使用旧版本来帮助自己。
  • 使用 openapi-generator-cli 工具验证规范并生成代码。
  • 运行 cargo fmt 格式化代码。
  • 调整具有 oneOf 的模式。目前(openapi-generator-cli v7.1.0)生成器无法从 oneOf 模式创建枚举,您需要手动完成。
  • 一切准备就绪后,运行 cargo test 确保获取器函数正常工作。

依赖项

~4–19MB
~254K SLoC