Browse Source

XRPL API changes 4.0.0

master
nitowa 1 month ago
parent
commit
d84684aa84
6 changed files with 1037 additions and 725 deletions
  1. 939
    675
      package-lock.json
  2. 5
    3
      package.json
  3. 24
    8
      src/RJSVM/framework/rjsvm.ts
  4. 25
    7
      src/user-defined-contract.ts
  5. 30
    18
      test/Test.ts
  6. 14
    14
      test/tools.ts

+ 939
- 675
package-lock.json
File diff suppressed because it is too large
View File


+ 5
- 3
package.json View File

1
 {
1
 {
2
   "name": "rjsvm",
2
   "name": "rjsvm",
3
-  "version": "0.2.4",
3
+  "version": "0.2.5",
4
   "description": "",
4
   "description": "",
5
   "main": "lib/src/main.js",
5
   "main": "lib/src/main.js",
6
   "scripts": {
6
   "scripts": {
15
   "author": "",
15
   "author": "",
16
   "license": "ISC",
16
   "license": "ISC",
17
   "dependencies": {
17
   "dependencies": {
18
+    "axios": "^1.7.7",
18
     "stream-http": "^3.2.0",
19
     "stream-http": "^3.2.0",
19
     "url": "^0.11.3",
20
     "url": "^0.11.3",
20
-    "xrpio": "^0.2.1",
21
-    "xrpl": "^2.7.0",
21
+    "xrpio": "^0.3.0",
22
+    "xrpl": "^4.0.0",
22
     "zod": "^3.21.4"
23
     "zod": "^3.21.4"
23
   },
24
   },
24
   "devDependencies": {
25
   "devDependencies": {
46
     "lib/src/main.js",
47
     "lib/src/main.js",
47
     "lib/src/main.d.ts",
48
     "lib/src/main.d.ts",
48
     "lib/src/util/*",
49
     "lib/src/util/*",
50
+    "lib/browser/rjsvm.browser.js",
49
     "lib/src/RJSVM/*"
51
     "lib/src/RJSVM/*"
50
   ]
52
   ]
51
 }
53
 }

+ 24
- 8
src/RJSVM/framework/rjsvm.ts View File

1
 import { PaymentTx_T, ParameterizedFunction, Payload, RJSVM, RJSVM_Config, RJSVM_Implementations, Function_Map, Endpoints_Of, State_Of, Generic_Ctor_ReturnType, RJSVM_Endpoint } from "./types"
1
 import { PaymentTx_T, ParameterizedFunction, Payload, RJSVM, RJSVM_Config, RJSVM_Implementations, Function_Map, Endpoints_Of, State_Of, Generic_Ctor_ReturnType, RJSVM_Endpoint } from "./types"
2
-import { Client as Xrpl } from 'xrpl';
2
+import { AccountTxTransaction, Client as Xrpl } from 'xrpl';
3
 import { xrpIO } from 'xrpio';
3
 import { xrpIO } from 'xrpio';
4
 import { DataParser } from "../../util/dataparser";
4
 import { DataParser } from "../../util/dataparser";
5
 import { XRP_ADDRESS } from "../../util/protocol.constants";
5
 import { XRP_ADDRESS } from "../../util/protocol.constants";
151
                 }
151
                 }
152
             }
152
             }
153
 
153
 
154
-            private parseMemos = async (tx:PaymentTx_T, memos: any[]) => {
154
+            private parseMemos = async (tx:PaymentTx_T) => {
155
+                const memos = tx.Memos
155
                 memos
156
                 memos
156
                     .map((memo: any) => {
157
                     .map((memo: any) => {
157
                         if (!memo.Memo || !memo.Memo.MemoData) {
158
                         if (!memo.Memo || !memo.Memo.MemoData) {
191
                 })
192
                 })
192
 
193
 
193
                 //results may not be ordered, so sort them
194
                 //results may not be ordered, so sort them
194
-                const raw_txs = resp.result.transactions
195
-                    .sort((a,b) => a.tx.ledger_index - b.tx.ledger_index)
196
-                    .map((entry: any) => entry.tx)
197
-                
198
-                //apply any state changes
199
-                await Promise.all(raw_txs.map((tx: PaymentTx_T) => this.parseMemos(tx, tx.Memos)))
195
+                const raw_txs = await Promise.all(
196
+                    resp.result.transactions
197
+                        .filter(tx => tx.tx_json.TransactionType === "Payment")
198
+                        .sort((a,b) => a.tx_json.ledger_index - b.tx_json.ledger_index)
199
+                        .map((tx) => this.xrpIO.getTransaction(tx.hash))
200
+                )
201
+
202
+                //XRPL API update adaption
203
+                await Promise.all(
204
+                    raw_txs
205
+                        .filter(tx => !!tx.result.tx_json.Memos)
206
+                        .map(tx => {
207
+                            return({
208
+                                ...tx.result.tx_json,
209
+                                hash: tx.result.hash,
210
+                                Amount: tx.result.tx_json["DeliverMax"],
211
+                                inLedger: tx.result.tx_json['ledger_index']
212
+                            } as PaymentTx_T)
213
+                        })
214
+                        .map(tx => this.parseMemos(tx))
215
+                    )
200
                 
216
                 
201
                 //if marker is present the result is paginated.
217
                 //if marker is present the result is paginated.
202
                 //re-run the same request with the marker included
218
                 //re-run the same request with the marker included

+ 25
- 7
src/user-defined-contract.ts View File

20
 
20
 
21
 const setup = async () => {
21
 const setup = async () => {
22
 
22
 
23
-    [ownerWallet, listeningWallet, userWallet, drainWallet] = await Promise.all([makeTestnetWallet(),makeTestnetWallet(),makeTestnetWallet(),makeTestnetWallet()])
23
+    //    [ownerWallet, listeningWallet, userWallet, drainWallet] = await Promise.all([makeTestnetWallet(),makeTestnetWallet(),makeTestnetWallet(),makeTestnetWallet()])
24
+    [ownerWallet, listeningWallet, userWallet, drainWallet] = [
25
+        {
26
+            secret: 'sEd7tPDHFk3w7ABSaCPYi12iwucMQLt',
27
+            address: 'rDML7QhJeVxZTwsjPtm4iyLUJTCzzuppMo'
28
+        },
29
+        {
30
+            secret: 'sEdSgbyouvLM26vq2F2XthfKGWndP72',
31
+            address: 'rPvwbb4JHncG9rKVrYATJw6HRixR69wgjg'
32
+        },
33
+        {
34
+            secret: 'sEd7c1Y8JwWyA2owbchMWnuzAhB3ypY',
35
+            address: 'rwuuWdyxwf3o78Ce2DAZrYCQ12SSawTp6M'
36
+        },
37
+        {
38
+            secret: 'sEdScantwGA5gYe4atKPbG5bsriURpf',
39
+            address: 'rDiwSioTRyV29rNk2FskxqFtMaRsmWvS7C'
40
+        }
41
+    ]
24
 
42
 
25
     owner_datawriter = new Datawriter({
43
     owner_datawriter = new Datawriter({
26
         receiveAddress: drainWallet.address,
44
         receiveAddress: drainWallet.address,
44
     // #########################
62
     // #########################
45
     // Define parameter types
63
     // Define parameter types
46
     // #########################
64
     // #########################
47
-    
65
+
48
     const shoutSchema = z.object({
66
     const shoutSchema = z.object({
49
         title: z.string(),
67
         title: z.string(),
50
         body: z.string(),
68
         body: z.string(),
78
     // #########################
96
     // #########################
79
 
97
 
80
     abstract class RJSVM_Base
98
     abstract class RJSVM_Base
81
-    extends RJSVM<State, RJSVM_Endpoints>
82
-    implements RJSVM_Interface<RJSVM_Base> {
99
+        extends RJSVM<State, RJSVM_Endpoints>
100
+        implements RJSVM_Interface<RJSVM_Base> {
83
         owner = ownerWallet.address
101
         owner = ownerWallet.address
84
 
102
 
85
         state: State = {
103
         state: State = {
122
     // #########################
140
     // #########################
123
     // Build and connect
141
     // Build and connect
124
     // #########################
142
     // #########################
125
-    
143
+
126
     const Rjsvm = RJSVM_Builder.from(RJSVM_Base, RJSVM_Contract);
144
     const Rjsvm = RJSVM_Builder.from(RJSVM_Base, RJSVM_Contract);
127
 
145
 
128
     const conf: RJSVM_Config = {
146
     const conf: RJSVM_Config = {
135
 }
153
 }
136
 
154
 
137
 (async () => {
155
 (async () => {
138
-        await setup()
139
-        await owner_datawriter.callEndpoint('submit', { title: "1", body: "2", from: "3" }, 100)
156
+    await setup()
157
+    await owner_datawriter.callEndpoint('submit', { title: "1", body: "2", from: "3" }, 100)
140
 })()
158
 })()

+ 30
- 18
test/Test.ts View File

27
 
27
 
28
 
28
 
29
 const setup = async () => {
29
 const setup = async () => {
30
-
31
-    [ownerWallet, listeningWallet, userWallet, drainWallet] = await Promise.all([makeTestnetWallet(), makeTestnetWallet(), makeTestnetWallet(), makeTestnetWallet()])
32
-
30
+    //    [ownerWallet, listeningWallet, userWallet, drainWallet] = await Promise.all([makeTestnetWallet(),makeTestnetWallet(),makeTestnetWallet(),makeTestnetWallet()])
31
+    [ownerWallet, listeningWallet, userWallet, drainWallet] = [
32
+        {
33
+            secret: 'sEd7tPDHFk3w7ABSaCPYi12iwucMQLt',
34
+            address: 'rDML7QhJeVxZTwsjPtm4iyLUJTCzzuppMo'
35
+        },
36
+        {
37
+            secret: 'sEdSgbyouvLM26vq2F2XthfKGWndP72',
38
+            address: 'rPvwbb4JHncG9rKVrYATJw6HRixR69wgjg'
39
+        },
40
+        {
41
+            secret: 'sEd7c1Y8JwWyA2owbchMWnuzAhB3ypY',
42
+            address: 'rwuuWdyxwf3o78Ce2DAZrYCQ12SSawTp6M'
43
+        },
44
+        {
45
+            secret: 'sEdScantwGA5gYe4atKPbG5bsriURpf',
46
+            address: 'rDiwSioTRyV29rNk2FskxqFtMaRsmWvS7C'
47
+        }
48
+    ]
33
     owner_datawriter = new Datawriter({
49
     owner_datawriter = new Datawriter({
34
         receiveAddress: drainWallet.address,
50
         receiveAddress: drainWallet.address,
35
         sendWallet: ownerWallet,
51
         sendWallet: ownerWallet,
129
 describe('RJSVM basic functions', () => {
145
 describe('RJSVM basic functions', () => {
130
 
146
 
131
     before(async function () {
147
     before(async function () {
132
-        this.timeout(10000)
148
+        this.timeout(100000)
133
         await setup()
149
         await setup()
134
     })
150
     })
135
 
151
 
138
     })
154
     })
139
 
155
 
140
     it('Env contains the payload carrying transaction', function (done) {
156
     it('Env contains the payload carrying transaction', function (done) {
141
-        this.timeout(30000)
157
+        this.timeout(45000)
142
 
158
 
159
+        console.log("Making wallet...")
143
         makeTestnetWallet().then(async testWallet => {
160
         makeTestnetWallet().then(async testWallet => {
161
+            console.log(testWallet)
144
             //create a mock RJSVM
162
             //create a mock RJSVM
145
             abstract class RJSVM_Base
163
             abstract class RJSVM_Base
146
-            extends RJSVM<undefined, { testEndpoint: () => void }>
147
-            implements RJSVM_Interface<RJSVM_Base> {
164
+                extends RJSVM<undefined, { testEndpoint: () => void }>
165
+                implements RJSVM_Interface<RJSVM_Base> {
148
                 owner = ownerWallet.address
166
                 owner = ownerWallet.address
149
                 state = undefined
167
                 state = undefined
150
             }
168
             }
153
             const RJSVM_Contract: RJSVM_Implementations<RJSVM_Base> = {
171
             const RJSVM_Contract: RJSVM_Implementations<RJSVM_Base> = {
154
                 testEndpoint: {
172
                 testEndpoint: {
155
                     implementation: function (env) {
173
                     implementation: function (env) {
156
-
157
                         expect(env.Account).to.be.equal(userWallet.address)
174
                         expect(env.Account).to.be.equal(userWallet.address)
158
                         expect(env.Amount).to.be.equal('1')
175
                         expect(env.Amount).to.be.equal('1')
159
                         expect(env.Destination).to.be.equal(testWallet.address)
176
                         expect(env.Destination).to.be.equal(testWallet.address)
160
                         expect(Number(env.Fee)).to.be.greaterThanOrEqual(10)
177
                         expect(Number(env.Fee)).to.be.greaterThanOrEqual(10)
161
                         expect(env.LastLedgerSequence).to.be.a('number')
178
                         expect(env.LastLedgerSequence).to.be.a('number')
162
-
163
                         expect(env.Memos).to.be.an('Array')
179
                         expect(env.Memos).to.be.an('Array')
164
-                        const memo = JSON.parse(DataParser.hex_to_ascii(env.Memos[0].Memo.MemoData))
165
-                        expect(memo.endpoint).to.be.equal('testEndpoint')
166
-
167
                         expect(env.Sequence).to.be.a('number')
180
                         expect(env.Sequence).to.be.a('number')
168
                         expect(env.SigningPubKey).to.be.a('string')
181
                         expect(env.SigningPubKey).to.be.a('string')
169
                         expect(env.TransactionType).to.be.equal('Payment')
182
                         expect(env.TransactionType).to.be.equal('Payment')
172
                         expect(env.hash).to.be.a('string')
185
                         expect(env.hash).to.be.a('string')
173
                         expect(env.inLedger).to.be.a('number')
186
                         expect(env.inLedger).to.be.a('number')
174
                         expect(env.ledger_index).to.be.a('number')
187
                         expect(env.ledger_index).to.be.a('number')
175
-
176
                         runnable.disconnect()
188
                         runnable.disconnect()
177
                         done()
189
                         done()
178
                     },
190
                     },
182
             }
194
             }
183
 
195
 
184
             const Rjsvm = RJSVM_Builder.from(RJSVM_Base, RJSVM_Contract)
196
             const Rjsvm = RJSVM_Builder.from(RJSVM_Base, RJSVM_Contract)
185
-            const runnable = new Rjsvm({listeningAddress: testWallet.address,rippleNode: xrpNode})
197
+            const runnable = new Rjsvm({ listeningAddress: testWallet.address, rippleNode: xrpNode })
186
             await runnable.connect()
198
             await runnable.connect()
187
 
199
 
188
             const dw = new Datawriter({
200
             const dw = new Datawriter({
197
     })
209
     })
198
 
210
 
199
     it('Called endpoint triggers in RJSVM', function (done) {
211
     it('Called endpoint triggers in RJSVM', function (done) {
200
-        this.timeout(30000)
212
+        this.timeout(45000)
201
 
213
 
202
         const data = { title: "1", body: "2", from: "3" };
214
         const data = { title: "1", body: "2", from: "3" };
203
         rjsvm.once('submit', (payload) => {
215
         rjsvm.once('submit', (payload) => {
209
     })
221
     })
210
 
222
 
211
     it('Calling an endpoint with insufficient fee fails', function (done) {
223
     it('Calling an endpoint with insufficient fee fails', function (done) {
212
-        this.timeout(30000)
224
+        this.timeout(45000)
213
 
225
 
214
         const data = { title: "f", body: "f", from: "f" };
226
         const data = { title: "f", body: "f", from: "f" };
215
         rjsvm.once('error', (err) => {
227
         rjsvm.once('error', (err) => {
220
     })
232
     })
221
 
233
 
222
     it('Restricted endpoint can be called by owner', function (done) {
234
     it('Restricted endpoint can be called by owner', function (done) {
223
-        this.timeout(30000)
235
+        this.timeout(45000)
224
 
236
 
225
         const data = { title: "11", body: "22", from: "33" };
237
         const data = { title: "11", body: "22", from: "33" };
226
         rjsvm.once('restricted', (payload) => {
238
         rjsvm.once('restricted', (payload) => {
233
 
245
 
234
 
246
 
235
     it('Restricted endpoint cannot be called by non-owner', function (done) {
247
     it('Restricted endpoint cannot be called by non-owner', function (done) {
236
-        this.timeout(30000)
248
+        this.timeout(45000)
237
 
249
 
238
         const data = { title: "e", body: "e", from: "e" };
250
         const data = { title: "e", body: "e", from: "e" };
239
         rjsvm.once('error', (err) => {
251
         rjsvm.once('error', (err) => {

+ 14
- 14
test/tools.ts View File

1
+import axios from 'axios';
2
+
1
 type Wallet = {secret: string, address:string }
3
 type Wallet = {secret: string, address:string }
2
 
4
 
3
-export const makeTestnetWallet = () : Promise<Wallet> => fetch('https://faucet.altnet.rippletest.net/accounts', {
4
-    method: 'POST',
5
-    headers: {
6
-        'Accept': 'application/json',
7
-        'Content-Type': 'application/json'
8
-    },
9
-}).then((raw:any) => {
10
-    return raw.json().then(content => {
11
-        return({
12
-            secret: content.account.secret,
13
-            address: content.account.address
14
-        });
15
-    })
16
-});
5
+export const makeTestnetWallet = async () : Promise<Wallet> => {
6
+    try{
7
+        const response = await axios.post('https://faucet.altnet.rippletest.net/accounts', {})
8
+        return ({
9
+            secret: response.data.seed,
10
+            address: response.data.account.address
11
+        })
12
+    
13
+    }catch(e){
14
+        console.log(e)
15
+    }
16
+}

Loading…
Cancel
Save