Skip to content

Commit

Permalink
Merge pull request #107 from XRPL-Labs/patch-release-v2.8.2
Browse files Browse the repository at this point in the history
v2.8.2
  • Loading branch information
N3TC4T authored Apr 11, 2024
2 parents 1aef6e2 + e222dca commit e6b13d8
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 50 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ apply plugin: "com.google.firebase.crashlytics"

import com.android.build.OutputFile

def canonicalVersionName = "2.8.1"
def canonicalVersionCode = 30_007
def canonicalVersionName = "2.8.2"
def canonicalVersionCode = 30_008

/**
* This is the configuration block to customize your React Native Android app.
Expand Down
4 changes: 2 additions & 2 deletions ios/Xaman.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@
INFOPLIST_FILE = Xaman/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 2.8.1;
MARKETING_VERSION = 2.8.2;
OTHER_LDFLAGS = (
"$(inherited)",
"-ObjC",
Expand Down Expand Up @@ -1176,7 +1176,7 @@
INFOPLIST_FILE = Xaman/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 2.8.1;
MARKETING_VERSION = 2.8.2;
OTHER_LDFLAGS = (
"$(inherited)",
"-ObjC",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xaman",
"version": "2.8.1",
"version": "2.8.2",
"license": "SEE LICENSE IN <LICENSE>",
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
Expand Down
191 changes: 153 additions & 38 deletions src/common/utils/__tests__/fee.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,160 @@
/* eslint-disable spellcheck/spell-checker */
import { NormalizeFeeDataSet } from '../fee';
import { NormalizeFeeDataSet, PrepareTxForHookFee } from '../fee';

import * as AccountLib from 'xrpl-accountlib';

describe('Utils.Fee', () => {
it('Should return right values', () => {
const baseFees = {
10: ['12', '15', '25'],
12: ['12', '15', '25'],
24: ['24', '30', '45'],
100: ['100', '150', '200'],
500: ['500', '600', '900'],
1200: ['1200', '1500', '2500'],
10000: ['10000', '15000', '20000'],
50000: ['50000', '60000', '75000'],
20000: ['20000', '25000', '35000'],
800000: ['800000', '850000', '850000'],
};

Object.keys(baseFees).forEach((base) => {
expect(
NormalizeFeeDataSet({
drops: { base_fee: Number(base) },
fee_hooks_feeunits: 0,
}),
).toMatchObject({
availableFees: [
{
type: 'LOW',
value: baseFees[base][0],
},
{
type: 'MEDIUM',
value: baseFees[base][1],
},
{
type: 'HIGH',
value: baseFees[base][2],
},
],
feeHooks: 0,
suggested: 'LOW',
describe('NormalizeFeeDataSet', () => {
it('Should return right values', () => {
const baseFees = {
10: ['12', '15', '25'],
12: ['12', '15', '25'],
24: ['24', '30', '45'],
100: ['100', '150', '200'],
500: ['500', '600', '900'],
1200: ['1200', '1500', '2500'],
10000: ['10000', '15000', '20000'],
50000: ['50000', '60000', '75000'],
20000: ['20000', '25000', '35000'],
800000: ['800000', '850000', '850000'],
};

Object.keys(baseFees).forEach((base) => {
expect(
NormalizeFeeDataSet({
drops: { base_fee: base },
fee_hooks_feeunits: '0',
}),
).toMatchObject({
availableFees: [
{
type: 'LOW',
value: baseFees[base][0],
},
{
type: 'MEDIUM',
value: baseFees[base][1],
},
{
type: 'HIGH',
value: baseFees[base][2],
},
],
feeHooks: 0,
suggested: 'LOW',
});
});
});
});

describe('PrepareTxForHookFee', () => {
it('Should throw an error if txJson is not a valid object', () => {
expect(() => {
PrepareTxForHookFee(undefined, {}, 0);
}).toThrowError('PrepareTxForHookFee requires a json transaction to calculate the fee for');

expect(() => {
PrepareTxForHookFee('invalid', {}, 0);
}).toThrowError('PrepareTxForHookFee requires a json transaction to calculate the fee for');
});

it('Should prepare the txJson correctly for signing', () => {
const txJson = {
Fee: '12',
SigningPubKey: 'SOME_PUB_KEY',
};

// mock and spy sign method
const signSpy = jest.spyOn(AccountLib, 'sign');

// call the method
PrepareTxForHookFee(txJson, undefined, 1);

expect(signSpy).toBeCalledWith(
{
Fee: '0',
SigningPubKey: '',
Sequence: 0,
},
expect.any(Object),
undefined,
);

signSpy.mockClear();
});

it('Should include NetworkID if necessary', () => {
const txJson = {};

const signSpy = jest.spyOn(AccountLib, 'sign');
PrepareTxForHookFee(txJson, undefined, 2337);
expect(signSpy).toBeCalledWith(
{
Fee: '0',
SigningPubKey: '',
Sequence: 0,
NetworkID: 2337,
},
expect.any(Object),
undefined,
);
signSpy.mockClear();
});

it('Should not include NetworkID if networkId is less than or equal to 1024', () => {
const txJson = {};

const signSpy = jest.spyOn(AccountLib, 'sign');
PrepareTxForHookFee(txJson, undefined, 1024);
expect(signSpy).toBeCalledWith(
{
Fee: '0',
SigningPubKey: '',
Sequence: 0,
},
expect.any(Object),
undefined,
);
signSpy.mockClear();
});

it('Should set Amount to 0 if TransactionType is Payment and Amount is not set', () => {
const txJson = {
TransactionType: 'Payment',
};

const signSpy = jest.spyOn(AccountLib, 'sign');
PrepareTxForHookFee(txJson, undefined, 0);
expect(signSpy).toBeCalledWith(
{
...txJson,
Amount: '0',
Fee: '0',
SigningPubKey: '',
Sequence: 0,
},
expect.any(Object),
undefined,
);
signSpy.mockClear();
});

it('Should set xrplDefinitions if definitions is an object', () => {
const txJson = {};

const signSpy = jest.spyOn(AccountLib, 'sign');
PrepareTxForHookFee(txJson, AccountLib.binary.DEFAULT_DEFINITIONS, 0);
expect(signSpy).toBeCalledWith(
{
...txJson,
Fee: '0',
SigningPubKey: '',
Sequence: 0,
},
expect.any(Object),
expect.any(Object),
);
signSpy.mockClear();
});
});
});
19 changes: 15 additions & 4 deletions src/common/utils/fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { sign, derive, XrplDefinitions } from 'xrpl-accountlib';
* Prepare transaction for getting hook tx fee
* @param txJson
* @param definitions
* @param networkId
* @returns string
*/
const PrepareTxForHookFee = (txJson: any, definitions: any): string => {
const PrepareTxForHookFee = (txJson: any, definitions: any, networkId: number): string => {
if (!txJson || typeof txJson !== 'object') {
throw new Error('PrepareTxForHookFee requires a json transaction to calculate the fee for');
}

// normalize the transaction
// Shallow copy txJson
// Fee and SigningPubKey should be empty
const transaction = {
...txJson,
Expand All @@ -23,14 +24,24 @@ const PrepareTxForHookFee = (txJson: any, definitions: any): string => {

// check if we need to populate the transaction with dummy details
// set the Sequence if not set
if (!Object.prototype.hasOwnProperty.call(txJson, 'Sequence')) {
if (!Object.prototype.hasOwnProperty.call(transaction, 'Sequence')) {
Object.assign(transaction, {
Sequence: 0,
});
}

// include Network ID if necessary
if (!Object.prototype.hasOwnProperty.call(transaction, 'NetworkID')) {
// legacy networks have ids less than 1024, these networks cannot specify NetworkID in txn
if (networkId > 1024) {
Object.assign(transaction, {
NetworkID: networkId,
});
}
}

// Payment payloads can have no amount set
if (txJson.TransactionType === 'Payment' && !txJson.Amount) {
if (transaction.TransactionType === 'Payment' && !transaction.Amount) {
Object.assign(transaction, {
Amount: '0',
});
Expand Down
6 changes: 3 additions & 3 deletions src/services/NetworkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,16 @@ class NetworkService extends EventEmitter {
try {
const resp = await this.send<FeeRequest, FeeResponse>({
command: 'fee',
tx_blob: PrepareTxForHookFee(txJson, this.network.definitions),
tx_blob: PrepareTxForHookFee(txJson, this.network.definitions, this.network.networkId),
});

if ('error' in resp) {
throw new Error(resp.error);
}

resolve(NormalizeFeeDataSet(resp));
} catch (e) {
this.logger.warn('Unable to calculate available network fees:', e);
} catch (error: any) {
this.logger.warn('Unable to calculate available network fees:', error);
reject(new Error('Unable to calculate available network fees!'));
}
});
Expand Down

0 comments on commit e6b13d8

Please sign in to comment.