#sdk #cardano #marlowe

已下架 marlowe_rust_sdk_test

Marlowe运行时REST API的SDK

1个不稳定版本

0.0.5 2024年2月26日

#5 in #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
~255K SLoC