Skip to content

Commit

Permalink
fix(aepp): don't require connection to wallet to make a static call
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Oct 5, 2022
1 parent 4ea9698 commit bab9eee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/contract/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
AmbiguousEventDefinitionError,
UnexpectedTsError,
InternalError,
NoWalletConnectedError,
} from '../utils/errors';
import { hash as calcHash } from '../utils/crypto';
import { Aci as BaseAci } from '../apis/compiler';
Expand Down Expand Up @@ -357,11 +358,11 @@ class Contract<M extends ContractMethodsBase> {
try {
callerId = opt.onAccount.address;
} catch (error) {
const messageToSwallow = 'Account should be an address (ak-prefixed string), or instance of AccountBase, got undefined instead';
if (
opt.callStatic !== true || !(error instanceof TypeError)
|| error.message !== messageToSwallow
) throw error;
const useFallbackAccount = opt.callStatic === true && (
(error instanceof TypeError && error.message === 'Account should be an address (ak-prefixed string), or instance of AccountBase, got undefined instead')
|| (error instanceof NoWalletConnectedError)
);
if (!useFallbackAccount) throw error;
callerId = DRY_RUN_ACCOUNT.pub;
}
const callData = this._calldata.encode(this._name, fn, params);
Expand Down
23 changes: 22 additions & 1 deletion test/integration/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from 'mocha';
import { expect } from 'chai';
import {
AeSdk,
AeSdkAepp,
AeSdkWallet,
BrowserWindowMessageConnection,
Expand Down Expand Up @@ -102,11 +103,13 @@ describe('Aepp<->Wallet', function aeppWallet() {

describe('New RPC Wallet-AEPP: AEPP node', () => {
const keypair = generateKeyPair();
let aeSdk: AeSdk;
let aepp: AeSdkAepp;
let wallet: AeSdkWallet;

before(async () => {
[account] = Object.values((await getSdk()).accounts);
aeSdk = await getSdk();
[account] = Object.values(aeSdk.accounts);
wallet = new AeSdkWallet({
nodes: [{ name: 'local', instance: node }],
accounts: [account, MemoryAccount.generate()],
Expand All @@ -127,6 +130,24 @@ describe('Aepp<->Wallet', function aeppWallet() {
});
});

it('aepp can do static calls without connection to wallet', async () => {
const contract = await aeSdk.initializeContract({
sourceCode: 'contract Test =\n'
+ ' entrypoint getArg(x : int) = x',
});
await contract.$deploy([]);
const aeppSdk = new AeSdkAepp({
name: 'AEPP',
nodes: [{ name: 'test', instance: node }],
compilerUrl: aeSdk._options.compilerUrl,
});
const contractAepp = await aeppSdk.initializeContract<{ getArg: (a: number) => number }>({
aci: contract._aci,
address: contract.$options.address,
});
expect((await contractAepp.getArg(42)).decodedResult).to.be.equal(42n);
});

it('Fail on not connected', async () => {
await Promise.all([
aepp.subscribeAddress(SUBSCRIPTION_TYPES.subscribe, 'current'),
Expand Down

0 comments on commit bab9eee

Please sign in to comment.