Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support aptos GET restful api for inject window.starcoin in Dapps
  • Loading branch information
wk3368 committed Oct 23, 2022
1 parent 4e36c30 commit 598eeb3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
27 changes: 22 additions & 5 deletions src/networks/index.ts
Expand Up @@ -58,13 +58,30 @@ const STANDARD_NETWORKS: { [name: string]: Network } = {
name: 'main',
},
};
export function getNetwork(network: Networkish): Network {

const APTOS_NETWORKS: { [name: string]: Network } = {
test: {
chainId: 2,
name: 'testnet',
},
dev: {
chainId: 34,
name: 'devnet',
},
main: {
chainId: 1,
name: 'mainnet',
},
};

export function getNetwork(network: Networkish, isAptos = false): Network {
if (network == null) {
return null;
}
const NETWORKS = isAptos ? APTOS_NETWORKS : STANDARD_NETWORKS
if (typeof network === 'number') {
for (const name in STANDARD_NETWORKS) {
const standard = STANDARD_NETWORKS[name];
for (const name in NETWORKS) {
const standard = NETWORKS[name];
if (standard.chainId == network) {
return {
name: standard.name,
Expand All @@ -78,7 +95,7 @@ export function getNetwork(network: Networkish): Network {
name: 'unknown',
};
} else if (typeof network === 'string') {
const standard = STANDARD_NETWORKS[network];
const standard = NETWORKS[network];
if (standard == null) {
return null;
}
Expand All @@ -88,7 +105,7 @@ export function getNetwork(network: Networkish): Network {
_defaultProvider: standard._defaultProvider || null,
};
} else {
const standard = STANDARD_NETWORKS[network.name];
const standard = NETWORKS[network.name];
if (!standard) {
if (typeof network.chainId !== 'number') {
logger.throwArgumentError(
Expand Down
3 changes: 2 additions & 1 deletion src/providers/jsonrpc-provider.spec.ts
Expand Up @@ -14,14 +14,15 @@ describe('jsonrpc-provider', () => {
// const nodeUrl = 'https://barnard-seed.starcoin.org';
// const chainId = 251;
const nodeUrl = 'https://main-seed.starcoin.org';
// const nodeUrl = 'https://fullnode.mainnet.aptoslabs.com/v1/';
const chainId = 1;


const provider = new JsonRpcProvider(nodeUrl);
test('detectNetwork', async () => {
const net = await provider.getNetwork();
expect(net.chainId).toBe(chainId);
});
}, 10000);
test('getBlockNumber', async () => {
const blockNumber = await provider.getBlockNumber();
expect(provider.blockNumber).toBe(blockNumber);
Expand Down
64 changes: 63 additions & 1 deletion src/providers/jsonrpc-provider.ts
Expand Up @@ -438,7 +438,8 @@ export class JsonRpcProvider extends BaseProvider {

if (chainId != null) {
try {
return getNetwork(BigNumber.from(chainId).toNumber());
const isAptos = this.isAptos()
return getNetwork(BigNumber.from(chainId).toNumber(), isAptos);
} catch (error) {
return logger.throwError(
'could not detect network',
Expand Down Expand Up @@ -474,7 +475,68 @@ export class JsonRpcProvider extends BaseProvider {
return nodeInfo.now_seconds;
}

isAptos(): boolean {
return /aptoslabs\.com/.test(this.connection.url)
}

send(method: string, params: Array<any>): Promise<any> {
console.log('jsonRpcProvider send', { url: this.connection, method, params })
const isAptos = this.isAptos()
console.log({ isAptos })
// most aptos Rest APIs used in Dapp's injected window.starcoin is GET
if (isAptos) {
const request = null
let fetchUrl = this.connection.url // default for chain.id, chain.info
if (method === 'state.list_resource' || method === 'getAccountResources') {
fetchUrl = `${ fetchUrl }accounts/${ params[0] }/resources`
}
if (method === 'getAccount') {
fetchUrl = `${ fetchUrl }accounts/${ params[0] }`
}
if (method === 'getAccountResource') {
fetchUrl = `${ fetchUrl }accounts/${ params[0] }/resource/${ encodeURI(params[1]) }`
}
if (method === 'chain.get_transaction_info') {
fetchUrl = `${ fetchUrl }transactions/by_hash/${ params[0] }`
}
console.log({ fetchUrl, request })
return fetchJson(fetchUrl).then(
(data) => {
console.log({ data })
this.emit('debug', {
action: 'response',
request,
response: data,
provider: this,
});
let result: any
switch (method) {
case 'chain.id':
const network = this.connection.url.split('.')[1]
result = { id: data.chain_id, name: network }
break;
case 'chain.info':
result = { head: { number: Number(data.block_height) } }
break;
default:
result = data
}
return result;

},
(error) => {
this.emit('debug', {
action: 'response',
error,
request,
provider: this,
});

throw error;
}
);
}
// starcoin
const request = {
method,
params,
Expand Down

0 comments on commit 598eeb3

Please sign in to comment.