Skip to content

Commit

Permalink
Merge branch 'feat/token-info' of github.com:gabrielbazan7/bitcore
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsujlangston committed Jun 18, 2021
2 parents 1559563 + 929c1a3 commit dbc724d
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 18 deletions.
16 changes: 14 additions & 2 deletions packages/bitcore-wallet-client/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1594,8 +1594,6 @@ export class API extends EventEmitter {

var args = [];
if (opts.coin) {
if (!_.includes(Constants.COINS, opts.coin))
return cb(new Error('Invalid coin'));
args.push('coin=' + opts.coin);
}
if (opts.tokenAddress) {
Expand Down Expand Up @@ -2546,6 +2544,20 @@ export class API extends EventEmitter {
});
}

// /**
// * Returns contract info. (name symbol precision)
// * @param {string} opts.tokenAddress - token contract address
// * @return {Callback} cb - Return error (if exists) instantiation info
// */
getTokenContractInfo(opts, cb) {
var url = '/v1/token/info';
opts.network = this.credentials.network;
this.request.post(url, opts, (err, contractInfo) => {
if (err) return cb(err);
return cb(null, contractInfo);
});
}

// /**
// * Get wallet status based on a string identifier (one of: walletId, address, txid)
// *
Expand Down
11 changes: 6 additions & 5 deletions packages/bitcore-wallet-client/src/lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ export class Utils {

static formatAmount(satoshis, unit, opts?) {
$.shouldBeNumber(satoshis);
$.checkArgument(_.includes(_.keys(Constants.UNITS), unit));

var clipDecimals = (number, decimals) => {
let str = number.toString();
Expand Down Expand Up @@ -295,15 +294,17 @@ export class Utils {

var u = Constants.UNITS[unit];
var precision = opts.fullPrecision ? 'full' : 'short';
var decimals = opts.decimals ? opts.decimals[precision] : u[precision];
var toSatoshis = opts.toSatoshis ? opts.toSatoshis : u.toSatoshis;
var amount = clipDecimals(
satoshis / u.toSatoshis,
u[precision].maxDecimals
).toFixed(u[precision].maxDecimals);
satoshis / toSatoshis,
decimals.maxDecimals
).toFixed(decimals.maxDecimals);
return addSeparators(
amount,
opts.thousandsSeparator || ',',
opts.decimalSeparator || '.',
u[precision].minDecimals
decimals.minDecimals
);
}

Expand Down
5 changes: 3 additions & 2 deletions packages/bitcore-wallet-client/src/lib/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ export class Key {

if (password) $.shouldBeString(password, 'provide password');

this._checkCoin(opts.coin);
this._checkNetwork(opts.network);
$.shouldBeNumber(opts.account, 'Invalid account');
$.shouldBeNumber(opts.n, 'Invalid n');
Expand Down Expand Up @@ -519,7 +518,9 @@ export class Key {
} else {
let tx = t.uncheckedSerialize();
tx = typeof tx === 'string' ? [tx] : tx;
const chain = Utils.getChain(txp.coin);
const chain = txp.chain
? txp.chain.toUpperCase()
: Utils.getChain(txp.coin);
const txArray = _.isArray(tx) ? tx : [tx];
const isChange = false;
const addressIndex = 0;
Expand Down
5 changes: 4 additions & 1 deletion packages/bitcore-wallet-client/src/lib/verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ export class Verifier {

var creatorKeys = _.find(credentials.publicKeyRing, item => {
if (
Utils.xPubToCopayerId(txp.coin || 'btc', item.xPubKey) === txp.creatorId
Utils.xPubToCopayerId(
txp.chain ? txp.chain : txp.coin ? txp.coin : 'btc',
item.xPubKey
) === txp.creatorId
)
return true;
});
Expand Down
14 changes: 14 additions & 0 deletions packages/bitcore-wallet-service/src/lib/blockchainexplorers/v8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,20 @@ export class V8 {
});
}

getTokenContractInfo(opts, cb) {
const url = this.baseUrl + '/token/' + opts.tokenAddress;
console.log('[v8.js.378:url:] CHECKING CONTRACT INFO', url);
this.request
.get(url, {})
.then(contractInfo => {
contractInfo = JSON.parse(contractInfo);
return cb(null, contractInfo);
})
.catch(err => {
return cb(err);
});
}

getMultisigTxpsInfo(opts, cb) {
const url = this.baseUrl + '/ethmultisig/txps/' + opts.multisigContractAddress;
console.log('[v8.js.378:url:] CHECKING CONTRACT TXPS INFO', url);
Expand Down
12 changes: 6 additions & 6 deletions packages/bitcore-wallet-service/src/lib/chain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class ChainProxy {
}

getBitcoreTx(txp: TxProposal, opts = { signed: true }) {
return this.get(txp.coin).getBitcoreTx(txp, { signed: opts.signed });
return this.get(txp.chain).getBitcoreTx(txp, { signed: opts.signed });
}

convertFeePerKb(coin, p, feePerKb) {
Expand All @@ -132,15 +132,15 @@ class ChainProxy {
}

checkTx(server, txp) {
return this.get(txp.coin).checkTx(server, txp);
return this.get(txp.chain).checkTx(server, txp);
}

checkTxUTXOs(server, txp, opts, cb) {
return this.get(txp.coin).checkTxUTXOs(server, txp, opts, cb);
return this.get(txp.chain).checkTxUTXOs(server, txp, opts, cb);
}

selectTxInputs(server, txp, wallet, opts, cb) {
return this.get(txp.coin).selectTxInputs(server, txp, wallet, opts, cb);
return this.get(txp.chain).selectTxInputs(server, txp, wallet, opts, cb);
}

checkUtxos(coin, opts) {
Expand All @@ -167,8 +167,8 @@ class ChainProxy {
return this.get(coin).supportsMultisig();
}

addSignaturesToBitcoreTx(coin, tx, inputs, inputPaths, signatures, xpub, signingMethod) {
this.get(coin).addSignaturesToBitcoreTx(tx, inputs, inputPaths, signatures, xpub, signingMethod);
addSignaturesToBitcoreTx(chain, tx, inputs, inputPaths, signatures, xpub, signingMethod) {
this.get(chain).addSignaturesToBitcoreTx(tx, inputs, inputPaths, signatures, xpub, signingMethod);
}

validateAddress(wallet, inaddr, opts) {
Expand Down
11 changes: 11 additions & 0 deletions packages/bitcore-wallet-service/src/lib/expressapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,17 @@ export class ExpressApp {
});
});

router.post('/v1/token/info', (req, res) => {
getServerWithAuth(req, res, async server => {
try {
const tokenContractInfo = await server.getTokenContractInfo(req.body);
res.json(tokenContractInfo);
} catch (err) {
returnError(err, res, req);
}
});
});

router.get('/v1/sendmaxinfo/', (req, res) => {
getServerWithAuth(req, res, server => {
const q = req.query;
Expand Down
7 changes: 5 additions & 2 deletions packages/bitcore-wallet-service/src/lib/model/txproposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ITxProposal {
walletId: string;
creatorId: string;
coin: string;
chain: string;
network: string;
message: string;
payProUrl: string;
Expand Down Expand Up @@ -79,6 +80,7 @@ export class TxProposal {
walletId: string;
creatorId: string;
coin: string;
chain: string;
network: string;
message: string;
payProUrl: string;
Expand Down Expand Up @@ -131,7 +133,6 @@ export class TxProposal {
static create(opts) {
opts = opts || {};

$.checkArgument(Utils.checkValueInCollection(opts.coin, Constants.COINS));
$.checkArgument(Utils.checkValueInCollection(opts.network, Constants.NETWORKS));

const x = new TxProposal();
Expand All @@ -151,6 +152,7 @@ export class TxProposal {
x.walletId = opts.walletId;
x.creatorId = opts.creatorId;
x.coin = opts.coin;
x.chain = opts.chain;
x.network = opts.network;
x.signingMethod = opts.signingMethod;
x.message = opts.message;
Expand Down Expand Up @@ -220,6 +222,7 @@ export class TxProposal {
x.walletId = obj.walletId;
x.creatorId = obj.creatorId;
x.coin = obj.coin || Defaults.COIN;
x.chain = obj.chain ? obj.chain : x.coin;
x.network = obj.network;
x.outputs = obj.outputs;
x.amount = obj.amount;
Expand Down Expand Up @@ -374,7 +377,7 @@ export class TxProposal {
// Tests signatures are OK
const tx = ChainService.getBitcoreTx(this);
ChainService.addSignaturesToBitcoreTx(
this.coin,
this.chain,
tx,
this.inputs,
this.inputPaths,
Expand Down
15 changes: 15 additions & 0 deletions packages/bitcore-wallet-service/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2161,6 +2161,20 @@ export class WalletService {
});
}

getTokenContractInfo(opts) {
const bc = this._getBlockchainExplorer('eth', opts.network);
return new Promise((resolve, reject) => {
if (!bc) return reject(new Error('Could not get blockchain explorer instance'));
bc.getTokenContractInfo(opts, (err, contractInfo) => {
if (err) {
this.logw('Error getting contract info', err);
return reject(err);
}
return resolve(contractInfo);
});
});
}

getMultisigTxpsInfo(opts) {
const bc = this._getBlockchainExplorer('eth', opts.network);
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -2311,6 +2325,7 @@ export class WalletService {
walletId: this.walletId,
creatorId: this.copayerId,
coin: opts.coin,
chain: opts.chain ? opts.chain : opts.coin,
network: wallet.network,
outputs: opts.outputs,
message: opts.message,
Expand Down

0 comments on commit dbc724d

Please sign in to comment.