Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class Transaction extends BaseTransaction {
}
case TransactionType.Send: {
const txData = this.toJson();
outputs.push({ address: txData.sender, amount: txData.amount });
outputs.push({ address: txData.receiver, amount: txData.amount });
outputAmount = txData.amount;
break;
}
Expand Down
14 changes: 13 additions & 1 deletion modules/sdk-coin-canton/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BigNumber from 'bignumber.js';
import crypto from 'crypto';

import { BaseUtils, isValidEd25519PublicKey } from '@bitgo/sdk-core';
Expand Down Expand Up @@ -145,10 +146,11 @@ export class Utils implements BaseUtils {
if (!amount) missingFields.push('amount');
throw new Error(`invalid transaction data: missing ${missingFields.join(', ')}`);
}
const convertedAmount = this.convertAmountToLowestUnit(new BigNumber(amount));
return {
sender,
receiver,
amount,
amount: convertedAmount,
};
}

Expand Down Expand Up @@ -296,6 +298,16 @@ export class Utils implements BaseUtils {
buf.writeInt32BE(value, 0);
return buf;
}

/**
* Convert to canton raw units
* @param {BigNumber} value
* @returns {String} the converted raw canton units
* @private
*/
private convertAmountToLowestUnit(value: BigNumber): string {
return value.multipliedBy(new BigNumber(10).pow(10)).toFixed(0);
}
}

const utils = new Utils();
Expand Down
39 changes: 36 additions & 3 deletions modules/sdk-coin-canton/test/integration/canton.integration.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,59 @@
import assert from 'assert';

import { BitGoAPI } from '@bitgo/sdk-api';
import { TransactionType } from '@bitgo/sdk-core';
import { ITransactionRecipient, TransactionType, Wallet } from '@bitgo/sdk-core';
import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test';

import { getCantonBuilderFactory } from '../helper';
import {
CANTON_RECEIVE_ADDRESS,
GenerateTopologyResponse,
TransferAcceptRawTransaction,
TransferRawTxn,
TransferRejectRawTransaction,
TxParams,
WalletInitRawTransaction,
} from '../resources';
import { Tcanton } from '../../src';

describe('Canton integration tests', function () {
let bitgo: TestBitGoAPI;
let basecoin: Tcanton;
let newTxPrebuild: () => { txHex: string; txInfo: Record<string, unknown> };
let newTxParams: () => { recipients: ITransactionRecipient[] };
let wallet: Wallet;
const txPrebuild = {
txHex: TransferRawTxn,
txInfo: {},
};
const txParams = {
recipients: [
{
address: TxParams.RECIPIENT_ADDRESS,
amount: TxParams.AMOUNT,
},
],
};
before(() => {
bitgo = TestBitGo.decorate(BitGoAPI, { env: 'mock' });
bitgo.safeRegister('tcanton', Tcanton.createInstance);
basecoin = bitgo.coin('tcanton') as Tcanton;
newTxPrebuild = () => {
return structuredClone(txPrebuild);
};
newTxParams = () => {
return structuredClone(txParams);
};
wallet = new Wallet(bitgo, basecoin, {});
});

describe('Verify Transaction', function () {
it('should verify transfer transaction', async function () {
const txPrebuild = newTxPrebuild();
const txParams = newTxParams();
const isTxnVerifies = await basecoin.verifyTransaction({ txPrebuild: txPrebuild, txParams: txParams, wallet });
isTxnVerifies.should.equal(true);
});
});

describe('Explain raw transaction', function () {
Expand All @@ -41,7 +74,7 @@ describe('Canton integration tests', function () {
assert(explainTxData);
assert(explainTxData.id);
assert.equal(explainTxData.type, TransactionType.TransferAccept);
assert.equal(explainTxData.inputAmount, '5.0000000000');
assert.equal(explainTxData.inputAmount, '50000000000');
});

it('should explain raw transfer rejection transaction', function () {
Expand All @@ -50,7 +83,7 @@ describe('Canton integration tests', function () {
const explainTxData = txn.explainTransaction();
assert(explainTxData);
assert.equal(explainTxData.type, TransactionType.TransferReject);
assert.equal(explainTxData.inputAmount, '5.0000000000');
assert.equal(explainTxData.inputAmount, '50000000000');
});
});

Expand Down
8 changes: 8 additions & 0 deletions modules/sdk-coin-canton/test/resources.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions modules/sdk-coin-canton/test/unit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Canton Util', function () {
should.exist(parsedData);
assert.equal(parsedData.sender, 'abc-1::12200c1ee226fbdf9fba3461c2c0c73331b69d3c6fd8cfce28cdf864141141cc656d');
assert.equal(parsedData.receiver, 'abc-2::12207e96ada18a845adf4dc01410265633d5266dca9bb280c98e35c3692db87d3e35');
assert.equal(parsedData.amount, '20.0000000000');
assert.equal(parsedData.amount, '200000000000');
});

it('should parse the acceptance prepared transaction', () => {
Expand All @@ -29,7 +29,7 @@ describe('Canton Util', function () {
parsedData.receiver,
'ravi-demo-party-txn-01-tapper::1220ea7ab5a723f8a6b2078e617e6c58cb7e78e49947ddc239e1a941aa56e6ba08b4'
);
assert.equal(parsedData.amount, '5.0000000000');
assert.equal(parsedData.amount, '50000000000');
});

it('should parse the one-step preapproval prepared transaction', () => {
Expand Down