Skip to content

Commit

Permalink
Merge pull request #3235 from motrebski/blocktrades-bridge-update
Browse files Browse the repository at this point in the history
Make bridge withdrawals based on mapping data
  • Loading branch information
Stefan Schießl committed Apr 27, 2020
2 parents 64d8541 + ec54ffe commit a48264f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class ButtonConversion extends React.Component {
let feeID = balances.has(defaultFeeAssetId)
? defaultFeeAssetId
: balance
? balance.get("asset_type")
: "1.3.0";
? balance.get("asset_type")
: "1.3.0";
return feeID;
}

Expand Down Expand Up @@ -421,6 +421,7 @@ class ButtonWithdraw extends React.Component {
asset={this.props.asset.get("id")}
output_coin_name={this.props.output_coin_name}
output_coin_symbol={this.props.output_coin_symbol}
input_coin_type={this.props.input_coin_type}
output_coin_type={this.props.output_coin_type}
output_supports_memos={this.props.output_supports_memos}
amount_to_withdraw={this.props.amount_to_withdraw}
Expand Down Expand Up @@ -460,6 +461,7 @@ class ButtonWithdrawContainer extends React.Component {
asset={this.props.asset}
output_coin_name={this.props.output_coin_name}
output_coin_symbol={this.props.output_coin_symbol}
input_coin_type={this.props.input_coin_type}
output_coin_type={this.props.output_coin_type}
output_supports_memos={this.props.output_supports_memos}
amount_to_withdraw={this.props.amount_to_withdraw}
Expand Down Expand Up @@ -2414,6 +2416,9 @@ class BlockTradesBridgeDepositRequest extends React.Component {
this.state.withdraw_output_coin_type
].symbol
}
input_coin_type={
this.state.withdraw_input_coin_type
}
output_coin_type={
this.state.withdraw_output_coin_type
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import BalanceComponent from "components/Utility/BalanceComponent";
import counterpart from "counterpart";
import AmountSelector from "components/Utility/AmountSelector";
import AccountActions from "actions/AccountActions";
import {validateAddress, WithdrawAddresses} from "common/gatewayMethods";
import {
validateAddress,
WithdrawAddresses,
getMappingData
} from "common/gatewayMethods";
import {ChainStore} from "bitsharesjs";
import {checkFeeStatusAsync, checkBalance} from "common/trxHelper";
import {debounce} from "lodash-es";
Expand Down Expand Up @@ -272,8 +276,7 @@ class WithdrawModalBlocktrades extends React.Component {
onSubmit() {
if (
!this.state.withdraw_address_check_in_progress &&
(this.state.withdraw_address &&
this.state.withdraw_address.length) &&
this.state.withdraw_address && this.state.withdraw_address.length &&
this.state.withdraw_amount !== null
) {
if (!this.state.withdraw_address_is_valid) {
Expand Down Expand Up @@ -357,20 +360,24 @@ class WithdrawModalBlocktrades extends React.Component {
sendAmount = balanceAmount;
}

AccountActions.transfer(
this.props.account.get("id"),
this.props.issuer.get("id"),
sendAmount.getAmount(),
asset.get("id"),
this.props.output_coin_type +
":" +
this.state.withdraw_address +
(this.state.memo
? ":" + new Buffer(this.state.memo, "utf-8")
: ""),
null,
feeAmount ? feeAmount.asset_id : "1.3.0"
);
getMappingData(
this.props.input_coin_type,
this.props.output_coin_type,
this.state.withdraw_address
).then(result => {
AccountActions.transfer(
this.props.account.get("id"),
this.props.issuer.get("id"),
sendAmount.getAmount(),
asset.get("id"),
result["memo"] +
(this.state.memo
? ":" + new Buffer(this.state.memo, "utf-8")
: ""),
null,
feeAmount ? feeAmount.asset_id : "1.3.0"
);
});

this.setState({
empty_withdraw_value: false
Expand Down Expand Up @@ -419,20 +426,24 @@ class WithdrawModalBlocktrades extends React.Component {

const {feeAmount, fee_asset_id} = this.state;

AccountActions.transfer(
this.props.account.get("id"),
this.props.issuer.get("id"),
parseInt(amount * precision, 10),
asset.get("id"),
this.props.output_coin_type +
":" +
this.state.withdraw_address +
(this.state.memo
? ":" + new Buffer(this.state.memo, "utf-8")
: ""),
null,
feeAmount ? feeAmount.asset_id : fee_asset_id
);
getMappingData(
this.props.input_coin_type,
this.props.output_coin_type,
this.state.withdraw_address
).then(result => {
AccountActions.transfer(
this.props.account.get("id"),
this.props.issuer.get("id"),
parseInt(amount * precision, 10),
asset.get("id"),
result["memo"] +
(this.state.memo
? ":" + new Buffer(this.state.memo, "utf-8")
: ""),
null,
feeAmount ? feeAmount.asset_id : fee_asset_id
);
});
}

onDropDownList() {
Expand Down Expand Up @@ -602,7 +613,7 @@ class WithdrawModalBlocktrades extends React.Component {

if (
!this.state.withdraw_address_check_in_progress &&
(this.state.withdraw_address && this.state.withdraw_address.length)
this.state.withdraw_address && this.state.withdraw_address.length
) {
if (!this.state.withdraw_address_is_valid) {
invalid_address_message = (
Expand Down Expand Up @@ -865,18 +876,13 @@ class WithdrawModalBlocktrades extends React.Component {

WithdrawModalBlocktrades = BindToChainState(WithdrawModalBlocktrades);

export default connect(
WithdrawModalBlocktrades,
{
listenTo() {
return [SettingsStore];
},
getProps(props) {
return {
fee_asset_symbol: SettingsStore.getState().settings.get(
"fee_asset"
)
};
}
export default connect(WithdrawModalBlocktrades, {
listenTo() {
return [SettingsStore];
},
getProps(props) {
return {
fee_asset_symbol: SettingsStore.getState().settings.get("fee_asset")
};
}
);
});
49 changes: 49 additions & 0 deletions app/lib/common/gatewayMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import ls from "./localStorage";
import {blockTradesAPIs, openledgerAPIs} from "api/apiConfig";
import {availableGateways} from "common/gateways";
const blockTradesStorage = new ls("");
let oidcStorage = new ls(
"oidc.user:https://blocktrades.us/:10ecf048-b982-467b-9965-0b0926330869"
);

let fetchInProgess = {};
let fetchCache = {};
Expand Down Expand Up @@ -333,6 +336,52 @@ export function requestDepositAddress({
});
}

export function getMappingData(inputCoinType, outputCoinType, outputAddress) {
let body = JSON.stringify({
inputCoinType,
outputCoinType,
outputAddress: {
address: outputAddress
}
});
let mapping = inputCoinType + outputCoinType + outputAddress;
if (blockTradesStorage.has(`history_mapping_${mapping}`)) {
return Promise.resolve(
blockTradesStorage.get(`history_mapping_${mapping}`)
);
} else {
return new Promise((resolve, reject) => {
let headers = {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${oidcStorage.get("")["access_token"]}`
};
fetch(`${blockTradesAPIs.BASE}/mappings`, {
method: "post",
headers: headers,
body: body
})
.then(reply => {
reply.json().then(result => {
if (result["inputAddress"]) {
blockTradesStorage.set(
`history_mapping_${mapping}`,
result["inputAddress"]
);
resolve(result && result["inputAddress"]);
} else {
reject();
}
});
})
.catch(error => {
console.log("Error: ", error);
reject();
});
});
}
}

export function getBackedCoins({allCoins, tradingPairs, backer}) {
let gatewayStatus = availableGateways[backer];
let coins_by_type = {};
Expand Down

0 comments on commit a48264f

Please sign in to comment.