@exodus/solana-lib 1.2.28 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/constants.js +4 -0
- package/src/helpers/spl-token.js +30 -0
- package/src/index.js +1 -1
- package/src/tx/index.js +1 -0
- package/src/tx/simulate-and-sign-tx.js +77 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/solana-lib",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Exodus internal Solana low-level library",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"files": [
|
|
@@ -24,5 +24,6 @@
|
|
|
24
24
|
"create-hash": "^1.1.3",
|
|
25
25
|
"lodash": "^4.17.11",
|
|
26
26
|
"tweetnacl": "^1.0.3"
|
|
27
|
-
}
|
|
27
|
+
},
|
|
28
|
+
"gitHead": "158aae6b4aeb871b1566b2500456a5f475ef9d5f"
|
|
28
29
|
}
|
package/src/constants.js
CHANGED
package/src/helpers/spl-token.js
CHANGED
|
@@ -43,6 +43,20 @@ export class U64 extends BN {
|
|
|
43
43
|
|
|
44
44
|
const u64 = U64 // alias
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Layout for a public key
|
|
48
|
+
*/
|
|
49
|
+
const publicKey = (property = 'publicKey') => {
|
|
50
|
+
return BufferLayout.blob(32, property)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Layout for a 64bit unsigned value
|
|
55
|
+
*/
|
|
56
|
+
const uint64 = (property = 'uint64') => {
|
|
57
|
+
return BufferLayout.blob(8, property)
|
|
58
|
+
}
|
|
59
|
+
|
|
46
60
|
/**
|
|
47
61
|
* An ERC20-like Token
|
|
48
62
|
*/
|
|
@@ -105,4 +119,20 @@ export class Token {
|
|
|
105
119
|
data,
|
|
106
120
|
})
|
|
107
121
|
}
|
|
122
|
+
|
|
123
|
+
static decode(data: string) {
|
|
124
|
+
return BufferLayout.struct([
|
|
125
|
+
publicKey('mint'),
|
|
126
|
+
publicKey('owner'),
|
|
127
|
+
uint64('amount'),
|
|
128
|
+
BufferLayout.u32('delegateOption'),
|
|
129
|
+
publicKey('delegate'),
|
|
130
|
+
BufferLayout.u8('state'),
|
|
131
|
+
BufferLayout.u32('isNativeOption'),
|
|
132
|
+
uint64('isNative'),
|
|
133
|
+
uint64('delegatedAmount'),
|
|
134
|
+
BufferLayout.u32('closeAuthorityOption'),
|
|
135
|
+
publicKey('closeAuthority'),
|
|
136
|
+
]).decode(data)
|
|
137
|
+
}
|
|
108
138
|
}
|
package/src/index.js
CHANGED
|
@@ -4,6 +4,6 @@ export * from './constants'
|
|
|
4
4
|
export * from './encode'
|
|
5
5
|
export * from './keypair'
|
|
6
6
|
export * from './tx'
|
|
7
|
-
export { StakeInstruction, PublicKey } from './vendor'
|
|
7
|
+
export { StakeInstruction, PublicKey, Transaction as SolanaWeb3Transaction, Message as SolanaWeb3Message } from './vendor'
|
|
8
8
|
export { default as tokens } from './tokens'
|
|
9
9
|
export { default as Transaction } from './transaction'
|
package/src/tx/index.js
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Token, U64 } from '../helpers/spl-token'
|
|
2
|
+
import { PublicKey } from '../vendor/'
|
|
3
|
+
|
|
4
|
+
export function computeBalance(futureAccountBalance: string, currentAccountBalance: string) {
|
|
5
|
+
return new U64(futureAccountBalance).sub(new U64(currentAccountBalance))
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function getTransactionSimulationParams(transaction) {
|
|
9
|
+
const config = {
|
|
10
|
+
encoding: 'base64',
|
|
11
|
+
commitment: 'confirmed',
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const accountAddresses = transaction
|
|
15
|
+
.compileMessage()
|
|
16
|
+
.accountKeys.map((account) => account.toString())
|
|
17
|
+
config['accounts'] = {
|
|
18
|
+
encoding: 'base64',
|
|
19
|
+
addresses: accountAddresses,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
config,
|
|
24
|
+
accountAddresses,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const isSolAccount = (account) => account === '1111111111111111'
|
|
29
|
+
|
|
30
|
+
export function filterAccountsByOwner(futureAccountsState, accountAddresses, publicKey) {
|
|
31
|
+
const solAccounts = []
|
|
32
|
+
const tokenAccounts = []
|
|
33
|
+
|
|
34
|
+
// Shouldn't happen
|
|
35
|
+
if (futureAccountsState.length !== accountAddresses.length) {
|
|
36
|
+
throw Error('Simulation returning wrong account length')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
futureAccountsState.forEach((futureAccount, index) => {
|
|
40
|
+
try {
|
|
41
|
+
const accountAddress = accountAddresses[index]
|
|
42
|
+
|
|
43
|
+
// Check if it's SOL account (not token)
|
|
44
|
+
if (isSolAccount(futureAccount.owner.toString())) {
|
|
45
|
+
if (accountAddress.toString() === publicKey.toString()) {
|
|
46
|
+
solAccounts.push({
|
|
47
|
+
amount: futureAccount.lamports,
|
|
48
|
+
address: publicKey,
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const data = Buffer.from(futureAccount.data[0], 'base64')
|
|
55
|
+
const token = Token.decode(data)
|
|
56
|
+
const owner = new PublicKey(token.owner).toString()
|
|
57
|
+
const amount = U64.fromBuffer(token.amount)
|
|
58
|
+
|
|
59
|
+
// Get tokens by owner using the public key
|
|
60
|
+
if (owner === publicKey.toString()) {
|
|
61
|
+
tokenAccounts.push({
|
|
62
|
+
amount,
|
|
63
|
+
mint: new PublicKey(token.mint).toString(),
|
|
64
|
+
owner: owner,
|
|
65
|
+
address: accountAddress.toString(),
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.warn(error)
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
solAccounts,
|
|
75
|
+
tokenAccounts,
|
|
76
|
+
}
|
|
77
|
+
}
|