Skip to content

Commit 75927f0

Browse files
committed
fix(sdk-coin-sol): set fee for unsigned sweep
TICKET: WIN-4768
1 parent b266562 commit 75927f0

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

modules/sdk-coin-sol/src/sol.ts

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,6 @@ export class Sol extends BaseCoin {
781781
let totalFee = new BigNumber(0);
782782
let totalFeeForTokenRecovery = new BigNumber(0);
783783

784-
if (params.durableNonce) {
785-
const durableNonceInfo = await this.getAccountInfo(params.durableNonce.publicKey);
786-
blockhash = durableNonceInfo.blockhash;
787-
authority = durableNonceInfo.authority;
788-
}
789-
790784
// check for possible token recovery, recover the token provide by user
791785
if (params.tokenContractAddress) {
792786
const tokenAccounts = await this.getTokenAccountsByOwner(bs58EncodedPublicKey);
@@ -869,6 +863,10 @@ export class Sol extends BaseCoin {
869863
}
870864

871865
if (params.durableNonce) {
866+
const durableNonceInfo = await this.getAccountInfo(params.durableNonce.publicKey);
867+
blockhash = durableNonceInfo.blockhash;
868+
authority = durableNonceInfo.authority;
869+
872870
txBuilder.nonce(blockhash, {
873871
walletNonceAddress: params.durableNonce.publicKey,
874872
authWalletAddress: authority,
@@ -879,13 +877,43 @@ export class Sol extends BaseCoin {
879877
const unsignedTransactionWithoutFee = (await txBuilder.build()) as Transaction;
880878
const serializedMessage = unsignedTransactionWithoutFee.solTransaction.serializeMessage().toString('base64');
881879

882-
const feePerSignature = await this.getFeeForMessage(serializedMessage);
883-
const baseFee = params.durableNonce ? feePerSignature * 2 : feePerSignature;
880+
const baseFee = await this.getFeeForMessage(serializedMessage);
881+
const feePerSignature = params.durableNonce ? baseFee / 2 : baseFee;
884882
totalFee = totalFee.plus(new BigNumber(baseFee));
885883
totalFeeForTokenRecovery = totalFeeForTokenRecovery.plus(new BigNumber(baseFee));
886884
if (totalFee.gt(balance)) {
887885
throw Error('Did not find address with funds to recover');
888886
}
887+
888+
if (params.tokenContractAddress) {
889+
// Check if there is sufficient native solana to recover tokens
890+
if (new BigNumber(balance).lt(totalFeeForTokenRecovery)) {
891+
throw Error(
892+
'Not enough funds to pay for recover tokens fees, have: ' +
893+
balance +
894+
' need: ' +
895+
totalFeeForTokenRecovery.toString()
896+
);
897+
}
898+
txBuilder.fee({ amount: feePerSignature });
899+
} else {
900+
const netAmount = new BigNumber(balance).minus(totalFee);
901+
txBuilder = factory
902+
.getTransferBuilder()
903+
.nonce(blockhash)
904+
.sender(bs58EncodedPublicKey)
905+
.send({ address: params.recoveryDestination, amount: netAmount.toString() })
906+
.feePayer(bs58EncodedPublicKey)
907+
.fee({ amount: feePerSignature });
908+
909+
if (params.durableNonce) {
910+
txBuilder.nonce(blockhash, {
911+
walletNonceAddress: params.durableNonce.publicKey,
912+
authWalletAddress: authority,
913+
});
914+
}
915+
}
916+
889917
if (!isUnsignedSweep) {
890918
// Sign the txn
891919
if (!params.userKey) {
@@ -900,25 +928,6 @@ export class Sol extends BaseCoin {
900928
throw new Error('missing wallet passphrase');
901929
}
902930

903-
if (params.tokenContractAddress) {
904-
totalFeeForTokenRecovery = totalFeeForTokenRecovery.plus(new BigNumber(baseFee));
905-
// Check if there is sufficient native solana to recover tokens
906-
if (new BigNumber(balance).lt(totalFeeForTokenRecovery)) {
907-
throw Error(
908-
'Not enough funds to pay for recover tokens fees, have: ' +
909-
balance +
910-
' need: ' +
911-
totalFeeForTokenRecovery.toString()
912-
);
913-
}
914-
txBuilder.fee({ amount: feePerSignature });
915-
} else {
916-
totalFee = new BigNumber(baseFee);
917-
const netAmount = new BigNumber(balance).minus(totalFee);
918-
txBuilder
919-
.send({ address: params.recoveryDestination, amount: netAmount.toString() })
920-
.fee({ amount: feePerSignature });
921-
}
922931
// build the transaction with fee
923932
const unsignedTransaction = (await txBuilder.build()) as Transaction;
924933

modules/sdk-coin-sol/test/unit/sol.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,9 +1431,10 @@ describe('SOL:', function () {
14311431
const sandBox = sinon.createSandbox();
14321432
const coin = coins.get('tsol');
14331433
const usdtMintAddress = '9cgpBeNZ2HnLda7NWaaU1i3NyTstk2c4zCMUcoAGsi9C';
1434+
let callBack;
14341435

14351436
beforeEach(() => {
1436-
const callBack = sandBox.stub(Sol.prototype, 'getDataFromNode' as keyof Sol);
1437+
callBack = sandBox.stub(Sol.prototype, 'getDataFromNode' as keyof Sol);
14371438

14381439
callBack
14391440
.withArgs({
@@ -1963,6 +1964,24 @@ describe('SOL:', function () {
19631964
});
19641965

19651966
it('should recover sol tokens to recovery destination with existing token accounts for unsigned sweep recoveries', async function () {
1967+
const feeResponse = testData.SolResponses.getFeesForMessageResponse;
1968+
feeResponse.body.result.value = 10000;
1969+
callBack
1970+
.withArgs({
1971+
payload: {
1972+
id: '1',
1973+
jsonrpc: '2.0',
1974+
method: 'getFeeForMessage',
1975+
params: [
1976+
sinon.match.string,
1977+
{
1978+
commitment: 'finalized',
1979+
},
1980+
],
1981+
},
1982+
})
1983+
.resolves(feeResponse);
1984+
19661985
const tokenTxn = (await basecoin.recover({
19671986
bitgoKey: testData.wrwUser.bitgoKey,
19681987
recoveryDestination: testData.keys.destinationPubKey2,

0 commit comments

Comments
 (0)