Skip to content

Commit

Permalink
Merge branch 'remove-grpc-v1' into platform-agnostic-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenbf committed Oct 2, 2023
2 parents 077c25a + b9bd3b1 commit 0beaf38
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 199 deletions.
3 changes: 1 addition & 2 deletions examples/ccd-js-gen/wCCD/client-tokenMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ const contractAddress = SDK.ContractAddress.create(
throw e;
});

const wCCDTokenId = '';
const parameter = [wCCDTokenId];
const parameter = SDK.Parameter.fromHexString('010000'); // First 2 bytes for number of tokens to query, 1 byte for the token ID.
const contract = await wCCDContractClient.create(
grpcClient,
contractAddress
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Several types have been replaced with a module containing the type itself togeth
- Removed `JsonRpcClient` and types and functionality associated solely with this class.

- Renamed `AccountSequenceNumber` module to `SequenceNumber`.
- Fix type for `TranferredEvent` from `ContractTraceEvent` to only be from contract addresses to account addresses.

### Added

Expand Down
24 changes: 12 additions & 12 deletions packages/sdk/src/GenericContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ export class ContractDryRun<E extends string = string> {
* @template T - The type of the input given
*
* @param {EntrypointName.Type} entrypoint - The name of the receive function to invoke.
* @param {ContractAddress | AccountAddress} invoker - The address of the invoker.
* @param {ContractAddress | AccountAddress.Type} invoker - The address of the invoker.
* @param {Function} serializer - A function for serializing the input to bytes.
* @param {T} input - Input for for contract function.
* @param {HexString} [blockHash] - The hash of the block to perform the invocation of. Defaults to the latest finalized block on chain.
* @param {BlockHash.Type} [blockHash] - The hash of the block to perform the invocation of. Defaults to the latest finalized block on chain.
*
* @returns {InvokeContractResult} the contract invocation result, which includes whether or not the invocation succeeded along with the energy spent.
*/
Expand Down Expand Up @@ -193,7 +193,7 @@ class ContractBase<E extends string = string, V extends string = string> {
* Helper function for getting the {@link InstanceInfo} of a contract
*
* @param {ConcordiumGRPCClient} grpcClient - The GRPC client for accessing a node.
* @param {ContractAddress} contractAddress - The address of the contract.
* @param {ContractAddress.Type} contractAddress - The address of the contract.
*
* @throws if the {@link InstanceInfo} of the contract could not be found.
*
Expand All @@ -218,11 +218,11 @@ class ContractBase<E extends string = string, V extends string = string> {
* Helper function for getting the name of a contract
*
* @param {ConcordiumGRPCClient} grpcClient - The GRPC client for accessing a node.
* @param {ContractAddress} contractAddress - The address of the contract.
* @param {ContractAddress.Type} contractAddress - The address of the contract.
*
* @throws if the {@link InstanceInfo} of the contract could not be found.
*
* @returns {string} the name of the contract.
* @returns {ContractName.Type} the name of the contract.
*/
protected static async getContractName(
grpcClient: ConcordiumGRPCClient,
Expand Down Expand Up @@ -394,7 +394,7 @@ class ContractBase<E extends string = string, V extends string = string> {
*
* @throws If the query could not be invoked successfully.
*
* @returns {HexString} The transaction hash of the update transaction
* @returns {TransactionHash.Type} The transaction hash of the update transaction
*/
protected async sendUpdateTransaction(
transactionBase: ContractUpdateTransaction,
Expand Down Expand Up @@ -425,15 +425,15 @@ class ContractBase<E extends string = string, V extends string = string> {
*
* @template T - The type of the input
*
* @param {string} entrypoint - The name of the receive function to invoke.
* @param {EntrypointName.Type} entrypoint - The name of the receive function to invoke.
* @param {Function} serializeInput - A function to serialize the `input` to bytes.
* @param {CIS2.TransactionMetadata} metadata - Metadata to be used for the transaction (with defaults).
* @param {T} input - Input for for contract function.
* @param {AccountSigner} signer - An object to use for signing the transaction.
*
* @throws If the query could not be invoked successfully.
*
* @returns {HexString} The transaction hash of the update transaction
* @returns {TransactionHash.Type} The transaction hash of the update transaction
*/
public async createAndSendUpdateTransaction<T>(
entrypoint: EntrypointName.Type<E>,
Expand All @@ -457,11 +457,11 @@ class ContractBase<E extends string = string, V extends string = string> {
* @template T - The type of the input
* @template R - The type the invocation response should be deserialized into.
*
* @param {string} entrypoint - The name of the view function to invoke.
* @param {EntrypointName.Type} entrypoint - The name of the view function to invoke.
* @param {Function} serializeInput - A function to serialize the `input` to bytes.
* @param {Function} deserializeResponse - A function to deserialize the value returned from the view invocation.
* @param {T | T[]} input - Input for for contract function.
* @param {HexString} [blockHash] - The hash of the block to perform the invocation of. Defaults to the latest finalized block on chain.
* @param {BlockHash.Type} [blockHash] - The hash of the block to perform the invocation of. Defaults to the latest finalized block on chain.
*
* @throws If the query could not be invoked successfully.
*
Expand Down Expand Up @@ -618,7 +618,7 @@ export abstract class CISContract<
/**
* Creates a {@link ContractUpdateTransactionWithSchema} contract update transaction, holding the necessary parts to sign/submit to the chain.
*
* @param {string} entrypoint - The name of the receive function to invoke.
* @param {EntrypointName.Type} entrypoint - The name of the receive function to invoke.
* @param {Function} serializeInput - A function to serialize the `input` to bytes.
* @param {ContractTransactionMetadata} metadata - Metadata to be used for the transaction creation (with defaults).
* @param {T} input - Input for for contract function.
Expand All @@ -637,7 +637,7 @@ export abstract class CISContract<
/**
* Creates a {@link ContractUpdateTransactionWithSchema} contract update transaction, holding the necessary parts to sign/submit to the chain.
*
* @param {string} entrypoint - The name of the receive function to invoke.
* @param {EntrypointName.Type} entrypoint - The name of the receive function to invoke.
* @param {Function} serializeInput - A function to serialize the `input` to bytes.
* @param {ContractTransactionMetadata} metadata - Metadata to be used for the transaction creation (with defaults).
* @param {T} input - Input for for contract function.
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/cis2/CIS2Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class CIS2DryRun extends ContractDryRun<Updates> {
*
* @param {CIS2.Address} owner - Address of the owner of the address to perform the update on.
* @param {CIS2.UpdateOperator | CIS2.UpdateOperator[]} update(s) - The update object(s).
* @param {HexString} [blockHash] - The hash of the block to perform the invocation of. Defaults to the latest finalized block on chain.
* @param {BlockHash.Type} [blockHash] - The hash of the block to perform the invocation of. Defaults to the latest finalized block on chain.
*
* @returns {InvokeContractResult} the contract invocation result, which includes whether or not the invocation succeeded along with the energy spent.
*/
Expand Down
43 changes: 15 additions & 28 deletions packages/sdk/src/grpc/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,31 +818,12 @@ export function consensusInfo(ci: v2.ConsensusInfo): v1.ConsensusStatus {
return ci1;
}

function trAccountAddress(
accountAddress: v2.AccountAddress | undefined
): v1.AddressAccount {
return {
type: 'AddressAccount',
address: AccountAddress.fromProto(unwrap(accountAddress)),
};
}

function trAddress(
addr: v2.Address | v2.ContractAddress | v2.AccountAddress | undefined
): v1.Address {
const accountAddress = <v2.AccountAddress>unwrap(addr);
const contractAddress = <v2.ContractAddress>unwrap(addr);
const address = <v2.Address>unwrap(addr);

if (accountAddress.value) {
return trAccountAddress(accountAddress);
} else if (ContractAddress.isContractAddress(contractAddress)) {
function trAddress(address: v2.Address): v1.Address {
if (address.type.oneofKind === 'account') {
return {
type: 'AddressContract',
address: contractAddress,
type: 'AddressAccount',
address: AccountAddress.fromProto(unwrap(address.type.account)),
};
} else if (address.type.oneofKind === 'account') {
return trAccountAddress(address.type.account);
} else if (address.type.oneofKind === 'contract') {
return {
type: 'AddressContract',
Expand All @@ -865,7 +846,7 @@ function trContractTraceElement(
address: ContractAddress.fromProto(
unwrap(element.updated.address)
),
instigator: trAddress(element.updated.instigator),
instigator: trAddress(unwrap(element.updated.instigator)),
amount: unwrap(element.updated.amount?.value),
message: Parameter.fromProto(unwrap(element.updated.parameter)),
receiveName: ReceiveName.fromProto(
Expand All @@ -876,9 +857,13 @@ function trContractTraceElement(
case 'transferred':
return {
tag: v1.TransactionEventTag.Transferred,
from: trAddress(element.transferred.sender),
from: ContractAddress.fromProto(
unwrap(element.transferred.sender)
),
amount: unwrap(element.transferred.amount?.value),
to: trAddress(element.transferred.receiver),
to: AccountAddress.fromProto(
unwrap(element.transferred.receiver)
),
};
case 'interrupted':
return {
Expand Down Expand Up @@ -1236,7 +1221,7 @@ function trRejectReason(
return {
tag: Tag.AmountTooLarge,
contents: {
address: trAddress(reason.amountTooLarge.address),
address: trAddress(unwrap(reason.amountTooLarge.address)),
amount: unwrap(reason.amountTooLarge.amount?.value),
},
};
Expand Down Expand Up @@ -1957,7 +1942,9 @@ function trAccountTransactionSummary(
const transfer: v1.AccountTransferredEvent = {
tag: v1.TransactionEventTag.Transferred,
amount: unwrap(effect.accountTransfer.amount?.value),
to: trAccountAddress(effect.accountTransfer.receiver).address,
to: AccountAddress.fromProto(
unwrap(effect.accountTransfer.receiver)
),
};
if (effect.accountTransfer.memo) {
return {
Expand Down
8 changes: 2 additions & 6 deletions packages/sdk/src/types/blockItemSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,15 +503,11 @@ export function affectedAccounts(
(addresses: AccountAddress.Type[], event) => {
if (
event.tag === TransactionEventTag.Transferred &&
event.to.type === 'AddressAccount' &&
!addresses.some(
AccountAddress.equals.bind(
undefined,
event.to.address
)
AccountAddress.equals.bind(undefined, event.to)
)
) {
return [...addresses, event.to.address];
return [...addresses, event.to];
}
return addresses;
},
Expand Down
21 changes: 8 additions & 13 deletions packages/sdk/src/types/transactionEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ export enum TransactionEventTag {
}

export type TransactionEvent =
| TransferredEvent
| UpdatedEvent
| ResumedEvent
| InterruptedEvent
| UpgradedEvent
| AccountTransferredEvent
| MemoEvent
| TransferredWithScheduleEvent
| AccountCreatedEvent
Expand Down Expand Up @@ -116,6 +112,13 @@ export interface UpdatedEvent {
events: HexString[];
}

export interface TransferredEvent {
tag: TransactionEventTag.Transferred;
amount: Amount;
to: AccountAddress.Type;
from: ContractAddress.Type;
}

export interface UpgradedEvent {
tag: TransactionEventTag.Upgraded;
address: ContractAddress.Type;
Expand Down Expand Up @@ -151,17 +154,9 @@ export interface AccountTransferredEvent {
to: AccountAddress.Type;
}

export interface TransferredEvent {
tag: TransactionEventTag.Transferred;
amount: Amount;
to: Address;
from?: Address;
}

export interface TransferredWithScheduleEvent {
tag: TransactionEventTag.TransferredWithSchedule;
to: AccountAddress.Type;
from?: AccountAddress.Type;
amount: ReleaseSchedule[];
}

Expand Down
3 changes: 1 addition & 2 deletions packages/sdk/test/ci/deserialization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
CIS2,
ContractAddress,
} from '../../src/index.js';
import { expectToEqual } from './testHelpers.js';

function deserializeAccountTransactionBase(
type: AccountTransactionType,
Expand Down Expand Up @@ -55,7 +54,7 @@ function deserializeAccountTransactionBase(
throw new Error('Incorrect BlockItemKind');
}

expectToEqual(deserialized.transaction, {
expect(deserialized.transaction).toEqual({
accountTransaction: transaction,
signatures,
});
Expand Down
35 changes: 13 additions & 22 deletions packages/sdk/test/ci/types/blockItemSummary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
Parameter,
ReceiveName,
} from '../../../src/index.js';
import { expectToEqual } from '../testHelpers.js';

const chainUpdate: UpdateSummary = {
type: TransactionSummaryType.UpdateTransaction,
Expand Down Expand Up @@ -140,22 +139,18 @@ const contractUpdate: UpdateContractSummary & BaseAccountTransactionSummary = {
{
tag: TransactionEventTag.Transferred,
amount: 0n,
to: {
type: 'AddressAccount',
address: AccountAddress.fromBase58(
'4UC8o4m8AgTxt5VBFMdLwMCwwJQVJwjesNzW7RPXkACynrULmd'
),
},
to: AccountAddress.fromBase58(
'4UC8o4m8AgTxt5VBFMdLwMCwwJQVJwjesNzW7RPXkACynrULmd'
),
from: ContractAddress.create(3496),
},
{
tag: TransactionEventTag.Transferred,
amount: 0n,
to: {
type: 'AddressAccount',
address: AccountAddress.fromBase58(
'3ybJ66spZ2xdWF3avgxQb2meouYa7mpvMWNPmUnczU8FoF8cGB'
),
},
to: AccountAddress.fromBase58(
'3ybJ66spZ2xdWF3avgxQb2meouYa7mpvMWNPmUnczU8FoF8cGB'
),
from: ContractAddress.create(3496),
},
],
};
Expand Down Expand Up @@ -349,41 +344,37 @@ describe('affectedAccounts', () => {

test('Returns list of unique account addresses corresponding to transaction', () => {
let accounts = affectedAccounts(contractUpdate);
expectToEqual(
accounts,
expect(accounts).toEqual(
[
'4UC8o4m8AgTxt5VBFMdLwMCwwJQVJwjesNzW7RPXkACynrULmd',
'3ybJ66spZ2xdWF3avgxQb2meouYa7mpvMWNPmUnczU8FoF8cGB',
].map(AccountAddress.fromBase58)
);

accounts = affectedAccounts(rejected);
expectToEqual(accounts, [
expect(accounts).toEqual([
AccountAddress.fromBase58(
'4UC8o4m8AgTxt5VBFMdLwMCwwJQVJwjesNzW7RPXkACynrULmd'
),
]);

accounts = affectedAccounts(transfer);
expectToEqual(
accounts,
expect(accounts).toEqual(
[
'3v1JUB1R1JLFtcKvHqD9QFqe2NXeBF53tp69FLPHYipTjNgLrV',
'4owvMHZSKsPW8QGYUEWSdgqxfoPBh3ZwPameBV46pSvmeHDkEe',
].map(AccountAddress.fromBase58)
);

accounts = affectedAccounts(transferToSelf);
expectToEqual(
accounts,
expect(accounts).toEqual(
['4owvMHZSKsPW8QGYUEWSdgqxfoPBh3ZwPameBV46pSvmeHDkEe'].map(
AccountAddress.fromBase58
)
);

accounts = affectedAccounts(configureDelegation);
expectToEqual(
accounts,
expect(accounts).toEqual(
['4owvMHZSKsPW8QGYUEWSdgqxfoPBh3ZwPameBV46pSvmeHDkEe'].map(
AccountAddress.fromBase58
)
Expand Down
Loading

0 comments on commit 0beaf38

Please sign in to comment.