Skip to content

Instantly share code, notes, and snippets.

@madis
Last active July 21, 2021 07:16
Show Gist options
  • Save madis/76aaa356a9f494c5807f9a15e8de9f01 to your computer and use it in GitHub Desktop.
Save madis/76aaa356a9f494c5807f9a15e8de9f01 to your computer and use it in GitHub Desktop.
Debugging ERC20 approval & transferFrom flow in ClojureScript
; To run in Ethlance repl
; 1. ./bin/repl
; 2. (shadow/repl :dev-server)
; 3. (in-ns 'ethlance.server.contract.ethlance)
(require '[cljs-web3-next.eth :as web3-eth])
(require '[cljs.core.async :refer [go <!]])
(require '[district.server.web3 :refer [web3]])
(require '[district.shared.async-helpers :refer [<?]])
(require '[ethlance.shared.smart-contracts-dev :as addresses])
(defn approve-n-check [from to amount]
(go
(println "----- START -----")
(let [
token-addr (get-in addresses/smart-contracts [:token :address])
_ (println "--> Doing ERC20 token (contract at" token-addr ") transfer from " from " to " to)
mint-result (<! (smart-contracts/contract-send :token :mint [from amount] {:from from}))
_ (println "--> Minting result: " mint-result)
initial-balance-result (<! (smart-contracts/contract-call :token :balance-of [from]))
_ (println "--> Initial balance of " from ": " initial-balance-result)
approval-result (<! (smart-contracts/contract-send :token :approve [from to amount] {:from from}))
_ (println "--> Approval result: " approval-result) ; Returns nil, no effect
allowance-result (<! (smart-contracts/contract-call :token :allowance [from to]))
_ (println "--> Allowance result: " allowance-result) ; Because of previous, allowance stays unchanged
transfer-from-result (<! (smart-contracts/contract-send :token :transfer-from [from to amount]))
_ (println "--> Transfer result: " transfer-from-result)
balance-after-result (<! (smart-contracts/contract-call :token :balance-of [to]))
_ (println "--> Balance after: " balance-after-result)
])
(println "----- END -----\n")
))
; From employer to Ethlance
(approve-n-check "0x2edd65Db76A4c02E851702CAc5E51b77Dc721cf0" "0xed2b68ab9f299e47eef3eb9d401b94079fefe75b" 1)
; From owner (1st address) to Ethlance
(approve-n-check "0x0935D2ec65144343df67Bd3c7399c37Beb1bBce0" "0xed2b68ab9f299e47eef3eb9d401b94079fefe75b" 1)
----- START -----
--> Doing ERC20 token (contract at 0xf2e5d62f843a5b3e21ba7e03991241cd8acca1a8 ) transfer from 0x0935D2ec65144343df67Bd3c7399c37Beb1bBce0 to 0xed2b68ab9f299e47eef3eb9d401b94079fefe75b
--> Minting result: {:contract-address nil, :transaction-index 0, :logs-bloom 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, :events {}, :block-hash 0xa8bc91a83142151eea1a88435a48cfe438123fd3208de18d344a1e7afd1d11e1, :cumulative-gas-used 34763, :block-number 1180, :transaction-hash 0x6896bf79200807d612efdd0238857dce413b31d20418f2c8571d45a25b1be7f5, :gas-used 34763, :status true, :from 0x0935d2ec65144343df67bd3c7399c37beb1bbce0, :to 0xf2e5d62f843a5b3e21ba7e03991241cd8acca1a8}
--> Initial balance of 0x0935D2ec65144343df67Bd3c7399c37Beb1bBce0 : 4
--> Approval result: nil
--> Allowance result: 0
--> Transfer result: {:contract-address nil, :transaction-index 0, :logs-bloom 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, :events {}, :block-hash 0x64063a6450f31a598cffff0ed3817a3ef9de58ec0cb524edb532d987f0872226, :cumulative-gas-used 24107, :block-number 1181, :transaction-hash 0x013f72b6412b94b94436b85b506338960a0a34ca94b3210515debc23f51b7d98, :gas-used 24107, :status true, :from 0x0935d2ec65144343df67bd3c7399c37beb1bbce0, :to 0xf2e5d62f843a5b3e21ba7e03991241cd8acca1a8}
--> Balance after: 0
----- END -----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment