-
Notifications
You must be signed in to change notification settings - Fork 12
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
feat(PE-5825): ANT read interface #19
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c941c96
feat(ant): add ANT read interface
1ba5db0
Merge remote-tracking branch 'origin/alpha' into PE-5825
014a262
fix(types): use string instead of any
d8d910b
fix(types): remove any types
53601c1
fix(types): add @ to records
93777db
chore(readme): adds ANT example in readme
d02276d
fix(signer): remove signer, will do in other pr
77235ce
fix(readme): update ant header
atticusofsparta 134319b
fix(build): remove redundant exported type
2296cc3
fix(ario): re-add contract default config
70c8520
fix(readme): update ANT usage description
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { | ||
ANTContract, | ||
ANTRecord, | ||
ANTState, | ||
ContractConfiguration, | ||
EvaluationOptions, | ||
EvaluationParameters, | ||
SmartWeaveContract, | ||
isContractConfiguration, | ||
isContractTxIdConfiguration, | ||
} from '../types.js'; | ||
import { RemoteContract } from './contracts/remote-contract.js'; | ||
|
||
export class ANT implements ANTContract { | ||
private contract: SmartWeaveContract<ANTState>; | ||
|
||
constructor(config: ContractConfiguration) { | ||
if (isContractConfiguration<ANTState>(config)) { | ||
this.contract = config.contract; | ||
} else if (isContractTxIdConfiguration(config)) { | ||
this.contract = new RemoteContract<ANTState>({ | ||
contractTxId: config.contractTxId, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the current state of the contract. | ||
*/ | ||
async getState(params: EvaluationParameters): Promise<ANTState> { | ||
const state = await this.contract.getContractState(params); | ||
return state; | ||
} | ||
|
||
async getRecord({ | ||
domain, | ||
evaluationOptions, | ||
}: EvaluationParameters<{ domain: string }>): Promise<ANTRecord> { | ||
const records = await this.getRecords({ evaluationOptions }); | ||
return records[domain]; | ||
} | ||
|
||
async getRecords({ | ||
evaluationOptions, | ||
}: { | ||
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined; | ||
}): Promise<Record<string, ANTRecord>> { | ||
const state = await this.contract.getContractState({ evaluationOptions }); | ||
return state.records; | ||
} | ||
|
||
async getOwner({ | ||
evaluationOptions, | ||
}: { | ||
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined; | ||
}): Promise<string> { | ||
const state = await this.contract.getContractState({ evaluationOptions }); | ||
return state.owner; | ||
} | ||
|
||
async getControllers({ | ||
evaluationOptions, | ||
}: { | ||
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined; | ||
}): Promise<string[]> { | ||
const state = await this.contract.getContractState({ evaluationOptions }); | ||
return state.controllers; | ||
} | ||
|
||
async getName({ | ||
evaluationOptions, | ||
}: { | ||
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined; | ||
}): Promise<string> { | ||
const state = await this.contract.getContractState({ evaluationOptions }); | ||
return state.name; | ||
} | ||
|
||
async getTicker({ | ||
evaluationOptions, | ||
}: { | ||
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined; | ||
}): Promise<string> { | ||
const state = await this.contract.getContractState({ evaluationOptions }); | ||
return state.ticker; | ||
} | ||
|
||
async getBalances({ | ||
evaluationOptions, | ||
}: { | ||
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined; | ||
}): Promise<Record<string, number>> { | ||
const state = await this.contract.getContractState({ evaluationOptions }); | ||
return state.balances; | ||
} | ||
|
||
async getBalance({ | ||
address, | ||
evaluationOptions, | ||
}: EvaluationParameters<{ address: string }>): Promise<number> { | ||
const balances = await this.getBalances({ evaluationOptions }); | ||
return balances[address]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,8 @@ | |
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { ARNS_TESTNET_REGISTRY_TX } from '../constants.js'; | ||
import { ArconnectSigner, ArweaveSigner } from 'arbundles'; | ||
|
||
import { | ||
ArIOContract, | ||
ArIOState, | ||
|
@@ -32,13 +33,17 @@ import { | |
import { RemoteContract } from './contracts/remote-contract.js'; | ||
|
||
// TODO: append this with other configuration options (e.g. local vs. remote evaluation) | ||
export type ContractConfiguration = | ||
export type ArIOSigner = ArweaveSigner | ArconnectSigner; | ||
export type ContractConfiguration = { | ||
signer?: ArIOSigner; // TODO: optionally allow JWK in place of signer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not for this PR 🥲 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. woops! |
||
} & ( | ||
| { | ||
contract?: SmartWeaveContract<unknown>; | ||
} | ||
| { | ||
contractTxId: string; | ||
}; | ||
} | ||
); | ||
|
||
function isContractConfiguration<T>( | ||
config: ContractConfiguration, | ||
|
@@ -54,21 +59,20 @@ function isContractTxIdConfiguration( | |
|
||
export class ArIO implements ArIOContract { | ||
private contract: SmartWeaveContract<ArIOState>; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
private signer: ArIOSigner | undefined; | ||
|
||
constructor( | ||
config: ContractConfiguration = { | ||
// default to a contract that uses the arns service to do the evaluation | ||
contract: new RemoteContract<ArIOState>({ | ||
contractTxId: ARNS_TESTNET_REGISTRY_TX, | ||
}), | ||
}, | ||
) { | ||
constructor({ signer, ...config }: ContractConfiguration) { | ||
this.signer = signer; | ||
if (isContractConfiguration<ArIOState>(config)) { | ||
this.contract = config.contract; | ||
} else if (isContractTxIdConfiguration(config)) { | ||
this.contract = new RemoteContract<ArIOState>({ | ||
contractTxId: config.contractTxId, | ||
}); | ||
} else { | ||
throw new Error('Invalid configuration.'); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { ANT } from '../src/common/ant'; | ||
import { RemoteContract } from '../src/common/contracts/remote-contract'; | ||
import { ANTState } from '../src/contract-state'; | ||
|
||
describe('ANT contract apis', () => { | ||
const ant = new ANT({ | ||
contract: new RemoteContract<ANTState>({ | ||
url: process.env.REMOTE_CACHE_URL || 'http://localhost:3000', | ||
contractTxId: 'UC2zwawQoTnh0TNd9mYLQS4wObBBeaOU5LPQTNETqA4', | ||
dtfiedler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}), | ||
}); | ||
|
||
const sortKey = | ||
'000001383961,0000000000000,13987aba2d71b6229989690c15d2838a4deef0a90c3fc9e4d7227ed17e35d0bd'; | ||
const blockHeight = 1383961; | ||
|
||
it.each([[{ sortKey }], [{ blockHeight }]])( | ||
`should get contract state with evaluation options: ${JSON.stringify('%s')}`, | ||
async (evalTo) => { | ||
const state = await ant.getState({ evaluationOptions: { evalTo } }); | ||
expect(state).toBeDefined(); | ||
}, | ||
); | ||
|
||
it.each([[{ sortKey }], [{ blockHeight }]])( | ||
`should get record: ${JSON.stringify('%s')}`, | ||
async (evalTo) => { | ||
const record = await ant.getRecord({ | ||
domain: '@', | ||
evaluationOptions: { evalTo }, | ||
}); | ||
expect(record).toBeDefined(); | ||
}, | ||
); | ||
|
||
it.each([[{ sortKey }], [{ blockHeight }]])( | ||
`should get records with evaluation options: ${JSON.stringify('%s')}`, | ||
async (evalTo) => { | ||
const records = await ant.getRecords({ evaluationOptions: { evalTo } }); | ||
console.dir({ records: records['@'] }, { depth: 4 }); | ||
expect(records).toBeDefined(); | ||
}, | ||
); | ||
|
||
it.each([[{ sortKey }], [{ blockHeight }]])( | ||
`should get owner with evaluation options: ${JSON.stringify('%s')}`, | ||
async (evalTo) => { | ||
const owner = await ant.getOwner({ evaluationOptions: { evalTo } }); | ||
expect(owner).toBeDefined(); | ||
}, | ||
); | ||
|
||
it.each([[{ sortKey }], [{ blockHeight }]])( | ||
`should get controllers with evaluation options: ${JSON.stringify('%s')}`, | ||
async (evalTo) => { | ||
const controllers = await ant.getControllers({ | ||
evaluationOptions: { evalTo }, | ||
}); | ||
expect(controllers).toBeDefined(); | ||
}, | ||
); | ||
|
||
it.each([[{ sortKey }], [{ blockHeight }]])( | ||
`should get name with evaluation options: ${JSON.stringify('%s')}`, | ||
async (evalTo) => { | ||
const state = await ant.getName({ evaluationOptions: { evalTo } }); | ||
expect(state).toBeDefined(); | ||
}, | ||
); | ||
|
||
it.each([[{ sortKey }], [{ blockHeight }]])( | ||
`should get ticker with evaluation options: ${JSON.stringify('%s')}`, | ||
async (evalTo) => { | ||
const state = await ant.getTicker({ evaluationOptions: { evalTo } }); | ||
expect(state).toBeDefined(); | ||
}, | ||
); | ||
|
||
it.each([[{ sortKey }], [{ blockHeight }]])( | ||
`should get balances with evaluation options: ${JSON.stringify('%s')}`, | ||
async (evalTo) => { | ||
const state = await ant.getBalances({ evaluationOptions: { evalTo } }); | ||
expect(state).toBeDefined(); | ||
}, | ||
); | ||
|
||
it.each([[{ sortKey }], [{ blockHeight }]])( | ||
`should get balance with evaluation options: ${JSON.stringify('%s')}`, | ||
async (evalTo) => { | ||
const state = await ant.getBalance({ | ||
address: 'TRVCopHzzO1VSwRUUS8umkiO2MpAL53XtVGlLaJuI94', | ||
evaluationOptions: { evalTo }, | ||
}); | ||
expect(state).toBeDefined(); | ||
}, | ||
); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can think about creating an abstract
Contract
class that both this andArIO
inherit - to avoid the duplicate constructor code - but no need for now, we'll stick with the composition and type patternThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an example of how an abstract class works
and then
just showing for how it could work - but again, not necessary to implement