Contract-Invocation

我如何直接從 sbt-ethereum 在 uniswap 中將 ETH 換成 DAI?

  • May 6, 2019

首先,我 sbt-ethereum。這是活潑的,絕對的樂趣。完成本教程的合約互動部分後,我想測試一個實時合約。

我想直接與 uniswap.io ETH-DAI-exchange 合約互動sbt-ethereum以獲得 2 DAI。

  1. 我通過在etherscandai-token上滾動找到了地址
  2. 然後使用Uniswap 文件獲取uniswap-factory合約地址
  3. ethTransactionView uniswap-factory getExchange dai-token
  4. 這返回了uniswap-dai-exchange地址 ( 0x09cabec1ead1c0ba254b09efb3ee13841712be14)
  5. 我驗證了etherscan 上的 Contract ABI 與github 版本匹配
  6. 然後我嘗試用 5 分鐘的計時器獲得 2 DAI。輸入參數的不同組合(參見 A 到 E),包括一些帶有[ETH to pay, optional]欄位的組合,都因相同的錯誤而失敗。
  7. 我檢查了dai合約,似乎是18位小數,所以我將DAI輸入更改為2e18。我嘗試了乙太幣和芬尼面額的可選支付。我還嘗試了 ethToTokenSwapInput,指定了 ETH 的數量而不是 DAI,同樣的結果。

$$ error $$com.mchange.sc.v2.jsonrpc.package$JsonrpcException:所需的氣體超過限額或交易總是失敗$$ code=-32000 $$: 沒有更多資訊

我確信這是我正在做的簡單的事情。發送錢包的資金來自價值超過 5 DAI 的 ETH。

有任何想法嗎?

注意:我無法按預期sbt-ethereum創建新標籤,因為信譽 <150。


A:ethTransactionInvoke uniswap-dai-exchange ethToTokenSwapOutput 2 300

乙:ethTransactionInvoke uniswap-dai-exchange ethToTokenSwapOutput 2 300 15 finney

C:ethTransactionInvoke uniswap-dai-exchange ethToTokenSwapOutput 2000000000000000000 300

丁:ethTransactionInvoke uniswap-dai-exchange ethToTokenSwapOutput 2000000000000000000 300 15 finney

和:ethTransactionInvoke uniswap-dai-exchange ethToTokenSwapOutput 2000000000000000000 300 0.015 ether

首先,非常感謝您嘗試sbt-ethereum(並且喜歡它!)。

我能夠我認為使您想做的交易工作。有兩個問題:

  1. 正如@Ismael 所說,截止日期應該是一個unix時間戳。(鑑於這些的普遍性,我可能應該添加一個實用程序來輕鬆計算它們sbt-ethereum。)
  2. 您正在呼叫的函式已標記為@payable,並且確實需要您支付您願意為交換提供的乙太幣數量。我突然想到本sbt-ethereum教程並沒有過多強調這一點,但是在呼叫payable函式時,在提供函式參數後,您可以提供您希望支付的金額作為數字和單位。(**更新:**我看到你已經註意到並嘗試了這個!所以這主要是最後期限。)

另一個問題是 DAI 小數點是 18,所以如果我們只想要 2 個 DAI 代幣,我們想要 2 加上很多零。(裡面的uint解析器sbt-ethereum不支持科學記數法,還有一件事要補救!)

所以,當一切都說完了,我們有…

> ethTransactionInvoke unverified-uniswap-dai-exchange ethToTokenSwapOutput 2000000000000000000 1557109479 0.015 ether
[info] Unlocking address '0x465e79b940bc2157e4259ff6b2d92f454497f1e4' (on chain with ID 1, aliases ['default-sender','testing0'])
Enter passphrase or hex private key for address '0x465e79b940bc2157e4259ff6b2d92f454497f1e4': *******************
[info] V3 wallet(s) found for '0x465e79b940bc2157e4259ff6b2d92f454497f1e4' (aliases ['default-sender','testing0'])

==&gt; T R A N S A C T I O N   S U B M I S S I O N   R E Q U E S T
==&gt;
==&gt; The transaction would be a message with...
==&gt;   To:    0x09cabec1ead1c0ba254b09efb3ee13841712be14 (with aliases ['unverified-uniswap-dai-exchange'] on chain with ID 1)
==&gt;   From:  0x465e79b940bc2157e4259ff6b2d92f454497f1e4 (with aliases ['default-sender','testing0'] on chain with ID 1)
==&gt;   Data:  0x6b1d4db70000000000000000000000000000000000000000000000001bc16d674ec80000000000000000000000000000000000000000000000000000000000005ccf9ae7
==&gt;   Value: 0.015 Ether
==&gt;
==&gt; The transaction is signed with Chain ID 1 (which correctly matches the current session's 'ethNodeChainId').
==&gt;
==&gt; According to the ABI currently associated with the 'to' address, this message would amount to the following method call...
==&gt;   Function called: ethToTokenSwapOutput(uint256,uint256)
==&gt;     Arg 1 [name=tokens_bought, type=uint256]: 2000000000000000000
==&gt;     Arg 2 [name=deadline, type=uint256]: 1557109479
==&gt;
==&gt; The nonce of the transaction would be 407.
==&gt;
==&gt; $$$ The transaction you have requested could use up to 66235 units of gas.
==&gt; $$$ You would pay 3 gwei for each unit of gas, for a maximum cost of 0.000198705 ether.
==&gt; $$$ This is worth 0.031652712975 USD (according to Coinbase at 7:20 PM).
==&gt; $$$ You would also send 0.015 ether (2.389425 USD), for a maximum total cost of 0.015198705 ether (2.421077712975 USD).

Would you like to submit this transaction? [y/n] y
A transaction with hash '0x0509bafc336c760f21e6c7a1c491d7ba86a6c17c18ec46eec8d87d4995afd8e6' will be submitted. Please wait.
[info] Called function 'ethToTokenSwapOutput', with args '2000000000000000000, 1557109479', sending 15000000000000000 wei to address '0x09cabec1ead1c0ba254b09efb3ee13841712be14' in transaction '0x0509bafc336c760f21e6c7a1c491d7ba86a6c17c18ec46eec8d87d4995afd8e6'.
[info] Waiting for the transaction to be mined (will wait up to 5 minutes).
[info] Transaction Receipt:
[info]        Transaction Hash:    0x0509bafc336c760f21e6c7a1c491d7ba86a6c17c18ec46eec8d87d4995afd8e6
[info]        Transaction Index:   122
[info]        Transaction Status:  SUCCEEDED
[info]        Block Hash:          0x3257d8fd31b49c2c7d55d72c0fddd01f02b92a3326529ed903fdd0e6cb520f9d
[info]        Block Number:        7704627
[info]        From:                0x465e79b940bc2157e4259ff6b2d92f454497f1e4
[info]        To:                  0x09cabec1ead1c0ba254b09efb3ee13841712be14
[info]        Cumulative Gas Used: 6482478
[info]        Gas Used:            54719
[info]        Contract Address:    None
[info]        Logs:                0 =&gt; EthLogEntry [source=0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359] (
[info]                                    topics=[
[info]                                      0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef,
[info]                                      0x00000000000000000000000009cabec1ead1c0ba254b09efb3ee13841712be14,
[info]                                      0x000000000000000000000000465e79b940bc2157e4259ff6b2d92f454497f1e4
[info]                                    ],
[info]                                    data=0000000000000000000000000000000000000000000000001bc16d674ec80000
[info]                                  ),
[info]                             1 =&gt; EthLogEntry [source=0x09cabec1ead1c0ba254b09efb3ee13841712be14] (
[info]                                    topics=[
[info]                                      0xcd60aa75dea3072fbc07ae6d7d856b5dc5f4eee88854f5b4abf7b680ef8bc50f,
[info]                                      0x000000000000000000000000465e79b940bc2157e4259ff6b2d92f454497f1e4,
[info]                                      0x000000000000000000000000000000000000000000000000002c13f43dbb6c42,
[info]                                      0x0000000000000000000000000000000000000000000000001bc16d674ec80000
[info]                                    ],
[info]                                    data=
[info]                                  )
[info]        Events:              0 =&gt; Transfer [source=0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359] (
[info]                                    _from (of type address): 0x09cabec1ead1c0ba254b09efb3ee13841712be14,
[info]                                    _to (of type address): 0x465e79b940bc2157e4259ff6b2d92f454497f1e4,
[info]                                    _value (of type uint256): 2000000000000000000
[info]                                  ),
[info]                             1 =&gt; TokenPurchase [source=0x09cabec1ead1c0ba254b09efb3ee13841712be14] (
[info]                                    buyer (of type address): 0x465e79b940bc2157e4259ff6b2d92f454497f1e4,
[info]                                    eth_sold (of type uint256): 12406838703909954,
[info]                                    tokens_bought (of type uint256): 2000000000000000000
[info]                                  )
[success] Total time: 37 s, completed May 5, 2019 7:20:27 PM

sbt:ethdocstore&gt; ethTransactionView 0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359 balanceOf testing0
[info] The function 'balanceOf' yields 1 result.
[info]  + Result 1 of type 'uint256', named 'balance', is 2000000000000000002
[success] Total time: 1 s, completed May 5, 2019 7:26:36 PM

帳戶餘額末尾的額外 2 個原子是由於我最初嘗試沒有所有這些額外的零。

您可以查看您是否查看了成功的交易,對於我發送的 2 DAI 0.015 ether,但(在“內部交易”中)0.002593161296090046 ether作為零錢回來了。

再次感謝您sbt-ethereum試一試。我希望這有幫助!

ps 如果您想按sbt-ethereum原樣計算 300 秒(5 分鐘)截止日期的 UNIX 時間戳,您可以這樣做。

> consoleQuick
[info] Starting scala interpreter...
Welcome to Scala 2.12.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_172).
Type in expressions for evaluation. Or try :help.

scala&gt; (System.currentTimeMillis / 1000) + 300
res0: Long = 1557110378

scala&gt; 

不過,顯然最好不要進入 Scala REPL 來執行此操作。

引用自:https://ethereum.stackexchange.com/questions/70408