Skip to content

Commit

Permalink
140 protocol 20 support (#141)
Browse files Browse the repository at this point in the history
* Protocol 20 support

Co-authored-by: norn <keliumJU@users.noreply.github.com>

* change camel-case

* Fix credo

* remove xdr map

* rename bump by extend in contract functions

* Update readme

* Add capitalization to titles

---------

Co-authored-by: CristhianRodriguezMolina <50219561+CristhianRodriguezMolina@users.noreply.github.com>
Co-authored-by: norn <keliumJU@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 30, 2023
1 parent 9866c29 commit 7a908c9
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 133 deletions.
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ Soroban.RPC.get_ledger_entries(keys)
%{
key: "AAAAB+qfy4GuVKKfazvyk4R9P9fpo2n9HICsr+xqvVcTF+DC",
xdr: "AAAABwAAAADqn8uBrlSin2s78pOEfT/X6aNp/RyArK/sar1XExfgwgAAAAphIGNvbnRyYWN0AAA=",
last_modified_ledger_seq: "13"
last_modified_ledger_seq: "13",
live_until_ledger_seq: "320384"
}
],
latest_ledger: "179436"
Expand Down Expand Up @@ -432,7 +433,7 @@ limit = 1
start_ledger = "674736"
args = [Symbol.new("transfer"), "*", "*", "*"]
topic_filter = [TopicFilter.new(args)]
contract_ids = ["7d9defe0ccf9b680014a343b8880c22b160c2ea2c9a69df876decb28ddbd03dc"]
contract_ids = ["CCEMOFO5TE7FGOAJOA3RDHPC6RW3CFXRVIGOFQPFE4ZGOKA2QEA636SN"]

filters = [
EventFilter.new(type: [:contract], contract_ids: contract_ids, topics: topic_filter)
Expand All @@ -452,7 +453,7 @@ Soroban.RPC.get_events(events_payload)
latest_ledger: "685870",
events: [
%{
contract_id: "7d9defe0ccf9b680014a343b8880c22b160c2ea2c9a69df876decb28ddbd03dc",
contract_id: "CCEMOFO5TE7FGOAJOA3RDHPC6RW3CFXRVIGOFQPFE4ZGOKA2QEA636SN",
id: "0002917807507378176-0000000000",
in_successful_contract_call: true,
ledger: "679355",
Expand All @@ -465,7 +466,7 @@ Soroban.RPC.get_events(events_payload)
"AAAADQAAACVVU0RDOl3dfLGIo7lPPO+E0KPPSVxWCQ1qOen8umo/g+Jx8baEAAAA"
],
type: "contract",
value: %{xdr: "AAAACgAAAAAF9eEAAAAAAAAAAAA="}
value: "AAAACgAAAAAF9eEAAAAAAAAAAAA="
}
]
}}
Expand Down Expand Up @@ -675,28 +676,28 @@ secret_key = "SCA..."

```

#### BumpFootprint operation
#### ExtendFootprintTTL operation

##### Bump contract
##### Extend contract

Extends a contract instance lifetime.

**Parameters**

- `contract_address`: Identifier of the contract to be bumped, encoded as `StrKey`.
- `contract_address`: Identifier of the contract to be extended, encoded as `StrKey`.
- `source_secret_key`: Secret key of the function invoker responsible for signing the transaction.
- `ledgers_to_bump`: The number of ledgers wanted to extend the contract lifetime.
- `ledgers_to_extend`: The number of ledgers wanted to extend the contract lifetime.

```elixir
alias Soroban.Contract
alias Soroban.RPC.SendTransactionResponse

contract_address = "CAEYZ6JI5YV2CBI3NRRUNA2DMERJ4KLJTI76WDZBTWZ7VMPGY6JDIZD5"
secret_key = "SDRD4CSRGPWUIPRDS5O3CJBNJME5XVGWNI677MZDD4OD2ZL2R6K5IQ24"
ledgers_to_bump = 100_000
ledgers_to_extend = 100_000

{:ok, %SendTransactionResponse{hash: hash}} =
Contract.bump_contract(contract_address, secret_key, ledgers_to_bump)
Contract.extend_contract(contract_address, secret_key, ledgers_to_extend)

{:ok,
%Soroban.RPC.SendTransactionResponse{
Expand All @@ -709,26 +710,26 @@ ledgers_to_bump = 100_000

```

##### Bump contract wasm
##### Extend contract wasm

Extends the lifetime of a contract's uploaded wasm code.

**Parameters**

- `wasm_id`: Binary identification of an uploaded contract.
- `source_secret_key`: Secret key of the function invoker responsible for signing the transaction.
- `ledgers_to_bump`: The number of ledgers wanted to extend the wasm lifetime.
- `ledgers_to_extend`: The number of ledgers wanted to extend the wasm lifetime.

```elixir
alias Soroban.Contract
alias Soroban.RPC.SendTransactionResponse

wasm_id = "067eb7ba419edd3e946e08eb17a81fbe1e850e690ed7692160875c2b65b45f21"
secret_key = "SDRD4CSRGPWUIPRDS5O3CJBNJME5XVGWNI677MZDD4OD2ZL2R6K5IQ24"
ledgers_to_bump = 100_000
ledgers_to_extend = 100_000

{:ok, %SendTransactionResponse{hash: hash}} =
Contract.bump_contract_wasm(wasm_id, secret_key, ledgers_to_bump)
Contract.extend_contract_wasm(wasm_id, secret_key, ledgers_to_extend)

{:ok,
%Soroban.RPC.SendTransactionResponse{
Expand All @@ -741,15 +742,15 @@ ledgers_to_bump = 100_000

```

##### Bump contract keys
##### Extend contract keys

Extends the lifetime of a contract's data entry keys.

**Parameters**

- `contract_address`: Identifier of the contract to be bumped, encoded as `StrKey`.
- `contract_address`: Identifier of the contract to be extended, encoded as `StrKey`.
- `source_secret_key`: Secret key of the function invoker responsible for signing the transaction.
- `ledgers_to_bump`: The number of ledgers wanted to extend the contract lifetime.
- `ledgers_to_extend`: The number of ledgers wanted to extend the contract lifetime.
- `keys`: A list of tuples indicating the durability and the name of the data entry, to increase its lifetime.
- `durability`: Allowed types `:persistent`, `:temporary`
- `data entry`: Any `String` that is 32 characters or less.
Expand All @@ -760,11 +761,11 @@ alias Soroban.RPC.SendTransactionResponse

contract_address = "CAEYZ6JI5YV2CBI3NRRUNA2DMERJ4KLJTI76WDZBTWZ7VMPGY6JDIZD5"
secret_key = "SDRD4CSRGPWUIPRDS5O3CJBNJME5XVGWNI677MZDD4OD2ZL2R6K5IQ24"
ledgers_to_bump = 100_000
ledgers_to_extend = 100_000
keys = [{:persistent, "Prst"}, {:temporary, "Tmp"}]

{:ok, %SendTransactionResponse{hash: hash}} =
Contract.bump_contract_keys(contract_address, secret_key, ledgers_to_bump, keys)
Contract.extend_contract_keys(contract_address, secret_key, ledgers_to_extend, keys)

{:ok,
%Soroban.RPC.SendTransactionResponse{
Expand Down
20 changes: 10 additions & 10 deletions lib/contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ defmodule Soroban.Contract do
"""

alias Soroban.Contract.{
BumpFootprintExpiration,
DeployAssetContract,
DeployContract,
ExtendFootprintTTL,
InvokeContractFunction,
RestoreFootprint,
UploadContractCode
Expand Down Expand Up @@ -54,17 +54,17 @@ defmodule Soroban.Contract do
to: DeployAssetContract,
as: :deploy

defdelegate bump_contract(contract_address, secret_key, ledgers_to_bump),
to: BumpFootprintExpiration,
as: :bump_contract
defdelegate extend_contract(contract_address, secret_key, ledgers_to_extend),
to: ExtendFootprintTTL,
as: :extend_contract

defdelegate bump_contract_wasm(wasm_id, secret_key, ledgers_to_bump),
to: BumpFootprintExpiration,
as: :bump_contract_wasm
defdelegate extend_contract_wasm(wasm_id, secret_key, ledgers_to_extend),
to: ExtendFootprintTTL,
as: :extend_contract_wasm

defdelegate bump_contract_keys(contract_address, secret_key, ledgers_to_bump, keys),
to: BumpFootprintExpiration,
as: :bump_contract_keys
defdelegate extend_contract_keys(contract_address, secret_key, ledgers_to_extend, keys),
to: ExtendFootprintTTL,
as: :extend_contract_keys

defdelegate restore_contract(contract_address, secret_key),
to: RestoreFootprint,
Expand Down
2 changes: 1 addition & 1 deletion lib/contract/deploy_asset_contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ defmodule Soroban.Contract.DeployAssetContract do
@spec create_host_function_deploy_op(asset :: asset()) :: invoke_host_function()
defp create_host_function_deploy_op(asset) do
contract_id_preimage = ContractIDPreimage.new(from_asset: asset)
contract_executable = ContractExecutable.new(:token)
contract_executable = ContractExecutable.new(:stellar_asset)

create_contract_args =
CreateContractArgs.new(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Soroban.Contract.BumpFootprintExpiration do
defmodule Soroban.Contract.ExtendFootprintTTL do
@moduledoc """
`BumpFootprintExpiration` implementation to bump a contract.
`ExtendFootprintTTL` implementation to extend a contract.
"""

alias Soroban.Contract.RPCCalls
Expand All @@ -9,7 +9,7 @@ defmodule Soroban.Contract.BumpFootprintExpiration do

alias Stellar.TxBuild.{
Account,
BumpFootprintExpiration,
ExtendFootprintTTL,
LedgerFootprint,
LedgerKey,
SCAddress,
Expand All @@ -27,80 +27,80 @@ defmodule Soroban.Contract.BumpFootprintExpiration do
@type contract_address :: String.t()
@type wasm_id :: String.t()
@type secret_key :: String.t()
@type ledgers_to_bump :: non_neg_integer()
@type ledgers_to_extend :: non_neg_integer()
@type send_response :: {:ok, SendTransactionResponse.t()}
@type bump_footprint_validation :: {:ok, BumpFootprintExpiration.t()} | error()
@type extend_footprint_ttl_validation :: {:ok, ExtendFootprintTTL.t()} | error()
@type soroban_data :: SorobanTransactionData.t()

@spec bump_contract(
@spec extend_contract(
contract_address :: contract_address(),
secret_key :: secret_key(),
ledgers_to_bump :: ledgers_to_bump()
ledgers_to_extend :: ledgers_to_extend()
) :: send_response()
def bump_contract(contract_address, secret_key, ledgers_to_bump) do
def extend_contract(contract_address, secret_key, ledgers_to_extend) do
with {public_key, _secret} = keypair <- Stellar.KeyPair.from_secret_seed(secret_key),
{:ok, seq_num} <- Accounts.fetch_next_sequence_number(public_key),
{:ok, bump_footprint_op} <- create_bump_footprint_op(ledgers_to_bump),
{:ok, extend_footprint_ttl_op} <- create_extend_footprint_ttl_op(ledgers_to_extend),
%SequenceNumber{} = sequence_number <- SequenceNumber.new(seq_num),
%SorobanTransactionData{} = soroban_data <- create_soroban_data(contract_address),
%Account{} = source_account <- Account.new(public_key),
%Signature{} = signature <- Signature.new(keypair) do
bump_footprint_op
extend_footprint_ttl_op
|> RPCCalls.simulate(source_account, sequence_number, soroban_data)
|> RPCCalls.send_transaction(
source_account,
sequence_number,
signature,
bump_footprint_op
extend_footprint_ttl_op
)
end
end

@spec bump_contract_wasm(
@spec extend_contract_wasm(
wasm_id :: wasm_id(),
secret_key :: secret_key(),
ledgers_to_bump :: ledgers_to_bump()
ledgers_to_extend :: ledgers_to_extend()
) :: send_response()
def bump_contract_wasm(wasm_id, secret_key, ledgers_to_bump) do
def extend_contract_wasm(wasm_id, secret_key, ledgers_to_extend) do
with {public_key, _secret} = keypair <- Stellar.KeyPair.from_secret_seed(secret_key),
{:ok, seq_num} <- Accounts.fetch_next_sequence_number(public_key),
{:ok, bump_footprint_op} <- create_bump_footprint_op(ledgers_to_bump),
{:ok, extend_footprint_ttl_op} <- create_extend_footprint_ttl_op(ledgers_to_extend),
%SequenceNumber{} = sequence_number <- SequenceNumber.new(seq_num),
%SorobanTransactionData{} = soroban_data <- create_wasm_soroban_data(wasm_id),
%Account{} = source_account <- Account.new(public_key),
%Signature{} = signature <- Signature.new(keypair) do
bump_footprint_op
extend_footprint_ttl_op
|> RPCCalls.simulate(source_account, sequence_number, soroban_data)
|> RPCCalls.send_transaction(
source_account,
sequence_number,
signature,
bump_footprint_op
extend_footprint_ttl_op
)
end
end

@spec bump_contract_keys(
@spec extend_contract_keys(
contract_address :: contract_address(),
secret_key :: secret_key(),
ledgers_to_bump :: ledgers_to_bump(),
ledgers_to_extend :: ledgers_to_extend(),
keys :: keys()
) :: send_response()
def bump_contract_keys(contract_address, secret_key, ledgers_to_bump, keys) do
def extend_contract_keys(contract_address, secret_key, ledgers_to_extend, keys) do
with {public_key, _secret} = keypair <- Stellar.KeyPair.from_secret_seed(secret_key),
{:ok, seq_num} <- Accounts.fetch_next_sequence_number(public_key),
{:ok, bump_footprint_op} <- create_bump_footprint_op(ledgers_to_bump),
{:ok, extend_footprint_ttl_op} <- create_extend_footprint_ttl_op(ledgers_to_extend),
%SequenceNumber{} = sequence_number <- SequenceNumber.new(seq_num),
%SorobanTransactionData{} = soroban_data <- create_soroban_data(contract_address, keys),
%Account{} = source_account <- Account.new(public_key),
%Signature{} = signature <- Signature.new(keypair) do
bump_footprint_op
extend_footprint_ttl_op
|> RPCCalls.simulate(source_account, sequence_number, soroban_data)
|> RPCCalls.send_transaction(
source_account,
sequence_number,
signature,
bump_footprint_op
extend_footprint_ttl_op
)
end
end
Expand All @@ -117,7 +117,7 @@ defmodule Soroban.Contract.BumpFootprintExpiration do
write_bytes: 0
]
|> SorobanResources.new()
|> (&SorobanTransactionData.new(resources: &1, refundable_fee: 0)).()
|> (&SorobanTransactionData.new(resources: &1, resource_fee: 0)).()
end
end

Expand All @@ -135,16 +135,16 @@ defmodule Soroban.Contract.BumpFootprintExpiration do
write_bytes: 0
]
|> SorobanResources.new()
|> (&SorobanTransactionData.new(resources: &1, refundable_fee: 0)).()
|> (&SorobanTransactionData.new(resources: &1, resource_fee: 0)).()
end

@spec create_bump_footprint_op(ledgers_to_bump :: ledgers_to_bump()) ::
bump_footprint_validation()
defp create_bump_footprint_op(ledgers_to_bump)
when is_integer(ledgers_to_bump) and ledgers_to_bump > 0,
do: {:ok, BumpFootprintExpiration.new(ledgers_to_expire: ledgers_to_bump)}
@spec create_extend_footprint_ttl_op(ledgers_to_extend :: ledgers_to_extend()) ::
extend_footprint_ttl_validation()
defp create_extend_footprint_ttl_op(ledgers_to_extend)
when is_integer(ledgers_to_extend) and ledgers_to_extend > 0,
do: {:ok, ExtendFootprintTTL.new(extend_to: ledgers_to_extend)}

defp create_bump_footprint_op(_ledgers_to_bump), do: {:error, :invalid_ledger_to_bump}
defp create_extend_footprint_ttl_op(_ledgers_to_extend), do: {:error, :invalid_ledger_to_extend}

@spec create_keys(contract_address :: SCAddress.t(), keys :: keys()) ::
list(LedgerKey.t()) | error()
Expand Down
6 changes: 3 additions & 3 deletions lib/contract/restore_footprint.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Soroban.Contract.RestoreFootprint do
@moduledoc """
`RestoreFootprint` implementation to bump a contract.
`RestoreFootprint` implementation to extend a contract.
"""

alias Soroban.Contract.RPCCalls
Expand Down Expand Up @@ -112,7 +112,7 @@ defmodule Soroban.Contract.RestoreFootprint do
write_bytes: 0
]
|> SorobanResources.new()
|> (&SorobanTransactionData.new(resources: &1, refundable_fee: 0)).()
|> (&SorobanTransactionData.new(resources: &1, resource_fee: 0)).()
end
end

Expand All @@ -130,7 +130,7 @@ defmodule Soroban.Contract.RestoreFootprint do
write_bytes: 0
]
|> SorobanResources.new()
|> (&SorobanTransactionData.new(resources: &1, refundable_fee: 0)).()
|> (&SorobanTransactionData.new(resources: &1, resource_fee: 0)).()
end

@spec create_restore_footprint_op() :: restore_footprint_validation()
Expand Down
6 changes: 3 additions & 3 deletions lib/contract/rpc_calls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Soroban.Contract.RPCCalls do
}

alias Stellar.TxBuild
alias Stellar.TxBuild.{BumpFootprintExpiration, RestoreFootprint}
alias Stellar.TxBuild.{ExtendFootprintTTL, RestoreFootprint}
alias Stellar.TxBuild.SorobanTransactionData, as: TxSorobanTransactionData
alias StellarBase.XDR.{SorobanTransactionData, UInt32}

Expand All @@ -29,7 +29,7 @@ defmodule Soroban.Contract.RPCCalls do
@type auths :: list(String.t()) | nil
@type auth_secret_key :: String.t() | list() | nil
@type envelope_xdr :: String.t()
@type footprint_operations :: BumpFootprintExpiration.t() | RestoreFootprint.t()
@type footprint_operations :: ExtendFootprintTTL.t() | RestoreFootprint.t()
@type operation :: InvokeHostFunction.t() | footprint_operations()
@type simulate_response :: {:ok, SimulateTransactionResponse.t()}
@type send_response :: {:ok, SendTransactionResponse.t()}
Expand Down Expand Up @@ -281,7 +281,7 @@ defmodule Soroban.Contract.RPCCalls do
do: {:error, :invalid_auth_secret_keys_length}

@spec validate_operation(operation :: footprint_operations()) :: validation()
defp validate_operation(%BumpFootprintExpiration{} = operation), do: {:ok, operation}
defp validate_operation(%ExtendFootprintTTL{} = operation), do: {:ok, operation}
defp validate_operation(%RestoreFootprint{} = operation), do: {:ok, operation}

# This function is needed since when the function invoker is not the function authorizer
Expand Down
Loading

0 comments on commit 7a908c9

Please sign in to comment.