import BigNumber from 'bignumber.js' ; import { createCodeParamsScript } from '../../transaction/scriptBuilder' ; import { Transaction } from '../../transaction/transaction' ; import { bigIntToBytes , str2hexstr } from '../../utils' ; import { Parameter , ParameterType } from '../abi/parameter' ; import { Address } from './../../crypto/address' ; import { makeInvokeTransaction } from './../../transaction/transactionBuilder' ; const functionNames = { Init : 'init' , Transfer : 'transfer' , TransferMulti : 'transferMulti' , Approve : 'approve' , TransferFromm : 'transferFrom' , Allowance : 'allowance' , BalanceOf : 'balanceOf' , TotalSupply : 'totalSupply' , Symbol : 'symbol' , Decimals : 'decimals' , Name : 'name' } ; export const formatBigNumParameter = ( amount ) : Parameter => { const valHex = bigIntToBytes ( amount ) ; const p = new Parameter ( 'value' , ParameterType . ByteArray , valHex ) ; return p ; } ; export class Oep4State { from : string ; to : string ; amount : string ; constructor ( from , to , amount ) { this . from = from . serialize ( ) ; this . to = to . serialize ( ) ; this . amount = formatBigNumParameter ( amount ) . value ; } } /**
 * Transaction builder for oep-4 contracts
 */ export class Oep4TxBuilder { contractAddr : Address ; constructor ( contractAddr ) { this . contractAddr = contractAddr ; } param gasPrice param gasLimit param payer init ( gasPrice , gasLimit , payer ? ) { const funcName = functionNames . Init ; return makeInvokeTransaction ( funcName , [ ] , this . contractAddr , gasPrice , gasLimit , payer ) ; } param from param to param amount param gasPrice param gasLimit param payer makeTransferTx ( from , to , amount , gasPrice , gasLimit , payer ) { const funcName = functionNames . Transfer ; const p1 = new Parameter ( 'from' , ParameterType . ByteArray , from . serialize ( ) ) ; const p2 = new Parameter ( 'to' , ParameterType . ByteArray , to . serialize ( ) ) ; const p3 = formatBigNumParameter ( amount ) ; return makeInvokeTransaction ( funcName , [ p1 , p2 , p3 ] , this . contractAddr , gasPrice , gasLimit , payer ) ; } param states param gasPrice param gasLimit param payer makeTransferMultiTx ( states , gasPrice , gasLimit , payer ) { const list = [ ] ; list . push ( str2hexstr ( functionNames . TransferMulti ) ) ; const temp = [ ] ; for ( const state of states ) { temp . push ( [ state . from , state . to , state . amount ] ) ; } list . push ( temp ) ; const params = createCodeParamsScript ( list ) ; return makeInvokeTransaction ( '' , params , this . contractAddr , gasPrice , gasLimit , payer ) ; } param owner param spender param amount param gasPrice param gasLimit param payer makeApproveTx ( owner , spender , amount , gasPrice , gasLimit , payer ) { const funcName = functionNames . Approve ; const params = [ new Parameter ( 'owner' , ParameterType . ByteArray , owner . serialize ( ) ) , new Parameter ( 'spender' , ParameterType . ByteArray , spender . serialize ( ) ) , formatBigNumParameter ( amount ) ] ; return makeInvokeTransaction ( funcName , params , this . contractAddr , gasPrice , gasLimit , payer ) ; } makeTransferFromTx ( sender , from , to , amount , gasPrice , gasLimit , payer ) { const funcName = functionNames . TransferFromm ; const params = [ new Parameter ( 'owner' , ParameterType . ByteArray , sender . serialize ( ) ) , new Parameter ( 'from' , ParameterType . ByteArray , from . serialize ( ) ) , new Parameter ( 'to' , ParameterType . ByteArray , to . serialize ( ) ) , formatBigNumParameter ( amount ) ] ; return makeInvokeTransaction ( funcName , params , this . contractAddr , gasPrice , gasLimit , payer ) ; } makeQueryAllowanceTx ( owner , spender ) { const funcName = functionNames . Allowance ; const params = [ new Parameter ( 'owner' , ParameterType . ByteArray , owner . serialize ( ) ) , new Parameter ( 'spender' , ParameterType . ByteArray , spender . serialize ( ) ) ] ; return makeInvokeTransaction ( funcName , params , this . contractAddr ) ; } param address queryBalanceOf ( address ) { const funcName = functionNames . BalanceOf ; const p1 = new Parameter ( 'from' , ParameterType . ByteArray , address . serialize ( ) ) ; return makeInvokeTransaction ( funcName , [ p1 ] , this . contractAddr ) ; } /**
     * Query the total supply of oep-4 contract
     */ queryTotalSupply ( ) { const funcName = functionNames . TotalSupply ; return makeInvokeTransaction ( funcName , [ ] , this . contractAddr ) ; } /**
     * Query the total supply of oep-4 contract
     */ queryDecimals ( ) { const funcName = functionNames . Decimals ; return makeInvokeTransaction ( funcName , [ ] , this . contractAddr ) ; } /**
     * Query the total supply of oep-4 contract
     */ querySymbol ( ) { const funcName = functionNames . Symbol ; return makeInvokeTransaction ( funcName , [ ] , this . contractAddr ) ; } /**
     * Query the total supply of oep-4 contract
     */ queryName ( ) { const funcName = functionNames . Name ; return makeInvokeTransaction ( funcName , [ ] , this . contractAddr ) ; } }