Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit 2260427

Browse files
author
James Calfee
committed
Default "symbol" and "asset" types to eosio.token contract #152
1 parent 44ff378 commit 2260427

File tree

1 file changed

+69
-96
lines changed

1 file changed

+69
-96
lines changed

src/structs.js

Lines changed: 69 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,10 @@ const PublicKeyEcc = (validation) => {
183183
}
184184
}
185185

186-
/** Current action within a transaction. */
187-
let currentAccount
188-
189186
/** @private */
190187
function precisionCache(assetCache, value) {
191188
const symbolInfo = parseExtendedAsset(value)
192-
const contract = symbolInfo.contract || currentAccount
189+
const contract = symbolInfo.contract || 'eosio.token'
193190

194191
let precision
195192

@@ -259,7 +256,7 @@ const Symbol = assetCache => validation => {
259256

260257
appendByteBuffer (b, value) {
261258
const {symbol, precision} = precisionCache(assetCache, value)
262-
assert(precision != null, `Precision unknown for asset: ${symbol}@${currentAccount}`)
259+
assert(precision != null, `Precision unknown for asset: ${symbol}@eosio.token`)
263260
const pad = '\0'.repeat(7 - symbol.length)
264261
b.append(String.fromCharCode(precision) + symbol + pad)
265262
},
@@ -329,20 +326,27 @@ function toAssetString(value, assetCache, format = '') {
329326

330327
const {precision, symbol, amount, contract} = precisionCache(assetCache, value)
331328

332-
if(format === 'plain') {
329+
if(format === 'plain_asset') {
333330
return `${UDecimalPad(amount, precision)} ${symbol}`
334331
}
335332

336-
if(format === 'extended') {
333+
if(format === 'extended_asset') {
337334
const contractSuffix = contract ? `@${contract}` : ''
338335
return `${UDecimalPad(amount, precision)} ${symbol}${contractSuffix}`
339336
}
340337

341-
if(format === 'full') {
338+
if(format === 'full_asset') {
339+
const precisionPrefix = precision != null ? `${precision},` : ''
340+
const full = `${UDecimalPad(amount, precision)} ${precisionPrefix}${symbol}`
341+
// console.log('full_asset', full)
342+
return full
343+
}
344+
345+
if(format === 'full_extended_asset') {
342346
const contractSuffix = contract ? `@${contract}` : ''
343347
const precisionPrefix = precision != null ? `${precision},` : ''
344348
const full = `${UDecimalPad(amount, precision)} ${precisionPrefix}${symbol}${contractSuffix}`
345-
// console.log('full', full)
349+
// console.log('full_extended_asset', full)
346350
return full
347351
}
348352

@@ -362,26 +366,26 @@ const Asset = assetCache => (validation, baseTypes, customTypes) => {
362366
const amount = amountType.fromByteBuffer(b)
363367
const sym = symbolType.fromByteBuffer(b)
364368
const {precision, symbol} = precisionCache(assetCache, sym)
365-
return toAssetString(`${UDecimalUnimply(amount, precision)} ${precision},${symbol}`, assetCache, 'full')
369+
return toAssetString(`${UDecimalUnimply(amount, precision)} ${precision},${symbol}`, assetCache, 'full_asset')
366370
},
367371

368372
appendByteBuffer (b, value) {
369373
assert.equal(typeof value, 'string', `expecting asset string, got ` + (typeof value))
370-
const {amount, precision, symbol, contract} = precisionCache(assetCache, value)
374+
const {amount, precision, symbol} = precisionCache(assetCache, value)
371375
assert(precision != null, `Precision unknown for asset: ${value}`)
372376
amountType.appendByteBuffer(b, UDecimalImply(amount, precision))
373377
symbolType.appendByteBuffer(b, value)
374378
},
375379

376380
fromObject (value) {
377-
return toAssetString(value, assetCache, 'full')
381+
return toAssetString(value, assetCache, 'full_asset')
378382
},
379383

380384
toObject (value) {
381385
if (validation.defaults && value == null) {
382386
return '0.0001 SYS'
383387
}
384-
return toAssetString(value, assetCache, 'plain')
388+
return toAssetString(value, assetCache, 'plain_asset')
385389
}
386390
}
387391
}
@@ -414,15 +418,14 @@ const ExtendedAsset = assetCache => (validation, baseTypes, customTypes) => {
414418
// like: 1.0000 SYS@contract or 1 SYS@contract
415419
assert(/^\d+(\.\d+)* [A-Z]+@[a-z0-5]+(\.[a-z0-5]+)*$/.test(value),
416420
'Invalid extended asset: ' + value)
417-
418-
return toAssetString(value, assetCache, 'full')
421+
return toAssetString(value, assetCache, 'full_extended_asset')
419422
},
420423

421424
toObject (value) {
422425
if (validation.defaults && value == null) {
423426
return '1.0000 [email protected]'
424427
}
425-
return toAssetString(value, assetCache, 'extended')
428+
return toAssetString(value, assetCache, 'extended_asset')
426429
}
427430
}
428431
}
@@ -519,105 +522,75 @@ const wasmCodeOverride = config => ({
519522
*/
520523
const actionDataOverride = (structLookup, forceActionDataHex) => ({
521524
'action.data.fromByteBuffer': ({fields, object, b, config}) => {
522-
currentAccount = object.account
523-
try {
524-
const ser = (object.name || '') == '' ? fields.data : structLookup(object.name, object.account)
525-
if(ser) {
526-
b.readVarint32() // length prefix (usefull if object.name is unknown)
527-
object.data = ser.fromByteBuffer(b, config)
528-
} else {
529-
// console.log(`Unknown Action.name ${object.name}`)
530-
const lenPrefix = b.readVarint32()
531-
const bCopy = b.copy(b.offset, b.offset + lenPrefix)
532-
b.skip(lenPrefix)
533-
object.data = Buffer.from(bCopy.toBinary(), 'binary')
534-
}
535-
} catch(error) {
536-
throw error
537-
} finally {
538-
currentAccount = null
525+
const ser = (object.name || '') == '' ? fields.data : structLookup(object.name, object.account)
526+
if(ser) {
527+
b.readVarint32() // length prefix (usefull if object.name is unknown)
528+
object.data = ser.fromByteBuffer(b, config)
529+
} else {
530+
// console.log(`Unknown Action.name ${object.name}`)
531+
const lenPrefix = b.readVarint32()
532+
const bCopy = b.copy(b.offset, b.offset + lenPrefix)
533+
b.skip(lenPrefix)
534+
object.data = Buffer.from(bCopy.toBinary(), 'binary')
539535
}
540536
},
541537

542538
'action.data.appendByteBuffer': ({fields, object, b}) => {
543-
currentAccount = object.account
544-
try {
545-
const ser = (object.name || '') == '' ? fields.data : structLookup(object.name, object.account)
546-
if(ser) {
547-
const b2 = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
548-
ser.appendByteBuffer(b2, object.data)
549-
b.writeVarint32(b2.offset)
550-
b.append(b2.copy(0, b2.offset), 'binary')
551-
} else {
552-
// console.log(`Unknown Action.name ${object.name}`)
553-
const data = typeof object.data === 'string' ? new Buffer(object.data, 'hex') : object.data
554-
if(!Buffer.isBuffer(data)) {
555-
throw new TypeError(`Unknown struct '${object.name}' for contract '${object.account}', locate this struct or provide serialized action.data`)
556-
}
557-
b.writeVarint32(data.length)
558-
b.append(data.toString('binary'), 'binary')
539+
const ser = (object.name || '') == '' ? fields.data : structLookup(object.name, object.account)
540+
if(ser) {
541+
const b2 = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
542+
ser.appendByteBuffer(b2, object.data)
543+
b.writeVarint32(b2.offset)
544+
b.append(b2.copy(0, b2.offset), 'binary')
545+
} else {
546+
// console.log(`Unknown Action.name ${object.name}`)
547+
const data = typeof object.data === 'string' ? new Buffer(object.data, 'hex') : object.data
548+
if(!Buffer.isBuffer(data)) {
549+
throw new TypeError(`Unknown struct '${object.name}' for contract '${object.account}', locate this struct or provide serialized action.data`)
559550
}
560-
} catch(error) {
561-
throw error
562-
} finally {
563-
currentAccount = null
551+
b.writeVarint32(data.length)
552+
b.append(data.toString('binary'), 'binary')
564553
}
565554
},
566555

567556
'action.data.fromObject': ({fields, object, result}) => {
568557
const {data, name} = object
569-
currentAccount = object.account
570-
571-
try {
572-
const ser = (name || '') == '' ? fields.data : structLookup(name, object.account)
573-
if(ser) {
574-
if(typeof data === 'object') {
575-
result.data = ser.fromObject(data) // resolve shorthand
576-
} else if(typeof data === 'string') {
577-
const buf = new Buffer(data, 'hex')
578-
result.data = Fcbuffer.fromBuffer(ser, buf)
579-
} else {
580-
throw new TypeError('Expecting hex string or object in action.data')
581-
}
558+
const ser = (name || '') == '' ? fields.data : structLookup(name, object.account)
559+
if(ser) {
560+
if(typeof data === 'object') {
561+
result.data = ser.fromObject(data) // resolve shorthand
562+
} else if(typeof data === 'string') {
563+
const buf = new Buffer(data, 'hex')
564+
result.data = Fcbuffer.fromBuffer(ser, buf)
582565
} else {
583-
// console.log(`Unknown Action.name ${object.name}`)
584-
result.data = data
566+
throw new TypeError('Expecting hex string or object in action.data')
585567
}
586-
} catch(error) {
587-
throw error
588-
} finally {
589-
currentAccount = null
568+
} else {
569+
// console.log(`Unknown Action.name ${object.name}`)
570+
result.data = data
590571
}
591572
},
592573

593574
'action.data.toObject': ({fields, object, result, config}) => {
594575
const {data, name} = object || {}
595-
currentAccount = object.account
596-
597-
try {
598-
const ser = (name || '') == '' ? fields.data : structLookup(name, object.account)
599-
if(!ser) {
600-
// Types without an ABI will accept hex
601-
result.data = Buffer.isBuffer(data) ? data.toString('hex') : data
602-
return
603-
}
576+
const ser = (name || '') == '' ? fields.data : structLookup(name, object.account)
577+
if(!ser) {
578+
// Types without an ABI will accept hex
579+
result.data = Buffer.isBuffer(data) ? data.toString('hex') : data
580+
return
581+
}
604582

605-
if(forceActionDataHex) {
606-
const b2 = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
607-
if(data) {
608-
ser.appendByteBuffer(b2, data)
609-
}
610-
result.data = b2.copy(0, b2.offset).toString('hex')
611-
// console.log('result.data', result.data)
612-
return
583+
if(forceActionDataHex) {
584+
const b2 = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
585+
if(data) {
586+
ser.appendByteBuffer(b2, data)
613587
}
614-
615-
// Serializable JSON
616-
result.data = ser.toObject(data, config)
617-
} catch(error) {
618-
throw error
619-
} finally {
620-
currentAccount = null
588+
result.data = b2.copy(0, b2.offset).toString('hex')
589+
// console.log('result.data', result.data)
590+
return
621591
}
592+
593+
// Serializable JSON
594+
result.data = ser.toObject(data, config)
622595
}
623596
})

0 commit comments

Comments
 (0)