Swapsteem Development # 2 : Added features to make the trade process more realtime

in #utopian-io6 years ago (edited)

Repository

https://github.com/nirvanaitsolutions/swapsteem

New Features

  • This contribution is related to the utopian Task Request . To complete the task request, we had to implement Issue which is related to trade process. We strictly had to to provide the following features in a flow :

    • For Seller
      • Transfer STEEM/SBD to escrow account
      • Cancel order before it is approved by the agent, in case you you created the order by mistake or do not want to trade anymore.
      • Raise a dispute, in case a buyer has confirmed payment but you have not received fiat in your bank.
      • Release escrow, In case a buyer has confirmed payment and you have received fiat in your account.
    • For buyer
      • Approve escrow, in case you agree with the terms of trade and want to go ahead with the transaction.
      • Reject escrow, in case you are no longer willing to trade.
      • Confirm Payment, When order is approved by the agent and you have transferred the fiat to specified account.
      • Raise a dispute, in case a seller has not released payment after you have paid the fiat.
  • To complete the above features, I created a new order component that will appear just after order creation in the purchase component for open trade. I have have implemented the logic to identify the current user as buyer or seller as shown in below snippet.

 if (data.order_type === 'buy') {
   this.sender = data.createdfor;
   this.reciever = data.createdby;
} else if (data.order_type === 'sell') {
      this.sender = data.createdby;
      this.reciever = data.createdfor
}

The trade starts with the Seller transferring the STEEM/SBD into an escrow via an escrow_transfer

  • IN order to Implement Escrow transfer by seller , we created a button to generate a steemconnect hot sign url for escrow_transfer and set the agent as @swapsteem along with the redirect_uri which redirects the user to our application after successful escrow transfer, and we update order status to escrow_transfer using PUT request to the order API.
transferEscrow() {
    const now = moment();
    const rDeadline: string = now.add(2, 'hours').format('YYYY-MM-DDTHH:MM:SS');
    const eDeadline: string = now.add(3, 'days').format('YYYY-MM-DDTHH:MM:SS');
    const steemAmount: number = this.selectedOrder.order_coin == "STEEM" ? this.selectedOrder.order_coin_amount : 0;
    const sbdAmount: number = this.selectedOrder.order_coin == "SBD" ? this.selectedOrder.order_coin_amount : 0;
    console.log(rDeadline, eDeadline, steemAmount, sbdAmount);
    window.location.href = `https://steemconnect.com/sign/escrow-transfer?from=${this.sender}&to=${this.reciever}&agent=${this.agent}&escrow_id=${this.selectedOrder.escrowID}&sbd_amount=${sbdAmount}%20SBD&steem_amount=${steemAmount}%20STEEM&fee=${0.001}%20STEEM&ratification_deadline=${rDeadline}&escrow_expiration=${eDeadline}&json_meta={"terms":"${this.selectedAd.terms}", "order_id": "${this.selectedOrder._id}"}&redirect_uri=${window.location.origin}/order/${this.selectedOrder._id}?status=escrow_transfer`;
  }

A Buyer/Seller can cancel an order created for his listing until the order_status is agent_escrow_approved.

  • In order to cancel the order from a buyer/seller, we update order status as canceled using PUT request to the order API and update the componenent.
 updateOrderStatus(order_status: string, getAdd?: boolean) {
    this._apiSer.updateSelectedOrderFromAPI(this.selectedOrder._id, JSON.stringify({
      order_status
    })).subscribe((data) => this.zone.run(() => {
      this.selectedOrder = data;
      this.selectedOrder.order_status = order_status;
      if (data.order_type === 'buy') {
        this.sender = data.createdfor;
        this.reciever = data.createdby;
      } else if (data.order_type === 'sell') {
        this.sender = data.createdby;
        this.reciever = data.createdfor
      }
      if (getAdd) {
        this._apiSer.getSelectedTradeFromAPI(this.selectedOrder.ad_id).subscribe(res => {
          this.selectedAd = res;
          console.log(this.selectedAd, 'afjsfhj');
          this.router.navigate([`/order/${this.selectedOrder._id}`], {
            queryParams: {
              status: ''
            }
          });
        });
      }
    }));
  }

transfer escrow.PNG

Once the Seller has transferred STEEM/SBD to escrow, the Buyer can to approve or reject the escrow transfer.
  • In order to implement the Escrow approve/reject from the Buyer, We generate a steemconnect hot sign url and redirect the user to our site , also we update order status to escrow_approve/escrow_reject using PUT request to the order API. ,
  approveRejectEscrow(approve: number) {
    window.location.href = `https://steemconnect.com/sign/escrow-approve?from=${this.sender}&to=${this.reciever}&agent=${this.agent}&who=${this.userData._id}&escrow_id=${this.selectedOrder.escrowID}&approve=${approve}&json_meta={"terms":"${this.selectedAd.terms}", "order_id": "${this.selectedOrder._id}"}&redirect_uri=${window.location.origin}/order/${this.selectedOrder._id}?status=escrow_${approve ? 'approve' : 'reject'}`;
  }

buyer_approve_reject_escrow.PNG
After Approval from the Agent, the Buyer can transfer the fiat to the Seller's specified account via bank tansfer. Once the funds are transferred the Buyer needs the Confirm the payment by clicking the onfirm payment button.

  • In order to implement confirm payment from buyer, we update order status as payment_confirmed using PUT request to the order API and update the componenent.
  updateOrderStatus(order_status: string, getAdd?: boolean) {
    this._apiSer.updateSelectedOrderFromAPI(this.selectedOrder._id, JSON.stringify({
      order_status
    })).subscribe((data) => this.zone.run(() => {
      this.selectedOrder = data;
      this.selectedOrder.order_status = order_status;
      if (data.order_type === 'buy') {
        this.sender = data.createdfor;
        this.reciever = data.createdby;
      } else if (data.order_type === 'sell') {
        this.sender = data.createdby;
        this.reciever = data.createdfor
      }
      if (getAdd) {
        this._apiSer.getSelectedTradeFromAPI(this.selectedOrder.ad_id).subscribe(res => {
          this.selectedAd = res;
          console.log(this.selectedAd, 'afjsfhj');
          this.router.navigate([`/order/${this.selectedOrder._id}`], {
            queryParams: {
              status: ''
            }
          });
        });
      }
    }));
  }

buyer_paymet_confirmation_page.PNG

After confirmation of of payment by the Buyer, A Seller can release the STEEM/SBD to Buyers account via an escrow_release transaction.
  • In order to release escrow from a seller, we generate a steemconnect hot sign url which will redirect the user to our application after successful escrow_release and we update order status to escrow_release using PUT request to the api.
  releaseEscrow() {
    const steemAmount: number = this.selectedOrder.order_coin == "STEEM" ? this.selectedOrder.order_coin_amount : 0;
    const sbdAmount: number = this.selectedOrder.order_coin == "SBD" ? this.selectedOrder.order_coin_amount : 0;
    window.location.href = `https://steemconnect.com/sign/escrow-release?from=${this.sender}&to=${this.reciever}&agent=${this.agent}&who=${this.userData._id}&receiver=${this.reciever}&escrow_id=${this.selectedOrder.escrowID}&sbd_amount=${sbdAmount}%20SBD&steem_amount=${steemAmount}%20STEEM&json_meta={"terms":"${this.selectedAd.terms}", "order_id": "${this.selectedOrder._id}"}&redirect_uri=${window.location.origin}/order/${this.selectedOrder._id}?status=escrow_release`;
  }

seller_relase_escrow.PNG
There are two types of disputes that may arise,

  1. From Seller - Buyer has Confirmed payment but the seller hasnt recieved it till the deadline.
  2. From Buyer - Buyer has Cinfirmed payment but the seller hasnt released the STEEM/SBD within the deadline.
  • In order to raise a dispute escrow from a buyer/seller, we generate a steemconnect hot sign url which will be redirected to our application after successful operation and we update the order status buyer_escrow_dispute/seller_escrow_dispute using PUT request to the api.
  raiseDispute(disputeType) {
    window.location.href = `https://steemconnect.com/sign/escrow-dispute?from=${this.sender}&to=${this.reciever}&agent=${this.agent}&who=${this.userData._id}&escrow_id=${this.selectedOrder.escrowID}&json_meta={"terms":"${this.selectedAd.terms}", "order_id": "${this.selectedOrder._id}"}&redirect_uri=${window.location.origin}/order/${this.selectedOrder._id}?status=${disputeType}_escrow_dispute`
  }

buyer_raise_dispute.PNG
seller_raise_dipute.PNG

More Features

In order to remove dependency on chat for Account number exchange, we added those fields to listing and order components so that we can make the process more real time. We added the textboxes as per the target currency since there are different paams needed when sending mone within differrent countries. As of now
- IF Target currency is INR
- Account Holder's Name
- Account Number
- IFSC Code
- Bank Name
- Bank Address (optional)
- SWIFT/BIC code (optional)
- IF Target currency is USD
- Account Holder's Name
- Account Number
- ABA Number
- Bank Name
- Bank Address(optional)
- SWIFT/BIC code(optional)
- IF Target currency is KRW
- Account Holder's Name
- Account Number
- Bank Name
- Bank Address(optional)
- SWIFT/BIC code(optional)
You can see the added fields in the screenshot below:

Bug Fixes

- Added checks for API failure and various fixes.
There were many error messages being shown on the browser console because of the absence of the data checks. I implemented validations wherever required to ensure smooth transactions.

Relevant PRs and commits

GitHub Account

https://github.com/Shubh1692

Sort:  

Thanks for the contribution, @svijay1692! I'm glad to see the task request was successful! It also seems like a lot of work went into this (not just from you), and overall the quality seems high - great work! There are a couple of things I would like to point out though:

  • I see you've left quite a few console.logs throughout the code. I assume you used this for debugging, but I think it's better to remove it once the function has been completed, for example.
  • The spelling and / or grammar could be improved. I would recommend asking others to have a quick read over - it's easy to miss these mistakes, so more eyes always help!
  • Add some comments / docstrings to the added functions. It will help future contributors (and yourself when coming back to old code).

I was also wondering if, since there are multiple authors working on the same branch (master) / pull request, it wouldn't be easier for you guys to separate this? You could create a branch for each feature and merge that into master (or create a develop branch) instead, but of course it's up to you.

Also, it takes a really long time for https://swapsteem.herokuapp.com/home to load (it took a long time to loa to 95% and then just got stuck). I don't know if this is because of Heroku, or because of something on Swapsteem's side - try to make sure this is working in the future.

Other than that everything looks great. Keep up the great work and good luck with future bug reports (I saw Swapsteem was added to the bug hunting whitelist)!


Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thanks @amosbastian for the review!
The demo is available at www.swapsteem.online, please referit for checking out the application.
We are a small team as of now, all of us having access to the repo. We will surely set up a git work flow for an ideal dev env.

Great! You should update the website linked in the repository from https://swapsteem.herokuapp.com to http://www.swapsteem.online/home in that case. Good luck with further development!

Thank you for your review, @amosbastian! Keep up the good work!

Purely from a workflow PoV, if the Buyer selling STEEM / SBD declines the fact that she received the payment and thus disputes, how does the conflict resolution happens ?

Lets assume that the buyer has made the payment and the seller is malicious.

@bobinson, the agent contacts both the accounts via memo to have a chat on discord. in case of dispute, the agent has authority to release funds to either parties. the agent asks for proofs from both sides and takes the decision. The agent releases the STEEM to the buyer in case the seller is found malicious.

Ok - this process going to be more or less like other p2p exchange right now. Sounds fair. I think you can make it better at a later pint of time.

Hi @svijay1692!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Congratulations @svijay1692! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 100 upvotes. Your next target is to reach 250 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!

Hey, @svijay1692!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!