Skip to content

Commit

Permalink
feat: restore ability to specify array of accounts in AeSdk constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Aug 6, 2022
1 parent f142ef9 commit aba9b9f
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 49 deletions.
6 changes: 2 additions & 4 deletions docs/guides/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ const node = new Node('https://testnet.aeternity.io') // ideally host your own n
const account = new MemoryAccount(SECRET_KEY)

const aeSdk = new AeSdk({
nodes: [
{ name: 'testnet', instance: node }
],
nodes: [{ name: 'testnet', instance: node }],
accounts: [account],
compilerUrl: 'https://compiler.aepps.com', // ideally host your own compiler
})
aeSdk.addAccount(accoount, { select: true })
```

Note:
Expand Down
3 changes: 1 addition & 2 deletions docs/guides/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,8 @@ const newUserAccount = MemoryAccount.generate()
const node = new Node('https://testnet.aeternity.io')
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [payerAccount, newUserAccount],
})
aeSdk.addAccount(payerAccount, { select: true })
aeSdk.addAccount(newUserAccount)

// catch exceptions
try {
Expand Down
8 changes: 4 additions & 4 deletions docs/guides/low-vs-high-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ async function init () {
const node = new Node('https://testnet.aeternity.io') // ideally host your own node!

const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }]
nodes: [{ name: 'testnet', instance: node }],
accounts: [new MemoryAccount('<SECRET_KEY_HERE>')],
})
aeSdk.addAccount(new MemoryAccount('<SECRET_KEY_HERE>'), { select: true })

// log transaction info
console.log(await aeSdk.spend(100, 'ak_...'))
Expand All @@ -41,9 +41,9 @@ import { MemoryAccount, Node, AeSdk, Tag } from '@aeternity/aepp-sdk'
async function spend (amount, recipient) {
const node = new Node('https://testnet.aeternity.io') // ideally host your own node!
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }]
nodes: [{ name: 'testnet', instance: node }],
accounts: [new MemoryAccount('<SECRET_KEY_HERE>')],
})
aeSdk.addAccount(new MemoryAccount('<SECRET_KEY_HERE>'), { select: true })

// builds an unsigned SpendTx using integrated transaction builder
const spendTx = await aeSdk.buildTx(Tag.SpendTx, {
Expand Down
3 changes: 1 addition & 2 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ const senderAccount = new MemoryAccount('<SENDER_SECRET_KEY>');
const aeSdk = new AeSdk({
compilerUrl: COMPILER_URL,
nodes: [{ name: 'testnet', instance: node }],
accounts: [senderAccount],
})
// Add sender account to the aeSdk state
aeSdk.addAccount(senderAccount, { select: true })

// spend one AE
await aeSdk.spend(1, '<RECIPIENT_PUBLIC_KEY>', {
Expand Down
9 changes: 4 additions & 5 deletions examples/browser/wallet-iframe/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export default {
{ name: 'ae_uat', instance: new Node('https://testnet.aeternity.io') },
{ name: 'ae_mainnet', instance: new Node('https://mainnet.aeternity.io') },
],
accounts: [
new MemoryAccount('bf66e1c256931870908a649572ed0257876bb84e3cdf71efb12f56c7335fad54d5cf08400e988222f26eb4b02c8f89077457467211a6e6d955edb70749c6a33b'),
MemoryAccount.generate(),
],
compilerUrl: 'https://compiler.aepps.com',
name: 'Wallet Iframe',
onConnection: (aeppId, params) => {
Expand All @@ -101,11 +105,6 @@ export default {
this.shareWalletInfo(clientId)
}
})
this.aeSdk.addAccount(
new MemoryAccount('bf66e1c256931870908a649572ed0257876bb84e3cdf71efb12f56c7335fad54d5cf08400e988222f26eb4b02c8f89077457467211a6e6d955edb70749c6a33b'),
{ select: true },
)
this.aeSdk.addAccount(MemoryAccount.generate())
this.nodeName = this.aeSdk.selectedNodeName
this.address = this.aeSdk.addresses()[0]
Expand Down
9 changes: 4 additions & 5 deletions examples/browser/wallet-web-extension/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const aeSdk = new AeSdkWallet({
name: 'testnet',
instance: new Node('https://testnet.aeternity.io'),
}],
accounts: [
new MemoryAccount('bf66e1c256931870908a649572ed0257876bb84e3cdf71efb12f56c7335fad54d5cf08400e988222f26eb4b02c8f89077457467211a6e6d955edb70749c6a33b'),
MemoryAccount.generate(),
],
id: browser.runtime.id,
type: WALLET_TYPE.extension,
name: 'Wallet WebExtension',
Expand Down Expand Up @@ -52,11 +56,6 @@ const aeSdk = new AeSdkWallet({
});
// The `ExtensionProvider` uses the first account by default.
// You can change active account using `selectAccount(address)` function
aeSdk.addAccount(
new MemoryAccount('bf66e1c256931870908a649572ed0257876bb84e3cdf71efb12f56c7335fad54d5cf08400e988222f26eb4b02c8f89077457467211a6e6d955edb70749c6a33b'),
{ select: true },
);
aeSdk.addAccount(MemoryAccount.generate());

browser.runtime.onConnect.addListener((port) => {
// create connection
Expand Down
2 changes: 1 addition & 1 deletion examples/node/contract-interaction.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ const account = new MemoryAccount(ACCOUNT_SECRET_KEY);
const node = new Node(NODE_URL);
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [account],
compilerUrl: COMPILER_URL,
});
aeSdk.addAccount(account, { select: true });

// ## 4. Get contract instance
// Knowing the source code allows you to initialize a contract instance and interact with the
Expand Down
3 changes: 1 addition & 2 deletions examples/node/paying-for-contract-call-tx.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ const newUserAccount = MemoryAccount.generate();
const node = new Node(NODE_URL);
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [payerAccount, newUserAccount],
compilerUrl: COMPILER_URL,
});
aeSdk.addAccount(payerAccount, { select: true });
aeSdk.addAccount(newUserAccount);

// ## 4. Create and sign `ContractCallTx` on behalf of new user
// Currently there is no high-level API available that allows you to create and sign the
Expand Down
3 changes: 1 addition & 2 deletions examples/node/paying-for-spend-tx.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ const newUserAccount = MemoryAccount.generate();
const node = new Node(NODE_URL);
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [payerAccount, newUserAccount],
});
aeSdk.addAccount(payerAccount, { select: true });
aeSdk.addAccount(newUserAccount);

// ## 4. Send 1 `aetto` from payer to new user
const spendTxResult = await aeSdk.spend(
Expand Down
2 changes: 1 addition & 1 deletion examples/node/transfer-ae.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ const account = new MemoryAccount(ACCOUNT_KEYPAIR.secretKey);
const node = new Node(NODE_URL);
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [account],
});
aeSdk.addAccount(account, { select: true });

// ## 4. Get AE balance of recipient (before transfer)
// Before the transfer of AE you can check the AE balance of the recipient.
Expand Down
8 changes: 8 additions & 0 deletions src/AeSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export default class AeSdk extends AeSdkBase {

selectedAddress?: Encoded.AccountAddress;

constructor(
{ accounts, ...options }: { accounts?: AccountBase[] }
& ConstructorParameters<typeof AeSdkBase>[0] = {},
) {
super(options);
accounts?.forEach((account, idx) => this.addAccount(account, { select: idx === 0 }));
}

override _resolveAccount(
account: Account | Encoded.AccountAddress = this.selectedAddress,
): AccountBase {
Expand Down
4 changes: 2 additions & 2 deletions src/AeSdkWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class AeSdkWallet extends AeSdk {

name: string;

_clients: Map<string, RpcClientsInfo>;
_clients = new Map<string, RpcClientsInfo>();

onConnection: OnConnection;

Expand Down Expand Up @@ -122,13 +122,13 @@ export default class AeSdkWallet extends AeSdk {
this.onDisconnect = onDisconnect;
this.onAskAccounts = onAskAccounts;
this.onMessageSign = onMessageSign;
this._clients = new Map();
this.name = name;
this.id = id;
this._type = type;
}

_pushAccountsToApps(): void {
if (this._clients == null) return;
Array.from(this._clients.keys())
.filter((clientId) => this._isRpcClientSubscribed(clientId))
.map((clientId) => this._getClient(clientId).rpc)
Expand Down
7 changes: 3 additions & 4 deletions test/environment/browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
const node = new Node('https://testnet.aeternity.io');
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [
new MemoryAccount('bf66e1c256931870908a649572ed0257876bb84e3cdf71efb12f56c7335fad54d5cf08400e988222f26eb4b02c8f89077457467211a6e6d955edb70749c6a33b'),
],
compilerUrl: 'https://compiler.aepps.com'
});
aeSdk.addAccount(
new MemoryAccount('bf66e1c256931870908a649572ed0257876bb84e3cdf71efb12f56c7335fad54d5cf08400e988222f26eb4b02c8f89077457467211a6e6d955edb70749c6a33b'),
{ select: true },
);

(async () => {
console.log('Height:', await aeSdk.getHeight());
Expand Down
7 changes: 3 additions & 4 deletions test/environment/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ contract Test =
const node = new Node('https://testnet.aeternity.io');
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [
new MemoryAccount('bf66e1c256931870908a649572ed0257876bb84e3cdf71efb12f56c7335fad54d5cf08400e988222f26eb4b02c8f89077457467211a6e6d955edb70749c6a33b'),
],
compilerUrl: 'https://compiler.aepps.com',
});
aeSdk.addAccount(
new MemoryAccount('bf66e1c256931870908a649572ed0257876bb84e3cdf71efb12f56c7335fad54d5cf08400e988222f26eb4b02c8f89077457467211a6e6d955edb70749c6a33b'),
{ select: true },
);

(async () => {
console.log('Height:', await aeSdk.getHeight());
Expand Down
4 changes: 2 additions & 2 deletions test/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ export const ignoreVersion = process.env.IGNORE_VERSION === 'true';
const genesisAccount = new MemoryAccount(secretKey);

export async function getSdk(accountCount = 1): Promise<AeSdk> {
const accounts = new Array(accountCount).fill(null).map(() => MemoryAccount.generate());
const sdk = new AeSdk({
compilerUrl,
ignoreVersion,
nodes: [{ name: 'test', instance: new Node(url, { ignoreVersion }) }],
accounts,
_expectedMineRate: 1000,
_microBlockCycle: 300,
});
await sdk.awaitHeight(2);
const accounts = new Array(accountCount).fill(null).map(() => MemoryAccount.generate());
for (let i = 0; i < accounts.length; i += 1) {
await sdk.spend(1e32, accounts[i].address, { onAccount: genesisAccount });
sdk.addAccount(accounts[i], { select: i === 0 });
}
return sdk;
}
9 changes: 3 additions & 6 deletions test/integration/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
decode, postQueryToOracle, registerOracle,
ORACLE_TTL_TYPES, QUERY_FEE,
} from '../../src';
import MemoryAccount from '../../src/account/Memory';
import { Encoded } from '../../src/utils/encoder';

describe('Oracle', () => {
Expand All @@ -33,7 +32,7 @@ describe('Oracle', () => {
const queryResponse = "{'tmp': 30}";

before(async () => {
aeSdk = await getSdk(1);
aeSdk = await getSdk(2);
});

it('Register Oracle with 5000 TTL', async () => {
Expand Down Expand Up @@ -88,10 +87,8 @@ describe('Oracle', () => {
const queryFee = 24000n;

before(async () => {
const account = MemoryAccount.generate();
await aeSdk.spend(1e15, account.address);
aeSdk.addAccount(account, { select: true });
oracleWithFee = await aeSdk.registerOracle("{'city': str}", "{'tmp': num}", { queryFee: queryFee.toString(), onAccount: account });
await aeSdk.selectAccount(aeSdk.addresses()[1]);
oracleWithFee = await aeSdk.registerOracle("{'city': str}", "{'tmp': num}", { queryFee: queryFee.toString() });
});

it('Post Oracle Query with default query fee', async () => {
Expand Down
5 changes: 2 additions & 3 deletions test/integration/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ describe('Aepp<->Wallet', function aeppWallet() {
[account] = Object.values((await getSdk()).accounts);
wallet = new AeSdkWallet({
nodes: [{ name: 'local', instance: node }],
accounts: [account, MemoryAccount.generate()],
id: 'test',
type: WALLET_TYPE.window,
name: 'Wallet',
Expand All @@ -120,8 +121,6 @@ describe('Aepp<->Wallet', function aeppWallet() {
onMessageSign: handlerRejectPromise,
onDisconnect() {},
});
wallet.addAccount(account, { select: true });
wallet.addAccount(MemoryAccount.generate());
aepp = new AeSdkAepp({
name: 'AEPP',
nodes: [{ name: 'test', instance: node }],
Expand Down Expand Up @@ -474,6 +473,7 @@ describe('Aepp<->Wallet', function aeppWallet() {
before(async () => {
wallet = new AeSdkWallet({
nodes: [{ name: 'local', instance: node }],
accounts: [account],
id: 'test',
type: WALLET_TYPE.window,
name: 'Wallet',
Expand All @@ -484,7 +484,6 @@ describe('Aepp<->Wallet', function aeppWallet() {
onMessageSign: handlerRejectPromise,
onDisconnect() {},
});
wallet.addAccount(account, { select: true });
aepp = new AeSdkAepp({
name: 'AEPP',
onNetworkChange() {},
Expand Down

0 comments on commit aba9b9f

Please sign in to comment.