mirror of
https://github.com/status-im/open-bounty.git
synced 2025-02-17 11:56:30 +00:00
Utility functions for transferring ETH and tokens
* implemented some util functions to transfer ETH and ERC20 tokens for aiding testing * add and improve doctrings
This commit is contained in:
parent
aeb5d08657
commit
881e79477f
@ -30,6 +30,7 @@
|
|||||||
response (:body @(post (eth-rpc-url) options))
|
response (:body @(post (eth-rpc-url) options))
|
||||||
result (json/read-str response :key-fn keyword)]
|
result (json/read-str response :key-fn keyword)]
|
||||||
(log/debug body "\n" result)
|
(log/debug body "\n" result)
|
||||||
|
|
||||||
(if (= (:id result) request-id)
|
(if (= (:id result) request-id)
|
||||||
(:result result)
|
(:result result)
|
||||||
(do
|
(do
|
||||||
@ -57,6 +58,11 @@
|
|||||||
[wei]
|
[wei]
|
||||||
(/ wei 1000000000000000000))
|
(/ wei 1000000000000000000))
|
||||||
|
|
||||||
|
(defn eth->wei
|
||||||
|
[eth]
|
||||||
|
(biginteger (* eth 1000000000000000000)))
|
||||||
|
|
||||||
|
|
||||||
(defn hex->eth
|
(defn hex->eth
|
||||||
[hex digits]
|
[hex digits]
|
||||||
(->> hex hex->big-integer from-wei double (format (str "%." digits "f"))))
|
(->> hex hex->big-integer from-wei double (format (str "%." digits "f"))))
|
||||||
@ -74,6 +80,7 @@
|
|||||||
(hex->eth (get-balance-hex account) digits))
|
(hex->eth (get-balance-hex account) digits))
|
||||||
|
|
||||||
(defn send-transaction
|
(defn send-transaction
|
||||||
|
"Send transaction using default commiteth bot account."
|
||||||
[from to value & [params]]
|
[from to value & [params]]
|
||||||
(let [args (merge params
|
(let [args (merge params
|
||||||
{:from from
|
{:from from
|
||||||
@ -91,6 +98,25 @@
|
|||||||
args)
|
args)
|
||||||
(eth-password)])))
|
(eth-password)])))
|
||||||
|
|
||||||
|
(defn send-transaction-using-from-account
|
||||||
|
"Send transaction using account address in parameter from. Assumes
|
||||||
|
account has been unlocked."
|
||||||
|
[from to value & [params]]
|
||||||
|
(let [args (merge params
|
||||||
|
{:from from
|
||||||
|
:value value}
|
||||||
|
(when-not (nil? (gas-price))
|
||||||
|
{:gasPrice (integer->hex (gas-price))})
|
||||||
|
(when-not (contains? params :gas)
|
||||||
|
{:gas
|
||||||
|
(estimate-gas from to value params)}))]
|
||||||
|
(log/debug "args:" args)
|
||||||
|
(eth-rpc
|
||||||
|
"eth_sendTransaction"
|
||||||
|
[(if-not (nil? to)
|
||||||
|
(merge args {:to to})
|
||||||
|
args)])))
|
||||||
|
|
||||||
(defn get-transaction-receipt
|
(defn get-transaction-receipt
|
||||||
[hash]
|
[hash]
|
||||||
(eth-rpc "eth_getTransactionReceipt" [hash]))
|
(eth-rpc "eth_getTransactionReceipt" [hash]))
|
||||||
@ -117,6 +143,28 @@
|
|||||||
value (format "0x%x" 0)]
|
value (format "0x%x" 0)]
|
||||||
(send-transaction from contract value {:data data})))
|
(send-transaction from contract value {:data data})))
|
||||||
|
|
||||||
|
(defn execute-using-addr
|
||||||
|
[from-addr from-passphrase contract method-id & params]
|
||||||
|
(eth-rpc "personal_unlockAccount" [from-addr from-passphrase 30])
|
||||||
|
(let [data (apply format-call-params method-id params)
|
||||||
|
value (format "0x%x" 0)]
|
||||||
|
(send-transaction-using-from-account from-addr
|
||||||
|
contract
|
||||||
|
value
|
||||||
|
{:data data})))
|
||||||
|
|
||||||
|
(defn transfer-eth
|
||||||
|
"Transfer amount-wei of ETH from from-addr to to-addr."
|
||||||
|
[from-addr from-passphrase to-addr amount-wei]
|
||||||
|
(eth-rpc "personal_unlockAccount" [from-addr from-passphrase 30])
|
||||||
|
(let [data "0x"
|
||||||
|
value (integer->hex amount-wei)]
|
||||||
|
(println "value" value)
|
||||||
|
(send-transaction-using-from-account from-addr
|
||||||
|
to-addr
|
||||||
|
value
|
||||||
|
{:data data})))
|
||||||
|
|
||||||
|
|
||||||
(defn hex-ch->num
|
(defn hex-ch->num
|
||||||
[ch]
|
[ch]
|
||||||
|
@ -19,7 +19,8 @@
|
|||||||
:get-token-list "getTokenList()"
|
:get-token-list "getTokenList()"
|
||||||
:create "create(address[],uint256)"
|
:create "create(address[],uint256)"
|
||||||
:watch "watch(address)"
|
:watch "watch(address)"
|
||||||
:balance-of "balanceOf(address)"})))
|
:balance-of "balanceOf(address)"
|
||||||
|
:transfer "transfer(address,uint256)"})))
|
||||||
|
|
||||||
(def topics
|
(def topics
|
||||||
{:factory-create (eth/event-sig->topic-id "Create(address,address)")
|
{:factory-create (eth/event-sig->topic-id "Create(address,address)")
|
||||||
@ -120,7 +121,7 @@
|
|||||||
(BigInteger/valueOf 500000)))
|
(BigInteger/valueOf 500000)))
|
||||||
|
|
||||||
(defn convert-token-value
|
(defn convert-token-value
|
||||||
"Convert given value to decimal using given token's base"
|
"Convert given value to decimal using given token's base."
|
||||||
[value token]
|
[value token]
|
||||||
(let [token-details (token-data/token-info token)
|
(let [token-details (token-data/token-info token)
|
||||||
token-base (:base token-details)]
|
token-base (:base token-details)]
|
||||||
@ -130,7 +131,8 @@
|
|||||||
|
|
||||||
|
|
||||||
(defn token-balance-in-bounty
|
(defn token-balance-in-bounty
|
||||||
"Query (internal) ERC20 token balance from bounty contract for given token TLA"
|
"Query (internal) ERC20 token balance from bounty contract for given
|
||||||
|
token TLA."
|
||||||
[bounty-addr token]
|
[bounty-addr token]
|
||||||
(let [bounty-contract (load-bounty-contract bounty-addr)
|
(let [bounty-contract (load-bounty-contract bounty-addr)
|
||||||
token-address (get-token-address token)
|
token-address (get-token-address token)
|
||||||
@ -142,7 +144,8 @@
|
|||||||
(convert-token-value token))))
|
(convert-token-value token))))
|
||||||
|
|
||||||
(defn token-balance
|
(defn token-balance
|
||||||
"Query balance of given ERC20 token TLA for given address from ERC20 contract"
|
"Query balance of given ERC20 token TLA for given address from ERC20
|
||||||
|
contract."
|
||||||
[bounty-addr token]
|
[bounty-addr token]
|
||||||
(let [token-address (get-token-address token)]
|
(let [token-address (get-token-address token)]
|
||||||
|
|
||||||
@ -154,7 +157,8 @@
|
|||||||
|
|
||||||
|
|
||||||
(defn token-balances
|
(defn token-balances
|
||||||
"Get a given bounty contract's token balances. Assumes contract's internal balances have been updated"
|
"Get a given bounty contract's token balances. Assumes contract's
|
||||||
|
internal balances have been updated."
|
||||||
[bounty-addr]
|
[bounty-addr]
|
||||||
(let [bounty-contract (load-bounty-contract bounty-addr)
|
(let [bounty-contract (load-bounty-contract bounty-addr)
|
||||||
token-addresses (-> bounty-contract
|
token-addresses (-> bounty-contract
|
||||||
@ -168,3 +172,16 @@
|
|||||||
(assert tla)
|
(assert tla)
|
||||||
[tla (token-balance bounty-addr tla)])) addrs)))
|
[tla (token-balance bounty-addr tla)])) addrs)))
|
||||||
{})))
|
{})))
|
||||||
|
|
||||||
|
(defn transfer-tokens
|
||||||
|
"Transfer mount of given ERC20 token from from-addr to
|
||||||
|
to-addr. Connected geth needs to have keys for the account and
|
||||||
|
passphrase needs to be supplied. Returns transaction ID."
|
||||||
|
[from-addr from-passphrase token to-addr amount]
|
||||||
|
(let [token-addr (get-token-address token)]
|
||||||
|
(eth/execute-using-addr from-addr
|
||||||
|
from-passphrase
|
||||||
|
token-addr
|
||||||
|
(method-ids :transfer)
|
||||||
|
to-addr
|
||||||
|
amount)))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user