You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
5.2 KiB
110 lines
5.2 KiB
7 years ago
|
'use strict'
|
||
4 years ago
|
import { ethers } from 'ethers'
|
||
7 years ago
|
|
||
|
module.exports = {
|
||
|
/**
|
||
|
* deploy the given contract
|
||
|
*
|
||
7 years ago
|
* @param {String} from - sender address
|
||
7 years ago
|
* @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ).
|
||
7 years ago
|
* @param {String} value - decimal representation of value.
|
||
|
* @param {String} gasLimit - decimal representation of gas limit.
|
||
|
* @param {Object} txRunner - TxRunner.js instance
|
||
|
* @param {Object} callbacks - { confirmationCb, gasEstimationForceSend, promptCb }
|
||
|
* [validate transaction] confirmationCb (network, tx, gasEstimation, continueTxExecution, cancelCb)
|
||
|
* [transaction failed, force send] gasEstimationForceSend (error, continueTxExecution, cancelCb)
|
||
|
* [personal mode enabled, need password to continue] promptCb (okCb, cancelCb)
|
||
|
* @param {Function} finalCallback - last callback.
|
||
7 years ago
|
*/
|
||
7 years ago
|
createContract: function (from, data, value, gasLimit, txRunner, callbacks, finalCallback) {
|
||
|
if (!callbacks.confirmationCb || !callbacks.gasEstimationForceSend || !callbacks.promptCb) {
|
||
|
return finalCallback('all the callbacks must have been defined')
|
||
|
}
|
||
5 years ago
|
const tx = { from: from, to: null, data: data, useCall: false, value: value, gasLimit: gasLimit }
|
||
7 years ago
|
txRunner.rawRun(tx, callbacks.confirmationCb, callbacks.gasEstimationForceSend, callbacks.promptCb, (error, txResult) => {
|
||
7 years ago
|
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
|
||
7 years ago
|
finalCallback(error, txResult)
|
||
7 years ago
|
})
|
||
|
},
|
||
|
|
||
|
/**
|
||
7 years ago
|
* call the current given contract ! that will create a transaction !
|
||
7 years ago
|
*
|
||
7 years ago
|
* @param {String} from - sender address
|
||
|
* @param {String} to - recipient address
|
||
7 years ago
|
* @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ).
|
||
7 years ago
|
* @param {String} value - decimal representation of value.
|
||
|
* @param {String} gasLimit - decimal representation of gas limit.
|
||
|
* @param {Object} txRunner - TxRunner.js instance
|
||
|
* @param {Object} callbacks - { confirmationCb, gasEstimationForceSend, promptCb }
|
||
|
* [validate transaction] confirmationCb (network, tx, gasEstimation, continueTxExecution, cancelCb)
|
||
|
* [transaction failed, force send] gasEstimationForceSend (error, continueTxExecution, cancelCb)
|
||
|
* [personal mode enabled, need password to continue] promptCb (okCb, cancelCb)
|
||
|
* @param {Function} finalCallback - last callback.
|
||
7 years ago
|
*/
|
||
7 years ago
|
callFunction: function (from, to, data, value, gasLimit, funAbi, txRunner, callbacks, finalCallback) {
|
||
4 years ago
|
const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure' || funAbi.constant
|
||
5 years ago
|
const tx = { from, to, data, useCall, value, gasLimit }
|
||
7 years ago
|
txRunner.rawRun(tx, callbacks.confirmationCb, callbacks.gasEstimationForceSend, callbacks.promptCb, (error, txResult) => {
|
||
7 years ago
|
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
|
||
7 years ago
|
finalCallback(error, txResult)
|
||
7 years ago
|
})
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* check if the vm has errored
|
||
|
*
|
||
|
* @param {Object} txResult - the value returned by the vm
|
||
|
* @return {Object} - { error: true/false, message: DOMNode }
|
||
|
*/
|
||
|
checkVMError: function (txResult) {
|
||
5 years ago
|
const errorCode = {
|
||
7 years ago
|
OUT_OF_GAS: 'out of gas',
|
||
|
STACK_UNDERFLOW: 'stack underflow',
|
||
|
STACK_OVERFLOW: 'stack overflow',
|
||
|
INVALID_JUMP: 'invalid JUMP',
|
||
|
INVALID_OPCODE: 'invalid opcode',
|
||
|
REVERT: 'revert',
|
||
5 years ago
|
STATIC_STATE_CHANGE: 'static state change',
|
||
|
INTERNAL_ERROR: 'internal error',
|
||
|
CREATE_COLLISION: 'create collision',
|
||
|
STOP: 'stop',
|
||
5 years ago
|
REFUND_EXHAUSTED: 'refund exhausted'
|
||
7 years ago
|
}
|
||
5 years ago
|
const ret = {
|
||
7 years ago
|
error: false,
|
||
|
message: ''
|
||
|
}
|
||
5 years ago
|
if (!txResult.result.execResult.exceptionError) {
|
||
7 years ago
|
return ret
|
||
|
}
|
||
5 years ago
|
const exceptionError = txResult.result.execResult.exceptionError.error || ''
|
||
|
const error = `VM error: ${exceptionError}.\n`
|
||
|
let msg
|
||
7 years ago
|
if (exceptionError === errorCode.INVALID_OPCODE) {
|
||
7 years ago
|
msg = `\t\n\tThe execution might have thrown.\n`
|
||
|
ret.error = true
|
||
7 years ago
|
} else if (exceptionError === errorCode.OUT_OF_GAS) {
|
||
7 years ago
|
msg = `\tThe transaction ran out of gas. Please increase the Gas Limit.\n`
|
||
|
ret.error = true
|
||
7 years ago
|
} else if (exceptionError === errorCode.REVERT) {
|
||
5 years ago
|
const returnData = txResult.result.execResult.returnValue
|
||
7 years ago
|
// It is the hash of Error(string)
|
||
|
if (returnData && (returnData.slice(0, 4).toString('hex') === '08c379a0')) {
|
||
5 years ago
|
const abiCoder = new ethers.utils.AbiCoder()
|
||
|
const reason = abiCoder.decode(['string'], returnData.slice(4))[0]
|
||
7 years ago
|
msg = `\tThe transaction has been reverted to the initial state.\nReason provided by the contract: "${reason}".`
|
||
|
} else {
|
||
6 years ago
|
msg = `\tThe transaction has been reverted to the initial state.\nNote: The called function should be payable if you send value and the value you send should be less than your current balance.`
|
||
7 years ago
|
}
|
||
7 years ago
|
ret.error = true
|
||
7 years ago
|
} else if (exceptionError === errorCode.STATIC_STATE_CHANGE) {
|
||
7 years ago
|
msg = `\tState changes is not allowed in Static Call context\n`
|
||
|
ret.error = true
|
||
|
}
|
||
7 years ago
|
ret.message = `${error}${exceptionError}${msg}\tDebug the transaction to get more information.`
|
||
7 years ago
|
return ret
|
||
|
}
|
||
|
}
|
||
|
|