Skip to content

Commit

Permalink
feat(contract): resolve names on node side in Ceres
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 8, 2023
1 parent f3cc3bb commit 474f0fd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/contract/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { Encoder as Calldata } from '@aeternity/aepp-calldata';
import { DRY_RUN_ACCOUNT } from '../tx/builder/schema';
import { Tag, AensName } from '../tx/builder/constants';
import { Tag, AensName, ConsensusProtocolVersion } from '../tx/builder/constants';
import {
buildContractIdByContractTx, unpackTx, buildTxAsync, BuildTxOptions, buildTxHash,
} from '../tx/builder';
Expand Down Expand Up @@ -45,6 +45,7 @@ import {
import AccountBase from '../account/Base';
import { TxUnpacked } from '../tx/builder/schema.generated';
import { isAccountNotFoundError } from '../utils/other';
import { isNameValid, produceNameId } from '../tx/builder/helpers';

type ContractAci = NonNullable<Aci[0]['contract']>;
type FunctionAci = ContractAci['functions'][0];
Expand Down Expand Up @@ -308,7 +309,9 @@ class Contract<M extends ContractMethodsBase> {
): Promise<SendAndProcessReturnType & Partial<GetCallResultByHashReturnType<M, Fn>>> {
const { callStatic, top, ...opt } = { ...this.$options, ...options };
const fnAci = this.#getFunctionAci(fn);
const contractId = this.$options.address;
const { address, name } = this.$options;
// TODO: call `produceNameId` on buildTx side
const contractId = name != null ? produceNameId(name) : address;
const { onNode } = opt;

if (fn == null) throw new MissingFunctionNameError();
Expand Down Expand Up @@ -485,12 +488,16 @@ class Contract<M extends ContractMethodsBase> {
}
if (aci == null) throw new MissingContractDefError();

let name;
if (address != null) {
address = await resolveName(
address,
'contract_pubkey',
{ resolveByNode: true, onNode },
) as Encoded.ContractAddress;
const isIris = (await onNode.getNodeInfo())
.consensusProtocolVersion === ConsensusProtocolVersion.Iris;
if (!isIris && isNameValid(address)) name = address;
}

if (address == null && sourceCode == null && sourceCodePath == null && bytecode == null) {
Expand Down Expand Up @@ -527,6 +534,7 @@ class Contract<M extends ContractMethodsBase> {
bytecode,
aci,
address,
name,
fileSystem,
...otherOptions,
});
Expand All @@ -548,6 +556,10 @@ class Contract<M extends ContractMethodsBase> {
bytecode?: Encoded.ContractBytearray;
aci: Aci;
address?: Encoded.ContractAddress;
/**
* Supported only in Ceres
*/
name?: AensName;
sourceCodePath?: Parameters<CompilerBase['compile']>[0];
sourceCode?: Parameters<CompilerBase['compileBySourceCode']>[0];
fileSystem?: Parameters<CompilerBase['compileBySourceCode']>[1];
Expand Down
5 changes: 5 additions & 0 deletions src/tx/builder/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export const txSchema = [{
tag: shortUIntConst(Tag.SpendTx),
version: shortUIntConst(1, true),
senderId: address(Encoding.AccountAddress),
// TODO: accept also an AENS name
recipientId: address(Encoding.AccountAddress, Encoding.Name),
amount: coinAmount,
fee,
Expand Down Expand Up @@ -212,6 +213,7 @@ export const txSchema = [{
accountId: address(Encoding.AccountAddress),
nonce: nonce('accountId'),
nameId,
// TODO: accept also an AENS name
recipientId: address(Encoding.AccountAddress, Encoding.Name),
fee,
ttl,
Expand Down Expand Up @@ -258,6 +260,7 @@ export const txSchema = [{
version: shortUIntConst(1, true),
callerId: address(Encoding.AccountAddress),
nonce: nonce('callerId'),
// TODO: accept also an AENS name
contractId: address(Encoding.ContractAddress, Encoding.Name),
abiVersion,
fee,
Expand Down Expand Up @@ -306,6 +309,7 @@ export const txSchema = [{
}, {
tag: shortUIntConst(Tag.OracleExtendTx),
version: shortUIntConst(1, true),
// TODO: accept also an AENS name
oracleId: address(Encoding.OracleAddress, Encoding.Name),
nonce: nonce('oracleId'),
oracleTtlType: withDefault(ORACLE_TTL_TYPES.delta, enumeration(ORACLE_TTL_TYPES)),
Expand All @@ -317,6 +321,7 @@ export const txSchema = [{
version: shortUIntConst(1, true),
senderId: address(Encoding.AccountAddress),
nonce: nonce('senderId'),
// TODO: accept also an AENS name
oracleId: address(Encoding.OracleAddress, Encoding.Name),
query: string,
queryFee,
Expand Down
2 changes: 2 additions & 0 deletions src/tx/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ validators.push(
},
async (tx, { node }) => {
if (Tag.ContractCallTx !== tx.tag) return [];
// TODO: remove after solving https://github.com/aeternity/aeternity/issues/3669
if (tx.contractId.startsWith('nm_')) return [];
try {
const { active } = await node.getContract(tx.contractId);
if (active) return [];
Expand Down
3 changes: 2 additions & 1 deletion test/integration/aens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ describe('Aens', () => {
await nameObject.update({ contract_pubkey: contract.$options.address });

contract = await aeSdk.initializeContract<ContractApi>({ sourceCode, address: name });
expect((await contract.getArg(42)).decodedResult).to.be.equal(42n);
expect((await contract.getArg(42, { callStatic: true })).decodedResult).to.be.equal(42n);
expect((await contract.getArg(42, { callStatic: false })).decodedResult).to.be.equal(42n);
});

const address = generateKeyPair().publicKey;
Expand Down

0 comments on commit 474f0fd

Please sign in to comment.