Skip to content

Commit

Permalink
Finish all code. real deposit/2-step withdrawal/redeem for UMA; edit …
Browse files Browse the repository at this point in the history
…Balancer weights.
  • Loading branch information
EndymionJkb committed May 23, 2020
1 parent 5b9320c commit cf148e7
Show file tree
Hide file tree
Showing 24 changed files with 567 additions and 121 deletions.
54 changes: 0 additions & 54 deletions app/assets/javascripts/uma_synthetic.js

This file was deleted.

139 changes: 139 additions & 0 deletions app/assets/javascripts/uma_synthetic.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
function deposit_collateral(pie, uma_address) {
var amount = $('#amount').val();

if (amount > 0) {
try {
uma_deposit(uma_address, amount.toString());

jQuery.ajax({url: '/pies/'+ pie + '/deposit_collateral.js',
data: {'amount': amount},
type: "PUT",
success: function(data) { update_graphics(JSON.parse(data)); },
error: function() { alert('Oh noes!'); },
async: false});
}
catch(err) {
alert(JSON.stringify(err));
}
}
else {
alert("Please enter a valid amount");
}
}

async function uma_deposit(uma_address, amount) {
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.deposit({ rawValue: web3.utils.toWei(amount) });
}

async function request_withdrawal(pool, uma_address) {
var amount = $('#amount').val();

if (amount > 0) {
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));

try {
await emp.requestWithdrawal({ rawValue: web3.utils.toWei(amount) });

jQuery.ajax({url: '/balancer_pools/'+ pool + '/request_withdrawal.js',
data: {'amount': amount},
type: "PUT",
success: function(data) { alert("If undisputed, your withdrawal will be available in 2 hours"); window.location.reload(); },
error: function() { alert('You cannot withdraw so much that your collateralization falls below the minimum!'); },
async: false});
}
catch(err) {
alert(JSON.stringify(err));
}
}
else {
alert("Please enter a valid amount");
}
}

function withdraw_collateral(pie, uma_address) {
try {
uma_withdrawal(uma_address);

jQuery.ajax({url: '/pies/'+ pie + '/withdraw_collateral.js',
type: "PUT",
success: function(data) { update_graphics(JSON.parse(data)); },
error: function() { alert('You cannot withdraw so much that your collateralization falls below the minimum!'); },
async: false});
}
catch(err) {
alert(JSON.stringify(err));
}
}

async function uma_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.withdrawPassedRequest();
}

function redeem_tokens(pie, uma_address) {
var amount = $('#amount').val();

if (amount > 0) {
try {
uma_redeem(uma_address, amount);

jQuery.ajax({url: '/pies/'+ pie + '/redeem_tokens.js',
data: {'amount': amount},
type: "PUT",
success: function(data) { update_graphics(JSON.parse(data)); },
error: function() { alert('Oh noes!'); },
async: false});
}
catch(err) {
alert(JSON.stringify(err));
}
}
else {
alert("Please enter a valid amount");
}
}

async function uma_redeem(uma_address, amount) {
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));

// Get the address of the synthetic token we deployed
const tokenAddress = await emp.tokenCurrency();

var syntheticToken = new ethers.Contract(tokenAddress,
JSON.parse('<%=SYNTHETIC_TOKEN_ABI.html_safe%>'),
provider.getSigner(0));

await syntheticToken.approve(uma_address, web3.utils.toWei(amount.toString()));

await emp.redeem({ rawValue: web3.utils.toWei(amount.toString()) });
}

function update_graphics(data) {
$('#collateralization').text(data.collateralization);
$('#collateral_progress').attr('class', data.progress_class + " progress-bar progress-bar-striped");
$('#adjustments').text(data.adjustments);
$('#total_value').text(data.total_value);
}
Loading

0 comments on commit cf148e7

Please sign in to comment.