Skip to content

Commit 7625eb2

Browse files
author
Neo
committed
add payment generator
1 parent c62b180 commit 7625eb2

File tree

5 files changed

+69
-21
lines changed

5 files changed

+69
-21
lines changed

example/mvm.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { Client,
2-
extraGeneratorByInfo,
2+
extraGeneratByInfo,
3+
paymentGeneratByInfo,
34
getMvmTransaction,
45
getAssetIDByAddress,
56
getContractByAssetID,
@@ -12,7 +13,7 @@ const client = new Client(keystore)
1213

1314
async function main() {
1415
// 1.1 使用参数进行生成 extra
15-
const extra = extraGeneratorByInfo({
16+
const extra = extraGeneratByInfo({
1617
contractAddress: '0x4f31E2eAF25DCDD46651AcE019B61E3E750023E0', // 要调用的合约地址
1718
methodName: 'addAny', // 要调用的合约方法
1819
types: ['uint256'], // 参数类型列表
@@ -49,4 +50,20 @@ async function main() {
4950
console.log(userAddress) // 0xc49E0F42A844273E41F0d00e8C2406468b55AFEa
5051
}
5152

53+
async function paymentTest() {
54+
const t = await paymentGeneratByInfo({
55+
contractAddress: '0x4883Ae7CB5c3Cf9219Aeb02d010F2F6Ef353C40c',
56+
methodName: 'addAny',
57+
types: ['uint256'],
58+
values: ['11'],
59+
payment: {
60+
type: 'tx'
61+
}
62+
})
63+
64+
const tx = await client.transaction(t)
65+
console.log(t)
66+
console.log(tx)
67+
}
68+
5269
main()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mixin-node-sdk",
3-
"version": "3.0.24",
3+
"version": "3.0.25",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/client/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export {
8282
getMvmTransaction,
8383
abiParamsGenerator,
8484
extraGeneratByInfo,
85+
paymentGeneratByInfo,
8586
getContractByAssetID,
8687
getContractByUserIDs,
8788
getAssetIDByAddress,
@@ -257,7 +258,7 @@ export class Client
257258

258259
// Pin...
259260
verifyPin!: (pin: string) => Promise<void>;
260-
modifyPin!: (pin: string, newPin: string) => Promise<void>;
261+
modifyPin!: (pin: string, oldPin?: string) => Promise<void>;
261262
readTurnServers!: () => Promise<Turn[]>;
262263

263264
// Snapshot...

src/client/mvm.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TransactionInput, InvokeCodeParams, ExtraGeneratParams } from "../types"
1+
import { TransactionInput, InvokeCodeParams, ExtraGeneratParams, Payment, PaymentGeneratParams } from "../types"
22
import { v4 as newUUID, parse, stringify } from 'uuid'
33
import { Encoder } from "../mixin/encoder"
44
import { base64url } from "../mixin/sign"
@@ -12,6 +12,10 @@ const OperationPurposeGroupEvent = 1
1212
// const OperationPurposeAddProcess = 11
1313
// const OperationPurposeCreditProcess = 12
1414

15+
const mvmClient = axios.create({
16+
baseURL: 'https://mvm-api.test.mixinbots.com',
17+
})
18+
1519
const receivers = [
1620
"a15e0b6d-76ed-4443-b83f-ade9eca2681a",
1721
"b9126674-b07d-49b6-bf4f-48d965b2242b",
@@ -60,18 +64,18 @@ export const extraGeneratByInfo = async (params: ExtraGeneratParams): Promise<st
6064
if (!methodID) methodID = getMethodIdByAbi(methodName!, types)
6165
if (types.length != values.length) return Promise.reject('error: types.length!=values.length')
6266
let extra = contractAddress + methodID
63-
let opcode: number = 0
64-
const { uploadkey, delegatecall, process = registryProcess, address = registryAddress } = options
67+
const { uploadkey, delegatecall, ignoreUpload, process = registryProcess, address = registryAddress } = options
6568
if (types.length !== 0) {
6669
const abiCoder = new utils.AbiCoder()
6770
extra += abiCoder.encode(types, values).slice(2)
6871
}
69-
const memo = encodeMemo(extra, process)
70-
if (memo.length > 200) {
72+
if (ignoreUpload) return extra
73+
let opcode: number = 0
74+
if (encodeMemo(extra, process).length > 200) {
7175
if (!uploadkey) return Promise.reject('please provide key to generate extra(length > 200)')
7276
const raw = '0x' + extra
7377
const key = utils.keccak256(raw)
74-
const res = await axios.post(`https://mvm-api.test.mixinbots.com/`, { uploadkey, key, raw, address })
78+
const res = await mvmClient.post(`/`, { uploadkey, key, raw, address })
7579
if (!res.data.hash) return Promise.reject(res)
7680
opcode += 1
7781
extra = key.slice(2)
@@ -80,11 +84,23 @@ export const extraGeneratByInfo = async (params: ExtraGeneratParams): Promise<st
8084
return ('0' + opcode + extra).toLowerCase()
8185
}
8286

87+
export const paymentGeneratByInfo = async (params: PaymentGeneratParams): Promise<Payment> => {
88+
if (!params.options) params.options = {}
89+
params.options.ignoreUpload = true
90+
const extra = await extraGeneratByInfo(params)
91+
const { type, trace, asset, amount } = params.payment || {}
92+
const { process, delegatecall, uploadkey, address } = params.options
93+
const res = await mvmClient.post('/payment', {
94+
extra,
95+
process, delegatecall, uploadkey, address,
96+
type, trace, asset, amount
97+
})
98+
return res.data
99+
}
83100

84101
export const getContractByAssetID = (id: string, processAddress = registryAddress): Promise<string> =>
85102
getRegistryContract(processAddress).contracts('0x' + Buffer.from(parse(id) as Buffer).toString('hex'))
86103

87-
88104
export const getContractByUserIDs = (ids: string | string[], threshold?: number, processAddress = registryAddress): Promise<string> => {
89105
if (typeof ids === 'string') ids = [ids]
90106
if (!threshold) threshold = ids.length

src/types/mvm.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,39 @@ export interface InvokeCodeParams {
1010
process?: string
1111
}
1212

13-
export interface ExtraGeneratOptions {
14-
uploadkey?: string // auto upload params
15-
delegatecall?: boolean // use delegatecall
16-
process?: string // registry process
17-
address?: string // registry address
18-
}
19-
20-
export interface ExtraGeneratParams {
13+
interface ContractParams {
2114
contractAddress: string
2215
methodName?: string
2316
types?: string[]
2417
values?: any[]
25-
options?: ExtraGeneratOptions
2618
methodID?: string
2719
}
2820

21+
export interface ExtraGeneratParams extends ContractParams {
22+
options: {
23+
delegatecall?: boolean // use delegatecall
24+
process?: string // registry process
25+
address?: string // registry address
26+
uploadkey?: string // auto upload params
27+
28+
ignoreUpload?: boolean // ignore upload params
29+
}
30+
}
31+
32+
export interface PaymentGeneratParams extends ExtraGeneratParams {
33+
extra?: string,
34+
payment: {
35+
asset?: string
36+
amount?: string
37+
trace?: string
38+
type?: 'payment' | 'tx' // payment or tx, default is payment
39+
}
40+
}
41+
2942

3043
export interface MvmClientRequest {
3144
getMvmTransaction(params: InvokeCodeParams): TransactionInput
3245
abiParamsGenerator(contractAddress: string, abi: JsonFragment[]): { [method: string]: Function }
33-
extraGeneratorByInfo(params: ExtraGeneratParams, options: ExtraGeneratOptions): string
46+
extraGeneratByInfo(params: ExtraGeneratParams): string
47+
paymentGeneratByInfo(params: PaymentGeneratParams): string
3448
}

0 commit comments

Comments
 (0)