https://github.com/glamsystems/ix-proxy
Converts Solana instructions to GLAM program instructions.
https://github.com/glamsystems/ix-proxy
glam solana
Last synced: 11 months ago
JSON representation
Converts Solana instructions to GLAM program instructions.
- Host: GitHub
- URL: https://github.com/glamsystems/ix-proxy
- Owner: glamsystems
- License: apache-2.0
- Created: 2025-02-25T14:49:20.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-02-27T15:08:42.000Z (11 months ago)
- Last Synced: 2025-02-27T21:24:52.069Z (11 months ago)
- Topics: glam, solana
- Language: Java
- Homepage:
- Size: 81.1 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ixProxy
Facilitates the re-mapping of instructions from one program to another proxy program. The primary use case to add
additional safety checks in the proxy program before forwarding the request to the original program.
## Configuration
```json
{
"program_id": "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH",
"instructions": [
{
"src_ix_name": "cancel_orders",
"src_discriminator": [
238,
225,
95,
158,
227,
103,
8,
194
],
"dst_ix_name": "drift_cancel_orders",
"dst_discriminator": [
98,
107,
48,
79,
97,
60,
99,
58
],
"dynamic_accounts": [
{
"name": "glam_state",
"index": 0,
"writable": false,
"signer": false
},
{
"name": "glam_vault",
"index": 1,
"writable": false,
"signer": false
},
{
"name": "glam_signer",
"index": 2,
"writable": true,
"signer": true
},
{
"name": "cpi_program",
"index": 3,
"writable": false,
"signer": false
}
],
"static_accounts": [],
"index_map": [
4,
5,
-1
]
}
]
}
```
### **src_discriminator**
The instruction discriminator of the original instruction.
### **dst_discriminator**
The instruction discriminator of the proxy instruction.
### **dynamic_accounts**
Account Meta information for accounts which can differ at runtime.
### **static_accounts**
Account Meta information for accounts which are always the same given a Solana cluster such as mainnet.
### **index_map**
Defines the parameter index for the destination instruction. If the account has been removed or replaced use a negative
number.
## Usage
### Construct IxProxy Map
Parse a `ProgramMapConfig` and construct an IxProxy for each instruction. With the `ixProxyMap`, IxProxy's can be
retrieved given the source instruction discriminator and then used to translate the instruction to the proxy program.
```java
var mappingJson = "";
var ji = JsonIterator.parse(mappingJson);
var programMapConfig = ProgramMapConfig.parseConfig(ji);
var program = programMapConfig.program();
var programAccountMeta = AccountMeta.createRead(program);
var ixMapConfigs = programMapConfig.ixMapConfigs();
record GlamVaultAccounts(AccountMeta readGlamState,
AccountMeta writeGlamState,
AccountMeta readGlamVault,
AccountMeta writeGlamVault) {
}
var ixProxyMap = HashMap.>newHashMap(ixMapConfigs.size());
for (var ixMapConfig : ixMapConfigs) {
Function> dynamicAccountFactory = accountConfig -> {
int index = accountConfig.index();
boolean w = accountConfig.writable();
return switch (accountConfig.name()) {
case "glam_state" -> (mappedAccounts, _, vaultAccounts) -> mappedAccounts[index] = w
? vaultAccounts.writeGlamState() : vaultAccounts.readGlamState();
case "glam_vault" -> (mappedAccounts, _, vaultAccounts) -> mappedAccounts[index] = w
? vaultAccounts.writeGlamVault() : vaultAccounts.readGlamVault();
case "glam_signer" -> accountConfig.createFeePayerAccount();
case "cpi_program" -> accountConfig.createDynamicAccount(programAccountMeta);
default -> throw new IllegalStateException("Unknown dynamic account type: " + accountConfig.name());
};
};
var srcDiscriminator = ixMapConfig.srcDiscriminator();
var ixProxy = ixMapConfig.createProxy(dynamicAccountFactory);
ixProxyMap.put(srcDiscriminator, ixProxy);
}
```
### Translate Source Program Instruction
```java
AccountMeta feePayer = AccountMeta.createFeePayer(PublicKey.fromBase58Encoded(""));
PublicKey glamStateAccount = PublicKey.fromBase58Encoded("");
GlamVaultAccounts vaultAccounts = GlamVaultAccounts.create(glamStateAccount);
Instruction sourceIx = null;
IxProxyRecord ixProxy = ixProxyMap.g;
Instruction mappedIx = ixProxy.mapInstruction(feePayer, vaultAccounts, sourceIx);
```