import { BigNumber } from 'bignumber.js' ; import { createCodeParamsScript } from '../../transaction/scriptBuilder' ; import { Transaction } from '../../transaction/transaction' ; import { str2hexstr } from '../../utils' ; import { Address } from './../../crypto/address' ; import { makeInvokeTransaction } from './../../transaction/transactionBuilder' ; import { num2hexstring } from './../../utils' ; import { Parameter , ParameterType } from './../abi/parameter' ; export class Oep8State { public from : string ; public to : string ; public tokenId : string ; public value : BigNumber ; public constructor ( from , to , tokenId , value ) { this . from = from . serialize ( ) ; this . to = to . serialize ( ) ; this . tokenId = num2hexstring ( tokenId ) ; this . value = new BigNumber ( value ) ; } } export class TransferFrom { spender : string ; from : string ; to : string ; tokenId : string ; value : BigNumber ; constructor ( spender , from , to , tokenId , value ) { this . spender = spender . serialize ( ) ; this . from = from . serialize ( ) ; this . to = to . serialize ( ) ; this . tokenId = num2hexstring ( tokenId ) ; this . value = new BigNumber ( value ) ; } } const FunctionNames = { Name : 'name' , Symbol : 'symbol' , TotalSupply : 'totalSupply' , BalanceOf : 'balanceOf' , Transfer : 'transfer' , TransferMulti : 'transferMulti' , Approve : 'approve' , ApproveMulti : 'approveMulti' , Allowance : 'allowance' , TransferFrom : 'transferFrom' , TransferFromMulti : 'transferFromMulti' , Compound : 'compound' , Concatkey : 'concatkey' , Init : 'init' , CreateMultiKindsPumpkin : 'createMultiKindsPumpkin' , CheckTokenPrefix : 'checkTokenPrefix' , BalancesOf : 'balancesOf' , TotalBalanceOf : 'totalBalanceOf' , CheckTokenId : 'checkTokenId' } ; export class Oep8TxBuilder { contractAddr : Address ; constructor ( contractAddr ) { this . contractAddr = contractAddr ; } makeInitTx ( gasPrice , gasLimit , payer ) { const func = FunctionNames . Init ; return makeInvokeTransaction ( func , [ ] , this . contractAddr , gasPrice , gasLimit , payer ) ; } makeTransferTx ( sendAddr , recvAddr , tokenId , amount , gasPrice , gasLimit , payer ) { const func = FunctionNames . Transfer ; const params = [ new Parameter ( 'sender' , ParameterType . ByteArray , sendAddr . serialize ( ) ) , new Parameter ( 'recv' , ParameterType . ByteArray , recvAddr . serialize ( ) ) , new Parameter ( 'tokenId' , ParameterType . ByteArray , tokenId ) , new Parameter ( 'amount' , ParameterType . Long , amount ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr , gasPrice , gasLimit , 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 . tokenId , state . value ] ) ; } list . push ( temp ) ; const params = createCodeParamsScript ( list ) ; return makeInvokeTransaction ( '' , params , this . contractAddr , gasPrice , gasLimit , payer ) ; } makeApproveTx ( owner , spender , tokenId , amount , gasPrice , gasLimit , payer ) { const func = FunctionNames . Approve ; const params = [ new Parameter ( 'owner' , ParameterType . ByteArray , owner . serialize ( ) ) , new Parameter ( 'spender' , ParameterType . ByteArray , spender . serialize ( ) ) , new Parameter ( 'tokenId' , ParameterType . ByteArray , num2hexstring ( tokenId ) ) , new Parameter ( 'amount' , ParameterType . Long , amount ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr , gasPrice , gasLimit , payer ) ; } makeApproveMulti ( states , gasPrice , gasLimit , payer ) { const func = FunctionNames . ApproveMulti ; const list = [ ] ; list . push ( str2hexstr ( func ) ) ; const temp = [ ] ; for ( const state of states ) { temp . push ( [ state . from , state . to , state . tokenId , state . value ] ) ; } list . push ( temp ) ; const params = createCodeParamsScript ( list ) ; return makeInvokeTransaction ( '' , params , this . contractAddr , gasPrice , gasLimit , payer ) ; } makeTransferFromMulti ( states , gasPrice , gasLimit , payer ) { const func = FunctionNames . TransferFromMulti ; const list = [ ] ; list . push ( str2hexstr ( func ) ) ; const temp = [ ] ; for ( const state of states ) { temp . push ( [ state . spender , state . from , state . to , state . tokenId , state . value ] ) ; } list . push ( temp ) ; const params = createCodeParamsScript ( list ) ; return makeInvokeTransaction ( '' , params , this . contractAddr , gasPrice , gasLimit , payer ) ; } makeTransferFromTx ( sender , from , to , tokenId , amount , gasPrice , gasLimit , payer ) { const func = FunctionNames . TransferFrom ; const params = [ new Parameter ( 'sender' , ParameterType . ByteArray , sender . serialize ( ) ) , new Parameter ( 'from' , ParameterType . ByteArray , from . serialize ( ) ) , new Parameter ( 'to' , ParameterType . ByteArray , to . serialize ( ) ) , new Parameter ( 'tokenId' , ParameterType . ByteArray , num2hexstring ( tokenId ) ) , new Parameter ( 'amount' , ParameterType . Long , amount ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr , gasPrice , gasLimit , payer ) ; } param account param compoundNum param gasPrice param gasLimit param payer makeCompoundTx ( account , compoundNum , gasPrice , gasLimit , payer ) { const func = FunctionNames . Compound ; const params = [ new Parameter ( 'account' , ParameterType . ByteArray , account . serialize ( ) ) , new Parameter ( 'compoundNum' , ParameterType . Integer , compoundNum ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr , gasPrice , gasLimit , payer ) ; } makeQueryAllowanceTx ( owner , spender , tokenId ) { const func = FunctionNames . Allowance ; const params = [ new Parameter ( 'owner' , ParameterType . ByteArray , owner . serialize ( ) ) , new Parameter ( 'spender' , ParameterType . ByteArray , spender . serialize ( ) ) , new Parameter ( 'tokenId' , ParameterType . ByteArray , num2hexstring ( tokenId ) ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr ) ; } makeQueryBalanceOfTx ( addr , tokenId ) { const func = FunctionNames . BalanceOf ; const params = [ new Parameter ( 'addr' , ParameterType . ByteArray , addr . serialize ( ) ) , new Parameter ( 'tokenId' , ParameterType . ByteArray , num2hexstring ( tokenId ) ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr ) ; } makeQueryTotalSupplyTx ( tokenId ) { const func = FunctionNames . TotalSupply ; const params = [ new Parameter ( 'tokenId' , ParameterType . ByteArray , num2hexstring ( tokenId ) ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr ) ; } makeQueryNameTx ( tokenId ) { const func = FunctionNames . Name ; const params = [ new Parameter ( 'tokenId' , ParameterType . ByteArray , num2hexstring ( tokenId ) ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr ) ; } makeQueryDecimalsTx ( ) { const func = FunctionNames . Symbol ; return makeInvokeTransaction ( func , [ ] , this . contractAddr ) ; } makeQuerySymbolTx ( tokenId ) { const func = FunctionNames . Symbol ; const params = [ new Parameter ( 'tokenId' , ParameterType . ByteArray , num2hexstring ( tokenId ) ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr ) ; } makeQueryBalancesTx ( account ) { const func = FunctionNames . BalancesOf ; const params = [ new Parameter ( 'account' , ParameterType . ByteArray , account . serialize ( ) ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr ) ; } makeQueryTotalBalanceTx ( account ) { const func = FunctionNames . TotalBalanceOf ; const params = [ new Parameter ( 'account' , ParameterType . ByteArray , account . serialize ( ) ) ] ; return makeInvokeTransaction ( func , params , this . contractAddr ) ; } }