Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Working Token transfer for multi-sig wallet #3282
Browse files Browse the repository at this point in the history
  • Loading branch information
ngotchac committed Dec 6, 2016
1 parent 4678633 commit e0fef73
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 26 deletions.
18 changes: 12 additions & 6 deletions js/src/api/contract/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,21 @@ export default class Contract {
});
}

_encodeOptions (func, options, values) {
getCallData = (func, options, values) => {
let data = options.data;

const tokens = func ? this._abi.encodeTokens(func.inputParamTypes(), values) : null;
const call = tokens ? func.encodeCall(tokens) : null;

if (options.data && options.data.substr(0, 2) === '0x') {
options.data = options.data.substr(2);
if (data && data.substr(0, 2) === '0x') {
data = data.substr(2);
}
options.data = `0x${options.data || ''}${call || ''}`;

return `0x${data || ''}${call || ''}`;
}

_encodeOptions (func, options, values) {
options.data = this.getCallData(func, options, values);
return options;
}

Expand All @@ -209,10 +215,10 @@ export default class Contract {

_bindFunction = (func) => {
func.call = (options, values = []) => {
const callData = this._encodeOptions(func, this._addOptionsTo(options), values);
const callParams = this._encodeOptions(func, this._addOptionsTo(options), values);

return this._api.eth
.call(callData)
.call(callParams)
.then((encoded) => func.decodeOutput(encoded))
.then((tokens) => tokens.map((token) => token.value))
.then((returns) => returns.length === 1 ? returns[0] : returns);
Expand Down
69 changes: 49 additions & 20 deletions js/src/modals/Transfer/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,26 +409,52 @@ export default class TransferStore {
return this._getTransferMethod().postTransaction(options, values);
}

_estimateGas (forceToken = false) {
const { options, values } = this._getTransferParams(true, forceToken);
return this._getTransferMethod(true, forceToken).estimateGas(options, values);
}

estimateGas () {
const { options, values } = this._getTransferParams(true);
return this._getTransferMethod(true).estimateGas(options, values);
if (this.isEth || !this.isWallet) {
return this._estimateGas();
}

return Promise
.all([
this._estimateGas(true),
this._estimateGas()
])
.then((results) => results[0].plus(results[1]));
}

_getTransferMethod (gas = false) {
_getTransferMethod (gas = false, forceToken = false) {
const { isEth, isWallet } = this;

if (isEth && !isWallet) {
if (isEth && !isWallet && !forceToken) {
return gas ? this.api.eth : this.api.parity;
}

if (isWallet) {
if (isWallet && !forceToken) {
return this.wallet.instance.execute;
}

return this.token.contract.instance.transfer;
}

_getTransferParams (gas = false) {
_getData (gas = false) {
const { isEth, isWallet } = this;

if (!isWallet || isEth) {
return this.data && this.data.length ? this.data : '';
}

const func = this._getTransferMethod(gas, true);
const { options, values } = this._getTransferParams(gas, true);

return this.token.contract.getCallData(func, options, values);
}

_getTransferParams (gas = false, forceToken = false) {
const { isEth, isWallet } = this;

const to = (isEth && !isWallet) ? this.recipient
Expand All @@ -446,27 +472,30 @@ export default class TransferStore {
options.gas = MAX_GAS_ESTIMATION;
}

if (isEth && !isWallet) {
if (isEth && !isWallet && !forceToken) {
options.value = this.api.util.toWei(this.value || 0);

if (this.data && this.data.length) {
options.data = this.data;
}
options.data = this._getData(gas);

return { options, values: [] };
}

const values = isWallet
? [
this.recipient,
this.api.util.toWei(this.value || 0),
this.data || ''
]
: [
this.recipient,
new BigNumber(this.value || 0).mul(this.token.format).toFixed(0)
if (isWallet && !forceToken) {
const to = isEth ? this.recipient : this.token.contract.address;
const value = isEth ? this.api.util.toWei(this.value || 0) : new BigNumber(0);

const values = [
to, value,
this._getData(gas)
];

return { options, values };
}

const values = [
this.recipient,
new BigNumber(this.value || 0).mul(this.token.format).toFixed(0)
];

return { options, values };
}

Expand Down

0 comments on commit e0fef73

Please sign in to comment.