Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
added listenTransactions method
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Jun 23, 2023
1 parent ea92848 commit d2e35f4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"namespace": "multiplechain",
"name": "@multiplechain/solana",
"version": "0.1.6",
"version": "0.1.7",
"description": "Solana has ready-made methods to connect to wallets and perform many transactions.",
"scripts": {
"serve": "parcel test.html --no-cache --dist-dir test",
Expand Down
5 changes: 3 additions & 2 deletions src/entity/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ class Transaction {
}

/**
* @param {Object} options
* @returns {Number}
*/
async getTransactionAmount() {
async getTransferAmount(options) {
await this.getData();
let beforeBalance, afterBalance, diff, decimals;
if (this.data.meta.preTokenBalances.length > 0) {
if (options && options.tokenAddress) {
decimals = this.data.meta.preTokenBalances[0].uiTokenAmount.decimals;
beforeBalance = this.data.meta.preTokenBalances[0].uiTokenAmount.uiAmount;
afterBalance = this.data.meta.postTokenBalances[0].uiTokenAmount.uiAmount;
Expand Down
119 changes: 82 additions & 37 deletions src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ class Provider {
*/
network = {};

/**
* @var {String}
*/
wsUrl;

/**
* @var {Boolean}
*/
qrPayments = false;

/**
* @var {Object}
*/
Expand All @@ -61,14 +71,19 @@ class Provider {
];

/**
* @param {Boolean} testnet
* @param {Object} options
*/
constructor(testnet = false, customRpc = null) {
this.testnet = testnet;
constructor(options) {
this.wsUrl = options.customWs;
this.testnet = options.testnet;

this.network = this.networks[this.testnet ? 'devnet' : 'mainnet'];
if (!this.testnet && customRpc) {
this.network.host = customRpc;
if (!this.testnet && options.customRpc) {
this.network.host = options.customRpc;
}

if (this.wsUrl) {
this.qrPayments = true;
}

this.web3 = new Web3.Connection(this.network.host);
Expand All @@ -77,45 +92,75 @@ class Provider {
}

/**
* @param {String} receiver
* @param {String} tokenAddress
* @returns {Object}
* @param {Object} options
* @param {Function} callback
*/
async getLastTransactionByReceiver(receiver, tokenAddress = null) {
let requestSignatures;
let receiverPublicKey = new Web3.PublicKey(receiver);

if (tokenAddress) {
async listenTransactions(options, callback) {
let subscriptionId;
let receiver = options.receiver;
let tokenAddress = options.tokenAddress;
if (this.wsUrl) {
let ws = new WebSocket(this.wsUrl);
let subscription = {
unsubscribe: () => {
ws.send(
JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'logsUnsubscribe',
params: [subscriptionId],
})
);
ws.close();
}
}

if (tokenAddress) {
let token = new SplToken.Token(
this.web3,
new Web3.PublicKey(tokenAddress),
SplToken.TOKEN_PROGRAM_ID
);

let tokenPublicKey = new Web3.PublicKey(tokenAddress);
let receiverPublicKey = new Web3.PublicKey(receiver);
receiver = (await SplToken.Token.getAssociatedTokenAddress(
token.associatedProgramId,
token.programId,
tokenPublicKey,
receiverPublicKey
)).toBase58();
}

let token = new SplToken.Token(
this.web3,
new Web3.PublicKey(tokenAddress),
SplToken.TOKEN_PROGRAM_ID
);
ws.addEventListener('open', () => {
ws.send(
JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'logsSubscribe',
params: [
{
mentions: [receiver],
},
],
})
);
});

let tokenPublicKey = new Web3.PublicKey(tokenAddress);
let tokenAccount = await SplToken.Token.getAssociatedTokenAddress(
token.associatedProgramId,
token.programId,
tokenPublicKey,
receiverPublicKey
);
ws.addEventListener('message', (res) => {
let data = JSON.parse(res.data);

if (typeof data.result == 'number') {
subscriptionId = data.result;
}

requestSignatures = await this.web3.getSignaturesForAddress(tokenAccount, {
limit: 1
if (data.method && data.method == 'logsNotification' && data.params) {
callback(subscription, this.Transaction(data.params.result.value.signature));
}
});
} else {
requestSignatures = await this.web3.getSignaturesForAddress(receiverPublicKey, {
limit: 1
});
throw new Error('Websocket provider is not defined');
}

let transaction = this.Transaction(requestSignatures[0].signature);

return {
hash: transaction.hash,
amount: await transaction.getTransactionAmount()
};
}

/**
Expand Down

0 comments on commit d2e35f4

Please sign in to comment.