Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: Support web3 >= 1.0.0-beta.38 #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 114 additions & 69 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,81 +15,126 @@ import Output from './output';
import Period from './period';
import Tx from './transaction';

// Hard to use web3-core-method package. It requires a lot of deps
// that should be synced with web3 version, so i had to copypaste this class
// and patch it a bit (remove async/await and some meaningless stuff like cloneDeep)
import AbstractMethod from './web3/AbstractMethod';

import { BLOCKS_PER_PERIOD } from './constants';

const formatUtxo = utxo => ({
output: Output.fromJSON(utxo.output),
outpoint: Outpoint.fromRaw(utxo.outpoint),
})

export function extendWeb3(web3Instance) {
// `_extend` for web3 0.2x.x, `extend` for 1.x
const extend = web3Instance._extend || web3Instance.extend; // eslint-disable-line no-underscore-dangle, max-len

const getUnspent = {
name: 'getUnspent',
call: 'plasma_unspent',
params: 1,
inputFormatters: [
extend.formatters.inputAddressFormatter, // account address
],
outputFormatter: (unspent) => {
if (Array.isArray(unspent)) {
// web3 0.2x.x passes in an array
return unspent.map((u) => ({
output: u.output,
outpoint: Outpoint.fromRaw(u.outpoint),
}));
}
return {
output: unspent.output,
outpoint: Outpoint.fromRaw(unspent.outpoint),
}
},
};

extend({
methods: [
new extend.Method(getUnspent),
new extend.Method({
...getUnspent,
name: 'getUnspentAll',
params: 0,
}),
new extend.Method({
name: 'getColor',
call: 'plasma_getColor',
params: 1,
inputFormatters: [
extend.formatters.inputAddressFormatter, // token contract address
],
outputFormatter: Number,
}),
new extend.Method({
name: 'getColors',
call: 'plasma_getColors',
params: 0,
inputFormatters: [],
outputFormatter: String,
}),
new extend.Method({
name: 'status',
call: 'plasma_status',
params: 0,
inputFormatters: [],
outputFormatter: String,
}),
new extend.Method({
name: 'getConfig',
call: 'plasma_getConfig',
params: 0,
inputFormatters: [],
outputFormatter: a => a,
}),
new extend.Method({
name: 'getValidatorInfo',
call: 'validator_getAddress',
params: 0,
inputFormatters: [],
outputFormatter: a => a,
}),
],
});
if (extend) {
const getUnspent = {
name: 'getUnspent',
call: 'plasma_unspent',
params: 1,
inputFormatters: [
extend.formatters.inputAddressFormatter, // account address
],
outputFormatter: (unspent) => {
if (Array.isArray(unspent)) {
// web3 0.2x.x passes in an array
return unspent.map(formatUtxo);
}
return formatUtxo(unspent);
},
};
extend({
methods: [
new extend.Method(getUnspent),
new extend.Method({
...getUnspent,
name: 'getUnspentAll',
params: 0,
}),
new extend.Method({
name: 'getColor',
call: 'plasma_getColor',
params: 1,
inputFormatters: [
extend.formatters.inputAddressFormatter, // token contract address
],
outputFormatter: Number,
}),
new extend.Method({
name: 'getColors',
call: 'plasma_getColors',
params: 0,
inputFormatters: [],
outputFormatter: String,
}),
new extend.Method({
name: 'status',
call: 'plasma_status',
params: 0,
inputFormatters: [],
outputFormatter: String,
}),
new extend.Method({
name: 'getConfig',
call: 'plasma_getConfig',
params: 0,
inputFormatters: [],
outputFormatter: a => a,
}),
new extend.Method({
name: 'getValidatorInfo',
call: 'validator_getAddress',
params: 0,
inputFormatters: [],
outputFormatter: a => a,
}),
],
});
} else { // web3 since 1.0.0-beta.38 has no extend
const getConfig = () => {
const method = new AbstractMethod('plasma_getConfig', 0, null, null, web3Instance);
return method.execute();
}

const getColor = (...args) => {
const method = new AbstractMethod('plasma_getColor', 1, null, null, web3Instance);
method.setArguments(args);
return method.execute().then(Number);
}

const getUnspent = (...args) => {
const method = new AbstractMethod('plasma_unspent', 1, null, null, web3Instance);
method.setArguments(args);
return method.execute().then(utxos => utxos.map(formatUtxo));
}

const getUnspentAll = () => {
const method = new AbstractMethod('plasma_unspent', 0, null, null, web3Instance);
return method.execute().then(utxos => utxos.map(formatUtxo));
}

const getStatus = () => {
const method = new AbstractMethod('plasma_status', 0, null, null, web3Instance);
return method.execute();
}

const getValidatorInfo = () => {
const method = new AbstractMethod('validator_getAddress', 0, null, null, web3Instance);
return method.execute();
}

web3Instance.getConfig = getConfig;
web3Instance.getColor = getColor;
web3Instance.getUnspent = getUnspent;
web3Instance.getUnspentAll = getUnspentAll;
web3Instance.getStatus = getStatus;
web3Instance.getValidatorInfo = getValidatorInfo;
}

return web3Instance;
}

Expand Down
Loading