Skip to content

Commit

Permalink
Merge pull request #81 from FluxAstromeshLabs/f/intent-calculate-svm-acc
Browse files Browse the repository at this point in the history
feat(intent): render svm acc for fis input
  • Loading branch information
nntruong96 authored Oct 30, 2024
2 parents e7e283e + 0fce91e commit c7451ac
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"grpc-tools": "^1.12.4",
"grpc-web": "^1.4.2",
"grpc_tools_node_protoc_ts": "^5.3.3",
"handlebars": "^4.7.8",
"http-status-codes": "^2.3.0",
"keccak256": "^1.0.6",
"mocha": "^10.4.0",
Expand Down
44 changes: 34 additions & 10 deletions packages/utils/intent-parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@ import {
FISQueryRequest,
queryActionFromJSON
} from '../../../chain/flux/astromesh/v1beta1/query'
import { Plane, planeFromJSON, planeToJSON } from '../../../chain/flux/astromesh/v1beta1/tx'
import {
Schema,
SchemaFISQuery,
SchemaPrompt,
StrategyMetadata
} from '../../../chain/flux/strategy/v1beta1/strategy'
import { planeFromJSON } from '../../../chain/flux/astromesh/v1beta1/tx'
import { MsgTriggerStrategies } from '../../../chain/flux/strategy/v1beta1/tx'
function replaceTypedPlaceholders(template: string, values: any) {
return template.replace(/\${(\w+:\w+)}/g, (_: any, key: string) => values[key] || '')
}
import * as Handlebars from 'handlebars'
import * as web3 from '@solana/web3.js'

function replacePlaceholders(template: string, values: any) {
return template.replace(/\${(\w+)}/g, (_: any, key: string) => values[key] || '')
}

Handlebars.registerHelper('decodeBase58', function(base58EncodedString) {
return new web3.PublicKey(base58EncodedString).toBytes()
})

Handlebars.registerHelper('pda', function(...args) {
let programId = new web3.PublicKey(args[args.length - 2])
let seeds: Uint8Array[] = []
for(let i = 0; i<args.length - 2; i++) {
seeds.push(args[i])
}
let pdaResult = web3.PublicKey.findProgramAddressSync(seeds, programId)
return pdaResult[0]
});

/*
Pending Todo: input address by plane?
E.g query order of an address (calculated from programId and some seeds) => need to support equation eval() with input params
Expand All @@ -42,6 +49,23 @@ export function compileTriggerMsg(
// replace vars within queries
let inputs = []
for (let i = 0; i < ix.input.length; i++) {
// parse accounts input for SVM for now
// we can propagate to other planes when it's needed
if (ix.plane == "SVM") {
let input = Buffer.from(ix.input[i], 'base64').toString('latin1')
// only consider template, other stays unchanged
if (input.startsWith("{{") && input.endsWith("}}")) {
let templateSource = Handlebars.compile(input);
const result = templateSource({
...userInput,
...defaultConst,
});
inputs.push(new web3.PublicKey(result).toBytes())
continue
}
}

// do normal parsing to get it backward compatible for now
let input = Buffer.from(ix.input[i], 'base64')
let replacedBytes = replacePlaceholders(input.toString('latin1'), knownVars)
inputs.push(Buffer.from(replacedBytes, 'latin1'))
Expand Down
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4332,6 +4332,18 @@ [email protected]:
optionalDependencies:
uglify-js "^3.1.4"

handlebars@^4.7.8:
version "4.7.8"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.8.tgz#41c42c18b1be2365439188c77c6afae71c0cd9e9"
integrity sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==
dependencies:
minimist "^1.2.5"
neo-async "^2.6.2"
source-map "^0.6.1"
wordwrap "^1.0.0"
optionalDependencies:
uglify-js "^3.1.4"

has-flag@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
Expand Down Expand Up @@ -5102,7 +5114,7 @@ napi-wasm@^1.1.0:
resolved "https://registry.yarnpkg.com/napi-wasm/-/napi-wasm-1.1.3.tgz#7bb95c88e6561f84880bb67195437b1cfbe99224"
integrity sha512-h/4nMGsHjZDCYmQVNODIrYACVJ+I9KItbG+0si6W/jSjdA9JbWDoU4LLeMXVcEQGHjttI2tuXqDrbGF7qkUHHg==

neo-async@^2.6.0:
neo-async@^2.6.0, neo-async@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f"
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==
Expand Down

0 comments on commit c7451ac

Please sign in to comment.