Skip to content

Commit

Permalink
Feat/multi msg (#49)
Browse files Browse the repository at this point in the history
* feat: Multi Send

* feat: Compatibility multi tx message
  • Loading branch information
rrr523 authored May 24, 2023
1 parent 6ad6f97 commit fc3dd98
Show file tree
Hide file tree
Showing 43 changed files with 514 additions and 3,400 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-ducks-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-chain-sdk': patch
---

feat: Update EIP712 struct, Support Multi Message !!!!!
12 changes: 11 additions & 1 deletion .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@

{
"mode": "pre",
"tag": "alpha",
"initialVersions": {
"@demo/wallet": "0.0.2-alpha.6",
"@demo/nodejs": "0.0.4",
"@bnb-chain/greenfield-chain-sdk": "0.2.0",
"@bnb-chain/greenfiled-file-handle": "0.2.0"
},
"changesets": []
}
11 changes: 0 additions & 11 deletions examples/nextjs/src/components/transfer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ export const Transfer = () => {

return (
<div>
<button
onClick={async () => {
const account = await client.account.getAccount(
'0x1C893441AB6c1A75E01887087ea508bE8e07AAae',
);

console.log(account);
}}
>
click
</button>
<h2>Transfer</h2>
to :
<input
Expand Down
104 changes: 98 additions & 6 deletions packages/chain-sdk/src/api/account.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { describe, expect, test } from '@jest/globals';
import { GREENFIELD_CHAIN_ID, GRPC_URL, ACCOUNT } from '../config.spec';
import {
GREENFIELD_CHAIN_ID,
GRPC_URL,
ACCOUNT,
ZERO_ACCOUNT_ADDRESS,
DEFAULT_SIMULATE_INFO,
} from '../config.spec';
import { Client } from '../client';

const client = Client.create(GRPC_URL, GREENFIELD_CHAIN_ID);
Expand Down Expand Up @@ -52,8 +58,94 @@ describe('accountQuery', () => {
// });
});

// describe('accountTx', () => {
// describe('transfer', async () => {
// // ...
// });
// });
describe('accountTx', () => {
let simulateInfo = DEFAULT_SIMULATE_INFO;

describe('transfer', () => {
test('simulate works', async () => {
const transferTx = await makeTransferTx();
simulateInfo = await transferTx.simulate({
denom: 'BNB',
});
expect(simulateInfo).not.toBeNull();
});

test('broadcast works', async () => {
const transferTx = await makeTransferTx();
const broadcastInfo = await transferTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo.gasLimit),
gasPrice: simulateInfo.gasPrice,
granter: '',
payer: ACCOUNT.address,
privateKey: ACCOUNT.privateKey,
});

expect(broadcastInfo.code).toEqual(0);
});
});

describe('multiTransfer', () => {
test('simulate works', async () => {
const transferTx = await makeMultiTransferTx();
simulateInfo = await transferTx.simulate({
denom: 'BNB',
});
expect(simulateInfo).not.toBeNull();
});

test('broadcast works', async () => {
const transferTx = await makeMultiTransferTx();
const broadcastInfo = await transferTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo.gasLimit),
gasPrice: simulateInfo.gasPrice,
granter: '',
payer: ACCOUNT.address,
privateKey: ACCOUNT.privateKey,
});

expect(broadcastInfo.code).toEqual(0);
});
});
});

async function makeMultiTransferTx() {
return await client.account.multiTransfer(ACCOUNT.address, {
inputs: [
{
address: ACCOUNT.address,
coins: [
{
amount: '10',
denom: 'BNB',
},
],
},
],
outputs: [
{
address: ZERO_ACCOUNT_ADDRESS,
coins: [
{
amount: '10',
denom: 'BNB',
},
],
},
],
});
}

async function makeTransferTx() {
return await client.account.transfer({
amount: [
{
amount: '10',
denom: 'BNB',
},
],
fromAddress: ACCOUNT.address,
toAddress: ZERO_ACCOUNT_ADDRESS,
});
}
2 changes: 1 addition & 1 deletion packages/chain-sdk/src/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import {
QueryGetPaymentAccountsByOwnerResponse,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/payment/query';
import { MsgCreatePaymentAccount } from '@bnb-chain/greenfield-cosmos-types/greenfield/payment/tx';
import { container, delay, inject, singleton } from 'tsyringe';
import { TxResponse } from '..';
import { Basic } from './basic';
import { RpcQueryClient } from './queryclient';
import { autoInjectable, container, delay, inject, injectable, singleton } from 'tsyringe';

export interface IAccount {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/chain-sdk/src/api/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import { DeliverTxResponse, StargateClient } from '@cosmjs/stargate';
import { Tendermint37Client } from '@cosmjs/tendermint-rpc';
import { toBuffer } from '@ethereumjs/util';
import Long from 'long';
import { container, inject, singleton } from 'tsyringe';
import { BroadcastOptions, ISimulateGasFee, SimulateOptions } from '..';
import { DEFAULT_DENOM, ZERO_PUBKEY } from '../constants';
import { createEIP712, generateFee, generateMessage, generateTypes } from '../messages';
import { eip712Hash, makeCosmsPubKey, recoverPk } from '../sign';
import { typeWrapper } from '../tx/utils';
import { RpcQueryClient } from './queryclient';
import { Account } from './account';
import { autoInjectable, container, delay, inject, injectable, singleton } from 'tsyringe';
import { RpcQueryClient } from './queryclient';

export interface IBasic {
/**
Expand Down
34 changes: 26 additions & 8 deletions packages/chain-sdk/src/api/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ import {
MsgDeleteBucketSDKTypeEIP712,
MsgDeleteBucketTypeUrl,
} from '@/messages/greenfield/storage/MsgDeleteBucket';
import { MsgDeletePolicySDKTypeEIP712 } from '@/messages/greenfield/storage/MsgDeletePolicy';
import { MsgPutPolicySDKTypeEIP712 } from '@/messages/greenfield/storage/MsgPutPolicy';
import { MsgUpdateBucketInfoSDKTypeEIP712 } from '@/messages/greenfield/storage/MsgUpdateBucketInfo';
import {
MsgDeletePolicySDKTypeEIP712,
MsgDeletePolicyTypeUrl,
} from '@/messages/greenfield/storage/MsgDeletePolicy';
import {
MsgPutPolicySDKTypeEIP712,
MsgPutPolicyTypeUrl,
} from '@/messages/greenfield/storage/MsgPutPolicy';
import {
MsgUpdateBucketInfoSDKTypeEIP712,
MsgUpdateBucketInfoTypeUrl,
} from '@/messages/greenfield/storage/MsgUpdateBucketInfo';
import { decodeObjectFromHexString, encodeObjectToHexString } from '@/utils/encoding';
import { fetchWithTimeout, METHOD_GET, MOCK_SIGNATURE, NORMAL_ERROR_CODE } from '@/utils/http';
import { generateUrlByBucketName, isValidAddress, isValidBucketName, isValidUrl } from '@/utils/s3';
import { ActionType } from '@bnb-chain/greenfield-cosmos-types/greenfield/permission/common';
import { visibilityTypeFromJSON } from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/common';
import {
QueryHeadBucketResponse,
QueryPolicyForAccountRequest,
QueryPolicyForAccountResponse,
QueryVerifyPermissionResponse,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/query';
import {
Expand Down Expand Up @@ -87,6 +98,10 @@ export interface IBucket {
putBucketPolicy(msg: MsgPutPolicy): Promise<TxResponse>;

deleteBucketPolicy(msg: MsgDeletePolicy): Promise<TxResponse>;

getBucketPolicy(request: QueryPolicyForAccountRequest): Promise<QueryPolicyForAccountResponse>;

// TODO: getBucketReadQuota();
}

@singleton()
Expand Down Expand Up @@ -371,7 +386,7 @@ export class Bucket implements IBucket {

public async updateBucketInfo(msg: MsgUpdateBucketInfo) {
return await this.basic.tx(
'/greenfield.storage.MsgUpdateBucketInfo',
MsgUpdateBucketInfoTypeUrl,
msg.operator,
MsgUpdateBucketInfoSDKTypeEIP712,
MsgUpdateBucketInfo.toSDK(msg),
Expand All @@ -381,7 +396,7 @@ export class Bucket implements IBucket {

public async putBucketPolicy(msg: MsgPutPolicy) {
return await this.basic.tx(
'/greenfield.storage.MsgPutPolicy',
MsgPutPolicyTypeUrl,
msg.operator,
MsgPutPolicySDKTypeEIP712,
MsgPutPolicy.toSDK(msg),
Expand All @@ -390,14 +405,17 @@ export class Bucket implements IBucket {
}

public async deleteBucketPolicy(msg: MsgDeletePolicy) {
const typeUrl = '/greenfield.storage.MsgDeletePolicy';

return await this.basic.tx(
typeUrl,
MsgDeletePolicyTypeUrl,
msg.operator,
MsgDeletePolicySDKTypeEIP712,
MsgDeletePolicy.toSDK(msg),
MsgDeletePolicy.encode(msg).finish(),
);
}

public async getBucketPolicy(request: QueryPolicyForAccountRequest) {
const rpc = await this.queryClient.getStorageQueryClient();
return rpc.QueryPolicyForAccount(request);
}
}
23 changes: 14 additions & 9 deletions packages/chain-sdk/src/api/challenge.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { MsgAttestSDKTypeEIP712 } from '@/messages/greenfield/chanenge/MsgAttest';
import { MsgSubmitSDKTypeEIP712 } from '@/messages/greenfield/chanenge/MsgSubmit';
import { MsgAttestSDKTypeEIP712, MsgAttestTypeUrl } from '@/messages/greenfield/chanenge/MsgAttest';
import { MsgSubmitSDKTypeEIP712, MsgSubmitTypeUrl } from '@/messages/greenfield/chanenge/MsgSubmit';
import {
QueryInturnAttestationSubmitterResponse,
QueryLatestAttestedChallengesResponse,
QueryParamsResponse,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/challenge/query';
import { MsgAttest, MsgSubmit } from '@bnb-chain/greenfield-cosmos-types/greenfield/challenge/tx';
import { container, delay, inject, singleton } from 'tsyringe';
Expand All @@ -11,11 +12,8 @@ import { Basic } from './basic';
import { RpcQueryClient } from './queryclient';

export interface IChallenge {
/**
* sends request to challenge and get challenge result info
* The challenge info includes the piece data, piece hash roots and integrity hash corresponding to the accessed SP
*/
// getChallengeInfo();
// TODO: getChallengeInfo();

/**
* challenges the service provider data integrity, used by off-chain service greenfield-challenger.
*/
Expand All @@ -32,6 +30,8 @@ export interface IChallenge {
latestAttestedChallenges(): Promise<QueryLatestAttestedChallengesResponse>;

inturnAttestationSubmitter(): Promise<QueryInturnAttestationSubmitterResponse>;

params(): Promise<QueryParamsResponse>;
}

@singleton()
Expand All @@ -41,7 +41,7 @@ export class Challenge implements IChallenge {

public async submitChallenge(address: string, msg: MsgSubmit) {
return await this.basic.tx(
'/greenfield.challenge.MsgSubmit',
MsgSubmitTypeUrl,
address,
MsgSubmitSDKTypeEIP712,
MsgSubmit.toSDK(msg),
Expand All @@ -51,7 +51,7 @@ export class Challenge implements IChallenge {

public async attestChallenge(address: string, msg: MsgAttest) {
return await this.basic.tx(
'/greenfield.challenge.MsgAttest',
MsgAttestTypeUrl,
address,
MsgAttestSDKTypeEIP712,
MsgAttest.toSDK(msg),
Expand All @@ -68,4 +68,9 @@ export class Challenge implements IChallenge {
const rpc = await this.queryClient.getChallengeQueryClient();
return await rpc.InturnAttestationSubmitter();
}

public async params() {
const rpc = await this.queryClient.getChallengeQueryClient();
return await rpc.Params();
}
}
25 changes: 17 additions & 8 deletions packages/chain-sdk/src/api/crosschain.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { MsgClaimSDKTypeEIP712 } from '@/messages/cosmos/oracle/MsgClaim';
import { MsgClaimSDKTypeEIP712, MsgClaimTypeUrl } from '@/messages/cosmos/oracle/MsgClaim';
import {
MsgTransferOutSDKTypeEIP712,
MsgTransferOutTypeUrl,
} from '@/messages/greenfield/bridge/MsgTransferOut';
import { MsgMirrorBucketSDKTypeEIP712 } from '@/messages/greenfield/storage/MsgMirrorBucket';
import { MsgMirrorGroupSDKTypeEIP712 } from '@/messages/greenfield/storage/MsgMirrorGroup';
import { MsgMirrorObjectSDKTypeEIP712 } from '@/messages/greenfield/storage/MsgMirrorObject';
import {
MsgMirrorBucketSDKTypeEIP712,
MsgMirrorBucketTypeUrl,
} from '@/messages/greenfield/storage/MsgMirrorBucket';
import {
MsgMirrorGroupSDKTypeEIP712,
MsgMirrorGroupTypeUrl,
} from '@/messages/greenfield/storage/MsgMirrorGroup';
import {
MsgMirrorObjectSDKTypeEIP712,
MsgMirrorObjectTypeUrl,
} from '@/messages/greenfield/storage/MsgMirrorObject';
import {
QueryCrossChainPackageResponse,
QueryReceiveSequenceResponse,
Expand Down Expand Up @@ -92,7 +101,7 @@ export class CrossChain implements ICrossChain {

public async claims(msg: MsgClaim) {
return await this.basic.tx(
'/cosmos.oracle.v1.MsgClaim',
MsgClaimTypeUrl,
msg.fromAddress,
MsgClaimSDKTypeEIP712,
MsgClaim.toSDK(msg),
Expand Down Expand Up @@ -129,7 +138,7 @@ export class CrossChain implements ICrossChain {

public async mirrorGroup(msg: MsgMirrorGroup) {
return await this.basic.tx(
'/greenfield.storage.MsgMirrorGroup',
MsgMirrorGroupTypeUrl,
msg.operator,
MsgMirrorGroupSDKTypeEIP712,
MsgMirrorGroup.toSDK(msg),
Expand All @@ -139,7 +148,7 @@ export class CrossChain implements ICrossChain {

public async mirrorBucket(msg: MsgMirrorBucket) {
return await this.basic.tx(
'/greenfield.storage.MsgMirrorBucket',
MsgMirrorBucketTypeUrl,
msg.operator,
MsgMirrorBucketSDKTypeEIP712,
MsgMirrorBucket.toSDK(msg),
Expand All @@ -149,7 +158,7 @@ export class CrossChain implements ICrossChain {

public async mirrorObject(msg: MsgMirrorObject) {
return await this.basic.tx(
'/greenfield.storage.MsgMirrorObject',
MsgMirrorObjectTypeUrl,
msg.operator,
MsgMirrorObjectSDKTypeEIP712,
MsgMirrorObject.toSDK(msg),
Expand Down
Loading

0 comments on commit fc3dd98

Please sign in to comment.