Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unlock transaction transformers - Closes #3492, #3493, #3476 #3494

Merged
merged 8 commits into from
Apr 21, 2021
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
40 changes: 25 additions & 15 deletions src/utils/api/account/lsk.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const getAccountParams = (params) => {
passphrase,
publicKey,
} = params;

// Pick username, cause the address is not obtainable from the username
if (username) return { username };
// If you have the address, you don't need anything else
Expand Down Expand Up @@ -79,26 +80,35 @@ export const getAccount = async ({
params: normParams,
});

return response.data[0];
if (response.data[0]) {
const account = { ...response.data[0] };
const isAccountUninitialized = !account.summary.publicKey;
if (isAccountUninitialized && (params.publicKey || params.passphrase)) {
const publicKey = params.publicKey ?? extractPublicKey(params.passphrase);
UsamaHameed marked this conversation as resolved.
Show resolved Hide resolved
account.summary.publicKey = publicKey;
}

return account;
}
} catch (e) {
// eslint-disable-next-line no-console
console.log('Lisk account not found.');
let publicKey = params.publicKey;
if (!publicKey && params.passphrase) {
publicKey = extractPublicKey(params.passphrase);
}

const account = {
summary: {
publicKey,
balance: 0,
address: normParams.address,
token: tokenMap.LSK.key,
},
};

return account;
if (params.publicKey || params.passphrase) {
const publicKey = params.publicKey ?? extractPublicKey(params.passphrase);
const account = {
summary: {
publicKey,
balance: 0,
address: normParams.address,
token: tokenMap.LSK.key,
},
};
return account;
}
}

throw Error('Error retrieving account');
};

const accountFilters = {
Expand Down
60 changes: 57 additions & 3 deletions src/utils/api/account/lsk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,17 @@ describe('API: LSK Account', () => {
});
});

it('should call http without parameters', async () => {
it('should call http without base url if not passed', async () => {
http.mockImplementation(() => Promise.resolve({ data: [{}] }));
// Checks with no baseUrl
await getAccount({
network,
params: { },
params: { passphrase },
});

expect(http).toHaveBeenCalledWith({
network,
params: { },
params: { address },
baseUrl: undefined,
path,
});
Expand Down Expand Up @@ -209,5 +209,59 @@ describe('API: LSK Account', () => {
},
});
});

it('should use the public key from params if the account is uninitialized', async () => {
http.mockImplementation(() => Promise.resolve({
data: [{
summary: {
publicKey: '', address, balance: 0, token: 'LSK',
},
}],
}));
// Checks the baseUrl too
const result = await getAccount({
network,
params: {
publicKey,
},
baseUrl,
});

expect(result).toEqual({
summary: {
address,
balance: 0,
token: 'LSK',
publicKey,
},
});
});

it('should use extract the public key from params.passphrase if the account is uninitialized', async () => {
http.mockImplementation(() => Promise.resolve({
data: [{
summary: {
publicKey: '', address, balance: 0, token: 'LSK',
},
}],
}));
// Checks the baseUrl too
const result = await getAccount({
network,
params: {
passphrase,
},
baseUrl,
});

expect(result).toEqual({
summary: {
address,
balance: 0,
token: 'LSK',
publicKey,
},
});
});
});
});
16 changes: 10 additions & 6 deletions src/utils/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ const transformTransaction = (transaction) => {

case unlockToken: {
transformedTransaction.asset = {
// unlockObjects: transaction.unlockObjects.map(unlockingObject => ({
// delegateAddress: unlockingObject.delegateAddress,
// amount: unlockingObject.amount,
// unvoteHeight: unlockingObject.height.start
// })),
unlockObjects: transaction.asset.unlockObjects.map(unlockingObject => ({
delegateAddress: getBase32AddressFromAddress(unlockingObject.delegateAddress),
amount: Number(unlockingObject.amount),
unvoteHeight: unlockingObject.height.start,
})),
};
break;
}
Expand Down Expand Up @@ -150,7 +150,11 @@ const createTransactionObject = (tx, moduleAssetId) => {

case unlockToken: {
transaction.asset = {
unlockObjects: tx.unlockObjects,
unlockObjects: tx.unlockingObjects.map(unlockingObject => ({
amount: BigInt(unlockingObject.amount),
delegateAddress: getAddressFromBase32Address(unlockingObject.delegateAddress),
unvoteHeight: unlockingObject.unvoteHeight,
})),
};
break;
}
Expand Down