Skip to content

Commit

Permalink
read accounts mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
konnov committed Dec 23, 2024
1 parent 2633999 commit 77376b7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
26 changes: 23 additions & 3 deletions doc/case-studies/xycloans/ingest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,25 @@

const fs = require('fs')
const assert = require('assert')
const { execSync } = require('child_process')

network = 'testnet'

// Since stellar-cli does not let us sign a transaction by supplying a public key,
// we have to extract account ids. Shall we add a command to solarkraft?
function readOrFindAccounts() {
const accountsFile = 'accounts.json'
if (!fs.existsSync(accountsFile)) {
execSync('solarkraft accounts')
}
try {
return JSON.parse(fs.readFileSync(accountsFile, 'utf8'))
} catch (err) {
console.error(`Error reading ${accountsFile}: ${err.message}`)
process.exit(1)
}
}

// check that we have at least two arguments
const args = process.argv.slice(2)
if (args.length < 2) {
Expand All @@ -40,26 +56,28 @@ const call = trace.states[1].last_tx.call
const callType = call.tag
assert(callType !== undefined, 'traces.states[1].last_tx.call.tag is undefined')

const accounts = readOrFindAccounts()

// produce the arguments for the xycloans transaction
let signer
let callArgs
switch (callType) {
case 'Deposit': {
signer = call.value.from
signer = accounts[call.value.from]
const amount = call.value.amount["#bigint"]
callArgs = `deposit --from ${call.value.from} --amount ${amount}`
break
}

case 'Borrow': {
signer = call.value.receiver_id
signer = accounts[call.value.receiver_id]
const amount = call.value.amount["#bigint"]
callArgs = `borrow --receiver_id ${call.value.receiver_id} --amount ${amount}`
break
}

case 'UpdateFeeRewards':
signer = call.value.addr
signer = accounts[call.value.addr]
callArgs = `update_fee_rewards --addr ${signer}`
break

Expand All @@ -68,6 +86,8 @@ switch (callType) {
process.exit(1)
}

assert(signer !== undefined, 'signer is undefined')

// produce the command for stellar-cli
const cmd =
`stellar contract invoke --id ${state.contractId} --source ${signer} --network ${network} -- ${callArgs}`
Expand Down
17 changes: 17 additions & 0 deletions solarkraft/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { verify } from './verify.js'
import { list } from './list.js'
import { SOLARKRAFT_DEFAULT_HOME } from './globals.js'
import { aggregate } from './aggregate.js'
import { extractAccounts } from './accounts.js'

// The default options present in every command
const defaultOpts = (yargs: any) =>
Expand Down Expand Up @@ -136,12 +137,28 @@ const listCmd = {
handler: list,
}

// accounts: construct an accounts mapping
const accountsCmd = {
command: ['accounts'],
desc: 'construct an accounts mapping, needed for input generation',
builder: (yargs: any) =>
defaultOpts(yargs)
.option('out', {
desc: 'The name of the file to output the accounts mapping to',
type: 'string',
require: false,
default: 'accounts.json',
}),
handler: extractAccounts,
}

function main() {
return yargs(process.argv.slice(2))
.command(fetchCmd)
.command(aggregateCmd)
.command(verifyCmd)
.command(listCmd)
.command(accountsCmd)
.demandCommand(1)
.version(version)
.strict()
Expand Down

0 comments on commit 77376b7

Please sign in to comment.