Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read a contract as State #50

Merged
merged 4 commits into from
May 10, 2024
Merged

Read a contract as State #50

merged 4 commits into from
May 10, 2024

Conversation

Kukovec
Copy link
Collaborator

@Kukovec Kukovec commented May 8, 2024

closes #49

Adds the following:

  • Methods to submit an RPC request for a subset of contract data fields
  • Response validation
  • XDR -> State conversion
  • Test implementing the tutorial

Copy link
Contributor

@konnov konnov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only a few small comments. Apart from that, the code seems to be doing what it is supposed to do. It should be good enough for MVP. Let's merge it

Comment on lines +44 to +62
function isResponse(data: unknown): boolean {
if (typeof data === 'object' && data !== null) {
const maybe = data as Response
if (typeof maybe['jsonrpc'] !== 'string') return false
if (typeof maybe['id'] !== 'number') return false
const res = maybe['result']
if (typeof res !== 'object') return false
if (typeof res['latestLedger'] !== 'number') return false
const entries = res['entries']
if (!Array.isArray(entries)) return false

return !entries.some((e) => {
if (typeof e['key'] !== 'string') return true
if (typeof e['xdr'] !== 'string') return true
if (typeof e['lastModifiedLedgerSeq'] !== 'number') return true
return false
})
} else return false
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this could be rewritten, but this is definitely not a show-stopper for stopping this PR from being merged

new xdr.LedgerKeyContractData({
contract: new Address(contractId).toScAddress(),
key: xdr.ScVal.scvSymbol(symbolText),
durability: xdr.ContractDataDurability.persistent(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note: this method only works for persistent fields. Perhaps, it's worth adding a comment

return V.i64((v.value() as xdr.Int64).toBigInt())
case xdr.ScValType.scvU128(): {
const cast = v.value() as xdr.UInt128Parts
const hiInt = cast.hi().toBigInt() * 2n ** 64n
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const hiInt = cast.hi().toBigInt() * 2n ** 64n
const hiInt = cast.hi().toBigInt() * (2n ** 64n)

There is no bug here. I had to lookup the precedence of * and **, so it would be better to have it explicit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had parenthesis initially, and I think the liter or the formatter actually removed them

@konnov
Copy link
Contributor

konnov commented May 8, 2024

The test is failing though. @Kukovec do you know what is happening?

@Kukovec
Copy link
Collaborator Author

Kukovec commented May 8, 2024

The test is failing though. @Kukovec do you know what is happening?

No clue, it's not the actual tests in the code, but the solarkraft build itself. The error messages are pretty unhelpful ("Cannot find type definition file for 'urijs'."). Cannot reproduce locally.

@konnov
Copy link
Contributor

konnov commented May 8, 2024

The test is failing though. @Kukovec do you know what is happening?

No clue, it's not the actual tests in the code, but the solarkraft build itself. The error messages are pretty unhelpful ("Cannot find type definition file for 'urijs'."). Cannot reproduce locally.

it looks like you do not have @types/urijs maybe?

@konnov
Copy link
Contributor

konnov commented May 10, 2024

Looking at the reported error, I had to fix the missing dependency:

npm i @types/urijs

After doing that, I am getting exactly the same error message, as Github is reporting.

@konnov
Copy link
Contributor

konnov commented May 10, 2024

I guess, the issue is that we are compiling the project with nodejs, whereas stellar-sdk expect some types that are available only in a browser.

@konnov
Copy link
Contributor

konnov commented May 10, 2024

There were several reasons for the test not going through:

  1. The dependency @types/urijs missing.
  2. Another dependency eventsource mentioned in the failing node_modules/@stellar/stellar-sdk/types/dom-monkeypatch.d.ts.
  3. Finally, tsc had collisions on Event. I could not figure out how to fix that but by setting skipLibCheck to true in tsconfig.json.

@konnov
Copy link
Contributor

konnov commented May 10, 2024

Since the test is passing now, I will merge the PR, so we are not blocked with further development

@konnov konnov merged commit b3febef into main May 10, 2024
2 checks passed
@konnov konnov deleted the jk/xdr branch May 10, 2024 12:55
@konnov konnov added this to the M1: Transaction extractor milestone May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fetch contract data via the SDK
2 participants