-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore/add-base-sepolia
- Loading branch information
Showing
20 changed files
with
421 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/base/adapters/solana/web3js/tests/createSendTransaction.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { beforeEach, describe, expect, it } from 'vitest' | ||
import { mockConnection } from './mocks/Connection' | ||
import { createSendTransaction } from '../utils/createSendTransaction' | ||
import type { Provider } from '@web3modal/scaffold-utils/solana' | ||
import { PublicKey } from '@solana/web3.js' | ||
|
||
const mockProvider = () => { | ||
return { | ||
publicKey: new PublicKey('2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP') | ||
} as unknown as Provider | ||
} | ||
|
||
describe('createSendTransaction', () => { | ||
let provider = mockProvider() | ||
let connection = mockConnection() | ||
|
||
beforeEach(() => { | ||
provider = mockProvider() | ||
connection = mockConnection() | ||
}) | ||
|
||
it('should create a transaction', async () => { | ||
const transaction = await createSendTransaction({ | ||
provider, | ||
connection, | ||
to: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP', | ||
value: 10 | ||
}) | ||
|
||
expect(transaction).toBeDefined() | ||
}) | ||
|
||
it('should create a correct serialized transaction', async () => { | ||
const transaction = await createSendTransaction({ | ||
provider, | ||
connection, | ||
to: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP', | ||
value: 10 | ||
}) | ||
|
||
// Serializing to base64 only for comparison of the transaction bytes | ||
const serialized = transaction.serialize({ verifySignatures: false }).toString('base64') | ||
expect(serialized).toBe( | ||
'AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAIDFj6WhBP/eepC4T4bDgYuJMiSVXNh9IvPWv1ZDUV52gYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMGRm/lIRcy/+ytunLDm+e8jOW7xfcSayxDmzpAAAAAyZpToWInFL+zDFy34fwit57sURE//y+sa4B0X3QA16UDAgAJAwAtMQEAAAAAAgAFAvQBAAABAgAADAIAAAAA5AtUAgAAAA==' | ||
) | ||
}) | ||
}) |
13 changes: 13 additions & 0 deletions
13
packages/base/adapters/solana/web3js/tests/mocks/Connection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Connection } from '@solana/web3.js' | ||
import { vi } from 'vitest' | ||
|
||
export function mockConnection() { | ||
const connection = new Connection('https://mocked.api.connection') | ||
|
||
return Object.assign(connection, { | ||
getLatestBlockhash: vi.fn().mockResolvedValue({ | ||
blockhash: 'EZySCpmzXRuUtM95P2JGv9SitqYph6Nv6HaYBK7a8PKJ', | ||
lastValidBlockHeight: 1 | ||
}) | ||
}) | ||
} |
57 changes: 57 additions & 0 deletions
57
packages/base/adapters/solana/web3js/utils/createSendTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
PublicKey, | ||
SystemProgram, | ||
type Connection, | ||
Transaction, | ||
LAMPORTS_PER_SOL, | ||
ComputeBudgetProgram | ||
} from '@solana/web3.js' | ||
import type { Provider } from '@web3modal/scaffold-utils/solana' | ||
|
||
type SendTransactionArgs = { | ||
provider: Provider | ||
connection: Connection | ||
to: string | ||
value: number | ||
} | ||
|
||
/** | ||
* These constants defines the cost of running the program, allowing to calculate the maximum | ||
* amount of SOL that can be sent in case of cleaning the account and remove the rent exemption error. | ||
*/ | ||
const COMPUTE_BUDGET_CONSTANTS = { | ||
UNIT_PRICE_MICRO_LAMPORTS: 20000000, | ||
UNIT_LIMIT: 500 | ||
} | ||
|
||
export async function createSendTransaction({ | ||
provider, | ||
to, | ||
value, | ||
connection | ||
}: SendTransactionArgs): Promise<Transaction> { | ||
if (!provider.publicKey) { | ||
throw Error('No public key found') | ||
} | ||
|
||
const toPubkey = new PublicKey(to) | ||
const lamports = Math.floor(value * LAMPORTS_PER_SOL) | ||
|
||
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash() | ||
|
||
const instructions = [ | ||
ComputeBudgetProgram.setComputeUnitPrice({ | ||
microLamports: COMPUTE_BUDGET_CONSTANTS.UNIT_PRICE_MICRO_LAMPORTS | ||
}), | ||
ComputeBudgetProgram.setComputeUnitLimit({ units: COMPUTE_BUDGET_CONSTANTS.UNIT_LIMIT }), | ||
SystemProgram.transfer({ | ||
fromPubkey: provider.publicKey, | ||
toPubkey, | ||
lamports | ||
}) | ||
] | ||
|
||
return new Transaction({ feePayer: provider.publicKey, blockhash, lastValidBlockHeight }).add( | ||
...instructions | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.