11import { Transaction } from '../../src' ;
22import { coins } from '@bitgo/statics' ;
33import assert from 'assert' ;
4- // import should from 'should';
4+ import should from 'should' ;
55import { Utils } from '../../src/lib/utils' ;
66import { InvalidTransactionError } from '@bitgo/sdk-core' ;
7- // import { rawTransaction, accounts, parsedRawTransaction } from '../resources/icp';
7+ import * as testData from '../resources/icp' ;
8+ import { getBuilderFactory } from './getBuilderFactory' ;
89import sinon from 'sinon' ;
910
1011describe ( 'ICP Transaction' , ( ) => {
1112 let tx : Transaction ;
1213 let utils : Utils ;
13- // let localRawTransaction : any;
14+ let serializedTxHex : any ;
1415 const config = coins . get ( 'ticp' ) ;
1516
1617 beforeEach ( ( ) => {
1718 utils = new Utils ( ) ;
1819 tx = new Transaction ( config , utils ) ;
19- // localRawTransaction = JSON.stringify(rawTransaction);
20+ const serializedTxFormat = {
21+ serializedTxHex : testData . PayloadsData ,
22+ publicKey : testData . Accounts . account1 . publicKey ,
23+ } ;
24+ serializedTxHex = Buffer . from ( JSON . stringify ( serializedTxFormat ) , 'utf-8' ) . toString ( 'hex' ) ;
2025 sinon . stub ( utils , 'validateExpireTime' ) . returns ( true ) ;
2126 } ) ;
2227
@@ -35,42 +40,61 @@ describe('ICP Transaction', () => {
3540 } ) ;
3641 } ) ;
3742
38- // describe('from raw transaction', () => {
39- // it('build a json transaction from raw hex', async () => {
40- // await tx.fromRawTransaction(localRawTransaction);
41- // const json = tx.toJson();
42- // should.equal(json.memo, parsedRawTransaction.metadata.memo);
43- // should.equal(json.feeAmount, parsedRawTransaction.operations[2].amount.value);
44- // should.equal(json.sender, parsedRawTransaction.operations[0].account.address);
45- // should.equal(json.recipient, parsedRawTransaction.operations[1].account.address);
46- // should.equal(json.type, BitGoTransactionType.Send);
47- // should.equal(json.senderPublicKey, accounts.account1.publicKey);
48- // });
49- // });
43+ describe ( 'build a txn from init() method' , ( ) => {
44+ it ( 'start and build a txn with builder init method' , async ( ) => {
45+ const txn = new Transaction ( config , utils ) ;
46+ txn . icpTransactionData = testData . IcpTransactionData ;
47+ const factory = getBuilderFactory ( 'ticp' ) ;
48+ const txBuilder = factory . getTransferBuilder ( ) ;
49+ txBuilder . initBuilder ( txn ) ;
50+ await txBuilder . build ( ) ;
5051
51- // describe('Explain', () => {
52- // it('explain transaction', async () => {
53- // await tx.fromRawTransaction(localRawTransaction);
54- // const explain = tx.explainTransaction();
52+ const icpTransaction = txBuilder . transaction . icpTransaction ;
53+ const payloadsData = txBuilder . transaction . payloadsData ;
54+ should . equal ( icpTransaction . metadata . memo , testData . IcpTransactionData . memo ) ;
55+ should . equal ( icpTransaction . operations [ 0 ] . account . address , testData . IcpTransactionData . senderAddress ) ;
56+ should . equal ( icpTransaction . operations [ 1 ] . account . address , testData . IcpTransactionData . receiverAddress ) ;
57+ should . equal ( icpTransaction . operations [ 1 ] . amount . value , testData . IcpTransactionData . amount ) ;
58+ should . equal ( icpTransaction . operations [ 2 ] . amount . value , testData . IcpTransactionData . fee ) ;
59+ should . equal ( icpTransaction . public_keys [ 0 ] . hex_bytes , testData . IcpTransactionData . senderPublicKeyHex ) ;
60+ payloadsData . payloads . should . be . an . Array ( ) ;
61+ payloadsData . payloads . length . should . equal ( 1 ) ;
62+ } ) ;
63+ } ) ;
64+
65+ describe ( 'from raw transaction' , ( ) => {
66+ it ( 'build a json transaction from raw hex' , async ( ) => {
67+ await tx . fromRawTransaction ( serializedTxHex ) ;
68+ const json = tx . toJson ( ) ;
69+ should . equal ( json . memo , testData . ParsedRawTransaction . metadata . memo ) ;
70+ should . equal ( json . feeAmount , testData . ParsedRawTransaction . operations [ 2 ] . amount . value ) ;
71+ should . equal ( json . sender , testData . ParsedRawTransaction . operations [ 0 ] . account . address ) ;
72+ should . equal ( json . recipient , testData . ParsedRawTransaction . operations [ 1 ] . account . address ) ;
73+ should . equal ( json . senderPublicKey , testData . Accounts . account1 . publicKey ) ;
74+ should . equal ( json . id , testData . OnChainTransactionHash ) ;
75+ } ) ;
76+ } ) ;
77+
78+ describe ( 'Explain' , ( ) => {
79+ it ( 'explain transaction' , async ( ) => {
80+ await tx . fromRawTransaction ( serializedTxHex ) ;
81+ const explain = tx . explainTransaction ( ) ;
5582
56- // explain.outputAmount.should.equal('1000000');
57- // explain.outputs[0].amount.should.equal('1000000');
58- // explain.outputs[0].address.should.equal(accounts.account2.address);
59- // explain.fee.fee.should.equal('-10000');
60- // explain.changeAmount.should.equal('0');
61- // if (explain.displayOrder !== undefined) {
62- // explain.displayOrder.should.deepEqual([
63- // 'id',
64- // 'outputAmount',
65- // 'changeAmount',
66- // 'outputs',
67- // 'changeOutputs',
68- // 'fee',
69- // ]);
70- // }
71- // if (explain.type !== undefined) {
72- // explain.type.should.equal(BitGoTransactionType.Send);
73- // }
74- // });
75- // });
83+ explain . outputAmount . should . equal ( '10' ) ;
84+ explain . outputs [ 0 ] . amount . should . equal ( '10' ) ;
85+ explain . outputs [ 0 ] . address . should . equal ( testData . Accounts . account2 . address ) ;
86+ explain . fee . fee . should . equal ( '-10000' ) ;
87+ explain . changeAmount . should . equal ( '0' ) ;
88+ if ( explain . displayOrder !== undefined ) {
89+ explain . displayOrder . should . deepEqual ( [
90+ 'id' ,
91+ 'outputAmount' ,
92+ 'changeAmount' ,
93+ 'outputs' ,
94+ 'changeOutputs' ,
95+ 'fee' ,
96+ ] ) ;
97+ }
98+ } ) ;
99+ } ) ;
76100} ) ;
0 commit comments