Skip to content

Instantly share code, notes, and snippets.

@WietseWind
Created September 18, 2024 10:40
Show Gist options
  • Save WietseWind/4c0475d39c57a02c4155daa5f36a725b to your computer and use it in GitHub Desktop.
Save WietseWind/4c0475d39c57a02c4155daa5f36a725b to your computer and use it in GitHub Desktop.
Create Escrow in incrementing instalments
import fetch from 'node-fetch'
import { XrplDefinitions, derive, sign } from 'xrpl-accountlib'
const now = new Date()
const Server = 'https://xahau-test.net' // can be testnet, xahau-test.net / xahau.network
const secret = '{sender secret}' // can be RK secret
const Account = derive.familySeed(secret)
Account.address = '{sender account r-address}'
const Destination = '{destination r-address}'
const XahAmount = 10_000 // in XAH
const Instalments = 24 // months
const StartAtInstalment = 0 // skip if already at some place
const ServerDefinitions = (await (await fetch(Server + '/server_definitions')).json()).result
const ServerInfo = (await (await fetch(Server + '/server_info')).json()).result
const AccountInfo = (await (await fetch(Server + '/account_info?account=' + Account.address)).json()).result
let iteration = 0
let AccountTxnID
let Sequence = AccountInfo.account_data.Sequence
const definitions = new XrplDefinitions(ServerDefinitions)
const submit = async signed => {
// For next tx
Sequence++
AccountTxnID = signed.id
const submitted = await (await fetch(Server, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
method: 'submit',
params: [ { tx_blob: signed.signedTransaction, } ]
})
})).json()
console.log(submitted)
return submitted
}
const releaseDates = Array(Instalments)
.fill()
.map((_, index) => new Date(now.getFullYear(), now.getMonth() + ++index, 1))
.map(r => Math.round(Number(r) / 1000) - 946684800)
if (typeof AccountInfo.account_data.AccountTxnID === 'undefined') {
// First set tracking of Account TX ID
const tx = {
TransactionType: 'AccountSet',
Fee: String(40),
SetFlag: 5, // asfAccountTxnID
Account: Account.address,
NetworkID: ServerInfo.info.network_id,
Sequence,
}
const signed = sign(tx, Account, definitions)
console.log(signed)
await submit(signed)
// First one not required
AccountTxnID = undefined
}
///
for await (const FinishAfter of releaseDates) {
iteration++
if (iteration > StartAtInstalment) {
const tx = {
TransactionType: 'EscrowCreate',
Account: Account.address,
Destination,
Fee: String(1337),
Amount: String(XahAmount * iteration * 1_000_000),
NetworkID: ServerInfo.info.network_id,
FinishAfter,
AccountTxnID,
Sequence,
}
console.log(tx.Amount)
// This makes it real
const signed = sign(tx, Account, definitions)
console.log(signed)
await submit(signed)
await new Promise(resolve => setTimeout(resolve, 4500))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment