From b7756cc60dcc895e6c1d473b05cdb5f4aa9d91bc Mon Sep 17 00:00:00 2001 From: allen Date: Tue, 3 May 2022 17:24:53 +0800 Subject: [PATCH] feat: add support for dex internal transfer, agent deposit and assigned withdraw --- src/HTTP/type.ts | 5 ++- src/IPC/type.ts | 5 ++- src/WS/type.ts | 5 ++- src/abi/type.ts | 5 ++- src/accountBlock/account.ts | 56 +++++++++++++++++++++++ src/accountBlock/type.ts | 5 ++- src/communication/type.ts | 5 ++- src/constant/index.ts | 12 +++++ src/constant/type.ts | 5 ++- src/error/type.ts | 5 ++- src/keystore/type.ts | 5 ++- src/type.ts | 5 ++- src/utils/type.ts | 5 ++- src/viteAPI/type.ts | 5 ++- src/vitejs/type.ts | 5 ++- src/wallet/type.ts | 5 ++- test/RPC/dex.js | 90 ++++++++++++++++++++++++++++++++++--- 17 files changed, 207 insertions(+), 21 deletions(-) diff --git a/src/HTTP/type.ts b/src/HTTP/type.ts index 9b928ee7..6258cf88 100644 --- a/src/HTTP/type.ts +++ b/src/HTTP/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/IPC/type.ts b/src/IPC/type.ts index 9b928ee7..6258cf88 100644 --- a/src/IPC/type.ts +++ b/src/IPC/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/WS/type.ts b/src/WS/type.ts index 9b928ee7..6258cf88 100644 --- a/src/WS/type.ts +++ b/src/WS/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/abi/type.ts b/src/abi/type.ts index 9b928ee7..6258cf88 100644 --- a/src/abi/type.ts +++ b/src/abi/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/accountBlock/account.ts b/src/accountBlock/account.ts index efa38aa1..4f60e92e 100644 --- a/src/accountBlock/account.ts +++ b/src/accountBlock/account.ts @@ -837,6 +837,62 @@ class AccountClass { params: [id] }); } + + dexTransfer({ destination, tokenId, amount }: { + destination: Address; + tokenId: TokenId; + amount: Uint256; + }): AccountBlock { + const err = checkParams({ destination, tokenId, amount }, [ 'destination', 'tokenId', 'amount' ]); + if (err) { + throw err; + } + + return this.callContract({ + abi: Contracts.DexTransfer.abi, + toAddress: Contracts.DexTransfer.contractAddress, + params: [ destination, tokenId, amount ], + tokenId + }); + } + + dexAgentDeposit({ beneficiary, tokenId, amount }: { + beneficiary: Address; + tokenId: TokenId; + amount: BigInt; + }): AccountBlock { + const err = checkParams({ beneficiary, tokenId, amount }, [ 'beneficiary', 'tokenId', 'amount' ]); + if (err) { + throw err; + } + + return this.callContract({ + abi: Contracts.DexAgentDeposit.abi, + toAddress: Contracts.DexAgentDeposit.contractAddress, + params: [beneficiary], + tokenId, + amount + }); + } + + dexAssignedWithdraw({ destination, tokenId, amount, label = '0x00' }: { + destination: Address; + tokenId: TokenId; + amount: Uint256; + label?: Bytes32; + }): AccountBlock { + const err = checkParams({ destination, tokenId, amount }, [ 'destination', 'tokenId', 'amount' ]); + if (err) { + throw err; + } + + return this.callContract({ + abi: Contracts.DexAssignedWithdraw.abi, + toAddress: Contracts.DexAssignedWithdraw.contractAddress, + params: [ destination, tokenId, amount, label ], + tokenId + }); + } } export const Account = AccountClass; diff --git a/src/accountBlock/type.ts b/src/accountBlock/type.ts index 9b928ee7..6258cf88 100644 --- a/src/accountBlock/type.ts +++ b/src/accountBlock/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/communication/type.ts b/src/communication/type.ts index 9b928ee7..6258cf88 100644 --- a/src/communication/type.ts +++ b/src/communication/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/constant/index.ts b/src/constant/index.ts index 09a0de4d..ae1ecfd5 100644 --- a/src/constant/index.ts +++ b/src/constant/index.ts @@ -278,5 +278,17 @@ export const Contracts = { DexCancelStakeById: { contractAddress: DexFund_ContractAddress, abi: { 'type': 'function', 'name': 'CancelStakeById', 'inputs': [{ 'name': 'id', 'type': 'bytes32' }] } + }, + DexTransfer: { + contractAddress: DexFund_ContractAddress, + abi: {'type': 'function', 'name': 'Transfer', 'inputs': [ {'name': 'target', 'type': 'address'}, {'name': 'token', 'type': 'tokenId'}, {'name': 'amount', 'type': 'uint256'} ]} + }, + DexAgentDeposit: { + contractAddress: DexFund_ContractAddress, + abi: {'type': 'function', 'name': 'AgentDeposit', 'inputs': [{'name': 'beneficiary', 'type': 'address'}]} + }, + DexAssignedWithdraw: { + contractAddress: DexFund_ContractAddress, + abi: {'type': 'function', 'name': 'AssignedWithdraw', 'inputs': [ {'name': 'target', 'type': 'address'}, {'name': 'token', 'type': 'tokenId'}, {'name': 'amount', 'type': 'uint256'}, {'name': 'label', 'type': 'bytes'} ]} } }; diff --git a/src/constant/type.ts b/src/constant/type.ts index 9b928ee7..6258cf88 100644 --- a/src/constant/type.ts +++ b/src/constant/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/error/type.ts b/src/error/type.ts index 9b928ee7..6258cf88 100644 --- a/src/error/type.ts +++ b/src/error/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/keystore/type.ts b/src/keystore/type.ts index 9b928ee7..6258cf88 100644 --- a/src/keystore/type.ts +++ b/src/keystore/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/type.ts b/src/type.ts index 9b928ee7..6258cf88 100644 --- a/src/type.ts +++ b/src/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/utils/type.ts b/src/utils/type.ts index 9b928ee7..6258cf88 100644 --- a/src/utils/type.ts +++ b/src/utils/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/viteAPI/type.ts b/src/viteAPI/type.ts index 9b928ee7..6258cf88 100644 --- a/src/viteAPI/type.ts +++ b/src/viteAPI/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/vitejs/type.ts b/src/vitejs/type.ts index 9b928ee7..6258cf88 100644 --- a/src/vitejs/type.ts +++ b/src/vitejs/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/src/wallet/type.ts b/src/wallet/type.ts index 9b928ee7..6258cf88 100644 --- a/src/wallet/type.ts +++ b/src/wallet/type.ts @@ -80,7 +80,10 @@ export enum TransactionType { 'DexLockVxForDividend', 'DexSwitchConfig', 'DexStakeForPrincipalSVIP', - 'DexCancelStakeById' + 'DexCancelStakeById', + 'DexTransfer', + 'DexAgentDeposit', + 'DexAssignedWithdraw' } export declare type TokenInfo = { diff --git a/test/RPC/dex.js b/test/RPC/dex.js index 8ba78625..59bd46c3 100644 --- a/test/RPC/dex.js +++ b/test/RPC/dex.js @@ -96,6 +96,28 @@ export default async function TestFunc() { console.log('Step 16 OpenNewMarket. \n'); previousAccountBlock = await OpenNewMarket(previousAccountBlock); + // 7. Internal Transfer, Agent Deposit And Assigned Withdraw + + await sleep(2000); + + console.log('Step 17 Dex Transfer. \n'); + previousAccountBlock = await InternalTransfer(); + + await sleep(2000); + + console.log('Step 18 Agent Deposit. \n'); + previousAccountBlock = await AgentDeposit(previousAccountBlock); + + await sleep(2000); + + console.log('Step 19 Assigned Withdraw. \n'); + previousAccountBlock = await AssignedWithdraw(previousAccountBlock); + + await sleep(2000); + + console.log('Step 20 Assigned Withdraw with Label. \n'); + previousAccountBlock = await AssignedWithdrawWithLabel(previousAccountBlock); + return null; } @@ -144,7 +166,7 @@ async function StakeForMining(previousAccountBlock) { async function CancelStakeMining(previousAccountBlock) { const data = await viteProvider.request('dex_getMiningStakeInfoList', address, 0, 10); console.log('[LOG] dex_getMiningStakeInfoList', data, '\n'); - + if (!data || !data.stakeList || !data.stakeList.length) { return; } @@ -205,7 +227,7 @@ async function CreateNewInviter(previousAccountBlock) { async function BindInviteCode(previousAccountBlock) { const code = await viteProvider.request('dex_getInviteCode', address); console.log('[LOG] dex_getInviteCode', code, '\n'); - + // 1. Stake Quota For addr2 let accountBlock = tx.stakeForQuota({ beneficiaryAddress: addr2.address, @@ -220,7 +242,7 @@ async function BindInviteCode(previousAccountBlock) { await sleep(2000); accountBlock = tx.send({ - toAddress: addr2.address, + toAddress: addr2.address, tokenId: Vite_TokenId, amount: '10000000000000000000' }); @@ -368,10 +390,10 @@ async function OpenNewMarket(previousAccountBlock) { await sleep(2000); accountBlock = tx.dexPlaceOrder({ - tradeToken: tokens[0].tokenId, - quoteToken: Vite_TokenId, - side: 1, - price: '1', + tradeToken: tokens[0].tokenId, + quoteToken: Vite_TokenId, + side: 1, + price: '1', quantity: '10' }); @@ -380,3 +402,57 @@ async function OpenNewMarket(previousAccountBlock) { return previousAccountBlock; } + +async function InternalTransfer(previousAccountBlock) { + const accountBlock = await tx.dexTransfer({ + destination: addr2.address, + tokenId: Vite_TokenId, + amount: '100000000000000000000' + }); + + const result = await SendTXByPreviousAccountBlock(accountBlock, previousAccountBlock); + console.log('[LOG] Dex Transfer', result, '\n'); + return result; +} + +async function AgentDeposit(previousAccountBlock) { + const accountBlock = await tx.dexAgentDeposit({ + beneficiary: addr2.address, + tokenId: Vite_TokenId, + amount: '300000000000000000000' + }); + + const result = await SendTXByPreviousAccountBlock(accountBlock, previousAccountBlock); + console.log('[LOG] Agent Deposit', result, '\n'); + return result; +} + +async function AssignedWithdraw(previousAccountBlock) { + const accountBlock = await tx.dexAssignedWithdraw({ + destination: addr2.address, + tokenId: Vite_TokenId, + amount: '1000000000000000000' + }); + + const result = await SendTXByPreviousAccountBlock(accountBlock, previousAccountBlock); + console.log('[LOG] Withdraw', result, '\n'); + return result; +} + +async function AssignedWithdrawWithLabel(previousAccountBlock) { + const label = Buffer.concat([ + Buffer.from('0bc3', 'hex'), + Buffer.from([0]), + Buffer.from('0x0000000000000000000000000000000000000001') + ]).toString("hex"); + const accountBlock = await tx.dexAssignedWithdraw({ + destination: addr2.address, + tokenId: Vite_TokenId, + amount: '1000000000000000000', + label + }); + + const result = await SendTXByPreviousAccountBlock(accountBlock, previousAccountBlock); + console.log('[LOG] Assigned Withdraw', result, '\n'); + return result; +}