Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

NearApi.RPC.Contracts

notebooks/near_api/rpc/contracts.livemd

NearApi.RPC.Contracts

Setup

System.put_env("NEAR_PUBLIC_KEY", "ed25519:H9k5eiU4xXS3M4z8HzKJSLaZdqGdGwBG49o7orNC4eZW")
System.put_env("NEAR_NODE_URL", "https://rpc.testnet.near.org")
:ok

NearApi.RPC.Contracts.view_code/3

Returns the contract code (Wasm binary) deployed to the account.

Please note that the returned code will be encoded in base64.

{:ok, result} = NearApi.RPC.Contracts.view_code("client.chainlink.testnet")
{:ok,
 %{
   "id" => "dontcare",
   "jsonrpc" => "2.0",
   "result" => %{
     "block_hash" => "8t2rCuzPj9cD6nKA6NoRg7HHRA6v3XNxNRP1sus5RH3o",
     "block_height" => 85948035,
     "code_base64" => "AGFzbQEAAAABiwRDYAF/AGAAAX9gAn9/AX9gA39+fgBgBH9+fn4BfmACf34AYAR/fn5+AGADf35+AX5gCH9+fn5+fn5+AGACf34BfmAGf35+fn5+AX5gBX9+fn5+AGAKf35+fn5+fn5+fgBgAn9/AGADf39/AX9gAX8BfmACfn4AYAF+AX5gAX4AYAABfmADfn5+AGAAAGAIfn5+fn5+fn4BfmAJfn5+fn5+fn5+AX5gAn5+AX5gA35+fgF+YAd+fn5+fn5+AGAEfn5+fgBgCX5+fn5+fn5+fgBgBX5+fn5+AX5gA39/fwBgAX8Bf2AEf39/fgBgBX9/f35/AGAFf39/f38AYAR/f39/AGAFf39/f38Bf2AGf39/f39/AGAEf39/fwF/YAJ/fwF+YAV/f35/fwBgA39/fgBgBH9/fn8AYAR/f35+AGAEf35+fwF+YAR/fn9/AX9gAn9+AX9gD39+fn9/f35+fn5/f35+fgBgCX9+fn5+fn5+fgF+YAp/fn5+fn5+fn5+AX5gB39/f39/f38AYAN/f38BfmACfn8BfmADfn9/AGAIfn9/f39+fn4AYAR+fn5/AGADfn9+AGAIfn9+fn5/f38AYAJ+fwBgB39/f39+fn4AYAR/fH9/AX9gBn9/f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CoAKfAqEBgwW1BuEDggWFA7MDhwOTA4ADgQOCA9cF/wLYBdkFlwOtA6kDrAOaA5YD1AWUA+ID5APlA+YD8APqA+8D6wOaBOwEsgSZBrAEmAaqBK8EsQSuBP4F7QSzBJ8EoQSiBPcE+gT5BPYE9ATABs0G1wbTBscGkAWUBY0FjgWPBYwFlwWjBaAFogWWBcMF1gbTBd4FyAXQBc0FzwXABcUFxgXJBdEFygXjBeQFwQXlBeYFvwXE" <> ...,
     "hash" => "2snK55re3tXyYAPhCfv34VcCFsr6CUZYwD3G8ouWK2PP"
   }
 }}
{:ok, result} = NearApi.RPC.Contracts.view_code("client.chainlink.testnet")
binary = Base.decode64!(result["result"]["code_base64"])
<<0, 97, 115, 109, 1, 0, 0, 0, 1, 139, 4, 67, 96, 1, 127, 0, 96, 0, 1, 127, 96, 2, 127, 127, 1, 127,
  96, 3, 127, 126, 126, 0, 96, 4, 127, 126, 126, 126, 1, 126, 96, 2, 127, 126, 0, 96, 4, 127, 126,
  126, ...>>

NearApi.RPC.Contracts.view_state/3

Returns the state (key value pairs) of a contract based on the key prefix (base64 encoded). Pass an empty string for prefix_base64 if you would like to return the entire state. Please note that the returned state will be base64 encoded as well.

{:ok, result} = NearApi.RPC.Contracts.view_state("yellowpie.testnet")
{:ok,
 %{
   "id" => "dontcare",
   "jsonrpc" => "2.0",
   "result" => %{
     "block_hash" => "13LSzQNvNpYA9nXspgS2x5jMzoxh6P3Bk57fUrAft4uu",
     "block_height" => 85947187,
     "proof" => [],
     "values" => [
       %{
         "key" => "U1RBVEU=",
         "proof" => [],
         "value" => "AQAAAGEAgMakfo0DAAAAAAAAAAAAfQAAAAAAAAABAAAAbQ=="
       },
       %{
         "key" => "YREAAAB5ZWxsb3dwaWUudGVzdG5ldA==",
         "proof" => [],
         "value" => "7X/GpH6NAwAAAAAAAAAAAA=="
       },
       %{
         "key" => "YRUAAABib2IueWVsbG93cGllLnRlc3RuZXQ=",
         "proof" => [],
         "value" => "EwAAAAAAAAAAAAAAAAAAAA=="
       },
       %{"key" => "bGFzdF9zZW5kZXI=", "proof" => [], "value" => "eWVsbG93cGllMy50ZXN0bmV0"},
       %{
         "key" => "bQ==",
         "proof" => [],
         "value" => "CAAAAGZ0LTEuMC4wDQAAAFhPMTB4MTAgVG9rZW4EAAAAWE8xMAAAAAg="
       }
     ]
   }
 }}

NearApi.RPC.Contracts.data_changes/3

Returns the state change details of a contract based on the key prefix (encoded to base64). Pass an empty string for this param if you would like to return all state changes. Warning: Experimental

{:ok, result} = NearApi.RPC.Contracts.data_changes(["yellowpie.testnet"])
{:ok,
 %{
   "id" => "dontcare",
   "jsonrpc" => "2.0",
   "result" => %{"block_hash" => "AiQAxyu7DCz77WaaKxkJp6qjz8hKnzRUg1CYZTVKRZXd", "changes" => []}
 }}

NearApi.RPC.Contracts.contract_code_changes/2

Returns code changes made when deploying a contract. Change is returned is a base64 encoded WASM file. Warning: Experimental

{:ok, result} = NearApi.RPC.Contracts.contract_code_changes(["yellowpie.testnet"])
{:ok,
 %{
   "id" => "dontcare",
   "jsonrpc" => "2.0",
   "result" => %{"block_hash" => "GmGqe45das8DbikKyu7xqxH8As9Mzc7CxXcXr2tVdjHn", "changes" => []}
 }}

NearApi.RPC.Contracts.call_function/3

Allows you to call a contract method as a view function.

args_base64 =
  %{account_id: "status_message.yellowpie.testnet"}
  |> Jason.encode!()
  |> Base.encode64()

{:ok, result} =
  NearApi.RPC.Contracts.call_function(
    "status_message.yellowpie.testnet",
    "get_status",
    args_base64
  )
{:ok,
 %{
   "id" => "dontcare",
   "jsonrpc" => "2.0",
   "result" => %{
     "block_hash" => "8yq3jXjJ6BLazS4aPSqnqKE21NcakH1PbX8TDTvdfuiY",
     "block_height" => 85947809,
     "logs" => [],
     "result" => '"hello world"'
   }
 }}