-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from Concordium/ccd-js-gen-schema-param
Use schema information when generating client
- Loading branch information
Showing
31 changed files
with
2,430 additions
and
418 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { credentials } from '@grpc/grpc-js'; | ||
import * as SDK from '@concordium/web-sdk'; | ||
import { ConcordiumGRPCNodeClient } from '@concordium/web-sdk/nodejs'; | ||
import meow from 'meow'; | ||
import { parseEndpoint } from '../../shared/util.js'; | ||
|
||
// The generated module could be imported directly like below, | ||
// but for this example it is imported dynamicly to improve | ||
// the error message when not generated. | ||
// import * as wCCDContractClient from './lib/cis2_wCCD'; | ||
|
||
const cli = meow( | ||
` | ||
This example uses a generated smart contract client for the wCCD smart contract. | ||
Usage | ||
$ yarn run-example <path-to-this-file> [options] | ||
Required | ||
--index, -i The index of the smart contract. Defaults to 2059, which is wCCD on testnet. | ||
Options | ||
--help, -h Displays this message | ||
--endpoint, -e Specify endpoint of a grpc2 interface of a Concordium node in the format "<scheme>://<address>:<port>". Defaults to 'http://localhost:20000' | ||
--subindex, The subindex of the smart contract. Defaults to 0 | ||
`, | ||
{ | ||
importMeta: import.meta, | ||
flags: { | ||
endpoint: { | ||
type: 'string', | ||
alias: 'e', | ||
default: 'http://localhost:20000', | ||
}, | ||
index: { | ||
type: 'number', | ||
alias: 'i', | ||
default: 2059, | ||
}, | ||
subindex: { | ||
type: 'number', | ||
default: 0, | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
const [address, port, scheme] = parseEndpoint(cli.flags.endpoint); | ||
const grpcClient = new ConcordiumGRPCNodeClient( | ||
address, | ||
Number(port), | ||
scheme === 'https' ? credentials.createSsl() : credentials.createInsecure() | ||
); | ||
|
||
const contractAddress = SDK.ContractAddress.create( | ||
cli.flags.index, | ||
cli.flags.subindex | ||
); | ||
|
||
(async () => { | ||
// Importing the generated smart contract module client. | ||
/* eslint-disable import/no-unresolved */ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const wCCDContractClient = await import('./lib/wCCD_cis2_wCCD.js').catch( | ||
(e) => { | ||
/* eslint-enable import/no-unresolved */ | ||
console.error( | ||
'\nFailed to load the generated wCCD module, did you run the `generate` script?\n' | ||
); | ||
throw e; | ||
} | ||
); | ||
|
||
const wCCDTokenId = ''; | ||
const fromAddress = SDK.AccountAddress.fromBuffer( | ||
new Uint8Array(32).fill(0) | ||
); | ||
const toAddress = SDK.AccountAddress.fromBuffer(new Uint8Array(32).fill(1)); | ||
const parameter = [ | ||
{ | ||
token_id: wCCDTokenId, | ||
amount: 1000, | ||
from: { type: 'Account', content: fromAddress }, | ||
to: { type: 'Account', content: toAddress }, | ||
data: '', | ||
} as const, | ||
]; | ||
const contract = await wCCDContractClient.create( | ||
grpcClient, | ||
contractAddress | ||
); | ||
|
||
const unauthorizedInvoker = SDK.AccountAddress.fromBase58( | ||
'357EYHqrmMiJBmUZTVG5FuaMq4soAhgtgz6XNEAJaXHW3NHaUf' | ||
); | ||
|
||
const result = await wCCDContractClient.dryRunTransfer( | ||
contract, | ||
parameter, | ||
{ | ||
invoker: unauthorizedInvoker, | ||
} | ||
); | ||
const errorMessage = wCCDContractClient.parseErrorMessageTransfer(result); | ||
console.log('Transfer failed with error: ', errorMessage); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { credentials } from '@grpc/grpc-js'; | ||
import { ConcordiumGRPCNodeClient } from '@concordium/web-sdk/nodejs'; | ||
import * as SDK from '@concordium/web-sdk'; | ||
import meow from 'meow'; | ||
import { parseEndpoint } from '../../shared/util.js'; | ||
|
||
// The generated module could be imported directly like below, | ||
// but for this example it is imported dynamicly to improve | ||
// the error message when not generated. | ||
// import * as wCCDContractClient from './lib/cis2_wCCD.js'; | ||
|
||
const cli = meow( | ||
` | ||
This example uses a generated smart contract client for the wCCD smart contract to display events. | ||
Usage | ||
$ yarn run-example <path-to-this-file> [options] | ||
Required | ||
--index, -i The index of the smart contract. Defaults to 2059, which is wCCD on testnet. | ||
Options | ||
--help, -h Displays this message | ||
--endpoint, -e Specify endpoint of a grpc2 interface of a Concordium node in the format "<scheme>://<address>:<port>". Defaults to 'http://localhost:20000' | ||
--subindex, The subindex of the smart contract. Defaults to 0 | ||
`, | ||
{ | ||
importMeta: import.meta, | ||
flags: { | ||
endpoint: { | ||
type: 'string', | ||
alias: 'e', | ||
default: 'http://localhost:20000', | ||
}, | ||
index: { | ||
type: 'number', | ||
alias: 'i', | ||
default: 2059, | ||
}, | ||
subindex: { | ||
type: 'number', | ||
default: 0, | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
const [address, port, scheme] = parseEndpoint(cli.flags.endpoint); | ||
const grpcClient = new ConcordiumGRPCNodeClient( | ||
address, | ||
Number(port), | ||
scheme === 'https' ? credentials.createSsl() : credentials.createInsecure() | ||
); | ||
|
||
const contractAddress = SDK.ContractAddress.create( | ||
cli.flags.index, | ||
cli.flags.subindex | ||
); | ||
|
||
(async () => { | ||
// Importing the generated smart contract module client. | ||
/* eslint-disable import/no-unresolved */ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const wCCDContractClient = await import('./lib/wCCD_cis2_wCCD.js').catch( | ||
(e) => { | ||
/* eslint-enable import/no-unresolved */ | ||
console.error( | ||
'\nFailed to load the generated wCCD module, did you run the `generate` script?\n' | ||
); | ||
throw e; | ||
} | ||
); | ||
|
||
// The sender of the transaction, i.e the one updating an operator. | ||
const senderAccount = SDK.AccountAddress.fromBase58( | ||
'357EYHqrmMiJBmUZTVG5FuaMq4soAhgtgz6XNEAJaXHW3NHaUf' | ||
); | ||
// The parameter adding the wCCD contract as an operator of sender. | ||
const parameter = [ | ||
{ | ||
update: { type: 'Add' }, | ||
operator: { type: 'Contract', content: contractAddress }, | ||
} as const, | ||
]; | ||
|
||
// The client for the wCCD contract | ||
const contract = await wCCDContractClient.create( | ||
grpcClient, | ||
contractAddress | ||
); | ||
|
||
// Dry run the update of operator. | ||
const result = await wCCDContractClient.dryRunUpdateOperator( | ||
contract, | ||
parameter, | ||
{ invoker: senderAccount } | ||
); | ||
if (result.tag !== 'success') { | ||
throw new Error('Unexpected failure'); | ||
} | ||
for (const traceEvent of result.events) { | ||
if ( | ||
traceEvent.tag === SDK.TransactionEventTag.Updated || | ||
traceEvent.tag === SDK.TransactionEventTag.Interrupted | ||
) { | ||
for (const contractEvent of traceEvent.events) { | ||
const parsed = wCCDContractClient.parseEvent(contractEvent); | ||
console.log(parsed); | ||
} | ||
} | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
#!/usr/bin/env node | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
#!/usr/bin/env -S node --no-warnings | ||
import { main } from '../lib/src/cli.js'; | ||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.