Skip to content

Commit

Permalink
Support fulfilling spl-token2022 (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: mrlotfi <[email protected]>
  • Loading branch information
mrlotfi and mrlotfi authored Sep 26, 2024
1 parent cf0f661 commit 4648e46
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 167 deletions.
140 changes: 74 additions & 66 deletions src/abis/swift.idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export type Swift = {
isMut: false;
isSigner: false;
},
{
name: 'tokenProgram';
isMut: false;
isSigner: false;
},
];
args: [
{
Expand Down Expand Up @@ -167,7 +172,12 @@ export type Swift = {
isSigner: false;
},
];
args: [];
args: [
{
name: 'tryCloseAta';
type: 'bool';
},
];
},
{
name: 'postUnlock';
Expand Down Expand Up @@ -283,22 +293,6 @@ export type Swift = {
isMut: false;
isSigner: false;
},
{
name: 'mintTo';
isMut: false;
isSigner: false;
},
{
name: 'tokenProgram';
isMut: false;
isSigner: false;
},
{
name: 'stateToAcc';
isMut: true;
isSigner: false;
isOptional: true;
},
];
args: [
{
Expand Down Expand Up @@ -653,7 +647,7 @@ export type Swift = {
type: 'u64';
},
{
name: 'lastSeqMsg';
name: 'seqMsg';
type: 'u64';
},
];
Expand Down Expand Up @@ -1111,71 +1105,81 @@ export type Swift = {
},
{
code: 6036;
name: 'RelayerIsTraderFeeSubmit';
msg: 'relayer is trader but fee_submit > 0';
},
{
code: 6037;
name: 'MintAndTokenProgramMismatch';
msg: 'Mint is not match with token program';
},
{
code: 6038;
name: 'InvalidUnlockBatchVAA';
msg: 'Invalid unlock batch vaa';
},
{
code: 6037;
code: 6039;
name: 'InvalidUnlockVAA';
msg: 'Invalid unlock vaa';
},
{
code: 6038;
code: 6040;
name: 'DriverIsNotUnlocker';
msg: 'Driver is not equal to vaa unlocker';
},
{
code: 6039;
code: 6041;
name: 'MintIsNotTokenIn';
msg: 'Mint is not equal to vaa token in';
},
{
code: 6040;
code: 6042;
name: 'InvalidRemainingAccountsCount';
},
{
code: 6041;
code: 6043;
name: 'InvalidTokenAccountMint';
},
{
code: 6042;
code: 6044;
name: 'InvalidTokenAccountOwner';
},
{
code: 6043;
code: 6045;
name: 'InvalidEmitterChain';
},
{
code: 6044;
code: 6046;
name: 'InvalidEmitterAddress';
},
{
code: 6045;
code: 6047;
name: 'InvalidCancelVAA';
msg: 'Invalid cancel vaa';
},
{
code: 6046;
code: 6048;
name: 'WrongCancelRelayerAddress';
},
{
code: 6047;
code: 6049;
name: 'InsufficientFundsToRefundFee';
},
{
code: 6048;
code: 6050;
name: 'InvalidTrader';
},
{
code: 6049;
code: 6051;
name: 'InvalidOneOwner';
},
{
code: 6050;
code: 6052;
name: 'InvalidTwoOwner';
},
{
code: 6051;
code: 6053;
name: 'InvalidThreeOwner';
},
];
Expand Down Expand Up @@ -1268,6 +1272,11 @@ export const IDL: Swift = {
isMut: false,
isSigner: false,
},
{
name: 'tokenProgram',
isMut: false,
isSigner: false,
},
],
args: [
{
Expand Down Expand Up @@ -1350,7 +1359,12 @@ export const IDL: Swift = {
isSigner: false,
},
],
args: [],
args: [
{
name: 'tryCloseAta',
type: 'bool',
},
],
},
{
name: 'postUnlock',
Expand Down Expand Up @@ -1466,22 +1480,6 @@ export const IDL: Swift = {
isMut: false,
isSigner: false,
},
{
name: 'mintTo',
isMut: false,
isSigner: false,
},
{
name: 'tokenProgram',
isMut: false,
isSigner: false,
},
{
name: 'stateToAcc',
isMut: true,
isSigner: false,
isOptional: true,
},
],
args: [
{
Expand Down Expand Up @@ -1836,7 +1834,7 @@ export const IDL: Swift = {
type: 'u64',
},
{
name: 'lastSeqMsg',
name: 'seqMsg',
type: 'u64',
},
],
Expand Down Expand Up @@ -2294,71 +2292,81 @@ export const IDL: Swift = {
},
{
code: 6036,
name: 'RelayerIsTraderFeeSubmit',
msg: 'relayer is trader but fee_submit > 0',
},
{
code: 6037,
name: 'MintAndTokenProgramMismatch',
msg: 'Mint is not match with token program',
},
{
code: 6038,
name: 'InvalidUnlockBatchVAA',
msg: 'Invalid unlock batch vaa',
},
{
code: 6037,
code: 6039,
name: 'InvalidUnlockVAA',
msg: 'Invalid unlock vaa',
},
{
code: 6038,
code: 6040,
name: 'DriverIsNotUnlocker',
msg: 'Driver is not equal to vaa unlocker',
},
{
code: 6039,
code: 6041,
name: 'MintIsNotTokenIn',
msg: 'Mint is not equal to vaa token in',
},
{
code: 6040,
code: 6042,
name: 'InvalidRemainingAccountsCount',
},
{
code: 6041,
code: 6043,
name: 'InvalidTokenAccountMint',
},
{
code: 6042,
code: 6044,
name: 'InvalidTokenAccountOwner',
},
{
code: 6043,
code: 6045,
name: 'InvalidEmitterChain',
},
{
code: 6044,
code: 6046,
name: 'InvalidEmitterAddress',
},
{
code: 6045,
code: 6047,
name: 'InvalidCancelVAA',
msg: 'Invalid cancel vaa',
},
{
code: 6046,
code: 6048,
name: 'WrongCancelRelayerAddress',
},
{
code: 6047,
code: 6049,
name: 'InsufficientFundsToRefundFee',
},
{
code: 6048,
code: 6050,
name: 'InvalidTrader',
},
{
code: 6049,
code: 6051,
name: 'InvalidOneOwner',
},
{
code: 6050,
code: 6052,
name: 'InvalidTwoOwner',
},
{
code: 6051,
code: 6053,
name: 'InvalidThreeOwner',
},
],
Expand Down
29 changes: 25 additions & 4 deletions src/config/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TOKEN_2022_PROGRAM_ID } from '@solana/spl-token';
import { Connection, ParsedAccountData, PublicKey } from '@solana/web3.js';
import axios from 'axios';
import { getDecimals, getSymbol } from '../utils/erc20';
Expand Down Expand Up @@ -31,6 +32,8 @@ export type Token = {
realOriginChainId: number;
realOriginContractAddress: string;
supportsPermit?: boolean;
hasTransferFee?: boolean;
standard: 'native' | 'erc20' | 'spl' | 'spl2022';
};

export class TokenList {
Expand Down Expand Up @@ -144,6 +147,7 @@ export class TokenList {
realOriginContractAddress: tokenContract,
symbol: symbol,
supportsPermit: false,
standard: 'erc20',
};
}

Expand All @@ -152,8 +156,27 @@ export class TokenList {
if (!mintAccountInfo.value) {
throw new Error(`Token account not found on solana chain for ${tokenContract}`);
}
let isToken2022 = false;
if (mintAccountInfo && mintAccountInfo.value) {
const ownerProgramId = (mintAccountInfo.value as any).owner;
isToken2022 = ownerProgramId.equals(TOKEN_2022_PROGRAM_ID);
}
const mintData = mintAccountInfo.value.data as ParsedAccountData;
const decimals = Number(mintData.parsed.info.decimals);
let transferFeeExtension = mintData.parsed.info.extensions?.find(
(e: any) => e.extension === 'transferFeeConfig',
);

let hasTransferFee = false;
if (transferFeeExtension) {
if (Number(transferFeeExtension.withheldAmount)) {
hasTransferFee = true;
}

if (Number(transferFeeExtension.state?.newerTransferFee?.transferFeeBasisPoints)) {
hasTransferFee = true;
}
}

return {
chainId: CHAIN_ID_SOLANA,
Expand All @@ -167,13 +190,11 @@ export class TokenList {
realOriginContractAddress: tokenContract,
symbol: '',
supportsPermit: false,
standard: isToken2022 ? 'spl2022' : 'spl',
hasTransferFee: hasTransferFee,
};
}

// private fetchERC20TokenData(tokenContract: string): Promise<Token> {
// const tokenData = null;
// }

private getPreDefinedTokenData(tokenChain: number, tokenContract: string): Token | undefined {
if (!this.tokensPerChain[tokenChain]) {
throw new Error(`Chain ${tokenChain} is not found in token list`);
Expand Down
Loading

0 comments on commit 4648e46

Please sign in to comment.