Skip to content

Commit

Permalink
chore: tweak frontend to allow users to payout grants
Browse files Browse the repository at this point in the history
  • Loading branch information
thelostone-mc committed Jun 23, 2021
1 parent c4e1aa8 commit 4dc589d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 64 deletions.
10 changes: 9 additions & 1 deletion app/assets/v2/js/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Vue.component('grants-cart', {
`${static_url}v2/js/grants/cart/rsk_extension.js`
],
'ALGORAND': [
`${static_url}v2/js/tokens.js`,
`${static_url}v2/js/grants/cart/algorand_extension.js`
]
}
Expand Down Expand Up @@ -555,7 +556,11 @@ Vue.component('grants-cart', {

switch (tenant) {
case 'ALGORAND':
contributeWithAlgorandExtension(grant, vm);
if (data) {
contributeWithAlgorandExtension(grant, vm, data);
} else {
initAlgorandConnection(grant, vm);
}
break;
case 'RSK':
contributeWithRskExtension(grant, vm);
Expand Down Expand Up @@ -623,9 +628,12 @@ Vue.component('grants-cart', {
}
if (additionalAttributes) {
vm.grantData[index].additionalAttributes = additionalAttributes;
console.log(vm.grantData[index]);
}
}
});

vm.$forceUpdate();
},

/**
Expand Down
108 changes: 53 additions & 55 deletions app/assets/v2/js/grants/cart/algorand_extension.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
const contributeWithAlgorandExtension = async(grant, vm, modal) => {
const token_name = grant.grant_donation_currency;
const amount = grant.grant_donation_amount;
const to_address = grant.rsk_payout_address;
const token = vm.getTokenByName(token_name);

// TODO: FIGURE OUT from_address

// const NETWORK = 'TestNet';
const NETWORK = 'MainNet';
const initAlgorandConnection = async(grant, vm) => {

// 1. check if AlgoSigner is available
if (!AlgoSigner) {
_alert({ message: 'Please download or enable AlgoSigner extension' }, 'danger');
modal.closeModal();
return;
}

try {
AlgoSigner.connect().then(async() => {
// step2: get connected accounts
const accounts = await AlgoSigner.accounts({ ledger: NETWORK });
// const NETWORK = 'TestNet';
const NETWORK = 'MainNet';

let is_account_present = false;
// step2: get connected accounts
AlgoSigner.connect().then(async() => {
const addresses = await AlgoSigner.accounts({ ledger: NETWORK });

accounts.map(account=> {
if (account.address == from_address)
is_account_present = true;
});

if (!is_account_present) {
_alert({ message: `Unable to access address ${from_address} in wallet` }, 'danger');
modal.closeModal();
return;
}
if (addresses.length == 0) {
_alert({ message: 'No wallet addresses detected on AlgoSigner extension' }, 'danger');
return;
}
vm.updatePaymentStatus(grant.grant_id, 'waiting-on-user-input', null, {addresses: addresses});
});

};

const contributeWithAlgorandExtension = async(grant, vm, from_address) => {
const token_name = grant.grant_donation_currency;
const amount = grant.grant_donation_amount;
const to_address = grant.algorand_payout_address;
const token = vm.getTokenByName(token_name);

// const NETWORK = 'TestNet';
const NETWORK = 'MainNet';

try {
AlgoSigner.connect().then(async() => {

// step3: check if enough balance is present
const balance = await AlgoSigner.algod({
Expand All @@ -46,41 +47,39 @@ const contributeWithAlgorandExtension = async(grant, vm, modal) => {
) {
// ALGO token
_alert({ message: `Insufficent balance in address ${from_address}` }, 'danger');
modal.closeModal();
return;

}
// ALGO assets
let is_asset_present = false;

if (balance.assets && balance.assets.length > 0) {
balance.assets.map(asset => {
if (asset['asset-id'] == asset_index)
if (asset['asset-id'] == token.addr)
is_asset_present = true;
});
}

if (is_asset_present) {
_alert({ message: `Asset ${token_name} is not present in ${from_address}` }, 'danger');
modal.closeModal();
return;
}

let has_enough_asset_balance = false;

balance.assets.map(asset => {
if (asset['asset-id'] == asset_index && asset['amount'] <= amount * 10 ** token.decimals)
if (asset['asset-id'] == token.addr && asset['amount'] <= amount * 10 ** token.decimals)
has_enough_asset_balance = true;
});

if (has_enough_asset_balance) {
_alert({ message: `Insufficent balance in address ${from_address}` }, 'danger');
modal.closeModal();
return;
}

// step4: set modal to waiting state
vm.updatePaymentStatus(grant.grant_id, 'waiting');

// step4: get txnParams
// step5: get txnParams
const txParams = await AlgoSigner.algod({
ledger: NETWORK,
path: '/v2/transactions/params'
Expand All @@ -92,8 +91,8 @@ const contributeWithAlgorandExtension = async(grant, vm, modal) => {
if (token_name == 'ALGO') {
// ALGO token
txn = {
from: from_address,
to: to_address,
from: from_address.toUpperCase(),
to: to_address.toUpperCase(),
fee: txParams['fee'],
type: 'pay',
amount: amount * 10 ** token.decimals,
Expand All @@ -106,8 +105,8 @@ const contributeWithAlgorandExtension = async(grant, vm, modal) => {
} else {
// ALGO assets
txn = {
from: from_address,
to: to_address,
from: from_address.toUpperCase(),
to: to_address.toUpperCase(),
assetIndex: Number(asset_index.addr),
note: 'contributing to gitcoin grant',
amount: amount * 10 ** token.decimals,
Expand All @@ -121,39 +120,34 @@ const contributeWithAlgorandExtension = async(grant, vm, modal) => {
}

AlgoSigner.sign(txn).then(signedTx => {

// step6: broadcast txn
// step7: broadcast txn
AlgoSigner.send({
ledger: NETWORK,
tx: signedTx.blob
})
.then(tx => {
callback(null, from_address, tx.txId);
})
.catch((e) => {
console.log(e);
_alert({ message: 'Unable to broadcast txn. Please try again' }, 'danger');
modal.closeModal();
return;
});
}).then(tx => {
callback(null, from_address, tx.txId);
}).catch((e) => {
console.log(e);
_alert({ message: 'Unable to broadcast txn. Please try again' }, 'danger');
vm.updatePaymentStatus(grant.grant_id, 'failed');
return;
});

}).catch(e => {
console.log(e);
_alert({ message: 'Unable to sign txn. Please try again' }, 'danger');
modal.closeModal();
vm.updatePaymentStatus(grant.grant_id, 'failed');
return;
});

}).catch(e => {
console.log(e);
_alert({ message: 'Please allow Gitcoin to connect to AlgoSigner extension' }, 'danger');
modal.closeModal();
vm.updatePaymentStatus(grant.grant_id, 'failed');
return;
});
} catch (e) {
modal.closeModal();
_alert({ message: 'Unable to make contribution to grant. Please try again later.' }, 'error');
console.log(error);
callback(err);
return;
}

Expand All @@ -180,7 +174,9 @@ const contributeWithAlgorandExtension = async(grant, vm, modal) => {
const apiUrlBounty = 'v1/api/contribute';

fetchData(apiUrlBounty, 'POST', JSON.stringify(payload)).then(response => {

if (200 <= response.status && response.status <= 204) {
console.log('success', response);
MauticEvent.createEvent({
'alias': 'products',
'data': [
Expand All @@ -194,15 +190,17 @@ const contributeWithAlgorandExtension = async(grant, vm, modal) => {
}
]
});

vm.updatePaymentStatus(grant.grant_id, 'done', txn);

} else {
vm.updatePaymentStatus(grant.grant_id, 'failed');
_alert('Unable to make contribute to grant. Please try again later', 'danger');
_alert('Unable to contribute to grant. Please try again later', 'danger');
console.error(`error: grant contribution failed with status: ${response.status} and message: ${response.message}`);
}
}).catch(function(error) {
vm.updatePaymentStatus(grant.grant_id, 'failed');
_alert('Unable to make contribute to grant. Please try again later', 'danger');
_alert('Unable to contribute to grant. Please try again later', 'danger');
console.log(error);
});
}
Expand Down
4 changes: 2 additions & 2 deletions app/assets/v2/js/pages/bounty_detail/algorand_extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const payWithAlgorandExtension = async(fulfillment_id, to_address, vm, modal) =>

if (balance.assets && balance.assets.length > 0) {
balance.assets.map(asset => {
if (asset['asset-id'] == asset_index)
if (asset['asset-id'] == asset_index.addr)
is_asset_present = true;
});
}
Expand All @@ -67,7 +67,7 @@ const payWithAlgorandExtension = async(fulfillment_id, to_address, vm, modal) =>
let has_enough_asset_balance = false;

balance.assets.map(asset => {
if (asset['asset-id'] == asset_index && asset['amount'] <= amount * 10 ** vm.decimals)
if (asset['asset-id'] == asset_index.addr && asset['amount'] <= amount * 10 ** vm.decimals)
has_enough_asset_balance = true;
});

Expand Down
27 changes: 23 additions & 4 deletions app/grants/templates/grants/cart/extension/algorand.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
Not Eligible for matching
</div>
</div>
<div class=" align-items-center justify-content-around flex-fill flex-shrink-0 d-flex">
<b-button :disabled=!isAlgorandExtInstalled variant="primary" class="btn px-4 font-body" @click="updatePaymentStatus(grant.grant_id, 'waiting');contributeWithExtension(grant, 'ALGORAND')" data-toggle="modal" :data-target="'#payoutModal' + grant.grant_id">
<div class="align-items-center justify-content-around flex-fill flex-shrink-0 d-flex">
<b-button :disabled="!isAlgorandExtInstalled" variant="primary" class="btn btn-primary px-4 font-body" @click="updatePaymentStatus(grant.grant_id, 'waiting');contributeWithExtension(grant, 'ALGORAND')" data-toggle="modal" :data-target="'#payoutModal' + grant.grant_id">
Payout
</b-button>
</div>
Expand All @@ -76,7 +76,7 @@
{% include '../comment.html' %}

<modal :id="'payoutModal' + grant.grant_id" ref="payout-modal">
<div slot="body" class="text-center px-5">
<div slot="body" class="text-center px-3">
<div v-if="grant.payment_status == 'waiting'">
<i class="fas fa-spinner fa-pulse d-block" style="font-size: 7rem;"></i>
<p class="font-bigger-1 mt-5">
Expand All @@ -89,9 +89,28 @@
<p class="font-header font-weight-semibold">
Signing Transaction Failed.
</p>
<p>
<i class="fas fa-times" style="font-size: 7rem;"></i>
<a href="#" class="btn btn-gc-pink btn-block py-3 font-smaller-1 mt-4" data-dismiss="modal" class="btn btn-primary font-smaller-2 py-2 px-4">Try again in some time</a>
<a href="#" class="btn btn-danger btn-block py-3 font-smaller-1 mt-4" data-dismiss="modal" class="btn btn-primary font-smaller-2 py-2 px-4">Try again in some time</a>
</p>
</div>
<div v-else-if="grant.payment_status == 'waiting-on-user-input'">
<p class="font-header font-weight-semibold">
Select the account you want to pay from
</p>

<ul class="pl-0 pt-3" v-if="grant.additionalAttributes && grant.additionalAttributes.addresses">
<li class="notifications__list" v-for="address_info in grant.additionalAttributes.addresses" :key="address_info.address">
<button class="p-2 font-caption text-left btn btn-block btn-outline-dark d-block mb-3 cursor-pointer`"
@click="contributeWithExtension(grant, 'ALGORAND', address_info.address)"
>
<p class="mb-1">
<span>[[ address_info.address ]] </span>
</p>
</button>
</li>
</ul>

</div>
<div v-else>
<p class="font-header font-weight-semibold mb-4">
Expand Down
1 change: 1 addition & 0 deletions app/grants/templates/grants/components/card.html
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ <h2 class="font-subheader">
<img class="float-right" height="20" src="{% static "v2/images/chains/harmony.svg" %}" v-if="grant.tenants.indexOf('HARMONY') > -1">
<img class="float-right" height="20" src="{% static "v2/images/chains/binance.svg" %}" v-if="grant.tenants.indexOf('BINANCE') > -1">
<img class="float-right" height="20" src="{% static "v2/images/chains/rsk.svg" %}" v-if="grant.tenants.indexOf('RSK') > -1">
<img class="float-right" height="20" src="{% static "v2/images/chains/algorand.svg" %}" v-if="grant.tenants.indexOf('ALGORAND') > -1">

<p class="mb-0" v-if="grant.clr_round_num && grant.is_clr_eligible">
<!-- on a round -->
Expand Down
2 changes: 0 additions & 2 deletions app/grants/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
from django.templatetags.static import static
from django.utils import timezone

from app import settings
from app.dashboard.sync.algorand import sync_algorand_payout
from app.settings import BASE_DIR, BASE_URL, MEDIA_URL, STATIC_HOST, STATIC_URL
from avatar.utils import convert_img
from economy.utils import ConversionRateNotFoundError, convert_amount
Expand Down

0 comments on commit 4dc589d

Please sign in to comment.