Skip to content

Commit

Permalink
Disable new job tx button while tx in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
madis committed Nov 5, 2023
1 parent 605195f commit 754b303
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 44 deletions.
1 change: 0 additions & 1 deletion contracts/Job.sol
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ contract Job is IERC721Receiver, IERC1155Receiver, DSAuth, JobStorage {
ethlance.emitFundsWithdrawn(address(this), msg.sender, withdrawAmounts);
}


function endJob() external hasNoOutstandingPayments {
for(uint i = 0; i < depositorsLength(); i++) {
address depositor = getDepositor(i);
Expand Down
2 changes: 1 addition & 1 deletion contracts/JobStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract JobStorage {

// The bytes32 being keccak256(abi.encodePacked(depositorAddress, TokenType, contractAddress, tokenId))
mapping(bytes32 => EthlanceStructs.Deposit) deposits;
bytes32[] depositIds; // To allow looking up and listing all deposits
bytes32[] public depositIds; // To allow looking up and listing all deposits
EnumerableSet.AddressSet depositors;

mapping(address => EthlanceStructs.TokenValue) public arbiterQuotes;
Expand Down
4 changes: 2 additions & 2 deletions contracts/external/MutableForwarder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ contract MutableForwarder is DelegateProxy, DSAuth {
target = _target;
}

event Received(address, uint);
receive() external payable {
payable(target).transfer(msg.value);
// This method doesn't (and shouldn't) do anything. It's here to be able to receive Ether only
// When Job gets created, ETH gets transferred to it (EthlanceStructs.transferToJob)
}

fallback() external payable {
Expand Down
28 changes: 19 additions & 9 deletions ui/src/ethlance/ui/page/job_detail.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@
" ("
(get-in arbiter [:arbiter/feedback :total-count])
")"]))
arbiter-address-fn (comp :user/id :user)]
arbiter-address-fn (comp :user/id :user)
invite-arbiters-tx-in-progress? (re/subscribe [:page.job-detail/invite-arbiters-tx-in-progress?])]
[:div.proposal-form
[:div.label "Invite arbiter"]
[c-chip-search-input
Expand All @@ -320,10 +321,12 @@
:placeholder "Searh arbiter by name"}]

[c-button
{:style (when nothing-added? {:background :gray})
{:style (when (or nothing-added? @invite-arbiters-tx-in-progress?) {:background :gray})
:size :small
:on-click (fn []
(when (not (empty? @selected-arbiters))
(when (and
(not (empty? @selected-arbiters))
(not @invite-arbiters-tx-in-progress?))
(>evt [:page.job-detail/invite-arbiters
{:job/id job-address
:employer employer-address
Expand Down Expand Up @@ -503,7 +506,9 @@
(defn c-add-funds [contract-address token-id token-details]
(let [amount @(re/subscribe [:page.job-detail/add-funds-amount])
step (if (= (:token-detail/type token-details) :eth) 0.001 1)
adding-funds? (re/subscribe [:page.job-detail/adding-funds?])]
adding-funds? (re/subscribe [:page.job-detail/adding-funds?])
add-funds-tx-in-progress? (re/subscribe [:page.job-detail/add-funds-tx-in-progress?])
]
(if @adding-funds?
[:div.add-funds
[c-text-input
Expand All @@ -514,7 +519,9 @@
:value amount
:on-change #(re/dispatch [:page.job-detail/set-add-funds-amount (js/parseFloat %)])}]
[c-button {:on-click (fn []
(>evt [:page.job-detail/finish-adding-funds contract-address token-details token-id amount]))
(when (not @add-funds-tx-in-progress?)
(>evt [:page.job-detail/finish-adding-funds contract-address token-details token-id amount])))
:disabled? @add-funds-tx-in-progress?
:size :small}
[c-button-label "Confirm"]]]

Expand Down Expand Up @@ -565,7 +572,9 @@
show-end-job? (and
(ilike= employer-id active-user)
(not= :ended job-status))
can-end-job? (not (or has-unpaid-invoices? has-unresolved-disputes?))]
job-ongoing? (not= :ended job-status)
can-end-job? (not (or has-unpaid-invoices? has-unresolved-disputes?))
end-job-tx-in-progress? (re/subscribe [:page.job-detail/end-job-tx-in-progress?])]
[:div.header
[:div.main
[:div.title *title]
Expand All @@ -579,7 +588,7 @@
[:div.label "Available Funds"]
[c-token-info job-balance token-details]]]

[c-add-funds contract-address (:job/token-id results) token-details]
(when job-ongoing? [c-add-funds contract-address (:job/token-id results) token-details])
[:div.profiles
[c-participant-info :employer employer-id]
(when has-accepted-arbiter? [c-participant-info :arbiter arbiter-id])]]
Expand All @@ -590,9 +599,10 @@
(when show-end-job?
[:div
[:div.button.primary.active
{:style (when (or has-unpaid-invoices? has-unresolved-disputes?) {:background :gray})
{:style (when (or @end-job-tx-in-progress? has-unpaid-invoices? has-unresolved-disputes?) {:background :gray})
:disabled? @end-job-tx-in-progress?
:on-click (fn []
(when can-end-job?
(when (and can-end-job? (not @end-job-tx-in-progress?))
(re/dispatch [:page.job-detail/end-job {:job/id contract-address :employer employer-id}])))}
[:div.button-label "End job"]]
(when has-unpaid-invoices?
Expand Down
62 changes: 45 additions & 17 deletions ui/src/ethlance/ui/page/job_detail/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
;; Page State
(def state-key :page.job-detail)
(def state-default
{:proposal-offset 0
{:add-funds-tx-in-progress? false
:end-job-tx-in-progress? false
:invite-arbiters-in-progress? false
:proposal-offset 0
:proposal-limit 3
:arbitrations-offset 0
:arbitrations-limit 3
Expand All @@ -42,6 +45,14 @@
}]
:db (assoc-in db [state-key] state-default)}))

(defn set-add-funds-tx-in-progress [db in-progress?]
(assoc-in db [state-key :add-funds-tx-in-progress?] in-progress?))

(defn set-end-job-tx-in-progress [db in-progress?]
(assoc-in db [state-key :end-job-tx-in-progress?] in-progress?))

(defn set-invite-arbiters-tx-in-progress [db in-progress?]
(assoc-in db [state-key :invite-arbiters-tx-in-progress?] in-progress?))
;;
;; Registered Events
;;
Expand Down Expand Up @@ -238,14 +249,15 @@
:on-tx-success [:page.job-detail/arbitration-tx-success "Transaction to accept quote successful"]
:on-tx-error [::accept-quote-for-arbitration-tx-error]}]}))

(defn invite-arbiters [cofx [_event-name event-data]]
(defn invite-arbiters [{:keys [db] :as cofx} [_event-name event-data]]
(let [job-address (:job/id event-data)
arbiter-addresses (:arbiters event-data)
employer-address (:employer event-data)
instance (contract-queries/instance (:db cofx) :job job-address)
instance (contract-queries/instance db :job job-address)
tx-opts {:from employer-address :gas 10000000}
contract-args [employer-address arbiter-addresses]]
{:dispatch [::web3-events/send-tx
{:db (set-invite-arbiters-tx-in-progress db true)
:dispatch [::web3-events/send-tx
{:instance instance
:fn :invite-arbiters
:args contract-args
Expand All @@ -255,12 +267,13 @@
:on-tx-success [:page.job-detail/arbitration-tx-success "Transaction to invite arbiters successful"]
:on-tx-error [::invite-arbiters-tx-error]}]}))

(defn end-job-tx [cofx [_event-name event-data]]
(defn end-job-tx [{:keys [db] :as cofx} [_event-name event-data]]
(let [job-address (:job/id event-data)
employer-address (:employer event-data)
instance (contract-queries/instance (:db cofx) :job job-address)
tx-opts {:from employer-address :gas 10000000}]
{:dispatch [::web3-events/send-tx
{:db (set-end-job-tx-in-progress db true)
:dispatch [::web3-events/send-tx
{:instance instance
:fn :end-job
:args []
Expand All @@ -284,11 +297,21 @@

(re/reg-event-fx :page.job-detail/end-job end-job-tx)
(re/reg-event-fx ::end-job-tx-hash-error (create-logging-handler))
(re/reg-event-fx ::end-job-tx-error (create-logging-handler))
(re/reg-event-fx
::end-job-tx-error
(fn [{:keys [db]} _]
{:db (set-end-job-tx-in-progress db false)}))

(re/reg-event-fx :page.job-detail/invite-arbiters invite-arbiters)
(re/reg-event-fx ::invite-arbiters-tx-error (create-logging-handler))
(re/reg-event-fx ::invite-arbiters-tx-hash-error (create-logging-handler))
(re/reg-event-db
::invite-arbiters-tx-error
(fn [db _]
(set-invite-arbiters-tx-in-progress db false)))

(re/reg-event-db
::invite-arbiters-tx-hash-error
(fn [db _]
(set-invite-arbiters-tx-in-progress db false)))

(re/reg-event-fx
:page.job-detail/fetch-job-arbiter-status
Expand Down Expand Up @@ -318,7 +341,9 @@
(re/reg-event-fx
:page.job-detail/arbitration-tx-success
(fn [cofx [event message]]
{:db (assoc-in (:db cofx) [state-key] state-default)
{:db (-> (:db cofx)
(set-invite-arbiters-tx-in-progress ,,, false)
(assoc-in ,,, [state-key] state-default))
:fx [[:dispatch [:page.job-detail/arbitrations-updated]]
[:dispatch [::notification.events/show message]]]}))

Expand All @@ -329,8 +354,9 @@

(re/reg-event-fx
:page.job-detail/end-job-tx-success
(fn [cofx event]
{:fx [[:dispatch [:page.job-detail/job-updated]]
(fn [{:keys [db] :as cofx} event]
{:db (set-end-job-tx-in-progress db false)
:fx [[:dispatch [:page.job-detail/job-updated]]
[:dispatch [::notification.events/show "Transaction to end job processed successfully"]]]}))

(re/reg-event-fx :page.job-detail/set-add-funds-amount (create-assoc-handler :add-funds-amount))
Expand Down Expand Up @@ -435,8 +461,7 @@
(re/reg-event-fx
:page.job-detail/finish-adding-funds
(fn [{:keys [db] :as cofx} [_ job-address token-details token-id amount]]
(let [
funder (accounts-queries/active-account (:db cofx))
(let [funder (accounts-queries/active-account (:db cofx))
tx-opts-base {:from funder :gas 10000000}
token-type (:token-detail/type token-details)
tx-amount (util.tokens/machine-amount amount token-type)
Expand All @@ -463,19 +488,22 @@
:erc20 ::ensure-erc20-allowance
:erc721 ::safe-transfer-with-add-funds
:erc1155 ::safe-transfer-with-add-funds}]
{:fx [[:dispatch [(get next-event token-type) funds-params]]]})))
{:db (set-add-funds-tx-in-progress db true)
:fx [[:dispatch [(get next-event token-type) funds-params]]]})))

(re/reg-event-fx
::add-funds-tx-success
(fn [{:keys [db]} [event-name tx-data]]
(let [events (get-in tx-data [:events])]
{:db (-> db
(assoc-in ,,, [state-key :adding-funds?] false)
(assoc-in ,,, [state-key :add-funds-amount] nil))
(assoc-in ,,, [state-key :add-funds-amount] nil)
(set-add-funds-tx-in-progress ,,, false))
:fx [[:dispatch [::notification.events/show "Transaction to add funds processed successfully"]]
[:dispatch [:page.job-detail/job-updated]]]})))

(re/reg-event-db
::add-funds-tx-error
(fn [db event]
{:dispatch [::notification.events/show "Error with add funds to job transaction"]}))
{:db (set-add-funds-tx-in-progress db false)
:dispatch [::notification.events/show "Error with add funds to job transaction"]}))
3 changes: 3 additions & 0 deletions ui/src/ethlance/ui/page/job_detail/subscriptions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@

(re/reg-sub :page.job-detail/add-funds-amount (create-get-handler :add-funds-amount))
(re/reg-sub :page.job-detail/adding-funds? (create-get-handler :adding-funds?))
(re/reg-sub :page.job-detail/add-funds-tx-in-progress? (create-get-handler :add-funds-tx-in-progress?))
(re/reg-sub :page.job-detail/end-job-tx-in-progress? (create-get-handler :end-job-tx-in-progress?))
(re/reg-sub :page.job-detail/invite-arbiters-tx-in-progress? (create-get-handler :invite-arbiters-tx-in-progress?))
13 changes: 9 additions & 4 deletions ui/src/ethlance/ui/page/new_job.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@
*token-type (re/subscribe [:page.new-job/token-type])
*token-amount (re/subscribe [:page.new-job/token-amount])
*token-address (re/subscribe [:page.new-job/token-address])
*token-id (re/subscribe [:page.new-job/token-id])]
*token-id (re/subscribe [:page.new-job/token-id])
tx-in-progress? (re/subscribe [:page.new-job/tx-in-progress?])]
(fn []
(let [arbiters (get-in @arbiters-result [:arbiter-search :items])
with-token? (#{:erc20 :erc721 :erc1155} @*token-type)
with-nft? (#{:erc721} @*token-type)
token-with-amount? (#{:erc20 :erc1155 :eth} @*token-type)
token-with-id? (#{:erc721 :erc1155} @*token-type)]
token-with-id? (#{:erc721 :erc1155} @*token-type)
disabled? (or
(not (s/valid? :page.new-job/create @form-values))
@tx-in-progress?)]
[c-main-layout {:container-opts {:class :new-job-main-container}}
[:div.forms-left
[:div.title "New job"]
Expand Down Expand Up @@ -198,8 +202,9 @@
[c-arbiter-for-hire arbiter])])

[c-button
{:on-click (fn [] (>evt [:page.new-job/create]))
:disabled? (not (s/valid? :page.new-job/create @form-values))}
{:on-click (fn [] (when (not disabled?) (>evt [:page.new-job/create])))
:disabled? disabled?
:active? true}
[:div.label "Create"]
[c-icon {:name :ic-arrow-right :size :small}]]]))))

Expand Down
31 changes: 21 additions & 10 deletions ui/src/ethlance/ui/page/new_job/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
[job-data acc ipfs-key db-key]
(assoc acc ipfs-key (job-data db-key)))

(defn set-tx-in-progress [db in-progress?]
(assoc-in db [state-key :tx-in-progress?] in-progress?))

(re/reg-event-fx
:page.new-job/create
[interceptors]
Expand All @@ -148,7 +151,8 @@
tx-opts (if (= token-type :eth)
(assoc tx-opts-base :value (:value offered-value))
tx-opts-base)]
{:fx [[:dispatch [::web3-events/send-tx
{:db (set-tx-in-progress db true)
:fx [[:dispatch [::web3-events/send-tx
{:instance (contract-queries/instance db :ethlance)
:fn :createJob
:args [employer [(clj->js offered-value)] arbiters ipfs-hash]
Expand Down Expand Up @@ -194,7 +198,8 @@
:tx-opts {:from owner}
:on-tx-success [::erc20-allowance-approval-success]
:on-tx-error [::erc20-allowance-approval-error]}]]
{:fx [[:dispatch (if enough-allowance?
{:db (set-tx-in-progress db true)
:fx [[:dispatch (if enough-allowance?
[::send-create-job-tx]
increase-allowance-event)]]})))

Expand Down Expand Up @@ -253,7 +258,8 @@
:tx-opts {:from owner}
:on-tx-success [::create-job-tx-success]
:on-tx-error [::create-job-tx-error]}]]
{:fx [[:dispatch safe-transfer-with-create-job-tx]]})))
{:db (set-tx-in-progress db true)
:fx [[:dispatch safe-transfer-with-create-job-tx]]})))

(re/reg-event-fx
:job-to-ipfs-success
Expand Down Expand Up @@ -305,25 +311,30 @@
(fn [{:keys [db]} [event-name tx-data]]
(let [job-from-event (get-in tx-data [:events :Job-created :return-values :job])]
(println ">>> ::create-job-tx-success" tx-data)
{:fx [[:dispatch [::notification.events/show "Transaction to create job processed successfully"]]
{:db (set-tx-in-progress db false)
:fx [[:dispatch [::notification.events/show "Transaction to create job processed successfully"]]
; When creating job via ERC721/1155 callback (onERC{721,1155}Received), the event data is part of the
; tx-receipt, but doesn't have event name, making it difficult to find and decode. Thus this workaround:
; - requesting the JobCreated event directly from Ethlance and receiving it correctly decoded
(if job-from-event
[:dispatch [::router-events/navigate :route.job/detail {:id (get-in tx-data [:events :Job-created :return-values :job])}]]
[:dispatch-later [{:ms 1000 :dispatch [::router-events/navigate :route.job/detail {:id (get-in tx-data [:events :Job-created :return-values :job])}]}]]
(async-request-event {:event "allEvents"
:block-number (:block-number tx-data)
:contract (district.ui.smart-contracts.queries/instance db :ethlance)
:callback (fn [result]
(re/dispatch
[::router-events/navigate
:route.job/detail
{:id (get (js-obj->clj-map (.-returnValues (first result))) "job")}]))}))]})))
; Delaying navigation to give server time to process the contract event
(js/setTimeout
#(re/dispatch
[::router-events/navigate
:route.job/detail
{:id (get (js-obj->clj-map (.-returnValues (first result))) "job")}])
1000))}))]})))

(re/reg-event-db
::create-job-tx-error
(fn [db event]
{:dispatch [::notification.events/show "Error with creating new job transaction"]}))
{:db (set-tx-in-progress db false)
:dispatch [::notification.events/show "Error with creating new job transaction"]}))

(re/reg-event-fx
::job-to-ipfs-failure
Expand Down
1 change: 1 addition & 0 deletions ui/src/ethlance/ui/page/new_job/subscriptions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@


(re/reg-sub :page.new-job/form (fn [db] (get db new-job.events/state-key)))
(re/reg-sub :page.new-job/tx-in-progress? (create-get-handler :tx-in-progress?))

0 comments on commit 754b303

Please sign in to comment.