Skip to content

Commit

Permalink
Merge 1c9eabf into f3ae6f3
Browse files Browse the repository at this point in the history
  • Loading branch information
keefertaylor committed Dec 10, 2020
2 parents f3ae6f3 + 1c9eabf commit 04f69be
Show file tree
Hide file tree
Showing 2 changed files with 56 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
});
});
34 changes: 30 additions & 4 deletions src/chain/tezos/TezosNodeReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ export namespace TezosNodeReader {
});
}

/**
* Gets the delegate for a smart contract or an implicit account.
*
* @param {string} server Tezos node to query
* @param {stirng} address The smart contract address or implicit account to query.
*/
export async function getDelegate(server: string, address: string): Promise<string | undefined> {
const requestUrl = `chains/main/blocks/head/context/contracts/${address}/delegate`

try {
const delegate = await performGetRequest(server, requestUrl)
// Delegate is a string, even though `performGetRequest` purports to return an object.
return (delegate as unknown) as string
} catch (error) {
const tezosRequestError = error as TezosRequestError

// Tezos returns a 404 if delegate is not set.
if (tezosRequestError.httpStatus === 404) {
return undefined
}

// Otherwise, re-throw the error.
throw tezosRequestError
}
}

/**
* Gets a block for a given hash.
*
Expand All @@ -46,7 +72,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 +96,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 +110,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 +137,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 04f69be

Please sign in to comment.