Skip to content

Commit

Permalink
Merge e9bbe0e into f3ae6f3
Browse files Browse the repository at this point in the history
  • Loading branch information
keefertaylor committed Dec 10, 2020
2 parents f3ae6f3 + e9bbe0e commit ea79ffb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
26 changes: 26 additions & 0 deletions integration_test/chain/tezos/TezosNodeReader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,30 @@ describe('TezosNodeReader integration test suite', () => {

expect(result.header.level).to.be.greaterThan(1);
});

it('Gets delegate for a delegated implicit account', async () => {
const result = await TezosNodeReader.getDelegate(tezosServer, "tz1PnUd6R31MnjEE8VhfZhZdbGc1hrWQvjnK");
expect(result).to.not.be.undefined
});

it('Gets delegate for a delegated smart contract', async () => {
const result = await TezosNodeReader.getDelegate(tezosServer, "KT1DRJPyaDTgeXrM2cgQdp5siNF8PP5RLS7T");
expect(result).to.not.be.undefined
});

it('Gets delegate for a baker as itself', async () => {
const baker = "tz1Na5QB98cDA3BC1SQU4w3iiWGVGktU14LE"
const result = await TezosNodeReader.getDelegate(tezosServer, baker);
expect(result).to.be.equal(baker)
});

it('Returns undefined for undelegated implicit account', async () => {
const result = await TezosNodeReader.getDelegate(tezosServer, "tz1fzHtv2UqtXzFUBHuBPh2xXVv5Pv5MTh5Z");
expect(result).to.be.undefined
});

it('Returns undefined for undelegated smart contract', async () => {
const result = await TezosNodeReader.getDelegate(tezosServer, "KT1BipUDR93YFCJjVpghzVFS8N45Lkgigfqs");
expect(result).to.be.undefined
});
});
20 changes: 16 additions & 4 deletions src/chain/tezos/TezosNodeReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ export namespace TezosNodeReader {
});
}

/**
* Gets the delegate for a smart contract or an implicit account.
*
* @param {string} server Tezos node to query
* @param {string} accountHash The smart contract address or implicit account to query.
* @returns The address of the delegate, or undefined if there was no delegate set.
*/
export async function getDelegate(server: string, accountHash: string): Promise<string | undefined> {
const contractData = await getAccountForBlock(server, 'head', accountHash)
return (contractData.delegate as unknown) as string
}

/**
* Gets a block for a given hash.
*
Expand All @@ -46,7 +58,7 @@ export namespace TezosNodeReader {
* @returns {Promise<TezosRPCTypes.TezosBlock>} Block
*/
export function getBlock(server: string, hash: string = 'head', chainid: string = 'main'): Promise<TezosRPCTypes.TezosBlock> {
return performGetRequest(server, `chains/${chainid}/blocks/${hash}`).then(json => { return <TezosRPCTypes.TezosBlock> json });
return performGetRequest(server, `chains/${chainid}/blocks/${hash}`).then(json => { return <TezosRPCTypes.TezosBlock>json });
}

/**
Expand All @@ -70,7 +82,7 @@ export namespace TezosNodeReader {
if (offset <= 0) { return getBlock(server); }

const head = await getBlock(server);
return performGetRequest(server, `chains/${chainid}/blocks/${Number(head['header']['level']) - offset}`).then(json => { return <TezosRPCTypes.TezosBlock> json });
return performGetRequest(server, `chains/${chainid}/blocks/${Number(head['header']['level']) - offset}`).then(json => { return <TezosRPCTypes.TezosBlock>json });
}

/**
Expand All @@ -84,7 +96,7 @@ export namespace TezosNodeReader {
*/
export function getAccountForBlock(server: string, blockHash: string, accountHash: string, chainid: string = 'main'): Promise<TezosRPCTypes.Contract> {
return performGetRequest(server, `chains/${chainid}/blocks/${blockHash}/context/contracts/${accountHash}`)
.then(json => <TezosRPCTypes.Contract> json);
.then(json => <TezosRPCTypes.Contract>json);
}

/**
Expand All @@ -111,7 +123,7 @@ export namespace TezosNodeReader {
*/
export async function getSpendableBalanceForAccount(server: string, accountHash: string, chainid: string = 'main'): Promise<number> {
const account = await performGetRequest(server, `chains/${chainid}/blocks/head/context/contracts/${accountHash}`) // TODO: get /balance
.then(json => <TezosRPCTypes.Contract> json);
.then(json => <TezosRPCTypes.Contract>json);
return parseInt(account.balance.toString(), 10);
}

Expand Down

0 comments on commit ea79ffb

Please sign in to comment.