Skip to content

Commit

Permalink
Merge pull request #19 from getsafle/feature-test-suite
Browse files Browse the repository at this point in the history
Feature test suite
  • Loading branch information
sshubhamagg authored Jul 25, 2024
2 parents 79636dd + f15c96f commit 6590667
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
- Added functionality to sign SOL and fungible tokens transfer transaction
- Added functionality to broadcast a signed transaction
- Added functionality to get estimated fee for a transaction
- Added functionality to accept priority fee for a transaction
- Added functionality to accept priority fee for a transaction
- Added test cases
19 changes: 19 additions & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module.exports = {
HD_WALLET_12_MNEMONIC: 'affair entry detect broom axis crawl found valve bamboo taste broken hundred',

TEST_ADDRESS_1: 'FwHMHCWLZDDoFU3e9fngmX5KtGZ2dyvTQfqZxPcEX2Zi',
PRIVATE_KEY_1: '59gSMJ78dCitP5Yw328BGL7fyG4qeT2ovE1bxkRXwfPkzPEMsAP3CGHmbE4r1eFqwcQH4H7AY5CzAsEr5t2cfVBz',
TEST_ADDRESS_2: '71H3rBodCbkMSNDg4vzoH7tmdQmpY8h8SEe4UY6ZujYS',
TEST_ADDRESS_3: '2JUYPMzwdFvrJWcRweyXzFWHQodCgK93EhEB9yF91FUW',

SOLANA_NETWORK: {
MAINNET: {
Expand All @@ -16,4 +21,18 @@ module.exports = {
URL: 'https://api.devnet.solana.com'
}
},

TRANSACTION_TYPE: {
NATIVE_TRANSFER: "NATIVE_TRANSFER",
TOKEN_TRANSFER: "TOKEN_TRANSFER"
},

TRANSFER_SOL: {
SOL_RECEIVER: '71H3rBodCbkMSNDg4vzoH7tmdQmpY8h8SEe4UY6ZujYS',
SOL_AMOUNT: 100000000
},

TESTING_MESSAGE_1: "ThisMessageOneIsForTesting",
TESTING_MESSAGE_2: "This_message_two_is_for_testing",
TESTING_MESSAGE_3: "This message three is for testing"
}
77 changes: 77 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,102 @@ const { KeyringController: Solana, getBalance } = require('../src/index')

const {
HD_WALLET_12_MNEMONIC,
TEST_ADDRESS_1,
TEST_ADDRESS_2,
PRIVATE_KEY_1,
SOLANA_NETWORK: {
MAINNET,
DEVNET,
TESTNET
},
TRANSACTION_TYPE: {
NATIVE_TRANSFER,
TOKEN_TRANSFER
},
TRANSFER_SOL: {
SOL_RECEIVER,
SOL_AMOUNT
},
TESTING_MESSAGE_1,
TESTING_MESSAGE_2,
TESTING_MESSAGE_3,
} = require('./constants.js')

const opts = {
mnemonic: HD_WALLET_12_MNEMONIC,
network: DEVNET.NETWORK
}

const SOL_TXN_PARAM = {
to: SOL_RECEIVER,
amount: SOL_AMOUNT,
txnType: NATIVE_TRANSFER,
}

describe('Controller test', () => {
const solWallet = new Solana(opts)

it("Should generate new address ", async () => {
const wallet = await solWallet.addAccount()
assert(wallet.address === TEST_ADDRESS_1, "Added address should be " + TEST_ADDRESS_1)
console.log("wallet, ", wallet)
const wallet2 = await solWallet.addAccount()
console.log("wallet2, ", wallet2)
assert(wallet2.address === TEST_ADDRESS_2, "Added address should be " + TEST_ADDRESS_2)
})

it("Should get accounts", async () => {
const acc = await solWallet.getAccounts()
console.log("acc ", acc)
assert(acc.length === 2, "Should have 2 addresses")
})

it("Should get privateKey ", async () => {
const acc = await solWallet.getAccounts()
const privateKey = await solWallet.exportPrivateKey(acc[0])
console.log("privateKey, ", privateKey)
assert(privateKey.privateKey === PRIVATE_KEY_1, "Private key should be " + PRIVATE_KEY_1)
})

it("Should import new account ", async () => {
const acc = await solWallet.getAccounts()
const { privateKey } = await solWallet.exportPrivateKey(acc[0])
const account = await solWallet.importWallet(privateKey)
console.log("account, ", account)
assert(account === acc[0], "Should be the zeroth account")
})

it("Should get balance of the address ", async () => {
const acc = await solWallet.getAccounts()
const balance = await getBalance(acc[0], opts.network)
console.log("balance ", balance)
assert(balance.balance > 0, "Balance should be greater than 0")
})

it("Should sign SOL transfer transaction ", async () => {
assert(solWallet.address !== null)
const acc = await solWallet.getAccounts()
SOL_TXN_PARAM['from'] = acc[0]

const wallet = await solWallet.signTransaction(SOL_TXN_PARAM)

console.log("SOL Transfer signed transaction ", wallet.signedTransaction)
})

it("Sign message", async () => {
const acc = await solWallet.getAccounts()

const signedMessage1 = await solWallet.signMessage(TESTING_MESSAGE_1, acc[0])
console.log("Signed message 1: ", signedMessage1)
assert(signedMessage1.signedMessage, "Message not signed successfully")

const signedMessage2 = await solWallet.signMessage(TESTING_MESSAGE_2, acc[0])
console.log("Signed message 2: ", signedMessage2)
assert(signedMessage2.signedMessage, "Message not signed successfully")

const signedMessage3 = await solWallet.signMessage(TESTING_MESSAGE_3, acc[0])
console.log("Signed message 3: ", signedMessage3)
assert(signedMessage3.signedMessage, "Message not signed successfully")
})

})

0 comments on commit 6590667

Please sign in to comment.