Skip to content

Commit 7f6dd94

Browse files
committed
add submit_and_wait
1 parent 13eb8e1 commit 7f6dd94

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ podman build --tag erlang_builder -f build.Dockerfile
1111
./build.sh
1212
```
1313

14+
### Testnet
15+
```
16+
#run local testnet with RPC api
17+
18+
TESTNET=true WORKFOLDER=/tmp/testnet HTTP_IPV4=127.0.0.1 HTTP_PORT=8080 ./amadeusd
19+
20+
# inside REPL submit a transfer to self
21+
22+
pk = Application.fetch_env!(:ama, :trainer_pk)
23+
sk = Application.fetch_env!(:ama, :trainer_sk)
24+
Testnet.call(sk, "Coin", "transfer", [pk,"1","AMA"])
25+
```
26+
1427
### AutoUpdates + Running as a systemd service
1528

1629
```

ex/lib/api/api_tx.ex

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,44 @@ defmodule API.TX do
132132
end
133133
end
134134

135+
def submit_and_wait(tx_packed, broadcast \\ true) do
136+
result = TX.validate(tx_packed)
137+
if result[:error] == :ok do
138+
txu = TX.unpack(tx_packed)
139+
if tx_packed =~ "deploy" do
140+
action = hd(txu.tx.actions)
141+
if action.contract == "Contract" and action.function == "deploy" do
142+
case BIC.Contract.validate(List.first(action.args)) do
143+
%{error: :ok} ->
144+
if broadcast do TXPool.insert_and_broadcast(tx_packed) else TXPool.insert(tx_packed) end
145+
txres = submit_and_wait_1(result.txu.hash)
146+
%{error: :ok, hash: Base58.encode(result.txu.hash), entry_hash: txres.metadata.entry_hash, result: txres[:result]}
147+
error -> error
148+
end
149+
else
150+
if broadcast do TXPool.insert_and_broadcast(tx_packed) else TXPool.insert(tx_packed) end
151+
txres = submit_and_wait_1(result.txu.hash)
152+
%{error: :ok, hash: Base58.encode(result.txu.hash), entry_hash: txres.metadata.entry_hash, result: txres[:result]}
153+
end
154+
else
155+
if broadcast do TXPool.insert_and_broadcast(tx_packed) else TXPool.insert(tx_packed) end
156+
txres = submit_and_wait_1(result.txu.hash)
157+
%{error: :ok, hash: Base58.encode(result.txu.hash), entry_hash: txres.metadata.entry_hash, result: txres[:result]}
158+
end
159+
else
160+
%{error: result.error}
161+
end
162+
end
163+
164+
def submit_and_wait_1(hash, 30) do nil end
165+
def submit_and_wait_1(hash, tries \\ 0) do
166+
tx = get(hash)
167+
if tx do tx else
168+
Process.sleep(100)
169+
submit_and_wait_1(hash, tries + 1)
170+
end
171+
end
172+
135173
def format_tx_for_client(nil) do nil end
136174
def format_tx_for_client(tx) do
137175
tx = Map.drop(tx, [:tx_encoded])

ex/lib/misc/testnet.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule Testnet do
2+
def call(sk, contract, function, args, attach_symbol \\ nil, attach_amount \\ nil) do
3+
packed_tx = TX.build(sk, contract, function, args, nil, attach_symbol, attach_amount)
4+
API.TX.submit_and_wait(packed_tx, false)
5+
end
6+
7+
def read(key) do
8+
%{db: db, cf: cf} = :persistent_term.get({:rocksdb, Fabric})
9+
RocksDB.get(key, %{db: db, cf: cf.contractstate})
10+
end
11+
end

0 commit comments

Comments
 (0)