Skip to content

Commit 6ee2d78

Browse files
Merge branch 'master' into BTC-2143.delegation-message-sample
2 parents 251ced3 + f577966 commit 6ee2d78

24 files changed

+124
-575
lines changed

modules/bitgo/test/v2/unit/keychains.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ describe('V2 Keychains', function () {
8989
n.asset !== UnderlyingAsset.FETCHAI &&
9090
n.asset !== UnderlyingAsset.INITIA &&
9191
n.asset !== UnderlyingAsset.ASI &&
92+
n.asset !== UnderlyingAsset.SONIC &&
9293
coinFamilyValues.includes(n.name)
9394
);
9495

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ export class AtaInitializationBuilder extends TransactionBuilder {
4242
this._tokenAssociateRecipients.push({
4343
ownerAddress: ataInitInstruction.params.ownerAddress,
4444
tokenName: ataInitInstruction.params.tokenName,
45-
tokenAddress: ataInitInstruction.params.mintAddress,
46-
programId: ataInitInstruction.params.programId,
4745
});
4846
}
4947
}
@@ -117,15 +115,10 @@ export class AtaInitializationBuilder extends TransactionBuilder {
117115
}
118116
validateOwnerAddress(recipient.ownerAddress);
119117
const token = getSolTokenFromTokenName(recipient.tokenName);
120-
let tokenAddress: string;
121-
if (recipient.tokenAddress) {
122-
tokenAddress = recipient.tokenAddress;
123-
} else if (token) {
124-
tokenAddress = token.tokenAddress;
125-
} else {
118+
if (!token) {
126119
throw new BuildTransactionError('Invalid transaction: invalid token name, got: ' + recipient.tokenName);
127120
}
128-
validateMintAddress(tokenAddress);
121+
validateMintAddress(token.tokenAddress);
129122

130123
this._tokenAssociateRecipients.push(recipient);
131124
return this;
@@ -148,33 +141,30 @@ export class AtaInitializationBuilder extends TransactionBuilder {
148141
await Promise.all(
149142
this._tokenAssociateRecipients.map(async (recipient) => {
150143
const token = getSolTokenFromTokenName(recipient.tokenName);
151-
let tokenAddress: string;
152-
let programId: string;
153-
if (recipient.tokenAddress && recipient.programId) {
154-
tokenAddress = recipient.tokenAddress;
155-
programId = recipient.programId;
156-
} else if (token) {
157-
tokenAddress = token.tokenAddress;
158-
programId = token.programId;
159-
} else {
144+
if (!token) {
160145
throw new BuildTransactionError('Invalid transaction: invalid token name, got: ' + recipient.tokenName);
161146
}
162147

163148
// Use the provided ataAddress if it exists, otherwise calculate it
164149
let ataPk = recipient.ataAddress;
165150
if (!ataPk) {
166-
ataPk = await getAssociatedTokenAccountAddress(tokenAddress, recipient.ownerAddress, false, programId);
151+
ataPk = await getAssociatedTokenAccountAddress(
152+
token.tokenAddress,
153+
recipient.ownerAddress,
154+
false,
155+
token.programId
156+
);
167157
}
168158

169159
this._instructionsData.push({
170160
type: InstructionBuilderTypes.CreateAssociatedTokenAccount,
171161
params: {
172-
mintAddress: tokenAddress,
162+
mintAddress: token.tokenAddress,
173163
ataAddress: ataPk,
174164
ownerAddress: recipient.ownerAddress,
175165
payerAddress: this._sender,
176166
tokenName: recipient.tokenName,
177-
programId: programId,
167+
programId: token.programId,
178168
},
179169
});
180170
})

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ export interface TokenTransfer {
7272
amount: string;
7373
tokenName: string;
7474
sourceAddress: string;
75-
tokenAddress?: string;
76-
decimalPlaces?: number;
77-
programId?: string;
7875
};
7976
}
8077

@@ -185,6 +182,4 @@ export class TokenAssociateRecipient {
185182
ownerAddress: string;
186183
tokenName: string;
187184
ataAddress?: string;
188-
tokenAddress?: string;
189-
programId?: string;
190185
}

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

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,21 @@ import { getInstructionType } from './utils';
5252
export function instructionParamsFactory(
5353
type: TransactionType,
5454
instructions: TransactionInstruction[],
55-
coinName?: string,
56-
instructionMetadata?: InstructionParams[],
57-
_useTokenAddressTokenName?: boolean
55+
coinName?: string
5856
): InstructionParams[] {
5957
switch (type) {
6058
case TransactionType.WalletInitialization:
6159
return parseWalletInitInstructions(instructions);
6260
case TransactionType.Send:
63-
return parseSendInstructions(instructions, instructionMetadata, _useTokenAddressTokenName);
61+
return parseSendInstructions(instructions);
6462
case TransactionType.StakingActivate:
6563
return parseStakingActivateInstructions(instructions);
6664
case TransactionType.StakingDeactivate:
6765
return parseStakingDeactivateInstructions(instructions, coinName);
6866
case TransactionType.StakingWithdraw:
6967
return parseStakingWithdrawInstructions(instructions);
7068
case TransactionType.AssociatedTokenAccountInitialization:
71-
return parseAtaInitInstructions(instructions, instructionMetadata, _useTokenAddressTokenName);
69+
return parseAtaInitInstructions(instructions);
7270
case TransactionType.CloseAssociatedTokenAccount:
7371
return parseAtaCloseInstructions(instructions);
7472
case TransactionType.StakingAuthorize:
@@ -122,9 +120,7 @@ function parseWalletInitInstructions(instructions: TransactionInstruction[]): Ar
122120
* @returns {InstructionParams[]} An array containing instruction params for Send tx
123121
*/
124122
function parseSendInstructions(
125-
instructions: TransactionInstruction[],
126-
instructionMetadata?: InstructionParams[],
127-
_useTokenAddressTokenName?: boolean
123+
instructions: TransactionInstruction[]
128124
): Array<Nonce | Memo | Transfer | TokenTransfer | AtaInit | AtaClose | SetPriorityFee> {
129125
const instructionData: Array<Nonce | Memo | Transfer | TokenTransfer | AtaInit | AtaClose | SetPriorityFee> = [];
130126
for (const instruction of instructions) {
@@ -164,12 +160,7 @@ function parseSendInstructions(
164160
} else {
165161
tokenTransferInstruction = decodeTransferCheckedInstruction(instruction, TOKEN_2022_PROGRAM_ID);
166162
}
167-
const tokenAddress = tokenTransferInstruction.keys.mint.pubkey.toString();
168-
const tokenName = findTokenName(tokenAddress, instructionMetadata, _useTokenAddressTokenName);
169-
let programIDForTokenTransfer: string | undefined;
170-
if (instruction.programId) {
171-
programIDForTokenTransfer = instruction.programId.toString();
172-
}
163+
const tokenName = findTokenName(tokenTransferInstruction.keys.mint.pubkey.toString());
173164
const tokenTransfer: TokenTransfer = {
174165
type: InstructionBuilderTypes.TokenTransfer,
175166
params: {
@@ -178,20 +169,13 @@ function parseSendInstructions(
178169
amount: tokenTransferInstruction.data.amount.toString(),
179170
tokenName,
180171
sourceAddress: tokenTransferInstruction.keys.source.pubkey.toString(),
181-
tokenAddress: tokenAddress,
182-
programId: programIDForTokenTransfer,
183-
decimalPlaces: tokenTransferInstruction.data.decimals,
184172
},
185173
};
186174
instructionData.push(tokenTransfer);
187175
break;
188176
case ValidInstructionTypesEnum.InitializeAssociatedTokenAccount:
189177
const mintAddress = instruction.keys[ataInitInstructionKeysIndexes.MintAddress].pubkey.toString();
190-
const mintTokenName = findTokenName(mintAddress, instructionMetadata, _useTokenAddressTokenName);
191-
let programID: string | undefined;
192-
if (instruction.programId) {
193-
programID = instruction.programId.toString();
194-
}
178+
const mintTokenName = findTokenName(mintAddress);
195179

196180
const ataInit: AtaInit = {
197181
type: InstructionBuilderTypes.CreateAssociatedTokenAccount,
@@ -201,7 +185,6 @@ function parseSendInstructions(
201185
ownerAddress: instruction.keys[ataInitInstructionKeysIndexes.OwnerAddress].pubkey.toString(),
202186
payerAddress: instruction.keys[ataInitInstructionKeysIndexes.PayerAddress].pubkey.toString(),
203187
tokenName: mintTokenName,
204-
programId: programID,
205188
},
206189
};
207190
instructionData.push(ataInit);
@@ -669,11 +652,7 @@ const closeAtaInstructionKeysIndexes = {
669652
* @param {TransactionInstruction[]} instructions - an array of supported Solana instructions
670653
* @returns {InstructionParams[]} An array containing instruction params for Send tx
671654
*/
672-
function parseAtaInitInstructions(
673-
instructions: TransactionInstruction[],
674-
instructionMetadata?: InstructionParams[],
675-
_useTokenAddressTokenName?: boolean
676-
): Array<AtaInit | Memo | Nonce> {
655+
function parseAtaInitInstructions(instructions: TransactionInstruction[]): Array<AtaInit | Memo | Nonce> {
677656
const instructionData: Array<AtaInit | Memo | Nonce> = [];
678657
let memo: Memo | undefined;
679658

@@ -696,11 +675,8 @@ function parseAtaInitInstructions(
696675
break;
697676
case ValidInstructionTypesEnum.InitializeAssociatedTokenAccount:
698677
const mintAddress = instruction.keys[ataInitInstructionKeysIndexes.MintAddress].pubkey.toString();
699-
const tokenName = findTokenName(mintAddress, instructionMetadata, _useTokenAddressTokenName);
700-
let programID: string | undefined;
701-
if (instruction.programId) {
702-
programID = instruction.programId.toString();
703-
}
678+
const tokenName = findTokenName(mintAddress);
679+
704680
const ataInit: AtaInit = {
705681
type: InstructionBuilderTypes.CreateAssociatedTokenAccount,
706682
params: {
@@ -709,7 +685,6 @@ function parseAtaInitInstructions(
709685
ownerAddress: instruction.keys[ataInitInstructionKeysIndexes.OwnerAddress].pubkey.toString(),
710686
payerAddress: instruction.keys[ataInitInstructionKeysIndexes.PayerAddress].pubkey.toString(),
711687
tokenName,
712-
programId: programID,
713688
},
714689
};
715690
instructionData.push(ataInit);
@@ -856,11 +831,7 @@ function parseStakingAuthorizeRawInstructions(instructions: TransactionInstructi
856831
return instructionData;
857832
}
858833

859-
function findTokenName(
860-
mintAddress: string,
861-
instructionMetadata?: InstructionParams[],
862-
_useTokenAddressTokenName?: boolean
863-
): string {
834+
function findTokenName(mintAddress: string): string {
864835
let token: string | undefined;
865836

866837
coins.forEach((value, key) => {
@@ -869,26 +840,6 @@ function findTokenName(
869840
}
870841
});
871842

872-
if (!token && instructionMetadata) {
873-
instructionMetadata.forEach((instruction) => {
874-
if (
875-
instruction.type === InstructionBuilderTypes.CreateAssociatedTokenAccount &&
876-
instruction.params.mintAddress === mintAddress
877-
) {
878-
token = instruction.params.tokenName;
879-
} else if (
880-
instruction.type === InstructionBuilderTypes.TokenTransfer &&
881-
instruction.params.tokenAddress === mintAddress
882-
) {
883-
token = instruction.params.tokenName;
884-
}
885-
});
886-
}
887-
888-
if (!token && _useTokenAddressTokenName) {
889-
token = mintAddress;
890-
}
891-
892843
assert(token);
893844

894845
return token;

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SolCoin } from '@bitgo/statics';
1+
import { coins, SolCoin } from '@bitgo/statics';
22
import {
33
createAssociatedTokenAccountInstruction,
44
createCloseAccountInstruction,
@@ -35,7 +35,6 @@ import {
3535
WalletInit,
3636
SetPriorityFee,
3737
} from './iface';
38-
import { getSolTokenFromTokenName } from './utils';
3938

4039
/**
4140
* Construct Solana instructions from instructions params
@@ -158,43 +157,28 @@ function tokenTransferInstruction(data: TokenTransfer): TransactionInstruction[]
158157
assert(amount, 'Missing amount param');
159158
assert(tokenName, 'Missing token name');
160159
assert(sourceAddress, 'Missing ata address');
161-
const token = getSolTokenFromTokenName(data.params.tokenName);
162-
let tokenAddress: string;
163-
let programId: string | undefined;
164-
let decimalPlaces: number;
165-
if (data.params.tokenAddress && data.params.decimalPlaces) {
166-
tokenAddress = data.params.tokenAddress;
167-
decimalPlaces = data.params.decimalPlaces;
168-
programId = data.params.programId;
169-
} else if (token) {
170-
assert(token instanceof SolCoin);
171-
tokenAddress = token.tokenAddress;
172-
decimalPlaces = token.decimalPlaces;
173-
programId = token.programId;
174-
} else {
175-
throw new Error('Invalid token name, got:' + data.params.tokenName);
176-
}
177-
160+
const token = coins.get(data.params.tokenName);
161+
assert(token instanceof SolCoin);
178162
let transferInstruction: TransactionInstruction;
179-
if (programId === TOKEN_2022_PROGRAM_ID.toString()) {
163+
if (token.programId === TOKEN_2022_PROGRAM_ID.toString()) {
180164
transferInstruction = createTransferCheckedInstruction(
181165
new PublicKey(sourceAddress),
182-
new PublicKey(tokenAddress),
166+
new PublicKey(token.tokenAddress),
183167
new PublicKey(toAddress),
184168
new PublicKey(fromAddress),
185169
BigInt(amount),
186-
decimalPlaces,
170+
token.decimalPlaces,
187171
[],
188172
TOKEN_2022_PROGRAM_ID
189173
);
190174
} else {
191175
transferInstruction = createTransferCheckedInstruction(
192176
new PublicKey(sourceAddress),
193-
new PublicKey(tokenAddress),
177+
new PublicKey(token.tokenAddress),
194178
new PublicKey(toAddress),
195179
new PublicKey(fromAddress),
196180
BigInt(amount),
197-
decimalPlaces
181+
token.decimalPlaces
198182
);
199183
}
200184
return [transferInstruction];

0 commit comments

Comments
 (0)