From 91c924079eec98c21a7d7c984f48ffa171e3ca8c Mon Sep 17 00:00:00 2001 From: Edwin Steven Guayacan <80716239+EdwinGuayacan@users.noreply.github.com> Date: Wed, 20 Sep 2023 15:32:07 -0500 Subject: [PATCH 1/2] Soroban preview 11 support (#334) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add support for Soroban preview 11 Co-authored-by: David Roldán * Update Stellar Base version * Set harden-runner egress-policy to audit * Add new allowed-endpoint in CI workflow: actions-results-receiver-production.githubapp.com:443 * Keep harden-runner egress-policy to audit * Add requested changes Co-authored-by: David Roldán * Remove warnings * Fix type --------- Co-authored-by: David Roldán Co-authored-by: Miguel Nieto A <39246879+miguelnietoa@users.noreply.github.com> --- .github/workflows/cd.yml | 13 +-- .github/workflows/ci.yml | 15 +-- .github/workflows/scorecards.yml | 21 ++-- .../soroban/bump_footprint_expiration.md | 25 ++--- docs/examples/soroban/create_contract.md | 11 ++- .../soroban/invoke_contract_functions.md | 99 ++++++++++--------- docs/examples/soroban/restore_footprint.md | 18 ++-- docs/examples/soroban/upload_contract_code.md | 4 +- lib/tx_build/config_setting_id.ex | 48 +++++++++ lib/tx_build/host_function.ex | 8 +- ...ct_function.ex => invoke_contract_args.ex} | 22 +++-- lib/tx_build/ledger/config_setting.ex | 30 ++++++ lib/tx_build/ledger/contract_code.ex | 33 ++----- lib/tx_build/ledger/contract_data.ex | 27 ++--- lib/tx_build/ledger/expiration.ex | 28 ++++++ lib/tx_build/ledger_key.ex | 34 +++++++ lib/tx_build/sc_error.ex | 24 +++-- lib/tx_build/sc_vec.ex | 10 +- lib/tx_build/soroban_address_credentials.ex | 26 ++--- lib/tx_build/soroban_authorization_entry.ex | 18 ++-- lib/tx_build/soroban_authorized_function.ex | 12 +-- lib/tx_build/soroban_resources.ex | 23 ++--- mix.exs | 2 +- mix.lock | 5 +- test/support/fixtures/xdr.ex | 2 + test/support/fixtures/xdr/ledger.ex | 26 ++++- test/support/xdr_fixtures.ex | 50 ++++------ test/tx_build/config_setting_id_test.exs | 32 ++++++ test/tx_build/config_setting_test.exs | 32 ++++++ test/tx_build/default_test.exs | 4 +- test/tx_build/expiration_test.exs | 30 ++++++ ...id_preimage_soroban_authorization_test.exs | 7 +- test/tx_build/hash_id_preimage_test.exs | 25 ++--- test/tx_build/host_function_test.exs | 20 ++-- test/tx_build/invoke_host_function_test.exs | 21 ++-- test/tx_build/ledger/contract_code_test.exs | 30 +++--- test/tx_build/ledger/contract_data_test.exs | 48 +++------ test/tx_build/ledger_footprint_test.exs | 12 +-- test/tx_build/ledger_key_test.exs | 70 +++++++++++-- test/tx_build/sc_error_test.exs | 28 +++--- test/tx_build/sc_val_test.exs | 4 +- test/tx_build/sc_vec_test.exs | 47 +++++++++ test/tx_build/soroba_tarsaction_data_test.exs | 19 ++-- .../soroban_address_credentials_test.exs | 49 +++++---- .../soroban_authorization_entry_test.exs | 31 +++--- ...oban_authorized_contract_function_test.exs | 34 +++---- .../soroban_authorized_function_test.exs | 11 +-- .../soroban_authorized_invocation_test.exs | 11 +-- test/tx_build/soroban_credentials_test.exs | 23 ++--- test/tx_build/soroban_resources_test.exs | 46 +++------ test/tx_build/validations_test.exs | 37 ++++++- 51 files changed, 799 insertions(+), 506 deletions(-) create mode 100644 lib/tx_build/config_setting_id.ex rename lib/tx_build/{soroban_authorized_contract_function.ex => invoke_contract_args.ex} (77%) create mode 100644 lib/tx_build/ledger/config_setting.ex create mode 100644 lib/tx_build/ledger/expiration.ex create mode 100644 test/tx_build/config_setting_id_test.exs create mode 100644 test/tx_build/config_setting_test.exs create mode 100644 test/tx_build/expiration_test.exs create mode 100644 test/tx_build/sc_vec_test.exs diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 816d58d..6b300c7 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -23,12 +23,13 @@ jobs: uses: step-security/harden-runner@cba0d00b1fc9a034e1e642ea0f1103c282990604 # v2.5.0 with: disable-sudo: true - egress-policy: block - allowed-endpoints: > - github.com:443 - hex.pm:443 - repo.hex.pm:443 - builds.hex.pm:443 + egress-policy: audit + # egress-policy: block + # allowed-endpoints: > + # github.com:443 + # hex.pm:443 + # repo.hex.pm:443 + # builds.hex.pm:443 - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 - uses: erlef/setup-elixir@61e01a43a562a89bfc54c7f9a378ff67b03e4a21 # v1.16.0 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5513e77..dac43e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,15 +25,16 @@ jobs: uses: step-security/harden-runner@cba0d00b1fc9a034e1e642ea0f1103c282990604 # v2.5.0 with: disable-sudo: true - egress-policy: block - allowed-endpoints: > - coveralls.io:443 - github.com:443 - repo.hex.pm:443 - builds.hex.pm:443 + egress-policy: audit + # egress-policy: block + # allowed-endpoints: > + # coveralls.io:443 + # github.com:443 + # repo.hex.pm:443 + # builds.hex.pm:443 - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 - - uses: erlef/setup-elixir@61e01a43a562a89bfc54c7f9a378ff67b03e4a21 # v1.16.0 + - uses: erlef/setup-beam@61e01a43a562a89bfc54c7f9a378ff67b03e4a21 # v1.16.0 with: otp-version: ${{ matrix.otp }} elixir-version: ${{ matrix.elixir }} diff --git a/.github/workflows/scorecards.yml b/.github/workflows/scorecards.yml index 323fe44..11e0721 100644 --- a/.github/workflows/scorecards.yml +++ b/.github/workflows/scorecards.yml @@ -25,16 +25,17 @@ jobs: uses: step-security/harden-runner@cba0d00b1fc9a034e1e642ea0f1103c282990604 # v2.5.0 with: disable-sudo: true - egress-policy: block - allowed-endpoints: > - api.github.com:443 - api.osv.dev:443 - api.securityscorecards.dev:443 - bestpractices.coreinfrastructure.org:443 - *.sigstore.dev:443 - github.com:443 - sigstore-tuf-root.storage.googleapis.com:443 - oss-fuzz-build-logs.storage.googleapis.com:443 + egress-policy: audit + # egress-policy: block + # allowed-endpoints: > + # api.github.com:443 + # api.osv.dev:443 + # api.securityscorecards.dev:443 + # bestpractices.coreinfrastructure.org:443 + # *.sigstore.dev:443 + # github.com:443 + # sigstore-tuf-root.storage.googleapis.com:443 + # oss-fuzz-build-logs.storage.googleapis.com:443 - name: "Checkout code" uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 diff --git a/docs/examples/soroban/bump_footprint_expiration.md b/docs/examples/soroban/bump_footprint_expiration.md index e9f8d8a..2306ade 100644 --- a/docs/examples/soroban/bump_footprint_expiration.md +++ b/docs/examples/soroban/bump_footprint_expiration.md @@ -1,8 +1,10 @@ # Bump Footprint Expiration + The `BumpFootprintExpirationOp` operation is used to extend a contract data entry's lifetime. A contract instance, wasm hash, and data storage entry (persistent/instance/temporary) can expire, so you can use this bump operation to extend its lifetime. Read more about it: + - https://soroban.stellar.org/docs/fundamentals-and-concepts/state-expiration#bumpfootprintexpirationop - https://docs.rs/soroban-sdk/latest/soroban_sdk/storage/struct.Storage.html @@ -19,6 +21,7 @@ alias Stellar.TxBuild.{ Account, BaseFee, BumpFootprintExpiration, + LedgerFootprint, LedgerKey, SCAddress, SCVal, @@ -28,13 +31,15 @@ alias Stellar.TxBuild.{ SorobanTransactionData } -contract_address = "CAMGSYINVVL6WP3Q5WPNL7FS4GZP37TWV7MKIRQF5QMYLK3N2SW4P3RC" +alias Stellar.Horizon.Accounts +alias Stellar.KeyPair + +contract_address = "CCNVHP2UAOJAICTQUDSRVZDCB5OJKGQNOJFPOXINELWQHGX33EG34NV2" contract_sc_address = SCAddress.new(contract_address) key = SCVal.new(ledger_key_contract_instance: nil) keypair = - {public_key, _secret} = - Stellar.KeyPair.from_secret_seed("SCAVFA3PI3MJLTQNMXOUNBSEUOSY66YMG3T2KCQKLQBENNVLVKNPV3EK") + {public_key, _secret} = KeyPair.from_secret_seed("SCAVFA3PI3MJLTQNMXOUNBSEUOSY66YMG3T2KCQKLQBENNVLVKNPV3EK") contract_data = LedgerKey.new( @@ -42,13 +47,12 @@ contract_data = [ contract: contract_sc_address, key: key, - durability: :persistent, - body_type: :data_entry + durability: :persistent ]} ) -hash = Base.decode16!("067eb7ba419edd3e946e08eb17a81fbe1e850e690ed7692160875c2b65b45f21", case: :lower) -contract_code = LedgerKey.new({:contract_code, [hash: hash, body_type: :data_entry]}) +hash = Base.decode16!("d14f427a7d4ad78008e5f18a8d9ed7c9dcc02a868995064b9a88a5a684b86624", case: :lower) +contract_code = LedgerKey.new({:contract_code, [hash: hash]}) footprint = LedgerFootprint.new(read_only: [contract_data, contract_code]) soroban_data = @@ -56,8 +60,7 @@ soroban_data = footprint: footprint, instructions: 0, read_bytes: 0, - write_bytes: 0, - extended_meta_data_size_bytes: 0 + write_bytes: 0 ] |> SorobanResources.new() |> (&SorobanTransactionData.new(resources: &1, refundable_fee: 0)).() @@ -78,9 +81,9 @@ source_account # Simulate Transaction soroban_data = - "AAAAAAAAAAIAAAAGAAAAARhpYQ2tV+s/cO2e1fyy4bL9/nav2KRGBewZhatt1K3HAAAAFAAAAAEAAAAAAAAABxhpYQ2tV+s/cO2e1fyy4bL9/nav2KRGBewZhatt1K3HAAAAAAAAAAAAAAAAAAABLAAAAAAAAAJYAAAAAAAAAHY=" + "AAAAAAAAAAIAAAAGAAAAAZtTv1QDkgQKcKDlGuRiD1yVGg1ySvddDSLtA5r72Q2+AAAAFAAAAAEAAAAH0U9Cen1K14AI5fGKjZ7XydzAKoaJlQZLmoilpoS4ZiQAAAAAAAAAAAAAAxwAAAAAAAAAAAAAGzI=" -min_resource_fee = 11_516 +min_resource_fee = 13_946 fee = BaseFee.new(min_resource_fee + 100) # Use the XDR generated here to send it to the futurenet diff --git a/docs/examples/soroban/create_contract.md b/docs/examples/soroban/create_contract.md index 8330b29..15ea7bf 100644 --- a/docs/examples/soroban/create_contract.md +++ b/docs/examples/soroban/create_contract.md @@ -25,6 +25,7 @@ alias Stellar.TxBuild.{ SCAddress } +alias Stellar.KeyPair alias Stellar.Horizon.Accounts wasm_ref = <> @@ -47,7 +48,7 @@ invoke_host_function_op = InvokeHostFunction.new(host_function: host_function) keypair = {public_key, _secret} = - Stellar.KeyPair.from_secret_seed("SC5J4N7JTTWK6QS34OFEX67PB7X2UDLZMOPX2ORA5KTPQHFCESBKZ46D") + KeyPair.from_secret_seed("SC5J4N7JTTWK6QS34OFEX67PB7X2UDLZMOPX2ORA5KTPQHFCESBKZ46D") source_account = Account.new(public_key) @@ -67,6 +68,11 @@ source_account soroban_data = "AAAAAAAAAAEAAAAHuoVwkiq7sFT5+6wPecWIC3zW3SXzDactjjMN9VUNzQIAAAAAAAAAAQAAAAYAAAABet+3VCiKSYoZDd/Ce32Dtp9tYwNFc64V/QfdZUJm4boAAAAUAAAAAQAAAAAAAX/UAAACyAAAAKQAAADYAAAAAAAAACs=" +auth = [ + "AAAAAAAAAAEAAAAAAAAAAAAAAADJTnjlYcJxSKDat78jbEyDkVqo2uhpNX4BdBtqGrx+1t3O7skFonbhP9PT+l5IGaavsMV+AyUtQF88+kCpS/YbAAAAAIw32Y19ZRfshxeO+Nw4BNSkE0ZhibcEWId4SVzs0PZPAAAAAA==" +] + +invoke_host_function_op = InvokeHostFunction.set_auth(invoke_host_function_op, auth) min_resource_fee = 38_733 fee = BaseFee.new(min_resource_fee + 100) @@ -98,9 +104,10 @@ alias Stellar.TxBuild.{ Signature } +alias Stellar.KeyPair alias Stellar.Horizon.Accounts -keypair = {public_key, _secret} = Stellar.KeyPair.from_secret_seed("SCA...3EK") +keypair = {public_key, _secret} = KeyPair.from_secret_seed("SCA...3EK") asset = Asset.new(code: :ABC, issuer: public_key) diff --git a/docs/examples/soroban/invoke_contract_functions.md b/docs/examples/soroban/invoke_contract_functions.md index 1072eac..5836393 100644 --- a/docs/examples/soroban/invoke_contract_functions.md +++ b/docs/examples/soroban/invoke_contract_functions.md @@ -17,10 +17,11 @@ There are three ways to perform a contract function invocation: alias Stellar.TxBuild.{ Account, BaseFee, + InvokeContractArgs, InvokeHostFunction, HostFunction, + SCAddress, SCVal, - SCVec, SequenceNumber, Signature } @@ -28,16 +29,19 @@ alias Stellar.TxBuild.{ alias Stellar.Horizon.Accounts alias Stellar.KeyPair -contract_address = - "CD3HNKU3ERTEYLBBBVTSOYE4ZL2ZWV7NHLQIZRRKC4CBNMZXC7ISBXHV" - |> SCAddress.new() - |> (&SCVal.new(address: &1)).() -function_name = SCVal.new(symbol: "hello") -args = SCVec.new([contract_address, function_name, SCVal.new(string: "dev")]) +contract_address = SCAddress.new("CAMGSYINVVL6WP3Q5WPNL7FS4GZP37TWV7MKIRQF5QMYLK3N2SW4P3RC") + +function_name = "hello" +args = + InvokeContractArgs.new( + contract_address: contract_address, + function_name: function_name, + args: [SCVal.new(string: "dev")] + ) host_function = HostFunction.new(invoke_contract: args) invoke_host_function_op = InvokeHostFunction.new(host_function: host_function) -keypair = {public_key, _secret} = Stellar.KeyPair.from_secret_seed("SDR...Q24") +keypair = {public_key, _secret} = KeyPair.from_secret_seed("SDR...Q24") source_account = Account.new(public_key) {:ok, seq_num} = Accounts.fetch_next_sequence_number(public_key) sequence_number = SequenceNumber.new(seq_num) @@ -52,9 +56,9 @@ source_account # simulate transaction soroban_data = - "AAAAAgAAAAZQma4vqEU8Njqacc34GYyiWNEvpEu13GiuAiVZX0YdNwAAABQAAAAHRhmB7Imi4CwJhmzp1r/d76UShPJrO5PSHOV2Z9tPbE8AAAAAABJ8KwAAE3AAAAAAAAAAAAAAAAAAAAAAAAAAAA==" + "AAAAAAAAAAIAAAAGAAAAAQmM+SjuK6EFG2xjRoNDYSKeKWmaP+sPIZ2z+rHmx5I0AAAAFAAAAAEAAAAHjDfZjX1lF+yHF4743DgE1KQTRmGJtwRYh3hJXOzQ9k8AAAAAAE6LRgAAGDQAAAAAAAAAAAAAAAw=" -min_resource_fee = 51_470 +min_resource_fee = 64_141 fee = BaseFee.new(min_resource_fee) # Use the XDR generated here to send it to the futurenet @@ -75,10 +79,12 @@ source_account ```elixir alias Stellar.TxBuild.{ + Account, + BaseFee, + InvokeContractArgs, InvokeHostFunction, HostFunction, SCVal, - SCVec, SCAddress, SequenceNumber, Signature @@ -87,29 +93,26 @@ alias Stellar.TxBuild.{ alias Stellar.Horizon.Accounts alias Stellar.KeyPair -contract_address = - "CD3HNKU3ERTEYLBBBVTSOYE4ZL2ZWV7NHLQIZRRKC4CBNMZXC7ISBXHV" - |> SCAddress.new() - |> (&SCVal.new(address: &1)).() -function_name = SCVal.new(symbol: "inc") -keypair = {public_key, _secret} = Stellar.KeyPair.from_secret_seed("SDR...Q24") +contract_address = SCAddress.new("CAMGSYINVVL6WP3Q5WPNL7FS4GZP37TWV7MKIRQF5QMYLK3N2SW4P3RC") + +function_name = "inc" +keypair = {public_key, _secret} = KeyPair.from_secret_seed("SDR...Q24") address_type = SCAddress.new(public_key) address = SCVal.new(address: address_type) args = - SCVec.new([ - contract_address, - function_name, - address, - SCVal.new(u128: %{hi: 0, lo: 2}) - ]) + InvokeContractArgs.new( + contract_address: contract_address, + function_name: function_name, + args: [address, SCVal.new(u128: %{hi: 0, lo: 2})] + ) host_function = HostFunction.new(invoke_contract: args) invoke_host_function_op = InvokeHostFunction.new(host_function: host_function) -source_account = Stellar.TxBuild.Account.new(public_key) +source_account = Account.new(public_key) {:ok, seq_num} = Accounts.fetch_next_sequence_number(public_key) sequence_number = SequenceNumber.new(seq_num) -signature = Stellar.TxBuild.Signature.new(keypair) +signature = Signature.new(keypair) # Use this XDR to simulate the transaction and get the soroban_data, the invoke_host_function auth # and the min_resource_fee @@ -120,14 +123,14 @@ source_account # Simulate Transaction soroban_data = - "AAAAAAAAAAEAAAAHmDXys1KuBimD87u2AiUG/jb5CqOkQW/qASpb6gMVRlsAAAAAAAAAAQAAAAYAAAAB9naqmyRmTCwhDWcnYJzK9ZtX7TrgjMYqFwQWszcX0SAAAAAUAAAAAQAAAAAANnOcAAAUOAAAAQQAAAK8AAAAAAAAAIk=" + "AAAAAAAAAAEAAAAHjDfZjX1lF+yHF4743DgE1KQTRmGJtwRYh3hJXOzQ9k8AAAABAAAABgAAAAEJjPko7iuhBRtsY0aDQ2Einilpmj/rDyGds/qx5seSNAAAABQAAAABAE9HyAAAGDQAAADIAAAAAAAAACU=" auth = [ - "AAAAAAAAAAAAAAAB9naqmyRmTCwhDWcnYJzK9ZtX7TrgjMYqFwQWszcX0SAAAAADaW5jAAAAAAIAAAASAAAAAAAAAABaOyGfG/GU6itO0ElcKHcFqVS+fbN5bGtw0yDCwWKx2gAAAAkAAAAAAAAAAAAAAAAAAAACAAAAAA==" + "AAAAAAAAAAAAAAABCYz5KO4roQUbbGNGg0NhIp4paZo/6w8hnbP6sebHkjQAAAADaW5jAAAAAAIAAAASAAAAAAAAAABaOyGfG/GU6itO0ElcKHcFqVS+fbN5bGtw0yDCwWKx2gAAAAkAAAAAAAAAAAAAAAAAAAACAAAAAA==" ] invoke_host_function_op = InvokeHostFunction.set_auth(invoke_host_function_op, auth) -min_resource_fee = 78_281 +min_resource_fee = 68_996 fee = BaseFee.new(min_resource_fee + 100) # Use the XDR generated here to send it to the futurenet @@ -149,7 +152,9 @@ source_account alias StellarBase.XDR.{SorobanResources, SorobanTransactionData, UInt32} alias Stellar.TxBuild.{ + Account, BaseFee, + InvokeContractArgs, InvokeHostFunction, HostFunction, SCVal, @@ -161,40 +166,36 @@ alias Stellar.TxBuild.{ alias Stellar.Horizon.Accounts alias Stellar.KeyPair -contract_address = - "CAMGSYINVVL6WP3Q5WPNL7FS4GZP37TWV7MKIRQF5QMYLK3N2SW4P3RC" - |> SCAddress.new() - |> (&SCVal.new(address: &1)).() -function_name = SCVal.new(symbol: "inc") +contract_address = SCAddress.new("CAMGSYINVVL6WP3Q5WPNL7FS4GZP37TWV7MKIRQF5QMYLK3N2SW4P3RC") + +function_name = "inc" ## invoker -{invoker_public_key, invoker_secret_key} = - KeyPair.from_secret_seed("SCAVFA3PI3MJLTQNMXOUNBSEUOSY66YMG3T2KCQKLQBENNVLVKNPV3EK") +{invoker_public_key, invoker_secret_key} = KeyPair.from_secret_seed("SCA...3EK") ## submitter submitter_keypair = - {submitter_public_key, _submitter_secret_key} = - KeyPair.from_secret_seed("SDRD4CSRGPWUIPRDS5O3CJBNJME5XVGWNI677MZDD4OD2ZL2R6K5IQ24") + {submitter_public_key, _submitter_secret_key} = KeyPair.from_secret_seed("SDR...Q24") + address_type = SCAddress.new(invoker_public_key) address = SCVal.new(address: address_type) args = - SCVec.new([ - contract_address, - function_name, - address, - SCVal.new(u128: %{hi: 0, lo: 2}) - ]) + InvokeContractArgs.new( + contract_address: contract_address, + function_name: function_name, + args: SCVec.new([address, SCVal.new(u128: %{hi: 0, lo: 2})]) + ) host_function = HostFunction.new(invoke_contract: args) invoke_host_function_op = InvokeHostFunction.new(host_function: host_function) -source_account = Stellar.TxBuild.Account.new(submitter_public_key) +source_account = Account.new(submitter_public_key) {:ok, seq_num} = Accounts.fetch_next_sequence_number(submitter_public_key) sequence_number = SequenceNumber.new(seq_num) -signature = Stellar.TxBuild.Signature.new(submitter_keypair) +signature = Signature.new(submitter_keypair) # Use this XDR to simulate the transaction and get the soroban_data, the invoke_host_function auth and the min_resource_fee source_account @@ -207,7 +208,7 @@ source_account resources: %SorobanResources{instructions: %UInt32{datum: datum}} = resources } = soroban_data, ""} = - "AAAAAAAAAAIAAAAAAAAAAMlOeOVhwnFIoNq3vyNsTIORWqja6Gk1fgF0G2oavH7WAAAAB5g18rNSrgYpg/O7tgIlBv42+QqjpEFv6gEqW+oDFUZbAAAAAAAAAAIAAAAGAAAAAAAAAADJTnjlYcJxSKDat78jbEyDkVqo2uhpNX4BdBtqGrx+1gAAABU69WqNb/7SRQAAAAAAAAAAAAAABgAAAAEYaWENrVfrP3DtntX8suGy/f52r9ikRgXsGYWrbdStxwAAABQAAAABAAAAAAA4FMMAABWQAAABmAAAA/AAAAAAAAAAxQ==" + "AAAAAAAAAAIAAAAAAAAAAMlOeOVhwnFIoNq3vyNsTIORWqja6Gk1fgF0G2oavH7WAAAAB4w32Y19ZRfshxeO+Nw4BNSkE0ZhibcEWId4SVzs0PZPAAAAAgAAAAYAAAAAAAAAAMlOeOVhwnFIoNq3vyNsTIORWqja6Gk1fgF0G2oavH7WAAAAFQ68kGSYE4XvAAAAAAAAAAYAAAABCYz5KO4roQUbbGNGg0NhIp4paZo/6w8hnbP6sebHkjQAAAAUAAAAAQBRdfwAABl8AAABcAAAAAAAAAwE" |> Base.decode64!() |> SorobanTransactionData.decode_xdr!() @@ -215,7 +216,7 @@ source_account # This number needs to be in the same ledger when submitting the transaction, otherwise the function invocation will fail. latest_ledger = 164_265 -auth_xdr = "AAAAAQAAAAAAAAAAyU545WHCcUig2re/I2xMg5FaqNroaTV+AXQbahq8ftY69WqNb/7SRQAAAAAAAAAAAAAAAAAAAAEYaWENrVfrP3DtntX8suGy/f52r9ikRgXsGYWrbdStxwAAAANpbmMAAAAAAgAAABIAAAAAAAAAAMlOeOVhwnFIoNq3vyNsTIORWqja6Gk1fgF0G2oavH7WAAAACQAAAAAAAAAAAAAAAAAAAAIAAAAA" +auth_xdr = "AAAAAQAAAAAAAAAAyU545WHCcUig2re/I2xMg5FaqNroaTV+AXQbahq8ftYOvJBkmBOF7wAAAAAAAAABAAAAAAAAAAEJjPko7iuhBRtsY0aDQ2Einilpmj/rDyGds/qx5seSNAAAAANpbmMAAAAAAgAAABIAAAAAAAAAAMlOeOVhwnFIoNq3vyNsTIORWqja6Gk1fgF0G2oavH7WAAAACQAAAAAAAAAAAAAAAAAAAAIAAAAA" auth = SorobanAuthorizationEntry.sign_xdr(auth_xdr, invoker_secret_key, latest_ledger) invoke_host_function_op = InvokeHostFunction.set_auth(invoke_host_function_op, [auth]) @@ -226,8 +227,8 @@ new_instructions = UInt32.new(datum + round(datum * 0.25)) new_resources = %{resources | instructions: new_instructions} soroban_data = %{soroban_data | resources: new_resources} -# `round(min_resource_fee*0.1)` is needed since the cost of the transaction will increase because there are two signers. -fee = BaseFee.new(min_resource_fee + round(min_resource_fee*0.1) +100) +# `round(min_resource_fee*0.2)` is needed since the cost of the transaction will increase because there are two signers. +fee = BaseFee.new(min_resource_fee + round(min_resource_fee*0.2) +100) # Use the XDR generated here to send it to the futurenet source_account diff --git a/docs/examples/soroban/restore_footprint.md b/docs/examples/soroban/restore_footprint.md index 66c748a..7a4cb99 100644 --- a/docs/examples/soroban/restore_footprint.md +++ b/docs/examples/soroban/restore_footprint.md @@ -1,8 +1,10 @@ # Restore Footprint -The `RestoreFootprint` operation is used to restore a contract data entry's. The restored entry will have its expiration ledger bumped to the [minimums](https://github.com/stellar/stellar-core/blob/2109a168a895349f87b502ae3d182380b378fa47/src/ledger/NetworkConfig.h#L77-L78) the network allows for newly created entries, which is 4096 + current ledger for persistent entries, and 16 + current ledger for temporary entries. -A contract instance, wasm hash, and data storage entry (persistent/instance/temporary) can expire, so in case you need any of these already expired info, you can use this restore for it. +The `RestoreFootprint` operation is used to restore a contract data entry's. The restored entry will have its expiration ledger bumped to the [minimums](https://github.com/stellar/stellar-core/blob/2109a168a895349f87b502ae3d182380b378fa47/src/ledger/NetworkConfig.h#L77-L78) the network allows for newly created entries, which is 4096 + current ledger for persistent entries. + +A contract instance, wasm hash, and data storage entry (persistent/instance) can expire, so in case you need any of these already expired info, you can use this restore for it. Read more about it: + - https://soroban.stellar.org/docs/fundamentals-and-concepts/state-expiration#restorefootprintop - https://docs.rs/soroban-sdk/latest/soroban_sdk/storage/struct.Storage.html @@ -18,6 +20,7 @@ In this example, we will restore a contract instance of an already expired contr alias Stellar.TxBuild.{ Account, BaseFee, + LedgerFootprint, LedgerKey, RestoreFootprint, SCAddress, @@ -28,14 +31,13 @@ alias Stellar.TxBuild.{ SorobanTransactionData } +alias Stellar.Horizon.Accounts alias Stellar.KeyPair contract_sc_address = SCAddress.new("CAMGSYINVVL6WP3Q5WPNL7FS4GZP37TWV7MKIRQF5QMYLK3N2SW4P3RC") key = SCVal.new(ledger_key_contract_instance: nil) -keypair = - {public_key, _secret} = - Stellar.KeyPair.from_secret_seed("SCAVFA3PI3MJLTQNMXOUNBSEUOSY66YMG3T2KCQKLQBENNVLVKNPV3EK") +keypair = {public_key, _secret} = KeyPair.from_secret_seed("SCA...3EK") contract_data = LedgerKey.new( @@ -43,8 +45,7 @@ contract_data = [ contract: contract_sc_address, key: key, - durability: :persistent, - body_type: :data_entry + durability: :persistent ]} ) @@ -55,8 +56,7 @@ soroban_data = footprint: footprint, instructions: 0, read_bytes: 0, - write_bytes: 0, - extended_meta_data_size_bytes: 0 + write_bytes: 0 ] |> SorobanResources.new() |> (&SorobanTransactionData.new(resources: &1, refundable_fee: 0)).() diff --git a/docs/examples/soroban/upload_contract_code.md b/docs/examples/soroban/upload_contract_code.md index 95de3b1..0701826 100644 --- a/docs/examples/soroban/upload_contract_code.md +++ b/docs/examples/soroban/upload_contract_code.md @@ -26,9 +26,7 @@ host_function = HostFunction.new(upload_contract_wasm: code) invoke_host_function_op = InvokeHostFunction.new(host_function: host_function) -keypair = - {public_key, _secret} = - Stellar.KeyPair.from_secret_seed("SDRD4CSRGPWUIPRDS5O3CJBNJME5XVGWNI677MZDD4OD2ZL2R6K5IQ24") +keypair = {public_key, _secret} = KeyPair.from_secret_seed("SDR...Q24") source_account = Account.new(public_key) diff --git a/lib/tx_build/config_setting_id.ex b/lib/tx_build/config_setting_id.ex new file mode 100644 index 0000000..01b3a17 --- /dev/null +++ b/lib/tx_build/config_setting_id.ex @@ -0,0 +1,48 @@ +defmodule Stellar.TxBuild.ConfigSettingID do + @moduledoc """ + `ConfigSettingID` struct definition. + """ + alias StellarBase.XDR.ConfigSettingID + + @behaviour Stellar.TxBuild.XDR + + @type identifier_type :: atom() + @type validation :: {:ok, atom()} + + @type t :: %__MODULE__{identifier: identifier_type()} + + defstruct [:identifier] + + @identifiers %{ + max_size: :MAX_SIZE_BYTES, + compute: :COMPUTE_V0, + ledger_cost: :LEDGER_COST_V0, + historical_data: :HISTORICAL_DATA_V0, + events: :EVENTS_V0, + bandwidth: :BANDWIDTH_V0, + cost_params_cpu: :COST_PARAMS_CPU_INSTRUCTIONS, + cost_params_memory: :COST_PARAMS_MEMORY_BYTES, + data_key_size: :DATA_KEY_SIZE_BYTES, + data_entry_size: :DATA_ENTRY_SIZE_BYTES + } + + @impl true + def new(identifier, opts \\ []) + + def new(identifier, _opts) when is_map_key(@identifiers, identifier), + do: %__MODULE__{identifier: identifier} + + def new(_args, _opts), do: {:error, :invalid_identifier} + + @impl true + def to_xdr(%__MODULE__{identifier: identifier}) do + with {:ok, identifier} <- retrieve_xdr_identifier(identifier) do + ConfigSettingID.new(identifier) + end + end + + def to_xdr(_struct), do: {:error, :invalid_struct} + + @spec retrieve_xdr_identifier(identifier :: identifier_type()) :: validation() + defp retrieve_xdr_identifier(identifier), do: {:ok, Map.get(@identifiers, identifier)} +end diff --git a/lib/tx_build/host_function.ex b/lib/tx_build/host_function.ex index efac5b3..e29da5c 100644 --- a/lib/tx_build/host_function.ex +++ b/lib/tx_build/host_function.ex @@ -2,12 +2,12 @@ defmodule Stellar.TxBuild.HostFunction do @moduledoc """ `HostFunction` struct definition. """ - alias Stellar.TxBuild.{CreateContractArgs, SCVec} + alias Stellar.TxBuild.{CreateContractArgs, InvokeContractArgs} alias StellarBase.XDR.{HostFunction, HostFunctionType, VariableOpaque} @behaviour Stellar.TxBuild.XDR - @type value :: CreateContractArgs.t() | SCVec.t() | binary() + @type value :: CreateContractArgs.t() | InvokeContractArgs.t() | binary() @type error :: {:error, atom()} @type validation :: {:ok, any()} | error() @type type :: @@ -45,7 +45,7 @@ defmodule Stellar.TxBuild.HostFunction do type = HostFunctionType.new(:HOST_FUNCTION_TYPE_INVOKE_CONTRACT) value - |> SCVec.to_xdr() + |> InvokeContractArgs.to_xdr() |> HostFunction.new(type) end @@ -72,7 +72,7 @@ defmodule Stellar.TxBuild.HostFunction do end @spec validate_host_function({type :: atom(), value :: value()}) :: validation() - defp validate_host_function({:invoke_contract, %SCVec{} = value}), do: {:ok, value} + defp validate_host_function({:invoke_contract, %InvokeContractArgs{} = value}), do: {:ok, value} defp validate_host_function({:create_contract, %CreateContractArgs{} = value}), do: {:ok, value} defp validate_host_function({:upload_contract_wasm, value}) when is_binary(value), diff --git a/lib/tx_build/soroban_authorized_contract_function.ex b/lib/tx_build/invoke_contract_args.ex similarity index 77% rename from lib/tx_build/soroban_authorized_contract_function.ex rename to lib/tx_build/invoke_contract_args.ex index 37c345f..bb1e56a 100644 --- a/lib/tx_build/soroban_authorized_contract_function.ex +++ b/lib/tx_build/invoke_contract_args.ex @@ -1,13 +1,13 @@ -defmodule Stellar.TxBuild.SorobanAuthorizedContractFunction do +defmodule Stellar.TxBuild.InvokeContractArgs do @moduledoc """ - `SorobanAuthorizedContractFunction` struct definition. + `InvokeContractArgs` struct definition. """ import Stellar.TxBuild.Validations, - only: [validate_vec: 1, validate_address: 1] + only: [validate_sc_vals: 1, validate_address: 1] - alias StellarBase.XDR.{SorobanAuthorizedContractFunction, SCSymbol} - alias Stellar.TxBuild.{SCAddress, SCVec} + alias StellarBase.XDR.{InvokeContractArgs, SCSymbol} + alias Stellar.TxBuild.{SCAddress, SCVal, SCVec} @behaviour Stellar.TxBuild.XDR @@ -15,7 +15,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizedContractFunction do @type validation :: {:ok, any()} | error() @type contract_address :: SCAddress.t() @type function_name :: String.t() - @type args :: SCVec.t() + @type args :: list(SCVal.t()) @type t :: %__MODULE__{ contract_address: contract_address(), @@ -35,7 +35,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizedContractFunction do with {:ok, contract_address} <- validate_address(contract_address), {:ok, function_name} <- validate_function_name(function_name), - {:ok, function_args} <- validate_vec(function_args) do + {:ok, function_args} <- validate_sc_vals({:vals, function_args}) do %__MODULE__{ contract_address: contract_address, function_name: function_name, @@ -53,11 +53,15 @@ defmodule Stellar.TxBuild.SorobanAuthorizedContractFunction do args: args }) do function_name = SCSymbol.new(function_name) - args = SCVec.to_xdr(args) + + args = + args + |> SCVec.new() + |> SCVec.to_xdr() contract_address |> SCAddress.to_xdr() - |> SorobanAuthorizedContractFunction.new(function_name, args) + |> InvokeContractArgs.new(function_name, args) end def to_xdr(_struct), do: {:error, :invalid_struct} diff --git a/lib/tx_build/ledger/config_setting.ex b/lib/tx_build/ledger/config_setting.ex new file mode 100644 index 0000000..0cc33a8 --- /dev/null +++ b/lib/tx_build/ledger/config_setting.ex @@ -0,0 +1,30 @@ +defmodule Stellar.TxBuild.Ledger.ConfigSetting do + @moduledoc """ + Ledger `ConfigSetting` struct definition. + """ + alias StellarBase.XDR.LedgerKeyConfigSetting + alias Stellar.TxBuild.ConfigSettingID + + @behaviour Stellar.TxBuild.XDR + + @type t :: %__MODULE__{config_setting_id: ConfigSettingID.t()} + + defstruct [:config_setting_id] + + @impl true + def new(config_setting_id, opts \\ []) + + def new(config_setting_id, _opts) do + case ConfigSettingID.new(config_setting_id) do + %ConfigSettingID{} = config_setting_id -> %__MODULE__{config_setting_id: config_setting_id} + error -> error + end + end + + @impl true + def to_xdr(%__MODULE__{config_setting_id: config_setting_id}) do + config_setting_id + |> ConfigSettingID.to_xdr() + |> LedgerKeyConfigSetting.new() + end +end diff --git a/lib/tx_build/ledger/contract_code.ex b/lib/tx_build/ledger/contract_code.ex index 16dd8dd..dc02f1b 100644 --- a/lib/tx_build/ledger/contract_code.ex +++ b/lib/tx_build/ledger/contract_code.ex @@ -3,58 +3,41 @@ defmodule Stellar.TxBuild.Ledger.ContractCode do `ContractCode` struct definition. """ - alias StellarBase.XDR.{ContractEntryBodyType, Hash, LedgerKeyContractCode} + alias StellarBase.XDR.{Hash, LedgerKeyContractCode} @behaviour Stellar.TxBuild.XDR @type error :: {:error, atom()} @type validation :: {:ok, any()} | error() @type hash :: String.t() - @type body_type :: :data_entry | :expiration_ext - @type t :: %__MODULE__{ - hash: hash(), - body_type: body_type() - } + @type t :: %__MODULE__{hash: hash()} - defstruct [:hash, :body_type] - - @allowed_body_types ~w(data_entry expiration_ext)a + defstruct [:hash] @impl true def new(args, opts \\ []) def new( [ - {:hash, hash}, - {:body_type, body_type} + {:hash, hash} ], _opts ) - when is_binary(hash) and body_type in @allowed_body_types do + when is_binary(hash) do %__MODULE__{ - hash: hash, - body_type: body_type + hash: hash } end def new(_value, _opts), do: {:error, :invalid_ledger_key_args} @impl true - def to_xdr(%__MODULE__{ - hash: hash, - body_type: body_type - }) do - body_type = body_type_to_xdr(body_type) - + def to_xdr(%__MODULE__{hash: hash}) do hash |> Hash.new() - |> LedgerKeyContractCode.new(body_type) + |> LedgerKeyContractCode.new() end def to_xdr(_struct), do: {:error, :invalid_struct} - - @spec body_type_to_xdr(atom()) :: ContractEntryBodyType.t() - defp body_type_to_xdr(:data_entry), do: ContractEntryBodyType.new(:DATA_ENTRY) - defp body_type_to_xdr(:expiration_ext), do: ContractEntryBodyType.new(:EXPIRATION_EXTENSION) end diff --git a/lib/tx_build/ledger/contract_data.ex b/lib/tx_build/ledger/contract_data.ex index ed4423e..f2a0902 100644 --- a/lib/tx_build/ledger/contract_data.ex +++ b/lib/tx_build/ledger/contract_data.ex @@ -7,7 +7,7 @@ defmodule Stellar.TxBuild.Ledger.ContractData do only: [validate_address: 1] alias Stellar.TxBuild.{SCAddress, SCVal} - alias StellarBase.XDR.{ContractEntryBodyType, ContractDataDurability, LedgerKeyContractData} + alias StellarBase.XDR.{ContractDataDurability, LedgerKeyContractData} @behaviour Stellar.TxBuild.XDR @@ -16,19 +16,16 @@ defmodule Stellar.TxBuild.Ledger.ContractData do @type contract :: SCAddress.t() @type key :: SCVal.t() @type durability :: :temporary | :persistent - @type body_type :: :data_entry | :expiration_ext @type t :: %__MODULE__{ contract: contract(), key: key(), - durability: durability(), - body_type: body_type() + durability: durability() } - defstruct [:contract, :key, :durability, :body_type] + defstruct [:contract, :key, :durability] @allowed_durabilities ~w(temporary persistent)a - @allowed_body_types ~w(data_entry expiration_ext)a @impl true def new(args, opts \\ []) @@ -37,19 +34,17 @@ defmodule Stellar.TxBuild.Ledger.ContractData do [ {:contract, contract}, {:key, key}, - {:durability, durability}, - {:body_type, body_type} + {:durability, durability} ], _opts ) - when durability in @allowed_durabilities and body_type in @allowed_body_types do + when durability in @allowed_durabilities do with {:ok, contract} <- validate_address(contract), {:ok, key} <- validate_sc_val_ledger_instance(key) do %__MODULE__{ contract: contract, key: key, - durability: durability, - body_type: body_type + durability: durability } end end @@ -60,16 +55,14 @@ defmodule Stellar.TxBuild.Ledger.ContractData do def to_xdr(%__MODULE__{ contract: contract, key: key, - durability: durability, - body_type: body_type + durability: durability }) do key = SCVal.to_xdr(key) durability = durability_to_xdr(durability) - body_type = body_type_to_xdr(body_type) contract |> SCAddress.to_xdr() - |> LedgerKeyContractData.new(key, durability, body_type) + |> LedgerKeyContractData.new(key, durability) end def to_xdr(_struct), do: {:error, :invalid_struct} @@ -81,8 +74,4 @@ defmodule Stellar.TxBuild.Ledger.ContractData do @spec durability_to_xdr(atom()) :: ContractDataDurability.t() defp durability_to_xdr(:temporary), do: ContractDataDurability.new(:TEMPORARY) defp durability_to_xdr(:persistent), do: ContractDataDurability.new(:PERSISTENT) - - @spec body_type_to_xdr(atom()) :: ContractEntryBodyType.t() - defp body_type_to_xdr(:data_entry), do: ContractEntryBodyType.new(:DATA_ENTRY) - defp body_type_to_xdr(:expiration_ext), do: ContractEntryBodyType.new(:EXPIRATION_EXTENSION) end diff --git a/lib/tx_build/ledger/expiration.ex b/lib/tx_build/ledger/expiration.ex new file mode 100644 index 0000000..12e6c1f --- /dev/null +++ b/lib/tx_build/ledger/expiration.ex @@ -0,0 +1,28 @@ +defmodule Stellar.TxBuild.Ledger.Expiration do + @moduledoc """ + Ledger `Expiration` struct definition. + """ + alias StellarBase.XDR.{Hash, LedgerKeyExpiration} + + @behaviour Stellar.TxBuild.XDR + + @type t :: %__MODULE__{key_hash: binary()} + + defstruct [:key_hash] + + @impl true + def new(key_hash, opts \\ []) + + def new(key_hash, _opts) when is_binary(key_hash), do: %__MODULE__{key_hash: key_hash} + + def new(_key_hash, _opts), do: {:error, :invalid_expiration} + + @impl true + def to_xdr(%__MODULE__{key_hash: key_hash}) do + key_hash + |> Hash.new() + |> LedgerKeyExpiration.new() + end + + def to_xdr(_struct), do: {:error, :invalid_struct} +end diff --git a/lib/tx_build/ledger_key.ex b/lib/tx_build/ledger_key.ex index 780ebf2..eb92ae9 100644 --- a/lib/tx_build/ledger_key.ex +++ b/lib/tx_build/ledger_key.ex @@ -9,8 +9,10 @@ defmodule Stellar.TxBuild.LedgerKey do Account, ClaimableBalance, Data, + ConfigSetting, ContractCode, ContractData, + Expiration, LiquidityPool, Offer, Trustline @@ -27,6 +29,8 @@ defmodule Stellar.TxBuild.LedgerKey do | :liquidity_pool | :contract_data | :contract_code + | :config_setting + | :expiration @type entry :: Account.t() @@ -37,6 +41,8 @@ defmodule Stellar.TxBuild.LedgerKey do | Trustline.t() | ContractData.t() | ContractCode.t() + | ConfigSetting.t() + | Expiration.t() @type t :: %__MODULE__{type: type(), entry: entry()} @@ -122,6 +128,32 @@ defmodule Stellar.TxBuild.LedgerKey do end end + def new( + {:config_setting, args}, + _opts + ) do + case ConfigSetting.new(args) do + %ConfigSetting{} = config_setting -> + %__MODULE__{type: :config_setting, entry: config_setting} + + _error -> + {:error, :invalid_config_setting} + end + end + + def new( + {:expiration, args}, + _opts + ) do + case Expiration.new(args) do + %Expiration{} = expiration -> + %__MODULE__{type: :expiration, entry: expiration} + + _error -> + {:error, :invalid_expiration} + end + end + def new(_args, _opts), do: {:error, :invalid_ledger_key} @impl true @@ -142,4 +174,6 @@ defmodule Stellar.TxBuild.LedgerKey do defp ledger_entry_type(:liquidity_pool), do: LedgerEntryType.new(:LIQUIDITY_POOL) defp ledger_entry_type(:contract_data), do: LedgerEntryType.new(:CONTRACT_DATA) defp ledger_entry_type(:contract_code), do: LedgerEntryType.new(:CONTRACT_CODE) + defp ledger_entry_type(:config_setting), do: LedgerEntryType.new(:CONFIG_SETTING) + defp ledger_entry_type(:expiration), do: LedgerEntryType.new(:EXPIRATION) end diff --git a/lib/tx_build/sc_error.ex b/lib/tx_build/sc_error.ex index 8613f92..f66c9da 100644 --- a/lib/tx_build/sc_error.ex +++ b/lib/tx_build/sc_error.ex @@ -2,7 +2,7 @@ defmodule Stellar.TxBuild.SCError do @moduledoc """ `ScErrorCode` struct definition. """ - alias StellarBase.XDR.{SCError, SCErrorType, SCErrorCode} + alias StellarBase.XDR.{SCError, SCErrorType, SCErrorCode, UInt32} @behaviour Stellar.TxBuild.XDR @@ -42,7 +42,7 @@ defmodule Stellar.TxBuild.SCError do crypto: :SCE_CRYPTO, events: :SCE_EVENTS, budget: :SCE_BUDGET, - code: :SCE_VALUE, + value: :SCE_VALUE, auth: :SCE_AUTH } @@ -63,21 +63,31 @@ defmodule Stellar.TxBuild.SCError do def new(args, opts \\ []) def new([{type, code}], _opts) - when is_map_key(@types, type) and is_map_key(@codes, code) do + when is_map_key(@types, type) and (is_map_key(@codes, code) or is_integer(code)) do %__MODULE__{type: type, code: code} end def new(_args, _opts), do: {:error, :invalid_sc_error} @impl true + def to_xdr(%__MODULE__{type: :contract = type, code: code}) when is_integer(code) do + with {:ok, type} <- retrieve_xdr_type(type) do + type = SCErrorType.new(type) + + code + |> UInt32.new() + |> SCError.new(type) + end + end + def to_xdr(%__MODULE__{type: type, code: code}) do with {:ok, type} <- retrieve_xdr_type(type), {:ok, code} <- retrieve_xdr_code(code) do - code = SCErrorCode.new(code) + type = SCErrorType.new(type) - type - |> SCErrorType.new() - |> SCError.new(code) + code + |> SCErrorCode.new() + |> SCError.new(type) end end diff --git a/lib/tx_build/sc_vec.ex b/lib/tx_build/sc_vec.ex index d8a583e..75016ab 100644 --- a/lib/tx_build/sc_vec.ex +++ b/lib/tx_build/sc_vec.ex @@ -3,7 +3,7 @@ defmodule Stellar.TxBuild.SCVec do `ScVec` struct definition. """ alias Stellar.TxBuild.SCVal - alias StellarBase.XDR.SCVec + alias StellarBase.XDR.SCValList @behaviour Stellar.TxBuild.XDR @@ -30,15 +30,9 @@ defmodule Stellar.TxBuild.SCVec do def to_xdr(%__MODULE__{items: items}) do items |> Enum.map(&SCVal.to_xdr/1) - |> SCVec.new() + |> SCValList.new() end - @spec append_sc_val(t(), item :: SCVal.t()) :: t() | error() - def append_sc_val(%__MODULE__{items: items} = module, %SCVal{} = item), - do: %{module | items: items ++ [item]} - - def append_sc_val(_module, _item), do: {:error, :invalid_item} - @spec validate_vec_items(items :: items()) :: validation() defp validate_vec_items(items) do if Enum.all?(items, &is_sc_val?/1), diff --git a/lib/tx_build/soroban_address_credentials.ex b/lib/tx_build/soroban_address_credentials.ex index 8ccd7be..7ccf7fc 100644 --- a/lib/tx_build/soroban_address_credentials.ex +++ b/lib/tx_build/soroban_address_credentials.ex @@ -3,10 +3,10 @@ defmodule Stellar.TxBuild.SorobanAddressCredentials do `SorobanAddressCredentials` struct definition. """ import Stellar.TxBuild.Validations, - only: [validate_address: 1, validate_vec: 1] + only: [validate_address: 1] alias StellarBase.XDR.{Int64, SorobanAddressCredentials, UInt32} - alias Stellar.TxBuild.{SCAddress, SCVec} + alias Stellar.TxBuild.{SCAddress, SCVal} @behaviour Stellar.TxBuild.XDR @@ -15,20 +15,20 @@ defmodule Stellar.TxBuild.SorobanAddressCredentials do @type address :: SCAddress.t() @type nonce :: non_neg_integer() @type signature_expiration_ledger :: non_neg_integer() - @type signature_args :: SCVec.t() + @type signature :: SCVal.t() @type t :: %__MODULE__{ address: address(), nonce: nonce(), signature_expiration_ledger: signature_expiration_ledger(), - signature_args: signature_args() + signature: signature() } defstruct [ :address, :nonce, :signature_expiration_ledger, - :signature_args + :signature ] @impl true @@ -38,18 +38,18 @@ defmodule Stellar.TxBuild.SorobanAddressCredentials do address = Keyword.get(args, :address) nonce = Keyword.get(args, :nonce) signature_expiration_ledger = Keyword.get(args, :signature_expiration_ledger) - signature_args = Keyword.get(args, :signature_args) + signature = Keyword.get(args, :signature) with {:ok, address} <- validate_address(address), {:ok, nonce} <- validate_integer({nonce, :nonce}), {:ok, signature_expiration_ledger} <- validate_integer({signature_expiration_ledger, :signature_expiration_ledger}), - {:ok, signature_args} <- validate_vec(signature_args) do + {:ok, signature} <- validate_sc_val(signature) do %__MODULE__{ address: address, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger, - signature_args: signature_args + signature: signature } end end @@ -61,15 +61,15 @@ defmodule Stellar.TxBuild.SorobanAddressCredentials do address: address, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger, - signature_args: signature_args + signature: signature }) do nonce = Int64.new(nonce) signature_expiration_ledger = UInt32.new(signature_expiration_ledger) - signature_args = SCVec.to_xdr(signature_args) + signature = SCVal.to_xdr(signature) address |> SCAddress.to_xdr() - |> SorobanAddressCredentials.new(nonce, signature_expiration_ledger, signature_args) + |> SorobanAddressCredentials.new(nonce, signature_expiration_ledger, signature) end def to_xdr(_struct), do: {:error, :invalid_struct} @@ -77,4 +77,8 @@ defmodule Stellar.TxBuild.SorobanAddressCredentials do @spec validate_integer(tuple()) :: validation() defp validate_integer({integer, _type}) when is_integer(integer), do: {:ok, integer} defp validate_integer({_integer, type}), do: {:error, :"invalid_#{type}"} + + @spec validate_sc_val(sc_val :: signature()) :: validation() + defp validate_sc_val(%SCVal{} = sc_val), do: {:ok, sc_val} + defp validate_sc_val(_sc_val), do: {:error, :invalid_sc_val} end diff --git a/lib/tx_build/soroban_authorization_entry.ex b/lib/tx_build/soroban_authorization_entry.ex index 4810bb7..c7c4c40 100644 --- a/lib/tx_build/soroban_authorization_entry.ex +++ b/lib/tx_build/soroban_authorization_entry.ex @@ -8,7 +8,6 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntry do alias StellarBase.XDR.HashIDPreimageSorobanAuthorization, as: HashIDPreimageSorobanAuthorizationXDR - alias StellarBase.XDR.SCVec, as: SCVecXDR alias StellarBase.XDR.SorobanAddressCredentials, as: SorobanAddressCredentialsXDR alias StellarBase.XDR.SorobanAuthorizedInvocation, as: SorobanAuthorizedInvocationXDR alias StellarBase.XDR.{EnvelopeType, Hash, Int64, SorobanAuthorizationEntry, UInt32} @@ -19,7 +18,6 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntry do HashIDPreimageSorobanAuthorization, SCMapEntry, SCVal, - SCVec, SorobanAddressCredentials, SorobanCredentials, SorobanAuthorizedInvocation @@ -83,7 +81,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntry do %SorobanAddressCredentials{ nonce: nonce, signature_expiration_ledger: signature_expiration_ledger, - signature_args: signature_args + signature: %SCVal{value: signature_args} } = soroban_address_credentials }, root_invocation: root_invocation @@ -125,7 +123,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntry do soroban_address_credentials = %{ soroban_address_credentials - | signature_args: SCVec.append_sc_val(signature_args, signature_sc_val) + | signature: SCVal.new(vec: signature_args ++ [signature_sc_val]) } %{credentials | credentials: soroban_address_credentials} @@ -157,19 +155,19 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntry do signature_expiration_ledger = UInt32.new(latest_ledger + 3) - signature_args = + signature = nonce - |> build_signature_args_from_xdr( + |> build_signature_from_xdr( signature_expiration_ledger, root_invocation, secret_key ) + |> (&SCVal.new(vec: [&1])).() |> SCVal.to_xdr() - |> (&SCVecXDR.new([&1])).() soroban_address_credentials = %{ soroban_address_credentials - | signature_args: signature_args, + | signature: signature, signature_expiration_ledger: signature_expiration_ledger } @@ -198,13 +196,13 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntry do defp validate_root_invocation(_root_invocation), do: {:error, :invalid_root_invocation} - @spec build_signature_args_from_xdr( + @spec build_signature_from_xdr( nonce :: Int64.t(), signature_expiration_ledger :: UInt32.t(), root_invocation :: SorobanAuthorizedInvocationXDR.t(), secret_key :: secret_key() ) :: SCVal.t() | error() - defp build_signature_args_from_xdr( + defp build_signature_from_xdr( nonce, signature_expiration_ledger, root_invocation, diff --git a/lib/tx_build/soroban_authorized_function.ex b/lib/tx_build/soroban_authorized_function.ex index 6b9dea8..212c6d9 100644 --- a/lib/tx_build/soroban_authorized_function.ex +++ b/lib/tx_build/soroban_authorized_function.ex @@ -3,14 +3,14 @@ defmodule Stellar.TxBuild.SorobanAuthorizedFunction do `SorobanAuthorizedFunction` struct definition. """ alias StellarBase.XDR.{SorobanAuthorizedFunction, SorobanAuthorizedFunctionType} - alias Stellar.TxBuild.{CreateContractArgs, SorobanAuthorizedContractFunction} + alias Stellar.TxBuild.{CreateContractArgs, InvokeContractArgs} @behaviour Stellar.TxBuild.XDR @type error :: {:error, atom()} @type validation :: {:ok, any()} | error() @type type :: :contract_fn | :create_contract_host_fn - @type value :: CreateContractArgs.t() | SorobanAuthorizedContractFunction.t() + @type value :: CreateContractArgs.t() | InvokeContractArgs.t() @type t :: %__MODULE__{type: type(), value: value()} defstruct [:type, :value] @@ -33,7 +33,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizedFunction do type = SorobanAuthorizedFunctionType.new() value - |> SorobanAuthorizedContractFunction.to_xdr() + |> InvokeContractArgs.to_xdr() |> SorobanAuthorizedFunction.new(type) end @@ -49,10 +49,8 @@ defmodule Stellar.TxBuild.SorobanAuthorizedFunction do def to_xdr(_struct), do: {:error, :invalid_struct} @spec validate_soroban_auth_function(tuple()) :: validation() - defp validate_soroban_auth_function( - {:contract_fn, %SorobanAuthorizedContractFunction{} = value} - ), - do: {:ok, value} + defp validate_soroban_auth_function({:contract_fn, %InvokeContractArgs{} = value}), + do: {:ok, value} defp validate_soroban_auth_function({:create_contract_host_fn, %CreateContractArgs{} = value}), do: {:ok, value} diff --git a/lib/tx_build/soroban_resources.ex b/lib/tx_build/soroban_resources.ex index 5795248..9f0ce36 100644 --- a/lib/tx_build/soroban_resources.ex +++ b/lib/tx_build/soroban_resources.ex @@ -13,22 +13,19 @@ defmodule Stellar.TxBuild.SorobanResources do @type instructions :: integer() @type read_bytes :: integer() @type write_bytes :: integer() - @type extended_meta_data_size_bytes :: integer() @type t :: %__MODULE__{ footprint: footprint(), instructions: instructions(), read_bytes: read_bytes(), - write_bytes: write_bytes(), - extended_meta_data_size_bytes: extended_meta_data_size_bytes() + write_bytes: write_bytes() } defstruct [ :footprint, :instructions, :read_bytes, - :write_bytes, - :extended_meta_data_size_bytes + :write_bytes ] @impl true @@ -39,20 +36,17 @@ defmodule Stellar.TxBuild.SorobanResources do {:footprint, footprint}, {:instructions, instructions}, {:read_bytes, read_bytes}, - {:write_bytes, write_bytes}, - {:extended_meta_data_size_bytes, extended_meta_data_size_bytes} + {:write_bytes, write_bytes} ], _opts ) - when is_integer(instructions) and is_integer(read_bytes) and is_integer(write_bytes) and - is_integer(extended_meta_data_size_bytes) do + when is_integer(instructions) and is_integer(read_bytes) and is_integer(write_bytes) do with {:ok, footprint} <- validate_footprint(footprint) do %__MODULE__{ footprint: footprint, instructions: instructions, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes } end end @@ -64,21 +58,18 @@ defmodule Stellar.TxBuild.SorobanResources do footprint: footprint, instructions: instructions, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes }) do instructions = UInt32.new(instructions) read_bytes = UInt32.new(read_bytes) write_bytes = UInt32.new(write_bytes) - extended_meta_data_size_bytes = UInt32.new(extended_meta_data_size_bytes) footprint |> LedgerFootprint.to_xdr() |> SorobanResources.new( instructions, read_bytes, - write_bytes, - extended_meta_data_size_bytes + write_bytes ) end diff --git a/mix.exs b/mix.exs index 4697aa2..e2ef84c 100644 --- a/mix.exs +++ b/mix.exs @@ -37,7 +37,7 @@ defmodule Stellar.MixProject do # Run "mix help deps" to learn about dependencies. defp deps do [ - {:stellar_base, "~> 0.13"}, + {:stellar_base, "~> 0.14"}, {:ed25519, "~> 1.3"}, {:hackney, "~> 1.17"}, {:jason, "~> 1.0"}, diff --git a/mix.lock b/mix.lock index 4964281..1d2b8f2 100644 --- a/mix.lock +++ b/mix.lock @@ -1,5 +1,6 @@ %{ "bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"}, + "castore": {:hex, :castore, "1.0.3", "7130ba6d24c8424014194676d608cb989f62ef8039efd50ff4b3f33286d06db8", [:mix], [], "hexpm", "680ab01ef5d15b161ed6a95449fac5c6b8f60055677a8e79acf01b27baa4390b"}, "certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"}, "crc": {:hex, :crc, "0.10.5", "ee12a7c056ac498ef2ea985ecdc9fa53c1bfb4e53a484d9f17ff94803707dfd8", [:mix, :rebar3], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "3e673b6495a9525c5c641585af1accba59a1eb33de697bedf341e247012c2c7f"}, "credo": {:hex, :credo, "1.7.0", "6119bee47272e85995598ee04f2ebbed3e947678dee048d10b5feca139435f75", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "6839fcf63d1f0d1c0f450abc8564a57c43d644077ab96f2934563e68b8a769d7"}, @@ -7,7 +8,7 @@ "earmark_parser": {:hex, :earmark_parser, "1.4.33", "3c3fd9673bb5dcc9edc28dd90f50c87ce506d1f71b70e3de69aa8154bc695d44", [:mix], [], "hexpm", "2d526833729b59b9fdb85785078697c72ac5e5066350663e5be6a1182da61b8f"}, "ed25519": {:hex, :ed25519, "1.4.1", "479fb83c3e31987c9cad780e6aeb8f2015fb5a482618cdf2a825c9aff809afc4", [:mix], [], "hexpm", "0dacb84f3faa3d8148e81019ca35f9d8dcee13232c32c9db5c2fb8ff48c80ec7"}, "elixir_make": {:hex, :elixir_make, "0.7.7", "7128c60c2476019ed978210c245badf08b03dbec4f24d05790ef791da11aa17c", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5bc19fff950fad52bbe5f211b12db9ec82c6b34a9647da0c2224b8b8464c7e6c"}, - "elixir_xdr": {:hex, :elixir_xdr, "0.3.9", "d9c461a1c04bc31ccb36114b606c750d0ba0bed0a1d7ffc35d206e58faaba0ba", [:mix], [], "hexpm", "30afac1a82fafe11708e14d92532a83a881d1004ec92bf00e3d8a827c6685e3d"}, + "elixir_xdr": {:hex, :elixir_xdr, "0.3.10", "6443c48f29494b67bf48ef04d2abef2fccfb08d484c688bc3cbd0db390719b24", [:mix], [], "hexpm", "4041f284f802750c9c0b8c0fa5c69cd24911f73668a1d8eb42cbc4c8b8aa5b50"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "ex_doc": {:hex, :ex_doc, "0.30.3", "bfca4d340e3b95f2eb26e72e4890da83e2b3a5c5b0e52607333bf5017284b063", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "fbc8702046c1d25edf79de376297e608ac78cdc3a29f075484773ad1718918b6"}, "excoveralls": {:hex, :excoveralls, "0.16.1", "0bd42ed05c7d2f4d180331a20113ec537be509da31fed5c8f7047ce59ee5a7c5", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "dae763468e2008cf7075a64cb1249c97cb4bc71e236c5c2b5e5cdf1cfa2bf138"}, @@ -23,6 +24,6 @@ "nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"}, "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, - "stellar_base": {:hex, :stellar_base, "0.13.1", "ac3d1d6b5ad131eb27fbbdd8bfb42f998ea14c48e341a5cb091aa05a80c62deb", [:mix], [{:crc, "~> 0.10.0", [hex: :crc, repo: "hexpm", optional: false]}, {:elixir_xdr, "~> 0.3.9", [hex: :elixir_xdr, repo: "hexpm", optional: false]}], "hexpm", "8b72a9e0bb1cb13a031ace0c49ae51787ddd616f9d6333c2a3411cab068bc9c1"}, + "stellar_base": {:hex, :stellar_base, "0.14.0", "210540f579e3d223816a421d88e636716fd092559006f8e5b5a733092e439138", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:crc, "~> 0.10.0", [hex: :crc, repo: "hexpm", optional: false]}, {:elixir_xdr, "~> 0.3", [hex: :elixir_xdr, repo: "hexpm", optional: false]}], "hexpm", "d9576229fbc8fc571f5ba0c747c1ab0a6f28f83f8f7cb9825d023444c8f45d56"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, } diff --git a/test/support/fixtures/xdr.ex b/test/support/fixtures/xdr.ex index 041a70a..cfa02ca 100644 --- a/test/support/fixtures/xdr.ex +++ b/test/support/fixtures/xdr.ex @@ -90,6 +90,8 @@ defmodule Stellar.Test.Fixtures.XDR do defdelegate ledger_key_liquidity_pool(liquidity_pool_id), to: Ledger defdelegate ledger_key_contract_data(), to: Ledger defdelegate ledger_key_contract_code(), to: Ledger + defdelegate ledger_key_config_setting(), to: Ledger + defdelegate ledger_key_expiration(), to: Ledger defdelegate revoke_sponsorship(type, attrs), to: Ledger end diff --git a/test/support/fixtures/xdr/ledger.ex b/test/support/fixtures/xdr/ledger.ex index ca2fcc4..b71db24 100644 --- a/test/support/fixtures/xdr/ledger.ex +++ b/test/support/fixtures/xdr/ledger.ex @@ -363,8 +363,7 @@ defmodule Stellar.Test.Fixtures.XDR.Ledger do value: %StellarBase.XDR.Void{value: nil}, type: %StellarBase.XDR.SCValType{identifier: :SCV_LEDGER_KEY_CONTRACT_INSTANCE} }, - durability: %StellarBase.XDR.ContractDataDurability{identifier: :PERSISTENT}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + durability: %StellarBase.XDR.ContractDataDurability{identifier: :PERSISTENT} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_DATA} } @@ -374,13 +373,32 @@ defmodule Stellar.Test.Fixtures.XDR.Ledger do def ledger_key_contract_code do %StellarBase.XDR.LedgerKey{ entry: %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_CODE} } end + @spec ledger_key_config_setting() :: LedgerKey.t() + def ledger_key_config_setting do + %StellarBase.XDR.LedgerKey{ + entry: %StellarBase.XDR.LedgerKeyConfigSetting{ + config_setting_id: %StellarBase.XDR.ConfigSettingID{identifier: :MAX_SIZE_BYTES} + }, + type: %StellarBase.XDR.LedgerEntryType{identifier: :CONFIG_SETTING} + } + end + + @spec ledger_key_expiration() :: LedgerKey.t() + def ledger_key_expiration do + %StellarBase.XDR.LedgerKey{ + entry: %StellarBase.XDR.LedgerKeyExpiration{ + key_hash: %StellarBase.XDR.Hash{value: "ABC123"} + }, + type: %StellarBase.XDR.LedgerEntryType{identifier: :EXPIRATION} + } + end + @spec revoke_sponsorship(type :: atom(), args :: Keyword.t()) :: RevokeSponsorship.t() def revoke_sponsorship(:account, account_id: "GD726E62G6G4ANHWHIQTH5LNMFVF2EQSEXITB6DZCCTKVU6EQRRE2SJS" diff --git a/test/support/xdr_fixtures.ex b/test/support/xdr_fixtures.ex index 0dd2e9e..649c690 100644 --- a/test/support/xdr_fixtures.ex +++ b/test/support/xdr_fixtures.ex @@ -10,7 +10,7 @@ defmodule Stellar.Test.XDRFixtures do SignerKey, SorobanAuthorizedInvocation, SourceAccountContractID, - SCVec + InvokeContractArgs } alias Stellar.TxBuild.Transaction, as: Tx @@ -692,41 +692,33 @@ defmodule Stellar.Test.XDRFixtures do @spec host_function_xdr( type :: :invoke_contract, - args :: SCVec.t() + args :: InvokeContractArgs.t() ) :: HostFunction.t() def host_function_xdr( :invoke_contract, - %SCVec{} + %InvokeContractArgs{} ) do %StellarBase.XDR.HostFunction{ - value: %StellarBase.XDR.SCVec{ - items: [ - %StellarBase.XDR.SCVal{ - value: %StellarBase.XDR.SCAddress{ - sc_address: %StellarBase.XDR.Hash{ - value: - <<4, 97, 22, 140, 187, 174, 13, 169, 108, 84, 59, 113, 253, 87, 26, 236, 75, 68, - 84, 157, 80, 63, 154, 249, 231, 104, 92, 206, 219, 193, 97, 60>> - }, - type: %StellarBase.XDR.SCAddressType{ - identifier: :SC_ADDRESS_TYPE_CONTRACT - } - }, - type: %StellarBase.XDR.SCValType{identifier: :SCV_ADDRESS} - }, - %StellarBase.XDR.SCVal{ - value: %StellarBase.XDR.SCSymbol{value: "hello"}, - type: %StellarBase.XDR.SCValType{identifier: :SCV_SYMBOL} + value: %StellarBase.XDR.InvokeContractArgs{ + contract_address: %StellarBase.XDR.SCAddress{ + sc_address: %StellarBase.XDR.Hash{ + value: + <<4, 97, 22, 140, 187, 174, 13, 169, 108, 84, 59, 113, 253, 87, 26, 236, 75, 68, 84, + 157, 80, 63, 154, 249, 231, 104, 92, 206, 219, 193, 97, 60>> }, - %StellarBase.XDR.SCVal{ - value: %StellarBase.XDR.SCSymbol{value: "world"}, - type: %StellarBase.XDR.SCValType{identifier: :SCV_SYMBOL} - } - ] + type: %StellarBase.XDR.SCAddressType{identifier: :SC_ADDRESS_TYPE_CONTRACT} + }, + function_name: %StellarBase.XDR.SCSymbol{value: "hello"}, + args: %StellarBase.XDR.SCValList{ + items: [ + %StellarBase.XDR.SCVal{ + value: %StellarBase.XDR.SCSymbol{value: "world"}, + type: %StellarBase.XDR.SCValType{identifier: :SCV_SYMBOL} + } + ] + } }, - type: %StellarBase.XDR.HostFunctionType{ - identifier: :HOST_FUNCTION_TYPE_INVOKE_CONTRACT - } + type: %StellarBase.XDR.HostFunctionType{identifier: :HOST_FUNCTION_TYPE_INVOKE_CONTRACT} } end diff --git a/test/tx_build/config_setting_id_test.exs b/test/tx_build/config_setting_id_test.exs new file mode 100644 index 0000000..79fdfc6 --- /dev/null +++ b/test/tx_build/config_setting_id_test.exs @@ -0,0 +1,32 @@ +defmodule Stellar.TxBuild.ConfigSettingIDTest do + use ExUnit.Case + + alias Stellar.TxBuild.ConfigSettingID + + setup do + identifier = :max_size + + %{ + identifier: identifier + } + end + + test "new/1", %{identifier: identifier} do + %ConfigSettingID{identifier: ^identifier} = ConfigSettingID.new(identifier) + end + + test "new/1 with invalid args" do + {:error, :invalid_identifier} = ConfigSettingID.new(:invalid) + end + + test "to_xdr/1", %{identifier: identifier} do + %StellarBase.XDR.ConfigSettingID{identifier: :MAX_SIZE_BYTES} = + identifier + |> ConfigSettingID.new() + |> ConfigSettingID.to_xdr() + end + + test "to_xdr/1 with invalid struct" do + {:error, :invalid_struct} = ConfigSettingID.to_xdr(:invalid) + end +end diff --git a/test/tx_build/config_setting_test.exs b/test/tx_build/config_setting_test.exs new file mode 100644 index 0000000..c62c64b --- /dev/null +++ b/test/tx_build/config_setting_test.exs @@ -0,0 +1,32 @@ +defmodule Stellar.TxBuild.Ledger.ConfigSettingTest do + use ExUnit.Case + + alias StellarBase.XDR.{ConfigSettingID, LedgerKeyConfigSetting} + alias Stellar.TxBuild.ConfigSettingID, as: TxConfigSettingID + alias Stellar.TxBuild.Ledger.ConfigSetting + + setup do + %{ + config_setting_id: :max_size + } + end + + test "new/1", %{config_setting_id: config_setting_id} do + %ConfigSetting{ + config_setting_id: %TxConfigSettingID{identifier: ^config_setting_id} + } = ConfigSetting.new(config_setting_id) + end + + test "new/1 with invalid config_setting_id" do + {:error, :invalid_identifier} = ConfigSetting.new(:invalid) + end + + test "to_xdr/1", %{config_setting_id: config_setting_id} do + %LedgerKeyConfigSetting{ + config_setting_id: %ConfigSettingID{identifier: :MAX_SIZE_BYTES} + } = + config_setting_id + |> ConfigSetting.new() + |> ConfigSetting.to_xdr() + end +end diff --git a/test/tx_build/default_test.exs b/test/tx_build/default_test.exs index 76f18f0..18ad508 100644 --- a/test/tx_build/default_test.exs +++ b/test/tx_build/default_test.exs @@ -333,7 +333,7 @@ defmodule Stellar.TxBuild.DefaultTest do test "set_soroban_data/2", %{tx_build: tx_build} do soroban_data = - "AAAAAAAAAAEAAAAHmDXys1KuBimD87u2AiUG/jb5CqOkQW/qASpb6gMVRlsAAAAAAAAAAQAAAAYAAAABZ+A/h5d/0pJnw7wmIzptCvWoX0md87QkHN68pAOfYjgAAAAUAAAAAQAAAAAANwsUAAAUmAAAAQQAAAMcAAAAAAAAAJw=" + "AAAAAAAAAAEAAAAHjDfZjX1lF+yHF4743DgE1KQTRmGJtwRYh3hJXOzQ9k8AAAABAAAABgAAAAEJjPko7iuhBRtsY0aDQ2Einilpmj/rDyGds/qx5seSNAAAABQAAAABAE9HyAAAGDQAAADIAAAAAAAAACU=" {soroban_xdr_data, ""} = soroban_data |> Base.decode64!() |> SorobanTransactionData.decode_xdr!() @@ -352,7 +352,7 @@ defmodule Stellar.TxBuild.DefaultTest do test "set_soroban_data/2 piping_error" do soroban_data = - "AAAAAAAAAAEAAAAHmDXys1KuBimD87u2AiUG/jb5CqOkQW/qASpb6gMVRlsAAAAAAAAAAQAAAAYAAAABZ+A/h5d/0pJnw7wmIzptCvWoX0md87QkHN68pAOfYjgAAAAUAAAAAQAAAAAANwsUAAAUmAAAAQQAAAMcAAAAAAAAAJw=" + "AAAAAAAAAAEAAAAHjDfZjX1lF+yHF4743DgE1KQTRmGJtwRYh3hJXOzQ9k8AAAABAAAABgAAAAEJjPko7iuhBRtsY0aDQ2Einilpmj/rDyGds/qx5seSNAAAABQAAAABAE9HyAAAGDQAAADIAAAAAAAAACU=" {soroban_xdr_data, ""} = soroban_data |> Base.decode64!() |> SorobanTransactionData.decode_xdr!() diff --git a/test/tx_build/expiration_test.exs b/test/tx_build/expiration_test.exs new file mode 100644 index 0000000..7f0e4e6 --- /dev/null +++ b/test/tx_build/expiration_test.exs @@ -0,0 +1,30 @@ +defmodule Stellar.TxBuild.Ledger.ExpirationTest do + use ExUnit.Case + + alias Stellar.TxBuild.Ledger.Expiration + + setup do + hash = "hash_value" + + %{ + hash: hash + } + end + + test "new/2", %{hash: hash} do + %Expiration{key_hash: ^hash} = Expiration.new(hash) + end + + test "new/2 with invalid key hash" do + {:error, :invalid_expiration} = Expiration.new(:invalid_hash) + end + + test "to_xdr/1", %{hash: hash} do + %StellarBase.XDR.LedgerKeyExpiration{key_hash: %StellarBase.XDR.Hash{value: "hash_value"}} = + Expiration.new(hash) |> Expiration.to_xdr() + end + + test "to_xdr/1 with invalid Expiration" do + {:error, :invalid_struct} = Expiration.to_xdr("invalid_expiration") + end +end diff --git a/test/tx_build/hash_id_preimage_soroban_authorization_test.exs b/test/tx_build/hash_id_preimage_soroban_authorization_test.exs index 2d449a0..16fbdea 100644 --- a/test/tx_build/hash_id_preimage_soroban_authorization_test.exs +++ b/test/tx_build/hash_id_preimage_soroban_authorization_test.exs @@ -5,23 +5,22 @@ defmodule Stellar.TxBuild.HashIDPreimageSorobanAuthorizationTest do alias Stellar.TxBuild.{ HashIDPreimageSorobanAuthorization, - SCVec, SCVal, SCAddress, - SorobanAuthorizedContractFunction, + InvokeContractArgs, SorobanAuthorizedFunction, SorobanAuthorizedInvocation } setup do - fn_args = SCVec.new([SCVal.new(symbol: "dev")]) + fn_args = [SCVal.new(symbol: "dev")] contract_address = SCAddress.new("CBT6AP4HS575FETHYO6CMIZ2NUFPLKC7JGO7HNBEDTPLZJADT5RDRZP4") function_name = "hello" contract_fn = - SorobanAuthorizedContractFunction.new( + InvokeContractArgs.new( contract_address: contract_address, function_name: function_name, args: fn_args diff --git a/test/tx_build/hash_id_preimage_test.exs b/test/tx_build/hash_id_preimage_test.exs index 10a2ca6..5f96f6d 100644 --- a/test/tx_build/hash_id_preimage_test.exs +++ b/test/tx_build/hash_id_preimage_test.exs @@ -2,7 +2,7 @@ defmodule Stellar.TxBuild.HashIDPreimageTest do use ExUnit.Case alias Stellar.TxBuild.SCAddress, as: TxSCAddress - alias Stellar.TxBuild.SorobanAuthorizedContractFunction, as: TxSorobanAuthorizedContractFunction + alias Stellar.TxBuild.InvokeContractArgs, as: TxSorobanAuthorizedContractFunction alias Stellar.TxBuild.SorobanAuthorizedFunction, as: TxSorobanAuthorizedFunction alias Stellar.TxBuild.SorobanAuthorizedInvocation, as: TxSorobanAuthorizedInvocation @@ -14,7 +14,6 @@ defmodule Stellar.TxBuild.HashIDPreimageTest do alias Stellar.TxBuild.HashIDPreimage, as: TxHashIDPreimage alias Stellar.TxBuild.SequenceNumber, as: TxSequenceNumber alias Stellar.TxBuild.SCVal, as: TxSCVal - alias Stellar.TxBuild.SCVec, as: TxSCVec alias Stellar.TxBuild.HashIDPreimageOperationID, as: TxOperationID alias Stellar.TxBuild.HashIDPreimageRevokeID, as: TxRevokeID alias Stellar.TxBuild.HashIDPreimageContractID, as: TxHashIDPreimageContractID @@ -35,7 +34,7 @@ defmodule Stellar.TxBuild.HashIDPreimageTest do PublicKeyType, PoolID, HashIDPreimageRevokeID, - SorobanAuthorizedContractFunction, + InvokeContractArgs, SorobanAuthorizedFunction, SorobanAuthorizedFunctionType, SorobanAuthorizedInvocation, @@ -45,8 +44,8 @@ defmodule Stellar.TxBuild.HashIDPreimageTest do SCAddress, SCAddressType, SCVal, + SCValList, SCValType, - SCVec, SCSymbol, UInt256, UInt32, @@ -72,7 +71,7 @@ defmodule Stellar.TxBuild.HashIDPreimageTest do # SorobanAuthorizedInvocation contract_address = TxSCAddress.new("CACGCFUMXOXA3KLMKQ5XD7KXDLWEWRCUTVID7GXZ45UFZTW3YFQTZD6Y") function_name = "function_name" - args = TxSCVec.new([TxSCVal.new(i32: 654)]) + args = [TxSCVal.new(i32: 654)] contract_function = TxSorobanAuthorizedContractFunction.new( @@ -227,19 +226,17 @@ defmodule Stellar.TxBuild.HashIDPreimageTest do signature_expiration_ledger: %UInt32{datum: 123_541}, invocation: %SorobanAuthorizedInvocation{ function: %SorobanAuthorizedFunction{ - value: %SorobanAuthorizedContractFunction{ + value: %InvokeContractArgs{ contract_address: %SCAddress{ sc_address: %Hash{ value: <<4, 97, 22, 140, 187, 174, 13, 169, 108, 84, 59, 113, 253, 87, 26, 236, 75, 68, 84, 157, 80, 63, 154, 249, 231, 104, 92, 206, 219, 193, 97, 60>> }, - type: %SCAddressType{ - identifier: :SC_ADDRESS_TYPE_CONTRACT - } + type: %SCAddressType{identifier: :SC_ADDRESS_TYPE_CONTRACT} }, function_name: %SCSymbol{value: "function_name"}, - args: %SCVec{ + args: %SCValList{ items: [ %SCVal{ value: %Int32{datum: 654}, @@ -252,14 +249,10 @@ defmodule Stellar.TxBuild.HashIDPreimageTest do identifier: :SOROBAN_AUTHORIZED_FUNCTION_TYPE_CONTRACT_FN } }, - sub_invocations: %SorobanAuthorizedInvocationList{ - items: [] - } + sub_invocations: %SorobanAuthorizedInvocationList{items: []} } }, - type: %EnvelopeType{ - identifier: :ENVELOPE_TYPE_SOROBAN_AUTHORIZATION - } + type: %StellarBase.XDR.EnvelopeType{identifier: :ENVELOPE_TYPE_SOROBAN_AUTHORIZATION} } = TxHashIDPreimage.new(soroban_auth: soroban_auth) |> TxHashIDPreimage.to_xdr() diff --git a/test/tx_build/host_function_test.exs b/test/tx_build/host_function_test.exs index 9f3e110..6f33288 100644 --- a/test/tx_build/host_function_test.exs +++ b/test/tx_build/host_function_test.exs @@ -7,9 +7,9 @@ defmodule Stellar.TxBuild.HostFunctionTest do ContractIDPreimageFromAddress, CreateContractArgs, HostFunction, + InvokeContractArgs, SCAddress, - SCVal, - SCVec + SCVal } import Stellar.Test.XDRFixtures, @@ -18,13 +18,17 @@ defmodule Stellar.TxBuild.HostFunctionTest do describe "HostFunction" do setup do # :invoke_contract - contract_address = - "CACGCFUMXOXA3KLMKQ5XD7KXDLWEWRCUTVID7GXZ45UFZTW3YFQTZD6Y" - |> SCAddress.new() - |> (&SCVal.new(address: &1)).() + contract_address = SCAddress.new("CACGCFUMXOXA3KLMKQ5XD7KXDLWEWRCUTVID7GXZ45UFZTW3YFQTZD6Y") - function_name = SCVal.new(symbol: "hello") - invoke_args = SCVec.new([contract_address, function_name, SCVal.new(symbol: "world")]) + function_name = "hello" + args = [SCVal.new(symbol: "world")] + + invoke_args = + InvokeContractArgs.new( + contract_address: contract_address, + function_name: function_name, + args: args + ) # :create_contract wasm_id = diff --git a/test/tx_build/invoke_host_function_test.exs b/test/tx_build/invoke_host_function_test.exs index aba0c8b..7654189 100644 --- a/test/tx_build/invoke_host_function_test.exs +++ b/test/tx_build/invoke_host_function_test.exs @@ -6,8 +6,7 @@ defmodule Stellar.TxBuild.InvokeHostFunctionTest do InvokeHostFunction, SCAddress, SCVal, - SCVec, - SorobanAuthorizedContractFunction, + InvokeContractArgs, SorobanAuthorizedFunction, SorobanAuthorizedInvocation, SorobanCredentials, @@ -22,22 +21,20 @@ defmodule Stellar.TxBuild.InvokeHostFunctionTest do sc_contract_address = SCAddress.new("CBT6AP4HS575FETHYO6CMIZ2NUFPLKC7JGO7HNBEDTPLZJADT5RDRZP4") - contract_address = SCVal.new(address: sc_contract_address) - function_name = SCVal.new(symbol: "hello") - fn_args = SCVec.new([SCVal.new(symbol: "dev")]) - args = SCVec.new([contract_address, function_name, SCVal.new(string: "dev")]) - - host_function = HostFunction.new(invoke_contract: args) - - invoke_host_function_op = InvokeHostFunction.new(host_function: host_function, auths: []) + function_name = "hello" + fn_args = [SCVal.new(symbol: "dev")] contract_fn = - SorobanAuthorizedContractFunction.new( + InvokeContractArgs.new( contract_address: sc_contract_address, - function_name: "hello", + function_name: function_name, args: fn_args ) + host_function = HostFunction.new(invoke_contract: contract_fn) + + invoke_host_function_op = InvokeHostFunction.new(host_function: host_function, auths: []) + soroban_function = SorobanAuthorizedFunction.new(contract_fn: contract_fn) root_invocation = diff --git a/test/tx_build/ledger/contract_code_test.exs b/test/tx_build/ledger/contract_code_test.exs index 38e308b..329106f 100644 --- a/test/tx_build/ledger/contract_code_test.exs +++ b/test/tx_build/ledger/contract_code_test.exs @@ -5,46 +5,38 @@ defmodule Stellar.TxBuild.Ledger.ContractCodeTest do setup do hash = "ABC123" - body_type_data_entry = :data_entry - body_type_expiration_ext = :expiration_ext %{ - hash: hash, - body_type_data_entry: body_type_data_entry, - body_type_expiration_ext: body_type_expiration_ext + hash: hash } end - test "new/1 data_entry", %{hash: hash, body_type_data_entry: body_type} do - %ContractCode{hash: ^hash, body_type: ^body_type} = - ContractCode.new(hash: hash, body_type: body_type) + test "new/1 data_entry", %{hash: hash} do + %ContractCode{hash: ^hash} = ContractCode.new(hash: hash) end - test "new/1 expiration_ext", %{hash: hash, body_type_expiration_ext: body_type} do - %ContractCode{hash: ^hash, body_type: ^body_type} = - ContractCode.new([{:hash, hash}, {:body_type, body_type}]) + test "new/1 expiration_ext", %{hash: hash} do + %ContractCode{hash: ^hash} = ContractCode.new([{:hash, hash}]) end test "new/1 with invalid args" do {:error, :invalid_ledger_key_args} = ContractCode.new("ABC", "ABC") end - test "to_xdr/1 data_entry", %{hash: hash, body_type_data_entry: body_type} do + test "to_xdr/1 data_entry", %{hash: hash} do %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} } = - [{:hash, hash}, {:body_type, body_type}] + [{:hash, hash}] |> ContractCode.new() |> ContractCode.to_xdr() end - test "to_xdr/1 expiration_ext", %{hash: hash, body_type_expiration_ext: body_type} do + test "to_xdr/1 expiration_ext", %{hash: hash} do %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :EXPIRATION_EXTENSION} + hash: %StellarBase.XDR.Hash{value: "ABC123"} } = - [{:hash, hash}, {:body_type, body_type}] + [{:hash, hash}] |> ContractCode.new() |> ContractCode.to_xdr() end diff --git a/test/tx_build/ledger/contract_data_test.exs b/test/tx_build/ledger/contract_data_test.exs index 2c9c6ed..a707263 100644 --- a/test/tx_build/ledger/contract_data_test.exs +++ b/test/tx_build/ledger/contract_data_test.exs @@ -9,56 +9,46 @@ defmodule Stellar.TxBuild.Ledger.ContractDataTest do key = %Stellar.TxBuild.SCVal{type: :ledger_key_contract_instance} durability_temporary = :temporary durability_persistent = :persistent - body_type_data_entry = :data_entry - body_type_expiration_ext = :expiration_ext %{ contract: contract, key: key, durability_temporary: durability_temporary, - durability_persistent: durability_persistent, - body_type_data_entry: body_type_data_entry, - body_type_expiration_ext: body_type_expiration_ext + durability_persistent: durability_persistent } end test "new/1 data_entry", %{ contract: contract, key: key, - durability_temporary: durability, - body_type_data_entry: body_type + durability_temporary: durability } do %ContractData{ contract: ^contract, key: ^key, - durability: ^durability, - body_type: ^body_type + durability: ^durability } = ContractData.new( contract: contract, key: key, - durability: durability, - body_type: body_type + durability: durability ) end test "new/1 expiration_ext", %{ contract: contract, key: key, - durability_persistent: durability, - body_type_expiration_ext: body_type + durability_persistent: durability } do %ContractData{ contract: ^contract, key: ^key, - durability: ^durability, - body_type: ^body_type + durability: ^durability } = ContractData.new( contract: contract, key: key, - durability: durability, - body_type: body_type + durability: durability ) end @@ -74,23 +64,20 @@ defmodule Stellar.TxBuild.Ledger.ContractDataTest do test "new/1 with invalid key", %{ contract: contract, - durability_persistent: durability, - body_type_expiration_ext: body_type + durability_persistent: durability } do {:error, :invalid_key} = ContractData.new( contract: contract, key: "invalid_key", - durability: durability, - body_type: body_type + durability: durability ) end test "to_xdr/1 data_entry", %{ contract: contract, key: key, - durability_temporary: durability, - body_type_data_entry: body_type + durability_temporary: durability } do %StellarBase.XDR.LedgerKeyContractData{ contract: %StellarBase.XDR.SCAddress{ @@ -105,14 +92,12 @@ defmodule Stellar.TxBuild.Ledger.ContractDataTest do value: %StellarBase.XDR.Void{value: nil}, type: %StellarBase.XDR.SCValType{identifier: :SCV_LEDGER_KEY_CONTRACT_INSTANCE} }, - durability: %StellarBase.XDR.ContractDataDurability{identifier: :TEMPORARY}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + durability: %StellarBase.XDR.ContractDataDurability{identifier: :TEMPORARY} } = ContractData.new( contract: contract, key: key, - durability: durability, - body_type: body_type + durability: durability ) |> ContractData.to_xdr() end @@ -120,8 +105,7 @@ defmodule Stellar.TxBuild.Ledger.ContractDataTest do test "to_xdr/1 expiration_ext", %{ contract: contract, key: key, - durability_persistent: durability, - body_type_expiration_ext: body_type + durability_persistent: durability } do %StellarBase.XDR.LedgerKeyContractData{ contract: %StellarBase.XDR.SCAddress{ @@ -136,14 +120,12 @@ defmodule Stellar.TxBuild.Ledger.ContractDataTest do value: %StellarBase.XDR.Void{value: nil}, type: %StellarBase.XDR.SCValType{identifier: :SCV_LEDGER_KEY_CONTRACT_INSTANCE} }, - durability: %StellarBase.XDR.ContractDataDurability{identifier: :PERSISTENT}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :EXPIRATION_EXTENSION} + durability: %StellarBase.XDR.ContractDataDurability{identifier: :PERSISTENT} } = ContractData.new( contract: contract, key: key, - durability: durability, - body_type: body_type + durability: durability ) |> ContractData.to_xdr() end diff --git a/test/tx_build/ledger_footprint_test.exs b/test/tx_build/ledger_footprint_test.exs index b01a953..860e1aa 100644 --- a/test/tx_build/ledger_footprint_test.exs +++ b/test/tx_build/ledger_footprint_test.exs @@ -6,9 +6,8 @@ defmodule Stellar.TxBuild.LedgerFootprintTest do setup do hash = "ABC123" - body_type = :data_entry - contract_code_args = [hash: hash, body_type: body_type] + contract_code_args = [hash: hash] read_only = [LedgerKey.new({:contract_code, contract_code_args})] read_write = [ @@ -23,8 +22,7 @@ defmodule Stellar.TxBuild.LedgerFootprintTest do ledger_keys: [ %StellarBase.XDR.LedgerKey{ entry: %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_CODE} } @@ -34,15 +32,13 @@ defmodule Stellar.TxBuild.LedgerFootprintTest do ledger_keys: [ %StellarBase.XDR.LedgerKey{ entry: %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_CODE} }, %StellarBase.XDR.LedgerKey{ entry: %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_CODE} } diff --git a/test/tx_build/ledger_key_test.exs b/test/tx_build/ledger_key_test.exs index 383ce4b..541c0e9 100644 --- a/test/tx_build/ledger_key_test.exs +++ b/test/tx_build/ledger_key_test.exs @@ -2,14 +2,16 @@ defmodule Stellar.TxBuild.LedgerKeyTest do use ExUnit.Case alias Stellar.Test.Fixtures.XDR, as: XDRFixtures - alias Stellar.TxBuild.{LedgerKey, SCAddress, SCVal} + alias Stellar.TxBuild.{ConfigSettingID, LedgerKey, SCAddress, SCVal} alias Stellar.TxBuild.Ledger.{ Account, ClaimableBalance, + ConfigSetting, Data, ContractCode, ContractData, + Expiration, LiquidityPool, Offer, Trustline @@ -197,21 +199,18 @@ defmodule Stellar.TxBuild.LedgerKeyTest do contract = SCAddress.new("CCEMOFO5TE7FGOAJOA3RDHPC6RW3CFXRVIGOFQPFE4ZGOKA2QEA636SN") key = SCVal.new(ledger_key_contract_instance: nil) durability = :persistent - body_type = :data_entry contract_data = ContractData.new( contract: contract, key: key, - durability: durability, - body_type: body_type + durability: durability ) contract_data_args = [ contract: contract, key: key, - durability: durability, - body_type: body_type + durability: durability ] %{ @@ -240,11 +239,10 @@ defmodule Stellar.TxBuild.LedgerKeyTest do describe "contract_code" do setup do hash = "ABC123" - body_type = :data_entry - contract_code = ContractCode.new(hash: hash, body_type: body_type) + contract_code = ContractCode.new(hash: hash) - contract_code_args = [hash: hash, body_type: body_type] + contract_code_args = [hash: hash] %{ contract_code: contract_code, @@ -268,4 +266,58 @@ defmodule Stellar.TxBuild.LedgerKeyTest do |> LedgerKey.to_xdr() end end + + describe "config_setting" do + setup do + config_setting_arg = :max_size + + %{ + config_setting_arg: config_setting_arg, + xdr: XDRFixtures.ledger_key_config_setting() + } + end + + test "new/2", %{config_setting_arg: config_setting_arg} do + %LedgerKey{ + type: :config_setting, + entry: %ConfigSetting{ + config_setting_id: %ConfigSettingID{identifier: :max_size} + } + } = LedgerKey.new({:config_setting, config_setting_arg}) + end + + test "new/2 with invalid attributes" do + {:error, :invalid_config_setting} = LedgerKey.new({:config_setting, "invalid"}) + end + + test "to_xdr/1", %{config_setting_arg: config_setting_arg, xdr: xdr} do + ^xdr = + LedgerKey.new({:config_setting, config_setting_arg}) + |> LedgerKey.to_xdr() + end + end + + describe "expiration" do + setup do + hash = "ABC123" + + %{ + hash: hash, + expiration_entry: Expiration.new(hash), + xdr: XDRFixtures.ledger_key_expiration() + } + end + + test "new/2", %{hash: hash, expiration_entry: expiration_entry} do + %LedgerKey{entry: ^expiration_entry, type: :expiration} = LedgerKey.new({:expiration, hash}) + end + + test "new/2 with_invalid_attributes" do + {:error, :invalid_expiration} = LedgerKey.new({:expiration, 123}) + end + + test "to_xdr/1", %{hash: hash, xdr: xdr} do + ^xdr = LedgerKey.new({:expiration, hash}) |> LedgerKey.to_xdr() + end + end end diff --git a/test/tx_build/sc_error_test.exs b/test/tx_build/sc_error_test.exs index c0a8a14..55bca28 100644 --- a/test/tx_build/sc_error_test.exs +++ b/test/tx_build/sc_error_test.exs @@ -6,7 +6,7 @@ defmodule Stellar.TxBuild.SCErrorTest do setup do error_discriminants = [ - %{type: :contract, code: :arith_domain}, + %{type: :contract, code: 123}, %{type: :wasm_vm, code: :index_bounds}, %{type: :context, code: :invalid_input}, %{type: :storage, code: :missing_value}, @@ -14,7 +14,7 @@ defmodule Stellar.TxBuild.SCErrorTest do %{type: :crypto, code: :exceeded_limit}, %{type: :events, code: :invalid_action}, %{type: :budget, code: :internal_error}, - %{type: :code, code: :unexpected_type}, + %{type: :value, code: :unexpected_type}, %{type: :auth, code: :unexpected_size} ] @@ -22,15 +22,15 @@ defmodule Stellar.TxBuild.SCErrorTest do %{ xdr: %SCErrorXDR{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_CONTRACT}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_ARITH_DOMAIN} + value: %StellarBase.XDR.UInt32{datum: 123} }, type: :contract, - code: :arith_domain + code: 123 }, %{ xdr: %SCErrorXDR{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_WASM_VM}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_INDEX_BOUNDS} + value: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_INDEX_BOUNDS} }, type: :wasm_vm, code: :index_bounds @@ -38,7 +38,7 @@ defmodule Stellar.TxBuild.SCErrorTest do %{ xdr: %SCErrorXDR{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_CONTEXT}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_INVALID_INPUT} + value: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_INVALID_INPUT} }, type: :context, code: :invalid_input @@ -46,7 +46,7 @@ defmodule Stellar.TxBuild.SCErrorTest do %{ xdr: %SCErrorXDR{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_STORAGE}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_MISSING_VALUE} + value: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_MISSING_VALUE} }, type: :storage, code: :missing_value @@ -54,7 +54,7 @@ defmodule Stellar.TxBuild.SCErrorTest do %{ xdr: %SCErrorXDR{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_OBJECT}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_EXISTING_VALUE} + value: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_EXISTING_VALUE} }, type: :object, code: :existing_value @@ -62,7 +62,7 @@ defmodule Stellar.TxBuild.SCErrorTest do %{ xdr: %SCErrorXDR{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_CRYPTO}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_EXCEEDED_LIMIT} + value: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_EXCEEDED_LIMIT} }, type: :crypto, code: :exceeded_limit @@ -70,7 +70,7 @@ defmodule Stellar.TxBuild.SCErrorTest do %{ xdr: %SCErrorXDR{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_EVENTS}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_INVALID_ACTION} + value: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_INVALID_ACTION} }, type: :events, code: :invalid_action @@ -78,7 +78,7 @@ defmodule Stellar.TxBuild.SCErrorTest do %{ xdr: %SCErrorXDR{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_BUDGET}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_INTERNAL_ERROR} + value: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_INTERNAL_ERROR} }, type: :budget, code: :internal_error @@ -86,15 +86,15 @@ defmodule Stellar.TxBuild.SCErrorTest do %{ xdr: %SCErrorXDR{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_VALUE}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_UNEXPECTED_TYPE} + value: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_UNEXPECTED_TYPE} }, - type: :code, + type: :value, code: :unexpected_type }, %{ xdr: %SCErrorXDR{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_AUTH}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_UNEXPECTED_SIZE} + value: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_UNEXPECTED_SIZE} }, type: :auth, code: :unexpected_size diff --git a/test/tx_build/sc_val_test.exs b/test/tx_build/sc_val_test.exs index 928f7a4..428dfdb 100644 --- a/test/tx_build/sc_val_test.exs +++ b/test/tx_build/sc_val_test.exs @@ -93,7 +93,7 @@ defmodule Stellar.TxBuild.SCValTest do %{sc_error: SCError.new(crypto: :exceeded_limit)}, %{sc_error: SCError.new(events: :invalid_action)}, %{sc_error: SCError.new(budget: :internal_error)}, - %{sc_error: SCError.new(code: :unexpected_type)}, + %{sc_error: SCError.new(value: :unexpected_type)}, %{sc_error: SCError.new(auth: :unexpected_size)} ] @@ -109,7 +109,7 @@ defmodule Stellar.TxBuild.SCValTest do val_type: :SCV_ERROR, module: %StellarBase.XDR.SCError{ type: %StellarBase.XDR.SCErrorType{identifier: :SCE_CONTRACT}, - code: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_ARITH_DOMAIN} + value: %StellarBase.XDR.SCErrorCode{identifier: :SCEC_ARITH_DOMAIN} }, type: :error, value: SCError.new(contract: :arith_domain) diff --git a/test/tx_build/sc_vec_test.exs b/test/tx_build/sc_vec_test.exs new file mode 100644 index 0000000..1735b57 --- /dev/null +++ b/test/tx_build/sc_vec_test.exs @@ -0,0 +1,47 @@ +defmodule Stellar.TxBuild.SCVecTest do + use ExUnit.Case + + alias Stellar.TxBuild.SCVec + alias Stellar.TxBuild.SCVal + + setup do + items = [ + %SCVal{type: :i64, value: 42}, + %SCVal{type: :string, value: "Hello, World!"} + ] + + %{ + items: items + } + end + + test "new/1", %{items: items} do + %SCVec{items: ^items} = SCVec.new(items) + end + + test "new/1 with invalid args" do + {:error, :invalid_vec} = SCVec.new([42, "Hello, World!"]) + end + + test "new/1 with invalid items" do + {:error, :invalid_sc_vec} = SCVec.new(:invalid) + end + + test "to_xdr/1", %{items: items} do + %StellarBase.XDR.SCValList{ + items: [ + %StellarBase.XDR.SCVal{ + value: %StellarBase.XDR.Int64{datum: 42}, + type: %StellarBase.XDR.SCValType{identifier: :SCV_I64} + }, + %StellarBase.XDR.SCVal{ + value: %StellarBase.XDR.SCString{value: "Hello, World!"}, + type: %StellarBase.XDR.SCValType{identifier: :SCV_STRING} + } + ] + } = + items + |> SCVec.new() + |> SCVec.to_xdr() + end +end diff --git a/test/tx_build/soroba_tarsaction_data_test.exs b/test/tx_build/soroba_tarsaction_data_test.exs index 9a0c3d8..359a583 100644 --- a/test/tx_build/soroba_tarsaction_data_test.exs +++ b/test/tx_build/soroba_tarsaction_data_test.exs @@ -6,9 +6,8 @@ defmodule Stellar.TxBuild.SorobanTransactionDataTest do setup do hash = "ABC123" - body_type = :data_entry - contract_code_args = [hash: hash, body_type: body_type] + contract_code_args = [hash: hash] read_only = [LedgerKey.new({:contract_code, contract_code_args})] read_write = [ @@ -20,15 +19,13 @@ defmodule Stellar.TxBuild.SorobanTransactionDataTest do instructions = 1000 read_bytes = 1024 write_bytes = 512 - extended_meta_data_size_bytes = 256 resources = SorobanResources.new( footprint: footprint, instructions: instructions, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes ) refundable_fee = 100 @@ -47,8 +44,7 @@ defmodule Stellar.TxBuild.SorobanTransactionDataTest do ledger_keys: [ %StellarBase.XDR.LedgerKey{ entry: %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_CODE} } @@ -58,15 +54,13 @@ defmodule Stellar.TxBuild.SorobanTransactionDataTest do ledger_keys: [ %StellarBase.XDR.LedgerKey{ entry: %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_CODE} }, %StellarBase.XDR.LedgerKey{ entry: %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_CODE} } @@ -75,8 +69,7 @@ defmodule Stellar.TxBuild.SorobanTransactionDataTest do }, instructions: %StellarBase.XDR.UInt32{datum: 1000}, read_bytes: %StellarBase.XDR.UInt32{datum: 1024}, - write_bytes: %StellarBase.XDR.UInt32{datum: 512}, - extended_meta_data_size_bytes: %StellarBase.XDR.UInt32{datum: 256} + write_bytes: %StellarBase.XDR.UInt32{datum: 512} }, refundable_fee: %StellarBase.XDR.Int64{datum: 100} } diff --git a/test/tx_build/soroban_address_credentials_test.exs b/test/tx_build/soroban_address_credentials_test.exs index d2d5436..9c76047 100644 --- a/test/tx_build/soroban_address_credentials_test.exs +++ b/test/tx_build/soroban_address_credentials_test.exs @@ -1,14 +1,15 @@ defmodule Stellar.TxBuild.SorobanAddressCredentialsTest do use ExUnit.Case - alias Stellar.TxBuild.SCVal - alias Stellar.TxBuild.SCVec - alias Stellar.TxBuild.SCAddress - alias Stellar.TxBuild.SorobanAddressCredentials + alias Stellar.TxBuild.{ + SCAddress, + SCVal, + SorobanAddressCredentials + } setup do address = SCAddress.new("GBNDWIM7DPYZJ2RLJ3IESXBIO4C2SVF6PWZXS3DLODJSBQWBMKY5U4M3") - signature_args = SCVec.new([SCVal.new(symbol: "dev")]) + signature = SCVal.new(symbol: "dev") nonce = 544_841 signature_expiration_ledger = 106_977 @@ -28,25 +29,21 @@ defmodule Stellar.TxBuild.SorobanAddressCredentialsTest do }, nonce: %StellarBase.XDR.Int64{datum: 544_841}, signature_expiration_ledger: %StellarBase.XDR.UInt32{datum: 106_977}, - signature_args: %StellarBase.XDR.SCVec{ - items: [ - %StellarBase.XDR.SCVal{ - value: %StellarBase.XDR.SCSymbol{value: "dev"}, - type: %StellarBase.XDR.SCValType{identifier: :SCV_SYMBOL} - } - ] + signature: %StellarBase.XDR.SCVal{ + value: %StellarBase.XDR.SCSymbol{value: "dev"}, + type: %StellarBase.XDR.SCValType{identifier: :SCV_SYMBOL} } } %{ address: address, - signature_args: signature_args, + signature: signature, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger, soroban_address_credentials: SorobanAddressCredentials.new( address: address, - signature_args: signature_args, + signature: signature, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger ), @@ -56,47 +53,47 @@ defmodule Stellar.TxBuild.SorobanAddressCredentialsTest do test "new/2", %{ address: address, - signature_args: signature_args, + signature: signature, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger } do %SorobanAddressCredentials{ address: ^address, - signature_args: ^signature_args, + signature: ^signature, nonce: ^nonce, signature_expiration_ledger: ^signature_expiration_ledger } = SorobanAddressCredentials.new( address: address, - signature_args: signature_args, + signature: signature, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger ) end test "new/2 with invalid address", %{ - signature_args: signature_args, + signature: signature, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger } do {:error, :invalid_address} = SorobanAddressCredentials.new( address: :invalid, - signature_args: signature_args, + signature: signature, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger ) end - test "new/2 with invalid signature_args", %{ + test "new/2 with invalid signature", %{ address: address, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger } do - {:error, :invalid_args} = + {:error, :invalid_sc_val} = SorobanAddressCredentials.new( address: address, - signature_args: :invalid, + signature: :invalid, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger ) @@ -104,13 +101,13 @@ defmodule Stellar.TxBuild.SorobanAddressCredentialsTest do test "new/2 with invalid nonce", %{ address: address, - signature_args: signature_args, + signature: signature, signature_expiration_ledger: signature_expiration_ledger } do {:error, :invalid_nonce} = SorobanAddressCredentials.new( address: address, - signature_args: signature_args, + signature: signature, nonce: :invalid, signature_expiration_ledger: signature_expiration_ledger ) @@ -118,13 +115,13 @@ defmodule Stellar.TxBuild.SorobanAddressCredentialsTest do test "new/2 with invalid signature_expiration_ledger", %{ address: address, - signature_args: signature_args, + signature: signature, nonce: nonce } do {:error, :invalid_signature_expiration_ledger} = SorobanAddressCredentials.new( address: address, - signature_args: signature_args, + signature: signature, nonce: nonce, signature_expiration_ledger: :invalid ) diff --git a/test/tx_build/soroban_authorization_entry_test.exs b/test/tx_build/soroban_authorization_entry_test.exs index adb0655..23cd783 100644 --- a/test/tx_build/soroban_authorization_entry_test.exs +++ b/test/tx_build/soroban_authorization_entry_test.exs @@ -5,24 +5,23 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntryTest do SCAddress, SCMapEntry, SCVal, - SCVec, SorobanAddressCredentials, SorobanAuthorizedInvocation, SorobanAuthorizedFunction, - SorobanAuthorizedContractFunction, + InvokeContractArgs, SorobanAuthorizationEntry, SorobanCredentials } setup do - fn_args = SCVec.new([SCVal.new(symbol: "dev")]) + fn_args = [SCVal.new(symbol: "dev")] contract_address = SCAddress.new("CBT6AP4HS575FETHYO6CMIZ2NUFPLKC7JGO7HNBEDTPLZJADT5RDRZP4") function_name = "hello" contract_fn = - SorobanAuthorizedContractFunction.new( + InvokeContractArgs.new( contract_address: contract_address, function_name: function_name, args: fn_args @@ -47,7 +46,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntryTest do }, root_invocation: %StellarBase.XDR.SorobanAuthorizedInvocation{ function: %StellarBase.XDR.SorobanAuthorizedFunction{ - value: %StellarBase.XDR.SorobanAuthorizedContractFunction{ + value: %StellarBase.XDR.InvokeContractArgs{ contract_address: %StellarBase.XDR.SCAddress{ sc_address: %StellarBase.XDR.Hash{ value: @@ -57,7 +56,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntryTest do type: %StellarBase.XDR.SCAddressType{identifier: :SC_ADDRESS_TYPE_CONTRACT} }, function_name: %StellarBase.XDR.SCSymbol{value: "hello"}, - args: %StellarBase.XDR.SCVec{ + args: %StellarBase.XDR.SCValList{ items: [ %StellarBase.XDR.SCVal{ value: %StellarBase.XDR.SCSymbol{value: "dev"}, @@ -83,7 +82,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntryTest do address: address, nonce: nonce, signature_expiration_ledger: signature_expiration_ledger, - signature_args: SCVec.new([]) + signature: SCVal.new(vec: []) ) address_credentials = SorobanCredentials.new(address: soroban_address_credentials) @@ -100,12 +99,13 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntryTest do soroban_auth_entry: soroban_auth_entry, xdr: xdr, base_64: - "AAAAAQAAAAAAAAAAyU545WHCcUig2re/I2xMg5FaqNroaTV+AXQbahq8ftY69WqNb/7SRQAAAAAAAAAAAAAAAAAAAAEYaWENrVfrP3DtntX8suGy/f52r9ikRgXsGYWrbdStxwAAAANpbmMAAAAAAgAAABIAAAAAAAAAAMlOeOVhwnFIoNq3vyNsTIORWqja6Gk1fgF0G2oavH7WAAAACQAAAAAAAAAAAAAAAAAAAAIAAAAA", + "AAAAAQAAAAAAAAAAyU545WHCcUig2re/I2xMg5FaqNroaTV+AXQbahq8ftYOvJBkmBOF7wAAAAAAAAABAAAAAAAAAAEJjPko7iuhBRtsY0aDQ2Einilpmj/rDyGds/qx5seSNAAAAANpbmMAAAAAAgAAABIAAAAAAAAAAMlOeOVhwnFIoNq3vyNsTIORWqja6Gk1fgF0G2oavH7WAAAACQAAAAAAAAAAAAAAAAAAAAIAAAAA", secret_key: "SCAVFA3PI3MJLTQNMXOUNBSEUOSY66YMG3T2KCQKLQBENNVLVKNPV3EK", latest_ledger: 164_256, sign_xdr: - "AAAAAQAAAAAAAAAAyU545WHCcUig2re/I2xMg5FaqNroaTV+AXQbahq8ftY69WqNb/7SRQACgaMAAAABAAAAEQAAAAEAAAACAAAADwAAAApwdWJsaWNfa2V5AAAAAAANAAAAIMlOeOVhwnFIoNq3vyNsTIORWqja6Gk1fgF0G2oavH7WAAAADwAAAAlzaWduYXR1cmUAAAAAAAANAAAAQOdgROP+0omN51SCii/Ttcy5PhyPfGIaPWG4FBvQNEp1jGMio+lH5IKE5boB5dvdbR0wNixWSHXZBb/35hyftAIAAAAAAAAAARhpYQ2tV+s/cO2e1fyy4bL9/nav2KRGBewZhatt1K3HAAAAA2luYwAAAAACAAAAEgAAAAAAAAAAyU545WHCcUig2re/I2xMg5FaqNroaTV+AXQbahq8ftYAAAAJAAAAAAAAAAAAAAAAAAAAAgAAAAA=", - soroban_auth_entry_with_address_credentials: soroban_auth_entry_with_address_credentials + "AAAAAQAAAAAAAAAAyU545WHCcUig2re/I2xMg5FaqNroaTV+AXQbahq8ftYOvJBkmBOF7wACgaMAAAAQAAAAAQAAAAEAAAARAAAAAQAAAAIAAAAPAAAACnB1YmxpY19rZXkAAAAAAA0AAAAgyU545WHCcUig2re/I2xMg5FaqNroaTV+AXQbahq8ftYAAAAPAAAACXNpZ25hdHVyZQAAAAAAAA0AAABAIVZ/t4FbgBOE7+B6u41RkQhUrePyoyxrNwGh8oN2HtGtmyuxBwlU49nBUgUwBHmHiQMIMBEW7IbyPNGSuK4YCAAAAAAAAAABCYz5KO4roQUbbGNGg0NhIp4paZo/6w8hnbP6sebHkjQAAAADaW5jAAAAAAIAAAASAAAAAAAAAADJTnjlYcJxSKDat78jbEyDkVqo2uhpNX4BdBtqGrx+1gAAAAkAAAAAAAAAAAAAAAAAAAACAAAAAA==", + soroban_auth_entry_with_address_credentials: soroban_auth_entry_with_address_credentials, + address_credentials: address_credentials } end @@ -137,8 +137,15 @@ defmodule Stellar.TxBuild.SorobanAuthorizationEntryTest do } do %SorobanAuthorizationEntry{ credentials: %SorobanAddressCredentials{ - signature_args: %SCVec{ - items: [ + address: %SCAddress{ + type: :account, + value: "GDEU46HFMHBHCSFA3K336I3MJSBZCWVI3LUGSNL6AF2BW2Q2XR7NNAPM" + }, + nonce: 1_078_069_780, + signature_expiration_ledger: 164_080, + signature: %SCVal{ + type: :vec, + value: [ %SCVal{ type: :map, value: [ diff --git a/test/tx_build/soroban_authorized_contract_function_test.exs b/test/tx_build/soroban_authorized_contract_function_test.exs index b5a4c3d..b5c0fde 100644 --- a/test/tx_build/soroban_authorized_contract_function_test.exs +++ b/test/tx_build/soroban_authorized_contract_function_test.exs @@ -1,26 +1,23 @@ -defmodule Stellar.TxBuild.SorobanAuthorizedContractFunctionTest do +defmodule Stellar.TxBuild.InvokeContractArgsTest do use ExUnit.Case - alias Stellar.TxBuild.SCAddress - alias Stellar.TxBuild.SCVec - alias Stellar.TxBuild.SCVal - alias Stellar.TxBuild.SorobanAuthorizedContractFunction + alias Stellar.TxBuild.{InvokeContractArgs, SCAddress, SCVal} setup do - fn_args = SCVec.new([SCVal.new(symbol: "dev")]) + fn_args = [SCVal.new(symbol: "dev")] contract_address = SCAddress.new("CBT6AP4HS575FETHYO6CMIZ2NUFPLKC7JGO7HNBEDTPLZJADT5RDRZP4") function_name = "hello" contract_fn = - SorobanAuthorizedContractFunction.new( + InvokeContractArgs.new( contract_address: contract_address, function_name: function_name, args: fn_args ) - xdr = %StellarBase.XDR.SorobanAuthorizedContractFunction{ + xdr = %StellarBase.XDR.InvokeContractArgs{ contract_address: %StellarBase.XDR.SCAddress{ sc_address: %StellarBase.XDR.Hash{ value: @@ -30,7 +27,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizedContractFunctionTest do type: %StellarBase.XDR.SCAddressType{identifier: :SC_ADDRESS_TYPE_CONTRACT} }, function_name: %StellarBase.XDR.SCSymbol{value: "hello"}, - args: %StellarBase.XDR.SCVec{ + args: %StellarBase.XDR.SCValList{ items: [ %StellarBase.XDR.SCVal{ value: %StellarBase.XDR.SCSymbol{value: "dev"}, @@ -54,12 +51,12 @@ defmodule Stellar.TxBuild.SorobanAuthorizedContractFunctionTest do function_name: function_name, fn_args: fn_args } do - %SorobanAuthorizedContractFunction{ + %InvokeContractArgs{ contract_address: ^contract_address, function_name: ^function_name, args: ^fn_args } = - SorobanAuthorizedContractFunction.new( + InvokeContractArgs.new( contract_address: contract_address, function_name: function_name, args: fn_args @@ -71,7 +68,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizedContractFunctionTest do fn_args: fn_args } do {:error, :invalid_address} = - SorobanAuthorizedContractFunction.new( + InvokeContractArgs.new( contract_address: :invalid, function_name: function_name, args: fn_args @@ -83,7 +80,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizedContractFunctionTest do fn_args: fn_args } do {:error, :invalid_function_name} = - SorobanAuthorizedContractFunction.new( + InvokeContractArgs.new( contract_address: contract_address, function_name: :invalid, args: fn_args @@ -94,8 +91,8 @@ defmodule Stellar.TxBuild.SorobanAuthorizedContractFunctionTest do contract_address: contract_address, function_name: function_name } do - {:error, :invalid_args} = - SorobanAuthorizedContractFunction.new( + {:error, :invalid_vals} = + InvokeContractArgs.new( contract_address: contract_address, function_name: function_name, args: :invalid @@ -103,15 +100,14 @@ defmodule Stellar.TxBuild.SorobanAuthorizedContractFunctionTest do end test "new/2 with invalid args" do - {:error, :invalid_soroban_auth_contract_function_args} = - SorobanAuthorizedContractFunction.new(:invalid) + {:error, :invalid_soroban_auth_contract_function_args} = InvokeContractArgs.new(:invalid) end test "to_xdr/1", %{contract_fn: contract_fn, xdr: xdr} do - ^xdr = SorobanAuthorizedContractFunction.to_xdr(contract_fn) + ^xdr = InvokeContractArgs.to_xdr(contract_fn) end test "to_xdr/1 error" do - {:error, :invalid_struct} = SorobanAuthorizedContractFunction.to_xdr(:invalid) + {:error, :invalid_struct} = InvokeContractArgs.to_xdr(:invalid) end end diff --git a/test/tx_build/soroban_authorized_function_test.exs b/test/tx_build/soroban_authorized_function_test.exs index af8b15f..343cdc9 100644 --- a/test/tx_build/soroban_authorized_function_test.exs +++ b/test/tx_build/soroban_authorized_function_test.exs @@ -2,26 +2,25 @@ defmodule Stellar.TxBuild.SorobanAuthorizedFunctionTest do use ExUnit.Case alias Stellar.TxBuild.{ - SCVec, SCVal, CreateContractArgs, ContractIDPreimage, ContractIDPreimageFromAddress, SCAddress, ContractExecutable, - SorobanAuthorizedContractFunction, + InvokeContractArgs, SorobanAuthorizedFunction } setup do - fn_args = SCVec.new([SCVal.new(symbol: "dev")]) + fn_args = [SCVal.new(symbol: "dev")] contract_address = SCAddress.new("CBT6AP4HS575FETHYO6CMIZ2NUFPLKC7JGO7HNBEDTPLZJADT5RDRZP4") function_name = "hello" contract_fn = - SorobanAuthorizedContractFunction.new( + InvokeContractArgs.new( contract_address: contract_address, function_name: function_name, args: fn_args @@ -60,7 +59,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizedFunctionTest do xdr_discriminants = [ %{ xdr: %StellarBase.XDR.SorobanAuthorizedFunction{ - value: %StellarBase.XDR.SorobanAuthorizedContractFunction{ + value: %StellarBase.XDR.InvokeContractArgs{ contract_address: %StellarBase.XDR.SCAddress{ sc_address: %StellarBase.XDR.Hash{ value: @@ -70,7 +69,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizedFunctionTest do type: %StellarBase.XDR.SCAddressType{identifier: :SC_ADDRESS_TYPE_CONTRACT} }, function_name: %StellarBase.XDR.SCSymbol{value: "hello"}, - args: %StellarBase.XDR.SCVec{ + args: %StellarBase.XDR.SCValList{ items: [ %StellarBase.XDR.SCVal{ value: %StellarBase.XDR.SCSymbol{value: "dev"}, diff --git a/test/tx_build/soroban_authorized_invocation_test.exs b/test/tx_build/soroban_authorized_invocation_test.exs index 4d81214..f833231 100644 --- a/test/tx_build/soroban_authorized_invocation_test.exs +++ b/test/tx_build/soroban_authorized_invocation_test.exs @@ -2,23 +2,22 @@ defmodule Stellar.TxBuild.SorobanAuthorizedInvocationTest do use ExUnit.Case alias Stellar.TxBuild.{ - SCVec, SCVal, SCAddress, - SorobanAuthorizedContractFunction, + InvokeContractArgs, SorobanAuthorizedFunction, SorobanAuthorizedInvocation } setup do - fn_args = SCVec.new([SCVal.new(symbol: "dev")]) + fn_args = [SCVal.new(symbol: "dev")] contract_address = SCAddress.new("CBT6AP4HS575FETHYO6CMIZ2NUFPLKC7JGO7HNBEDTPLZJADT5RDRZP4") function_name = "hello" contract_fn = - SorobanAuthorizedContractFunction.new( + InvokeContractArgs.new( contract_address: contract_address, function_name: function_name, args: fn_args @@ -32,7 +31,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizedInvocationTest do auth_invocation_xdr = %StellarBase.XDR.SorobanAuthorizedInvocation{ function: %StellarBase.XDR.SorobanAuthorizedFunction{ - value: %StellarBase.XDR.SorobanAuthorizedContractFunction{ + value: %StellarBase.XDR.InvokeContractArgs{ contract_address: %StellarBase.XDR.SCAddress{ sc_address: %StellarBase.XDR.Hash{ value: @@ -42,7 +41,7 @@ defmodule Stellar.TxBuild.SorobanAuthorizedInvocationTest do type: %StellarBase.XDR.SCAddressType{identifier: :SC_ADDRESS_TYPE_CONTRACT} }, function_name: %StellarBase.XDR.SCSymbol{value: "hello"}, - args: %StellarBase.XDR.SCVec{ + args: %StellarBase.XDR.SCValList{ items: [ %StellarBase.XDR.SCVal{ value: %StellarBase.XDR.SCSymbol{value: "dev"}, diff --git a/test/tx_build/soroban_credentials_test.exs b/test/tx_build/soroban_credentials_test.exs index e035824..067ac44 100644 --- a/test/tx_build/soroban_credentials_test.exs +++ b/test/tx_build/soroban_credentials_test.exs @@ -2,7 +2,6 @@ defmodule Stellar.TxBuild.SorobanCredentialsTest do use ExUnit.Case alias Stellar.TxBuild.{ - SCVec, SCVal, SorobanCredentials, SCAddress, @@ -11,14 +10,14 @@ defmodule Stellar.TxBuild.SorobanCredentialsTest do setup do address = SCAddress.new("GBNDWIM7DPYZJ2RLJ3IESXBIO4C2SVF6PWZXS3DLODJSBQWBMKY5U4M3") - signature_args = SCVec.new([SCVal.new(symbol: "dev")]) + signature = SCVal.new(symbol: "dev") soroban_address_credentials = SorobanAddressCredentials.new( address: address, nonce: 123_135, signature_expiration_ledger: 123_515, - signature_args: signature_args + signature: signature ) discriminants = [ @@ -53,27 +52,19 @@ defmodule Stellar.TxBuild.SorobanCredentialsTest do <<90, 59, 33, 159, 27, 241, 148, 234, 43, 78, 208, 73, 92, 40, 119, 5, 169, 84, 190, 125, 179, 121, 108, 107, 112, 211, 32, 194, 193, 98, 177, 218>> }, - type: %StellarBase.XDR.PublicKeyType{ - identifier: :PUBLIC_KEY_TYPE_ED25519 - } + type: %StellarBase.XDR.PublicKeyType{identifier: :PUBLIC_KEY_TYPE_ED25519} } }, type: %StellarBase.XDR.SCAddressType{identifier: :SC_ADDRESS_TYPE_ACCOUNT} }, nonce: %StellarBase.XDR.Int64{datum: 123_135}, signature_expiration_ledger: %StellarBase.XDR.UInt32{datum: 123_515}, - signature_args: %StellarBase.XDR.SCVec{ - items: [ - %StellarBase.XDR.SCVal{ - value: %StellarBase.XDR.SCSymbol{value: "dev"}, - type: %StellarBase.XDR.SCValType{identifier: :SCV_SYMBOL} - } - ] + signature: %StellarBase.XDR.SCVal{ + value: %StellarBase.XDR.SCSymbol{value: "dev"}, + type: %StellarBase.XDR.SCValType{identifier: :SCV_SYMBOL} } }, - type: %StellarBase.XDR.SorobanCredentialsType{ - identifier: :SOROBAN_CREDENTIALS_ADDRESS - } + type: %StellarBase.XDR.SorobanCredentialsType{identifier: :SOROBAN_CREDENTIALS_ADDRESS} }, type: :address, value: soroban_address_credentials diff --git a/test/tx_build/soroban_resources_test.exs b/test/tx_build/soroban_resources_test.exs index 0a7b001..665f506 100644 --- a/test/tx_build/soroban_resources_test.exs +++ b/test/tx_build/soroban_resources_test.exs @@ -7,9 +7,8 @@ defmodule Stellar.TxBuild.SorobanResourcesTest do setup do hash = "ABC123" - body_type = :data_entry - contract_code_args = [hash: hash, body_type: body_type] + contract_code_args = [hash: hash] read_only = [LedgerKey.new({:contract_code, contract_code_args})] read_write = [ @@ -21,14 +20,12 @@ defmodule Stellar.TxBuild.SorobanResourcesTest do instructions = 1000 read_bytes = 1024 write_bytes = 512 - extended_meta_data_size_bytes = 256 %{ footprint: footprint, instructions: instructions, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes } end @@ -36,54 +33,47 @@ defmodule Stellar.TxBuild.SorobanResourcesTest do footprint: footprint, instructions: instructions, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes } do %SorobanResources{ footprint: ^footprint, instructions: ^instructions, read_bytes: ^read_bytes, - write_bytes: ^write_bytes, - extended_meta_data_size_bytes: ^extended_meta_data_size_bytes + write_bytes: ^write_bytes } = SorobanResources.new( footprint: footprint, instructions: instructions, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes ) end test "new/1 with invalid instructions", %{ footprint: footprint, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes } do {:error, :invalid_soroban_resources_args} = SorobanResources.new( footprint: footprint, instructions: "invalid_instructions", read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes ) end test "new/1 with invalid footprint", %{ instructions: instructions, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes } do {:error, :invalid_footprint} = SorobanResources.new( footprint: "invalid", instructions: instructions, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes ) end @@ -91,8 +81,7 @@ defmodule Stellar.TxBuild.SorobanResourcesTest do footprint: footprint, instructions: instructions, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes } do %StellarBase.XDR.SorobanResources{ footprint: %StellarBase.XDR.LedgerFootprint{ @@ -100,8 +89,7 @@ defmodule Stellar.TxBuild.SorobanResourcesTest do ledger_keys: [ %StellarBase.XDR.LedgerKey{ entry: %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_CODE} } @@ -111,15 +99,13 @@ defmodule Stellar.TxBuild.SorobanResourcesTest do ledger_keys: [ %StellarBase.XDR.LedgerKey{ entry: %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_CODE} }, %StellarBase.XDR.LedgerKey{ entry: %StellarBase.XDR.LedgerKeyContractCode{ - hash: %StellarBase.XDR.Hash{value: "ABC123"}, - body_type: %StellarBase.XDR.ContractEntryBodyType{identifier: :DATA_ENTRY} + hash: %StellarBase.XDR.Hash{value: "ABC123"} }, type: %StellarBase.XDR.LedgerEntryType{identifier: :CONTRACT_CODE} } @@ -128,15 +114,13 @@ defmodule Stellar.TxBuild.SorobanResourcesTest do }, instructions: %StellarBase.XDR.UInt32{datum: 1000}, read_bytes: %StellarBase.XDR.UInt32{datum: 1024}, - write_bytes: %StellarBase.XDR.UInt32{datum: 512}, - extended_meta_data_size_bytes: %StellarBase.XDR.UInt32{datum: 256} + write_bytes: %StellarBase.XDR.UInt32{datum: 512} } = SorobanResources.new( footprint: footprint, instructions: instructions, read_bytes: read_bytes, - write_bytes: write_bytes, - extended_meta_data_size_bytes: extended_meta_data_size_bytes + write_bytes: write_bytes ) |> SorobanResources.to_xdr() end diff --git a/test/tx_build/validations_test.exs b/test/tx_build/validations_test.exs index 24e9bf2..f1335c4 100644 --- a/test/tx_build/validations_test.exs +++ b/test/tx_build/validations_test.exs @@ -15,7 +15,8 @@ defmodule Stellar.TxBuild.ValidationsTest do SCAddress, SequenceNumber, PoolID, - Price + Price, + SCVal } setup do @@ -166,4 +167,38 @@ defmodule Stellar.TxBuild.ValidationsTest do test "validate_vec error" do {:error, :invalid_args} = Validations.validate_vec(:invalid) end + + test "validate_sc_vals/1" do + sc_val = %SCVal{type: :int64, value: 42} + {:ok, [%SCVal{type: :int64, value: 42}]} = Validations.validate_sc_vals({:vals, [sc_val]}) + end + + test "validate_sc_vals/1 error invalid vals" do + {:error, :invalid_vals} = Validations.validate_sc_vals({:vals, ["invalid"]}) + end + + test "validate_sc_vals/1 error no list" do + {:error, :invalid_vals} = Validations.validate_sc_vals({:vals, :invalid}) + end + + test "validate_contract_id/1" do + contract_id = "26a7dec13192736d5ba23c2052fa149021601fc264bc4311fece431ffd388a5c" + {:ok, ^contract_id} = Validations.validate_contract_id({:contract_id, contract_id}) + end + + test "validate_contract_id/1 error binary size" do + {:error, :invalid_contract_id} = Validations.validate_contract_id({:contract_id, "invalid"}) + end + + test "validate_contract_id/1 error no binary" do + {:error, :invalid_contract_id} = Validations.validate_contract_id({:contract_id, 123}) + end + + test "validate_string/1" do + {:ok, "Hello, World!"} = Validations.validate_string({:str, "Hello, World!"}) + end + + test "validate_string/1 error" do + {:error, :invalid_str} = Validations.validate_string({:str, 123}) + end end From 9204e551bcf9c95eb8c06f5c8623cc151a7a5d43 Mon Sep 17 00:00:00 2001 From: Edwin Steven Guayacan <80716239+EdwinGuayacan@users.noreply.github.com> Date: Wed, 20 Sep 2023 16:13:50 -0500 Subject: [PATCH 2/2] Prepare for release v0.18.0 (#335) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: David Roldán --- CHANGELOG.md | 4 ++++ README.md | 2 +- mix.exs | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c189b0b..43b455c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.18.0 (20.09.2023) + +* Add Soroban Preview 11 support. + ## 0.17.1 (08.08.2023) * Allow contract_data key to be any type of `SCVal` diff --git a/README.md b/README.md index f61ed0f..42100e8 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ The **Stellar SDK** is composed of two complementary components: **`TxBuild`** + ```elixir def deps do [ - {:stellar_sdk, "~> 0.17.1"} + {:stellar_sdk, "~> 0.18.0"} ] end ``` diff --git a/mix.exs b/mix.exs index e2ef84c..447aa7d 100644 --- a/mix.exs +++ b/mix.exs @@ -2,7 +2,7 @@ defmodule Stellar.MixProject do use Mix.Project @github_url "https://github.com/kommitters/stellar_sdk" - @version "0.17.1" + @version "0.18.0" def project do [