Skip to content

Commit b994804

Browse files
Merge pull request #5763 from BitGo/WIN-4768
fix(sdk-coin-sol): set fee for unsigned sweep
2 parents 96c2adf + 75927f0 commit b994804

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
@@ -788,12 +788,6 @@ export class Sol extends BaseCoin {
788788
let totalFee = new BigNumber(0);
789789
let totalFeeForTokenRecovery = new BigNumber(0);
790790

791-
if (params.durableNonce) {
792-
const durableNonceInfo = await this.getAccountInfo(params.durableNonce.publicKey);
793-
blockhash = durableNonceInfo.blockhash;
794-
authority = durableNonceInfo.authority;
795-
}
796-
797791
// check for possible token recovery, recover the token provide by user
798792
if (params.tokenContractAddress) {
799793
const tokenAccounts = await this.getTokenAccountsByOwner(bs58EncodedPublicKey);
@@ -876,6 +870,10 @@ export class Sol extends BaseCoin {
876870
}
877871

878872
if (params.durableNonce) {
873+
const durableNonceInfo = await this.getAccountInfo(params.durableNonce.publicKey);
874+
blockhash = durableNonceInfo.blockhash;
875+
authority = durableNonceInfo.authority;
876+
879877
txBuilder.nonce(blockhash, {
880878
walletNonceAddress: params.durableNonce.publicKey,
881879
authWalletAddress: authority,
@@ -886,13 +884,43 @@ export class Sol extends BaseCoin {
886884
const unsignedTransactionWithoutFee = (await txBuilder.build()) as Transaction;
887885
const serializedMessage = unsignedTransactionWithoutFee.solTransaction.serializeMessage().toString('base64');
888886

889-
const feePerSignature = await this.getFeeForMessage(serializedMessage);
890-
const baseFee = params.durableNonce ? feePerSignature * 2 : feePerSignature;
887+
const baseFee = await this.getFeeForMessage(serializedMessage);
888+
const feePerSignature = params.durableNonce ? baseFee / 2 : baseFee;
891889
totalFee = totalFee.plus(new BigNumber(baseFee));
892890
totalFeeForTokenRecovery = totalFeeForTokenRecovery.plus(new BigNumber(baseFee));
893891
if (totalFee.gt(balance)) {
894892
throw Error('Did not find address with funds to recover');
895893
}
894+
895+
if (params.tokenContractAddress) {
896+
// Check if there is sufficient native solana to recover tokens
897+
if (new BigNumber(balance).lt(totalFeeForTokenRecovery)) {
898+
throw Error(
899+
'Not enough funds to pay for recover tokens fees, have: ' +
900+
balance +
901+
' need: ' +
902+
totalFeeForTokenRecovery.toString()
903+
);
904+
}
905+
txBuilder.fee({ amount: feePerSignature });
906+
} else {
907+
const netAmount = new BigNumber(balance).minus(totalFee);
908+
txBuilder = factory
909+
.getTransferBuilder()
910+
.nonce(blockhash)
911+
.sender(bs58EncodedPublicKey)
912+
.send({ address: params.recoveryDestination, amount: netAmount.toString() })
913+
.feePayer(bs58EncodedPublicKey)
914+
.fee({ amount: feePerSignature });
915+
916+
if (params.durableNonce) {
917+
txBuilder.nonce(blockhash, {
918+
walletNonceAddress: params.durableNonce.publicKey,
919+
authWalletAddress: authority,
920+
});
921+
}
922+
}
923+
896924
if (!isUnsignedSweep) {
897925
// Sign the txn
898926
if (!params.userKey) {
@@ -907,25 +935,6 @@ export class Sol extends BaseCoin {
907935
throw new Error('missing wallet passphrase');
908936
}
909937

910-
if (params.tokenContractAddress) {
911-
totalFeeForTokenRecovery = totalFeeForTokenRecovery.plus(new BigNumber(baseFee));
912-
// Check if there is sufficient native solana to recover tokens
913-
if (new BigNumber(balance).lt(totalFeeForTokenRecovery)) {
914-
throw Error(
915-
'Not enough funds to pay for recover tokens fees, have: ' +
916-
balance +
917-
' need: ' +
918-
totalFeeForTokenRecovery.toString()
919-
);
920-
}
921-
txBuilder.fee({ amount: feePerSignature });
922-
} else {
923-
totalFee = new BigNumber(baseFee);
924-
const netAmount = new BigNumber(balance).minus(totalFee);
925-
txBuilder
926-
.send({ address: params.recoveryDestination, amount: netAmount.toString() })
927-
.fee({ amount: feePerSignature });
928-
}
929938
// build the transaction with fee
930939
const unsignedTransaction = (await txBuilder.build()) as Transaction;
931940

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)