Skip to content

Commit

Permalink
Add cancel withdrawal functionality; remove unused tokenFactory address.
Browse files Browse the repository at this point in the history
  • Loading branch information
EndymionJkb committed May 23, 2020
1 parent cf148e7 commit 987de6a
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 21 deletions.
26 changes: 26 additions & 0 deletions app/assets/javascripts/uma_synthetic.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ async function request_withdrawal(pool, uma_address) {
}
}

function cancel_withdrawal(pool, uma_address) {
try {
uma_cancel_withdrawal(uma_address);

jQuery.ajax({url: '/balancer_pools/'+ pool + '/cancel_withdrawal.js',
type: "PUT",
success: function(data) { alert("Withdrawal canceled successfully"); window.location.reload(); },
error: function() { alert('Oh noes!'); },
async: false});
}
catch(err) {
alert(JSON.stringify(err));
}
}

async function uma_cancel_withdrawal(uma_address) {
web3 = new Web3(web3.currentProvider);
provider = new ethers.providers.Web3Provider(web3.currentProvider);

var emp = new ethers.Contract(uma_address,
JSON.parse('<%=EMP_ABI.html_safe%>'),
provider.getSigner(0));

await emp.cancelWithdrawal();
}

function withdraw_collateral(pie, uma_address) {
try {
uma_withdrawal(uma_address);
Expand Down
15 changes: 13 additions & 2 deletions app/controllers/balancer_pools_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ def show

# Read the address of the ExpiringMultiPartyCreator (from the uma_prep script)
@empCreatorAddress = IO.read('db/data/ExpiringMultiPartyCreator.txt')
@tokenFactoryAddress = IO.read('db/data/TokenFactory.txt')

@expiry_date_str = UmaExpiryDate.find_by_unix(@pie.uma_expiry_date).date_str

Expand All @@ -231,6 +230,7 @@ def show
base_amount = @data[:investment].to_f * @pie.pct_equities / 100.0

@uma_collateral = {:address => collateral_coin.address,
:coin => collateral_coin.coin,
:synthetic_amount => base_amount,
:collateral_amount => base_amount * MIN_COLLATERALIZATION}

Expand Down Expand Up @@ -517,7 +517,18 @@ def request_withdrawal
format.html { redirect_to root_path }
end
end


def cancel_withdrawal
pool = BalancerPool.find(params[:id])

pool.update_attributes(:pending_withdrawal => nil, :withdrawal_available => nil)

respond_to do |format|
format.js { head :ok }
format.html { redirect_to root_path }
end
end

private
def sanity_check
unless @pool.user == current_user
Expand Down
6 changes: 4 additions & 2 deletions app/views/balancer_pools/_rebalance.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
- if pool.pending_withdrawal.nil?
%br= link_to 'Request Withdrawal', '#', :style => withdraw_style, :class => 'btn btn-primary', :onclick => "request_withdrawal(#{pool.id}, '#{pool.uma_address}'); return false;"
- else
- ready_style = Time.now > pool.withdrawal_available ? '' : 'pointer-events: none;'
%br= link_to "Withdraw Collateral (#{pool.pending_withdrawal})", '#', :style => ready_style, :class => 'btn btn-primary', :onclick => "withdraw_collateral(#{pie.id}, '#{pool.uma_address}'); window.location.reload(); return false;"
- if Time.now > pool.withdrawal_available
%br= link_to "Withdraw Collateral (#{pool.pending_withdrawal})", '#', :class => 'btn btn-primary', :onclick => "withdraw_collateral(#{pie.id}, '#{pool.uma_address}'); window.location.reload(); return false;"
- else
%br= link_to "Cancel Withdrawal (#{pool.pending_withdrawal})", '#', :class => 'btn btn-primary', :onclick => "cancel_withdrawal(#{pool.id}, '#{pool.uma_address}'); window.location.reload(); return false;"
%br= link_to 'Redeem Tokens', '#', :title => 'After expiration', :class => 'btn btn-primary', :onclick => "redeem_tokens(#{pie.id}, '#{pool.uma_address}'); return false;"
.vl{:style => 'float:left'}
.col-md-3
Expand Down
28 changes: 17 additions & 11 deletions app/views/balancer_pools/_synthetic.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,20 @@
// Get the address of the deployed creator contract (deployed in the uma_prep script)
var empCreatorAddress = $('#emp_creator').val();

// Get the address of the deployed token factory contract (deployed in the uma_prep script)
var tokenFactoryAddress = $('#token_factory').val();

// Get the price feed assigned to this pie - in Hex format
var priceFeed = $('#uma_price_feed').val();

web3 = new Web3(web3.currentProvider);
provider = new ethers.providers.Web3Provider(web3.currentProvider);

var empCreator = new ethers.Contract(empCreatorAddress,
JSON.parse('#{EMP_CREATOR_ABI.html_safe}'),
provider.getSigner(0));
const empCreator = new ethers.Contract(empCreatorAddress,
JSON.parse('#{EMP_CREATOR_ABI.html_safe}'),
provider.getSigner(0));

create_emp(empCreator, priceFeed, tokenFactoryAddress);
create_emp(empCreator, priceFeed);
}

async function create_emp(empCreator, priceFeed, tokenFactoryAddress) {
async function create_emp(empCreator, priceFeed) {
const params = { expirationTimestamp: "#{pie.uma_expiry_date}",
collateralAddress: "#{uma_collateral[:address]}",
priceFeedIdentifier: priceFeed,
Expand All @@ -65,9 +62,18 @@
const empAddress = txResult.logs[0].args.expiringMultiPartyAddress;

// Now get the ExpiringMultiParty contract we just deployed
var emp = new ethers.Contract(empAddress,
JSON.parse('#{EMP_ABI.html_safe}'),
provider.getSigner(0));
const emp = new ethers.Contract(empAddress,
JSON.parse('#{EMP_ABI.html_safe}'),
provider.getSigner(0));

// Approve collateral
var abi = $('#' + "#{uma_collateral[:coin]}_abi").val();

const spend = new ethers.Contract("#{uma_collateral[:address]}",
JSON.parse(abi),
provider.getSigner(0));

await spend.approve(empAddress, web3.utils.toWei("#{uma_collateral[:collateral_amount]}"));

// And create the position
await emp.create({rawValue: web3.utils.toWei("#{uma_collateral[:collateral_amount]}")},
Expand Down
6 changes: 3 additions & 3 deletions app/views/balancer_pools/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@
}

async function getAvatar(ens_name) {
var ens = new ethers.Contract('#{ENS_PUBLIC_RESOLVER}',
JSON.parse('#{PUBLIC_RESOLVER_ABI.html_safe}'),
provider.getSigner(0));
const ens = new ethers.Contract('#{ENS_PUBLIC_RESOLVER}',
JSON.parse('#{PUBLIC_RESOLVER_ABI.html_safe}'),
provider.getSigner(0));

// Thank you, ethers! Need to hash the name a certain way, and also convert to Bytes32
const node = ethers.utils.namehash(ens_name);
Expand Down
1 change: 0 additions & 1 deletion app/views/balancer_pools/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
.progress
.progress_inner
= hidden_field_tag :emp_creator, @empCreatorAddress
= hidden_field_tag :token_factory, @tokenFactoryAddress
= hidden_field_tag :uma_price_feed, @price_feed_identifer
- @abis.each do |coin, abi|
= hidden_field_tag "#{coin}_abi", abi
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
put 'calculate_rebalance'
put 'confirm_rebalance'
put 'request_withdrawal'
put 'cancel_withdrawal'
end
end

Expand Down
2 changes: 1 addition & 1 deletion db/data/ExpiringMultiPartyCreator.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x9c2acA0a7FfaCB4581D2C8780FB561687Cd7bb47
0x2B4aA72BCD48347D7a72993Ec9712D855da14364
2 changes: 1 addition & 1 deletion db/data/TokenFactory.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x556B611d4Db9f228827Fa1Aa293EFe3244254F4D
0xcd8C03fE79B18A3e0db16204Fdb34223c86E80CF

0 comments on commit 987de6a

Please sign in to comment.