import { BigNumber } from 'bignumber.js' ; import BigInt from '../../common/bigInt' ; import { Address } from '../../crypto/address' ; import { ERROR_CODE } from '../../error' ; import { hex2VarBytes , hexstr2str , num2hexstring , str2VarBytes , StringReader } from '../../utils' ; export class Transfers { static deserialize ( sr ) { const t = new Transfers ( ) ; const states = [ ] ; const stateLen = sr . readNextLen ( ) ; for ( let i = 0 ; i < stateLen ; i ++ ) { const state = State . deserialize ( sr ) ; states . push ( state ) ; } t . states = states ; return t ; } states : State [ ] = [ ] ; constructor ( ) { } serialize ( ) { let result = '' ; result += num2hexstring ( this . states . length ) ; for ( let i = 0 ; i < this . states . length ; i ++ ) { result += this . states [ i ] . serialize ( ) ; } return result ; } } export class TokenTransfer { static deserialize ( sr ) { const tf = new TokenTransfer ( ) ; tf . states = [ ] ; const contract = sr . read ( 20 ) ; tf . contract = contract ; const len = sr . readNextLen ( ) ; for ( let i = 0 ; i < len ; i ++ ) { const state = State . deserialize ( sr ) ; tf . states . push ( state ) ; } return tf ; } contract : string ; states : State [ ] ; serialize ( ) { let result = '' ; result += this . contract ; const len = num2hexstring ( this . states . length ) ; result += len ; for ( let i = 0 ; i < this . states . length ; i ++ ) { result += this . states [ i ] . serialize ( ) ; } return result ; } } export class State { static deserialize ( sr ) { const from = new Address ( sr . readNextBytes ( ) ) ; const to = new Address ( sr . readNextBytes ( ) ) ; const value = BigInt . fromHexstr ( sr . readNextBytes ( ) ) . value ; return new State ( from , to , value . toString ( ) ) ; } from : Address ; to : Address ; value : string | number ; constructor ( from , to , value ) { const bi = new BigNumber ( value ) ; if ( ! bi . isInteger ( ) || bi . isNegative ( ) ) { throw ERROR_CODE . INVALID_PARAMS ; } this . from = from ; this . to = to ; this . value = value ; } serialize ( ) { let result = '' ; result += hex2VarBytes ( this . from . serialize ( ) ) ; result += hex2VarBytes ( this . to . serialize ( ) ) ; const bi = new BigInt ( this . value ) . toHexstr ( ) ; result += hex2VarBytes ( bi ) ; return result ; } } export class Contract { static deserialize ( sr ) { const c = new Contract ( ) ; const version = sr . read ( 1 ) ; const address = Address . deserialize ( sr ) ; const method = sr . readNextBytes ( ) ; const args = sr . readNextBytes ( ) ; c . version = version ; c . address = address ; c . method = hexstr2str ( method ) ; c . args = args ; return c ; } version : string ; address : Address ; method : string ; args : string ; constructor ( ) { this . version = '00' ; } serialize ( ) { let result = '' ; result += this . version ; result += this . address . serialize ( ) ; result += str2VarBytes ( this . method ) ; result += hex2VarBytes ( this . args ) ; return result ; } } export class TransferFrom { static deserialize ( sr ) { const sender = new Address ( sr . readNextBytes ( ) ) ; const from = new Address ( sr . readNextBytes ( ) ) ; const to = new Address ( sr . readNextBytes ( ) ) ; const value = BigInt . fromHexstr ( sr . readNextBytes ( ) ) . value ; const tf = new TransferFrom ( sender , from , to , value . toString ( ) ) ; return tf ; } sender : Address ; from : Address ; to : Address ; value : string ; constructor ( sender , from , to , value ) { const bi = new BigNumber ( value ) ; if ( ! bi . isInteger ( ) || ! bi . isNegative ( ) ) { throw ERROR_CODE . INVALID_PARAMS ; } this . sender = sender ; this . from = from ; this . to = to ; this . value = value ; } serialize ( ) { let result = '' ; result += hex2VarBytes ( this . sender . serialize ( ) ) ; result += hex2VarBytes ( this . from . serialize ( ) ) ; result += hex2VarBytes ( this . to . serialize ( ) ) ; const biHex = new BigInt ( this . value ) . toHexstr ( ) ; result += hex2VarBytes ( biHex ) ; return result ; } }